Used in Live Traffic Update Nokia LCD Display Project

Fork of NetServices by Segundo Equipo

Committer:
segundo
Date:
Tue Nov 09 20:54:15 2010 +0000
Revision:
0:ac1725ba162c

        

Who changed what in which revision?

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