Michiel Berckvens / Mbed 2 deprecated ProjectHTTP

Dependencies:   DS1307 TextLCD mbed

Committer:
Michielber
Date:
Thu Dec 04 10:36:40 2014 +0000
Revision:
0:f615d151a72c
Berckvens Michiel & Basteyns Jonas 4/12/2014

Who changed what in which revision?

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