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