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