Version of http://mbed.org/cookbook/NetServicesTribute with setting set the same for LPC2368

Dependents:   UDPSocketExample 24LCxx_I2CApp WeatherPlatform_pachube HvZServerLib ... more

Committer:
simon
Date:
Tue Nov 23 14:15:36 2010 +0000
Revision:
0:350011bf8be7
Experimental version for testing UDP

Who changed what in which revision?

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