mikroSDK Reference Manual
http_client.h
Go to the documentation of this file.
1
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 < 256)
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//Application specific context
174#ifndef HTTP_CLIENT_PRIVATE_CONTEXT
175 #define HTTP_CLIENT_PRIVATE_CONTEXT
176#endif
177
178//TLS supported?
179#if (HTTP_CLIENT_TLS_SUPPORT == ENABLED)
180 #include "core/crypto.h"
181 #include "tls.h"
182#endif
183
184//Basic authentication supported?
185#if (HTTP_CLIENT_BASIC_AUTH_SUPPORT == ENABLED)
186 #include "core/crypto.h"
187 #include "encoding/base64.h"
188#endif
189
190//Digest access authentication supported?
191#if (HTTP_CLIENT_DIGEST_AUTH_SUPPORT == ENABLED)
192 #include "core/crypto.h"
193 #include "hash/hash_algorithms.h"
194#endif
195
196//Forward declaration of HttpClientContext structure
197struct _HttpClientContext;
198#define HttpClientContext struct _HttpClientContext
199
200//C++ guard
201#ifdef __cplusplus
202extern "C" {
203#endif
204
205
210typedef enum
211{
212 HTTP_CLIENT_STATE_DISCONNECTED = 0,
213 HTTP_CLIENT_STATE_CONNECTING = 1,
214 HTTP_CLIENT_STATE_CONNECTED = 2,
215 HTTP_CLIENT_STATE_DISCONNECTING = 3
217
218
219//TLS supported?
220#if (HTTP_CLIENT_TLS_SUPPORT == ENABLED)
221
226typedef error_t (*HttpClientTlsInitCallback)(HttpClientContext *context,
227 TlsContext *tlsContext);
228
229#endif
230
231
236typedef error_t (*HttpClientRandCallback)(uint8_t *data, size_t length);
237
238
243typedef struct
244{
246 char_t username[HTTP_CLIENT_MAX_USERNAME_LEN + 1];
247 char_t password[HTTP_CLIENT_MAX_PASSWORD_LEN + 1];
248 char_t realm[HTTP_CLIENT_MAX_REALM_LEN + 1];
249#if (HTTP_CLIENT_DIGEST_AUTH_SUPPORT == ENABLED)
251 const HashAlgo *algorithm;
252 uint32_t nc;
253 char_t nonce[HTTP_CLIENT_MAX_NONCE_LEN + 1];
254 char_t cnonce[HTTP_CLIENT_CNONCE_SIZE * 2 + 1];
255 char_t opaque[HTTP_CLIENT_MAX_OPAQUE_LEN + 1];
256 bool_t stale;
257#endif
259
260
266{
269 NetInterface *interface;
272 Socket *socket;
273#if (HTTP_CLIENT_TLS_SUPPORT == ENABLED)
274 TlsContext *tlsContext;
275 TlsSessionState tlsSession;
277#endif
279 uint16_t serverPort;
280#if (HTTP_CLIENT_AUTH_SUPPORT == ENABLED)
282#endif
283#if (HTTP_CLIENT_DIGEST_AUTH_SUPPORT == ENABLED)
285#endif
287 char_t method[HTTP_CLIENT_MAX_METHOD_LEN + 1];
288 bool_t keepAlive;
290 char_t buffer[HTTP_CLIENT_BUFFER_SIZE + 1];
291 size_t bufferLen;
292 size_t bufferPos;
293 size_t bodyLen;
294 size_t bodyPos;
295 uint_t statusCode;
296 HTTP_CLIENT_PRIVATE_CONTEXT
297};
298
299
300//HTTP client related functions
301error_t httpClientInit(HttpClientContext *context);
302
303#if (HTTP_CLIENT_TLS_SUPPORT == ENABLED)
304
305error_t httpClientRegisterTlsInitCallback(HttpClientContext *context,
307
308#endif
309
310error_t httpClientRegisterRandCallback(HttpClientContext *context,
311 HttpClientRandCallback callback);
312
313error_t httpClientSetVersion(HttpClientContext *context, HttpVersion version);
314error_t httpClientSetTimeout(HttpClientContext *context, systime_t timeout);
315
316error_t httpClientSetAuthInfo(HttpClientContext *context,
317 const char_t *username, const char_t *password);
318
319error_t httpClientBindToInterface(HttpClientContext *context,
320 NetInterface *interface);
321
322error_t httpClientConnect(HttpClientContext *context,
323 const IpAddr *serverIpAddr, uint16_t serverPort);
324
325error_t httpClientCreateRequest(HttpClientContext *context);
326error_t httpClientSetMethod(HttpClientContext *context, const char_t *method);
327error_t httpClientSetUri(HttpClientContext *context, const char_t *uri);
328
329error_t httpClientSetHost(HttpClientContext *context, const char_t *host,
330 uint16_t port);
331
332error_t httpClientSetQueryString(HttpClientContext *context,
333 const char_t *queryString);
334
335error_t httpClientAddQueryParam(HttpClientContext *context,
336 const char_t *name, const char_t *value);
337
338error_t httpClientAddHeaderField(HttpClientContext *context,
339 const char_t *name, const char_t *value);
340
341error_t httpClientFormatHeaderField(HttpClientContext *context,
342 const char_t *name, const char_t *format, ...);
343
344error_t httpClientSetContentLength(HttpClientContext *context, size_t length);
345error_t httpClientWriteHeader(HttpClientContext *context);
346
347error_t httpClientWriteBody(HttpClientContext *context, const void *data,
348 size_t length, size_t *written, uint_t flags);
349
350error_t httpClientWriteTrailer(HttpClientContext *context);
351
352error_t httpClientReadHeader(HttpClientContext *context);
353uint_t httpClientGetStatus(HttpClientContext *context);
354
355const char_t *httpClientGetHeaderField(HttpClientContext *context,
356 const char_t *name);
357
358error_t httpClientGetNextHeaderField(HttpClientContext *context,
359 const char_t **name, const char_t **value);
360
361error_t httpClientReadBody(HttpClientContext *context, void *data,
362 size_t size, size_t *received, uint_t flags);
363
364error_t httpClientReadTrailer(HttpClientContext *context);
365error_t httpClientCloseBody(HttpClientContext *context);
366
367error_t httpClientDisconnect(HttpClientContext *context);
368error_t httpClientClose(HttpClientContext *context);
369
370void httpClientDeinit(HttpClientContext *context);
371
372//C++ guard
373#ifdef __cplusplus
374}
375#endif
376
377#endif
error_t
Error codes.
Definition error.h:43
error_t(* HttpClientRandCallback)(uint8_t *data, size_t length)
Random data generation callback function.
Definition http_client.h:236
HttpClientState
HTTP client states.
Definition http_client.h:211
error_t(* HttpClientTlsInitCallback)(HttpClientContext *context, TlsContext *tlsContext)
TLS initialization callback function.
Definition http_client.h:226
Definitions common to HTTP client and server.
HttpRequestState
HTTP request states.
Definition http_common.h:110
HttpVersion
HTTP version numbers.
Definition http_common.h:60
HttpAuthMode
HTTP authentication schemes.
Definition http_common.h:72
HttpAuthQop
Quality of protection (digest authentication)
Definition http_common.h:84
TCP/IP stack core.
uint32_t systime_t
System time.
Definition os_port_none.h:90
HTTP authentication parameters.
Definition http_client.h:244
const HashAlgo * algorithm
Digest algorithm.
Definition http_client.h:251
HttpAuthQop qop
Quality of protection.
Definition http_client.h:250
HttpAuthMode mode
HTTP authentication mode.
Definition http_client.h:245
bool_t stale
Stale flag.
Definition http_client.h:256
uint32_t nc
Nonce count.
Definition http_client.h:252
IP network address.
Definition ip.h:72
HTTP client context.
Definition http_client.h:266
uint16_t serverPort
TCP port number.
Definition http_client.h:279
size_t bufferLen
Length of the buffer, in bytes.
Definition http_client.h:291
char_t method[HTTP_CLIENT_MAX_METHOD_LEN+1]
HTTP request method.
Definition http_client.h:287
IpAddr serverIpAddr
IP address of the HTTP server.
Definition http_client.h:278
char_t buffer[HTTP_CLIENT_BUFFER_SIZE+1]
Memory buffer for input/output operations.
Definition http_client.h:290
size_t bodyLen
Length of the body, in bytes.
Definition http_client.h:293
bool_t keepAlive
HTTP persistent connection.
Definition http_client.h:288
NetInterface * interface
Underlying network interface.
Definition http_client.h:269
size_t bodyPos
Current position in the body.
Definition http_client.h:294
Socket * socket
Underlying socket.
Definition http_client.h:272
HttpRequestState requestState
HTTP request state.
Definition http_client.h:286
HttpVersion version
HTTP protocol version.
Definition http_client.h:268
systime_t timeout
Timeout value.
Definition http_client.h:270
uint_t statusCode
HTTP status code.
Definition http_client.h:295
bool_t chunkedEncoding
Chunked transfer encoding.
Definition http_client.h:289
HttpClientRandCallback randCallback
Random data generation callback function.
Definition http_client.h:284
systime_t timestamp
Timestamp to manage timeout.
Definition http_client.h:271
HttpClientAuthParams authParams
HTTP authentication parameters.
Definition http_client.h:281
HttpClientState state
HTTP client state.
Definition http_client.h:267
size_t bufferPos
Current position in the buffer.
Definition http_client.h:292
TlsSessionState tlsSession
TLS session state.
Definition http_client.h:275
HttpClientTlsInitCallback tlsInitCallback
TLS initialization callback function.
Definition http_client.h:276
TlsContext * tlsContext
TLS context.
Definition http_client.h:274