ST / HTTPClient

Dependents:   HTTPClient_HelloWorld_IDW01M1 wifigianluigi HTTPClient_HelloWorld_IDW01M1_Fabio_Ricezione

Fork of HTTPClient by ST Expansion SW Team

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers HTTPClient.h Source File

HTTPClient.h

Go to the documentation of this file.
00001 /* HTTPClient.h */
00002 /* Copyright (C) 2012 mbed.org, MIT License
00003  *
00004  * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
00005  * and associated documentation files (the "Software"), to deal in the Software without restriction,
00006  * including without limitation the rights to use, copy, modify, merge, publish, distribute,
00007  * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
00008  * furnished to do so, subject to the following conditions:
00009  *
00010  * The above copyright notice and this permission notice shall be included in all copies or
00011  * substantial portions of the Software.
00012  *
00013  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
00014  * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
00015  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
00016  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00017  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00018  */
00019 
00020 /** \file
00021 HTTP Client header file
00022 */
00023 
00024 #ifndef HTTP_CLIENT_H
00025 #define HTTP_CLIENT_H
00026 
00027 #include "NetworkStack.h"
00028 #include "TCPSocket.h"
00029 #include "SpwfInterface.h"
00030 
00031 #define LICIO
00032 #define HTTP_CLIENT_DEFAULT_TIMEOUT 15000
00033 #define CHUNK_SIZE 256
00034 
00035 class HTTPData;
00036 
00037 #include "IHTTPData.h"
00038 
00039 #include "mbed.h"
00040 
00041 ///HTTP client results
00042 enum HTTPResult
00043 {
00044   HTTP_PROCESSING, ///<Processing
00045   HTTP_PARSE, ///<url Parse error
00046   HTTP_DNS, ///<Could not resolve name
00047   HTTP_PRTCL, ///<Protocol error
00048   HTTP_NOTFOUND, ///<HTTP 404 Error
00049   HTTP_REFUSED, ///<HTTP 403 Error
00050   HTTP_ERROR, ///<HTTP xxx error
00051   HTTP_TIMEOUT, ///<Connection timeout
00052   HTTP_CONN, ///<Connection error
00053   HTTP_CLOSED, ///<Connection was closed by remote host
00054   HTTP_OK = 0, ///<Success
00055 };
00056 
00057 /**A simple HTTP Client
00058 The HTTPClient is composed of:
00059 - The actual client (HTTPClient)
00060 - 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)
00061 */
00062 class HTTPClient
00063 {
00064 public:
00065   ///Instantiate the HTTP client
00066 //  HTTPClient(NetworkStack & _m_intf);      
00067   HTTPClient(SpwfSAInterface & _m_intf);      
00068 
00069   ~HTTPClient();
00070   
00071 #if 0 //TODO add header handlers
00072   /**
00073   Provides a basic authentification feature (Base64 encoded username and password)
00074   Pass two NULL pointers to switch back to no authentication
00075   @param user username to use for authentication, must remain valid durlng the whole HTTP session
00076   @param user password to use for authentication, must remain valid durlng the whole HTTP session
00077   */
00078   void basicAuth(const char* user, const char* password); //Basic Authentification
00079 #endif
00080   
00081   //High Level setup functions
00082   /** Execute a GET request on the URL
00083   Blocks until completion
00084   @param url : url on which to execute the request
00085   @param pDataIn : pointer to an IHTTPDataIn instance that will collect the data returned by the request, can be NULL
00086   @param timeout waiting timeout in ms (osWaitForever for blocking function, not recommended)
00087   @return 0 on success, HTTP error (<0) on failure
00088   */
00089   HTTPResult get(const char* url, IHTTPDataIn* pDataIn, int timeout /*= HTTP_CLIENT_DEFAULT_TIMEOUT*/); //Blocking
00090   
00091   /** Execute a GET request on the URL
00092   Blocks until completion
00093   This is a helper to directly get a piece of text from a HTTP result
00094   @param url : url on which to execute the request
00095   @param result : pointer to a char array in which the result will be stored
00096   @param maxResultLen : length of the char array (including space for the NULL-terminating char)
00097   @param timeout waiting timeout in ms (osWaitForever for blocking function, not recommended)
00098   @return 0 on success, HTTP error (<0) on failure
00099   */
00100   HTTPResult get(const char* url, char* result, size_t maxResultLen, int timeout /*= HTTP_CLIENT_DEFAULT_TIMEOUT*/); //Blocking
00101 
00102   /** Execute a POST request on the URL
00103   Blocks until completion
00104   @param url : url on which to execute the request
00105   @param dataOut : a IHTTPDataOut instance that contains the data that will be posted
00106   @param pDataIn : pointer to an IHTTPDataIn instance that will collect the data returned by the request, can be NULL
00107   @param timeout waiting timeout in ms (osWaitForever for blocking function, not recommended)
00108   @return 0 on success, HTTP error (<0) on failure
00109   */
00110   HTTPResult post(const char* url, const IHTTPDataOut& dataOut, IHTTPDataIn* pDataIn, int timeout /*= HTTP_CLIENT_DEFAULT_TIMEOUT*/); //Blocking
00111   
00112   /** Execute a PUT request on the URL
00113   Blocks until completion
00114   @param url : url on which to execute the request
00115   @param dataOut : a IHTTPDataOut instance that contains the data that will be put
00116   @param pDataIn : pointer to an IHTTPDataIn instance that will collect the data returned by the request, can be NULL
00117   @param timeout waiting timeout in ms (osWaitForever for blocking function, not recommended)
00118   @return 0 on success, HTTP error (<0) on failure
00119   */
00120   HTTPResult put(const char* url, const IHTTPDataOut& dataOut, IHTTPDataIn* pDataIn, int timeout = HTTP_CLIENT_DEFAULT_TIMEOUT); //Blocking
00121   
00122   /** Execute a DELETE request on the URL
00123   Blocks until completion
00124   @param url : url on which to execute the request
00125   @param pDataIn : pointer to an IHTTPDataIn instance that will collect the data returned by the request, can be NULL
00126   @param timeout waiting timeout in ms (osWaitForever for blocking function, not recommended)
00127   @return 0 on success, HTTP error (<0) on failure
00128   */
00129   HTTPResult del(const char* url, IHTTPDataIn* pDataIn, int timeout = HTTP_CLIENT_DEFAULT_TIMEOUT); //Blocking
00130   
00131   /** Get last request's HTTP response code
00132   @return The HTTP response code of the last request
00133   */
00134   int getHTTPResponseCode();
00135   
00136 private:
00137   enum HTTP_METH
00138   {
00139     HTTP_GET,
00140     HTTP_POST,
00141     HTTP_PUT,
00142     HTTP_DELETE,
00143     HTTP_HEAD
00144   };
00145 
00146   HTTPResult connect(const char* url, HTTP_METH method, IHTTPDataOut* pDataOut, IHTTPDataIn* pDataIn, int timeout); //Execute request
00147   HTTPResult recv(char* buf, size_t minLen, size_t maxLen, size_t* pReadLen); //0 on success, err code on failure
00148   HTTPResult send(char* buf, size_t len = 0); //0 on success, err code on failure
00149   HTTPResult parseURL(const char* url, char* scheme, size_t maxSchemeLen, char* host, size_t maxHostLen, uint16_t* port, char* path, size_t maxPathLen); //Parse URL
00150 
00151   //Parameters 
00152  // NetworkStack & m_intf;  // WiFi interface
00153   SpwfSAInterface & m_intf;  // WiFi interface
00154   TCPSocket  m_sock;         // TCP socket
00155 
00156   int m_timeout;
00157 
00158   const char* m_basicAuthUser;
00159   const char* m_basicAuthPassword;
00160   int m_httpResponseCode;
00161   char buf[CHUNK_SIZE];
00162 };
00163 
00164 //Including data containers here for more convenience
00165 #include "data/HTTPText.h"
00166 #include "data/HTTPMap.h"
00167 
00168 #endif