SNMP agent attached to SPI slave

Dependencies:   mbed

Committer:
lorcansmith
Date:
Mon Aug 13 15:07:40 2012 +0000
Revision:
0:2a53a4c3238c
v1.1 release includes ioAlarm traps

Who changed what in which revision?

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