2 /* Copyright (C) 2012 mbed.org, MIT License
4 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
5 * and associated documentation files (the "Software"), to deal in the Software without restriction,
6 * including without limitation the rights to use, copy, modify, merge, publish, distribute,
7 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
8 * furnished to do so, subject to the following conditions:
10 * The above copyright notice and this permission notice shall be included in all copies or
11 * substantial portions of the Software.
13 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
14 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
15 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
16 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
17 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21 HTTP Client header file
27 #include "TCPSocketConnection.h"
29 #define HTTP_CLIENT_DEFAULT_TIMEOUT 15000
33 #include "IHTTPData.h"
36 ///HTTP client results
39 HTTP_PROCESSING, ///<Processing
40 HTTP_PARSE, ///<url Parse error
41 HTTP_DNS, ///<Could not resolve name
42 HTTP_PRTCL, ///<Protocol error
43 HTTP_NOTFOUND, ///<HTTP 404 Error
44 HTTP_REFUSED, ///<HTTP 403 Error
45 HTTP_ERROR, ///<HTTP xxx error
46 HTTP_TIMEOUT, ///<Connection timeout
47 HTTP_CONN, ///<Connection error
48 HTTP_CLOSED, ///<Connection was closed by remote host
49 HTTP_OK = 0, ///<Success
52 /**A simple HTTP Client
53 The HTTPClient is composed of:
54 - The actual client (HTTPClient)
55 - 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)
60 ///Instantiate the HTTP client
64 #if 0 //TODO add header handlers
66 Provides a basic authentification feature (Base64 encoded username and password)
67 Pass two NULL pointers to switch back to no authentication
68 @param user username to use for authentication, must remain valid durlng the whole HTTP session
69 @param user password to use for authentication, must remain valid durlng the whole HTTP session
71 void basicAuth(const char* user, const char* password); //Basic Authentification
74 //High Level setup functions
75 /** Execute a GET request on the URL
76 Blocks until completion
77 @param url : url on which to execute the request
78 @param pDataIn : pointer to an IHTTPDataIn instance that will collect the data returned by the request, can be NULL
79 @param timeout waiting timeout in ms (osWaitForever for blocking function, not recommended)
80 @return 0 on success, HTTP error (<0) on failure
82 HTTPResult get(const char* url, IHTTPDataIn* pDataIn, int timeout = HTTP_CLIENT_DEFAULT_TIMEOUT); //Blocking
84 /** Execute a GET request on the URL
85 Blocks until completion
86 This is a helper to directly get a piece of text from a HTTP result
87 @param url : url on which to execute the request
88 @param result : pointer to a char array in which the result will be stored
89 @param maxResultLen : length of the char array (including space for the NULL-terminating char)
90 @param timeout waiting timeout in ms (osWaitForever for blocking function, not recommended)
91 @return 0 on success, HTTP error (<0) on failure
93 HTTPResult get(const char* url, char* result, size_t maxResultLen, int timeout = HTTP_CLIENT_DEFAULT_TIMEOUT); //Blocking
95 /** Execute a POST request on the URL
96 Blocks until completion
97 @param url : url on which to execute the request
98 @param dataOut : a IHTTPDataOut instance that contains the data that will be posted
99 @param pDataIn : pointer to an IHTTPDataIn instance that will collect the data returned by the request, can be NULL
100 @param timeout waiting timeout in ms (osWaitForever for blocking function, not recommended)
101 @return 0 on success, HTTP error (<0) on failure
103 HTTPResult post(const char* url, const IHTTPDataOut& dataOut, IHTTPDataIn* pDataIn, int timeout = HTTP_CLIENT_DEFAULT_TIMEOUT); //Blocking
105 /** Execute a PUT request on the URL
106 Blocks until completion
107 @param url : url on which to execute the request
108 @param dataOut : a IHTTPDataOut instance that contains the data that will be put
109 @param pDataIn : pointer to an IHTTPDataIn instance that will collect the data returned by the request, can be NULL
110 @param timeout waiting timeout in ms (osWaitForever for blocking function, not recommended)
111 @return 0 on success, HTTP error (<0) on failure
113 HTTPResult put(const char* url, const IHTTPDataOut& dataOut, IHTTPDataIn* pDataIn, int timeout = HTTP_CLIENT_DEFAULT_TIMEOUT); //Blocking
115 /** Execute a DELETE request on the URL
116 Blocks until completion
117 @param url : url on which to execute the request
118 @param pDataIn : pointer to an IHTTPDataIn instance that will collect the data returned by the request, can be NULL
119 @param timeout waiting timeout in ms (osWaitForever for blocking function, not recommended)
120 @return 0 on success, HTTP error (<0) on failure
122 HTTPResult del(const char* url, IHTTPDataIn* pDataIn, int timeout = HTTP_CLIENT_DEFAULT_TIMEOUT); //Blocking
124 /** Get last request's HTTP response code
125 @return The HTTP response code of the last request
127 int getHTTPResponseCode();
139 HTTPResult connect(const char* url, HTTP_METH method, IHTTPDataOut* pDataOut, IHTTPDataIn* pDataIn, int timeout); //Execute request
140 HTTPResult recv(char* buf, size_t minLen, size_t maxLen, size_t* pReadLen); //0 on success, err code on failure
141 HTTPResult send(char* buf, size_t len = 0); //0 on success, err code on failure
142 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
145 TCPSocketConnection m_sock;
149 const char* m_basicAuthUser;
150 const char* m_basicAuthPassword;
151 int m_httpResponseCode;
155 //Including data containers here for more convenience
156 #include "data/HTTPText.h"
157 #include "data/HTTPMap.h"