Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependents: Simple_HTTP_JSON HTTPWebServer-WIZwiki-W7500 HTTPWebServer-WIZwiki-W750023 WIZ750SR_test
Fork of httpServer by
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 AnalogIn ain0(A0); 00013 AnalogIn ain1(A1); 00014 00015 static int matchstrings(const char* one, const char* two) 00016 { 00017 int m = 0; 00018 00019 for (m = 0; m < min(strlen(one), strlen(two)) ; m++) { 00020 if (one[m] != two[m]) 00021 return m; 00022 } 00023 return m; 00024 } 00025 00026 std::map<const char*, const char*> HTTPFsRequestHandler::m_fsMap; 00027 00028 HTTPFsRequestHandler::HTTPFsRequestHandler(const char* rootPath, const char* localPath, HTTPConnection::HTTPMessage& Msg, TCPSocketConnection& Tcp) 00029 : HTTPRequestHandler(Msg, Tcp) 00030 { 00031 m_rootPath = rootPath; 00032 m_localPath = localPath; 00033 00034 string myPath = m_rootPath + m_localPath; 00035 00036 // Now replace the virtual root path with a mounted device path 00037 std::map<const char*, const char*>::iterator it; 00038 const char *bestMatch = NULL; 00039 const char *bestMatchExchange = NULL; 00040 int match_ind = -1; 00041 for (it = m_fsMap.begin() ; it != m_fsMap.end() ; it++) { 00042 // find best match (if the given logical path is containted in the root 00043 int s = matchstrings(myPath.c_str(), it->second); 00044 INFO("Matching Root %s with handler %s results in %d identical characters\n", myPath.c_str(), it->second, s); 00045 if ((s == strlen(it->second)) && (s > match_ind)) { 00046 match_ind = s; 00047 bestMatch = it->first; 00048 bestMatchExchange = it->second; 00049 } 00050 } 00051 00052 if (bestMatch != NULL) { 00053 m_rootPath = bestMatch; 00054 m_localPath = string(myPath).substr(strlen(bestMatchExchange)); 00055 } 00056 00057 handleRequest(); 00058 } 00059 00060 HTTPFsRequestHandler::~HTTPFsRequestHandler() 00061 { 00062 } 00063 00064 std::map<int, EthernetInterface*> HTTPFsRequestHandler::m_eth_list; 00065 00066 int HTTPFsRequestHandler::handleGetRequest() 00067 { 00068 HTTPHeaders headers; 00069 EthernetInterface test_eth; 00070 int retval = 0; //success 00071 00072 if( std::string::npos != msg.uri.find("get_netinfo.cgi") ) 00073 { 00074 char buf[256]; 00075 00076 sprintf(buf, "NetinfoCallback({\"mac\":\"%s\",\"ip\":\"%s\",\"sn\":\"%s\",\"gw\":\"%s\",\"temp\":\"%s\"});" 00077 ,m_eth_list[0]->getMACAddress(),m_eth_list[0]->getIPAddress(),m_eth_list[0]->getNetworkMask(),m_eth_list[0]->getGateway()); 00078 00079 tcp.send(buf, strlen(buf)); 00080 } 00081 00082 else if( std::string::npos != msg.uri.find("get_ain.cgi") ) 00083 { 00084 char buf[256]; 00085 00086 sprintf(buf, "AinCallback({\"ain_v0\":\"%.3f\",\"ain_v1\":\"%.3f\"});",ain0.read()*100, ain1.read()*100); 00087 00088 tcp.send(buf, strlen(buf)); 00089 } 00090 00091 else //read html pages 00092 { 00093 if (m_localPath.length() > 4) 00094 getStandardHeaders(headers, m_localPath.substr(m_localPath.length()-4).c_str()); 00095 else 00096 getStandardHeaders(headers); 00097 00098 INFO("Handling Get Request (root = %s, local = %s).", m_rootPath.c_str(), m_localPath.c_str()); 00099 00100 std::string reqPath; 00101 00102 // Check if we received a directory with the local bath 00103 if ((m_localPath.length() == 0) || (m_localPath.substr( m_localPath.length()-1, 1) == "/")) { 00104 // yes, we shall append the default page name 00105 m_localPath += "index.html"; 00106 } 00107 00108 reqPath = m_rootPath + m_localPath; 00109 00110 INFO("Mapping \"%s\" to \"%s\"", msg.uri.c_str(), reqPath.c_str()); 00111 00112 FILE *fp = fopen(reqPath.c_str(), "r"); 00113 if (fp != NULL) { 00114 char * pBuffer = NULL; 00115 int sz = 8192; 00116 while( pBuffer == NULL) { 00117 sz /= 2; 00118 pBuffer = (char*)malloc(sz); 00119 if (sz < 128) 00120 error ("OutOfMemory"); 00121 } 00122 00123 // File was found and can be returned 00124 00125 // first determine the size 00126 fseek(fp, 0, SEEK_END); 00127 long size = ftell(fp); 00128 fseek(fp, 0, SEEK_SET); 00129 00130 startResponse(200, size); 00131 while(!feof(fp) && !ferror(fp)) { 00132 int cnt = fread(pBuffer, 1, sz , fp); 00133 if (cnt < 0) 00134 cnt = 0; 00135 processResponse(cnt, pBuffer); 00136 } 00137 delete pBuffer; 00138 endResponse(); 00139 fclose(fp); 00140 } 00141 else { 00142 retval = 404; 00143 ERR("Requested file was not found !"); 00144 } 00145 } 00146 00147 return retval; 00148 } 00149 00150 int HTTPFsRequestHandler::handlePostRequest() 00151 { 00152 00153 int pin = 0; 00154 00155 if( std::string::npos != msg.uri.find("set_dio.cgi") ) 00156 { 00157 pin = get_http_param_value("pin"); 00158 if(pin == 8) 00159 { 00160 led_red = get_http_param_value("val"); 00161 } 00162 else if(pin == 9) 00163 { 00164 led_green = get_http_param_value("val"); 00165 } 00166 else if(pin == 5) 00167 { 00168 led_blue = get_http_param_value("val"); 00169 } 00170 else 00171 { 00172 WARN("Wrong pin number"); 00173 } 00174 00175 return 0; 00176 } 00177 else 00178 { 00179 return 404; 00180 } 00181 } 00182 00183 int HTTPFsRequestHandler::handlePutRequest() 00184 { 00185 return 404; 00186 } 00187 00188 uint32_t HTTPFsRequestHandler::get_http_param_value(char* param_name) 00189 { 00190 uint8_t * name = 0; 00191 uint8_t * pos2; 00192 uint16_t len = 0; 00193 char value[10]; 00194 uint32_t ret = 0; 00195 00196 //msg.attri 00197 if((name = (uint8_t *)strstr(msg.attri, param_name))) 00198 { 00199 name += strlen(param_name) + 1; 00200 pos2 = (uint8_t*)strstr((char*)name, "&"); 00201 if(!pos2) 00202 { 00203 pos2 = name + strlen((char*)name); 00204 } 00205 len = pos2 - name; 00206 00207 if(len) 00208 { 00209 strncpy(value, (char*)name, len); 00210 ret = atoi(value); 00211 } 00212 } 00213 return ret; 00214 } 00215 00216
Generated on Wed Jul 13 2022 18:39:02 by
1.7.2
