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