mbed Weather Platform firmware http://mbed.org/users/okini3939/notebook/mbed-weather-platform-firmware/

Dependencies:   EthernetNetIf SDHCFileSystem I2CLEDDisp Agentbed NTPClient_NetServices mbed BMP085 HTTPClient ConfigFile I2CLCD

Committer:
okini3939
Date:
Fri Dec 10 17:15:15 2010 +0000
Revision:
0:4265d973a98f

        

Who changed what in which revision?

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