Modified version of NetServices. Fixes an issue where connections failed should the HTTP response status line be received in a packet on its own prior to any further headers. Changes are made to the HTTPClient.cpp file's readHeaders method.

Committer:
andrewbonney
Date:
Fri Apr 08 14:39:41 2011 +0000
Revision:
0:ec559500a63f

        

Who changed what in which revision?

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