HTTP Server library for Mbed OS-5. A fork of Henry Leinen's [[https://os.mbed.com/users/leihen/code/HTTPServer/]] library.
Dependents: STM32F407VET6_HTTPServer
Handler/FsHandler.cpp@17:8bcc62289a07, 2019-10-06 (annotated)
- Committer:
- hudakz
- Date:
- Sun Oct 06 18:10:41 2019 +0000
- Revision:
- 17:8bcc62289a07
- Parent:
- 16:cc3f5c53d0d5
HTTP Server library for Mbed OS-5. A fork of Henry Leine's [[https://os.mbed.com/users/leihen/code/HTTPServer | HTTPServer]] library.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
leihen | 5:dc88012caef1 | 1 | /* FsHandler.cpp */ |
leihen | 5:dc88012caef1 | 2 | #include "mbed.h" |
leihen | 5:dc88012caef1 | 3 | #include "FsHandler.h" |
leihen | 13:aa5338a5e452 | 4 | #define DEBUG |
leihen | 16:cc3f5c53d0d5 | 5 | #include "hl_debug.h" |
leihen | 5:dc88012caef1 | 6 | |
hudakz | 17:8bcc62289a07 | 7 | DigitalOut led1(LED1); |
leihen | 5:dc88012caef1 | 8 | |
leihen | 12:ba81cc117fb6 | 9 | static int matchstrings(const char* one, const char* two) |
leihen | 12:ba81cc117fb6 | 10 | { |
leihen | 12:ba81cc117fb6 | 11 | int m = 0; |
leihen | 12:ba81cc117fb6 | 12 | |
leihen | 12:ba81cc117fb6 | 13 | for (m = 0; m < min(strlen(one), strlen(two)) ; m++) { |
leihen | 12:ba81cc117fb6 | 14 | if (one[m] != two[m]) |
leihen | 12:ba81cc117fb6 | 15 | return m; |
leihen | 12:ba81cc117fb6 | 16 | } |
leihen | 12:ba81cc117fb6 | 17 | return m; |
leihen | 12:ba81cc117fb6 | 18 | } |
leihen | 5:dc88012caef1 | 19 | |
leihen | 5:dc88012caef1 | 20 | std::map<const char*, const char*> HTTPFsRequestHandler::m_fsMap; |
leihen | 5:dc88012caef1 | 21 | |
hudakz | 17:8bcc62289a07 | 22 | HTTPFsRequestHandler::HTTPFsRequestHandler(const char* rootPath, const char* localPath, HTTPConnection::HTTPMessage& Msg, TCPSocket* Tcp) |
leihen | 5:dc88012caef1 | 23 | : HTTPRequestHandler(Msg, Tcp) |
leihen | 5:dc88012caef1 | 24 | { |
hudakz | 17:8bcc62289a07 | 25 | INFO("Request handler called."); |
leihen | 5:dc88012caef1 | 26 | m_rootPath = rootPath; |
leihen | 5:dc88012caef1 | 27 | m_localPath = localPath; |
leihen | 5:dc88012caef1 | 28 | |
leihen | 12:ba81cc117fb6 | 29 | string myPath = m_rootPath + m_localPath; |
leihen | 12:ba81cc117fb6 | 30 | |
leihen | 5:dc88012caef1 | 31 | // Now replace the virtual root path with a mounted device path |
leihen | 5:dc88012caef1 | 32 | std::map<const char*, const char*>::iterator it; |
leihen | 12:ba81cc117fb6 | 33 | const char *bestMatch = NULL; |
leihen | 12:ba81cc117fb6 | 34 | const char *bestMatchExchange = NULL; |
leihen | 12:ba81cc117fb6 | 35 | int match_ind = -1; |
leihen | 5:dc88012caef1 | 36 | for (it = m_fsMap.begin() ; it != m_fsMap.end() ; it++) { |
leihen | 5:dc88012caef1 | 37 | // find best match (if the given logical path is containted in the root |
leihen | 12:ba81cc117fb6 | 38 | int s = matchstrings(myPath.c_str(), it->second); |
leihen | 12:ba81cc117fb6 | 39 | INFO("Matching Root %s with handler %s results in %d identical characters\n", myPath.c_str(), it->second, s); |
leihen | 12:ba81cc117fb6 | 40 | if ((s == strlen(it->second)) && (s > match_ind)) { |
leihen | 12:ba81cc117fb6 | 41 | match_ind = s; |
leihen | 12:ba81cc117fb6 | 42 | bestMatch = it->first; |
leihen | 12:ba81cc117fb6 | 43 | bestMatchExchange = it->second; |
leihen | 5:dc88012caef1 | 44 | } |
leihen | 5:dc88012caef1 | 45 | } |
leihen | 12:ba81cc117fb6 | 46 | |
leihen | 12:ba81cc117fb6 | 47 | if (bestMatch != NULL) { |
leihen | 12:ba81cc117fb6 | 48 | m_rootPath = bestMatch; |
leihen | 12:ba81cc117fb6 | 49 | m_localPath = string(myPath).substr(strlen(bestMatchExchange)); |
leihen | 12:ba81cc117fb6 | 50 | } |
leihen | 12:ba81cc117fb6 | 51 | |
leihen | 5:dc88012caef1 | 52 | handleRequest(); |
leihen | 5:dc88012caef1 | 53 | } |
leihen | 5:dc88012caef1 | 54 | |
leihen | 5:dc88012caef1 | 55 | HTTPFsRequestHandler::~HTTPFsRequestHandler() |
leihen | 5:dc88012caef1 | 56 | { |
leihen | 5:dc88012caef1 | 57 | } |
leihen | 5:dc88012caef1 | 58 | |
leihen | 5:dc88012caef1 | 59 | int HTTPFsRequestHandler::handleGetRequest() |
leihen | 5:dc88012caef1 | 60 | { |
leihen | 12:ba81cc117fb6 | 61 | HTTPHeaders headers; |
hudakz | 17:8bcc62289a07 | 62 | int retval = 0; //success |
leihen | 12:ba81cc117fb6 | 63 | |
leihen | 12:ba81cc117fb6 | 64 | if (m_localPath.length() > 4) |
leihen | 12:ba81cc117fb6 | 65 | getStandardHeaders(headers, m_localPath.substr(m_localPath.length()-4).c_str()); |
leihen | 12:ba81cc117fb6 | 66 | else |
leihen | 12:ba81cc117fb6 | 67 | getStandardHeaders(headers); |
leihen | 12:ba81cc117fb6 | 68 | |
leihen | 12:ba81cc117fb6 | 69 | INFO("Handling Get Request (root = %s, local = %s).", m_rootPath.c_str(), m_localPath.c_str()); |
hudakz | 17:8bcc62289a07 | 70 | |
leihen | 5:dc88012caef1 | 71 | std::string reqPath; |
leihen | 5:dc88012caef1 | 72 | |
leihen | 5:dc88012caef1 | 73 | // Check if we received a directory with the local bath |
leihen | 5:dc88012caef1 | 74 | if ((m_localPath.length() == 0) || (m_localPath.substr( m_localPath.length()-1, 1) == "/")) { |
leihen | 5:dc88012caef1 | 75 | // yes, we shall append the default page name |
leihen | 5:dc88012caef1 | 76 | m_localPath += "index.html"; |
leihen | 5:dc88012caef1 | 77 | } |
leihen | 5:dc88012caef1 | 78 | |
leihen | 5:dc88012caef1 | 79 | reqPath = m_rootPath + m_localPath; |
leihen | 5:dc88012caef1 | 80 | |
leihen | 5:dc88012caef1 | 81 | INFO("Mapping \"%s\" to \"%s\"", msg.uri.c_str(), reqPath.c_str()); |
leihen | 5:dc88012caef1 | 82 | |
leihen | 5:dc88012caef1 | 83 | FILE *fp = fopen(reqPath.c_str(), "r"); |
hudakz | 17:8bcc62289a07 | 84 | INFO("fopen(reqPath.c_str()"); |
leihen | 5:dc88012caef1 | 85 | if (fp != NULL) { |
hudakz | 17:8bcc62289a07 | 86 | INFO("fp != NULL"); |
leihen | 12:ba81cc117fb6 | 87 | char * pBuffer = NULL; |
leihen | 12:ba81cc117fb6 | 88 | int sz = 8192; |
leihen | 12:ba81cc117fb6 | 89 | while( pBuffer == NULL) { |
leihen | 12:ba81cc117fb6 | 90 | sz /= 2; |
leihen | 12:ba81cc117fb6 | 91 | pBuffer = (char*)malloc(sz); |
leihen | 12:ba81cc117fb6 | 92 | if (sz < 128) |
leihen | 12:ba81cc117fb6 | 93 | error ("OutOfMemory"); |
leihen | 12:ba81cc117fb6 | 94 | } |
leihen | 12:ba81cc117fb6 | 95 | |
leihen | 5:dc88012caef1 | 96 | // File was found and can be returned |
hudakz | 17:8bcc62289a07 | 97 | INFO("File was found and can be returned"); |
leihen | 5:dc88012caef1 | 98 | |
leihen | 5:dc88012caef1 | 99 | // first determine the size |
leihen | 5:dc88012caef1 | 100 | fseek(fp, 0, SEEK_END); |
leihen | 5:dc88012caef1 | 101 | long size = ftell(fp); |
leihen | 5:dc88012caef1 | 102 | fseek(fp, 0, SEEK_SET); |
leihen | 5:dc88012caef1 | 103 | |
leihen | 5:dc88012caef1 | 104 | startResponse(200, size); |
leihen | 5:dc88012caef1 | 105 | while(!feof(fp) && !ferror(fp)) { |
leihen | 12:ba81cc117fb6 | 106 | int cnt = fread(pBuffer, 1, sz , fp); |
leihen | 7:cb7fec1265b5 | 107 | if (cnt < 0) |
leihen | 7:cb7fec1265b5 | 108 | cnt = 0; |
leihen | 12:ba81cc117fb6 | 109 | processResponse(cnt, pBuffer); |
leihen | 5:dc88012caef1 | 110 | } |
leihen | 12:ba81cc117fb6 | 111 | delete pBuffer; |
leihen | 5:dc88012caef1 | 112 | endResponse(); |
leihen | 5:dc88012caef1 | 113 | fclose(fp); |
leihen | 5:dc88012caef1 | 114 | } |
leihen | 5:dc88012caef1 | 115 | else { |
leihen | 5:dc88012caef1 | 116 | retval = 404; |
leihen | 5:dc88012caef1 | 117 | ERR("Requested file was not found !"); |
leihen | 5:dc88012caef1 | 118 | } |
leihen | 5:dc88012caef1 | 119 | |
leihen | 5:dc88012caef1 | 120 | return retval; |
leihen | 5:dc88012caef1 | 121 | } |
leihen | 5:dc88012caef1 | 122 | |
leihen | 5:dc88012caef1 | 123 | int HTTPFsRequestHandler::handlePostRequest() |
hudakz | 17:8bcc62289a07 | 124 | { |
hudakz | 17:8bcc62289a07 | 125 | INFO("Handling Post Request (msg.uri = %s).", msg.uri.c_str()); |
hudakz | 17:8bcc62289a07 | 126 | |
hudakz | 17:8bcc62289a07 | 127 | if( std::string::npos != msg.uri.find("/toggle") ) |
hudakz | 17:8bcc62289a07 | 128 | { |
hudakz | 17:8bcc62289a07 | 129 | led1 = !led1; |
hudakz | 17:8bcc62289a07 | 130 | return 0; |
hudakz | 17:8bcc62289a07 | 131 | } |
hudakz | 17:8bcc62289a07 | 132 | else |
hudakz | 17:8bcc62289a07 | 133 | { |
hudakz | 17:8bcc62289a07 | 134 | return 404; |
hudakz | 17:8bcc62289a07 | 135 | } |
leihen | 5:dc88012caef1 | 136 | } |
leihen | 5:dc88012caef1 | 137 | |
leihen | 5:dc88012caef1 | 138 | int HTTPFsRequestHandler::handlePutRequest() |
leihen | 5:dc88012caef1 | 139 | { |
leihen | 5:dc88012caef1 | 140 | return 404; |
hudakz | 17:8bcc62289a07 | 141 | } |
hudakz | 17:8bcc62289a07 | 142 | |
hudakz | 17:8bcc62289a07 | 143 | uint32_t HTTPFsRequestHandler::get_http_param_value(char* param_name) |
hudakz | 17:8bcc62289a07 | 144 | { |
hudakz | 17:8bcc62289a07 | 145 | uint8_t * name = 0; |
hudakz | 17:8bcc62289a07 | 146 | uint8_t * pos2; |
hudakz | 17:8bcc62289a07 | 147 | uint16_t len = 0; |
hudakz | 17:8bcc62289a07 | 148 | char value[10]; |
hudakz | 17:8bcc62289a07 | 149 | uint32_t ret = 0; |
hudakz | 17:8bcc62289a07 | 150 | |
hudakz | 17:8bcc62289a07 | 151 | //msg.attri |
hudakz | 17:8bcc62289a07 | 152 | if((name = (uint8_t *)strstr(msg.attri, param_name))) |
hudakz | 17:8bcc62289a07 | 153 | { |
hudakz | 17:8bcc62289a07 | 154 | name += strlen(param_name) + 1; |
hudakz | 17:8bcc62289a07 | 155 | pos2 = (uint8_t*)strstr((char*)name, "&"); |
hudakz | 17:8bcc62289a07 | 156 | if(!pos2) |
hudakz | 17:8bcc62289a07 | 157 | { |
hudakz | 17:8bcc62289a07 | 158 | pos2 = name + strlen((char*)name); |
hudakz | 17:8bcc62289a07 | 159 | } |
hudakz | 17:8bcc62289a07 | 160 | len = pos2 - name; |
hudakz | 17:8bcc62289a07 | 161 | |
hudakz | 17:8bcc62289a07 | 162 | if(len) |
hudakz | 17:8bcc62289a07 | 163 | { |
hudakz | 17:8bcc62289a07 | 164 | strncpy(value, (char*)name, len); |
hudakz | 17:8bcc62289a07 | 165 | ret = atoi(value); |
hudakz | 17:8bcc62289a07 | 166 | } |
hudakz | 17:8bcc62289a07 | 167 | } |
hudakz | 17:8bcc62289a07 | 168 | return ret; |
hudakz | 17:8bcc62289a07 | 169 | } |
hudakz | 17:8bcc62289a07 | 170 |