Fork of HttpServer_snapshot_mbed-os library
Fork of HttpServer_snapshot_mbed-os by
HTTPRequestHandler.cpp@0:fdf9c2c5200f, 2014-02-20 (annotated)
- Committer:
- yueee_yt
- Date:
- Thu Feb 20 05:36:19 2014 +0000
- Revision:
- 0:fdf9c2c5200f
- Child:
- 4:1b6b021ee21d
Initialize version
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 | 0:fdf9c2c5200f | 20 | |
yueee_yt | 0:fdf9c2c5200f | 21 | //#include "core/netservice.h" |
yueee_yt | 0:fdf9c2c5200f | 22 | #include "HTTPRequestHandler.h" |
yueee_yt | 0:fdf9c2c5200f | 23 | |
yueee_yt | 0:fdf9c2c5200f | 24 | #include <string.h> |
yueee_yt | 0:fdf9c2c5200f | 25 | |
yueee_yt | 0:fdf9c2c5200f | 26 | //#define __DEBUG |
yueee_yt | 0:fdf9c2c5200f | 27 | //#include "dbg/dbg.h" |
yueee_yt | 0:fdf9c2c5200f | 28 | |
yueee_yt | 0:fdf9c2c5200f | 29 | #define HTTP_REQUEST_TIMEOUT 5000 |
yueee_yt | 0:fdf9c2c5200f | 30 | |
yueee_yt | 0:fdf9c2c5200f | 31 | //HTTPRequestHandler::HTTPRequestHandler(const char* rootPath, const char* path, TCPSocket* pTCPSocket) : NetService(), |
yueee_yt | 0:fdf9c2c5200f | 32 | // m_pTCPSocketConnection(pTCPSocketConnection), m_reqHeaders(), m_respHeaders(), |
yueee_yt | 0:fdf9c2c5200f | 33 | // m_rootPath(rootPath), m_path(path), m_errc(200), |
yueee_yt | 0:fdf9c2c5200f | 34 | // m_watchdog(), m_timeout(0),**/ m_closed(false), m_headersSent(false) //OK |
yueee_yt | 0:fdf9c2c5200f | 35 | HTTPRequestHandler::HTTPRequestHandler(const char* rootPath, const char* path, TCPSocketConnection* pTCPSocketConnection) : |
yueee_yt | 0:fdf9c2c5200f | 36 | m_pTCPSocketConnection(pTCPSocketConnection), m_reqHeaders(), m_respHeaders(), |
yueee_yt | 0:fdf9c2c5200f | 37 | m_rootPath(rootPath), m_path(path), m_errc(200), m_closed(false), m_headersSent(false) |
yueee_yt | 0:fdf9c2c5200f | 38 | { |
yueee_yt | 0:fdf9c2c5200f | 39 | printf("+++(HTTPRequestHandler) init \r\n"); |
yueee_yt | 0:fdf9c2c5200f | 40 | //Read & parse headers |
yueee_yt | 0:fdf9c2c5200f | 41 | readHeaders(); |
yueee_yt | 0:fdf9c2c5200f | 42 | //* m_pTCPSocket->setOnEvent(this, &HTTPRequestHandler::onTCPSocketEvent); |
yueee_yt | 0:fdf9c2c5200f | 43 | //* setTimeout(HTTP_REQUEST_TIMEOUT); |
yueee_yt | 0:fdf9c2c5200f | 44 | printf("+++(HTTPRequestHandler) init end \r\n"); |
yueee_yt | 0:fdf9c2c5200f | 45 | } |
yueee_yt | 0:fdf9c2c5200f | 46 | |
yueee_yt | 0:fdf9c2c5200f | 47 | HTTPRequestHandler::~HTTPRequestHandler() |
yueee_yt | 0:fdf9c2c5200f | 48 | { |
yueee_yt | 0:fdf9c2c5200f | 49 | close(); |
yueee_yt | 0:fdf9c2c5200f | 50 | printf("+++(HTTPRequestHandler) Destroy end\r\n"); |
yueee_yt | 0:fdf9c2c5200f | 51 | } |
yueee_yt | 0:fdf9c2c5200f | 52 | |
yueee_yt | 0:fdf9c2c5200f | 53 | void HTTPRequestHandler::onTimeout() //Connection has timed out |
yueee_yt | 0:fdf9c2c5200f | 54 | { |
yueee_yt | 0:fdf9c2c5200f | 55 | close(); |
yueee_yt | 0:fdf9c2c5200f | 56 | } |
yueee_yt | 0:fdf9c2c5200f | 57 | |
yueee_yt | 0:fdf9c2c5200f | 58 | void HTTPRequestHandler::close() //Close socket and destroy data |
yueee_yt | 0:fdf9c2c5200f | 59 | { |
yueee_yt | 0:fdf9c2c5200f | 60 | if(m_closed) |
yueee_yt | 0:fdf9c2c5200f | 61 | return; |
yueee_yt | 0:fdf9c2c5200f | 62 | m_closed = true; //Prevent recursive calling or calling on an object being destructed by someone else |
yueee_yt | 0:fdf9c2c5200f | 63 | /** m_watchdog.detach(); **/ |
yueee_yt | 0:fdf9c2c5200f | 64 | //* onClose(); |
yueee_yt | 0:fdf9c2c5200f | 65 | //* m_pTCPSocket->resetOnEvent(); |
yueee_yt | 0:fdf9c2c5200f | 66 | //* m_pTCPSocket->close(); |
yueee_yt | 0:fdf9c2c5200f | 67 | //* delete m_pTCPSocket; //Can safely destroy socket |
yueee_yt | 0:fdf9c2c5200f | 68 | //* NetService::close(); |
yueee_yt | 0:fdf9c2c5200f | 69 | } |
yueee_yt | 0:fdf9c2c5200f | 70 | |
yueee_yt | 0:fdf9c2c5200f | 71 | map<string, string>& HTTPRequestHandler::reqHeaders() //const |
yueee_yt | 0:fdf9c2c5200f | 72 | { |
yueee_yt | 0:fdf9c2c5200f | 73 | return m_reqHeaders; |
yueee_yt | 0:fdf9c2c5200f | 74 | } |
yueee_yt | 0:fdf9c2c5200f | 75 | |
yueee_yt | 0:fdf9c2c5200f | 76 | string& HTTPRequestHandler::path() //const |
yueee_yt | 0:fdf9c2c5200f | 77 | { |
yueee_yt | 0:fdf9c2c5200f | 78 | return m_path; |
yueee_yt | 0:fdf9c2c5200f | 79 | } |
yueee_yt | 0:fdf9c2c5200f | 80 | |
yueee_yt | 0:fdf9c2c5200f | 81 | int HTTPRequestHandler::dataLen() const |
yueee_yt | 0:fdf9c2c5200f | 82 | { |
yueee_yt | 0:fdf9c2c5200f | 83 | map<string,string>::iterator it; |
yueee_yt | 0:fdf9c2c5200f | 84 | it = m_reqHeaders.find("Content-Length"); |
yueee_yt | 0:fdf9c2c5200f | 85 | if( it == m_reqHeaders.end() ) { |
yueee_yt | 0:fdf9c2c5200f | 86 | return 0; |
yueee_yt | 0:fdf9c2c5200f | 87 | } |
yueee_yt | 0:fdf9c2c5200f | 88 | return atoi((*it).second.c_str()); //return 0 if parse fails, so that's fine |
yueee_yt | 0:fdf9c2c5200f | 89 | } |
yueee_yt | 0:fdf9c2c5200f | 90 | |
yueee_yt | 0:fdf9c2c5200f | 91 | int HTTPRequestHandler::readData(char* buf, int len) |
yueee_yt | 0:fdf9c2c5200f | 92 | { |
yueee_yt | 0:fdf9c2c5200f | 93 | return m_pTCPSocketConnection->receive(buf, len); |
yueee_yt | 0:fdf9c2c5200f | 94 | } |
yueee_yt | 0:fdf9c2c5200f | 95 | |
yueee_yt | 0:fdf9c2c5200f | 96 | string& HTTPRequestHandler::rootPath() //const |
yueee_yt | 0:fdf9c2c5200f | 97 | { |
yueee_yt | 0:fdf9c2c5200f | 98 | return m_rootPath; |
yueee_yt | 0:fdf9c2c5200f | 99 | } |
yueee_yt | 0:fdf9c2c5200f | 100 | |
yueee_yt | 0:fdf9c2c5200f | 101 | void HTTPRequestHandler::setErrCode(int errc) |
yueee_yt | 0:fdf9c2c5200f | 102 | { |
yueee_yt | 0:fdf9c2c5200f | 103 | m_errc = errc; |
yueee_yt | 0:fdf9c2c5200f | 104 | } |
yueee_yt | 0:fdf9c2c5200f | 105 | |
yueee_yt | 0:fdf9c2c5200f | 106 | void HTTPRequestHandler::setContentLen(int len) |
yueee_yt | 0:fdf9c2c5200f | 107 | { |
yueee_yt | 0:fdf9c2c5200f | 108 | char len_str[6] = {0}; |
yueee_yt | 0:fdf9c2c5200f | 109 | sprintf(len_str, "%d", len); |
yueee_yt | 0:fdf9c2c5200f | 110 | respHeaders()["Content-Length"] = len_str; |
yueee_yt | 0:fdf9c2c5200f | 111 | } |
yueee_yt | 0:fdf9c2c5200f | 112 | |
yueee_yt | 0:fdf9c2c5200f | 113 | map<string, string>& HTTPRequestHandler::respHeaders() |
yueee_yt | 0:fdf9c2c5200f | 114 | { |
yueee_yt | 0:fdf9c2c5200f | 115 | return m_respHeaders; |
yueee_yt | 0:fdf9c2c5200f | 116 | } |
yueee_yt | 0:fdf9c2c5200f | 117 | |
yueee_yt | 0:fdf9c2c5200f | 118 | int HTTPRequestHandler::writeData(const char* buf, int len) |
yueee_yt | 0:fdf9c2c5200f | 119 | { |
yueee_yt | 0:fdf9c2c5200f | 120 | if(!m_headersSent) { |
yueee_yt | 0:fdf9c2c5200f | 121 | m_headersSent = true; |
yueee_yt | 0:fdf9c2c5200f | 122 | writeHeaders(); |
yueee_yt | 0:fdf9c2c5200f | 123 | } |
yueee_yt | 0:fdf9c2c5200f | 124 | return m_pTCPSocketConnection->send((char *)buf, len); |
yueee_yt | 0:fdf9c2c5200f | 125 | } |
yueee_yt | 0:fdf9c2c5200f | 126 | /** |
yueee_yt | 0:fdf9c2c5200f | 127 | void HTTPRequestHandler::setTimeout(int ms) |
yueee_yt | 0:fdf9c2c5200f | 128 | { |
yueee_yt | 0:fdf9c2c5200f | 129 | m_timeout = 1000*ms; |
yueee_yt | 0:fdf9c2c5200f | 130 | resetTimeout(); |
yueee_yt | 0:fdf9c2c5200f | 131 | } |
yueee_yt | 0:fdf9c2c5200f | 132 | **/ |
yueee_yt | 0:fdf9c2c5200f | 133 | /** |
yueee_yt | 0:fdf9c2c5200f | 134 | void HTTPRequestHandler::resetTimeout() |
yueee_yt | 0:fdf9c2c5200f | 135 | { |
yueee_yt | 0:fdf9c2c5200f | 136 | m_watchdog.detach(); |
yueee_yt | 0:fdf9c2c5200f | 137 | m_watchdog.attach_us<HTTPRequestHandler>(this, &HTTPRequestHandler::onTimeout, m_timeout); |
yueee_yt | 0:fdf9c2c5200f | 138 | } |
yueee_yt | 0:fdf9c2c5200f | 139 | **/ |
yueee_yt | 0:fdf9c2c5200f | 140 | |
yueee_yt | 0:fdf9c2c5200f | 141 | void HTTPRequestHandler::readHeaders() |
yueee_yt | 0:fdf9c2c5200f | 142 | { |
yueee_yt | 0:fdf9c2c5200f | 143 | static char line[128]; |
yueee_yt | 0:fdf9c2c5200f | 144 | static char key[128]; |
yueee_yt | 0:fdf9c2c5200f | 145 | static char value[128]; |
yueee_yt | 0:fdf9c2c5200f | 146 | while( readLine(line, 128) > 0) { //if == 0, it is an empty line = end of headers |
yueee_yt | 0:fdf9c2c5200f | 147 | int n = sscanf(line, "%[^:]: %[^\n]", key, value); |
yueee_yt | 0:fdf9c2c5200f | 148 | if ( n == 2 ) { |
yueee_yt | 0:fdf9c2c5200f | 149 | printf("\r\n+++(HTTPRequestHandler)Read header : %s : %s\r\n", key, value); |
yueee_yt | 0:fdf9c2c5200f | 150 | m_reqHeaders[key] = value; |
yueee_yt | 0:fdf9c2c5200f | 151 | } |
yueee_yt | 0:fdf9c2c5200f | 152 | //TODO: Impl n==1 case (part 2 of previous header) |
yueee_yt | 0:fdf9c2c5200f | 153 | } |
yueee_yt | 0:fdf9c2c5200f | 154 | } |
yueee_yt | 0:fdf9c2c5200f | 155 | |
yueee_yt | 0:fdf9c2c5200f | 156 | void HTTPRequestHandler::writeHeaders() //Called at the first writeData call |
yueee_yt | 0:fdf9c2c5200f | 157 | { |
yueee_yt | 0:fdf9c2c5200f | 158 | static char line[128]; |
yueee_yt | 0:fdf9c2c5200f | 159 | |
yueee_yt | 0:fdf9c2c5200f | 160 | //Response line |
yueee_yt | 0:fdf9c2c5200f | 161 | 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 | 162 | m_pTCPSocketConnection->send(line, strlen(line)); |
yueee_yt | 0:fdf9c2c5200f | 163 | |
yueee_yt | 0:fdf9c2c5200f | 164 | map<string,string>::iterator it; |
yueee_yt | 0:fdf9c2c5200f | 165 | while( !m_respHeaders.empty() ) { |
yueee_yt | 0:fdf9c2c5200f | 166 | it = m_respHeaders.begin(); |
yueee_yt | 0:fdf9c2c5200f | 167 | sprintf(line, "%s: %s\r\n", (*it).first.c_str(), (*it).second.c_str() ); |
yueee_yt | 0:fdf9c2c5200f | 168 | printf("\r\n+++(HTTPRequestHandler)%s", line); |
yueee_yt | 0:fdf9c2c5200f | 169 | m_pTCPSocketConnection->send(line, strlen(line)); |
yueee_yt | 0:fdf9c2c5200f | 170 | m_respHeaders.erase(it); |
yueee_yt | 0:fdf9c2c5200f | 171 | } |
yueee_yt | 0:fdf9c2c5200f | 172 | m_pTCPSocketConnection->send("\r\n",2); //End of head |
yueee_yt | 0:fdf9c2c5200f | 173 | } |
yueee_yt | 0:fdf9c2c5200f | 174 | |
yueee_yt | 0:fdf9c2c5200f | 175 | int HTTPRequestHandler::readLine(char* str, int maxLen) |
yueee_yt | 0:fdf9c2c5200f | 176 | { |
yueee_yt | 0:fdf9c2c5200f | 177 | int ret; |
yueee_yt | 0:fdf9c2c5200f | 178 | int len = 0; |
yueee_yt | 0:fdf9c2c5200f | 179 | for(int i = 0; i < maxLen - 1; i++) { |
yueee_yt | 0:fdf9c2c5200f | 180 | ret = m_pTCPSocketConnection->receive(str, 1); |
yueee_yt | 0:fdf9c2c5200f | 181 | if(!ret) { |
yueee_yt | 0:fdf9c2c5200f | 182 | break; |
yueee_yt | 0:fdf9c2c5200f | 183 | } |
yueee_yt | 0:fdf9c2c5200f | 184 | if( (len > 1) && *(str-1)=='\r' && *str=='\n' ) { |
yueee_yt | 0:fdf9c2c5200f | 185 | str--; |
yueee_yt | 0:fdf9c2c5200f | 186 | len-=2; |
yueee_yt | 0:fdf9c2c5200f | 187 | break; |
yueee_yt | 0:fdf9c2c5200f | 188 | } else if( *str=='\n' ) { |
yueee_yt | 0:fdf9c2c5200f | 189 | len--; |
yueee_yt | 0:fdf9c2c5200f | 190 | break; |
yueee_yt | 0:fdf9c2c5200f | 191 | } |
yueee_yt | 0:fdf9c2c5200f | 192 | str++; |
yueee_yt | 0:fdf9c2c5200f | 193 | len++; |
yueee_yt | 0:fdf9c2c5200f | 194 | } |
yueee_yt | 0:fdf9c2c5200f | 195 | *str = 0; |
yueee_yt | 0:fdf9c2c5200f | 196 | return len; |
yueee_yt | 0:fdf9c2c5200f | 197 | } |
yueee_yt | 0:fdf9c2c5200f | 198 | /** |
yueee_yt | 0:fdf9c2c5200f | 199 | void HTTPRequestHandler::onTCPSocketEvent(TCPSocketEvent e) |
yueee_yt | 0:fdf9c2c5200f | 200 | { |
yueee_yt | 0:fdf9c2c5200f | 201 | //printf("\r\nEvent %d in HTTPRequestHandler\r\n", e); |
yueee_yt | 0:fdf9c2c5200f | 202 | printf("\r\n+++(HTTPRequestHandler)Event in HTTPRequestHandler\r\n"); |
yueee_yt | 0:fdf9c2c5200f | 203 | |
yueee_yt | 0:fdf9c2c5200f | 204 | if(m_closed) |
yueee_yt | 0:fdf9c2c5200f | 205 | { |
yueee_yt | 0:fdf9c2c5200f | 206 | printf("\r\n+++(HTTPRequestHandler)WARN: Discarded\r\n"); |
yueee_yt | 0:fdf9c2c5200f | 207 | return; |
yueee_yt | 0:fdf9c2c5200f | 208 | } |
yueee_yt | 0:fdf9c2c5200f | 209 | |
yueee_yt | 0:fdf9c2c5200f | 210 | switch(e) |
yueee_yt | 0:fdf9c2c5200f | 211 | { |
yueee_yt | 0:fdf9c2c5200f | 212 | case TCPSOCKET_READABLE: |
yueee_yt | 0:fdf9c2c5200f | 213 | resetTimeout(); |
yueee_yt | 0:fdf9c2c5200f | 214 | onReadable(); |
yueee_yt | 0:fdf9c2c5200f | 215 | break; |
yueee_yt | 0:fdf9c2c5200f | 216 | case TCPSOCKET_WRITEABLE: |
yueee_yt | 0:fdf9c2c5200f | 217 | resetTimeout(); |
yueee_yt | 0:fdf9c2c5200f | 218 | onWriteable(); |
yueee_yt | 0:fdf9c2c5200f | 219 | break; |
yueee_yt | 0:fdf9c2c5200f | 220 | case TCPSOCKET_CONTIMEOUT: |
yueee_yt | 0:fdf9c2c5200f | 221 | case TCPSOCKET_CONRST: |
yueee_yt | 0:fdf9c2c5200f | 222 | case TCPSOCKET_CONABRT: |
yueee_yt | 0:fdf9c2c5200f | 223 | case TCPSOCKET_ERROR: |
yueee_yt | 0:fdf9c2c5200f | 224 | case TCPSOCKET_DISCONNECTED: |
yueee_yt | 0:fdf9c2c5200f | 225 | DBG("\r\nConnection error in handler\r\n"); |
yueee_yt | 0:fdf9c2c5200f | 226 | close(); |
yueee_yt | 0:fdf9c2c5200f | 227 | break; |
yueee_yt | 0:fdf9c2c5200f | 228 | } |
yueee_yt | 0:fdf9c2c5200f | 229 | } |
yueee_yt | 0:fdf9c2c5200f | 230 | **/ |