SecureDweet is a dweet.io library that works on HTTPS protocol using SecureHttpClient library. It'll be able to fetch or listen to Dweet APIs and JSON messages.
SecureHttpClient.h@0:b30bbf6fe90d, 2016-08-22 (annotated)
- Committer:
- faheem_chaudhary
- Date:
- Mon Aug 22 18:37:13 2016 +0000
- Revision:
- 0:b30bbf6fe90d
First-cut library code check-in. No guarantees to work.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
faheem_chaudhary | 0:b30bbf6fe90d | 1 | /* |
faheem_chaudhary | 0:b30bbf6fe90d | 2 | * SecureHttpClient.h |
faheem_chaudhary | 0:b30bbf6fe90d | 3 | * |
faheem_chaudhary | 0:b30bbf6fe90d | 4 | * Created on: Aug 15, 2016 |
faheem_chaudhary | 0:b30bbf6fe90d | 5 | * Author: Faheem Inayat |
faheem_chaudhary | 0:b30bbf6fe90d | 6 | * Created for: Renesas Electronics America HQ, Santa Clara, CA, USA |
faheem_chaudhary | 0:b30bbf6fe90d | 7 | * |
faheem_chaudhary | 0:b30bbf6fe90d | 8 | * Copyright (c) 2016 Renesas Electronics America (REA) and Faheem Inayat |
faheem_chaudhary | 0:b30bbf6fe90d | 9 | */ |
faheem_chaudhary | 0:b30bbf6fe90d | 10 | /* |
faheem_chaudhary | 0:b30bbf6fe90d | 11 | * MIT License |
faheem_chaudhary | 0:b30bbf6fe90d | 12 | * |
faheem_chaudhary | 0:b30bbf6fe90d | 13 | * Permission is hereby granted, free of charge, to any person obtaining a copy of this software |
faheem_chaudhary | 0:b30bbf6fe90d | 14 | * and associated documentation files (the "Software"), to deal in the Software without restriction, |
faheem_chaudhary | 0:b30bbf6fe90d | 15 | * including without limitation the rights to use, copy, modify, merge, publish, distribute, |
faheem_chaudhary | 0:b30bbf6fe90d | 16 | * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is |
faheem_chaudhary | 0:b30bbf6fe90d | 17 | * furnished to do so, subject to the following conditions: |
faheem_chaudhary | 0:b30bbf6fe90d | 18 | * |
faheem_chaudhary | 0:b30bbf6fe90d | 19 | * The above copyright notice and this permission notice shall be included in all copies or |
faheem_chaudhary | 0:b30bbf6fe90d | 20 | * substantial portions of the Software. |
faheem_chaudhary | 0:b30bbf6fe90d | 21 | * |
faheem_chaudhary | 0:b30bbf6fe90d | 22 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING |
faheem_chaudhary | 0:b30bbf6fe90d | 23 | * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
faheem_chaudhary | 0:b30bbf6fe90d | 24 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
faheem_chaudhary | 0:b30bbf6fe90d | 25 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
faheem_chaudhary | 0:b30bbf6fe90d | 26 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
faheem_chaudhary | 0:b30bbf6fe90d | 27 | */ |
faheem_chaudhary | 0:b30bbf6fe90d | 28 | |
faheem_chaudhary | 0:b30bbf6fe90d | 29 | #ifndef SECUREDWEET_SECUREHTTPCLIENT_H_ |
faheem_chaudhary | 0:b30bbf6fe90d | 30 | #define SECUREDWEET_SECUREHTTPCLIENT_H_ |
faheem_chaudhary | 0:b30bbf6fe90d | 31 | |
faheem_chaudhary | 0:b30bbf6fe90d | 32 | #define HTTP_CLIENT_DEFAULT_TIMEOUT 300000 |
faheem_chaudhary | 0:b30bbf6fe90d | 33 | |
faheem_chaudhary | 0:b30bbf6fe90d | 34 | #include "IHttpDataIn.h" |
faheem_chaudhary | 0:b30bbf6fe90d | 35 | #include "IHttpDataOut.h" |
faheem_chaudhary | 0:b30bbf6fe90d | 36 | #include "ISecureHttpClientChunkDataListener.h" |
faheem_chaudhary | 0:b30bbf6fe90d | 37 | |
faheem_chaudhary | 0:b30bbf6fe90d | 38 | #include "mbed.h" |
faheem_chaudhary | 0:b30bbf6fe90d | 39 | |
faheem_chaudhary | 0:b30bbf6fe90d | 40 | ///HTTP client results |
faheem_chaudhary | 0:b30bbf6fe90d | 41 | enum HttpResult |
faheem_chaudhary | 0:b30bbf6fe90d | 42 | { |
faheem_chaudhary | 0:b30bbf6fe90d | 43 | HTTP_PROCESSING, ///<Processing |
faheem_chaudhary | 0:b30bbf6fe90d | 44 | HTTP_PARSE, ///<url Parse error |
faheem_chaudhary | 0:b30bbf6fe90d | 45 | HTTP_DNS, ///<Could not resolve name |
faheem_chaudhary | 0:b30bbf6fe90d | 46 | HTTP_PRTCL, ///<Protocol error |
faheem_chaudhary | 0:b30bbf6fe90d | 47 | HTTP_NOTFOUND, ///<HTTP 404 Error |
faheem_chaudhary | 0:b30bbf6fe90d | 48 | HTTP_REFUSED, ///<HTTP 403 Error |
faheem_chaudhary | 0:b30bbf6fe90d | 49 | HTTP_ERROR, ///<HTTP xxx error |
faheem_chaudhary | 0:b30bbf6fe90d | 50 | HTTP_TIMEOUT, ///<Connection timeout |
faheem_chaudhary | 0:b30bbf6fe90d | 51 | HTTP_CONN, ///<Connection error |
faheem_chaudhary | 0:b30bbf6fe90d | 52 | HTTP_CLOSED, ///<Connection was closed by remote host |
faheem_chaudhary | 0:b30bbf6fe90d | 53 | HTTP_REDIRECT, ///<HTTP 300 - 303 |
faheem_chaudhary | 0:b30bbf6fe90d | 54 | HTTP_OK = 0, ///<Success |
faheem_chaudhary | 0:b30bbf6fe90d | 55 | }; |
faheem_chaudhary | 0:b30bbf6fe90d | 56 | |
faheem_chaudhary | 0:b30bbf6fe90d | 57 | /** |
faheem_chaudhary | 0:b30bbf6fe90d | 58 | * HTTPS Client based on mbed and wolfSSL HTTP Client |
faheem_chaudhary | 0:b30bbf6fe90d | 59 | */ |
faheem_chaudhary | 0:b30bbf6fe90d | 60 | class SecureHttpClient |
faheem_chaudhary | 0:b30bbf6fe90d | 61 | { |
faheem_chaudhary | 0:b30bbf6fe90d | 62 | private: |
faheem_chaudhary | 0:b30bbf6fe90d | 63 | enum HTTP_METH |
faheem_chaudhary | 0:b30bbf6fe90d | 64 | { |
faheem_chaudhary | 0:b30bbf6fe90d | 65 | HTTP_GET, HTTP_POST, HTTP_PUT, HTTP_DELETE, HTTP_HEAD |
faheem_chaudhary | 0:b30bbf6fe90d | 66 | }; |
faheem_chaudhary | 0:b30bbf6fe90d | 67 | |
faheem_chaudhary | 0:b30bbf6fe90d | 68 | HttpResult connect ( const char* url, HTTP_METH method, IHttpDataOut* pDataOut, IHttpDataIn* pDataIn, |
faheem_chaudhary | 0:b30bbf6fe90d | 69 | int timeout ); //Execute request |
faheem_chaudhary | 0:b30bbf6fe90d | 70 | HttpResult recv ( char* buf, size_t minLen, size_t maxLen, size_t* pReadLen ); //0 on success, err code on failure |
faheem_chaudhary | 0:b30bbf6fe90d | 71 | HttpResult send ( const char* buf, size_t len = 0 ); //0 on success, err code on failure |
faheem_chaudhary | 0:b30bbf6fe90d | 72 | HttpResult flush ( void ); //0 on success, err code on failure |
faheem_chaudhary | 0:b30bbf6fe90d | 73 | HttpResult parseURL ( const char* url, const char* scheme, char* host, size_t maxHostLen, |
faheem_chaudhary | 0:b30bbf6fe90d | 74 | uint16_t* port, char* path, size_t maxPathLen ); //Parse URL |
faheem_chaudhary | 0:b30bbf6fe90d | 75 | |
faheem_chaudhary | 0:b30bbf6fe90d | 76 | void wolfssl_free ( void ); |
faheem_chaudhary | 0:b30bbf6fe90d | 77 | HttpResult bAuth ( void ); |
faheem_chaudhary | 0:b30bbf6fe90d | 78 | |
faheem_chaudhary | 0:b30bbf6fe90d | 79 | //Parameters |
faheem_chaudhary | 0:b30bbf6fe90d | 80 | |
faheem_chaudhary | 0:b30bbf6fe90d | 81 | int m_timeout; |
faheem_chaudhary | 0:b30bbf6fe90d | 82 | |
faheem_chaudhary | 0:b30bbf6fe90d | 83 | const char * m_basicAuthUser; |
faheem_chaudhary | 0:b30bbf6fe90d | 84 | const char * m_basicAuthPassword; |
faheem_chaudhary | 0:b30bbf6fe90d | 85 | int m_httpResponseCode; |
faheem_chaudhary | 0:b30bbf6fe90d | 86 | |
faheem_chaudhary | 0:b30bbf6fe90d | 87 | const char * header; |
faheem_chaudhary | 0:b30bbf6fe90d | 88 | char * redirect_url; |
faheem_chaudhary | 0:b30bbf6fe90d | 89 | int redirect_url_size; |
faheem_chaudhary | 0:b30bbf6fe90d | 90 | int redirect; |
faheem_chaudhary | 0:b30bbf6fe90d | 91 | |
faheem_chaudhary | 0:b30bbf6fe90d | 92 | /* for wolfSSL */ |
faheem_chaudhary | 0:b30bbf6fe90d | 93 | int SSLver; |
faheem_chaudhary | 0:b30bbf6fe90d | 94 | uint16_t port; |
faheem_chaudhary | 0:b30bbf6fe90d | 95 | struct WOLFSSL_CTX* ctx; |
faheem_chaudhary | 0:b30bbf6fe90d | 96 | struct WOLFSSL * ssl; |
faheem_chaudhary | 0:b30bbf6fe90d | 97 | |
faheem_chaudhary | 0:b30bbf6fe90d | 98 | ISecureHttpClientChunkDataListener * m_chunkDataListener; |
faheem_chaudhary | 0:b30bbf6fe90d | 99 | |
faheem_chaudhary | 0:b30bbf6fe90d | 100 | public: |
faheem_chaudhary | 0:b30bbf6fe90d | 101 | SecureHttpClient (); |
faheem_chaudhary | 0:b30bbf6fe90d | 102 | virtual ~SecureHttpClient (); |
faheem_chaudhary | 0:b30bbf6fe90d | 103 | |
faheem_chaudhary | 0:b30bbf6fe90d | 104 | /** |
faheem_chaudhary | 0:b30bbf6fe90d | 105 | Provides a basic authentification feature (Base64 encoded username and password) |
faheem_chaudhary | 0:b30bbf6fe90d | 106 | Pass two NULL pointers to switch back to no authentication |
faheem_chaudhary | 0:b30bbf6fe90d | 107 | @param user username to use for authentication, must remain valid durlng the whole HTTP session |
faheem_chaudhary | 0:b30bbf6fe90d | 108 | @param user password to use for authentication, must remain valid durlng the whole HTTP session |
faheem_chaudhary | 0:b30bbf6fe90d | 109 | */ |
faheem_chaudhary | 0:b30bbf6fe90d | 110 | HttpResult basicAuth ( const char* user, const char* password ); //Basic Authentification |
faheem_chaudhary | 0:b30bbf6fe90d | 111 | |
faheem_chaudhary | 0:b30bbf6fe90d | 112 | //High Level setup functions |
faheem_chaudhary | 0:b30bbf6fe90d | 113 | /** Execute a GET request on the URL |
faheem_chaudhary | 0:b30bbf6fe90d | 114 | Blocks until completion |
faheem_chaudhary | 0:b30bbf6fe90d | 115 | @param url : url on which to execute the request |
faheem_chaudhary | 0:b30bbf6fe90d | 116 | @param pDataIn : pointer to an IHttpDataIn instance that will collect the data returned by the request, can be NULL |
faheem_chaudhary | 0:b30bbf6fe90d | 117 | @param timeout waiting timeout in ms (osWaitForever for blocking function, not recommended) |
faheem_chaudhary | 0:b30bbf6fe90d | 118 | @return 0 on success, HTTP error (<0) on failure |
faheem_chaudhary | 0:b30bbf6fe90d | 119 | */ |
faheem_chaudhary | 0:b30bbf6fe90d | 120 | HttpResult get ( const char* url, IHttpDataIn* pDataIn, int timeout = HTTP_CLIENT_DEFAULT_TIMEOUT ); //Blocking |
faheem_chaudhary | 0:b30bbf6fe90d | 121 | |
faheem_chaudhary | 0:b30bbf6fe90d | 122 | /** Execute a POST request on the URL |
faheem_chaudhary | 0:b30bbf6fe90d | 123 | Blocks until completion |
faheem_chaudhary | 0:b30bbf6fe90d | 124 | @param url : url on which to execute the request |
faheem_chaudhary | 0:b30bbf6fe90d | 125 | @param dataOut : a IHttpDataOut instance that contains the data that will be posted |
faheem_chaudhary | 0:b30bbf6fe90d | 126 | @param pDataIn : pointer to an IHttpDataIn instance that will collect the data returned by the request, can be NULL |
faheem_chaudhary | 0:b30bbf6fe90d | 127 | @param timeout waiting timeout in ms (osWaitForever for blocking function, not recommended) |
faheem_chaudhary | 0:b30bbf6fe90d | 128 | @return 0 on success, HTTP error (<0) on failure |
faheem_chaudhary | 0:b30bbf6fe90d | 129 | */ |
faheem_chaudhary | 0:b30bbf6fe90d | 130 | HttpResult post ( const char* url, const IHttpDataOut& dataOut, IHttpDataIn* pDataIn, int timeout = |
faheem_chaudhary | 0:b30bbf6fe90d | 131 | HTTP_CLIENT_DEFAULT_TIMEOUT ); //Blocking |
faheem_chaudhary | 0:b30bbf6fe90d | 132 | |
faheem_chaudhary | 0:b30bbf6fe90d | 133 | /** Execute a PUT request on the URL |
faheem_chaudhary | 0:b30bbf6fe90d | 134 | Blocks until completion |
faheem_chaudhary | 0:b30bbf6fe90d | 135 | @param url : url on which to execute the request |
faheem_chaudhary | 0:b30bbf6fe90d | 136 | @param dataOut : a IHttpDataOut instance that contains the data that will be put |
faheem_chaudhary | 0:b30bbf6fe90d | 137 | @param pDataIn : pointer to an IHttpDataIn instance that will collect the data returned by the request, can be NULL |
faheem_chaudhary | 0:b30bbf6fe90d | 138 | @param timeout waiting timeout in ms (osWaitForever for blocking function, not recommended) |
faheem_chaudhary | 0:b30bbf6fe90d | 139 | @return 0 on success, HTTP error (<0) on failure |
faheem_chaudhary | 0:b30bbf6fe90d | 140 | */ |
faheem_chaudhary | 0:b30bbf6fe90d | 141 | HttpResult put ( const char* url, const IHttpDataOut& dataOut, IHttpDataIn* pDataIn, int timeout = |
faheem_chaudhary | 0:b30bbf6fe90d | 142 | HTTP_CLIENT_DEFAULT_TIMEOUT ); //Blocking |
faheem_chaudhary | 0:b30bbf6fe90d | 143 | |
faheem_chaudhary | 0:b30bbf6fe90d | 144 | /** Execute a DELETE request on the URL |
faheem_chaudhary | 0:b30bbf6fe90d | 145 | Blocks until completion |
faheem_chaudhary | 0:b30bbf6fe90d | 146 | @param url : url on which to execute the request |
faheem_chaudhary | 0:b30bbf6fe90d | 147 | @param pDataIn : pointer to an IHttpDataIn instance that will collect the data returned by the request, can be NULL |
faheem_chaudhary | 0:b30bbf6fe90d | 148 | @param timeout waiting timeout in ms (osWaitForever for blocking function, not recommended) |
faheem_chaudhary | 0:b30bbf6fe90d | 149 | @return 0 on success, HTTP error (<0) on failure |
faheem_chaudhary | 0:b30bbf6fe90d | 150 | */ |
faheem_chaudhary | 0:b30bbf6fe90d | 151 | HttpResult del ( const char* url, IHttpDataIn* pDataIn, int timeout = HTTP_CLIENT_DEFAULT_TIMEOUT ); //Blocking |
faheem_chaudhary | 0:b30bbf6fe90d | 152 | |
faheem_chaudhary | 0:b30bbf6fe90d | 153 | /** Get last request's HTTP response code |
faheem_chaudhary | 0:b30bbf6fe90d | 154 | @return The HTTP response code of the last request |
faheem_chaudhary | 0:b30bbf6fe90d | 155 | */ |
faheem_chaudhary | 0:b30bbf6fe90d | 156 | int getHTTPResponseCode (); |
faheem_chaudhary | 0:b30bbf6fe90d | 157 | |
faheem_chaudhary | 0:b30bbf6fe90d | 158 | void setHeader ( const char *header ); /* set http headers */ |
faheem_chaudhary | 0:b30bbf6fe90d | 159 | HttpResult setSslVersion ( int minorV ); /* set SSL/TLS version. 0: SSL3, 1: TLS1.0, 2: TLS1.1, 3: TLS1.2 */ |
faheem_chaudhary | 0:b30bbf6fe90d | 160 | void setRedirectUrlBuffer ( char *url, int size ); /* set URL buffer for redirection */ |
faheem_chaudhary | 0:b30bbf6fe90d | 161 | void setChunkDataListener ( ISecureHttpClientChunkDataListener * listener ); |
faheem_chaudhary | 0:b30bbf6fe90d | 162 | }; |
faheem_chaudhary | 0:b30bbf6fe90d | 163 | |
faheem_chaudhary | 0:b30bbf6fe90d | 164 | #endif /* SECUREDWEET_SECUREHTTPCLIENT_H_ */ |
faheem_chaudhary | 0:b30bbf6fe90d | 165 |