HTTP Client data container for form(multipart/form-data)

Dependencies:   mbed EthernetInterface HTTPClient mbed-rtos

Committer:
va009039
Date:
Thu May 31 10:32:39 2012 +0000
Revision:
0:fcd577a3925b

        

Who changed what in which revision?

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