HTTP/HTTPS Client Library for the X-NUCLEO-IDW01M1v2 wifi board.

Dependents:   HTTPClient_HelloWorld_IDW01M1 wifigianluigi HTTPClient_HelloWorld_IDW01M1_Fabio_Ricezione

Fork of HTTPClient by ST Expansion SW Team

Committer:
mapellil
Date:
Fri Jan 13 14:40:02 2017 +0000
Revision:
24:dafc02922e40
Parent:
22:ae30428dc030
Added HTTPS support

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
mapellil 21:6e5c122ad9e5 27 #include "NetworkStack.h"
mapellil 19:17578cfdb57a 28 #include "TCPSocket.h"
mapellil 21:6e5c122ad9e5 29 #include "SpwfInterface.h"
donatien 0:2ccb9960a044 30
mapellil 19:17578cfdb57a 31 #define LICIO
donatien 12:89d09a6db00a 32 #define HTTP_CLIENT_DEFAULT_TIMEOUT 15000
mapellil 19:17578cfdb57a 33 #define CHUNK_SIZE 256
donatien 0:2ccb9960a044 34
donatien 0:2ccb9960a044 35 class HTTPData;
donatien 0:2ccb9960a044 36
donatien 0:2ccb9960a044 37 #include "IHTTPData.h"
mapellil 19:17578cfdb57a 38
donatien 0:2ccb9960a044 39 #include "mbed.h"
donatien 0:2ccb9960a044 40
donatien 0:2ccb9960a044 41 ///HTTP client results
donatien 0:2ccb9960a044 42 enum HTTPResult
donatien 0:2ccb9960a044 43 {
donatien 0:2ccb9960a044 44 HTTP_PROCESSING, ///<Processing
donatien 0:2ccb9960a044 45 HTTP_PARSE, ///<url Parse error
donatien 0:2ccb9960a044 46 HTTP_DNS, ///<Could not resolve name
donatien 0:2ccb9960a044 47 HTTP_PRTCL, ///<Protocol error
donatien 0:2ccb9960a044 48 HTTP_NOTFOUND, ///<HTTP 404 Error
donatien 0:2ccb9960a044 49 HTTP_REFUSED, ///<HTTP 403 Error
donatien 0:2ccb9960a044 50 HTTP_ERROR, ///<HTTP xxx error
donatien 0:2ccb9960a044 51 HTTP_TIMEOUT, ///<Connection timeout
donatien 11:390362de8c3f 52 HTTP_CONN, ///<Connection error
donatien 11:390362de8c3f 53 HTTP_CLOSED, ///<Connection was closed by remote host
donatien 11:390362de8c3f 54 HTTP_OK = 0, ///<Success
donatien 0:2ccb9960a044 55 };
donatien 0:2ccb9960a044 56
donatien 0:2ccb9960a044 57 /**A simple HTTP Client
donatien 0:2ccb9960a044 58 The HTTPClient is composed of:
donatien 0:2ccb9960a044 59 - The actual client (HTTPClient)
donatien 0:2ccb9960a044 60 - 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 61 */
donatien 0:2ccb9960a044 62 class HTTPClient
donatien 0:2ccb9960a044 63 {
donatien 0:2ccb9960a044 64 public:
donatien 0:2ccb9960a044 65 ///Instantiate the HTTP client
mapellil 24:dafc02922e40 66 // HTTPClient(NetworkStack & _m_intf);
mapellil 24:dafc02922e40 67 HTTPClient(SpwfSAInterface & _m_intf);
mapellil 21:6e5c122ad9e5 68
donatien 0:2ccb9960a044 69 ~HTTPClient();
donatien 0:2ccb9960a044 70
donatien 0:2ccb9960a044 71 #if 0 //TODO add header handlers
donatien 0:2ccb9960a044 72 /**
donatien 0:2ccb9960a044 73 Provides a basic authentification feature (Base64 encoded username and password)
donatien 0:2ccb9960a044 74 Pass two NULL pointers to switch back to no authentication
donatien 0:2ccb9960a044 75 @param user username to use for authentication, must remain valid durlng the whole HTTP session
donatien 0:2ccb9960a044 76 @param user password to use for authentication, must remain valid durlng the whole HTTP session
donatien 0:2ccb9960a044 77 */
donatien 0:2ccb9960a044 78 void basicAuth(const char* user, const char* password); //Basic Authentification
donatien 0:2ccb9960a044 79 #endif
donatien 0:2ccb9960a044 80
donatien 0:2ccb9960a044 81 //High Level setup functions
donatien 16:1f743885e7de 82 /** Execute a GET request on the URL
donatien 0:2ccb9960a044 83 Blocks until completion
donatien 0:2ccb9960a044 84 @param url : url on which to execute the request
donatien 0:2ccb9960a044 85 @param pDataIn : pointer to an IHTTPDataIn instance that will collect the data returned by the request, can be NULL
donatien 0:2ccb9960a044 86 @param timeout waiting timeout in ms (osWaitForever for blocking function, not recommended)
donatien 12:89d09a6db00a 87 @return 0 on success, HTTP error (<0) on failure
donatien 0:2ccb9960a044 88 */
mapellil 19:17578cfdb57a 89 HTTPResult get(const char* url, IHTTPDataIn* pDataIn, int timeout /*= HTTP_CLIENT_DEFAULT_TIMEOUT*/); //Blocking
donatien 0:2ccb9960a044 90
donatien 16:1f743885e7de 91 /** Execute a GET request on the URL
donatien 0:2ccb9960a044 92 Blocks until completion
donatien 0:2ccb9960a044 93 This is a helper to directly get a piece of text from a HTTP result
donatien 0:2ccb9960a044 94 @param url : url on which to execute the request
donatien 0:2ccb9960a044 95 @param result : pointer to a char array in which the result will be stored
donatien 0:2ccb9960a044 96 @param maxResultLen : length of the char array (including space for the NULL-terminating char)
donatien 0:2ccb9960a044 97 @param timeout waiting timeout in ms (osWaitForever for blocking function, not recommended)
donatien 12:89d09a6db00a 98 @return 0 on success, HTTP error (<0) on failure
donatien 0:2ccb9960a044 99 */
mapellil 19:17578cfdb57a 100 HTTPResult get(const char* url, char* result, size_t maxResultLen, int timeout /*= HTTP_CLIENT_DEFAULT_TIMEOUT*/); //Blocking
donatien 0:2ccb9960a044 101
donatien 16:1f743885e7de 102 /** Execute a POST request on the URL
donatien 0:2ccb9960a044 103 Blocks until completion
donatien 0:2ccb9960a044 104 @param url : url on which to execute the request
donatien 0:2ccb9960a044 105 @param dataOut : a IHTTPDataOut instance that contains the data that will be posted
donatien 0:2ccb9960a044 106 @param pDataIn : pointer to an IHTTPDataIn instance that will collect the data returned by the request, can be NULL
donatien 0:2ccb9960a044 107 @param timeout waiting timeout in ms (osWaitForever for blocking function, not recommended)
donatien 12:89d09a6db00a 108 @return 0 on success, HTTP error (<0) on failure
donatien 0:2ccb9960a044 109 */
mapellil 19:17578cfdb57a 110 HTTPResult post(const char* url, const IHTTPDataOut& dataOut, IHTTPDataIn* pDataIn, int timeout /*= HTTP_CLIENT_DEFAULT_TIMEOUT*/); //Blocking
donatien 0:2ccb9960a044 111
donatien 16:1f743885e7de 112 /** Execute a PUT request on the URL
donatien 16:1f743885e7de 113 Blocks until completion
donatien 16:1f743885e7de 114 @param url : url on which to execute the request
donatien 16:1f743885e7de 115 @param dataOut : a IHTTPDataOut instance that contains the data that will be put
donatien 16:1f743885e7de 116 @param pDataIn : pointer to an IHTTPDataIn instance that will collect the data returned by the request, can be NULL
donatien 16:1f743885e7de 117 @param timeout waiting timeout in ms (osWaitForever for blocking function, not recommended)
donatien 16:1f743885e7de 118 @return 0 on success, HTTP error (<0) on failure
donatien 16:1f743885e7de 119 */
donatien 16:1f743885e7de 120 HTTPResult put(const char* url, const IHTTPDataOut& dataOut, IHTTPDataIn* pDataIn, int timeout = HTTP_CLIENT_DEFAULT_TIMEOUT); //Blocking
donatien 16:1f743885e7de 121
donatien 16:1f743885e7de 122 /** Execute a DELETE request on the URL
donatien 16:1f743885e7de 123 Blocks until completion
donatien 16:1f743885e7de 124 @param url : url on which to execute the request
donatien 16:1f743885e7de 125 @param pDataIn : pointer to an IHTTPDataIn instance that will collect the data returned by the request, can be NULL
donatien 16:1f743885e7de 126 @param timeout waiting timeout in ms (osWaitForever for blocking function, not recommended)
donatien 16:1f743885e7de 127 @return 0 on success, HTTP error (<0) on failure
donatien 16:1f743885e7de 128 */
donatien 16:1f743885e7de 129 HTTPResult del(const char* url, IHTTPDataIn* pDataIn, int timeout = HTTP_CLIENT_DEFAULT_TIMEOUT); //Blocking
donatien 16:1f743885e7de 130
donatien 0:2ccb9960a044 131 /** Get last request's HTTP response code
donatien 0:2ccb9960a044 132 @return The HTTP response code of the last request
donatien 0:2ccb9960a044 133 */
donatien 0:2ccb9960a044 134 int getHTTPResponseCode();
donatien 0:2ccb9960a044 135
donatien 0:2ccb9960a044 136 private:
donatien 0:2ccb9960a044 137 enum HTTP_METH
donatien 0:2ccb9960a044 138 {
donatien 0:2ccb9960a044 139 HTTP_GET,
donatien 0:2ccb9960a044 140 HTTP_POST,
donatien 16:1f743885e7de 141 HTTP_PUT,
donatien 16:1f743885e7de 142 HTTP_DELETE,
donatien 0:2ccb9960a044 143 HTTP_HEAD
donatien 0:2ccb9960a044 144 };
donatien 0:2ccb9960a044 145
donatien 12:89d09a6db00a 146 HTTPResult connect(const char* url, HTTP_METH method, IHTTPDataOut* pDataOut, IHTTPDataIn* pDataIn, int timeout); //Execute request
donatien 11:390362de8c3f 147 HTTPResult recv(char* buf, size_t minLen, size_t maxLen, size_t* pReadLen); //0 on success, err code on failure
donatien 11:390362de8c3f 148 HTTPResult send(char* buf, size_t len = 0); //0 on success, err code on failure
donatien 11:390362de8c3f 149 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
donatien 0:2ccb9960a044 150
mapellil 19:17578cfdb57a 151 //Parameters
mapellil 24:dafc02922e40 152 // NetworkStack & m_intf; // WiFi interface
mapellil 24:dafc02922e40 153 SpwfSAInterface & m_intf; // WiFi interface
mapellil 21:6e5c122ad9e5 154 TCPSocket m_sock; // TCP socket
mapellil 21:6e5c122ad9e5 155
donatien 12:89d09a6db00a 156 int m_timeout;
donatien 0:2ccb9960a044 157
donatien 0:2ccb9960a044 158 const char* m_basicAuthUser;
donatien 0:2ccb9960a044 159 const char* m_basicAuthPassword;
donatien 0:2ccb9960a044 160 int m_httpResponseCode;
mapellil 19:17578cfdb57a 161 char buf[CHUNK_SIZE];
donatien 0:2ccb9960a044 162 };
donatien 0:2ccb9960a044 163
donatien 0:2ccb9960a044 164 //Including data containers here for more convenience
donatien 0:2ccb9960a044 165 #include "data/HTTPText.h"
donatien 0:2ccb9960a044 166 #include "data/HTTPMap.h"
donatien 0:2ccb9960a044 167
donatien 0:2ccb9960a044 168 #endif