thermostat remote version
Dependents: df-2013-thermostat-handson df-2013-minihack-thermostat-complete df-2013-minihack-thermostat df-2013-thermostat-remotes
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 #include "TCPSocketConnection.h" 00028 00029 #define HTTP_CLIENT_DEFAULT_TIMEOUT 15000 00030 00031 class HTTPData; 00032 00033 #include "IHTTPData.h" 00034 #include "mbed.h" 00035 00036 ///HTTP client results 00037 enum HTTPResult 00038 { 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 HTTP_OK = 0, ///<Success 00050 }; 00051 00052 /**A simple HTTP Client 00053 The HTTPClient is composed of: 00054 - The actual client (HTTPClient) 00055 - 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) 00056 */ 00057 class HTTPClient 00058 { 00059 public: 00060 ///Instantiate the HTTP client 00061 HTTPClient(); 00062 ~HTTPClient(); 00063 00064 #if 0 //TODO add header handlers 00065 /** 00066 Provides a basic authentification feature (Base64 encoded username and password) 00067 Pass two NULL pointers to switch back to no authentication 00068 @param user username to use for authentication, must remain valid durlng the whole HTTP session 00069 @param user password to use for authentication, must remain valid durlng the whole HTTP session 00070 */ 00071 void basicAuth(const char* user, const char* password); //Basic Authentification 00072 #endif 00073 00074 //High Level setup functions 00075 /** Execute a GET request on the url 00076 Blocks until completion 00077 @param url : url on which to execute the request 00078 @param pDataIn : pointer to an IHTTPDataIn instance that will collect the data returned by the request, can be NULL 00079 @param timeout waiting timeout in ms (osWaitForever for blocking function, not recommended) 00080 @return 0 on success, HTTP error (<0) on failure 00081 */ 00082 HTTPResult get(const char* url, IHTTPDataIn* pDataIn, int timeout = HTTP_CLIENT_DEFAULT_TIMEOUT); //Blocking 00083 00084 /** Execute a GET request on the url 00085 Blocks until completion 00086 This is a helper to directly get a piece of text from a HTTP result 00087 @param url : url on which to execute the request 00088 @param result : pointer to a char array in which the result will be stored 00089 @param maxResultLen : length of the char array (including space for the NULL-terminating char) 00090 @param timeout waiting timeout in ms (osWaitForever for blocking function, not recommended) 00091 @return 0 on success, HTTP error (<0) on failure 00092 */ 00093 HTTPResult get(const char* url, char* result, size_t maxResultLen, int timeout = HTTP_CLIENT_DEFAULT_TIMEOUT); //Blocking 00094 00095 /** Execute a POST request on the url 00096 Blocks until completion 00097 @param url : url on which to execute the request 00098 @param dataOut : a IHTTPDataOut instance that contains the data that will be posted 00099 @param pDataIn : pointer to an IHTTPDataIn instance that will collect the data returned by the request, can be NULL 00100 @param timeout waiting timeout in ms (osWaitForever for blocking function, not recommended) 00101 @return 0 on success, HTTP error (<0) on failure 00102 */ 00103 HTTPResult post(const char* url, const IHTTPDataOut& dataOut, IHTTPDataIn* pDataIn, int timeout = HTTP_CLIENT_DEFAULT_TIMEOUT); //Blocking 00104 00105 /** Get last request's HTTP response code 00106 @return The HTTP response code of the last request 00107 */ 00108 int getHTTPResponseCode(); 00109 00110 private: 00111 enum HTTP_METH 00112 { 00113 HTTP_GET, 00114 HTTP_POST, 00115 HTTP_HEAD 00116 }; 00117 00118 HTTPResult connect(const char* url, HTTP_METH method, IHTTPDataOut* pDataOut, IHTTPDataIn* pDataIn, int timeout); //Execute request 00119 HTTPResult recv(char* buf, size_t minLen, size_t maxLen, size_t* pReadLen); //0 on success, err code on failure 00120 HTTPResult send(char* buf, size_t len = 0); //0 on success, err code on failure 00121 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 00122 00123 //Parameters 00124 TCPSocketConnection m_sock; 00125 00126 int m_timeout; 00127 00128 const char* m_basicAuthUser; 00129 const char* m_basicAuthPassword; 00130 int m_httpResponseCode; 00131 00132 }; 00133 00134 //Including data containers here for more convenience 00135 #include "data/HTTPText.h" 00136 #include "data/HTTPMap.h" 00137 00138 #endif
Generated on Thu Jul 14 2022 02:11:53 by 1.7.2