TwitterExample with newer library (2012Aug)

Dependencies:   EthernetNetIf HTTPClient mbed

Committer:
nxpfan
Date:
Wed Aug 29 03:50:19 2012 +0000
Revision:
0:075157567b0c
simple twitter example with newer library

Who changed what in which revision?

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