Renesas / SecureDweet
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers SecureHttpClient.h Source File

SecureHttpClient.h

00001 /*
00002  * SecureHttpClient.h
00003  *
00004  *  Created on: Aug 15, 2016
00005  *      Author: Faheem Inayat
00006  * Created for: Renesas Electronics America HQ, Santa Clara, CA, USA
00007  * 
00008  * Copyright (c) 2016 Renesas Electronics America (REA) and Faheem Inayat
00009  */
00010 /*
00011  * MIT License
00012  *
00013  * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
00014  * and associated documentation files (the "Software"), to deal in the Software without restriction,
00015  * including without limitation the rights to use, copy, modify, merge, publish, distribute,
00016  * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
00017  * furnished to do so, subject to the following conditions:
00018  *
00019  * The above copyright notice and this permission notice shall be included in all copies or
00020  * substantial portions of the Software.
00021  *
00022  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
00023  * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
00024  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
00025  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00026  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00027  */
00028 
00029 #ifndef SECUREDWEET_SECUREHTTPCLIENT_H_
00030     #define SECUREDWEET_SECUREHTTPCLIENT_H_
00031 
00032     #define HTTP_CLIENT_DEFAULT_TIMEOUT 300000
00033 
00034 #include "IHttpDataIn.h"
00035 #include "IHttpDataOut.h"
00036 #include "ISecureHttpClientChunkDataListener.h"
00037 
00038 #include "mbed.h"
00039 
00040 ///HTTP client results
00041 enum HttpResult
00042 {
00043     HTTP_PROCESSING, ///<Processing
00044     HTTP_PARSE, ///<url Parse error
00045     HTTP_DNS, ///<Could not resolve name
00046     HTTP_PRTCL, ///<Protocol error
00047     HTTP_NOTFOUND, ///<HTTP 404 Error
00048     HTTP_REFUSED, ///<HTTP 403 Error
00049     HTTP_ERROR, ///<HTTP xxx error
00050     HTTP_TIMEOUT, ///<Connection timeout
00051     HTTP_CONN, ///<Connection error
00052     HTTP_CLOSED, ///<Connection was closed by remote host
00053     HTTP_REDIRECT, ///<HTTP 300 - 303
00054     HTTP_OK = 0, ///<Success
00055 };
00056 
00057 /**
00058  * HTTPS Client based on mbed and wolfSSL HTTP Client
00059  */
00060 class SecureHttpClient
00061 {
00062     private:
00063         enum HTTP_METH
00064         {
00065             HTTP_GET, HTTP_POST, HTTP_PUT, HTTP_DELETE, HTTP_HEAD
00066         };
00067 
00068         HttpResult connect ( const char* url, HTTP_METH method, IHttpDataOut* pDataOut, IHttpDataIn* pDataIn,
00069                              int timeout ); //Execute request
00070         HttpResult recv ( char* buf, size_t minLen, size_t maxLen, size_t* pReadLen ); //0 on success, err code on failure
00071         HttpResult send ( const char* buf, size_t len = 0 ); //0 on success, err code on failure
00072         HttpResult flush ( void ); //0 on success, err code on failure
00073         HttpResult parseURL ( const char* url, const char* scheme, char* host, size_t maxHostLen,
00074                               uint16_t* port, char* path, size_t maxPathLen ); //Parse URL
00075 
00076         void wolfssl_free ( void );
00077         HttpResult bAuth ( void );
00078 
00079         //Parameters
00080 
00081         int m_timeout;
00082 
00083         const char * m_basicAuthUser;
00084         const char * m_basicAuthPassword;
00085         int m_httpResponseCode;
00086 
00087         const char * header;
00088         char * redirect_url;
00089         int redirect_url_size;
00090         int redirect;
00091 
00092         /* for wolfSSL */
00093         int SSLver;
00094         uint16_t port;
00095         struct WOLFSSL_CTX* ctx;
00096         struct WOLFSSL * ssl;
00097 
00098         ISecureHttpClientChunkDataListener * m_chunkDataListener;
00099 
00100     public:
00101         SecureHttpClient ();
00102         virtual ~SecureHttpClient ();
00103 
00104         /**
00105          Provides a basic authentification feature (Base64 encoded username and password)
00106          Pass two NULL pointers to switch back to no authentication
00107          @param user username to use for authentication, must remain valid durlng the whole HTTP session
00108          @param user password to use for authentication, must remain valid durlng the whole HTTP session
00109          */
00110         HttpResult basicAuth ( const char* user, const char* password ); //Basic Authentification
00111 
00112         //High Level setup functions
00113         /** Execute a GET request on the URL
00114          Blocks until completion
00115          @param url : url on which to execute the request
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 get ( const char* url, IHttpDataIn* pDataIn, int timeout = HTTP_CLIENT_DEFAULT_TIMEOUT ); //Blocking
00121 
00122         /** Execute a POST request on the URL
00123          Blocks until completion
00124          @param url : url on which to execute the request
00125          @param dataOut : a IHttpDataOut instance that contains the data that will be posted
00126          @param pDataIn : pointer to an IHttpDataIn instance that will collect the data returned by the request, can be NULL
00127          @param timeout waiting timeout in ms (osWaitForever for blocking function, not recommended)
00128          @return 0 on success, HTTP error (<0) on failure
00129          */
00130         HttpResult post ( const char* url, const IHttpDataOut& dataOut, IHttpDataIn* pDataIn, int timeout =
00131         HTTP_CLIENT_DEFAULT_TIMEOUT ); //Blocking
00132 
00133         /** Execute a PUT request on the URL
00134          Blocks until completion
00135          @param url : url on which to execute the request
00136          @param dataOut : a IHttpDataOut instance that contains the data that will be put
00137          @param pDataIn : pointer to an IHttpDataIn instance that will collect the data returned by the request, can be NULL
00138          @param timeout waiting timeout in ms (osWaitForever for blocking function, not recommended)
00139          @return 0 on success, HTTP error (<0) on failure
00140          */
00141         HttpResult put ( const char* url, const IHttpDataOut& dataOut, IHttpDataIn* pDataIn, int timeout =
00142         HTTP_CLIENT_DEFAULT_TIMEOUT ); //Blocking
00143 
00144         /** Execute a DELETE request on the URL
00145          Blocks until completion
00146          @param url : url on which to execute the request
00147          @param pDataIn : pointer to an IHttpDataIn instance that will collect the data returned by the request, can be NULL
00148          @param timeout waiting timeout in ms (osWaitForever for blocking function, not recommended)
00149          @return 0 on success, HTTP error (<0) on failure
00150          */
00151         HttpResult del ( const char* url, IHttpDataIn* pDataIn, int timeout = HTTP_CLIENT_DEFAULT_TIMEOUT ); //Blocking
00152 
00153         /** Get last request's HTTP response code
00154          @return The HTTP response code of the last request
00155          */
00156         int getHTTPResponseCode ();
00157 
00158         void setHeader ( const char *header ); /* set http headers */
00159         HttpResult setSslVersion ( int minorV ); /* set SSL/TLS version. 0: SSL3, 1: TLS1.0, 2: TLS1.1, 3: TLS1.2 */
00160         void setRedirectUrlBuffer ( char *url, int size ); /* set URL buffer for redirection */
00161         void setChunkDataListener ( ISecureHttpClientChunkDataListener * listener );
00162 };
00163 
00164 #endif /* SECUREDWEET_SECUREHTTPCLIENT_H_ */
00165