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.
services/http/client/HTTPClient.cpp@0:ec559500a63f, 2011-04-08 (annotated)
- Committer:
- andrewbonney
- Date:
- Fri Apr 08 14:39:41 2011 +0000
- Revision:
- 0:ec559500a63f
Who changed what in which revision?
User | Revision | Line number | New 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 "HTTPClient.h" |
andrewbonney | 0:ec559500a63f | 26 | #include "../util/base64.h" |
andrewbonney | 0:ec559500a63f | 27 | #include "../util/url.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 30000//15000 |
andrewbonney | 0:ec559500a63f | 33 | #define HTTP_PORT 80 |
andrewbonney | 0:ec559500a63f | 34 | |
andrewbonney | 0:ec559500a63f | 35 | #define CHUNK_SIZE 256 |
andrewbonney | 0:ec559500a63f | 36 | |
andrewbonney | 0:ec559500a63f | 37 | HTTPClient::HTTPClient() : NetService(false) /*Not owned by the pool*/, m_meth(HTTP_GET), m_pCbItem(NULL), m_pCbMeth(NULL), m_pCb(NULL), |
andrewbonney | 0:ec559500a63f | 38 | m_watchdog(), m_timeout(0), m_pDnsReq(NULL), m_server(), m_path(), |
andrewbonney | 0:ec559500a63f | 39 | m_closed(true), m_state(HTTP_CLOSED), |
andrewbonney | 0:ec559500a63f | 40 | m_pDataOut(NULL), m_pDataIn(NULL), m_dataChunked(false), m_dataPos(0), m_dataLen(0), m_httpResponseCode(0), m_blockingResult(HTTP_PROCESSING) |
andrewbonney | 0:ec559500a63f | 41 | |
andrewbonney | 0:ec559500a63f | 42 | { |
andrewbonney | 0:ec559500a63f | 43 | setTimeout(HTTP_REQUEST_TIMEOUT); |
andrewbonney | 0:ec559500a63f | 44 | m_buf = new char[CHUNK_SIZE]; |
andrewbonney | 0:ec559500a63f | 45 | DBG("New HTTPClient %p\n",this); |
andrewbonney | 0:ec559500a63f | 46 | } |
andrewbonney | 0:ec559500a63f | 47 | |
andrewbonney | 0:ec559500a63f | 48 | HTTPClient::~HTTPClient() |
andrewbonney | 0:ec559500a63f | 49 | { |
andrewbonney | 0:ec559500a63f | 50 | close(); |
andrewbonney | 0:ec559500a63f | 51 | delete[] m_buf; |
andrewbonney | 0:ec559500a63f | 52 | } |
andrewbonney | 0:ec559500a63f | 53 | |
andrewbonney | 0:ec559500a63f | 54 | void HTTPClient::basicAuth(const char* user, const char* password) //Basic Authentification |
andrewbonney | 0:ec559500a63f | 55 | { |
andrewbonney | 0:ec559500a63f | 56 | if(user==NULL) |
andrewbonney | 0:ec559500a63f | 57 | { |
andrewbonney | 0:ec559500a63f | 58 | m_reqHeaders.erase("Authorization"); //Remove auth str |
andrewbonney | 0:ec559500a63f | 59 | return; |
andrewbonney | 0:ec559500a63f | 60 | } |
andrewbonney | 0:ec559500a63f | 61 | string auth = "Basic "; |
andrewbonney | 0:ec559500a63f | 62 | string decStr = user; |
andrewbonney | 0:ec559500a63f | 63 | decStr += ":"; |
andrewbonney | 0:ec559500a63f | 64 | decStr += password; |
andrewbonney | 0:ec559500a63f | 65 | auth.append( Base64::encode(decStr) ); |
andrewbonney | 0:ec559500a63f | 66 | DBG("Auth str is %s\n", auth.c_str()); |
andrewbonney | 0:ec559500a63f | 67 | m_reqHeaders["Authorization"] = auth; |
andrewbonney | 0:ec559500a63f | 68 | } |
andrewbonney | 0:ec559500a63f | 69 | |
andrewbonney | 0:ec559500a63f | 70 | //High Level setup functions |
andrewbonney | 0:ec559500a63f | 71 | HTTPResult HTTPClient::get(const char* uri, HTTPData* pDataIn) //Blocking |
andrewbonney | 0:ec559500a63f | 72 | { |
andrewbonney | 0:ec559500a63f | 73 | doGet(uri, pDataIn); |
andrewbonney | 0:ec559500a63f | 74 | return blockingProcess(); |
andrewbonney | 0:ec559500a63f | 75 | } |
andrewbonney | 0:ec559500a63f | 76 | |
andrewbonney | 0:ec559500a63f | 77 | HTTPResult HTTPClient::get(const char* uri, HTTPData* pDataIn, void (*pMethod)(HTTPResult)) //Non blocking |
andrewbonney | 0:ec559500a63f | 78 | { |
andrewbonney | 0:ec559500a63f | 79 | setOnResult(pMethod); |
andrewbonney | 0:ec559500a63f | 80 | doGet(uri, pDataIn); |
andrewbonney | 0:ec559500a63f | 81 | return HTTP_PROCESSING; |
andrewbonney | 0:ec559500a63f | 82 | } |
andrewbonney | 0:ec559500a63f | 83 | |
andrewbonney | 0:ec559500a63f | 84 | #if 0 //For info only |
andrewbonney | 0:ec559500a63f | 85 | template<class T> |
andrewbonney | 0:ec559500a63f | 86 | HTTPResult HTTPClient::get(const char* uri, HTTPData* pDataIn, T* pItem, void (T::*pMethod)(HTTPResult)) //Non blocking |
andrewbonney | 0:ec559500a63f | 87 | { |
andrewbonney | 0:ec559500a63f | 88 | setOnResult(pItem, pMethod); |
andrewbonney | 0:ec559500a63f | 89 | doGet(uri, pDataIn); |
andrewbonney | 0:ec559500a63f | 90 | return HTTP_PROCESSING; |
andrewbonney | 0:ec559500a63f | 91 | } |
andrewbonney | 0:ec559500a63f | 92 | #endif |
andrewbonney | 0:ec559500a63f | 93 | |
andrewbonney | 0:ec559500a63f | 94 | HTTPResult HTTPClient::post(const char* uri, const HTTPData& dataOut, HTTPData* pDataIn) //Blocking |
andrewbonney | 0:ec559500a63f | 95 | { |
andrewbonney | 0:ec559500a63f | 96 | doPost(uri, dataOut, pDataIn); |
andrewbonney | 0:ec559500a63f | 97 | return blockingProcess(); |
andrewbonney | 0:ec559500a63f | 98 | } |
andrewbonney | 0:ec559500a63f | 99 | |
andrewbonney | 0:ec559500a63f | 100 | HTTPResult HTTPClient::post(const char* uri, const HTTPData& dataOut, HTTPData* pDataIn, void (*pMethod)(HTTPResult)) //Non blocking |
andrewbonney | 0:ec559500a63f | 101 | { |
andrewbonney | 0:ec559500a63f | 102 | setOnResult(pMethod); |
andrewbonney | 0:ec559500a63f | 103 | doPost(uri, dataOut, pDataIn); |
andrewbonney | 0:ec559500a63f | 104 | return HTTP_PROCESSING; |
andrewbonney | 0:ec559500a63f | 105 | } |
andrewbonney | 0:ec559500a63f | 106 | |
andrewbonney | 0:ec559500a63f | 107 | #if 0 //For info only |
andrewbonney | 0:ec559500a63f | 108 | template<class T> |
andrewbonney | 0:ec559500a63f | 109 | HTTPResult HTTPClient::post(const char* uri, const HTTPData& dataOut, HTTPData* pDataIn, T* pItem, void (T::*pMethod)(HTTPResult)) //Non blocking |
andrewbonney | 0:ec559500a63f | 110 | { |
andrewbonney | 0:ec559500a63f | 111 | setOnResult(pItem, pMethod); |
andrewbonney | 0:ec559500a63f | 112 | doPost(uri, dataOut, pDataIn); |
andrewbonney | 0:ec559500a63f | 113 | return HTTP_PROCESSING; |
andrewbonney | 0:ec559500a63f | 114 | } |
andrewbonney | 0:ec559500a63f | 115 | #endif |
andrewbonney | 0:ec559500a63f | 116 | |
andrewbonney | 0:ec559500a63f | 117 | void HTTPClient::doGet(const char* uri, HTTPData* pDataIn) |
andrewbonney | 0:ec559500a63f | 118 | { |
andrewbonney | 0:ec559500a63f | 119 | m_meth = HTTP_GET; |
andrewbonney | 0:ec559500a63f | 120 | setup(uri, NULL, pDataIn); |
andrewbonney | 0:ec559500a63f | 121 | } |
andrewbonney | 0:ec559500a63f | 122 | |
andrewbonney | 0:ec559500a63f | 123 | void HTTPClient::doPost(const char* uri, const HTTPData& dataOut, HTTPData* pDataIn) |
andrewbonney | 0:ec559500a63f | 124 | { |
andrewbonney | 0:ec559500a63f | 125 | m_meth = HTTP_POST; |
andrewbonney | 0:ec559500a63f | 126 | setup(uri, (HTTPData*) &dataOut, pDataIn); |
andrewbonney | 0:ec559500a63f | 127 | } |
andrewbonney | 0:ec559500a63f | 128 | |
andrewbonney | 0:ec559500a63f | 129 | void HTTPClient::setOnResult( void (*pMethod)(HTTPResult) ) |
andrewbonney | 0:ec559500a63f | 130 | { |
andrewbonney | 0:ec559500a63f | 131 | m_pCb = pMethod; |
andrewbonney | 0:ec559500a63f | 132 | m_pCbItem = NULL; |
andrewbonney | 0:ec559500a63f | 133 | m_pCbMeth = NULL; |
andrewbonney | 0:ec559500a63f | 134 | } |
andrewbonney | 0:ec559500a63f | 135 | |
andrewbonney | 0:ec559500a63f | 136 | #if 0 //For info only |
andrewbonney | 0:ec559500a63f | 137 | template<class T> |
andrewbonney | 0:ec559500a63f | 138 | void HTTPClient::setOnResult( T* pItem, void (T::*pMethod)(NtpResult) ) |
andrewbonney | 0:ec559500a63f | 139 | { |
andrewbonney | 0:ec559500a63f | 140 | m_pCb = NULL; |
andrewbonney | 0:ec559500a63f | 141 | m_pCbItem = (CDummy*) pItem; |
andrewbonney | 0:ec559500a63f | 142 | m_pCbMeth = (void (CDummy::*)(NtpResult)) pMethod; |
andrewbonney | 0:ec559500a63f | 143 | } |
andrewbonney | 0:ec559500a63f | 144 | #endif |
andrewbonney | 0:ec559500a63f | 145 | |
andrewbonney | 0:ec559500a63f | 146 | void HTTPClient::setTimeout(int ms) |
andrewbonney | 0:ec559500a63f | 147 | { |
andrewbonney | 0:ec559500a63f | 148 | m_timeout = ms; |
andrewbonney | 0:ec559500a63f | 149 | //resetTimeout(); |
andrewbonney | 0:ec559500a63f | 150 | } |
andrewbonney | 0:ec559500a63f | 151 | |
andrewbonney | 0:ec559500a63f | 152 | void HTTPClient::poll() //Called by NetServices |
andrewbonney | 0:ec559500a63f | 153 | { |
andrewbonney | 0:ec559500a63f | 154 | if(m_closed) |
andrewbonney | 0:ec559500a63f | 155 | { |
andrewbonney | 0:ec559500a63f | 156 | return; |
andrewbonney | 0:ec559500a63f | 157 | } |
andrewbonney | 0:ec559500a63f | 158 | if(m_watchdog.read_ms()>m_timeout) |
andrewbonney | 0:ec559500a63f | 159 | { |
andrewbonney | 0:ec559500a63f | 160 | onTimeout(); |
andrewbonney | 0:ec559500a63f | 161 | } |
andrewbonney | 0:ec559500a63f | 162 | else if(m_state == HTTP_READ_DATA_INCOMPLETE) |
andrewbonney | 0:ec559500a63f | 163 | { |
andrewbonney | 0:ec559500a63f | 164 | readData(); //Try to read more data |
andrewbonney | 0:ec559500a63f | 165 | if( m_state == HTTP_DONE ) |
andrewbonney | 0:ec559500a63f | 166 | { |
andrewbonney | 0:ec559500a63f | 167 | //All data has been read, close w/ success :) |
andrewbonney | 0:ec559500a63f | 168 | DBG("Done :)!\n"); |
andrewbonney | 0:ec559500a63f | 169 | onResult(HTTP_OK); |
andrewbonney | 0:ec559500a63f | 170 | close(); |
andrewbonney | 0:ec559500a63f | 171 | } |
andrewbonney | 0:ec559500a63f | 172 | } |
andrewbonney | 0:ec559500a63f | 173 | |
andrewbonney | 0:ec559500a63f | 174 | } |
andrewbonney | 0:ec559500a63f | 175 | |
andrewbonney | 0:ec559500a63f | 176 | int HTTPClient::getHTTPResponseCode() |
andrewbonney | 0:ec559500a63f | 177 | { |
andrewbonney | 0:ec559500a63f | 178 | return m_httpResponseCode; |
andrewbonney | 0:ec559500a63f | 179 | } |
andrewbonney | 0:ec559500a63f | 180 | |
andrewbonney | 0:ec559500a63f | 181 | void HTTPClient::setRequestHeader(const string& header, const string& value) |
andrewbonney | 0:ec559500a63f | 182 | { |
andrewbonney | 0:ec559500a63f | 183 | m_reqHeaders[header] = value; |
andrewbonney | 0:ec559500a63f | 184 | } |
andrewbonney | 0:ec559500a63f | 185 | |
andrewbonney | 0:ec559500a63f | 186 | string& HTTPClient::getResponseHeader(const string& header) |
andrewbonney | 0:ec559500a63f | 187 | { |
andrewbonney | 0:ec559500a63f | 188 | return m_respHeaders[header]; |
andrewbonney | 0:ec559500a63f | 189 | } |
andrewbonney | 0:ec559500a63f | 190 | |
andrewbonney | 0:ec559500a63f | 191 | void HTTPClient::resetRequestHeaders() |
andrewbonney | 0:ec559500a63f | 192 | { |
andrewbonney | 0:ec559500a63f | 193 | m_reqHeaders.clear(); |
andrewbonney | 0:ec559500a63f | 194 | } |
andrewbonney | 0:ec559500a63f | 195 | |
andrewbonney | 0:ec559500a63f | 196 | void HTTPClient::resetTimeout() |
andrewbonney | 0:ec559500a63f | 197 | { |
andrewbonney | 0:ec559500a63f | 198 | m_watchdog.reset(); |
andrewbonney | 0:ec559500a63f | 199 | m_watchdog.start(); |
andrewbonney | 0:ec559500a63f | 200 | } |
andrewbonney | 0:ec559500a63f | 201 | |
andrewbonney | 0:ec559500a63f | 202 | void HTTPClient::init() //Create and setup socket if needed |
andrewbonney | 0:ec559500a63f | 203 | { |
andrewbonney | 0:ec559500a63f | 204 | close(); //Remove previous elements |
andrewbonney | 0:ec559500a63f | 205 | if(!m_closed) //Already opened |
andrewbonney | 0:ec559500a63f | 206 | return; |
andrewbonney | 0:ec559500a63f | 207 | m_state = HTTP_WRITE_HEADERS; |
andrewbonney | 0:ec559500a63f | 208 | m_pTCPSocket = new TCPSocket; |
andrewbonney | 0:ec559500a63f | 209 | m_pTCPSocket->setOnEvent(this, &HTTPClient::onTCPSocketEvent); |
andrewbonney | 0:ec559500a63f | 210 | m_closed = false; |
andrewbonney | 0:ec559500a63f | 211 | m_httpResponseCode = 0; |
andrewbonney | 0:ec559500a63f | 212 | } |
andrewbonney | 0:ec559500a63f | 213 | |
andrewbonney | 0:ec559500a63f | 214 | void HTTPClient::close() |
andrewbonney | 0:ec559500a63f | 215 | { |
andrewbonney | 0:ec559500a63f | 216 | if(m_closed) |
andrewbonney | 0:ec559500a63f | 217 | return; |
andrewbonney | 0:ec559500a63f | 218 | m_state = HTTP_CLOSED; |
andrewbonney | 0:ec559500a63f | 219 | //Now Request headers are kept btw requests unless resetRequestHeaders() is called |
andrewbonney | 0:ec559500a63f | 220 | //m_reqHeaders.clear(); //Clear headers for next requests |
andrewbonney | 0:ec559500a63f | 221 | m_closed = true; //Prevent recursive calling or calling on an object being destructed by someone else |
andrewbonney | 0:ec559500a63f | 222 | m_watchdog.stop(); //Stop timeout |
andrewbonney | 0:ec559500a63f | 223 | m_watchdog.reset(); |
andrewbonney | 0:ec559500a63f | 224 | m_pTCPSocket->resetOnEvent(); |
andrewbonney | 0:ec559500a63f | 225 | m_pTCPSocket->close(); |
andrewbonney | 0:ec559500a63f | 226 | delete m_pTCPSocket; |
andrewbonney | 0:ec559500a63f | 227 | m_pTCPSocket = NULL; |
andrewbonney | 0:ec559500a63f | 228 | if( m_pDnsReq ) |
andrewbonney | 0:ec559500a63f | 229 | { |
andrewbonney | 0:ec559500a63f | 230 | m_pDnsReq->close(); |
andrewbonney | 0:ec559500a63f | 231 | delete m_pDnsReq; |
andrewbonney | 0:ec559500a63f | 232 | m_pDnsReq = NULL; |
andrewbonney | 0:ec559500a63f | 233 | } |
andrewbonney | 0:ec559500a63f | 234 | } |
andrewbonney | 0:ec559500a63f | 235 | |
andrewbonney | 0:ec559500a63f | 236 | void HTTPClient::setup(const char* uri, HTTPData* pDataOut, HTTPData* pDataIn) //Setup request, make DNS Req if necessary |
andrewbonney | 0:ec559500a63f | 237 | { |
andrewbonney | 0:ec559500a63f | 238 | init(); //Initialize client in known state, create socket |
andrewbonney | 0:ec559500a63f | 239 | m_pDataOut = pDataOut; |
andrewbonney | 0:ec559500a63f | 240 | m_pDataIn = pDataIn; |
andrewbonney | 0:ec559500a63f | 241 | resetTimeout(); |
andrewbonney | 0:ec559500a63f | 242 | |
andrewbonney | 0:ec559500a63f | 243 | //Erase previous headers |
andrewbonney | 0:ec559500a63f | 244 | //Do NOT clear m_reqHeaders as they might have already set before connecting |
andrewbonney | 0:ec559500a63f | 245 | m_respHeaders.clear(); |
andrewbonney | 0:ec559500a63f | 246 | |
andrewbonney | 0:ec559500a63f | 247 | //Erase response buffer |
andrewbonney | 0:ec559500a63f | 248 | if(m_pDataIn) |
andrewbonney | 0:ec559500a63f | 249 | m_pDataIn->clear(); |
andrewbonney | 0:ec559500a63f | 250 | |
andrewbonney | 0:ec559500a63f | 251 | //Assert that buffers are initialized properly |
andrewbonney | 0:ec559500a63f | 252 | m_dataLen = 0; |
andrewbonney | 0:ec559500a63f | 253 | m_bufRemainingLen = 0; |
andrewbonney | 0:ec559500a63f | 254 | |
andrewbonney | 0:ec559500a63f | 255 | Url url; |
andrewbonney | 0:ec559500a63f | 256 | url.fromString(uri); |
andrewbonney | 0:ec559500a63f | 257 | |
andrewbonney | 0:ec559500a63f | 258 | m_path = url.getPath(); |
andrewbonney | 0:ec559500a63f | 259 | |
andrewbonney | 0:ec559500a63f | 260 | m_server.setName(url.getHost().c_str()); |
andrewbonney | 0:ec559500a63f | 261 | |
andrewbonney | 0:ec559500a63f | 262 | if( url.getPort() > 0 ) |
andrewbonney | 0:ec559500a63f | 263 | { |
andrewbonney | 0:ec559500a63f | 264 | m_server.setPort( url.getPort() ); |
andrewbonney | 0:ec559500a63f | 265 | } |
andrewbonney | 0:ec559500a63f | 266 | else |
andrewbonney | 0:ec559500a63f | 267 | { |
andrewbonney | 0:ec559500a63f | 268 | m_server.setPort( HTTP_PORT ); |
andrewbonney | 0:ec559500a63f | 269 | } |
andrewbonney | 0:ec559500a63f | 270 | |
andrewbonney | 0:ec559500a63f | 271 | DBG("URL parsed,\r\nHost: %s\r\nPort: %d\r\nPath: %s\n", url.getHost().c_str(), url.getPort(), url.getPath().c_str()); |
andrewbonney | 0:ec559500a63f | 272 | |
andrewbonney | 0:ec559500a63f | 273 | IpAddr ip; |
andrewbonney | 0:ec559500a63f | 274 | if( url.getHostIp(&ip) ) |
andrewbonney | 0:ec559500a63f | 275 | { |
andrewbonney | 0:ec559500a63f | 276 | m_server.setIp(ip); |
andrewbonney | 0:ec559500a63f | 277 | connect(); |
andrewbonney | 0:ec559500a63f | 278 | } |
andrewbonney | 0:ec559500a63f | 279 | else |
andrewbonney | 0:ec559500a63f | 280 | { |
andrewbonney | 0:ec559500a63f | 281 | DBG("DNS Query...\n"); |
andrewbonney | 0:ec559500a63f | 282 | m_pDnsReq = new DNSRequest(); |
andrewbonney | 0:ec559500a63f | 283 | m_pDnsReq->setOnReply(this, &HTTPClient::onDNSReply); |
andrewbonney | 0:ec559500a63f | 284 | m_pDnsReq->resolve(&m_server); |
andrewbonney | 0:ec559500a63f | 285 | DBG("HTTPClient : DNSRequest %p\n", m_pDnsReq); |
andrewbonney | 0:ec559500a63f | 286 | } |
andrewbonney | 0:ec559500a63f | 287 | |
andrewbonney | 0:ec559500a63f | 288 | } |
andrewbonney | 0:ec559500a63f | 289 | |
andrewbonney | 0:ec559500a63f | 290 | void HTTPClient::connect() //Start Connection |
andrewbonney | 0:ec559500a63f | 291 | { |
andrewbonney | 0:ec559500a63f | 292 | resetTimeout(); |
andrewbonney | 0:ec559500a63f | 293 | DBG("Connecting...\n"); |
andrewbonney | 0:ec559500a63f | 294 | m_pTCPSocket->connect(m_server); |
andrewbonney | 0:ec559500a63f | 295 | } |
andrewbonney | 0:ec559500a63f | 296 | |
andrewbonney | 0:ec559500a63f | 297 | #define MIN(a,b) ((a)<(b)?(a):(b)) |
andrewbonney | 0:ec559500a63f | 298 | #define ABS(a) (((a)>0)?(a):0) |
andrewbonney | 0:ec559500a63f | 299 | int HTTPClient::tryRead() //Try to read data from tcp packet and put in the HTTPData object |
andrewbonney | 0:ec559500a63f | 300 | { |
andrewbonney | 0:ec559500a63f | 301 | int len = 0; |
andrewbonney | 0:ec559500a63f | 302 | int readLen; |
andrewbonney | 0:ec559500a63f | 303 | do |
andrewbonney | 0:ec559500a63f | 304 | { |
andrewbonney | 0:ec559500a63f | 305 | if(m_state == HTTP_READ_DATA_INCOMPLETE) //First try to complete buffer copy |
andrewbonney | 0:ec559500a63f | 306 | { |
andrewbonney | 0:ec559500a63f | 307 | readLen = m_bufRemainingLen; |
andrewbonney | 0:ec559500a63f | 308 | /* if (readLen == 0) |
andrewbonney | 0:ec559500a63f | 309 | { |
andrewbonney | 0:ec559500a63f | 310 | m_state = HTTP_READ_DATA; |
andrewbonney | 0:ec559500a63f | 311 | continue; |
andrewbonney | 0:ec559500a63f | 312 | }*/ |
andrewbonney | 0:ec559500a63f | 313 | } |
andrewbonney | 0:ec559500a63f | 314 | else |
andrewbonney | 0:ec559500a63f | 315 | { |
andrewbonney | 0:ec559500a63f | 316 | readLen = m_pTCPSocket->recv(m_buf, MIN(ABS(m_dataLen-m_dataPos),CHUNK_SIZE)); |
andrewbonney | 0:ec559500a63f | 317 | if(readLen < 0) //Error |
andrewbonney | 0:ec559500a63f | 318 | { |
andrewbonney | 0:ec559500a63f | 319 | return readLen; |
andrewbonney | 0:ec559500a63f | 320 | } |
andrewbonney | 0:ec559500a63f | 321 | |
andrewbonney | 0:ec559500a63f | 322 | DBG("%d bytes read\n", readLen); |
andrewbonney | 0:ec559500a63f | 323 | |
andrewbonney | 0:ec559500a63f | 324 | m_pBufRemaining = m_buf; |
andrewbonney | 0:ec559500a63f | 325 | } |
andrewbonney | 0:ec559500a63f | 326 | if (readLen == 0) |
andrewbonney | 0:ec559500a63f | 327 | { |
andrewbonney | 0:ec559500a63f | 328 | m_state = HTTP_READ_DATA; |
andrewbonney | 0:ec559500a63f | 329 | return len; |
andrewbonney | 0:ec559500a63f | 330 | } |
andrewbonney | 0:ec559500a63f | 331 | |
andrewbonney | 0:ec559500a63f | 332 | DBG("Trying to write %d bytes\n", readLen); |
andrewbonney | 0:ec559500a63f | 333 | |
andrewbonney | 0:ec559500a63f | 334 | int writtenLen = m_pDataIn->write(m_pBufRemaining, readLen); |
andrewbonney | 0:ec559500a63f | 335 | m_dataPos += writtenLen; |
andrewbonney | 0:ec559500a63f | 336 | |
andrewbonney | 0:ec559500a63f | 337 | DBG("%d bytes written\n", writtenLen); |
andrewbonney | 0:ec559500a63f | 338 | |
andrewbonney | 0:ec559500a63f | 339 | if(writtenLen<readLen) //Data was not completely written |
andrewbonney | 0:ec559500a63f | 340 | { |
andrewbonney | 0:ec559500a63f | 341 | m_pBufRemaining += writtenLen; |
andrewbonney | 0:ec559500a63f | 342 | m_bufRemainingLen = readLen - writtenLen; |
andrewbonney | 0:ec559500a63f | 343 | m_state = HTTP_READ_DATA_INCOMPLETE; |
andrewbonney | 0:ec559500a63f | 344 | return len + writtenLen; |
andrewbonney | 0:ec559500a63f | 345 | } |
andrewbonney | 0:ec559500a63f | 346 | else |
andrewbonney | 0:ec559500a63f | 347 | { |
andrewbonney | 0:ec559500a63f | 348 | m_state = HTTP_READ_DATA; |
andrewbonney | 0:ec559500a63f | 349 | } |
andrewbonney | 0:ec559500a63f | 350 | len += readLen; |
andrewbonney | 0:ec559500a63f | 351 | } while(readLen>0); |
andrewbonney | 0:ec559500a63f | 352 | |
andrewbonney | 0:ec559500a63f | 353 | return len; |
andrewbonney | 0:ec559500a63f | 354 | } |
andrewbonney | 0:ec559500a63f | 355 | |
andrewbonney | 0:ec559500a63f | 356 | void HTTPClient::readData() //Data has been read |
andrewbonney | 0:ec559500a63f | 357 | { |
andrewbonney | 0:ec559500a63f | 358 | if(m_pDataIn == NULL) //Nothing to read (in HEAD for instance, not supported now) |
andrewbonney | 0:ec559500a63f | 359 | { |
andrewbonney | 0:ec559500a63f | 360 | m_state = HTTP_DONE; |
andrewbonney | 0:ec559500a63f | 361 | return; |
andrewbonney | 0:ec559500a63f | 362 | } |
andrewbonney | 0:ec559500a63f | 363 | DBG("Reading response...\n"); |
andrewbonney | 0:ec559500a63f | 364 | int len = 0; |
andrewbonney | 0:ec559500a63f | 365 | do |
andrewbonney | 0:ec559500a63f | 366 | { |
andrewbonney | 0:ec559500a63f | 367 | if(m_dataChunked && (m_state != HTTP_READ_DATA_INCOMPLETE)) |
andrewbonney | 0:ec559500a63f | 368 | { |
andrewbonney | 0:ec559500a63f | 369 | if(m_dataLen==0) |
andrewbonney | 0:ec559500a63f | 370 | { |
andrewbonney | 0:ec559500a63f | 371 | DBG("Reading chunk length...\n"); |
andrewbonney | 0:ec559500a63f | 372 | //New block |
andrewbonney | 0:ec559500a63f | 373 | static char chunkHeader[16]; |
andrewbonney | 0:ec559500a63f | 374 | //We use m_dataPos to retain the read position in chunkHeader, it has been set to 0 before the first call of readData() |
andrewbonney | 0:ec559500a63f | 375 | m_dataPos += readLine(chunkHeader + m_dataPos, ABS(16 - m_dataPos)); |
andrewbonney | 0:ec559500a63f | 376 | if( m_dataPos > 0 ) |
andrewbonney | 0:ec559500a63f | 377 | { |
andrewbonney | 0:ec559500a63f | 378 | if( chunkHeader[strlen(chunkHeader)-1] == 0x0d ) |
andrewbonney | 0:ec559500a63f | 379 | { |
andrewbonney | 0:ec559500a63f | 380 | sscanf(chunkHeader, "%x%*[^\r\n]", &m_dataLen); |
andrewbonney | 0:ec559500a63f | 381 | DBG("Chunk length is %d\n", m_dataLen); |
andrewbonney | 0:ec559500a63f | 382 | m_dataPos = 0; |
andrewbonney | 0:ec559500a63f | 383 | } |
andrewbonney | 0:ec559500a63f | 384 | else |
andrewbonney | 0:ec559500a63f | 385 | { |
andrewbonney | 0:ec559500a63f | 386 | //Wait for end of line |
andrewbonney | 0:ec559500a63f | 387 | DBG("Wait for CRLF\n"); |
andrewbonney | 0:ec559500a63f | 388 | return; |
andrewbonney | 0:ec559500a63f | 389 | } |
andrewbonney | 0:ec559500a63f | 390 | } |
andrewbonney | 0:ec559500a63f | 391 | else |
andrewbonney | 0:ec559500a63f | 392 | { |
andrewbonney | 0:ec559500a63f | 393 | DBG("Wait for data\n"); |
andrewbonney | 0:ec559500a63f | 394 | //Wait for data |
andrewbonney | 0:ec559500a63f | 395 | return; |
andrewbonney | 0:ec559500a63f | 396 | } |
andrewbonney | 0:ec559500a63f | 397 | } |
andrewbonney | 0:ec559500a63f | 398 | } |
andrewbonney | 0:ec559500a63f | 399 | |
andrewbonney | 0:ec559500a63f | 400 | //Proper data recovery |
andrewbonney | 0:ec559500a63f | 401 | len = tryRead(); |
andrewbonney | 0:ec559500a63f | 402 | if(len<0) //Error |
andrewbonney | 0:ec559500a63f | 403 | { |
andrewbonney | 0:ec559500a63f | 404 | onResult(HTTP_CONN); |
andrewbonney | 0:ec559500a63f | 405 | return; |
andrewbonney | 0:ec559500a63f | 406 | } |
andrewbonney | 0:ec559500a63f | 407 | |
andrewbonney | 0:ec559500a63f | 408 | if(len>0) |
andrewbonney | 0:ec559500a63f | 409 | resetTimeout(); |
andrewbonney | 0:ec559500a63f | 410 | |
andrewbonney | 0:ec559500a63f | 411 | if(m_state == HTTP_READ_DATA_INCOMPLETE) |
andrewbonney | 0:ec559500a63f | 412 | return; |
andrewbonney | 0:ec559500a63f | 413 | |
andrewbonney | 0:ec559500a63f | 414 | //Chunk Tail |
andrewbonney | 0:ec559500a63f | 415 | if(m_dataChunked) |
andrewbonney | 0:ec559500a63f | 416 | { |
andrewbonney | 0:ec559500a63f | 417 | if(m_dataPos >= m_dataLen) |
andrewbonney | 0:ec559500a63f | 418 | { |
andrewbonney | 0:ec559500a63f | 419 | DBG("Chunk read, wait for CRLF\n"); |
andrewbonney | 0:ec559500a63f | 420 | char chunkTail[3]; |
andrewbonney | 0:ec559500a63f | 421 | m_dataPos += readLine(chunkTail, 3); |
andrewbonney | 0:ec559500a63f | 422 | } |
andrewbonney | 0:ec559500a63f | 423 | |
andrewbonney | 0:ec559500a63f | 424 | if(m_dataPos >= m_dataLen + 1) //1 == strlen("\n"), |
andrewbonney | 0:ec559500a63f | 425 | { |
andrewbonney | 0:ec559500a63f | 426 | DBG("End of chunk\n"); |
andrewbonney | 0:ec559500a63f | 427 | if(m_dataLen==0) |
andrewbonney | 0:ec559500a63f | 428 | { |
andrewbonney | 0:ec559500a63f | 429 | DBG("End of file\n"); |
andrewbonney | 0:ec559500a63f | 430 | //End of file |
andrewbonney | 0:ec559500a63f | 431 | m_state = HTTP_DONE; //Done |
andrewbonney | 0:ec559500a63f | 432 | } |
andrewbonney | 0:ec559500a63f | 433 | m_dataLen = 0; |
andrewbonney | 0:ec559500a63f | 434 | m_dataPos = 0; |
andrewbonney | 0:ec559500a63f | 435 | } |
andrewbonney | 0:ec559500a63f | 436 | } |
andrewbonney | 0:ec559500a63f | 437 | |
andrewbonney | 0:ec559500a63f | 438 | } while(len>0); |
andrewbonney | 0:ec559500a63f | 439 | |
andrewbonney | 0:ec559500a63f | 440 | |
andrewbonney | 0:ec559500a63f | 441 | if(!m_dataChunked && (m_dataPos >= m_dataLen)) //All Data has been received |
andrewbonney | 0:ec559500a63f | 442 | { |
andrewbonney | 0:ec559500a63f | 443 | DBG("End of file\n"); |
andrewbonney | 0:ec559500a63f | 444 | m_state = HTTP_DONE; //Done |
andrewbonney | 0:ec559500a63f | 445 | } |
andrewbonney | 0:ec559500a63f | 446 | } |
andrewbonney | 0:ec559500a63f | 447 | |
andrewbonney | 0:ec559500a63f | 448 | void HTTPClient::writeData() //Data has been written & buf is free |
andrewbonney | 0:ec559500a63f | 449 | { |
andrewbonney | 0:ec559500a63f | 450 | if(m_pDataOut == NULL) //Nothing to write (in POST for instance) |
andrewbonney | 0:ec559500a63f | 451 | { |
andrewbonney | 0:ec559500a63f | 452 | m_dataLen = 0; //Reset Data Length |
andrewbonney | 0:ec559500a63f | 453 | m_state = HTTP_READ_HEADERS; |
andrewbonney | 0:ec559500a63f | 454 | return; |
andrewbonney | 0:ec559500a63f | 455 | } |
andrewbonney | 0:ec559500a63f | 456 | int len = m_pDataOut->read(m_buf, CHUNK_SIZE); |
andrewbonney | 0:ec559500a63f | 457 | if( m_dataChunked ) |
andrewbonney | 0:ec559500a63f | 458 | { |
andrewbonney | 0:ec559500a63f | 459 | //Write chunk header |
andrewbonney | 0:ec559500a63f | 460 | char chunkHeader[16]; |
andrewbonney | 0:ec559500a63f | 461 | sprintf(chunkHeader, "%d\r\n", len); |
andrewbonney | 0:ec559500a63f | 462 | int ret = m_pTCPSocket->send(chunkHeader, strlen(chunkHeader)); |
andrewbonney | 0:ec559500a63f | 463 | if(ret < 0)//Error |
andrewbonney | 0:ec559500a63f | 464 | { |
andrewbonney | 0:ec559500a63f | 465 | onResult(HTTP_CONN); |
andrewbonney | 0:ec559500a63f | 466 | return; |
andrewbonney | 0:ec559500a63f | 467 | } |
andrewbonney | 0:ec559500a63f | 468 | } |
andrewbonney | 0:ec559500a63f | 469 | m_pTCPSocket->send(m_buf, len); |
andrewbonney | 0:ec559500a63f | 470 | m_dataPos+=len; |
andrewbonney | 0:ec559500a63f | 471 | if( m_dataChunked ) |
andrewbonney | 0:ec559500a63f | 472 | { |
andrewbonney | 0:ec559500a63f | 473 | m_pTCPSocket->send("\r\n", 2); //Chunk-terminating CRLF |
andrewbonney | 0:ec559500a63f | 474 | } |
andrewbonney | 0:ec559500a63f | 475 | if( ( !m_dataChunked && (m_dataPos >= m_dataLen) ) |
andrewbonney | 0:ec559500a63f | 476 | || ( m_dataChunked && !len ) ) //All Data has been sent |
andrewbonney | 0:ec559500a63f | 477 | { |
andrewbonney | 0:ec559500a63f | 478 | m_dataLen = 0; //Reset Data Length |
andrewbonney | 0:ec559500a63f | 479 | m_state = HTTP_READ_HEADERS; //Wait for resp |
andrewbonney | 0:ec559500a63f | 480 | } |
andrewbonney | 0:ec559500a63f | 481 | } |
andrewbonney | 0:ec559500a63f | 482 | |
andrewbonney | 0:ec559500a63f | 483 | void HTTPClient::onTCPSocketEvent(TCPSocketEvent e) |
andrewbonney | 0:ec559500a63f | 484 | { |
andrewbonney | 0:ec559500a63f | 485 | DBG("Event %d in HTTPClient::onTCPSocketEvent()\n", e); |
andrewbonney | 0:ec559500a63f | 486 | |
andrewbonney | 0:ec559500a63f | 487 | if(m_closed) |
andrewbonney | 0:ec559500a63f | 488 | { |
andrewbonney | 0:ec559500a63f | 489 | DBG("WARN: Discarded\n"); |
andrewbonney | 0:ec559500a63f | 490 | return; |
andrewbonney | 0:ec559500a63f | 491 | } |
andrewbonney | 0:ec559500a63f | 492 | |
andrewbonney | 0:ec559500a63f | 493 | switch(e) |
andrewbonney | 0:ec559500a63f | 494 | { |
andrewbonney | 0:ec559500a63f | 495 | case TCPSOCKET_READABLE: //Incoming data |
andrewbonney | 0:ec559500a63f | 496 | resetTimeout(); |
andrewbonney | 0:ec559500a63f | 497 | switch(m_state) |
andrewbonney | 0:ec559500a63f | 498 | { |
andrewbonney | 0:ec559500a63f | 499 | case HTTP_READ_HEADERS: |
andrewbonney | 0:ec559500a63f | 500 | if( !readHeaders() ) |
andrewbonney | 0:ec559500a63f | 501 | { |
andrewbonney | 0:ec559500a63f | 502 | return; //Connection has been closed or incomplete data |
andrewbonney | 0:ec559500a63f | 503 | } |
andrewbonney | 0:ec559500a63f | 504 | if( m_pDataIn ) |
andrewbonney | 0:ec559500a63f | 505 | { |
andrewbonney | 0:ec559500a63f | 506 | //Data chunked? |
andrewbonney | 0:ec559500a63f | 507 | if(m_respHeaders["Transfer-Encoding"].find("chunked")!=string::npos) |
andrewbonney | 0:ec559500a63f | 508 | { |
andrewbonney | 0:ec559500a63f | 509 | m_dataChunked = true; |
andrewbonney | 0:ec559500a63f | 510 | m_dataPos = 0; |
andrewbonney | 0:ec559500a63f | 511 | m_dataLen = 0; |
andrewbonney | 0:ec559500a63f | 512 | DBG("Encoding is chunked, Content-Type is %s\n", m_respHeaders["Content-Type"].c_str() ); |
andrewbonney | 0:ec559500a63f | 513 | } |
andrewbonney | 0:ec559500a63f | 514 | else |
andrewbonney | 0:ec559500a63f | 515 | { |
andrewbonney | 0:ec559500a63f | 516 | m_dataChunked = false; |
andrewbonney | 0:ec559500a63f | 517 | int len = 0; |
andrewbonney | 0:ec559500a63f | 518 | //DBG("Preparing read... len = %s\n", m_respHeaders["Content-Length"].c_str()); |
andrewbonney | 0:ec559500a63f | 519 | sscanf(m_respHeaders["Content-Length"].c_str(), "%d", &len); |
andrewbonney | 0:ec559500a63f | 520 | m_pDataIn->setDataLen( len ); |
andrewbonney | 0:ec559500a63f | 521 | m_dataPos = 0; |
andrewbonney | 0:ec559500a63f | 522 | m_dataLen = len; |
andrewbonney | 0:ec559500a63f | 523 | DBG("Content-Length is %d, Content-Type is %s\n", len, m_respHeaders["Content-Type"].c_str() ); |
andrewbonney | 0:ec559500a63f | 524 | } |
andrewbonney | 0:ec559500a63f | 525 | m_pDataIn->setDataType( m_respHeaders["Content-Type"] ); |
andrewbonney | 0:ec559500a63f | 526 | } |
andrewbonney | 0:ec559500a63f | 527 | case HTTP_READ_DATA: |
andrewbonney | 0:ec559500a63f | 528 | readData(); |
andrewbonney | 0:ec559500a63f | 529 | break; |
andrewbonney | 0:ec559500a63f | 530 | case HTTP_READ_DATA_INCOMPLETE: |
andrewbonney | 0:ec559500a63f | 531 | break; //We need to handle previously received data first |
andrewbonney | 0:ec559500a63f | 532 | default: |
andrewbonney | 0:ec559500a63f | 533 | //Should not receive data now, req is not complete |
andrewbonney | 0:ec559500a63f | 534 | onResult(HTTP_PRTCL); |
andrewbonney | 0:ec559500a63f | 535 | } |
andrewbonney | 0:ec559500a63f | 536 | //All data has been read, close w/ success :) |
andrewbonney | 0:ec559500a63f | 537 | if( m_state == HTTP_DONE ) |
andrewbonney | 0:ec559500a63f | 538 | { |
andrewbonney | 0:ec559500a63f | 539 | DBG("Done :)!\n"); |
andrewbonney | 0:ec559500a63f | 540 | onResult(HTTP_OK); |
andrewbonney | 0:ec559500a63f | 541 | } |
andrewbonney | 0:ec559500a63f | 542 | break; |
andrewbonney | 0:ec559500a63f | 543 | case TCPSOCKET_CONNECTED: |
andrewbonney | 0:ec559500a63f | 544 | case TCPSOCKET_WRITEABLE: //We can send data |
andrewbonney | 0:ec559500a63f | 545 | resetTimeout(); |
andrewbonney | 0:ec559500a63f | 546 | switch(m_state) |
andrewbonney | 0:ec559500a63f | 547 | { |
andrewbonney | 0:ec559500a63f | 548 | case HTTP_WRITE_HEADERS: |
andrewbonney | 0:ec559500a63f | 549 | //Update headers fields according to m_pDataOut |
andrewbonney | 0:ec559500a63f | 550 | if( m_pDataOut ) |
andrewbonney | 0:ec559500a63f | 551 | { |
andrewbonney | 0:ec559500a63f | 552 | //Data is chunked? |
andrewbonney | 0:ec559500a63f | 553 | if(m_pDataOut->getIsChunked()) |
andrewbonney | 0:ec559500a63f | 554 | { |
andrewbonney | 0:ec559500a63f | 555 | m_dataChunked = true; |
andrewbonney | 0:ec559500a63f | 556 | m_reqHeaders.erase("Content-Length"); |
andrewbonney | 0:ec559500a63f | 557 | m_reqHeaders["Transfer-Encoding"] = "chunked"; |
andrewbonney | 0:ec559500a63f | 558 | } |
andrewbonney | 0:ec559500a63f | 559 | else |
andrewbonney | 0:ec559500a63f | 560 | { |
andrewbonney | 0:ec559500a63f | 561 | m_dataChunked = false; |
andrewbonney | 0:ec559500a63f | 562 | char c_len[16] = "0"; |
andrewbonney | 0:ec559500a63f | 563 | int len = m_pDataOut->getDataLen(); |
andrewbonney | 0:ec559500a63f | 564 | sprintf(c_len, "%d", len); |
andrewbonney | 0:ec559500a63f | 565 | m_dataPos = 0; |
andrewbonney | 0:ec559500a63f | 566 | m_dataLen = len; |
andrewbonney | 0:ec559500a63f | 567 | m_reqHeaders.erase("Transfer-Encoding"); |
andrewbonney | 0:ec559500a63f | 568 | m_reqHeaders["Content-Length"] = string(c_len); |
andrewbonney | 0:ec559500a63f | 569 | } |
andrewbonney | 0:ec559500a63f | 570 | string type = m_pDataOut->getDataType(); |
andrewbonney | 0:ec559500a63f | 571 | if(!type.empty()) |
andrewbonney | 0:ec559500a63f | 572 | { |
andrewbonney | 0:ec559500a63f | 573 | m_reqHeaders["Content-Type"] = type; |
andrewbonney | 0:ec559500a63f | 574 | } |
andrewbonney | 0:ec559500a63f | 575 | else |
andrewbonney | 0:ec559500a63f | 576 | { |
andrewbonney | 0:ec559500a63f | 577 | m_reqHeaders.erase("Content-Type"); |
andrewbonney | 0:ec559500a63f | 578 | } |
andrewbonney | 0:ec559500a63f | 579 | } |
andrewbonney | 0:ec559500a63f | 580 | if( !writeHeaders() ) |
andrewbonney | 0:ec559500a63f | 581 | { |
andrewbonney | 0:ec559500a63f | 582 | return; //Connection has been closed |
andrewbonney | 0:ec559500a63f | 583 | } |
andrewbonney | 0:ec559500a63f | 584 | break; //Wait for writeable event before sending payload |
andrewbonney | 0:ec559500a63f | 585 | case HTTP_WRITE_DATA: |
andrewbonney | 0:ec559500a63f | 586 | writeData(); |
andrewbonney | 0:ec559500a63f | 587 | break; |
andrewbonney | 0:ec559500a63f | 588 | } |
andrewbonney | 0:ec559500a63f | 589 | //Otherwise request has been sent, now wait for resp |
andrewbonney | 0:ec559500a63f | 590 | break; |
andrewbonney | 0:ec559500a63f | 591 | case TCPSOCKET_CONTIMEOUT: |
andrewbonney | 0:ec559500a63f | 592 | case TCPSOCKET_CONRST: |
andrewbonney | 0:ec559500a63f | 593 | case TCPSOCKET_CONABRT: |
andrewbonney | 0:ec559500a63f | 594 | case TCPSOCKET_ERROR: |
andrewbonney | 0:ec559500a63f | 595 | DBG("Connection error.\n"); |
andrewbonney | 0:ec559500a63f | 596 | onResult(HTTP_CONN); |
andrewbonney | 0:ec559500a63f | 597 | case TCPSOCKET_DISCONNECTED: |
andrewbonney | 0:ec559500a63f | 598 | //There might still be some data available for reading |
andrewbonney | 0:ec559500a63f | 599 | //So if we are in a reading state, do not close the socket yet |
andrewbonney | 0:ec559500a63f | 600 | if( (m_state != HTTP_READ_DATA_INCOMPLETE) && (m_state != HTTP_DONE) && (m_state != HTTP_CLOSED) ) |
andrewbonney | 0:ec559500a63f | 601 | { |
andrewbonney | 0:ec559500a63f | 602 | onResult(HTTP_CONN); |
andrewbonney | 0:ec559500a63f | 603 | } |
andrewbonney | 0:ec559500a63f | 604 | DBG("Connection closed by remote host.\n"); |
andrewbonney | 0:ec559500a63f | 605 | break; |
andrewbonney | 0:ec559500a63f | 606 | } |
andrewbonney | 0:ec559500a63f | 607 | } |
andrewbonney | 0:ec559500a63f | 608 | |
andrewbonney | 0:ec559500a63f | 609 | void HTTPClient::onDNSReply(DNSReply r) |
andrewbonney | 0:ec559500a63f | 610 | { |
andrewbonney | 0:ec559500a63f | 611 | if(m_closed) |
andrewbonney | 0:ec559500a63f | 612 | { |
andrewbonney | 0:ec559500a63f | 613 | DBG("WARN: Discarded\n"); |
andrewbonney | 0:ec559500a63f | 614 | return; |
andrewbonney | 0:ec559500a63f | 615 | } |
andrewbonney | 0:ec559500a63f | 616 | |
andrewbonney | 0:ec559500a63f | 617 | if( r != DNS_FOUND ) |
andrewbonney | 0:ec559500a63f | 618 | { |
andrewbonney | 0:ec559500a63f | 619 | DBG("Could not resolve hostname.\n"); |
andrewbonney | 0:ec559500a63f | 620 | onResult(HTTP_DNS); |
andrewbonney | 0:ec559500a63f | 621 | return; |
andrewbonney | 0:ec559500a63f | 622 | } |
andrewbonney | 0:ec559500a63f | 623 | |
andrewbonney | 0:ec559500a63f | 624 | DBG("DNS Resolved to %d.%d.%d.%d.\n",m_server.getIp()[0],m_server.getIp()[1],m_server.getIp()[2],m_server.getIp()[3]); |
andrewbonney | 0:ec559500a63f | 625 | //If no error, m_server has been updated by m_pDnsReq so we're set to go ! |
andrewbonney | 0:ec559500a63f | 626 | m_pDnsReq->close(); |
andrewbonney | 0:ec559500a63f | 627 | delete m_pDnsReq; |
andrewbonney | 0:ec559500a63f | 628 | m_pDnsReq = NULL; |
andrewbonney | 0:ec559500a63f | 629 | connect(); |
andrewbonney | 0:ec559500a63f | 630 | } |
andrewbonney | 0:ec559500a63f | 631 | |
andrewbonney | 0:ec559500a63f | 632 | void HTTPClient::onResult(HTTPResult r) //Called when exchange completed or on failure |
andrewbonney | 0:ec559500a63f | 633 | { |
andrewbonney | 0:ec559500a63f | 634 | if(m_pCbItem && m_pCbMeth) |
andrewbonney | 0:ec559500a63f | 635 | (m_pCbItem->*m_pCbMeth)(r); |
andrewbonney | 0:ec559500a63f | 636 | else if(m_pCb) |
andrewbonney | 0:ec559500a63f | 637 | m_pCb(r); |
andrewbonney | 0:ec559500a63f | 638 | m_blockingResult = r; //Blocking mode |
andrewbonney | 0:ec559500a63f | 639 | close(); //FIXME:Remove suppl. close() calls |
andrewbonney | 0:ec559500a63f | 640 | } |
andrewbonney | 0:ec559500a63f | 641 | |
andrewbonney | 0:ec559500a63f | 642 | void HTTPClient::onTimeout() //Connection has timed out |
andrewbonney | 0:ec559500a63f | 643 | { |
andrewbonney | 0:ec559500a63f | 644 | DBG("Timed out.\n"); |
andrewbonney | 0:ec559500a63f | 645 | onResult(HTTP_TIMEOUT); |
andrewbonney | 0:ec559500a63f | 646 | close(); |
andrewbonney | 0:ec559500a63f | 647 | } |
andrewbonney | 0:ec559500a63f | 648 | |
andrewbonney | 0:ec559500a63f | 649 | //Headers |
andrewbonney | 0:ec559500a63f | 650 | |
andrewbonney | 0:ec559500a63f | 651 | //TODO: Factorize w/ HTTPRequestHandler in a single HTTPHeader class |
andrewbonney | 0:ec559500a63f | 652 | |
andrewbonney | 0:ec559500a63f | 653 | HTTPResult HTTPClient::blockingProcess() //Called in blocking mode, calls Net::poll() until return code is available |
andrewbonney | 0:ec559500a63f | 654 | { |
andrewbonney | 0:ec559500a63f | 655 | //Disable callbacks |
andrewbonney | 0:ec559500a63f | 656 | m_pCb = NULL; |
andrewbonney | 0:ec559500a63f | 657 | m_pCbItem = NULL; |
andrewbonney | 0:ec559500a63f | 658 | m_pCbMeth = NULL; |
andrewbonney | 0:ec559500a63f | 659 | m_blockingResult = HTTP_PROCESSING; |
andrewbonney | 0:ec559500a63f | 660 | do |
andrewbonney | 0:ec559500a63f | 661 | { |
andrewbonney | 0:ec559500a63f | 662 | Net::poll(); |
andrewbonney | 0:ec559500a63f | 663 | } while(m_blockingResult == HTTP_PROCESSING); |
andrewbonney | 0:ec559500a63f | 664 | Net::poll(); //Necessary for cleanup |
andrewbonney | 0:ec559500a63f | 665 | return m_blockingResult; |
andrewbonney | 0:ec559500a63f | 666 | } |
andrewbonney | 0:ec559500a63f | 667 | |
andrewbonney | 0:ec559500a63f | 668 | bool HTTPClient::readHeaders() |
andrewbonney | 0:ec559500a63f | 669 | { |
andrewbonney | 0:ec559500a63f | 670 | static char* line = m_buf; |
andrewbonney | 0:ec559500a63f | 671 | static char key[128]; |
andrewbonney | 0:ec559500a63f | 672 | static char value[128]; |
andrewbonney | 0:ec559500a63f | 673 | if(!m_dataLen) //No incomplete header in buffer, this is the first time we read data |
andrewbonney | 0:ec559500a63f | 674 | { |
andrewbonney | 0:ec559500a63f | 675 | if( readLine(line, 128) > 0 ) |
andrewbonney | 0:ec559500a63f | 676 | { |
andrewbonney | 0:ec559500a63f | 677 | //Check RC |
andrewbonney | 0:ec559500a63f | 678 | m_httpResponseCode = 0; |
andrewbonney | 0:ec559500a63f | 679 | if( sscanf(line, "HTTP/%*d.%*d %d %*[^\r\n]", &m_httpResponseCode) != 1 ) |
andrewbonney | 0:ec559500a63f | 680 | { |
andrewbonney | 0:ec559500a63f | 681 | //Cannot match string, error |
andrewbonney | 0:ec559500a63f | 682 | DBG("Not a correct HTTP answer : %s\n", line); |
andrewbonney | 0:ec559500a63f | 683 | onResult(HTTP_PRTCL); |
andrewbonney | 0:ec559500a63f | 684 | close(); |
andrewbonney | 0:ec559500a63f | 685 | return false; |
andrewbonney | 0:ec559500a63f | 686 | } |
andrewbonney | 0:ec559500a63f | 687 | |
andrewbonney | 0:ec559500a63f | 688 | if(m_httpResponseCode != 200) |
andrewbonney | 0:ec559500a63f | 689 | { |
andrewbonney | 0:ec559500a63f | 690 | DBG("Response: error code %d\n", m_httpResponseCode); |
andrewbonney | 0:ec559500a63f | 691 | HTTPResult res = HTTP_ERROR; |
andrewbonney | 0:ec559500a63f | 692 | switch(m_httpResponseCode) |
andrewbonney | 0:ec559500a63f | 693 | { |
andrewbonney | 0:ec559500a63f | 694 | case 404: |
andrewbonney | 0:ec559500a63f | 695 | res = HTTP_NOTFOUND; |
andrewbonney | 0:ec559500a63f | 696 | break; |
andrewbonney | 0:ec559500a63f | 697 | case 403: |
andrewbonney | 0:ec559500a63f | 698 | res = HTTP_REFUSED; |
andrewbonney | 0:ec559500a63f | 699 | break; |
andrewbonney | 0:ec559500a63f | 700 | default: |
andrewbonney | 0:ec559500a63f | 701 | res = HTTP_ERROR; |
andrewbonney | 0:ec559500a63f | 702 | } |
andrewbonney | 0:ec559500a63f | 703 | onResult(res); |
andrewbonney | 0:ec559500a63f | 704 | close(); |
andrewbonney | 0:ec559500a63f | 705 | return false; |
andrewbonney | 0:ec559500a63f | 706 | } |
andrewbonney | 0:ec559500a63f | 707 | DBG("Response OK\n"); |
andrewbonney | 0:ec559500a63f | 708 | } |
andrewbonney | 0:ec559500a63f | 709 | else |
andrewbonney | 0:ec559500a63f | 710 | { |
andrewbonney | 0:ec559500a63f | 711 | //Empty packet, weird! |
andrewbonney | 0:ec559500a63f | 712 | DBG("Empty packet!\n"); |
andrewbonney | 0:ec559500a63f | 713 | onResult(HTTP_PRTCL); |
andrewbonney | 0:ec559500a63f | 714 | close(); |
andrewbonney | 0:ec559500a63f | 715 | return false; |
andrewbonney | 0:ec559500a63f | 716 | } |
andrewbonney | 0:ec559500a63f | 717 | } |
andrewbonney | 0:ec559500a63f | 718 | if (m_dataLen == -1) { |
andrewbonney | 0:ec559500a63f | 719 | m_dataLen = 0; |
andrewbonney | 0:ec559500a63f | 720 | } |
andrewbonney | 0:ec559500a63f | 721 | bool incomplete = false; |
andrewbonney | 0:ec559500a63f | 722 | bool firstrun = true; |
andrewbonney | 0:ec559500a63f | 723 | while( true ) |
andrewbonney | 0:ec559500a63f | 724 | { |
andrewbonney | 0:ec559500a63f | 725 | int readLen = readLine(line + m_dataLen, 128 - m_dataLen, &incomplete); |
andrewbonney | 0:ec559500a63f | 726 | if (readLen == 0 && firstrun) { |
andrewbonney | 0:ec559500a63f | 727 | m_dataLen = -1; |
andrewbonney | 0:ec559500a63f | 728 | return false; |
andrewbonney | 0:ec559500a63f | 729 | } |
andrewbonney | 0:ec559500a63f | 730 | firstrun = false; |
andrewbonney | 0:ec559500a63f | 731 | m_dataLen = 0; |
andrewbonney | 0:ec559500a63f | 732 | if( readLen <= 2 ) //if == 1 or 2, it is an empty line = end of headers |
andrewbonney | 0:ec559500a63f | 733 | { |
andrewbonney | 0:ec559500a63f | 734 | DBG("All headers read.\n"); |
andrewbonney | 0:ec559500a63f | 735 | m_state = HTTP_READ_DATA; |
andrewbonney | 0:ec559500a63f | 736 | break; |
andrewbonney | 0:ec559500a63f | 737 | } |
andrewbonney | 0:ec559500a63f | 738 | else if( incomplete == true ) |
andrewbonney | 0:ec559500a63f | 739 | { |
andrewbonney | 0:ec559500a63f | 740 | m_dataLen = readLen;//Sets data length available in buffer |
andrewbonney | 0:ec559500a63f | 741 | return false; |
andrewbonney | 0:ec559500a63f | 742 | } |
andrewbonney | 0:ec559500a63f | 743 | //DBG("Header : %s\n", line); |
andrewbonney | 0:ec559500a63f | 744 | int n = sscanf(line, "%[^:] : %[^\r\n]", key, value); |
andrewbonney | 0:ec559500a63f | 745 | if ( n == 2 ) |
andrewbonney | 0:ec559500a63f | 746 | { |
andrewbonney | 0:ec559500a63f | 747 | DBG("Read header : %s: %s\n", key, value); |
andrewbonney | 0:ec559500a63f | 748 | m_respHeaders[key] = value; |
andrewbonney | 0:ec559500a63f | 749 | } |
andrewbonney | 0:ec559500a63f | 750 | //TODO: Impl n==1 case (part 2 of previous header) |
andrewbonney | 0:ec559500a63f | 751 | } |
andrewbonney | 0:ec559500a63f | 752 | |
andrewbonney | 0:ec559500a63f | 753 | return true; |
andrewbonney | 0:ec559500a63f | 754 | } |
andrewbonney | 0:ec559500a63f | 755 | |
andrewbonney | 0:ec559500a63f | 756 | bool HTTPClient::writeHeaders() //Called at the first writeData call |
andrewbonney | 0:ec559500a63f | 757 | { |
andrewbonney | 0:ec559500a63f | 758 | static char* line = m_buf; |
andrewbonney | 0:ec559500a63f | 759 | const char* HTTP_METH_STR[] = {"GET", "POST", "HEAD"}; |
andrewbonney | 0:ec559500a63f | 760 | |
andrewbonney | 0:ec559500a63f | 761 | //Req |
andrewbonney | 0:ec559500a63f | 762 | sprintf(line, "%s %s HTTP/1.1\r\nHost: %s\r\n", HTTP_METH_STR[m_meth], m_path.c_str(), m_server.getName()); //Write request |
andrewbonney | 0:ec559500a63f | 763 | m_pTCPSocket->send(line, strlen(line)); |
andrewbonney | 0:ec559500a63f | 764 | DBG("Request: %s\n", line); |
andrewbonney | 0:ec559500a63f | 765 | |
andrewbonney | 0:ec559500a63f | 766 | DBG("Writing headers:\n"); |
andrewbonney | 0:ec559500a63f | 767 | map<string,string>::iterator it; |
andrewbonney | 0:ec559500a63f | 768 | for( it = m_reqHeaders.begin(); it != m_reqHeaders.end(); it++ ) |
andrewbonney | 0:ec559500a63f | 769 | { |
andrewbonney | 0:ec559500a63f | 770 | sprintf(line, "%s: %s\r\n", (*it).first.c_str(), (*it).second.c_str() ); |
andrewbonney | 0:ec559500a63f | 771 | DBG("\r\n%s", line); |
andrewbonney | 0:ec559500a63f | 772 | m_pTCPSocket->send(line, strlen(line)); |
andrewbonney | 0:ec559500a63f | 773 | } |
andrewbonney | 0:ec559500a63f | 774 | m_pTCPSocket->send("\r\n",2); //End of head |
andrewbonney | 0:ec559500a63f | 775 | m_state = HTTP_WRITE_DATA; |
andrewbonney | 0:ec559500a63f | 776 | return true; |
andrewbonney | 0:ec559500a63f | 777 | } |
andrewbonney | 0:ec559500a63f | 778 | |
andrewbonney | 0:ec559500a63f | 779 | int HTTPClient::readLine(char* str, int maxLen, bool* pIncomplete /* = NULL*/) |
andrewbonney | 0:ec559500a63f | 780 | { |
andrewbonney | 0:ec559500a63f | 781 | int ret; |
andrewbonney | 0:ec559500a63f | 782 | int len = 0; |
andrewbonney | 0:ec559500a63f | 783 | if(pIncomplete) |
andrewbonney | 0:ec559500a63f | 784 | *pIncomplete = false; |
andrewbonney | 0:ec559500a63f | 785 | for(int i = 0; i < maxLen - 1; i++) |
andrewbonney | 0:ec559500a63f | 786 | { |
andrewbonney | 0:ec559500a63f | 787 | ret = m_pTCPSocket->recv(str, 1); |
andrewbonney | 0:ec559500a63f | 788 | if(ret != 1) |
andrewbonney | 0:ec559500a63f | 789 | { |
andrewbonney | 0:ec559500a63f | 790 | if(pIncomplete) |
andrewbonney | 0:ec559500a63f | 791 | *pIncomplete = true; |
andrewbonney | 0:ec559500a63f | 792 | break; |
andrewbonney | 0:ec559500a63f | 793 | } |
andrewbonney | 0:ec559500a63f | 794 | if( (len > 1) && *(str-1)=='\r' && *str=='\n' ) |
andrewbonney | 0:ec559500a63f | 795 | { |
andrewbonney | 0:ec559500a63f | 796 | break; |
andrewbonney | 0:ec559500a63f | 797 | } |
andrewbonney | 0:ec559500a63f | 798 | else if( *str=='\n' ) |
andrewbonney | 0:ec559500a63f | 799 | { |
andrewbonney | 0:ec559500a63f | 800 | break; |
andrewbonney | 0:ec559500a63f | 801 | } |
andrewbonney | 0:ec559500a63f | 802 | str++; |
andrewbonney | 0:ec559500a63f | 803 | len++; |
andrewbonney | 0:ec559500a63f | 804 | } |
andrewbonney | 0:ec559500a63f | 805 | *str = 0; |
andrewbonney | 0:ec559500a63f | 806 | return len; |
andrewbonney | 0:ec559500a63f | 807 | } |