NetTribute library with debug turned on in FShandler Donatien Garner -> Segundo Equipo -> this version

Committer:
hexley
Date:
Fri Nov 19 01:54:45 2010 +0000
Revision:
0:281d6ff68967

        

Who changed what in which revision?

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