Fork of the working HTTPClient adaptation using CyaSSL. This version adds a derivation of HTTPText called HTTPJson to emit JSON text properly. Additionally, the URL parser has defines that permit longer URLs to be utilized.
Dependents: SalesforceInterface df-2014-heroku-thermostat-k64f SalesforceInterface
Fork of HTTPClient by
This is a fork of the working HTTPS/SSL library that contains two extensions:
- HTTPJson - a derivation of HTTPText for emitting JSON strings specifically. No JSON parsing/checking is accomplished - HTTPJson simply sets the right Content-Type for HTTP(S).
- Expanded internal buffers for longer URLs. This is set in HTTPClient.cpp and is tunable.
HTTPClient.h@51:3bdf57f7fd60, 2015-08-20 (annotated)
- Committer:
- ansond
- Date:
- Thu Aug 20 18:55:41 2015 +0000
- Revision:
- 51:3bdf57f7fd60
- Parent:
- 50:a18a06b000f3
initial port of HTTPS library to make use of the mbedTLS library...
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
donatien | 0:2ccb9960a044 | 1 | /* HTTPClient.h */ |
donatien | 10:e1351de84c16 | 2 | /* Copyright (C) 2012 mbed.org, MIT License |
donatien | 10:e1351de84c16 | 3 | * |
donatien | 10:e1351de84c16 | 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy of this software |
donatien | 10:e1351de84c16 | 5 | * and associated documentation files (the "Software"), to deal in the Software without restriction, |
donatien | 10:e1351de84c16 | 6 | * including without limitation the rights to use, copy, modify, merge, publish, distribute, |
donatien | 10:e1351de84c16 | 7 | * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is |
donatien | 10:e1351de84c16 | 8 | * furnished to do so, subject to the following conditions: |
donatien | 10:e1351de84c16 | 9 | * |
donatien | 10:e1351de84c16 | 10 | * The above copyright notice and this permission notice shall be included in all copies or |
donatien | 10:e1351de84c16 | 11 | * substantial portions of the Software. |
donatien | 10:e1351de84c16 | 12 | * |
donatien | 10:e1351de84c16 | 13 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING |
donatien | 10:e1351de84c16 | 14 | * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
donatien | 10:e1351de84c16 | 15 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
donatien | 10:e1351de84c16 | 16 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
donatien | 10:e1351de84c16 | 17 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
donatien | 10:e1351de84c16 | 18 | */ |
donatien | 0:2ccb9960a044 | 19 | |
donatien | 0:2ccb9960a044 | 20 | /** \file |
donatien | 0:2ccb9960a044 | 21 | HTTP Client header file |
donatien | 0:2ccb9960a044 | 22 | */ |
donatien | 0:2ccb9960a044 | 23 | |
donatien | 0:2ccb9960a044 | 24 | #ifndef HTTP_CLIENT_H |
donatien | 0:2ccb9960a044 | 25 | #define HTTP_CLIENT_H |
donatien | 0:2ccb9960a044 | 26 | |
donatien | 12:89d09a6db00a | 27 | #define HTTP_CLIENT_DEFAULT_TIMEOUT 15000 |
donatien | 0:2ccb9960a044 | 28 | |
donatien | 0:2ccb9960a044 | 29 | class HTTPData; |
donatien | 0:2ccb9960a044 | 30 | |
donatien | 0:2ccb9960a044 | 31 | #include "IHTTPData.h" |
donatien | 0:2ccb9960a044 | 32 | #include "mbed.h" |
sarahmarshy | 50:a18a06b000f3 | 33 | #include "TCPSocketConnection.h" |
donatien | 0:2ccb9960a044 | 34 | |
ansond | 51:3bdf57f7fd60 | 35 | #include "polarssl/ssl.h" |
ansond | 51:3bdf57f7fd60 | 36 | #include "polarssl/net.h" |
ansond | 51:3bdf57f7fd60 | 37 | #include "polarssl/entropy.h" |
ansond | 51:3bdf57f7fd60 | 38 | #include "polarssl/ctr_drbg.h" |
ansond | 51:3bdf57f7fd60 | 39 | |
donatien | 0:2ccb9960a044 | 40 | ///HTTP client results |
wolfSSL | 18:d89df40b4cf3 | 41 | enum HTTPResult { |
wolfSSL | 18:d89df40b4cf3 | 42 | HTTP_PROCESSING, ///<Processing |
wolfSSL | 18:d89df40b4cf3 | 43 | HTTP_PARSE, ///<url Parse error |
wolfSSL | 18:d89df40b4cf3 | 44 | HTTP_DNS, ///<Could not resolve name |
wolfSSL | 18:d89df40b4cf3 | 45 | HTTP_PRTCL, ///<Protocol error |
wolfSSL | 18:d89df40b4cf3 | 46 | HTTP_NOTFOUND, ///<HTTP 404 Error |
wolfSSL | 18:d89df40b4cf3 | 47 | HTTP_REFUSED, ///<HTTP 403 Error |
wolfSSL | 18:d89df40b4cf3 | 48 | HTTP_ERROR, ///<HTTP xxx error |
wolfSSL | 18:d89df40b4cf3 | 49 | HTTP_TIMEOUT, ///<Connection timeout |
wolfSSL | 18:d89df40b4cf3 | 50 | HTTP_CONN, ///<Connection error |
wolfSSL | 18:d89df40b4cf3 | 51 | HTTP_CLOSED, ///<Connection was closed by remote host |
wolfSSL | 27:5d4739eae63e | 52 | HTTP_REDIRECT, ///<HTTP 300 - 303 |
wolfSSL | 18:d89df40b4cf3 | 53 | HTTP_OK = 0, ///<Success |
donatien | 0:2ccb9960a044 | 54 | }; |
donatien | 0:2ccb9960a044 | 55 | |
donatien | 0:2ccb9960a044 | 56 | /**A simple HTTP Client |
donatien | 0:2ccb9960a044 | 57 | The HTTPClient is composed of: |
donatien | 0:2ccb9960a044 | 58 | - The actual client (HTTPClient) |
donatien | 0:2ccb9960a044 | 59 | - 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) |
donatien | 0:2ccb9960a044 | 60 | */ |
donatien | 0:2ccb9960a044 | 61 | class HTTPClient |
donatien | 0:2ccb9960a044 | 62 | { |
donatien | 0:2ccb9960a044 | 63 | public: |
wolfSSL | 18:d89df40b4cf3 | 64 | ///Instantiate the HTTP client |
wolfSSL | 18:d89df40b4cf3 | 65 | HTTPClient(); |
wolfSSL | 18:d89df40b4cf3 | 66 | ~HTTPClient(); |
wolfSSL | 18:d89df40b4cf3 | 67 | |
wolfSSL | 18:d89df40b4cf3 | 68 | /** |
ansond | 36:debaeb6006a7 | 69 | Provides a OAUTH2 authentification feature |
ansond | 36:debaeb6006a7 | 70 | Pass NULL pointer to switch back to no authentication |
ansond | 36:debaeb6006a7 | 71 | @param token OAUTH2 token |
ansond | 36:debaeb6006a7 | 72 | */ |
ansond | 36:debaeb6006a7 | 73 | HTTPResult oauthToken(const char* token); // OAUTH2 Token Authentification |
ansond | 36:debaeb6006a7 | 74 | |
ansond | 36:debaeb6006a7 | 75 | /** |
wolfSSL | 18:d89df40b4cf3 | 76 | Provides a basic authentification feature (Base64 encoded username and password) |
wolfSSL | 18:d89df40b4cf3 | 77 | Pass two NULL pointers to switch back to no authentication |
wolfSSL | 18:d89df40b4cf3 | 78 | @param user username to use for authentication, must remain valid durlng the whole HTTP session |
wolfSSL | 18:d89df40b4cf3 | 79 | @param user password to use for authentication, must remain valid durlng the whole HTTP session |
wolfSSL | 18:d89df40b4cf3 | 80 | */ |
wolfSSL | 22:4b9a4151cc73 | 81 | HTTPResult basicAuth(const char* user, const char* password); //Basic Authentification |
wolfSSL | 18:d89df40b4cf3 | 82 | |
wolfSSL | 18:d89df40b4cf3 | 83 | //High Level setup functions |
wolfSSL | 18:d89df40b4cf3 | 84 | /** Execute a GET request on the URL |
wolfSSL | 18:d89df40b4cf3 | 85 | Blocks until completion |
wolfSSL | 18:d89df40b4cf3 | 86 | @param url : url on which to execute the request |
wolfSSL | 18:d89df40b4cf3 | 87 | @param pDataIn : pointer to an IHTTPDataIn instance that will collect the data returned by the request, can be NULL |
wolfSSL | 18:d89df40b4cf3 | 88 | @param timeout waiting timeout in ms (osWaitForever for blocking function, not recommended) |
wolfSSL | 18:d89df40b4cf3 | 89 | @return 0 on success, HTTP error (<0) on failure |
wolfSSL | 18:d89df40b4cf3 | 90 | */ |
wolfSSL | 18:d89df40b4cf3 | 91 | HTTPResult get(const char* url, IHTTPDataIn* pDataIn, int timeout = HTTP_CLIENT_DEFAULT_TIMEOUT); //Blocking |
wolfSSL | 18:d89df40b4cf3 | 92 | |
wolfSSL | 18:d89df40b4cf3 | 93 | /** Execute a GET request on the URL |
wolfSSL | 18:d89df40b4cf3 | 94 | Blocks until completion |
wolfSSL | 18:d89df40b4cf3 | 95 | This is a helper to directly get a piece of text from a HTTP result |
wolfSSL | 18:d89df40b4cf3 | 96 | @param url : url on which to execute the request |
wolfSSL | 18:d89df40b4cf3 | 97 | @param result : pointer to a char array in which the result will be stored |
wolfSSL | 18:d89df40b4cf3 | 98 | @param maxResultLen : length of the char array (including space for the NULL-terminating char) |
wolfSSL | 18:d89df40b4cf3 | 99 | @param timeout waiting timeout in ms (osWaitForever for blocking function, not recommended) |
wolfSSL | 18:d89df40b4cf3 | 100 | @return 0 on success, HTTP error (<0) on failure |
wolfSSL | 18:d89df40b4cf3 | 101 | */ |
wolfSSL | 18:d89df40b4cf3 | 102 | HTTPResult get(const char* url, char* result, size_t maxResultLen, int timeout = HTTP_CLIENT_DEFAULT_TIMEOUT); //Blocking |
wolfSSL | 18:d89df40b4cf3 | 103 | |
wolfSSL | 18:d89df40b4cf3 | 104 | /** Execute a POST request on the URL |
wolfSSL | 18:d89df40b4cf3 | 105 | Blocks until completion |
wolfSSL | 18:d89df40b4cf3 | 106 | @param url : url on which to execute the request |
wolfSSL | 18:d89df40b4cf3 | 107 | @param dataOut : a IHTTPDataOut instance that contains the data that will be posted |
wolfSSL | 18:d89df40b4cf3 | 108 | @param pDataIn : pointer to an IHTTPDataIn instance that will collect the data returned by the request, can be NULL |
wolfSSL | 18:d89df40b4cf3 | 109 | @param timeout waiting timeout in ms (osWaitForever for blocking function, not recommended) |
wolfSSL | 18:d89df40b4cf3 | 110 | @return 0 on success, HTTP error (<0) on failure |
wolfSSL | 18:d89df40b4cf3 | 111 | */ |
wolfSSL | 18:d89df40b4cf3 | 112 | HTTPResult post(const char* url, const IHTTPDataOut& dataOut, IHTTPDataIn* pDataIn, int timeout = HTTP_CLIENT_DEFAULT_TIMEOUT); //Blocking |
donatien | 0:2ccb9960a044 | 113 | |
wolfSSL | 18:d89df40b4cf3 | 114 | /** Execute a PUT request on the URL |
wolfSSL | 18:d89df40b4cf3 | 115 | Blocks until completion |
wolfSSL | 18:d89df40b4cf3 | 116 | @param url : url on which to execute the request |
wolfSSL | 18:d89df40b4cf3 | 117 | @param dataOut : a IHTTPDataOut instance that contains the data that will be put |
wolfSSL | 18:d89df40b4cf3 | 118 | @param pDataIn : pointer to an IHTTPDataIn instance that will collect the data returned by the request, can be NULL |
wolfSSL | 18:d89df40b4cf3 | 119 | @param timeout waiting timeout in ms (osWaitForever for blocking function, not recommended) |
wolfSSL | 18:d89df40b4cf3 | 120 | @return 0 on success, HTTP error (<0) on failure |
wolfSSL | 18:d89df40b4cf3 | 121 | */ |
wolfSSL | 18:d89df40b4cf3 | 122 | HTTPResult put(const char* url, const IHTTPDataOut& dataOut, IHTTPDataIn* pDataIn, int timeout = HTTP_CLIENT_DEFAULT_TIMEOUT); //Blocking |
wolfSSL | 18:d89df40b4cf3 | 123 | |
wolfSSL | 18:d89df40b4cf3 | 124 | /** Execute a DELETE request on the URL |
wolfSSL | 18:d89df40b4cf3 | 125 | Blocks until completion |
wolfSSL | 18:d89df40b4cf3 | 126 | @param url : url on which to execute the request |
wolfSSL | 18:d89df40b4cf3 | 127 | @param pDataIn : pointer to an IHTTPDataIn instance that will collect the data returned by the request, can be NULL |
wolfSSL | 18:d89df40b4cf3 | 128 | @param timeout waiting timeout in ms (osWaitForever for blocking function, not recommended) |
wolfSSL | 18:d89df40b4cf3 | 129 | @return 0 on success, HTTP error (<0) on failure |
wolfSSL | 18:d89df40b4cf3 | 130 | */ |
wolfSSL | 18:d89df40b4cf3 | 131 | HTTPResult del(const char* url, IHTTPDataIn* pDataIn, int timeout = HTTP_CLIENT_DEFAULT_TIMEOUT); //Blocking |
wolfSSL | 18:d89df40b4cf3 | 132 | |
wolfSSL | 18:d89df40b4cf3 | 133 | /** Get last request's HTTP response code |
wolfSSL | 18:d89df40b4cf3 | 134 | @return The HTTP response code of the last request |
wolfSSL | 18:d89df40b4cf3 | 135 | */ |
wolfSSL | 18:d89df40b4cf3 | 136 | int getHTTPResponseCode(); |
wolfSSL | 22:4b9a4151cc73 | 137 | |
wolfSSL | 27:5d4739eae63e | 138 | void setHeader(const char *header) ; /* set http headers */ |
wolfSSL | 22:4b9a4151cc73 | 139 | HTTPResult setSSLversion(int minorV) ; /* set SSL/TLS version. 0: SSL3, 1: TLS1.0, 2: TLS1.1, 3: TLS1.2 */ |
wolfSSL | 27:5d4739eae63e | 140 | void setLocationBuf(char *url, int size) ; /* set URL buffer for redirection */ |
wolfSSL | 18:d89df40b4cf3 | 141 | |
donatien | 0:2ccb9960a044 | 142 | private: |
wolfSSL | 18:d89df40b4cf3 | 143 | enum HTTP_METH { |
wolfSSL | 18:d89df40b4cf3 | 144 | HTTP_GET, |
wolfSSL | 18:d89df40b4cf3 | 145 | HTTP_POST, |
wolfSSL | 18:d89df40b4cf3 | 146 | HTTP_PUT, |
wolfSSL | 18:d89df40b4cf3 | 147 | HTTP_DELETE, |
wolfSSL | 18:d89df40b4cf3 | 148 | HTTP_HEAD |
wolfSSL | 18:d89df40b4cf3 | 149 | }; |
wolfSSL | 18:d89df40b4cf3 | 150 | |
wolfSSL | 18:d89df40b4cf3 | 151 | HTTPResult connect(const char* url, HTTP_METH method, IHTTPDataOut* pDataOut, IHTTPDataIn* pDataIn, int timeout); //Execute request |
wolfSSL | 18:d89df40b4cf3 | 152 | HTTPResult recv(char* buf, size_t minLen, size_t maxLen, size_t* pReadLen); //0 on success, err code on failure |
wolfSSL | 18:d89df40b4cf3 | 153 | HTTPResult send(char* buf, size_t len = 0); //0 on success, err code on failure |
wolfSSL | 18:d89df40b4cf3 | 154 | HTTPResult flush(void); //0 on success, err code on failure |
wolfSSL | 18:d89df40b4cf3 | 155 | 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 |
wolfSSL | 22:4b9a4151cc73 | 156 | HTTPResult bAuth(void) ; |
ansond | 36:debaeb6006a7 | 157 | HTTPResult tokenAuth(void) ; |
wolfSSL | 27:5d4739eae63e | 158 | HTTPResult readHeader(void) ; |
wolfSSL | 22:4b9a4151cc73 | 159 | |
ansond | 51:3bdf57f7fd60 | 160 | // SSL internal methods... |
ansond | 51:3bdf57f7fd60 | 161 | void ssl_cleanup(void) ; |
ansond | 51:3bdf57f7fd60 | 162 | void ssl_setup(const unsigned char *tag); |
ansond | 51:3bdf57f7fd60 | 163 | short ssl_connect(); |
ansond | 51:3bdf57f7fd60 | 164 | void set_ssl_version(); |
ansond | 51:3bdf57f7fd60 | 165 | |
wolfSSL | 18:d89df40b4cf3 | 166 | //Parameters |
wolfSSL | 18:d89df40b4cf3 | 167 | int m_timeout; |
wolfSSL | 18:d89df40b4cf3 | 168 | const char* m_basicAuthUser; |
wolfSSL | 18:d89df40b4cf3 | 169 | const char* m_basicAuthPassword; |
ansond | 51:3bdf57f7fd60 | 170 | int m_httpResponseCode; |
ansond | 36:debaeb6006a7 | 171 | const char* m_oauthToken; |
donatien | 0:2ccb9960a044 | 172 | |
sarahmarshy | 50:a18a06b000f3 | 173 | TCPSocketConnection m_sock; |
sarahmarshy | 50:a18a06b000f3 | 174 | |
wolfSSL | 27:5d4739eae63e | 175 | const char * header ; |
wolfSSL | 27:5d4739eae63e | 176 | char * redirect_url ; |
wolfSSL | 27:5d4739eae63e | 177 | int redirect_url_size ; |
wolfSSL | 27:5d4739eae63e | 178 | int redirect ; |
wolfSSL | 27:5d4739eae63e | 179 | |
ansond | 51:3bdf57f7fd60 | 180 | // mbedTLS Support |
ansond | 51:3bdf57f7fd60 | 181 | int SSLver; |
ansond | 51:3bdf57f7fd60 | 182 | uint16_t port; |
ansond | 51:3bdf57f7fd60 | 183 | entropy_context entropy; |
ansond | 51:3bdf57f7fd60 | 184 | ctr_drbg_context ctr_drbg; |
ansond | 51:3bdf57f7fd60 | 185 | ssl_context ssl; |
ansond | 51:3bdf57f7fd60 | 186 | bool ssl_connected; |
donatien | 0:2ccb9960a044 | 187 | }; |
donatien | 0:2ccb9960a044 | 188 | |
donatien | 0:2ccb9960a044 | 189 | //Including data containers here for more convenience |
ansond | 29:2d96cc752d19 | 190 | #include "data/HTTPJson.h" |
donatien | 0:2ccb9960a044 | 191 | #include "data/HTTPMap.h" |
donatien | 0:2ccb9960a044 | 192 | |
donatien | 0:2ccb9960a044 | 193 | #endif |