Fork with custom headers and basic auth.

Dependents:   MbedSmartRest

Fork of HTTPClient by Kazushi Mukaiyama

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 "TCPSocketConnection.h"
00028 
00029 #define HTTP_CLIENT_DEFAULT_TIMEOUT 60000
00030 
00031 class HTTPData;
00032 
00033 #include "IHTTPData.h"
00034 #include "mbed.h"
00035 
00036 ///HTTP client results
00037 enum HTTPResult
00038 {
00039   HTTP_PROCESSING, ///<Processing
00040   HTTP_PARSE, ///<url Parse error
00041   HTTP_DNS, ///<Could not resolve name
00042   HTTP_PRTCL, ///<Protocol error
00043   HTTP_NOTFOUND, ///<HTTP 404 Error
00044   HTTP_REFUSED, ///<HTTP 403 Error
00045   HTTP_ERROR, ///<HTTP xxx error
00046   HTTP_TIMEOUT, ///<Connection timeout
00047   HTTP_CONN, ///<Connection error
00048   HTTP_CLOSED, ///<Connection was closed by remote host
00049   HTTP_OK = 0, ///<Success
00050 };
00051 
00052 /**A simple HTTP Client
00053 The HTTPClient is composed of:
00054 - The actual client (HTTPClient)
00055 - 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)
00056 */
00057 class HTTPClient
00058 {
00059 public:
00060   ///Instantiate the HTTP client
00061   HTTPClient();
00062   ~HTTPClient();
00063   
00064   /**
00065   Provides a basic authentification feature (Base64 encoded username and password)
00066   Pass two NULL pointers to switch back to no authentication
00067   @param user username to use for authentication, must remain valid durlng the whole HTTP session
00068   @param user password to use for authentication, must remain valid durlng the whole HTTP session
00069   */
00070   void basicAuth(const char* user, const char* password); //Basic Authentification
00071   
00072   /**
00073   Set custom headers for request.
00074   Pass NULL, 0 to turn off custom headers.
00075   @param headers an array (size multiple of two) key-value pairs, must remain valid during the whole HTTP session
00076   @param pairs number of key-value pairs
00077   */
00078   void customHeaders(const char** headers, size_t pairs);
00079   
00080   //High Level setup functions
00081   /** Execute a GET request on the URL
00082   Blocks until completion
00083   @param url : url on which to execute the request
00084   @param pDataIn : pointer to an IHTTPDataIn instance that will collect the data returned by the request, can be NULL
00085   @param timeout waiting timeout in ms (osWaitForever for blocking function, not recommended)
00086   @return 0 on success, HTTP error (<0) on failure
00087   */
00088   HTTPResult get(const char* url, IHTTPDataIn* pDataIn, int timeout = HTTP_CLIENT_DEFAULT_TIMEOUT); //Blocking
00089   
00090   /** Execute a GET request on the URL
00091   Blocks until completion
00092   This is a helper to directly get a piece of text from a HTTP result
00093   @param url : url on which to execute the request
00094   @param result : pointer to a char array in which the result will be stored
00095   @param maxResultLen : length of the char array (including space for the NULL-terminating char)
00096   @param timeout waiting timeout in ms (osWaitForever for blocking function, not recommended)
00097   @return 0 on success, HTTP error (<0) on failure
00098   */
00099   HTTPResult get(const char* url, char* result, size_t maxResultLen, int timeout = HTTP_CLIENT_DEFAULT_TIMEOUT); //Blocking
00100 
00101   /** Execute a POST request on the URL
00102   Blocks until completion
00103   @param url : url on which to execute the request
00104   @param dataOut : a IHTTPDataOut instance that contains the data that will be posted
00105   @param pDataIn : pointer to an IHTTPDataIn instance that will collect the data returned by the request, can be NULL
00106   @param timeout waiting timeout in ms (osWaitForever for blocking function, not recommended)
00107   @return 0 on success, HTTP error (<0) on failure
00108   */
00109   HTTPResult post(const char* url, const IHTTPDataOut& dataOut, IHTTPDataIn* pDataIn, int timeout = HTTP_CLIENT_DEFAULT_TIMEOUT); //Blocking
00110   
00111   /** Execute a PUT request on the URL
00112   Blocks until completion
00113   @param url : url on which to execute the request
00114   @param dataOut : a IHTTPDataOut instance that contains the data that will be put
00115   @param pDataIn : pointer to an IHTTPDataIn instance that will collect the data returned by the request, can be NULL
00116   @param timeout waiting timeout in ms (osWaitForever for blocking function, not recommended)
00117   @return 0 on success, HTTP error (<0) on failure
00118   */
00119   HTTPResult put(const char* url, const IHTTPDataOut& dataOut, IHTTPDataIn* pDataIn, int timeout = HTTP_CLIENT_DEFAULT_TIMEOUT); //Blocking
00120   
00121   /** Execute a DELETE request on the URL
00122   Blocks until completion
00123   @param url : url on which to execute the request
00124   @param pDataIn : pointer to an IHTTPDataIn instance that will collect the data returned by the request, can be NULL
00125   @param timeout waiting timeout in ms (osWaitForever for blocking function, not recommended)
00126   @return 0 on success, HTTP error (<0) on failure
00127   */
00128   HTTPResult del(const char* url, IHTTPDataIn* pDataIn, int timeout = HTTP_CLIENT_DEFAULT_TIMEOUT); //Blocking
00129   
00130   /** Get last request's HTTP response code
00131   @return The HTTP response code of the last request
00132   */
00133   int getHTTPResponseCode();
00134   
00135 private:
00136   enum HTTP_METH
00137   {
00138     HTTP_GET,
00139     HTTP_POST,
00140     HTTP_PUT,
00141     HTTP_DELETE,
00142     HTTP_HEAD
00143   };
00144 
00145   HTTPResult connect(const char* url, HTTP_METH method, IHTTPDataOut* pDataOut, IHTTPDataIn* pDataIn, int timeout); //Execute request
00146   HTTPResult recv(char* buf, size_t minLen, size_t maxLen, size_t* pReadLen); //0 on success, err code on failure
00147   HTTPResult send(char* buf, size_t len = 0); //0 on success, err code on failure
00148   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
00149   void createauth (const char *user, const char *pwd, char *buf, int len);
00150   int base64enc(const char *input, unsigned int length, char *output, int len);
00151 
00152   //Parameters
00153   TCPSocketConnection m_sock;
00154   
00155   int m_timeout;
00156 
00157   const char* m_basicAuthUser;
00158   const char* m_basicAuthPassword;
00159   const char** m_customHeaders;
00160   size_t m_nCustomHeaders;
00161   int m_httpResponseCode;
00162 
00163 };
00164 
00165 //Including data containers here for more convenience
00166 #include "data/HTTPText.h"
00167 #include "data/HTTPMap.h"
00168 
00169 #endif