Dependents:   Lab3Translator lab3_Radio_design Sync WeatherPlatform_20110408 ... more

Committer:
mamezu
Date:
Thu Dec 09 01:36:34 2010 +0000
Revision:
0:62fac7f06c8d

        

Who changed what in which revision?

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