Oct122012mbedLab

Dependents:   Lab3_VoiceMeter

Fork of HTTPClient by masa haru

Committer:
psawant9
Date:
Fri Oct 12 16:02:09 2012 +0000
Revision:
1:b77740c0a846
Parent:
0:62fac7f06c8d
Done

Who changed what in which revision?

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