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.
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 int lumi_val = 1; 00008 int cel_val = 1; 00009 int touch_val = 1; 00010 int control = 1; 00011 00012 DigitalIn din(PC_14); 00013 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 int HTTPFsRequestHandler::handleGetRequest() 00065 { 00066 HTTPHeaders headers; 00067 int retval = 0; //success 00068 uint8_t pin_state; 00069 00070 if( std::string::npos != msg.uri.find("get_dio14.cgi") ) 00071 { 00072 if(din) 00073 pin_state = 1; 00074 else 00075 pin_state = 0; 00076 00077 /* 00078 *len = sprintf((char *)buf, "DioCallback({\"dio_p\":\"14\",\ 00079 \"dio_s\":\"%d\"\ 00080 });", 00081 pin_state // Digital io status 00082 ); 00083 00084 00085 Tcp. 00086 */ 00087 } 00088 else //read html pages 00089 { 00090 if (m_localPath.length() > 4) 00091 getStandardHeaders(headers, m_localPath.substr(m_localPath.length()-4).c_str()); 00092 else 00093 getStandardHeaders(headers); 00094 00095 INFO("Handling Get Request (root = %s, local = %s).", m_rootPath.c_str(), m_localPath.c_str()); 00096 00097 std::string reqPath; 00098 00099 // Check if we received a directory with the local bath 00100 if ((m_localPath.length() == 0) || (m_localPath.substr( m_localPath.length()-1, 1) == "/")) { 00101 // yes, we shall append the default page name 00102 m_localPath += "index.html"; 00103 } 00104 00105 reqPath = m_rootPath + m_localPath; 00106 00107 INFO("Mapping \"%s\" to \"%s\"", msg.uri.c_str(), reqPath.c_str()); 00108 00109 FILE *fp = fopen(reqPath.c_str(), "r"); 00110 if (fp != NULL) { 00111 char * pBuffer = NULL; 00112 int sz = 8192; 00113 while( pBuffer == NULL) { 00114 sz /= 2; 00115 pBuffer = (char*)malloc(sz); 00116 if (sz < 128) 00117 error ("OutOfMemory"); 00118 } 00119 00120 // File was found and can be returned 00121 00122 // first determine the size 00123 fseek(fp, 0, SEEK_END); 00124 long size = ftell(fp); 00125 fseek(fp, 0, SEEK_SET); 00126 00127 startResponse(200, size); 00128 while(!feof(fp) && !ferror(fp)) { 00129 int cnt = fread(pBuffer, 1, sz , fp); 00130 if (cnt < 0) 00131 cnt = 0; 00132 processResponse(cnt, pBuffer); 00133 } 00134 delete pBuffer; 00135 endResponse(); 00136 fclose(fp); 00137 } 00138 else { 00139 retval = 404; 00140 ERR("Requested file was not found !"); 00141 } 00142 } 00143 00144 return retval; 00145 } 00146 00147 int HTTPFsRequestHandler::handlePostRequest() 00148 { 00149 00150 int pin = 0; 00151 00152 if( std::string::npos != msg.uri.find("set_dio.cgi") ) 00153 { 00154 pin = get_http_param_value("pin"); 00155 if(pin == 8) 00156 { 00157 lumi_val = get_http_param_value("val"); 00158 } 00159 else if(pin == 9) 00160 { 00161 cel_val = get_http_param_value("val"); 00162 } 00163 else if(pin == 5) 00164 { 00165 touch_val = get_http_param_value("val"); 00166 } 00167 else if(pin == 0) 00168 { 00169 control = get_http_param_value("val"); 00170 } 00171 else 00172 { 00173 WARN("Wrong pin number"); 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
Generated on Wed Jul 13 2022 01:39:15 by
1.7.2
