httpServer example program for WIZwiki-W7500 Only LED control
Dependents: httpServer-WIZwiki-W7500
FsHandler.cpp
00001 /* FsHandler.cpp */ 00002 #include "mbed.h" 00003 #include "FsHandler.h" 00004 //#define DEBUG 00005 #include "hl_debug.h" 00006 00007 DigitalOut led_red(LED1); 00008 DigitalOut led_green(LED2); 00009 DigitalOut led_blue(LED3); 00010 00011 DigitalIn din(PC_14); 00012 00013 00014 static int matchstrings(const char* one, const char* two) 00015 { 00016 int m = 0; 00017 00018 for (m = 0; m < min(strlen(one), strlen(two)) ; m++) { 00019 if (one[m] != two[m]) 00020 return m; 00021 } 00022 return m; 00023 } 00024 00025 std::map<const char*, const char*> HTTPFsRequestHandler::m_fsMap; 00026 00027 HTTPFsRequestHandler::HTTPFsRequestHandler(const char* rootPath, const char* localPath, HTTPConnection::HTTPMessage& Msg, TCPSocketConnection& Tcp) 00028 : HTTPRequestHandler(Msg, Tcp) 00029 { 00030 m_rootPath = rootPath; 00031 m_localPath = localPath; 00032 00033 string myPath = m_rootPath + m_localPath; 00034 00035 // Now replace the virtual root path with a mounted device path 00036 std::map<const char*, const char*>::iterator it; 00037 const char *bestMatch = NULL; 00038 const char *bestMatchExchange = NULL; 00039 int match_ind = -1; 00040 for (it = m_fsMap.begin() ; it != m_fsMap.end() ; it++) { 00041 // find best match (if the given logical path is containted in the root 00042 int s = matchstrings(myPath.c_str(), it->second); 00043 INFO("Matching Root %s with handler %s results in %d identical characters\n", myPath.c_str(), it->second, s); 00044 if ((s == strlen(it->second)) && (s > match_ind)) { 00045 match_ind = s; 00046 bestMatch = it->first; 00047 bestMatchExchange = it->second; 00048 } 00049 } 00050 00051 if (bestMatch != NULL) { 00052 m_rootPath = bestMatch; 00053 m_localPath = string(myPath).substr(strlen(bestMatchExchange)); 00054 } 00055 00056 handleRequest(); 00057 } 00058 00059 HTTPFsRequestHandler::~HTTPFsRequestHandler() 00060 { 00061 } 00062 00063 int HTTPFsRequestHandler::handleGetRequest() 00064 { 00065 HTTPHeaders headers; 00066 int retval = 0; //success 00067 uint8_t pin_state; 00068 00069 if( std::string::npos != msg.uri.find("get_dio14.cgi") ) 00070 { 00071 if(din) 00072 pin_state = 1; 00073 else 00074 pin_state = 0; 00075 00076 /* 00077 *len = sprintf((char *)buf, "DioCallback({\"dio_p\":\"14\",\ 00078 \"dio_s\":\"%d\"\ 00079 });", 00080 pin_state // Digital io status 00081 ); 00082 00083 00084 Tcp. 00085 */ 00086 } 00087 else //read html pages 00088 { 00089 if (m_localPath.length() > 4) 00090 getStandardHeaders(headers, m_localPath.substr(m_localPath.length()-4).c_str()); 00091 else 00092 getStandardHeaders(headers); 00093 00094 INFO("Handling Get Request (root = %s, local = %s).", m_rootPath.c_str(), m_localPath.c_str()); 00095 00096 std::string reqPath; 00097 00098 // Check if we received a directory with the local bath 00099 if ((m_localPath.length() == 0) || (m_localPath.substr( m_localPath.length()-1, 1) == "/")) { 00100 // yes, we shall append the default page name 00101 m_localPath += "index.html"; 00102 } 00103 00104 reqPath = m_rootPath + m_localPath; 00105 00106 INFO("Mapping \"%s\" to \"%s\"", msg.uri.c_str(), reqPath.c_str()); 00107 00108 FILE *fp = fopen(reqPath.c_str(), "r"); 00109 if (fp != NULL) { 00110 char * pBuffer = NULL; 00111 int sz = 8192; 00112 while( pBuffer == NULL) { 00113 sz /= 2; 00114 pBuffer = (char*)malloc(sz); 00115 if (sz < 128) 00116 error ("OutOfMemory"); 00117 } 00118 00119 // File was found and can be returned 00120 00121 // first determine the size 00122 fseek(fp, 0, SEEK_END); 00123 long size = ftell(fp); 00124 fseek(fp, 0, SEEK_SET); 00125 00126 startResponse(200, size); 00127 while(!feof(fp) && !ferror(fp)) { 00128 int cnt = fread(pBuffer, 1, sz , fp); 00129 if (cnt < 0) 00130 cnt = 0; 00131 processResponse(cnt, pBuffer); 00132 } 00133 delete pBuffer; 00134 endResponse(); 00135 fclose(fp); 00136 } 00137 else { 00138 retval = 404; 00139 ERR("Requested file was not found !"); 00140 } 00141 } 00142 00143 return retval; 00144 } 00145 00146 int HTTPFsRequestHandler::handlePostRequest() 00147 { 00148 00149 int pin = 0; 00150 00151 if( std::string::npos != msg.uri.find("set_dio.cgi") ) 00152 { 00153 pin = get_http_param_value("pin"); 00154 if(pin == 8) 00155 { 00156 led_red = get_http_param_value("val"); 00157 } 00158 else if(pin == 9) 00159 { 00160 led_green = get_http_param_value("val"); 00161 } 00162 else if(pin == 5) 00163 { 00164 led_blue = get_http_param_value("val"); 00165 } 00166 else 00167 { 00168 WARN("Wrong pin number"); 00169 } 00170 00171 return 0; 00172 } 00173 else 00174 { 00175 return 404; 00176 } 00177 } 00178 00179 int HTTPFsRequestHandler::handlePutRequest() 00180 { 00181 return 404; 00182 } 00183 00184 uint32_t HTTPFsRequestHandler::get_http_param_value(char* param_name) 00185 { 00186 uint8_t * name = 0; 00187 uint8_t * pos2; 00188 uint16_t len = 0; 00189 char value[10]; 00190 uint32_t ret = 0; 00191 00192 //msg.attri 00193 if((name = (uint8_t *)strstr(msg.attri, param_name))) 00194 { 00195 name += strlen(param_name) + 1; 00196 pos2 = (uint8_t*)strstr((char*)name, "&"); 00197 if(!pos2) 00198 { 00199 pos2 = name + strlen((char*)name); 00200 } 00201 len = pos2 - name; 00202 00203 if(len) 00204 { 00205 strncpy(value, (char*)name, len); 00206 ret = atoi(value); 00207 } 00208 } 00209 return ret; 00210 } 00211
Generated on Wed Jul 13 2022 08:57:26 by 1.7.2