Adaptation of the HttpServer by user yueee_yt. This version has improved handling of the HTTP headers (**NOTE**: There are limitations with this implementation and it is not fully functional. Use it only as a starting point.)
Dependents: DMSupport DMSupport DMSupport DMSupport
Fork of DM_HttpServer by
HTTPRequestHandler.cpp@4:1b6b021ee21d, 2014-02-21 (annotated)
- Committer:
- yueee_yt
- Date:
- Fri Feb 21 07:10:30 2014 +0000
- Revision:
- 4:1b6b021ee21d
- Parent:
- 0:fdf9c2c5200f
- Child:
- 6:7b3320c34654
BugFix (JavaScript RPC Support (Not Cookbook js))
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
yueee_yt | 0:fdf9c2c5200f | 1 | /* |
yueee_yt | 0:fdf9c2c5200f | 2 | Permission is hereby granted, free of charge, to any person obtaining a copy |
yueee_yt | 0:fdf9c2c5200f | 3 | of this software and associated documentation files (the "Software"), to deal |
yueee_yt | 0:fdf9c2c5200f | 4 | in the Software without restriction, including without limitation the rights |
yueee_yt | 0:fdf9c2c5200f | 5 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
yueee_yt | 0:fdf9c2c5200f | 6 | copies of the Software, and to permit persons to whom the Software is |
yueee_yt | 0:fdf9c2c5200f | 7 | furnished to do so, subject to the following conditions: |
yueee_yt | 0:fdf9c2c5200f | 8 | |
yueee_yt | 0:fdf9c2c5200f | 9 | The above copyright notice and this permission notice shall be included in |
yueee_yt | 0:fdf9c2c5200f | 10 | all copies or substantial portions of the Software. |
yueee_yt | 0:fdf9c2c5200f | 11 | |
yueee_yt | 0:fdf9c2c5200f | 12 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
yueee_yt | 0:fdf9c2c5200f | 13 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
yueee_yt | 0:fdf9c2c5200f | 14 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
yueee_yt | 0:fdf9c2c5200f | 15 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
yueee_yt | 0:fdf9c2c5200f | 16 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
yueee_yt | 0:fdf9c2c5200f | 17 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
yueee_yt | 0:fdf9c2c5200f | 18 | THE SOFTWARE. |
yueee_yt | 0:fdf9c2c5200f | 19 | */ |
yueee_yt | 4:1b6b021ee21d | 20 | #ifdef _DEBUG_ALL |
yueee_yt | 4:1b6b021ee21d | 21 | #define _DEBUG_REQUEST_HANDLER |
yueee_yt | 4:1b6b021ee21d | 22 | #endif |
yueee_yt | 0:fdf9c2c5200f | 23 | |
yueee_yt | 0:fdf9c2c5200f | 24 | #include "HTTPRequestHandler.h" |
yueee_yt | 0:fdf9c2c5200f | 25 | |
yueee_yt | 0:fdf9c2c5200f | 26 | #include <string.h> |
yueee_yt | 0:fdf9c2c5200f | 27 | |
yueee_yt | 0:fdf9c2c5200f | 28 | #define HTTP_REQUEST_TIMEOUT 5000 |
yueee_yt | 0:fdf9c2c5200f | 29 | |
yueee_yt | 0:fdf9c2c5200f | 30 | //HTTPRequestHandler::HTTPRequestHandler(const char* rootPath, const char* path, TCPSocket* pTCPSocket) : NetService(), |
yueee_yt | 0:fdf9c2c5200f | 31 | // m_pTCPSocketConnection(pTCPSocketConnection), m_reqHeaders(), m_respHeaders(), |
yueee_yt | 0:fdf9c2c5200f | 32 | // m_rootPath(rootPath), m_path(path), m_errc(200), |
yueee_yt | 0:fdf9c2c5200f | 33 | // m_watchdog(), m_timeout(0),**/ m_closed(false), m_headersSent(false) //OK |
yueee_yt | 0:fdf9c2c5200f | 34 | HTTPRequestHandler::HTTPRequestHandler(const char* rootPath, const char* path, TCPSocketConnection* pTCPSocketConnection) : |
yueee_yt | 0:fdf9c2c5200f | 35 | m_pTCPSocketConnection(pTCPSocketConnection), m_reqHeaders(), m_respHeaders(), |
yueee_yt | 0:fdf9c2c5200f | 36 | m_rootPath(rootPath), m_path(path), m_errc(200), m_closed(false), m_headersSent(false) |
yueee_yt | 0:fdf9c2c5200f | 37 | { |
yueee_yt | 4:1b6b021ee21d | 38 | #ifdef _DEBUG_REQUEST_HANDLER |
yueee_yt | 0:fdf9c2c5200f | 39 | printf("+++(HTTPRequestHandler) init \r\n"); |
yueee_yt | 4:1b6b021ee21d | 40 | #endif |
yueee_yt | 0:fdf9c2c5200f | 41 | //Read & parse headers |
yueee_yt | 0:fdf9c2c5200f | 42 | readHeaders(); |
yueee_yt | 0:fdf9c2c5200f | 43 | //* m_pTCPSocket->setOnEvent(this, &HTTPRequestHandler::onTCPSocketEvent); |
yueee_yt | 0:fdf9c2c5200f | 44 | //* setTimeout(HTTP_REQUEST_TIMEOUT); |
yueee_yt | 4:1b6b021ee21d | 45 | #ifdef _DEBUG_REQUEST_HANDLER |
yueee_yt | 0:fdf9c2c5200f | 46 | printf("+++(HTTPRequestHandler) init end \r\n"); |
yueee_yt | 4:1b6b021ee21d | 47 | #endif |
yueee_yt | 0:fdf9c2c5200f | 48 | } |
yueee_yt | 0:fdf9c2c5200f | 49 | |
yueee_yt | 0:fdf9c2c5200f | 50 | HTTPRequestHandler::~HTTPRequestHandler() |
yueee_yt | 0:fdf9c2c5200f | 51 | { |
yueee_yt | 0:fdf9c2c5200f | 52 | close(); |
yueee_yt | 4:1b6b021ee21d | 53 | #ifdef _DEBUG_REQUEST_HANDLER |
yueee_yt | 0:fdf9c2c5200f | 54 | printf("+++(HTTPRequestHandler) Destroy end\r\n"); |
yueee_yt | 4:1b6b021ee21d | 55 | #endif |
yueee_yt | 0:fdf9c2c5200f | 56 | } |
yueee_yt | 0:fdf9c2c5200f | 57 | |
yueee_yt | 0:fdf9c2c5200f | 58 | void HTTPRequestHandler::onTimeout() //Connection has timed out |
yueee_yt | 0:fdf9c2c5200f | 59 | { |
yueee_yt | 0:fdf9c2c5200f | 60 | close(); |
yueee_yt | 0:fdf9c2c5200f | 61 | } |
yueee_yt | 0:fdf9c2c5200f | 62 | |
yueee_yt | 0:fdf9c2c5200f | 63 | void HTTPRequestHandler::close() //Close socket and destroy data |
yueee_yt | 0:fdf9c2c5200f | 64 | { |
yueee_yt | 0:fdf9c2c5200f | 65 | if(m_closed) |
yueee_yt | 0:fdf9c2c5200f | 66 | return; |
yueee_yt | 0:fdf9c2c5200f | 67 | m_closed = true; //Prevent recursive calling or calling on an object being destructed by someone else |
yueee_yt | 0:fdf9c2c5200f | 68 | /** m_watchdog.detach(); **/ |
yueee_yt | 0:fdf9c2c5200f | 69 | //* onClose(); |
yueee_yt | 0:fdf9c2c5200f | 70 | //* m_pTCPSocket->resetOnEvent(); |
yueee_yt | 0:fdf9c2c5200f | 71 | //* m_pTCPSocket->close(); |
yueee_yt | 0:fdf9c2c5200f | 72 | //* delete m_pTCPSocket; //Can safely destroy socket |
yueee_yt | 0:fdf9c2c5200f | 73 | //* NetService::close(); |
yueee_yt | 0:fdf9c2c5200f | 74 | } |
yueee_yt | 0:fdf9c2c5200f | 75 | |
yueee_yt | 0:fdf9c2c5200f | 76 | map<string, string>& HTTPRequestHandler::reqHeaders() //const |
yueee_yt | 0:fdf9c2c5200f | 77 | { |
yueee_yt | 0:fdf9c2c5200f | 78 | return m_reqHeaders; |
yueee_yt | 0:fdf9c2c5200f | 79 | } |
yueee_yt | 0:fdf9c2c5200f | 80 | |
yueee_yt | 0:fdf9c2c5200f | 81 | string& HTTPRequestHandler::path() //const |
yueee_yt | 0:fdf9c2c5200f | 82 | { |
yueee_yt | 0:fdf9c2c5200f | 83 | return m_path; |
yueee_yt | 0:fdf9c2c5200f | 84 | } |
yueee_yt | 0:fdf9c2c5200f | 85 | |
yueee_yt | 0:fdf9c2c5200f | 86 | int HTTPRequestHandler::dataLen() const |
yueee_yt | 0:fdf9c2c5200f | 87 | { |
yueee_yt | 0:fdf9c2c5200f | 88 | map<string,string>::iterator it; |
yueee_yt | 0:fdf9c2c5200f | 89 | it = m_reqHeaders.find("Content-Length"); |
yueee_yt | 0:fdf9c2c5200f | 90 | if( it == m_reqHeaders.end() ) { |
yueee_yt | 0:fdf9c2c5200f | 91 | return 0; |
yueee_yt | 0:fdf9c2c5200f | 92 | } |
yueee_yt | 0:fdf9c2c5200f | 93 | return atoi((*it).second.c_str()); //return 0 if parse fails, so that's fine |
yueee_yt | 0:fdf9c2c5200f | 94 | } |
yueee_yt | 0:fdf9c2c5200f | 95 | |
yueee_yt | 0:fdf9c2c5200f | 96 | int HTTPRequestHandler::readData(char* buf, int len) |
yueee_yt | 0:fdf9c2c5200f | 97 | { |
yueee_yt | 0:fdf9c2c5200f | 98 | return m_pTCPSocketConnection->receive(buf, len); |
yueee_yt | 0:fdf9c2c5200f | 99 | } |
yueee_yt | 0:fdf9c2c5200f | 100 | |
yueee_yt | 0:fdf9c2c5200f | 101 | string& HTTPRequestHandler::rootPath() //const |
yueee_yt | 0:fdf9c2c5200f | 102 | { |
yueee_yt | 0:fdf9c2c5200f | 103 | return m_rootPath; |
yueee_yt | 0:fdf9c2c5200f | 104 | } |
yueee_yt | 0:fdf9c2c5200f | 105 | |
yueee_yt | 0:fdf9c2c5200f | 106 | void HTTPRequestHandler::setErrCode(int errc) |
yueee_yt | 0:fdf9c2c5200f | 107 | { |
yueee_yt | 0:fdf9c2c5200f | 108 | m_errc = errc; |
yueee_yt | 0:fdf9c2c5200f | 109 | } |
yueee_yt | 0:fdf9c2c5200f | 110 | |
yueee_yt | 0:fdf9c2c5200f | 111 | void HTTPRequestHandler::setContentLen(int len) |
yueee_yt | 0:fdf9c2c5200f | 112 | { |
yueee_yt | 0:fdf9c2c5200f | 113 | char len_str[6] = {0}; |
yueee_yt | 0:fdf9c2c5200f | 114 | sprintf(len_str, "%d", len); |
yueee_yt | 0:fdf9c2c5200f | 115 | respHeaders()["Content-Length"] = len_str; |
yueee_yt | 0:fdf9c2c5200f | 116 | } |
yueee_yt | 0:fdf9c2c5200f | 117 | |
yueee_yt | 0:fdf9c2c5200f | 118 | map<string, string>& HTTPRequestHandler::respHeaders() |
yueee_yt | 0:fdf9c2c5200f | 119 | { |
yueee_yt | 0:fdf9c2c5200f | 120 | return m_respHeaders; |
yueee_yt | 0:fdf9c2c5200f | 121 | } |
yueee_yt | 0:fdf9c2c5200f | 122 | |
yueee_yt | 0:fdf9c2c5200f | 123 | int HTTPRequestHandler::writeData(const char* buf, int len) |
yueee_yt | 0:fdf9c2c5200f | 124 | { |
yueee_yt | 0:fdf9c2c5200f | 125 | if(!m_headersSent) { |
yueee_yt | 0:fdf9c2c5200f | 126 | m_headersSent = true; |
yueee_yt | 0:fdf9c2c5200f | 127 | writeHeaders(); |
yueee_yt | 0:fdf9c2c5200f | 128 | } |
yueee_yt | 0:fdf9c2c5200f | 129 | return m_pTCPSocketConnection->send((char *)buf, len); |
yueee_yt | 0:fdf9c2c5200f | 130 | } |
yueee_yt | 0:fdf9c2c5200f | 131 | /** |
yueee_yt | 0:fdf9c2c5200f | 132 | void HTTPRequestHandler::setTimeout(int ms) |
yueee_yt | 0:fdf9c2c5200f | 133 | { |
yueee_yt | 0:fdf9c2c5200f | 134 | m_timeout = 1000*ms; |
yueee_yt | 0:fdf9c2c5200f | 135 | resetTimeout(); |
yueee_yt | 0:fdf9c2c5200f | 136 | } |
yueee_yt | 0:fdf9c2c5200f | 137 | **/ |
yueee_yt | 0:fdf9c2c5200f | 138 | /** |
yueee_yt | 0:fdf9c2c5200f | 139 | void HTTPRequestHandler::resetTimeout() |
yueee_yt | 0:fdf9c2c5200f | 140 | { |
yueee_yt | 0:fdf9c2c5200f | 141 | m_watchdog.detach(); |
yueee_yt | 0:fdf9c2c5200f | 142 | m_watchdog.attach_us<HTTPRequestHandler>(this, &HTTPRequestHandler::onTimeout, m_timeout); |
yueee_yt | 0:fdf9c2c5200f | 143 | } |
yueee_yt | 0:fdf9c2c5200f | 144 | **/ |
yueee_yt | 0:fdf9c2c5200f | 145 | |
yueee_yt | 0:fdf9c2c5200f | 146 | void HTTPRequestHandler::readHeaders() |
yueee_yt | 0:fdf9c2c5200f | 147 | { |
yueee_yt | 0:fdf9c2c5200f | 148 | static char line[128]; |
yueee_yt | 0:fdf9c2c5200f | 149 | static char key[128]; |
yueee_yt | 0:fdf9c2c5200f | 150 | static char value[128]; |
yueee_yt | 0:fdf9c2c5200f | 151 | while( readLine(line, 128) > 0) { //if == 0, it is an empty line = end of headers |
yueee_yt | 0:fdf9c2c5200f | 152 | int n = sscanf(line, "%[^:]: %[^\n]", key, value); |
yueee_yt | 0:fdf9c2c5200f | 153 | if ( n == 2 ) { |
yueee_yt | 4:1b6b021ee21d | 154 | #ifdef _DEBUG_REQUEST_HANDLER |
yueee_yt | 0:fdf9c2c5200f | 155 | printf("\r\n+++(HTTPRequestHandler)Read header : %s : %s\r\n", key, value); |
yueee_yt | 4:1b6b021ee21d | 156 | #endif |
yueee_yt | 0:fdf9c2c5200f | 157 | m_reqHeaders[key] = value; |
yueee_yt | 0:fdf9c2c5200f | 158 | } |
yueee_yt | 0:fdf9c2c5200f | 159 | //TODO: Impl n==1 case (part 2 of previous header) |
yueee_yt | 0:fdf9c2c5200f | 160 | } |
yueee_yt | 0:fdf9c2c5200f | 161 | } |
yueee_yt | 0:fdf9c2c5200f | 162 | |
yueee_yt | 0:fdf9c2c5200f | 163 | void HTTPRequestHandler::writeHeaders() //Called at the first writeData call |
yueee_yt | 0:fdf9c2c5200f | 164 | { |
yueee_yt | 0:fdf9c2c5200f | 165 | static char line[128]; |
yueee_yt | 0:fdf9c2c5200f | 166 | |
yueee_yt | 0:fdf9c2c5200f | 167 | //Response line |
yueee_yt | 0:fdf9c2c5200f | 168 | sprintf(line, "HTTP/1.1 %d MbedInfo\r\n", m_errc); //Not a violation of the standard not to include the descriptive text |
yueee_yt | 0:fdf9c2c5200f | 169 | m_pTCPSocketConnection->send(line, strlen(line)); |
yueee_yt | 0:fdf9c2c5200f | 170 | |
yueee_yt | 0:fdf9c2c5200f | 171 | map<string,string>::iterator it; |
yueee_yt | 0:fdf9c2c5200f | 172 | while( !m_respHeaders.empty() ) { |
yueee_yt | 0:fdf9c2c5200f | 173 | it = m_respHeaders.begin(); |
yueee_yt | 0:fdf9c2c5200f | 174 | sprintf(line, "%s: %s\r\n", (*it).first.c_str(), (*it).second.c_str() ); |
yueee_yt | 4:1b6b021ee21d | 175 | #ifdef _DEBUG_REQUEST_HANDLER |
yueee_yt | 0:fdf9c2c5200f | 176 | printf("\r\n+++(HTTPRequestHandler)%s", line); |
yueee_yt | 4:1b6b021ee21d | 177 | #endif |
yueee_yt | 0:fdf9c2c5200f | 178 | m_pTCPSocketConnection->send(line, strlen(line)); |
yueee_yt | 0:fdf9c2c5200f | 179 | m_respHeaders.erase(it); |
yueee_yt | 0:fdf9c2c5200f | 180 | } |
yueee_yt | 0:fdf9c2c5200f | 181 | m_pTCPSocketConnection->send("\r\n",2); //End of head |
yueee_yt | 0:fdf9c2c5200f | 182 | } |
yueee_yt | 0:fdf9c2c5200f | 183 | |
yueee_yt | 0:fdf9c2c5200f | 184 | int HTTPRequestHandler::readLine(char* str, int maxLen) |
yueee_yt | 0:fdf9c2c5200f | 185 | { |
yueee_yt | 0:fdf9c2c5200f | 186 | int ret; |
yueee_yt | 0:fdf9c2c5200f | 187 | int len = 0; |
yueee_yt | 0:fdf9c2c5200f | 188 | for(int i = 0; i < maxLen - 1; i++) { |
yueee_yt | 0:fdf9c2c5200f | 189 | ret = m_pTCPSocketConnection->receive(str, 1); |
yueee_yt | 0:fdf9c2c5200f | 190 | if(!ret) { |
yueee_yt | 0:fdf9c2c5200f | 191 | break; |
yueee_yt | 0:fdf9c2c5200f | 192 | } |
yueee_yt | 0:fdf9c2c5200f | 193 | if( (len > 1) && *(str-1)=='\r' && *str=='\n' ) { |
yueee_yt | 0:fdf9c2c5200f | 194 | str--; |
yueee_yt | 0:fdf9c2c5200f | 195 | len-=2; |
yueee_yt | 0:fdf9c2c5200f | 196 | break; |
yueee_yt | 0:fdf9c2c5200f | 197 | } else if( *str=='\n' ) { |
yueee_yt | 0:fdf9c2c5200f | 198 | len--; |
yueee_yt | 0:fdf9c2c5200f | 199 | break; |
yueee_yt | 0:fdf9c2c5200f | 200 | } |
yueee_yt | 0:fdf9c2c5200f | 201 | str++; |
yueee_yt | 0:fdf9c2c5200f | 202 | len++; |
yueee_yt | 0:fdf9c2c5200f | 203 | } |
yueee_yt | 0:fdf9c2c5200f | 204 | *str = 0; |
yueee_yt | 0:fdf9c2c5200f | 205 | return len; |
yueee_yt | 0:fdf9c2c5200f | 206 | } |
yueee_yt | 0:fdf9c2c5200f | 207 | /** |
yueee_yt | 0:fdf9c2c5200f | 208 | void HTTPRequestHandler::onTCPSocketEvent(TCPSocketEvent e) |
yueee_yt | 0:fdf9c2c5200f | 209 | { |
yueee_yt | 0:fdf9c2c5200f | 210 | //printf("\r\nEvent %d in HTTPRequestHandler\r\n", e); |
yueee_yt | 0:fdf9c2c5200f | 211 | printf("\r\n+++(HTTPRequestHandler)Event in HTTPRequestHandler\r\n"); |
yueee_yt | 0:fdf9c2c5200f | 212 | |
yueee_yt | 0:fdf9c2c5200f | 213 | if(m_closed) |
yueee_yt | 0:fdf9c2c5200f | 214 | { |
yueee_yt | 0:fdf9c2c5200f | 215 | printf("\r\n+++(HTTPRequestHandler)WARN: Discarded\r\n"); |
yueee_yt | 0:fdf9c2c5200f | 216 | return; |
yueee_yt | 0:fdf9c2c5200f | 217 | } |
yueee_yt | 0:fdf9c2c5200f | 218 | |
yueee_yt | 0:fdf9c2c5200f | 219 | switch(e) |
yueee_yt | 0:fdf9c2c5200f | 220 | { |
yueee_yt | 0:fdf9c2c5200f | 221 | case TCPSOCKET_READABLE: |
yueee_yt | 0:fdf9c2c5200f | 222 | resetTimeout(); |
yueee_yt | 0:fdf9c2c5200f | 223 | onReadable(); |
yueee_yt | 0:fdf9c2c5200f | 224 | break; |
yueee_yt | 0:fdf9c2c5200f | 225 | case TCPSOCKET_WRITEABLE: |
yueee_yt | 0:fdf9c2c5200f | 226 | resetTimeout(); |
yueee_yt | 0:fdf9c2c5200f | 227 | onWriteable(); |
yueee_yt | 0:fdf9c2c5200f | 228 | break; |
yueee_yt | 0:fdf9c2c5200f | 229 | case TCPSOCKET_CONTIMEOUT: |
yueee_yt | 0:fdf9c2c5200f | 230 | case TCPSOCKET_CONRST: |
yueee_yt | 0:fdf9c2c5200f | 231 | case TCPSOCKET_CONABRT: |
yueee_yt | 0:fdf9c2c5200f | 232 | case TCPSOCKET_ERROR: |
yueee_yt | 0:fdf9c2c5200f | 233 | case TCPSOCKET_DISCONNECTED: |
yueee_yt | 0:fdf9c2c5200f | 234 | DBG("\r\nConnection error in handler\r\n"); |
yueee_yt | 0:fdf9c2c5200f | 235 | close(); |
yueee_yt | 0:fdf9c2c5200f | 236 | break; |
yueee_yt | 0:fdf9c2c5200f | 237 | } |
yueee_yt | 0:fdf9c2c5200f | 238 | } |
yueee_yt | 0:fdf9c2c5200f | 239 | **/ |