Change buffer sizes to support GR-PEACH

Dependencies:   CyaSSL

Dependents:   GR-PEACH_Azure_Speech

Fork of HTTPClient-SSL by MultiTech

Committer:
Vanger
Date:
Wed Jan 14 22:39:59 2015 +0000
Revision:
38:a4ccad70be9d
Parent:
33:3b2809748a9e
Child:
39:d7c5541a9124
Added functions to add Root Certificates for SSL verification of Peers, added a function to set the peer verify settings for the CyaSSL library, tweaked some debug and memory operations in the client.

Who changed what in which revision?

UserRevisionLine numberNew 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"
Vanger 33:3b2809748a9e 33 #include "TCPSocketConnection.h"
donatien 0:2ccb9960a044 34
Vanger 38:a4ccad70be9d 35 ///SSL peer verification setting
Vanger 38:a4ccad70be9d 36 enum SSLMethod {
Vanger 38:a4ccad70be9d 37 VERIFY_NONE = 0, ///Don't check peer certificate
Vanger 38:a4ccad70be9d 38 VERIFY_PEER = 1, ///Check peer certificate and skip if none available (insecure)
Vanger 38:a4ccad70be9d 39 VERIFY_FAIL_IF_NO_PEER_CERT = 2, ///Check peer certificate and fail if unavailable
Vanger 38:a4ccad70be9d 40 };
Vanger 38:a4ccad70be9d 41
donatien 0:2ccb9960a044 42 ///HTTP client results
wolfSSL 18:d89df40b4cf3 43 enum HTTPResult {
Vanger 33:3b2809748a9e 44 HTTP_OK = 0, ///<Success
wolfSSL 18:d89df40b4cf3 45 HTTP_PROCESSING, ///<Processing
wolfSSL 18:d89df40b4cf3 46 HTTP_PARSE, ///<url Parse error
wolfSSL 18:d89df40b4cf3 47 HTTP_DNS, ///<Could not resolve name
wolfSSL 18:d89df40b4cf3 48 HTTP_PRTCL, ///<Protocol error
wolfSSL 18:d89df40b4cf3 49 HTTP_NOTFOUND, ///<HTTP 404 Error
wolfSSL 18:d89df40b4cf3 50 HTTP_REFUSED, ///<HTTP 403 Error
wolfSSL 18:d89df40b4cf3 51 HTTP_ERROR, ///<HTTP xxx error
wolfSSL 18:d89df40b4cf3 52 HTTP_TIMEOUT, ///<Connection timeout
wolfSSL 18:d89df40b4cf3 53 HTTP_CONN, ///<Connection error
wolfSSL 18:d89df40b4cf3 54 HTTP_CLOSED, ///<Connection was closed by remote host
wolfSSL 27:5d4739eae63e 55 HTTP_REDIRECT, ///<HTTP 300 - 303
donatien 0:2ccb9960a044 56 };
donatien 0:2ccb9960a044 57
donatien 0:2ccb9960a044 58 /**A simple HTTP Client
donatien 0:2ccb9960a044 59 The HTTPClient is composed of:
donatien 0:2ccb9960a044 60 - The actual client (HTTPClient)
donatien 0:2ccb9960a044 61 - 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 62 */
donatien 0:2ccb9960a044 63 class HTTPClient
donatien 0:2ccb9960a044 64 {
donatien 0:2ccb9960a044 65 public:
wolfSSL 18:d89df40b4cf3 66 ///Instantiate the HTTP client
wolfSSL 18:d89df40b4cf3 67 HTTPClient();
wolfSSL 18:d89df40b4cf3 68 ~HTTPClient();
wolfSSL 18:d89df40b4cf3 69
wolfSSL 18:d89df40b4cf3 70 /**
wolfSSL 18:d89df40b4cf3 71 Provides a basic authentification feature (Base64 encoded username and password)
wolfSSL 18:d89df40b4cf3 72 Pass two NULL pointers to switch back to no authentication
wolfSSL 18:d89df40b4cf3 73 @param user username to use for authentication, must remain valid durlng the whole HTTP session
wolfSSL 18:d89df40b4cf3 74 @param user password to use for authentication, must remain valid durlng the whole HTTP session
wolfSSL 18:d89df40b4cf3 75 */
wolfSSL 22:4b9a4151cc73 76 HTTPResult basicAuth(const char* user, const char* password); //Basic Authentification
wolfSSL 18:d89df40b4cf3 77
wolfSSL 18:d89df40b4cf3 78 //High Level setup functions
wolfSSL 18:d89df40b4cf3 79 /** Execute a GET request on the URL
wolfSSL 18:d89df40b4cf3 80 Blocks until completion
wolfSSL 18:d89df40b4cf3 81 @param url : url on which to execute the request
wolfSSL 18:d89df40b4cf3 82 @param pDataIn : pointer to an IHTTPDataIn instance that will collect the data returned by the request, can be NULL
wolfSSL 18:d89df40b4cf3 83 @param timeout waiting timeout in ms (osWaitForever for blocking function, not recommended)
wolfSSL 18:d89df40b4cf3 84 @return 0 on success, HTTP error (<0) on failure
wolfSSL 18:d89df40b4cf3 85 */
wolfSSL 18:d89df40b4cf3 86 HTTPResult get(const char* url, IHTTPDataIn* pDataIn, int timeout = HTTP_CLIENT_DEFAULT_TIMEOUT); //Blocking
wolfSSL 18:d89df40b4cf3 87
wolfSSL 18:d89df40b4cf3 88 /** Execute a GET request on the URL
wolfSSL 18:d89df40b4cf3 89 Blocks until completion
wolfSSL 18:d89df40b4cf3 90 This is a helper to directly get a piece of text from a HTTP result
wolfSSL 18:d89df40b4cf3 91 @param url : url on which to execute the request
wolfSSL 18:d89df40b4cf3 92 @param result : pointer to a char array in which the result will be stored
wolfSSL 18:d89df40b4cf3 93 @param maxResultLen : length of the char array (including space for the NULL-terminating char)
wolfSSL 18:d89df40b4cf3 94 @param timeout waiting timeout in ms (osWaitForever for blocking function, not recommended)
wolfSSL 18:d89df40b4cf3 95 @return 0 on success, HTTP error (<0) on failure
wolfSSL 18:d89df40b4cf3 96 */
wolfSSL 18:d89df40b4cf3 97 HTTPResult get(const char* url, char* result, size_t maxResultLen, int timeout = HTTP_CLIENT_DEFAULT_TIMEOUT); //Blocking
wolfSSL 18:d89df40b4cf3 98
wolfSSL 18:d89df40b4cf3 99 /** Execute a POST request on the URL
wolfSSL 18:d89df40b4cf3 100 Blocks until completion
wolfSSL 18:d89df40b4cf3 101 @param url : url on which to execute the request
wolfSSL 18:d89df40b4cf3 102 @param dataOut : a IHTTPDataOut instance that contains the data that will be posted
wolfSSL 18:d89df40b4cf3 103 @param pDataIn : pointer to an IHTTPDataIn instance that will collect the data returned by the request, can be NULL
wolfSSL 18:d89df40b4cf3 104 @param timeout waiting timeout in ms (osWaitForever for blocking function, not recommended)
wolfSSL 18:d89df40b4cf3 105 @return 0 on success, HTTP error (<0) on failure
wolfSSL 18:d89df40b4cf3 106 */
wolfSSL 18:d89df40b4cf3 107 HTTPResult post(const char* url, const IHTTPDataOut& dataOut, IHTTPDataIn* pDataIn, int timeout = HTTP_CLIENT_DEFAULT_TIMEOUT); //Blocking
donatien 0:2ccb9960a044 108
wolfSSL 18:d89df40b4cf3 109 /** Execute a PUT request on the URL
wolfSSL 18:d89df40b4cf3 110 Blocks until completion
wolfSSL 18:d89df40b4cf3 111 @param url : url on which to execute the request
wolfSSL 18:d89df40b4cf3 112 @param dataOut : a IHTTPDataOut instance that contains the data that will be put
wolfSSL 18:d89df40b4cf3 113 @param pDataIn : pointer to an IHTTPDataIn instance that will collect the data returned by the request, can be NULL
wolfSSL 18:d89df40b4cf3 114 @param timeout waiting timeout in ms (osWaitForever for blocking function, not recommended)
wolfSSL 18:d89df40b4cf3 115 @return 0 on success, HTTP error (<0) on failure
wolfSSL 18:d89df40b4cf3 116 */
wolfSSL 18:d89df40b4cf3 117 HTTPResult put(const char* url, const IHTTPDataOut& dataOut, IHTTPDataIn* pDataIn, int timeout = HTTP_CLIENT_DEFAULT_TIMEOUT); //Blocking
wolfSSL 18:d89df40b4cf3 118
wolfSSL 18:d89df40b4cf3 119 /** Execute a DELETE request on the URL
wolfSSL 18:d89df40b4cf3 120 Blocks until completion
wolfSSL 18:d89df40b4cf3 121 @param url : url on which to execute the request
wolfSSL 18:d89df40b4cf3 122 @param pDataIn : pointer to an IHTTPDataIn instance that will collect the data returned by the request, can be NULL
wolfSSL 18:d89df40b4cf3 123 @param timeout waiting timeout in ms (osWaitForever for blocking function, not recommended)
wolfSSL 18:d89df40b4cf3 124 @return 0 on success, HTTP error (<0) on failure
wolfSSL 18:d89df40b4cf3 125 */
wolfSSL 18:d89df40b4cf3 126 HTTPResult del(const char* url, IHTTPDataIn* pDataIn, int timeout = HTTP_CLIENT_DEFAULT_TIMEOUT); //Blocking
wolfSSL 18:d89df40b4cf3 127
wolfSSL 18:d89df40b4cf3 128 /** Get last request's HTTP response code
wolfSSL 18:d89df40b4cf3 129 @return The HTTP response code of the last request
wolfSSL 18:d89df40b4cf3 130 */
wolfSSL 18:d89df40b4cf3 131 int getHTTPResponseCode();
wolfSSL 22:4b9a4151cc73 132
Vanger 38:a4ccad70be9d 133 /** Set headers to be included in the following HTTP requests. Pass a NULL pointer to reset the headers stored.
Vanger 38:a4ccad70be9d 134 * Make sure the headers are formatted with a "\r\n" after each header.
Vanger 38:a4ccad70be9d 135 * @param header pointer to array containing the headers to be added*/
Vanger 38:a4ccad70be9d 136 void setHeader(const char *header) ;
Vanger 38:a4ccad70be9d 137
Vanger 38:a4ccad70be9d 138 /** Set SSL/TLS version.
Vanger 38:a4ccad70be9d 139 * @param minorV integer witha a value between 0 and 3
Vanger 38:a4ccad70be9d 140 * 0: SSL3, 1: TLS1.0, 2: TLS1.1, 3: TLS1.2
Vanger 38:a4ccad70be9d 141 * @returns HTTPResult based on success*/
Vanger 38:a4ccad70be9d 142 HTTPResult setSSLversion(int minorV) ;
Vanger 38:a4ccad70be9d 143
Vanger 38:a4ccad70be9d 144 /* set URL buffer for redirection */
Vanger 38:a4ccad70be9d 145 void setLocationBuf(char *url, int size) ;
Vanger 38:a4ccad70be9d 146
Vanger 38:a4ccad70be9d 147 /** Stores a root CA certificate for host authentication of a website.
Vanger 38:a4ccad70be9d 148 * Each new line should end with "\r\n" including the last line of each certificate.
Vanger 38:a4ccad70be9d 149 * Pass a pointer to the char array containing the certificate stored as a c-string.
Vanger 38:a4ccad70be9d 150 * Pass a NULL pointer to reset all certificates stored.
Vanger 38:a4ccad70be9d 151 * (Can pass in multiple certificates with one function call if the array contains concatenated certificates) */
Vanger 38:a4ccad70be9d 152 HTTPResult addRootCACertificate(const char* cert) ;
Vanger 38:a4ccad70be9d 153
Vanger 38:a4ccad70be9d 154 /** Sets the verification for peer authenticity when connecting with SSL
Vanger 38:a4ccad70be9d 155 * @param method specifies the method to use for peer verification
Vanger 38:a4ccad70be9d 156 * @VERIFY_NONE Sets the client to not verify the peer's certificates
Vanger 38:a4ccad70be9d 157 * @VERIFY_PEER Sets the client to verify the peer's certificates but skips if certificates unavailable
Vanger 38:a4ccad70be9d 158 * @VERIFY_FAIL_IF_NO_PEER_CERT Sets the client to verify the peer's certificates and throw an error if the
Vanger 38:a4ccad70be9d 159 * certificates are unavailable.
Vanger 38:a4ccad70be9d 160 * */
Vanger 38:a4ccad70be9d 161 void setPeerVerification(SSLMethod method);
wolfSSL 18:d89df40b4cf3 162
donatien 0:2ccb9960a044 163 private:
wolfSSL 18:d89df40b4cf3 164 enum HTTP_METH {
wolfSSL 18:d89df40b4cf3 165 HTTP_GET,
wolfSSL 18:d89df40b4cf3 166 HTTP_POST,
wolfSSL 18:d89df40b4cf3 167 HTTP_PUT,
wolfSSL 18:d89df40b4cf3 168 HTTP_DELETE,
wolfSSL 18:d89df40b4cf3 169 HTTP_HEAD
wolfSSL 18:d89df40b4cf3 170 };
wolfSSL 18:d89df40b4cf3 171
wolfSSL 18:d89df40b4cf3 172 HTTPResult connect(const char* url, HTTP_METH method, IHTTPDataOut* pDataOut, IHTTPDataIn* pDataIn, int timeout); //Execute request
wolfSSL 18:d89df40b4cf3 173 HTTPResult recv(char* buf, size_t minLen, size_t maxLen, size_t* pReadLen); //0 on success, err code on failure
wolfSSL 18:d89df40b4cf3 174 HTTPResult send(char* buf, size_t len = 0); //0 on success, err code on failure
wolfSSL 18:d89df40b4cf3 175 HTTPResult flush(void); //0 on success, err code on failure
wolfSSL 18:d89df40b4cf3 176 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 18:d89df40b4cf3 177 void cyassl_free(void) ;
wolfSSL 22:4b9a4151cc73 178 HTTPResult bAuth(void) ;
wolfSSL 27:5d4739eae63e 179 HTTPResult readHeader(void) ;
wolfSSL 22:4b9a4151cc73 180
wolfSSL 18:d89df40b4cf3 181 //Parameters
Vanger 33:3b2809748a9e 182 TCPSocketConnection _m_sock;
wolfSSL 18:d89df40b4cf3 183
wolfSSL 18:d89df40b4cf3 184 int m_timeout;
Vanger 38:a4ccad70be9d 185
wolfSSL 18:d89df40b4cf3 186 const char* m_basicAuthUser;
wolfSSL 18:d89df40b4cf3 187 const char* m_basicAuthPassword;
wolfSSL 18:d89df40b4cf3 188 int m_httpResponseCode;
donatien 0:2ccb9960a044 189
wolfSSL 27:5d4739eae63e 190 const char * header ;
wolfSSL 27:5d4739eae63e 191 char * redirect_url ;
wolfSSL 27:5d4739eae63e 192 int redirect_url_size ;
wolfSSL 27:5d4739eae63e 193 int redirect ;
wolfSSL 27:5d4739eae63e 194
wolfSSL 18:d89df40b4cf3 195 /* for CyaSSL */
Vanger 38:a4ccad70be9d 196 const char* certificates; //CA certificates
Vanger 38:a4ccad70be9d 197 SSLMethod peerMethod;
wolfSSL 22:4b9a4151cc73 198 int SSLver ;
wolfSSL 18:d89df40b4cf3 199 uint16_t port;
wolfSSL 18:d89df40b4cf3 200 struct CYASSL_CTX* ctx ;
wolfSSL 18:d89df40b4cf3 201 struct CYASSL * ssl ;
donatien 0:2ccb9960a044 202 };
donatien 0:2ccb9960a044 203
donatien 0:2ccb9960a044 204 //Including data containers here for more convenience
ansond 29:2d96cc752d19 205 #include "data/HTTPJson.h"
donatien 0:2ccb9960a044 206 #include "data/HTTPMap.h"
donatien 0:2ccb9960a044 207
donatien 0:2ccb9960a044 208 #endif