]> git.gir.st - tmk_keyboard.git/blob - tmk_core/tool/mbed/mbed-sdk/libraries/tests/net/cellular/http/common/HTTPClient/HTTPClient.h
Merge commit '657d9f23fe47fb88cf221adb23095082f191ba6a'
[tmk_keyboard.git] / tmk_core / tool / mbed / mbed-sdk / libraries / tests / net / cellular / http / common / HTTPClient / HTTPClient.h
1 /* HTTPClient.h */
2 /* Copyright (C) 2012 mbed.org, MIT License
3 *
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:
9 *
10 * The above copyright notice and this permission notice shall be included in all copies or
11 * substantial portions of the Software.
12 *
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.
18 */
19
20 /** \file
21 HTTP Client header file
22 */
23
24 #ifndef HTTP_CLIENT_H
25 #define HTTP_CLIENT_H
26
27 #include "TCPSocketConnection.h"
28
29 #define HTTP_CLIENT_DEFAULT_TIMEOUT 15000
30
31 class HTTPData;
32
33 #include "IHTTPData.h"
34 #include "mbed.h"
35
36 ///HTTP client results
37 enum HTTPResult
38 {
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
50 };
51
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)
56 */
57 class HTTPClient
58 {
59 public:
60 ///Instantiate the HTTP client
61 HTTPClient();
62 ~HTTPClient();
63
64 #if 0 //TODO add header handlers
65 /**
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
70 */
71 void basicAuth(const char* user, const char* password); //Basic Authentification
72 #endif
73
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
81 */
82 HTTPResult get(const char* url, IHTTPDataIn* pDataIn, int timeout = HTTP_CLIENT_DEFAULT_TIMEOUT); //Blocking
83
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
92 */
93 HTTPResult get(const char* url, char* result, size_t maxResultLen, int timeout = HTTP_CLIENT_DEFAULT_TIMEOUT); //Blocking
94
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
102 */
103 HTTPResult post(const char* url, const IHTTPDataOut& dataOut, IHTTPDataIn* pDataIn, int timeout = HTTP_CLIENT_DEFAULT_TIMEOUT); //Blocking
104
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
112 */
113 HTTPResult put(const char* url, const IHTTPDataOut& dataOut, IHTTPDataIn* pDataIn, int timeout = HTTP_CLIENT_DEFAULT_TIMEOUT); //Blocking
114
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
121 */
122 HTTPResult del(const char* url, IHTTPDataIn* pDataIn, int timeout = HTTP_CLIENT_DEFAULT_TIMEOUT); //Blocking
123
124 /** Get last request's HTTP response code
125 @return The HTTP response code of the last request
126 */
127 int getHTTPResponseCode();
128
129 private:
130 enum HTTP_METH
131 {
132 HTTP_GET,
133 HTTP_POST,
134 HTTP_PUT,
135 HTTP_DELETE,
136 HTTP_HEAD
137 };
138
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
143
144 //Parameters
145 TCPSocketConnection m_sock;
146
147 int m_timeout;
148
149 const char* m_basicAuthUser;
150 const char* m_basicAuthPassword;
151 int m_httpResponseCode;
152
153 };
154
155 //Including data containers here for more convenience
156 #include "data/HTTPText.h"
157 #include "data/HTTPMap.h"
158
159 #endif
Imprint / Impressum