Modified version of NetServices. Fixes an issue where connections failed should the HTTP response status line be received in a packet on its own prior to any further headers. Changes are made to the HTTPClient.cpp file's readHeaders method.

Committer:
andrewbonney
Date:
Fri Apr 08 14:39:41 2011 +0000
Revision:
0:ec559500a63f

        

Who changed what in which revision?

UserRevisionLine numberNew contents of line
andrewbonney 0:ec559500a63f 1
andrewbonney 0:ec559500a63f 2 /*
andrewbonney 0:ec559500a63f 3 Copyright (c) 2010 Donatien Garnier (donatiengar [at] gmail [dot] com)
andrewbonney 0:ec559500a63f 4
andrewbonney 0:ec559500a63f 5 Permission is hereby granted, free of charge, to any person obtaining a copy
andrewbonney 0:ec559500a63f 6 of this software and associated documentation files (the "Software"), to deal
andrewbonney 0:ec559500a63f 7 in the Software without restriction, including without limitation the rights
andrewbonney 0:ec559500a63f 8 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
andrewbonney 0:ec559500a63f 9 copies of the Software, and to permit persons to whom the Software is
andrewbonney 0:ec559500a63f 10 furnished to do so, subject to the following conditions:
andrewbonney 0:ec559500a63f 11
andrewbonney 0:ec559500a63f 12 The above copyright notice and this permission notice shall be included in
andrewbonney 0:ec559500a63f 13 all copies or substantial portions of the Software.
andrewbonney 0:ec559500a63f 14
andrewbonney 0:ec559500a63f 15 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
andrewbonney 0:ec559500a63f 16 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
andrewbonney 0:ec559500a63f 17 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
andrewbonney 0:ec559500a63f 18 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
andrewbonney 0:ec559500a63f 19 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
andrewbonney 0:ec559500a63f 20 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
andrewbonney 0:ec559500a63f 21 THE SOFTWARE.
andrewbonney 0:ec559500a63f 22 */
andrewbonney 0:ec559500a63f 23
andrewbonney 0:ec559500a63f 24 /** \file
andrewbonney 0:ec559500a63f 25 HTTP Client header file
andrewbonney 0:ec559500a63f 26 */
andrewbonney 0:ec559500a63f 27
andrewbonney 0:ec559500a63f 28 #ifndef HTTP_CLIENT_H
andrewbonney 0:ec559500a63f 29 #define HTTP_CLIENT_H
andrewbonney 0:ec559500a63f 30
andrewbonney 0:ec559500a63f 31 class HTTPData;
andrewbonney 0:ec559500a63f 32
andrewbonney 0:ec559500a63f 33 #include "core/net.h"
andrewbonney 0:ec559500a63f 34 #include "api/TCPSocket.h"
andrewbonney 0:ec559500a63f 35 #include "api/DNSRequest.h"
andrewbonney 0:ec559500a63f 36 #include "HTTPData.h"
andrewbonney 0:ec559500a63f 37 #include "mbed.h"
andrewbonney 0:ec559500a63f 38
andrewbonney 0:ec559500a63f 39 #include <string>
andrewbonney 0:ec559500a63f 40 using std::string;
andrewbonney 0:ec559500a63f 41
andrewbonney 0:ec559500a63f 42 #include <map>
andrewbonney 0:ec559500a63f 43 using std::map;
andrewbonney 0:ec559500a63f 44
andrewbonney 0:ec559500a63f 45 ///HTTP client results
andrewbonney 0:ec559500a63f 46 enum HTTPResult
andrewbonney 0:ec559500a63f 47 {
andrewbonney 0:ec559500a63f 48 HTTP_OK, ///<Success
andrewbonney 0:ec559500a63f 49 HTTP_PROCESSING, ///<Processing
andrewbonney 0:ec559500a63f 50 HTTP_PARSE, ///<URI Parse error
andrewbonney 0:ec559500a63f 51 HTTP_DNS, ///<Could not resolve name
andrewbonney 0:ec559500a63f 52 HTTP_PRTCL, ///<Protocol error
andrewbonney 0:ec559500a63f 53 HTTP_NOTFOUND, ///<HTTP 404 Error
andrewbonney 0:ec559500a63f 54 HTTP_REFUSED, ///<HTTP 403 Error
andrewbonney 0:ec559500a63f 55 HTTP_ERROR, ///<HTTP xxx error
andrewbonney 0:ec559500a63f 56 HTTP_TIMEOUT, ///<Connection timeout
andrewbonney 0:ec559500a63f 57 HTTP_CONN ///<Connection error
andrewbonney 0:ec559500a63f 58 };
andrewbonney 0:ec559500a63f 59
andrewbonney 0:ec559500a63f 60 #include "core/netservice.h"
andrewbonney 0:ec559500a63f 61
andrewbonney 0:ec559500a63f 62 ///A simple HTTP Client
andrewbonney 0:ec559500a63f 63 /**
andrewbonney 0:ec559500a63f 64 The HTTPClient is composed of:
andrewbonney 0:ec559500a63f 65 - The actual client (HTTPClient)
andrewbonney 0:ec559500a63f 66 - Classes that act as a data repository, each of which deriving from the HTTPData class (HTTPText for short text content, HTTPFile for file I/O, HTTPMap for key/value pairs, and HTTPStream for streaming purposes)
andrewbonney 0:ec559500a63f 67 */
andrewbonney 0:ec559500a63f 68 class HTTPClient : protected NetService
andrewbonney 0:ec559500a63f 69 {
andrewbonney 0:ec559500a63f 70 public:
andrewbonney 0:ec559500a63f 71 ///Instantiates the HTTP client
andrewbonney 0:ec559500a63f 72 HTTPClient();
andrewbonney 0:ec559500a63f 73 virtual ~HTTPClient();
andrewbonney 0:ec559500a63f 74
andrewbonney 0:ec559500a63f 75 ///Provides a basic authentification feature (Base64 encoded username and password)
andrewbonney 0:ec559500a63f 76 void basicAuth(const char* user, const char* password); //Basic Authentification
andrewbonney 0:ec559500a63f 77
andrewbonney 0:ec559500a63f 78 //High Level setup functions
andrewbonney 0:ec559500a63f 79 ///Executes a GET Request (blocking)
andrewbonney 0:ec559500a63f 80 /**
andrewbonney 0:ec559500a63f 81 Executes a GET request on the URI uri
andrewbonney 0:ec559500a63f 82 @param uri : URI on which to execute the request
andrewbonney 0:ec559500a63f 83 @param pDataIn : pointer to an HTTPData instance that will collect the data returned by the request, can be NULL
andrewbonney 0:ec559500a63f 84 Blocks until completion
andrewbonney 0:ec559500a63f 85 */
andrewbonney 0:ec559500a63f 86 HTTPResult get(const char* uri, HTTPData* pDataIn); //Blocking
andrewbonney 0:ec559500a63f 87
andrewbonney 0:ec559500a63f 88 ///Executes a GET Request (non blocking)
andrewbonney 0:ec559500a63f 89 /**
andrewbonney 0:ec559500a63f 90 Executes a GET request on the URI uri
andrewbonney 0:ec559500a63f 91 @param uri : URI on which to execute the request
andrewbonney 0:ec559500a63f 92 @param pDataIn : pointer to an HTTPData instance that will collect the data returned by the request, can be NULL
andrewbonney 0:ec559500a63f 93 @param pMethod : callback function
andrewbonney 0:ec559500a63f 94 The function returns immediately and calls the callback on completion or error
andrewbonney 0:ec559500a63f 95 */
andrewbonney 0:ec559500a63f 96 HTTPResult get(const char* uri, HTTPData* pDataIn, void (*pMethod)(HTTPResult)); //Non blocking
andrewbonney 0:ec559500a63f 97
andrewbonney 0:ec559500a63f 98 ///Executes a GET Request (non blocking)
andrewbonney 0:ec559500a63f 99 /**
andrewbonney 0:ec559500a63f 100 Executes a GET request on the URI uri
andrewbonney 0:ec559500a63f 101 @param uri : URI on which to execute the request
andrewbonney 0:ec559500a63f 102 @param pDataIn : pointer to an HTTPData instance that will collect the data returned by the request, can be NULL
andrewbonney 0:ec559500a63f 103 @param pItem : instance of class on which to execute the callback method
andrewbonney 0:ec559500a63f 104 @param pMethod : callback method
andrewbonney 0:ec559500a63f 105 The function returns immediately and calls the callback on completion or error
andrewbonney 0:ec559500a63f 106 */
andrewbonney 0:ec559500a63f 107 template<class T>
andrewbonney 0:ec559500a63f 108 HTTPResult get(const char* uri, HTTPData* pDataIn, T* pItem, void (T::*pMethod)(HTTPResult)) //Non blocking
andrewbonney 0:ec559500a63f 109 {
andrewbonney 0:ec559500a63f 110 setOnResult(pItem, pMethod);
andrewbonney 0:ec559500a63f 111 doGet(uri, pDataIn);
andrewbonney 0:ec559500a63f 112 return HTTP_PROCESSING;
andrewbonney 0:ec559500a63f 113 }
andrewbonney 0:ec559500a63f 114
andrewbonney 0:ec559500a63f 115 ///Executes a POST Request (blocking)
andrewbonney 0:ec559500a63f 116 /**
andrewbonney 0:ec559500a63f 117 Executes a POST request on the URI uri
andrewbonney 0:ec559500a63f 118 @param uri : URI on which to execute the request
andrewbonney 0:ec559500a63f 119 @param dataOut : a HTTPData instance that contains the data that will be posted
andrewbonney 0:ec559500a63f 120 @param pDataIn : pointer to an HTTPData instance that will collect the data returned by the request, can be NULL
andrewbonney 0:ec559500a63f 121 Blocks until completion
andrewbonney 0:ec559500a63f 122 */
andrewbonney 0:ec559500a63f 123 HTTPResult post(const char* uri, const HTTPData& dataOut, HTTPData* pDataIn); //Blocking
andrewbonney 0:ec559500a63f 124
andrewbonney 0:ec559500a63f 125 ///Executes a POST Request (non blocking)
andrewbonney 0:ec559500a63f 126 /**
andrewbonney 0:ec559500a63f 127 Executes a POST request on the URI uri
andrewbonney 0:ec559500a63f 128 @param uri : URI on which to execute the request
andrewbonney 0:ec559500a63f 129 @param dataOut : a HTTPData instance that contains the data that will be posted
andrewbonney 0:ec559500a63f 130 @param pDataIn : pointer to an HTTPData instance that will collect the data returned by the request, can be NULL
andrewbonney 0:ec559500a63f 131 @param pMethod : callback function
andrewbonney 0:ec559500a63f 132 The function returns immediately and calls the callback on completion or error
andrewbonney 0:ec559500a63f 133 */
andrewbonney 0:ec559500a63f 134 HTTPResult post(const char* uri, const HTTPData& dataOut, HTTPData* pDataIn, void (*pMethod)(HTTPResult)); //Non blocking
andrewbonney 0:ec559500a63f 135
andrewbonney 0:ec559500a63f 136 ///Executes a POST Request (non blocking)
andrewbonney 0:ec559500a63f 137 /**
andrewbonney 0:ec559500a63f 138 Executes a POST request on the URI uri
andrewbonney 0:ec559500a63f 139 @param uri : URI on which to execute the request
andrewbonney 0:ec559500a63f 140 @param dataOut : a HTTPData instance that contains the data that will be posted
andrewbonney 0:ec559500a63f 141 @param pDataIn : pointer to an HTTPData instance that will collect the data returned by the request, can be NULL
andrewbonney 0:ec559500a63f 142 @param pItem : instance of class on which to execute the callback method
andrewbonney 0:ec559500a63f 143 @param pMethod : callback method
andrewbonney 0:ec559500a63f 144 The function returns immediately and calls the callback on completion or error
andrewbonney 0:ec559500a63f 145 */
andrewbonney 0:ec559500a63f 146 template<class T>
andrewbonney 0:ec559500a63f 147 HTTPResult post(const char* uri, const HTTPData& dataOut, HTTPData* pDataIn, T* pItem, void (T::*pMethod)(HTTPResult)) //Non blocking
andrewbonney 0:ec559500a63f 148 {
andrewbonney 0:ec559500a63f 149 setOnResult(pItem, pMethod);
andrewbonney 0:ec559500a63f 150 doPost(uri, dataOut, pDataIn);
andrewbonney 0:ec559500a63f 151 return HTTP_PROCESSING;
andrewbonney 0:ec559500a63f 152 }
andrewbonney 0:ec559500a63f 153
andrewbonney 0:ec559500a63f 154 ///Executes a GET Request (non blocking)
andrewbonney 0:ec559500a63f 155 /**
andrewbonney 0:ec559500a63f 156 Executes a GET request on the URI uri
andrewbonney 0:ec559500a63f 157 @param uri : URI on which to execute the request
andrewbonney 0:ec559500a63f 158 @param pDataIn : pointer to an HTTPData instance that will collect the data returned by the request, can be NULL
andrewbonney 0:ec559500a63f 159 The function returns immediately and calls the previously set callback on completion or error
andrewbonney 0:ec559500a63f 160 */
andrewbonney 0:ec559500a63f 161 void doGet(const char* uri, HTTPData* pDataIn);
andrewbonney 0:ec559500a63f 162
andrewbonney 0:ec559500a63f 163 ///Executes a POST Request (non blocking)
andrewbonney 0:ec559500a63f 164 /**
andrewbonney 0:ec559500a63f 165 Executes a POST request on the URI uri
andrewbonney 0:ec559500a63f 166 @param uri : URI on which to execute the request
andrewbonney 0:ec559500a63f 167 @param dataOut : a HTTPData instance that contains the data that will be posted
andrewbonney 0:ec559500a63f 168 @param pDataIn : pointer to an HTTPData instance that will collect the data returned by the request, can be NULL
andrewbonney 0:ec559500a63f 169 @param pMethod : callback function
andrewbonney 0:ec559500a63f 170 The function returns immediately and calls the previously set callback on completion or error
andrewbonney 0:ec559500a63f 171 */
andrewbonney 0:ec559500a63f 172 void doPost(const char* uri, const HTTPData& dataOut, HTTPData* pDataIn);
andrewbonney 0:ec559500a63f 173
andrewbonney 0:ec559500a63f 174 ///Setups the result callback
andrewbonney 0:ec559500a63f 175 /**
andrewbonney 0:ec559500a63f 176 @param pMethod : callback function
andrewbonney 0:ec559500a63f 177 */
andrewbonney 0:ec559500a63f 178 void setOnResult( void (*pMethod)(HTTPResult) );
andrewbonney 0:ec559500a63f 179
andrewbonney 0:ec559500a63f 180 ///Setups the result callback
andrewbonney 0:ec559500a63f 181 /**
andrewbonney 0:ec559500a63f 182 @param pItem : instance of class on which to execute the callback method
andrewbonney 0:ec559500a63f 183 @param pMethod : callback method
andrewbonney 0:ec559500a63f 184 */
andrewbonney 0:ec559500a63f 185 class CDummy;
andrewbonney 0:ec559500a63f 186 template<class T>
andrewbonney 0:ec559500a63f 187 void setOnResult( T* pItem, void (T::*pMethod)(HTTPResult) )
andrewbonney 0:ec559500a63f 188 {
andrewbonney 0:ec559500a63f 189 m_pCb = NULL;
andrewbonney 0:ec559500a63f 190 m_pCbItem = (CDummy*) pItem;
andrewbonney 0:ec559500a63f 191 m_pCbMeth = (void (CDummy::*)(HTTPResult)) pMethod;
andrewbonney 0:ec559500a63f 192 }
andrewbonney 0:ec559500a63f 193
andrewbonney 0:ec559500a63f 194 ///Setups timeout
andrewbonney 0:ec559500a63f 195 /**
andrewbonney 0:ec559500a63f 196 @param ms : time of connection inactivity in ms after which the request should timeout
andrewbonney 0:ec559500a63f 197 */
andrewbonney 0:ec559500a63f 198 void setTimeout(int ms);
andrewbonney 0:ec559500a63f 199
andrewbonney 0:ec559500a63f 200 virtual void poll(); //Called by NetServices
andrewbonney 0:ec559500a63f 201
andrewbonney 0:ec559500a63f 202 ///Gets last request's HTTP response code
andrewbonney 0:ec559500a63f 203 /**
andrewbonney 0:ec559500a63f 204 @return The HTTP response code of the last request
andrewbonney 0:ec559500a63f 205 */
andrewbonney 0:ec559500a63f 206 int getHTTPResponseCode();
andrewbonney 0:ec559500a63f 207
andrewbonney 0:ec559500a63f 208 ///Sets a specific request header
andrewbonney 0:ec559500a63f 209 void setRequestHeader(const string& header, const string& value);
andrewbonney 0:ec559500a63f 210
andrewbonney 0:ec559500a63f 211 ///Gets a response header
andrewbonney 0:ec559500a63f 212 string& getResponseHeader(const string& header);
andrewbonney 0:ec559500a63f 213
andrewbonney 0:ec559500a63f 214 ///Clears request headers
andrewbonney 0:ec559500a63f 215 void resetRequestHeaders();
andrewbonney 0:ec559500a63f 216
andrewbonney 0:ec559500a63f 217 protected:
andrewbonney 0:ec559500a63f 218 void resetTimeout();
andrewbonney 0:ec559500a63f 219
andrewbonney 0:ec559500a63f 220 void init();
andrewbonney 0:ec559500a63f 221 void close();
andrewbonney 0:ec559500a63f 222
andrewbonney 0:ec559500a63f 223 void setup(const char* uri, HTTPData* pDataOut, HTTPData* pDataIn); //Setup request, make DNS Req if necessary
andrewbonney 0:ec559500a63f 224 void connect(); //Start Connection
andrewbonney 0:ec559500a63f 225
andrewbonney 0:ec559500a63f 226 int tryRead(); //Read data and try to feed output
andrewbonney 0:ec559500a63f 227 void readData(); //Data has been read
andrewbonney 0:ec559500a63f 228 void writeData(); //Data has been written & buf is free
andrewbonney 0:ec559500a63f 229
andrewbonney 0:ec559500a63f 230 void onTCPSocketEvent(TCPSocketEvent e);
andrewbonney 0:ec559500a63f 231 void onDNSReply(DNSReply r);
andrewbonney 0:ec559500a63f 232 void onResult(HTTPResult r); //Called when exchange completed or on failure
andrewbonney 0:ec559500a63f 233 void onTimeout(); //Connection has timed out
andrewbonney 0:ec559500a63f 234
andrewbonney 0:ec559500a63f 235 private:
andrewbonney 0:ec559500a63f 236 HTTPResult blockingProcess(); //Called in blocking mode, calls Net::poll() until return code is available
andrewbonney 0:ec559500a63f 237
andrewbonney 0:ec559500a63f 238 bool readHeaders(); //Called first when receiving data
andrewbonney 0:ec559500a63f 239 bool writeHeaders(); //Called to create req
andrewbonney 0:ec559500a63f 240 int readLine(char* str, int maxLen, bool* pIncomplete = NULL);
andrewbonney 0:ec559500a63f 241
andrewbonney 0:ec559500a63f 242 enum HTTP_METH
andrewbonney 0:ec559500a63f 243 {
andrewbonney 0:ec559500a63f 244 HTTP_GET,
andrewbonney 0:ec559500a63f 245 HTTP_POST,
andrewbonney 0:ec559500a63f 246 HTTP_HEAD
andrewbonney 0:ec559500a63f 247 };
andrewbonney 0:ec559500a63f 248
andrewbonney 0:ec559500a63f 249 HTTP_METH m_meth;
andrewbonney 0:ec559500a63f 250
andrewbonney 0:ec559500a63f 251 CDummy* m_pCbItem;
andrewbonney 0:ec559500a63f 252 void (CDummy::*m_pCbMeth)(HTTPResult);
andrewbonney 0:ec559500a63f 253
andrewbonney 0:ec559500a63f 254 void (*m_pCb)(HTTPResult);
andrewbonney 0:ec559500a63f 255
andrewbonney 0:ec559500a63f 256 TCPSocket* m_pTCPSocket;
andrewbonney 0:ec559500a63f 257 map<string, string> m_reqHeaders;
andrewbonney 0:ec559500a63f 258 map<string, string> m_respHeaders;
andrewbonney 0:ec559500a63f 259
andrewbonney 0:ec559500a63f 260 Timer m_watchdog;
andrewbonney 0:ec559500a63f 261 int m_timeout;
andrewbonney 0:ec559500a63f 262
andrewbonney 0:ec559500a63f 263 DNSRequest* m_pDnsReq;
andrewbonney 0:ec559500a63f 264
andrewbonney 0:ec559500a63f 265 Host m_server;
andrewbonney 0:ec559500a63f 266 string m_path;
andrewbonney 0:ec559500a63f 267
andrewbonney 0:ec559500a63f 268 bool m_closed;
andrewbonney 0:ec559500a63f 269
andrewbonney 0:ec559500a63f 270 enum HTTPStep
andrewbonney 0:ec559500a63f 271 {
andrewbonney 0:ec559500a63f 272 // HTTP_INIT,
andrewbonney 0:ec559500a63f 273 HTTP_WRITE_HEADERS,
andrewbonney 0:ec559500a63f 274 HTTP_WRITE_DATA,
andrewbonney 0:ec559500a63f 275 HTTP_READ_HEADERS,
andrewbonney 0:ec559500a63f 276 HTTP_READ_DATA,
andrewbonney 0:ec559500a63f 277 HTTP_READ_DATA_INCOMPLETE,
andrewbonney 0:ec559500a63f 278 HTTP_DONE,
andrewbonney 0:ec559500a63f 279 HTTP_CLOSED
andrewbonney 0:ec559500a63f 280 };
andrewbonney 0:ec559500a63f 281
andrewbonney 0:ec559500a63f 282 HTTPStep m_state;
andrewbonney 0:ec559500a63f 283
andrewbonney 0:ec559500a63f 284 HTTPData* m_pDataOut;
andrewbonney 0:ec559500a63f 285 HTTPData* m_pDataIn;
andrewbonney 0:ec559500a63f 286
andrewbonney 0:ec559500a63f 287 bool m_dataChunked; //Data is encoded as chunks
andrewbonney 0:ec559500a63f 288 int m_dataPos; //Position in data
andrewbonney 0:ec559500a63f 289 int m_dataLen; //Data length
andrewbonney 0:ec559500a63f 290 char* m_buf;
andrewbonney 0:ec559500a63f 291 char* m_pBufRemaining; //Remaining
andrewbonney 0:ec559500a63f 292 int m_bufRemainingLen; //Data length in m_pBufRemaining
andrewbonney 0:ec559500a63f 293
andrewbonney 0:ec559500a63f 294 int m_httpResponseCode;
andrewbonney 0:ec559500a63f 295
andrewbonney 0:ec559500a63f 296 HTTPResult m_blockingResult; //Result if blocking mode
andrewbonney 0:ec559500a63f 297
andrewbonney 0:ec559500a63f 298 };
andrewbonney 0:ec559500a63f 299
andrewbonney 0:ec559500a63f 300 //Including data containers here for more convenience
andrewbonney 0:ec559500a63f 301 #include "data/HTTPFile.h"
andrewbonney 0:ec559500a63f 302 #include "data/HTTPStream.h"
andrewbonney 0:ec559500a63f 303 #include "data/HTTPText.h"
andrewbonney 0:ec559500a63f 304 #include "data/HTTPMap.h"
andrewbonney 0:ec559500a63f 305
andrewbonney 0:ec559500a63f 306 #endif