Measure system

Dependencies:   EthernetNetIf mbed RF12B

Committer:
benecsj
Date:
Thu Mar 03 08:45:49 2011 +0000
Revision:
0:8d62137f7ff4
For FRIENDs.

Who changed what in which revision?

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