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