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