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: STM32F407VET6_HTTPServer
FsHandler.cpp
00001 /* FsHandler.cpp */ 00002 #include "mbed.h" 00003 #include "FsHandler.h" 00004 #define DEBUG 00005 #include "hl_debug.h" 00006 00007 DigitalOut led1(LED1); 00008 00009 static int matchstrings(const char* one, const char* two) 00010 { 00011 int m = 0; 00012 00013 for (m = 0; m < min(strlen(one), strlen(two)) ; m++) { 00014 if (one[m] != two[m]) 00015 return m; 00016 } 00017 return m; 00018 } 00019 00020 std::map<const char*, const char*> HTTPFsRequestHandler::m_fsMap; 00021 00022 HTTPFsRequestHandler::HTTPFsRequestHandler(const char* rootPath, const char* localPath, HTTPConnection::HTTPMessage& Msg, TCPSocket* Tcp) 00023 : HTTPRequestHandler(Msg, Tcp) 00024 { 00025 INFO("Request handler called."); 00026 m_rootPath = rootPath; 00027 m_localPath = localPath; 00028 00029 string myPath = m_rootPath + m_localPath; 00030 00031 // Now replace the virtual root path with a mounted device path 00032 std::map<const char*, const char*>::iterator it; 00033 const char *bestMatch = NULL; 00034 const char *bestMatchExchange = NULL; 00035 int match_ind = -1; 00036 for (it = m_fsMap.begin() ; it != m_fsMap.end() ; it++) { 00037 // find best match (if the given logical path is containted in the root 00038 int s = matchstrings(myPath.c_str(), it->second); 00039 INFO("Matching Root %s with handler %s results in %d identical characters\n", myPath.c_str(), it->second, s); 00040 if ((s == strlen(it->second)) && (s > match_ind)) { 00041 match_ind = s; 00042 bestMatch = it->first; 00043 bestMatchExchange = it->second; 00044 } 00045 } 00046 00047 if (bestMatch != NULL) { 00048 m_rootPath = bestMatch; 00049 m_localPath = string(myPath).substr(strlen(bestMatchExchange)); 00050 } 00051 00052 handleRequest(); 00053 } 00054 00055 HTTPFsRequestHandler::~HTTPFsRequestHandler() 00056 { 00057 } 00058 00059 int HTTPFsRequestHandler::handleGetRequest() 00060 { 00061 HTTPHeaders headers; 00062 int retval = 0; //success 00063 00064 if (m_localPath.length() > 4) 00065 getStandardHeaders(headers, m_localPath.substr(m_localPath.length()-4).c_str()); 00066 else 00067 getStandardHeaders(headers); 00068 00069 INFO("Handling Get Request (root = %s, local = %s).", m_rootPath.c_str(), m_localPath.c_str()); 00070 00071 std::string reqPath; 00072 00073 // Check if we received a directory with the local bath 00074 if ((m_localPath.length() == 0) || (m_localPath.substr( m_localPath.length()-1, 1) == "/")) { 00075 // yes, we shall append the default page name 00076 m_localPath += "index.html"; 00077 } 00078 00079 reqPath = m_rootPath + m_localPath; 00080 00081 INFO("Mapping \"%s\" to \"%s\"", msg.uri.c_str(), reqPath.c_str()); 00082 00083 FILE *fp = fopen(reqPath.c_str(), "r"); 00084 INFO("fopen(reqPath.c_str()"); 00085 if (fp != NULL) { 00086 INFO("fp != NULL"); 00087 char * pBuffer = NULL; 00088 int sz = 8192; 00089 while( pBuffer == NULL) { 00090 sz /= 2; 00091 pBuffer = (char*)malloc(sz); 00092 if (sz < 128) 00093 error ("OutOfMemory"); 00094 } 00095 00096 // File was found and can be returned 00097 INFO("File was found and can be returned"); 00098 00099 // first determine the size 00100 fseek(fp, 0, SEEK_END); 00101 long size = ftell(fp); 00102 fseek(fp, 0, SEEK_SET); 00103 00104 startResponse(200, size); 00105 while(!feof(fp) && !ferror(fp)) { 00106 int cnt = fread(pBuffer, 1, sz , fp); 00107 if (cnt < 0) 00108 cnt = 0; 00109 processResponse(cnt, pBuffer); 00110 } 00111 delete pBuffer; 00112 endResponse(); 00113 fclose(fp); 00114 } 00115 else { 00116 retval = 404; 00117 ERR("Requested file was not found !"); 00118 } 00119 00120 return retval; 00121 } 00122 00123 int HTTPFsRequestHandler::handlePostRequest() 00124 { 00125 INFO("Handling Post Request (msg.uri = %s).", msg.uri.c_str()); 00126 00127 if( std::string::npos != msg.uri.find("/toggle") ) 00128 { 00129 led1 = !led1; 00130 return 0; 00131 } 00132 else 00133 { 00134 return 404; 00135 } 00136 } 00137 00138 int HTTPFsRequestHandler::handlePutRequest() 00139 { 00140 return 404; 00141 } 00142 00143 uint32_t HTTPFsRequestHandler::get_http_param_value(char* param_name) 00144 { 00145 uint8_t * name = 0; 00146 uint8_t * pos2; 00147 uint16_t len = 0; 00148 char value[10]; 00149 uint32_t ret = 0; 00150 00151 //msg.attri 00152 if((name = (uint8_t *)strstr(msg.attri, param_name))) 00153 { 00154 name += strlen(param_name) + 1; 00155 pos2 = (uint8_t*)strstr((char*)name, "&"); 00156 if(!pos2) 00157 { 00158 pos2 = name + strlen((char*)name); 00159 } 00160 len = pos2 - name; 00161 00162 if(len) 00163 { 00164 strncpy(value, (char*)name, len); 00165 ret = atoi(value); 00166 } 00167 } 00168 return ret; 00169 } 00170
Generated on Wed Jul 13 2022 12:04:08 by
1.7.2