I have a problem getting this to work. Server only recieves half of the data being sent. Whats wrong

Dependencies:   mbed

Committer:
tax
Date:
Tue Mar 29 13:20:15 2011 +0000
Revision:
0:66300c77c6e9

        

Who changed what in which revision?

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