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