Fixed custom headers and Basic authorization, added support for redirection, functional file download interface can be used for SW updates and more.

Dependents:   Sample_HTTPClient Sample_HTTPClient LWM2M_NanoService_Ethernet LWM2M_NanoService_Ethernet ... more

Fork of HTTPClient by Vincent Wochnik

More recent changes - added iCal processing.

Derivative of a derivative, however this one works when it comes to supplying Basic authorization to access a protected resource. Some additional changes to the debug interface to clean it up for consistency with many other components I have.

Committer:
WiredHome
Date:
Sun Jul 14 01:42:33 2019 +0000
Revision:
49:c5abb7ae070b
Parent:
38:2ef07232f65c
Correct an api parameter type.

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 11:390362de8c3f 27 #include "TCPSocketConnection.h"
donatien 0:2ccb9960a044 28
donatien 12:89d09a6db00a 29 #define HTTP_CLIENT_DEFAULT_TIMEOUT 15000
donatien 0:2ccb9960a044 30
donatien 0:2ccb9960a044 31 class HTTPData;
donatien 0:2ccb9960a044 32
donatien 0:2ccb9960a044 33 #include "IHTTPData.h"
donatien 0:2ccb9960a044 34 #include "mbed.h"
donatien 0:2ccb9960a044 35
donatien 0:2ccb9960a044 36 ///HTTP client results
WiredHome 20:4ea5255c1b04 37 enum HTTPResult {
WiredHome 30:1df62fedb13b 38 HTTP_OK = 0, ///<Success
WiredHome 30:1df62fedb13b 39 HTTP_PROCESSING, ///<Processing
WiredHome 30:1df62fedb13b 40 HTTP_PARSE, ///<url Parse error
WiredHome 30:1df62fedb13b 41 HTTP_DNS, ///<Could not resolve name
WiredHome 30:1df62fedb13b 42 HTTP_PRTCL, ///<Protocol error
WiredHome 30:1df62fedb13b 43 HTTP_NOTFOUND, ///<HTTP 404 Error
WiredHome 30:1df62fedb13b 44 HTTP_REFUSED, ///<HTTP 403 Error
WiredHome 30:1df62fedb13b 45 HTTP_ERROR, ///<HTTP xxx error
WiredHome 30:1df62fedb13b 46 HTTP_TIMEOUT, ///<Connection timeout
WiredHome 30:1df62fedb13b 47 HTTP_CONN, ///<Connection error
WiredHome 30:1df62fedb13b 48 HTTP_CLOSED, ///<Connection was closed by remote host
donatien 0:2ccb9960a044 49 };
donatien 0:2ccb9960a044 50
donatien 0:2ccb9960a044 51 /**A simple HTTP Client
donatien 0:2ccb9960a044 52 The HTTPClient is composed of:
donatien 0:2ccb9960a044 53 - The actual client (HTTPClient)
donatien 0:2ccb9960a044 54 - 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 55 */
donatien 0:2ccb9960a044 56 class HTTPClient
donatien 0:2ccb9960a044 57 {
donatien 0:2ccb9960a044 58 public:
WiredHome 20:4ea5255c1b04 59 ///Instantiate the HTTP client
WiredHome 20:4ea5255c1b04 60 HTTPClient();
WiredHome 20:4ea5255c1b04 61 ~HTTPClient();
WiredHome 20:4ea5255c1b04 62
WiredHome 38:2ef07232f65c 63 /// Get the text form of the error number
WiredHome 38:2ef07232f65c 64 ///
WiredHome 38:2ef07232f65c 65 /// Gets a pointer to a text message that described the result code.
WiredHome 38:2ef07232f65c 66 ///
WiredHome 38:2ef07232f65c 67 /// @param[in] res is the HTTPResult code to translate to text.
WiredHome 38:2ef07232f65c 68 /// @returns a pointer to a text message.
WiredHome 38:2ef07232f65c 69 ///
WiredHome 38:2ef07232f65c 70 static const char * GetErrorMessage(HTTPResult res);
WiredHome 38:2ef07232f65c 71
WiredHome 20:4ea5255c1b04 72 /**
WiredHome 20:4ea5255c1b04 73 Provides a basic authentification feature (Base64 encoded username and password)
WiredHome 20:4ea5255c1b04 74 Pass two NULL pointers to switch back to no authentication
WiredHome 32:7b9919d59194 75 @param[in] user username to use for authentication, must remain valid durlng the whole HTTP session
WiredHome 32:7b9919d59194 76 @param[in] user password to use for authentication, must remain valid durlng the whole HTTP session
WiredHome 20:4ea5255c1b04 77 */
WiredHome 20:4ea5255c1b04 78 void basicAuth(const char* user, const char* password); //Basic Authentification
WiredHome 20:4ea5255c1b04 79
WiredHome 20:4ea5255c1b04 80 /**
WiredHome 20:4ea5255c1b04 81 Set custom headers for request.
WiredHome 20:4ea5255c1b04 82
WiredHome 20:4ea5255c1b04 83 Pass NULL, 0 to turn off custom headers.
WiredHome 20:4ea5255c1b04 84
WiredHome 20:4ea5255c1b04 85 @code
WiredHome 20:4ea5255c1b04 86 const char * hdrs[] =
WiredHome 20:4ea5255c1b04 87 {
WiredHome 20:4ea5255c1b04 88 "Connection", "keep-alive",
WiredHome 20:4ea5255c1b04 89 "Accept", "text/html",
WiredHome 20:4ea5255c1b04 90 "User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64)",
WiredHome 20:4ea5255c1b04 91 "Accept-Encoding", "gzip,deflate,sdch",
WiredHome 20:4ea5255c1b04 92 "Accept-Language", "en-US,en;q=0.8",
WiredHome 20:4ea5255c1b04 93 };
WiredHome 20:4ea5255c1b04 94
WiredHome 20:4ea5255c1b04 95 http.basicAuth("username", "password");
WiredHome 20:4ea5255c1b04 96 http.customHeaders(hdrs, 5);
WiredHome 20:4ea5255c1b04 97 @endcode
WiredHome 20:4ea5255c1b04 98
WiredHome 32:7b9919d59194 99 @param[in] headers an array (size multiple of two) key-value pairs, must remain valid during the whole HTTP session
WiredHome 32:7b9919d59194 100 @param[in] pairs number of key-value pairs
WiredHome 20:4ea5255c1b04 101 */
WiredHome 20:4ea5255c1b04 102 void customHeaders(const char** headers, size_t pairs);
WiredHome 20:4ea5255c1b04 103
WiredHome 20:4ea5255c1b04 104 //High Level setup functions
WiredHome 20:4ea5255c1b04 105 /** Execute a GET request on the URL
WiredHome 20:4ea5255c1b04 106 Blocks until completion
WiredHome 32:7b9919d59194 107 @param[in] url : url on which to execute the request
WiredHome 32:7b9919d59194 108 @param[in,out] pDataIn : pointer to an IHTTPDataIn instance that will collect the data returned by the request, can be NULL
WiredHome 32:7b9919d59194 109 @param[in] timeout waiting timeout in ms (osWaitForever for blocking function, not recommended)
WiredHome 20:4ea5255c1b04 110 @return 0 on success, HTTP error (<0) on failure
WiredHome 20:4ea5255c1b04 111 */
WiredHome 20:4ea5255c1b04 112 HTTPResult get(const char* url, IHTTPDataIn* pDataIn, int timeout = HTTP_CLIENT_DEFAULT_TIMEOUT); //Blocking
WiredHome 20:4ea5255c1b04 113
WiredHome 20:4ea5255c1b04 114 /** Execute a GET request on the URL
WiredHome 20:4ea5255c1b04 115 Blocks until completion
WiredHome 20:4ea5255c1b04 116 This is a helper to directly get a piece of text from a HTTP result
WiredHome 32:7b9919d59194 117 @param[in] url : url on which to execute the request
WiredHome 32:7b9919d59194 118 @param[out] result : pointer to a char array in which the result will be stored
WiredHome 32:7b9919d59194 119 @param[in] maxResultLen : length of the char array (including space for the NULL-terminating char)
WiredHome 32:7b9919d59194 120 @param[in] timeout waiting timeout in ms (osWaitForever for blocking function, not recommended)
WiredHome 20:4ea5255c1b04 121 @return 0 on success, HTTP error (<0) on failure
WiredHome 20:4ea5255c1b04 122 */
WiredHome 20:4ea5255c1b04 123 HTTPResult get(const char* url, char* result, size_t maxResultLen, int timeout = HTTP_CLIENT_DEFAULT_TIMEOUT); //Blocking
donatien 0:2ccb9960a044 124
WiredHome 20:4ea5255c1b04 125 /** Execute a POST request on the URL
WiredHome 20:4ea5255c1b04 126 Blocks until completion
WiredHome 32:7b9919d59194 127 @param[in] url : url on which to execute the request
WiredHome 32:7b9919d59194 128 @param[out] dataOut : a IHTTPDataOut instance that contains the data that will be posted
WiredHome 32:7b9919d59194 129 @param[in,out] pDataIn : pointer to an IHTTPDataIn instance that will collect the data returned by the request, can be NULL
WiredHome 32:7b9919d59194 130 @param[in] timeout waiting timeout in ms (osWaitForever for blocking function, not recommended)
WiredHome 20:4ea5255c1b04 131 @return 0 on success, HTTP error (<0) on failure
WiredHome 20:4ea5255c1b04 132 */
WiredHome 20:4ea5255c1b04 133 HTTPResult post(const char* url, const IHTTPDataOut& dataOut, IHTTPDataIn* pDataIn, int timeout = HTTP_CLIENT_DEFAULT_TIMEOUT); //Blocking
WiredHome 20:4ea5255c1b04 134
WiredHome 20:4ea5255c1b04 135 /** Execute a PUT request on the URL
WiredHome 20:4ea5255c1b04 136 Blocks until completion
WiredHome 32:7b9919d59194 137 @param[in] url : url on which to execute the request
WiredHome 32:7b9919d59194 138 @param[in] dataOut : a IHTTPDataOut instance that contains the data that will be put
WiredHome 32:7b9919d59194 139 @param[in,out] pDataIn : pointer to an IHTTPDataIn instance that will collect the data returned by the request, can be NULL
WiredHome 32:7b9919d59194 140 @param[in] timeout waiting timeout in ms (osWaitForever for blocking function, not recommended)
WiredHome 20:4ea5255c1b04 141 @return 0 on success, HTTP error (<0) on failure
WiredHome 20:4ea5255c1b04 142 */
WiredHome 20:4ea5255c1b04 143 HTTPResult put(const char* url, const IHTTPDataOut& dataOut, IHTTPDataIn* pDataIn, int timeout = HTTP_CLIENT_DEFAULT_TIMEOUT); //Blocking
WiredHome 20:4ea5255c1b04 144
WiredHome 20:4ea5255c1b04 145 /** Execute a DELETE request on the URL
WiredHome 20:4ea5255c1b04 146 Blocks until completion
WiredHome 32:7b9919d59194 147 @param[in] url : url on which to execute the request
WiredHome 32:7b9919d59194 148 @param[in,out] pDataIn : pointer to an IHTTPDataIn instance that will collect the data returned by the request, can be NULL
WiredHome 32:7b9919d59194 149 @param[in] timeout waiting timeout in ms (osWaitForever for blocking function, not recommended)
WiredHome 20:4ea5255c1b04 150 @return 0 on success, HTTP error (<0) on failure
WiredHome 20:4ea5255c1b04 151 */
WiredHome 20:4ea5255c1b04 152 HTTPResult del(const char* url, IHTTPDataIn* pDataIn, int timeout = HTTP_CLIENT_DEFAULT_TIMEOUT); //Blocking
WiredHome 20:4ea5255c1b04 153
WiredHome 20:4ea5255c1b04 154 /** Get last request's HTTP response code
WiredHome 20:4ea5255c1b04 155 @return The HTTP response code of the last request
WiredHome 20:4ea5255c1b04 156 */
WiredHome 20:4ea5255c1b04 157 int getHTTPResponseCode();
WiredHome 20:4ea5255c1b04 158
WiredHome 24:eee214e3e806 159 /** Set the maximum number of automated redirections
WiredHome 32:7b9919d59194 160 @param[in] i is the number of redirections. Values < 1 are
WiredHome 24:eee214e3e806 161 set to 1.
WiredHome 24:eee214e3e806 162 */
WiredHome 24:eee214e3e806 163 void setMaxRedirections(int i = 1);
WiredHome 24:eee214e3e806 164
WiredHome 24:eee214e3e806 165 /** get the redirect location url
WiredHome 24:eee214e3e806 166 @returns const char pointer to the url.
WiredHome 24:eee214e3e806 167 */
WiredHome 24:eee214e3e806 168 const char * getLocation() {
WiredHome 24:eee214e3e806 169 return m_location;
WiredHome 24:eee214e3e806 170 }
WiredHome 24:eee214e3e806 171
donatien 0:2ccb9960a044 172 private:
WiredHome 20:4ea5255c1b04 173 enum HTTP_METH {
WiredHome 20:4ea5255c1b04 174 HTTP_GET,
WiredHome 20:4ea5255c1b04 175 HTTP_POST,
WiredHome 20:4ea5255c1b04 176 HTTP_PUT,
WiredHome 20:4ea5255c1b04 177 HTTP_DELETE,
WiredHome 20:4ea5255c1b04 178 HTTP_HEAD
WiredHome 20:4ea5255c1b04 179 };
donatien 0:2ccb9960a044 180
WiredHome 20:4ea5255c1b04 181 HTTPResult connect(const char* url, HTTP_METH method, IHTTPDataOut* pDataOut, IHTTPDataIn* pDataIn, int timeout); //Execute request
WiredHome 20:4ea5255c1b04 182 HTTPResult recv(char* buf, size_t minLen, size_t maxLen, size_t* pReadLen); //0 on success, err code on failure
WiredHome 49:c5abb7ae070b 183 HTTPResult send(char* buf, size_t len = 0); //0 on success, err code on failure
WiredHome 20:4ea5255c1b04 184 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
WiredHome 20:4ea5255c1b04 185 void createauth (const char *user, const char *pwd, char *buf, int len);
WiredHome 20:4ea5255c1b04 186 int base64enc(const char *input, unsigned int length, char *output, int len);
donatien 0:2ccb9960a044 187
WiredHome 20:4ea5255c1b04 188 //Parameters
WiredHome 20:4ea5255c1b04 189 TCPSocketConnection m_sock;
WiredHome 20:4ea5255c1b04 190
WiredHome 20:4ea5255c1b04 191 int m_timeout;
donatien 0:2ccb9960a044 192
WiredHome 21:d33f7e1ce811 193 char* m_basicAuthUser;
WiredHome 21:d33f7e1ce811 194 char* m_basicAuthPassword;
WiredHome 20:4ea5255c1b04 195 const char** m_customHeaders;
WiredHome 20:4ea5255c1b04 196 size_t m_nCustomHeaders;
WiredHome 20:4ea5255c1b04 197 int m_httpResponseCode;
WiredHome 24:eee214e3e806 198 int m_maxredirections;
WiredHome 24:eee214e3e806 199 char * m_location;
donatien 0:2ccb9960a044 200
donatien 0:2ccb9960a044 201 };
donatien 0:2ccb9960a044 202
donatien 0:2ccb9960a044 203 //Including data containers here for more convenience
donatien 0:2ccb9960a044 204 #include "data/HTTPText.h"
donatien 0:2ccb9960a044 205 #include "data/HTTPMap.h"
WiredHome 23:517fec8b8b99 206 #include "data/HTTPFile.h"
WiredHome 37:74c1c4527f70 207 #include "data/HTTPJson.h"
donatien 0:2ccb9960a044 208
donatien 0:2ccb9960a044 209 #endif