Netservices modded to read fragmented HTTP respsonse/payload from special purpose server - 180 bytes only

Committer:
RodColeman
Date:
Thu Sep 08 10:41:36 2011 +0000
Revision:
0:8f5825f330b0
setDataLen hacked to 180bytes

Who changed what in which revision?

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