Version of http://mbed.org/cookbook/NetServicesTribute with setting set the same for LPC2368

Dependents:   UDPSocketExample 24LCxx_I2CApp WeatherPlatform_pachube HvZServerLib ... more

Committer:
simon
Date:
Tue Nov 23 14:15:36 2010 +0000
Revision:
0:350011bf8be7
Experimental version for testing UDP

Who changed what in which revision?

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