http_client.h
Go to the documentation of this file.
1 /**
2  * @file http_client.h
3  * @brief HTTP client (HyperText Transfer Protocol)
4  *
5  * @section License
6  *
7  * SPDX-License-Identifier: GPL-2.0-or-later
8  *
9  * Copyright (C) 2010-2019 Oryx Embedded SARL. All rights reserved.
10  *
11  * This file is part of CycloneTCP Open.
12  *
13  * This program is free software; you can redistribute it and/or
14  * modify it under the terms of the GNU General Public License
15  * as published by the Free Software Foundation; either version 2
16  * of the License, or (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, write to the Free Software Foundation,
25  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
26  *
27  * @author Oryx Embedded SARL (www.oryx-embedded.com)
28  * @version 1.9.4
29  **/
30 
31 #ifndef _HTTP_CLIENT_H
32 #define _HTTP_CLIENT_H
33 
34 //Dependencies
35 #include "core/net.h"
36 #include "http/http_common.h"
37 
38 //HTTP client support
39 #ifndef HTTP_CLIENT_SUPPORT
40  #define HTTP_CLIENT_SUPPORT ENABLED
41 #elif (HTTP_CLIENT_SUPPORT != ENABLED && HTTP_CLIENT_SUPPORT != DISABLED)
42  #error HTTP_CLIENT_SUPPORT parameter is not valid
43 #endif
44 
45 //HTTP over TLS
46 #ifndef HTTP_CLIENT_TLS_SUPPORT
47  #define HTTP_CLIENT_TLS_SUPPORT DISABLED
48 #elif (HTTP_CLIENT_TLS_SUPPORT != ENABLED && HTTP_CLIENT_TLS_SUPPORT != DISABLED)
49  #error HTTP_CLIENT_TLS_SUPPORT parameter is not valid
50 #endif
51 
52 //Basic access authentication support
53 #ifndef HTTP_CLIENT_BASIC_AUTH_SUPPORT
54  #define HTTP_CLIENT_BASIC_AUTH_SUPPORT DISABLED
55 #elif (HTTP_CLIENT_BASIC_AUTH_SUPPORT != ENABLED && HTTP_CLIENT_BASIC_AUTH_SUPPORT != DISABLED)
56  #error HTTP_CLIENT_BASIC_AUTH_SUPPORT parameter is not valid
57 #endif
58 
59 //Digest access authentication support
60 #ifndef HTTP_CLIENT_DIGEST_AUTH_SUPPORT
61  #define HTTP_CLIENT_DIGEST_AUTH_SUPPORT DISABLED
62 #elif (HTTP_CLIENT_DIGEST_AUTH_SUPPORT != ENABLED && HTTP_CLIENT_DIGEST_AUTH_SUPPORT != DISABLED)
63  #error HTTP_CLIENT_DIGEST_AUTH_SUPPORT parameter is not valid
64 #endif
65 
66 //MD5 digest support
67 #ifndef HTTP_CLIENT_MD5_SUPPORT
68  #define HTTP_CLIENT_MD5_SUPPORT ENABLED
69 #elif (HTTP_CLIENT_MD5_SUPPORT != ENABLED && HTTP_CLIENT_MD5_SUPPORT != DISABLED)
70  #error HTTP_CLIENT_MD5_SUPPORT parameter is not valid
71 #endif
72 
73 //SHA-256 digest support
74 #ifndef HTTP_CLIENT_SHA256_SUPPORT
75  #define HTTP_CLIENT_SHA256_SUPPORT DISABLED
76 #elif (HTTP_CLIENT_SHA256_SUPPORT != ENABLED && HTTP_CLIENT_SHA256_SUPPORT != DISABLED)
77  #error HTTP_CLIENT_SHA256_SUPPORT parameter is not valid
78 #endif
79 
80 //SHA-512/256 digest support
81 #ifndef HTTP_CLIENT_SHA512_256_SUPPORT
82  #define HTTP_CLIENT_SHA512_256_SUPPORT DISABLED
83 #elif (HTTP_CLIENT_SHA512_256_SUPPORT != ENABLED && HTTP_CLIENT_SHA512_256_SUPPORT != DISABLED)
84  #error HTTP_CLIENT_SHA512_256_SUPPORT parameter is not valid
85 #endif
86 
87 //Default timeout
88 #ifndef HTTP_CLIENT_DEFAULT_TIMEOUT
89  #define HTTP_CLIENT_DEFAULT_TIMEOUT 20000
90 #elif (HTTP_CLIENT_DEFAULT_TIMEOUT < 1000)
91  #error HTTP_CLIENT_DEFAULT_TIMEOUT parameter is not valid
92 #endif
93 
94 //Size of the buffer for input/output operations
95 #ifndef HTTP_CLIENT_BUFFER_SIZE
96  #define HTTP_CLIENT_BUFFER_SIZE 2048
97 #elif (HTTP_CLIENT_BUFFER_SIZE < 64)
98  #error HTTP_CLIENT_BUFFER_SIZE parameter is not valid
99 #endif
100 
101 //TX buffer size for TLS connections
102 #ifndef HTTP_CLIENT_TLS_TX_BUFFER_SIZE
103  #define HTTP_CLIENT_TLS_TX_BUFFER_SIZE 2048
104 #elif (HTTP_CLIENT_TLS_TX_BUFFER_SIZE < 512)
105  #error HTTP_CLIENT_TLS_TX_BUFFER_SIZE parameter is not valid
106 #endif
107 
108 //RX buffer size for TLS connections
109 #ifndef HTTP_CLIENT_TLS_RX_BUFFER_SIZE
110  #define HTTP_CLIENT_TLS_RX_BUFFER_SIZE 16384
111 #elif (HTTP_CLIENT_TLS_RX_BUFFER_SIZE < 512)
112  #error HTTP_CLIENT_TLS_RX_BUFFER_SIZE parameter is not valid
113 #endif
114 
115 //Maximum length of HTTP method
116 #ifndef HTTP_CLIENT_MAX_METHOD_LEN
117  #define HTTP_CLIENT_MAX_METHOD_LEN 8
118 #elif (HTTP_CLIENT_MAX_METHOD_LEN < 1)
119  #error HTTP_CLIENT_MAX_METHOD_LEN parameter is not valid
120 #endif
121 
122 //Maximum length of the user name
123 #ifndef HTTP_CLIENT_MAX_USERNAME_LEN
124  #define HTTP_CLIENT_MAX_USERNAME_LEN 32
125 #elif (HTTP_CLIENT_MAX_USERNAME_LEN < 0)
126  #error HTTP_CLIENT_MAX_USERNAME_LEN parameter is not valid
127 #endif
128 
129 //Maximum length of the password
130 #ifndef HTTP_CLIENT_MAX_PASSWORD_LEN
131  #define HTTP_CLIENT_MAX_PASSWORD_LEN 32
132 #elif (HTTP_CLIENT_MAX_PASSWORD_LEN < 0)
133  #error HTTP_CLIENT_MAX_PASSWORD_LEN parameter is not valid
134 #endif
135 
136 //Maximum length of the realm
137 #ifndef HTTP_CLIENT_MAX_REALM_LEN
138  #define HTTP_CLIENT_MAX_REALM_LEN 32
139 #elif (HTTP_CLIENT_MAX_REALM_LEN < 1)
140  #error HTTP_CLIENT_MAX_REALM_LEN parameter is not valid
141 #endif
142 
143 //Maximum length of the nonce
144 #ifndef HTTP_CLIENT_MAX_NONCE_LEN
145  #define HTTP_CLIENT_MAX_NONCE_LEN 64
146 #elif (HTTP_CLIENT_MAX_NONCE_LEN < 1)
147  #error HTTP_CLIENT_MAX_NONCE_LEN parameter is not valid
148 #endif
149 
150 //Cnonce size
151 #ifndef HTTP_CLIENT_CNONCE_SIZE
152  #define HTTP_CLIENT_CNONCE_SIZE 16
153 #elif (HTTP_CLIENT_CNONCE_SIZE < 1)
154  #error HTTP_CLIENT_CNONCE_SIZE parameter is not valid
155 #endif
156 
157 //Maximum length of the opaque parameter
158 #ifndef HTTP_CLIENT_MAX_OPAQUE_LEN
159  #define HTTP_CLIENT_MAX_OPAQUE_LEN 64
160 #elif (HTTP_CLIENT_MAX_OPAQUE_LEN < 1)
161  #error HTTP_CLIENT_MAX_OPAQUE_LEN parameter is not valid
162 #endif
163 
164 //HTTP authentication supported?
165 #if (HTTP_CLIENT_BASIC_AUTH_SUPPORT == ENABLED)
166  #define HTTP_CLIENT_AUTH_SUPPORT ENABLED
167 #elif (HTTP_CLIENT_DIGEST_AUTH_SUPPORT == ENABLED)
168  #define HTTP_CLIENT_AUTH_SUPPORT ENABLED
169 #else
170  #define HTTP_CLIENT_AUTH_SUPPORT DISABLED
171 #endif
172 
173 //TLS supported?
174 #if (HTTP_CLIENT_TLS_SUPPORT == ENABLED)
175  #include "core/crypto.h"
176  #include "tls.h"
177 #endif
178 
179 //Basic authentication supported?
180 #if (HTTP_CLIENT_BASIC_AUTH_SUPPORT == ENABLED)
181  #include "core/crypto.h"
182  #include "encoding/base64.h"
183 #endif
184 
185 //MD5 digest authentication supported?
186 #if (HTTP_CLIENT_DIGEST_AUTH_SUPPORT == ENABLED && HTTP_CLIENT_MD5_SUPPORT == ENABLED)
187  #include "core/crypto.h"
188  #include "hash/md5.h"
189 #endif
190 
191 //SHA-256 digest authentication supported?
192 #if (HTTP_CLIENT_DIGEST_AUTH_SUPPORT == ENABLED && HTTP_CLIENT_SHA256_SUPPORT == ENABLED)
193  #include "core/crypto.h"
194  #include "hash/sha256.h"
195 #endif
196 
197 //SHA-512/256 digest authentication supported?
198 #if (HTTP_CLIENT_DIGEST_AUTH_SUPPORT == ENABLED && HTTP_CLIENT_SHA512_256_SUPPORT == ENABLED)
199  #include "core/crypto.h"
200  #include "hash/sha512_256.h"
201 #endif
202 
203 //Forward declaration of HttpClientContext structure
204 struct _HttpClientContext;
205 #define HttpClientContext struct _HttpClientContext
206 
207 //C++ guard
208 #ifdef __cplusplus
209  extern "C" {
210 #endif
211 
212 
213 /**
214  * @brief HTTP client states
215  */
216 
217 typedef enum
218 {
224 
225 
226 //TLS supported?
227 #if (HTTP_CLIENT_TLS_SUPPORT == ENABLED)
228 
229 /**
230  * @brief TLS initialization callback function
231  **/
232 
235 
236 #endif
237 
238 
239 /**
240  * @brief Random data generation callback function
241  **/
242 
243 typedef error_t (*HttpClientRandCallback)(uint8_t *data, size_t length);
244 
245 
246 /**
247  * @brief HTTP authentication parameters
248  **/
249 
250 typedef struct
251 {
252  HttpAuthMode mode; ///<HTTP authentication mode
253  char_t username[HTTP_CLIENT_MAX_USERNAME_LEN + 1]; ///<User name
254  char_t password[HTTP_CLIENT_MAX_PASSWORD_LEN + 1]; ///<Password
255  char_t realm[HTTP_CLIENT_MAX_REALM_LEN + 1]; ///<Realm
256 #if (HTTP_CLIENT_DIGEST_AUTH_SUPPORT == ENABLED)
257  HttpAuthQop qop; ///<Quality of protection
258  const HashAlgo *algorithm; ///<Digest algorithm
259  uint32_t nc; ///<Nonce count
260  char_t nonce[HTTP_CLIENT_MAX_NONCE_LEN + 1]; ///<Nonce value
261  char_t cnonce[HTTP_CLIENT_CNONCE_SIZE * 2 + 1]; ///<Cnonce value
262  char_t opaque[HTTP_CLIENT_MAX_OPAQUE_LEN + 1]; ///<Opaque parameter
263  bool_t stale; ///<Stale flag
264 #endif
266 
267 
268 /**
269  * @brief HTTP client context
270  **/
271 
273 {
274  HttpClientState state; ///<HTTP client state
275  HttpVersion version; ///<HTTP protocol version
276  NetInterface *interface; ///<Underlying network interface
277  systime_t timeout; ///<Timeout value
278  systime_t timestamp; ///<Timestamp to manage timeout
279  Socket *socket; ///<Underlying socket
280 #if (HTTP_CLIENT_TLS_SUPPORT == ENABLED)
281  TlsContext *tlsContext; ///<TLS context
282  TlsSessionState tlsSession; ///<TLS session state
283  HttpClientTlsInitCallback tlsInitCallback; ///<TLS initialization callback function
284 #endif
285  IpAddr serverIpAddr; ///<IP address of the HTTP server
286  uint16_t serverPort; ///<TCP port number
287 #if (HTTP_CLIENT_AUTH_SUPPORT == ENABLED)
288  HttpClientAuthParams authParams; ///<HTTP authentication parameters
289 #endif
290 #if (HTTP_CLIENT_DIGEST_AUTH_SUPPORT == ENABLED)
291  HttpClientRandCallback randCallback; ///<Random data generation callback function
292 #endif
293  HttpRequestState requestState; ///<HTTP request state
294  char_t method[HTTP_CLIENT_MAX_METHOD_LEN + 1]; ///<HTTP request method
295  bool_t keepAlive; ///<HTTP persistent connection
296  bool_t chunkedEncoding; ///<Chunked transfer encoding
297  char_t buffer[HTTP_CLIENT_BUFFER_SIZE + 1]; ///<Memory buffer for input/output operations
298  size_t bufferLen; ///<Length of the buffer, in bytes
299  size_t bufferPos; ///<Current position in the buffer
300  size_t bodyLen; ///<Length of the body, in bytes
301  size_t bodyPos; ///<Current position in the body
302  uint_t statusCode; ///<HTTP status code
303 };
304 
305 
306 //HTTP related functions
308 
309 #if (HTTP_CLIENT_TLS_SUPPORT == ENABLED)
310 
312  HttpClientTlsInitCallback callback);
313 
314 #endif
315 
317  HttpClientRandCallback callback);
318 
321 
323  const char_t *username, const char_t *password);
324 
326  NetInterface *interface);
327 
329  const IpAddr *serverIpAddr, uint16_t serverPort);
330 
332 error_t httpClientSetMethod(HttpClientContext *context, const char_t *method);
333 error_t httpClientSetUri(HttpClientContext *context, const char_t *uri);
334 
335 error_t httpClientSetHost(HttpClientContext *context, const char_t *host,
336  uint16_t port);
337 
339  const char_t *queryString);
340 
342  const char_t *name, const char_t *value);
343 
345  const char_t *name, const char_t *value);
346 
349 
350 error_t httpClientWriteBody(HttpClientContext *context, const void *data,
351  size_t length, size_t *written, uint_t flags);
352 
354 
357 
359  const char_t *name);
360 
362  const char_t **name, const char_t **value);
363 
365  size_t size, size_t *received, uint_t flags);
366 
369 
372 
373 void httpClientDeinit(HttpClientContext *context);
374 
375 //C++ guard
376 #ifdef __cplusplus
377  }
378 #endif
379 
380 #endif
TLS (Transport Layer Security)
HttpClientRandCallback randCallback
Random data generation callback function.
Definition: http_client.h:291
HttpAuthMode mode
HTTP authentication mode.
Definition: http_client.h:252
uint16_t version
Definition: dtls_misc.h:165
uint32_t systime_t
Definition: compiler_port.h:46
char char_t
Definition: compiler_port.h:43
uint8_t flags
Definition: tcp.h:314
HttpClientTlsInitCallback tlsInitCallback
TLS initialization callback function.
Definition: http_client.h:283
size_t bufferLen
Length of the buffer, in bytes.
Definition: http_client.h:298
error_t httpClientReadHeader(HttpClientContext *context)
Read HTTP response header.
Definition: http_client.c:1273
#define HTTP_CLIENT_BUFFER_SIZE
Definition: http_client.h:96
HTTP authentication parameters.
Definition: http_client.h:250
TCP/IP stack core.
#define HttpClientContext
Definition: http_client.h:205
#define HTTP_CLIENT_CNONCE_SIZE
Definition: http_client.h:152
HttpRequestState requestState
HTTP request state.
Definition: http_client.h:293
#define HTTP_CLIENT_MAX_OPAQUE_LEN
Definition: http_client.h:159
error_t httpClientReadBody(HttpClientContext *context, void *data, size_t size, size_t *received, uint_t flags)
Read HTTP response body.
Definition: http_client.c:1544
Definitions common to HTTP client and server.
HTTP client context.
Definition: http_client.h:272
Socket * socket
Underlying socket.
Definition: http_client.h:279
General definitions for cryptographic algorithms.
HttpClientAuthParams authParams
HTTP authentication parameters.
Definition: http_client.h:288
IpAddr serverIpAddr
IP address of the HTTP server.
Definition: http_client.h:285
HttpClientState state
HTTP client state.
Definition: http_client.h:274
IP network address.
Definition: ip.h:71
#define HTTP_CLIENT_MAX_NONCE_LEN
Definition: http_client.h:145
HttpAuthQop qop
Quality of protection.
Definition: http_client.h:257
HttpRequestState
HTTP request states.
Definition: http_common.h:109
HttpAuthMode
HTTP authentication schemes.
Definition: http_common.h:71
TLS session state.
Definition: tls.h:1830
bool_t keepAlive
HTTP persistent connection.
Definition: http_client.h:295
HttpAuthQop
Quality of protection (digest authentication)
Definition: http_common.h:83
error_t httpClientBindToInterface(HttpClientContext *context, NetInterface *interface)
Bind the HTTP client to a particular network interface.
Definition: http_client.c:246
error_t(* HttpClientRandCallback)(uint8_t *data, size_t length)
Random data generation callback function.
Definition: http_client.h:243
uint16_t serverPort
TCP port number.
Definition: http_client.h:286
#define Socket
Definition: socket.h:36
bool_t stale
Stale flag.
Definition: http_client.h:263
char_t buffer[HTTP_CLIENT_BUFFER_SIZE+1]
Memory buffer for input/output operations.
Definition: http_client.h:297
error_t httpClientSetHost(HttpClientContext *context, const char_t *host, uint16_t port)
Set the hostname and port number of the resource being requested.
Definition: http_client.c:533
TlsContext * tlsContext
TLS context.
Definition: http_client.h:281
#define HTTP_CLIENT_MAX_METHOD_LEN
Definition: http_client.h:117
uint32_t nc
Nonce count.
Definition: http_client.h:259
#define HTTP_CLIENT_MAX_USERNAME_LEN
Definition: http_client.h:124
error_t httpClientWriteBody(HttpClientContext *context, const void *data, size_t length, size_t *written, uint_t flags)
Write HTTP request body.
Definition: http_client.c:1038
char_t name[]
error_t httpClientRegisterTlsInitCallback(HttpClientContext *context, HttpClientTlsInitCallback callback)
Register TLS initialization callback function.
Definition: http_client.c:110
error_t(* HttpClientTlsInitCallback)(HttpClientContext *context, TlsContext *tlsContext)
TLS initialization callback function.
Definition: http_client.h:233
TlsSessionState tlsSession
TLS session state.
Definition: http_client.h:282
error_t httpClientClose(HttpClientContext *context)
Close the connection with the HTTP server.
Definition: http_client.c:2122
error_t httpClientSetVersion(HttpClientContext *context, HttpVersion version)
Set the HTTP protocol version to be used.
Definition: http_client.c:161
void httpClientDeinit(HttpClientContext *context)
Release HTTP client context.
Definition: http_client.c:2143
error_t httpClientSetMethod(HttpClientContext *context, const char_t *method)
Set HTTP request method.
Definition: http_client.c:402
error_t httpClientSetContentLength(HttpClientContext *context, size_t length)
Set the length of the HTTP request body.
Definition: http_client.c:888
bool_t chunkedEncoding
Chunked transfer encoding.
Definition: http_client.h:296
error_t httpClientReadTrailer(HttpClientContext *context)
Read HTTP trailer.
Definition: http_client.c:1782
size_t bufferPos
Current position in the buffer.
Definition: http_client.h:299
error_t httpClientSetUri(HttpClientContext *context, const char_t *uri)
Set request URI.
Definition: http_client.c:462
HttpClientState
HTTP client states.
Definition: http_client.h:217
error_t httpClientConnect(HttpClientContext *context, const IpAddr *serverIpAddr, uint16_t serverPort)
Establish a connection with the specified HTTP server.
Definition: http_client.c:269
error_t
Error codes.
Definition: error.h:42
Base64 encoding scheme.
unsigned int uint_t
Definition: compiler_port.h:45
error_t httpClientSetQueryString(HttpClientContext *context, const char_t *queryString)
Set query string.
Definition: http_client.c:594
uint_t statusCode
HTTP status code.
Definition: http_client.h:302
systime_t timestamp
Timestamp to manage timeout.
Definition: http_client.h:278
uint8_t data[]
Definition: dtls_misc.h:169
size_t bodyPos
Current position in the body.
Definition: http_client.h:301
error_t httpClientCloseBody(HttpClientContext *context)
Close HTTP request or response body.
Definition: http_client.c:1910
#define NetInterface
Definition: net.h:36
HttpVersion version
HTTP protocol version.
Definition: http_client.h:275
HttpVersion
HTTP version numbers.
Definition: http_common.h:59
const char_t * httpClientGetHeaderField(HttpClientContext *context, const char_t *name)
Retrieve the value of the specified header field name.
Definition: http_client.c:1437
char_t method[HTTP_CLIENT_MAX_METHOD_LEN+1]
HTTP request method.
Definition: http_client.h:294
#define HTTP_CLIENT_MAX_PASSWORD_LEN
Definition: http_client.h:131
uint8_t value[]
Definition: dtls_misc.h:143
error_t httpClientInit(HttpClientContext *context)
Initialize HTTP client context.
Definition: http_client.c:65
error_t httpClientGetNextHeaderField(HttpClientContext *context, const char_t **name, const char_t **value)
Iterate through the HTTP response header.
Definition: http_client.c:1496
SHA-512/256 (Secure Hash Algorithm)
error_t httpClientWriteHeader(HttpClientContext *context)
Write HTTP request header.
Definition: http_client.c:915
#define HTTP_CLIENT_MAX_REALM_LEN
Definition: http_client.h:138
uint16_t port
Definition: dns_common.h:223
error_t httpClientRegisterRandCallback(HttpClientContext *context, HttpClientRandCallback callback)
Register random data generation callback function.
Definition: http_client.c:134
uint_t httpClientGetStatus(HttpClientContext *context)
Retrieve the HTTP status code of the response.
Definition: http_client.c:1410
error_t httpClientSetAuthInfo(HttpClientContext *context, const char_t *username, const char_t *password)
Set authentication information.
Definition: http_client.c:208
systime_t timeout
Timeout value.
Definition: http_client.h:277
const HashAlgo * algorithm
Digest algorithm.
Definition: http_client.h:258
error_t httpClientAddQueryParam(HttpClientContext *context, const char_t *name, const char_t *value)
Add a key/value pair to the query string.
Definition: http_client.c:691
error_t httpClientDisconnect(HttpClientContext *context)
Gracefully disconnect from the HTTP server.
Definition: http_client.c:2047
Common interface for hash algorithms.
Definition: crypto.h:1070
uint8_t length
Definition: dtls_misc.h:142
MD5 (Message-Digest Algorithm)
size_t bodyLen
Length of the body, in bytes.
Definition: http_client.h:300
NetInterface * interface
Underlying network interface.
Definition: http_client.h:276
SHA-256 (Secure Hash Algorithm 256)
error_t httpClientCreateRequest(HttpClientContext *context)
Create a new HTTP request.
Definition: http_client.c:365
#define TlsContext
Definition: tls.h:36
int bool_t
Definition: compiler_port.h:49
error_t httpClientAddHeaderField(HttpClientContext *context, const char_t *name, const char_t *value)
Add a header field to the HTTP request.
Definition: http_client.c:808
error_t httpClientWriteTrailer(HttpClientContext *context)
Write HTTP trailer.
Definition: http_client.c:1194
error_t httpClientSetTimeout(HttpClientContext *context, systime_t timeout)
Set communication timeout.
Definition: http_client.c:186