A HTTP/HTTPS Client for the mbed networking/CyaSSL ssl library

Dependents:   Anpi Example--Dropbox Example-Xively dropbox_access ... more

Fork of HTTPClient by Donatien Garnier

HTTP and HTTPS Client Class with wolfSSL, embedded SSL library.

/media/uploads/wolfSSL/wolfssl_logo.png

The class was forked from http://mbed.org/users/donatien/code/HTTPClient/

It, now, accepts url both with "http://" and "https://".

Allocate caller thread with 16kbytes or larger stack for "https" requests.

Rest of the API stays compatible with HTTPClient.

For more about the library, see http://www.wolfssl.com. http://wolfssl.com/yaSSL/Docs.html.

Extended methods:

Committer:
wolfSSL
Date:
Thu Apr 28 00:55:27 2016 +0000
Revision:
34:76aa4f4021c1
Parent:
31:7fd621b83b60
Sync with wolfSSL 3.9.0

Who changed what in which revision?

UserRevisionLine numberNew contents of line
donatien0:2ccb9960a044 1/* HTTPClient.h */
donatien10:e1351de84c16 2/* Copyright (C) 2012 mbed.org, MIT License
donatien10:e1351de84c16 3 *
donatien10:e1351de84c16 4 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
donatien10:e1351de84c16 5 * and associated documentation files (the "Software"), to deal in the Software without restriction,
donatien10:e1351de84c16 6 * including without limitation the rights to use, copy, modify, merge, publish, distribute,
donatien10:e1351de84c16 7 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
donatien10:e1351de84c16 8 * furnished to do so, subject to the following conditions:
donatien10:e1351de84c16 9 *
donatien10:e1351de84c16 10 * The above copyright notice and this permission notice shall be included in all copies or
donatien10:e1351de84c16 11 * substantial portions of the Software.
donatien10:e1351de84c16 12 *
donatien10:e1351de84c16 13 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
donatien10:e1351de84c16 14 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
donatien10:e1351de84c16 15 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
donatien10:e1351de84c16 16 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
donatien10:e1351de84c16 17 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
donatien10:e1351de84c16 18 */
donatien0:2ccb9960a044 19
donatien0:2ccb9960a044 20/** \file
donatien0:2ccb9960a044 21HTTP Client header file
donatien0:2ccb9960a044 22*/
donatien0:2ccb9960a044 23
donatien0:2ccb9960a044 24#ifndef HTTP_CLIENT_H
donatien0:2ccb9960a044 25#define HTTP_CLIENT_H
donatien0:2ccb9960a044 26
donatien12:89d09a6db00a 27#define HTTP_CLIENT_DEFAULT_TIMEOUT 15000
donatien0:2ccb9960a044 28
donatien0:2ccb9960a044 29class HTTPData;
donatien0:2ccb9960a044 30
donatien0:2ccb9960a044 31#include "IHTTPData.h"
donatien0:2ccb9960a044 32#include "mbed.h"
donatien0:2ccb9960a044 33
donatien0:2ccb9960a044 34///HTTP client results
wolfSSL18:d89df40b4cf3 35enum HTTPResult {
wolfSSL18:d89df40b4cf3 36 HTTP_PROCESSING, ///<Processing
wolfSSL18:d89df40b4cf3 37 HTTP_PARSE, ///<url Parse error
wolfSSL18:d89df40b4cf3 38 HTTP_DNS, ///<Could not resolve name
wolfSSL18:d89df40b4cf3 39 HTTP_PRTCL, ///<Protocol error
wolfSSL18:d89df40b4cf3 40 HTTP_NOTFOUND, ///<HTTP 404 Error
wolfSSL18:d89df40b4cf3 41 HTTP_REFUSED, ///<HTTP 403 Error
wolfSSL18:d89df40b4cf3 42 HTTP_ERROR, ///<HTTP xxx error
wolfSSL18:d89df40b4cf3 43 HTTP_TIMEOUT, ///<Connection timeout
wolfSSL18:d89df40b4cf3 44 HTTP_CONN, ///<Connection error
wolfSSL18:d89df40b4cf3 45 HTTP_CLOSED, ///<Connection was closed by remote host
wolfSSL26:bf979804b653 46 HTTP_REDIRECT, ///<HTTP 300 - 303
wolfSSL18:d89df40b4cf3 47 HTTP_OK = 0, ///<Success
donatien0:2ccb9960a044 48};
donatien0:2ccb9960a044 49
donatien0:2ccb9960a044 50/**A simple HTTP Client
donatien0:2ccb9960a044 51The HTTPClient is composed of:
donatien0:2ccb9960a044 52- The actual client (HTTPClient)
donatien0:2ccb9960a044 53- 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)
donatien0:2ccb9960a044 54*/
donatien0:2ccb9960a044 55class HTTPClient
donatien0:2ccb9960a044 56{
donatien0:2ccb9960a044 57public:
wolfSSL18:d89df40b4cf3 58 ///Instantiate the HTTP client
wolfSSL18:d89df40b4cf3 59 HTTPClient();
wolfSSL18:d89df40b4cf3 60 ~HTTPClient();
wolfSSL18:d89df40b4cf3 61
wolfSSL18:d89df40b4cf3 62 /**
wolfSSL18:d89df40b4cf3 63 Provides a basic authentification feature (Base64 encoded username and password)
wolfSSL18:d89df40b4cf3 64 Pass two NULL pointers to switch back to no authentication
wolfSSL18:d89df40b4cf3 65 @param user username to use for authentication, must remain valid durlng the whole HTTP session
wolfSSL18:d89df40b4cf3 66 @param user password to use for authentication, must remain valid durlng the whole HTTP session
wolfSSL18:d89df40b4cf3 67 */
wolfSSL22:4b9a4151cc73 68 HTTPResult basicAuth(const char* user, const char* password); //Basic Authentification
donatien0:2ccb9960a044 69
wolfSSL18:d89df40b4cf3 70 //High Level setup functions
wolfSSL18:d89df40b4cf3 71 /** Execute a GET request on the URL
wolfSSL18:d89df40b4cf3 72 Blocks until completion
wolfSSL18:d89df40b4cf3 73 @param url : url on which to execute the request
wolfSSL18:d89df40b4cf3 74 @param pDataIn : pointer to an IHTTPDataIn instance that will collect the data returned by the request, can be NULL
wolfSSL18:d89df40b4cf3 75 @param timeout waiting timeout in ms (osWaitForever for blocking function, not recommended)
wolfSSL18:d89df40b4cf3 76 @return 0 on success, HTTP error (<0) on failure
wolfSSL18:d89df40b4cf3 77 */
wolfSSL18:d89df40b4cf3 78 HTTPResult get(const char* url, IHTTPDataIn* pDataIn, int timeout = HTTP_CLIENT_DEFAULT_TIMEOUT); //Blocking
wolfSSL18:d89df40b4cf3 79
wolfSSL18:d89df40b4cf3 80 /** Execute a GET request on the URL
wolfSSL18:d89df40b4cf3 81 Blocks until completion
wolfSSL18:d89df40b4cf3 82 This is a helper to directly get a piece of text from a HTTP result
wolfSSL18:d89df40b4cf3 83 @param url : url on which to execute the request
wolfSSL18:d89df40b4cf3 84 @param result : pointer to a char array in which the result will be stored
wolfSSL18:d89df40b4cf3 85 @param maxResultLen : length of the char array (including space for the NULL-terminating char)
wolfSSL18:d89df40b4cf3 86 @param timeout waiting timeout in ms (osWaitForever for blocking function, not recommended)
wolfSSL18:d89df40b4cf3 87 @return 0 on success, HTTP error (<0) on failure
wolfSSL18:d89df40b4cf3 88 */
wolfSSL18:d89df40b4cf3 89 HTTPResult get(const char* url, char* result, size_t maxResultLen, int timeout = HTTP_CLIENT_DEFAULT_TIMEOUT); //Blocking
wolfSSL18:d89df40b4cf3 90
wolfSSL18:d89df40b4cf3 91 /** Execute a POST request on the URL
wolfSSL18:d89df40b4cf3 92 Blocks until completion
wolfSSL18:d89df40b4cf3 93 @param url : url on which to execute the request
wolfSSL18:d89df40b4cf3 94 @param dataOut : a IHTTPDataOut instance that contains the data that will be posted
wolfSSL18:d89df40b4cf3 95 @param pDataIn : pointer to an IHTTPDataIn instance that will collect the data returned by the request, can be NULL
wolfSSL18:d89df40b4cf3 96 @param timeout waiting timeout in ms (osWaitForever for blocking function, not recommended)
wolfSSL18:d89df40b4cf3 97 @return 0 on success, HTTP error (<0) on failure
wolfSSL18:d89df40b4cf3 98 */
wolfSSL18:d89df40b4cf3 99 HTTPResult post(const char* url, const IHTTPDataOut& dataOut, IHTTPDataIn* pDataIn, int timeout = HTTP_CLIENT_DEFAULT_TIMEOUT); //Blocking
wolfSSL18:d89df40b4cf3 100
wolfSSL18:d89df40b4cf3 101 /** Execute a PUT request on the URL
wolfSSL18:d89df40b4cf3 102 Blocks until completion
wolfSSL18:d89df40b4cf3 103 @param url : url on which to execute the request
wolfSSL18:d89df40b4cf3 104 @param dataOut : a IHTTPDataOut instance that contains the data that will be put
wolfSSL18:d89df40b4cf3 105 @param pDataIn : pointer to an IHTTPDataIn instance that will collect the data returned by the request, can be NULL
wolfSSL18:d89df40b4cf3 106 @param timeout waiting timeout in ms (osWaitForever for blocking function, not recommended)
wolfSSL18:d89df40b4cf3 107 @return 0 on success, HTTP error (<0) on failure
wolfSSL18:d89df40b4cf3 108 */
wolfSSL18:d89df40b4cf3 109 HTTPResult put(const char* url, const IHTTPDataOut& dataOut, IHTTPDataIn* pDataIn, int timeout = HTTP_CLIENT_DEFAULT_TIMEOUT); //Blocking
wolfSSL18:d89df40b4cf3 110
wolfSSL18:d89df40b4cf3 111 /** Execute a DELETE request on the URL
wolfSSL18:d89df40b4cf3 112 Blocks until completion
wolfSSL18:d89df40b4cf3 113 @param url : url on which to execute the request
wolfSSL18:d89df40b4cf3 114 @param pDataIn : pointer to an IHTTPDataIn instance that will collect the data returned by the request, can be NULL
wolfSSL18:d89df40b4cf3 115 @param timeout waiting timeout in ms (osWaitForever for blocking function, not recommended)
wolfSSL18:d89df40b4cf3 116 @return 0 on success, HTTP error (<0) on failure
wolfSSL18:d89df40b4cf3 117 */
wolfSSL18:d89df40b4cf3 118 HTTPResult del(const char* url, IHTTPDataIn* pDataIn, int timeout = HTTP_CLIENT_DEFAULT_TIMEOUT); //Blocking
wolfSSL18:d89df40b4cf3 119
wolfSSL18:d89df40b4cf3 120 /** Get last request's HTTP response code
wolfSSL18:d89df40b4cf3 121 @return The HTTP response code of the last request
wolfSSL18:d89df40b4cf3 122 */
wolfSSL18:d89df40b4cf3 123 int getHTTPResponseCode();
wolfSSL33:77082c88748a 124
wolfSSL26:bf979804b653 125 void setHeader(const char *header) ; /* set http headers */
wolfSSL33:77082c88748a 126 void dumpReqHeader(bool sw) ; /* switch on dumpling request headers */
wolfSSL33:77082c88748a 127 void dumpResHeader(bool sw) ; /* switch on dumpling response headers */
wolfSSL22:4b9a4151cc73 128 HTTPResult setSSLversion(int minorV) ; /* set SSL/TLS version. 0: SSL3, 1: TLS1.0, 2: TLS1.1, 3: TLS1.2 */
wolfSSL26:bf979804b653 129 void setLocationBuf(char *url, int size) ; /* set URL buffer for redirection */
wolfSSL18:d89df40b4cf3 130
donatien0:2ccb9960a044 131private:
wolfSSL18:d89df40b4cf3 132 enum HTTP_METH {
wolfSSL18:d89df40b4cf3 133 HTTP_GET,
wolfSSL18:d89df40b4cf3 134 HTTP_POST,
wolfSSL18:d89df40b4cf3 135 HTTP_PUT,
wolfSSL18:d89df40b4cf3 136 HTTP_DELETE,
wolfSSL18:d89df40b4cf3 137 HTTP_HEAD
wolfSSL18:d89df40b4cf3 138 };
donatien0:2ccb9960a044 139
wolfSSL18:d89df40b4cf3 140 HTTPResult connect(const char* url, HTTP_METH method, IHTTPDataOut* pDataOut, IHTTPDataIn* pDataIn, int timeout); //Execute request
wolfSSL18:d89df40b4cf3 141 HTTPResult recv(char* buf, size_t minLen, size_t maxLen, size_t* pReadLen); //0 on success, err code on failure
wolfSSL18:d89df40b4cf3 142 HTTPResult send(char* buf, size_t len = 0); //0 on success, err code on failure
wolfSSL18:d89df40b4cf3 143 HTTPResult flush(void); //0 on success, err code on failure
wolfSSL18:d89df40b4cf3 144 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
wolfSSL31:7fd621b83b60 145 void wolfssl_free(void) ;
wolfSSL22:4b9a4151cc73 146 HTTPResult bAuth(void) ;
wolfSSL22:4b9a4151cc73 147
wolfSSL18:d89df40b4cf3 148 //Parameters
donatien0:2ccb9960a044 149
wolfSSL18:d89df40b4cf3 150 int m_timeout;
wolfSSL18:d89df40b4cf3 151
wolfSSL18:d89df40b4cf3 152 const char* m_basicAuthUser;
wolfSSL18:d89df40b4cf3 153 const char* m_basicAuthPassword;
wolfSSL18:d89df40b4cf3 154 int m_httpResponseCode;
wolfSSL18:d89df40b4cf3 155
wolfSSL26:bf979804b653 156 const char * header ;
wolfSSL26:bf979804b653 157 char * redirect_url ;
wolfSSL26:bf979804b653 158 int redirect_url_size ;
wolfSSL26:bf979804b653 159 int redirect ;
wolfSSL33:77082c88748a 160 bool dumpReqH ;
wolfSSL33:77082c88748a 161 bool dumpResH ;
wolfSSL26:bf979804b653 162
wolfSSL33:77082c88748a 163 /* for wolfSSL */
wolfSSL22:4b9a4151cc73 164 int SSLver ;
wolfSSL18:d89df40b4cf3 165 uint16_t port;
wolfSSL31:7fd621b83b60 166 struct WOLFSSL_CTX* ctx ;
wolfSSL31:7fd621b83b60 167 struct WOLFSSL * ssl ;
donatien0:2ccb9960a044 168};
donatien0:2ccb9960a044 169
donatien0:2ccb9960a044 170//Including data containers here for more convenience
donatien0:2ccb9960a044 171#include "data/HTTPText.h"
donatien0:2ccb9960a044 172#include "data/HTTPMap.h"
donatien0:2ccb9960a044 173
donatien0:2ccb9960a044 174#endif