PullRequest
Dependents: CyaSSL-Twitter-OAuth4Tw TweetTest NetworkThermometer CyaSSL-Twitter-OAuth4Tw_arrange
Fork of HTTPClient by
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 #define HTTP_CLIENT_DEFAULT_TIMEOUT 15000 00028 00029 class HTTPData; 00030 00031 #include "IHTTPData.h" 00032 #include "mbed.h" 00033 00034 ///HTTP client results 00035 enum HTTPResult { 00036 HTTP_PROCESSING = -11, ///<Processing 00037 HTTP_PARSE, ///<url Parse error 00038 HTTP_DNS, ///<Could not resolve name 00039 HTTP_PRTCL, ///<Protocol error 00040 HTTP_NOTFOUND, ///<HTTP 404 Error 00041 HTTP_REFUSED, ///<HTTP 403 Error 00042 HTTP_ERROR, ///<HTTP xxx error 00043 HTTP_TIMEOUT, ///<Connection timeout 00044 HTTP_CONN, ///<Connection error 00045 HTTP_CLOSED, ///<Connection was closed by remote host 00046 HTTP_REDIRECT, ///<HTTP 300 - 303 00047 HTTP_OK = 0, ///<Success 00048 }; 00049 00050 /**A simple HTTP Client 00051 The HTTPClient is composed of: 00052 - The actual client (HTTPClient) 00053 - 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) 00054 */ 00055 class HTTPClient 00056 { 00057 public: 00058 ///Instantiate the HTTP client 00059 HTTPClient(); 00060 ~HTTPClient(); 00061 00062 /** 00063 Provides a basic authentification feature (Base64 encoded username and password) 00064 Pass two NULL pointers to switch back to no authentication 00065 @param user username to use for authentication, must remain valid durlng the whole HTTP session 00066 @param user password to use for authentication, must remain valid durlng the whole HTTP session 00067 */ 00068 HTTPResult basicAuth(const char* user, const char* password); //Basic Authentification 00069 00070 //High Level setup functions 00071 /** Execute a GET request on the URL 00072 Blocks until completion 00073 @param url : url on which to execute the request 00074 @param pDataIn : pointer to an IHTTPDataIn instance that will collect the data returned by the request, can be NULL 00075 @param timeout waiting timeout in ms (osWaitForever for blocking function, not recommended) 00076 @return 0 on success, HTTP error (<0) on failure 00077 */ 00078 HTTPResult get(const char* url, IHTTPDataIn* pDataIn, int timeout = HTTP_CLIENT_DEFAULT_TIMEOUT); //Blocking 00079 00080 /** Execute a GET request on the URL 00081 Blocks until completion 00082 This is a helper to directly get a piece of text from a HTTP result 00083 @param url : url on which to execute the request 00084 @param result : pointer to a char array in which the result will be stored 00085 @param maxResultLen : length of the char array (including space for the NULL-terminating char) 00086 @param timeout waiting timeout in ms (osWaitForever for blocking function, not recommended) 00087 @return 0 on success, HTTP error (<0) on failure 00088 */ 00089 HTTPResult get(const char* url, char* result, size_t maxResultLen, int timeout = HTTP_CLIENT_DEFAULT_TIMEOUT); //Blocking 00090 00091 /** Execute a POST request on the URL 00092 Blocks until completion 00093 @param url : url on which to execute the request 00094 @param dataOut : a IHTTPDataOut instance that contains the data that will be posted 00095 @param pDataIn : pointer to an IHTTPDataIn instance that will collect the data returned by the request, can be NULL 00096 @param timeout waiting timeout in ms (osWaitForever for blocking function, not recommended) 00097 @return 0 on success, HTTP error (<0) on failure 00098 */ 00099 HTTPResult post(const char* url, const IHTTPDataOut& dataOut, IHTTPDataIn* pDataIn, int timeout = HTTP_CLIENT_DEFAULT_TIMEOUT); //Blocking 00100 00101 /** Execute a PUT request on the URL 00102 Blocks until completion 00103 @param url : url on which to execute the request 00104 @param dataOut : a IHTTPDataOut instance that contains the data that will be put 00105 @param pDataIn : pointer to an IHTTPDataIn instance that will collect the data returned by the request, can be NULL 00106 @param timeout waiting timeout in ms (osWaitForever for blocking function, not recommended) 00107 @return 0 on success, HTTP error (<0) on failure 00108 */ 00109 HTTPResult put(const char* url, const IHTTPDataOut& dataOut, IHTTPDataIn* pDataIn, int timeout = HTTP_CLIENT_DEFAULT_TIMEOUT); //Blocking 00110 00111 /** Execute a DELETE request on the URL 00112 Blocks until completion 00113 @param url : url on which to execute the request 00114 @param pDataIn : pointer to an IHTTPDataIn instance that will collect the data returned by the request, can be NULL 00115 @param timeout waiting timeout in ms (osWaitForever for blocking function, not recommended) 00116 @return 0 on success, HTTP error (<0) on failure 00117 */ 00118 HTTPResult del(const char* url, IHTTPDataIn* pDataIn, int timeout = HTTP_CLIENT_DEFAULT_TIMEOUT); //Blocking 00119 00120 /** Get last request's HTTP response code 00121 @return The HTTP response code of the last request 00122 */ 00123 int getHTTPResponseCode(); 00124 00125 void setHeader(const char *header) ; /* set http headers */ 00126 HTTPResult setSSLversion(int minorV) ; /* set SSL/TLS version. 0: SSL3, 1: TLS1.0, 2: TLS1.1, 3: TLS1.2 */ 00127 void setLocationBuf(char *url, int size) ; /* set URL buffer for redirection */ 00128 00129 private: 00130 enum HTTP_METH { 00131 HTTP_GET, 00132 HTTP_POST, 00133 HTTP_PUT, 00134 HTTP_DELETE, 00135 HTTP_HEAD 00136 }; 00137 00138 HTTPResult connect(const char* url, HTTP_METH method, IHTTPDataOut* pDataOut, IHTTPDataIn* pDataIn, int timeout); //Execute request 00139 HTTPResult recv(char* buf, size_t minLen, size_t maxLen, size_t* pReadLen); //0 on success, err code on failure 00140 HTTPResult send(char* buf, size_t len = 0); //0 on success, err code on failure 00141 HTTPResult flush(void); //0 on success, err code on failure 00142 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 00143 void wolfssl_free(void) ; 00144 HTTPResult bAuth(void) ; 00145 HTTPResult readHeader(void) ; 00146 00147 //Parameters 00148 00149 int m_timeout; 00150 00151 const char* m_basicAuthUser; 00152 const char* m_basicAuthPassword; 00153 int m_httpResponseCode; 00154 00155 const char * header ; 00156 char * redirect_url ; 00157 int redirect_url_size ; 00158 int redirect ; 00159 00160 /* for CyaSSL */ 00161 int SSLver ; 00162 uint16_t port; 00163 struct WOLFSSL_CTX* ctx ; 00164 struct WOLFSSL * ssl ; 00165 }; 00166 00167 //Including data containers here for more convenience 00168 #include "data/HTTPText.h" 00169 #include "data/HTTPMap.h" 00170 00171 #endif
Generated on Thu Jul 14 2022 18:25:01 by 1.7.2