Experimental HTTPClient with proxy support

Committer:
igorsk
Date:
Wed Jun 29 16:01:58 2011 +0000
Revision:
0:b56b6a05cad4

        

Who changed what in which revision?

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