Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependents: mbed-demo-http-get-json
HTTPClient.h
00001 /* HTTPClient.h */ 00002 /* Copyright (C) 2012 mbed.org, MIT License 00003 * 00004 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software 00005 * and associated documentation files (the "Software"), to deal in the Software without restriction, 00006 * including without limitation the rights to use, copy, modify, merge, publish, distribute, 00007 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is 00008 * furnished to do so, subject to the following conditions: 00009 * 00010 * The above copyright notice and this permission notice shall be included in all copies or 00011 * substantial portions of the Software. 00012 * 00013 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING 00014 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 00015 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 00016 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 00017 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 00018 */ 00019 00020 /** \file 00021 HTTP Client header file 00022 */ 00023 00024 #ifndef HTTP_CLIENT_H 00025 #define HTTP_CLIENT_H 00026 00027 //#include "TCPSocketConnection.h" //delete for mbed 6.0 00028 00029 #define HTTP_CLIENT_DEFAULT_TIMEOUT 5000 00030 00031 class HTTPData; 00032 00033 #include "IHTTPData.h" 00034 #include "mbed.h" 00035 00036 ///HTTP client results 00037 enum HTTPResult { 00038 HTTP_OK = 0, ///<Success 00039 HTTP_PROCESSING, ///<Processing 00040 HTTP_PARSE, ///<url Parse error 00041 HTTP_DNS, ///<Could not resolve name 00042 HTTP_PRTCL, ///<Protocol error 00043 HTTP_NOTFOUND, ///<HTTP 404 Error 00044 HTTP_REFUSED, ///<HTTP 403 Error 00045 HTTP_ERROR, ///<HTTP xxx error 00046 HTTP_TIMEOUT, ///<Connection timeout 00047 HTTP_CONN, ///<Connection error 00048 HTTP_CLOSED, ///<Connection was closed by remote host 00049 }; 00050 00051 /**A simple HTTP Client 00052 The HTTPClient is composed of: 00053 - The actual client (HTTPClient) 00054 - 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) 00055 */ 00056 class HTTPClient 00057 { 00058 public: 00059 ///Instantiate the HTTP client 00060 //HTTPClient(); 00061 HTTPClient(NetworkInterface *interface);// modify for mbed 6.0 00062 ~HTTPClient(); 00063 00064 /// Get the text form of the error number 00065 /// 00066 /// Gets a pointer to a text message that described the result code. 00067 /// 00068 /// @param[in] res is the HTTPResult code to translate to text. 00069 /// @returns a pointer to a text message. 00070 /// 00071 static const char * GetErrorMessage(HTTPResult res); 00072 00073 /** 00074 Provides a basic authentification feature (Base64 encoded username and password) 00075 Pass two NULL pointers to switch back to no authentication 00076 @param[in] user username to use for authentication, must remain valid durlng the whole HTTP session 00077 @param[in] user password to use for authentication, must remain valid durlng the whole HTTP session 00078 */ 00079 void basicAuth(const char* user, const char* password); //Basic Authentification 00080 00081 /** 00082 Set custom headers for request. 00083 00084 Pass NULL, 0 to turn off custom headers. 00085 00086 @code 00087 const char * hdrs[] = 00088 { 00089 "Connection", "keep-alive", 00090 "Accept", "text/html", 00091 "User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64)", 00092 "Accept-Encoding", "gzip,deflate,sdch", 00093 "Accept-Language", "en-US,en;q=0.8", 00094 }; 00095 00096 http.basicAuth("username", "password"); 00097 http.customHeaders(hdrs, 5); 00098 @endcode 00099 00100 @param[in] headers an array (size multiple of two) key-value pairs, must remain valid during the whole HTTP session 00101 @param[in] pairs number of key-value pairs 00102 */ 00103 void customHeaders(const char** headers, size_t pairs); 00104 00105 //High Level setup functions 00106 /** Execute a GET request on the URL 00107 Blocks until completion 00108 @param[in] url : url on which to execute the request 00109 @param[in,out] pDataIn : pointer to an IHTTPDataIn instance that will collect the data returned by the request, can be NULL 00110 @param[in] timeout waiting timeout in ms (osWaitForever for blocking function, not recommended) 00111 @return 0 on success, HTTP error (<0) on failure 00112 */ 00113 HTTPResult get(const char* url, IHTTPDataIn* pDataIn, int timeout = HTTP_CLIENT_DEFAULT_TIMEOUT); //Blocking 00114 00115 /** Execute a GET request on the URL 00116 Blocks until completion 00117 This is a helper to directly get a piece of text from a HTTP result 00118 @param[in] url : url on which to execute the request 00119 @param[out] result : pointer to a char array in which the result will be stored 00120 @param[in] maxResultLen : length of the char array (including space for the NULL-terminating char) 00121 @param[in] timeout waiting timeout in ms (osWaitForever for blocking function, not recommended) 00122 @return 0 on success, HTTP error (<0) on failure 00123 */ 00124 HTTPResult get(const char* url, char* result, size_t maxResultLen, int timeout = HTTP_CLIENT_DEFAULT_TIMEOUT); //Blocking 00125 00126 /** Execute a POST request on the URL 00127 Blocks until completion 00128 @param[in] url : url on which to execute the request 00129 @param[out] dataOut : a IHTTPDataOut instance that contains the data that will be posted 00130 @param[in,out] pDataIn : pointer to an IHTTPDataIn instance that will collect the data returned by the request, can be NULL 00131 @param[in] timeout waiting timeout in ms (osWaitForever for blocking function, not recommended) 00132 @return 0 on success, HTTP error (<0) on failure 00133 */ 00134 HTTPResult post(const char* url, const IHTTPDataOut& dataOut, IHTTPDataIn* pDataIn, int timeout = HTTP_CLIENT_DEFAULT_TIMEOUT); //Blocking 00135 00136 /** Execute a PUT request on the URL 00137 Blocks until completion 00138 @param[in] url : url on which to execute the request 00139 @param[in] dataOut : a IHTTPDataOut instance that contains the data that will be put 00140 @param[in,out] pDataIn : pointer to an IHTTPDataIn instance that will collect the data returned by the request, can be NULL 00141 @param[in] timeout waiting timeout in ms (osWaitForever for blocking function, not recommended) 00142 @return 0 on success, HTTP error (<0) on failure 00143 */ 00144 HTTPResult put(const char* url, const IHTTPDataOut& dataOut, IHTTPDataIn* pDataIn, int timeout = HTTP_CLIENT_DEFAULT_TIMEOUT); //Blocking 00145 00146 /** Execute a DELETE request on the URL 00147 Blocks until completion 00148 @param[in] url : url on which to execute the request 00149 @param[in,out] pDataIn : pointer to an IHTTPDataIn instance that will collect the data returned by the request, can be NULL 00150 @param[in] timeout waiting timeout in ms (osWaitForever for blocking function, not recommended) 00151 @return 0 on success, HTTP error (<0) on failure 00152 */ 00153 HTTPResult del(const char* url, IHTTPDataIn* pDataIn, int timeout = HTTP_CLIENT_DEFAULT_TIMEOUT); //Blocking 00154 00155 /** Get last request's HTTP response code 00156 @return The HTTP response code of the last request 00157 */ 00158 int getHTTPResponseCode(); 00159 00160 /** Set the maximum number of automated redirections 00161 @param[in] i is the number of redirections. Values < 1 are 00162 set to 1. 00163 */ 00164 void setMaxRedirections(int i = 1); 00165 00166 /** get the redirect location url 00167 @returns const char pointer to the url. 00168 */ 00169 const char * getLocation() { 00170 return m_location; 00171 } 00172 00173 private: 00174 enum HTTP_METH { 00175 HTTP_GET, 00176 HTTP_POST, 00177 HTTP_PUT, 00178 HTTP_DELETE, 00179 HTTP_HEAD 00180 }; 00181 00182 HTTPResult connect(const char* url, HTTP_METH method, IHTTPDataOut* pDataOut, IHTTPDataIn* pDataIn, int timeout); //Execute request 00183 HTTPResult recv(char* buf, size_t minLen, size_t maxLen, size_t* pReadLen); //0 on success, err code on failure 00184 HTTPResult send(char* buf, size_t len = 0); //0 on success, err code on failure 00185 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 00186 void createauth (const char *user, const char *pwd, char *buf, int len); 00187 int base64enc(const char *input, unsigned int length, char *output, int len); 00188 00189 //Parameters 00190 //TCPSocketConnection m_sock; 00191 NetworkInterface *net; //add for mbed 6.0 00192 TCPSocket m_sock; // modify for mbed 6.0 00193 nsapi_size_or_error_t tcp_connected; //add for mbed 6.0 00194 int m_timeout; 00195 00196 char* m_basicAuthUser; 00197 char* m_basicAuthPassword; 00198 const char** m_customHeaders; 00199 size_t m_nCustomHeaders; 00200 int m_httpResponseCode; 00201 int m_maxredirections; 00202 char * m_location; 00203 00204 }; 00205 00206 //Including data containers here for more convenience 00207 #include "data/HTTPText.h" 00208 #include "data/HTTPMap.h" 00209 #include "data/HTTPFile.h" 00210 #include "data/HTTPJson.h" 00211 00212 #endif
Generated on Sat Jul 16 2022 06:50:27 by
1.7.2