http_server.h
Go to the documentation of this file.
1 /**
2  * @file http_server.h
3  * @brief HTTP server (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.6
29  **/
30 
31 #ifndef _HTTP_SERVER_H
32 #define _HTTP_SERVER_H
33 
34 //Dependencies
35 #include "os_port.h"
36 #include "core/socket.h"
37 #include "web_socket/web_socket.h"
38 #include "http/http_common.h"
39 
40 //HTTP server support
41 #ifndef HTTP_SERVER_SUPPORT
42  #define HTTP_SERVER_SUPPORT ENABLED
43 #elif (HTTP_SERVER_SUPPORT != ENABLED && HTTP_SERVER_SUPPORT != DISABLED)
44  #error HTTP_SERVER_SUPPORT parameter is not valid
45 #endif
46 
47 //Support for persistent connections
48 #ifndef HTTP_SERVER_PERSISTENT_CONN_SUPPORT
49  #define HTTP_SERVER_PERSISTENT_CONN_SUPPORT DISABLED
50 #elif (HTTP_SERVER_PERSISTENT_CONN_SUPPORT != ENABLED && HTTP_SERVER_PERSISTENT_CONN_SUPPORT != DISABLED)
51  #error HTTP_SERVER_PERSISTENT_CONN_SUPPORT parameter is not valid
52 #endif
53 
54 //File system support
55 #ifndef HTTP_SERVER_FS_SUPPORT
56  #define HTTP_SERVER_FS_SUPPORT DISABLED
57 #elif (HTTP_SERVER_FS_SUPPORT != ENABLED && HTTP_SERVER_FS_SUPPORT != DISABLED)
58  #error HTTP_SERVER_FS_SUPPORT parameter is not valid
59 #endif
60 
61 //Server Side Includes support
62 #ifndef HTTP_SERVER_SSI_SUPPORT
63  #define HTTP_SERVER_SSI_SUPPORT DISABLED
64 #elif (HTTP_SERVER_SSI_SUPPORT != ENABLED && HTTP_SERVER_SSI_SUPPORT != DISABLED)
65  #error HTTP_SERVER_SSI_SUPPORT parameter is not valid
66 #endif
67 
68 //HTTP over TLS
69 #ifndef HTTP_SERVER_TLS_SUPPORT
70  #define HTTP_SERVER_TLS_SUPPORT DISABLED
71 #elif (HTTP_SERVER_TLS_SUPPORT != ENABLED && HTTP_SERVER_TLS_SUPPORT != DISABLED)
72  #error HTTP_SERVER_TLS_SUPPORT parameter is not valid
73 #endif
74 
75 //Basic access authentication support
76 #ifndef HTTP_SERVER_BASIC_AUTH_SUPPORT
77  #define HTTP_SERVER_BASIC_AUTH_SUPPORT DISABLED
78 #elif (HTTP_SERVER_BASIC_AUTH_SUPPORT != ENABLED && HTTP_SERVER_BASIC_AUTH_SUPPORT != DISABLED)
79  #error HTTP_SERVER_BASIC_AUTH_SUPPORT parameter is not valid
80 #endif
81 
82 //Digest access authentication support
83 #ifndef HTTP_SERVER_DIGEST_AUTH_SUPPORT
84  #define HTTP_SERVER_DIGEST_AUTH_SUPPORT DISABLED
85 #elif (HTTP_SERVER_DIGEST_AUTH_SUPPORT != ENABLED && HTTP_SERVER_DIGEST_AUTH_SUPPORT != DISABLED)
86  #error HTTP_SERVER_DIGEST_AUTH_SUPPORT parameter is not valid
87 #endif
88 
89 //WebSocket support
90 #ifndef HTTP_SERVER_WEB_SOCKET_SUPPORT
91  #define HTTP_SERVER_WEB_SOCKET_SUPPORT DISABLED
92 #elif (HTTP_SERVER_WEB_SOCKET_SUPPORT != ENABLED && HTTP_SERVER_WEB_SOCKET_SUPPORT != DISABLED)
93  #error HTTP_SERVER_WEB_SOCKET_SUPPORT parameter is not valid
94 #endif
95 
96 //Gzip content type support
97 #ifndef HTTP_SERVER_GZIP_TYPE_SUPPORT
98  #define HTTP_SERVER_GZIP_TYPE_SUPPORT DISABLED
99 #elif (HTTP_SERVER_GZIP_TYPE_SUPPORT != ENABLED && HTTP_SERVER_GZIP_TYPE_SUPPORT != DISABLED)
100  #error HTTP_SERVER_GZIP_TYPE_SUPPORT parameter is not valid
101 #endif
102 
103 //Multipart content type support
104 #ifndef HTTP_SERVER_MULTIPART_TYPE_SUPPORT
105  #define HTTP_SERVER_MULTIPART_TYPE_SUPPORT DISABLED
106 #elif (HTTP_SERVER_MULTIPART_TYPE_SUPPORT != ENABLED && HTTP_SERVER_MULTIPART_TYPE_SUPPORT != DISABLED)
107  #error HTTP_SERVER_MULTIPART_TYPE_SUPPORT parameter is not valid
108 #endif
109 
110 //Cookie support
111 #ifndef HTTP_SERVER_COOKIE_SUPPORT
112  #define HTTP_SERVER_COOKIE_SUPPORT DISABLED
113 #elif (HTTP_SERVER_COOKIE_SUPPORT != ENABLED && HTTP_SERVER_COOKIE_SUPPORT != DISABLED)
114  #error HTTP_SERVER_COOKIE_SUPPORT parameter is not valid
115 #endif
116 
117 //Stack size required to run the HTTP server
118 #ifndef HTTP_SERVER_STACK_SIZE
119  #define HTTP_SERVER_STACK_SIZE 650
120 #elif (HTTP_SERVER_STACK_SIZE < 1)
121  #error HTTP_SERVER_STACK_SIZE parameter is not valid
122 #endif
123 
124 //Priority at which the HTTP server should run
125 #ifndef HTTP_SERVER_PRIORITY
126  #define HTTP_SERVER_PRIORITY OS_TASK_PRIORITY_NORMAL
127 #endif
128 
129 //HTTP connection timeout
130 #ifndef HTTP_SERVER_TIMEOUT
131  #define HTTP_SERVER_TIMEOUT 10000
132 #elif (HTTP_SERVER_TIMEOUT < 1000)
133  #error HTTP_SERVER_TIMEOUT parameter is not valid
134 #endif
135 
136 //Maximum time the server will wait for a subsequent
137 //request before closing the connection
138 #ifndef HTTP_SERVER_IDLE_TIMEOUT
139  #define HTTP_SERVER_IDLE_TIMEOUT 5000
140 #elif (HTTP_SERVER_IDLE_TIMEOUT < 1000)
141  #error HTTP_SERVER_IDLE_TIMEOUT parameter is not valid
142 #endif
143 
144 //Maximum length of the pending connection queue
145 #ifndef HTTP_SERVER_BACKLOG
146  #define HTTP_SERVER_BACKLOG 4
147 #elif (HTTP_SERVER_BACKLOG < 1)
148  #error HTTP_SERVER_BACKLOG parameter is not valid
149 #endif
150 
151 //Maximum number of requests per connection
152 #ifndef HTTP_SERVER_MAX_REQUESTS
153  #define HTTP_SERVER_MAX_REQUESTS 1000
154 #elif (HTTP_SERVER_MAX_REQUESTS < 1)
155  #error HTTP_SERVER_MAX_REQUESTS parameter is not valid
156 #endif
157 
158 //Size of buffer used for input/output operations
159 #ifndef HTTP_SERVER_BUFFER_SIZE
160  #define HTTP_SERVER_BUFFER_SIZE 1024
161 #elif (HTTP_SERVER_BUFFER_SIZE < 128)
162  #error HTTP_SERVER_BUFFER_SIZE parameter is not valid
163 #endif
164 
165 //Maximum size of root directory
166 #ifndef HTTP_SERVER_ROOT_DIR_MAX_LEN
167  #define HTTP_SERVER_ROOT_DIR_MAX_LEN 31
168 #elif (HTTP_SERVER_ROOT_DIR_MAX_LEN < 7)
169  #error HTTP_SERVER_ROOT_DIR_MAX_LEN parameter is not valid
170 #endif
171 
172 //Maximum size of default index file
173 #ifndef HTTP_SERVER_DEFAULT_DOC_MAX_LEN
174  #define HTTP_SERVER_DEFAULT_DOC_MAX_LEN 31
175 #elif (HTTP_SERVER_DEFAULT_DOC_MAX_LEN < 7)
176  #error HTTP_SERVER_DEFAULT_DOC_MAX_LEN parameter is not valid
177 #endif
178 
179 //Maximum length of HTTP method
180 #ifndef HTTP_SERVER_METHOD_MAX_LEN
181  #define HTTP_SERVER_METHOD_MAX_LEN 7
182 #elif (HTTP_SERVER_METHOD_MAX_LEN < 1)
183  #error HTTP_SERVER_METHOD_MAX_LEN parameter is not valid
184 #endif
185 
186 //Maximum length of URI
187 #ifndef HTTP_SERVER_URI_MAX_LEN
188  #define HTTP_SERVER_URI_MAX_LEN 255
189 #elif (HTTP_SERVER_URI_MAX_LEN < 31)
190  #error HTTP_SERVER_URI_MAX_LEN parameter is not valid
191 #endif
192 
193 //Maximum length of query strings
194 #ifndef HTTP_SERVER_QUERY_STRING_MAX_LEN
195  #define HTTP_SERVER_QUERY_STRING_MAX_LEN 255
196 #elif (HTTP_SERVER_QUERY_STRING_MAX_LEN < 7)
197  #error HTTP_SERVER_QUERY_STRING_MAX_LEN parameter is not valid
198 #endif
199 
200 //Maximum host name length
201 #ifndef HTTP_SERVER_HOST_MAX_LEN
202  #define HTTP_SERVER_HOST_MAX_LEN 31
203 #elif (HTTP_SERVER_HOST_MAX_LEN < 7)
204  #error HTTP_SERVER_HOST_MAX_LEN parameter is not valid
205 #endif
206 
207 //Maximum user name length
208 #ifndef HTTP_SERVER_USERNAME_MAX_LEN
209  #define HTTP_SERVER_USERNAME_MAX_LEN 31
210 #elif (HTTP_SERVER_USERNAME_MAX_LEN < 7)
211  #error HTTP_SERVER_USERNAME_MAX_LEN parameter is not valid
212 #endif
213 
214 //Maximum length of CGI parameters
215 #ifndef HTTP_SERVER_CGI_PARAM_MAX_LEN
216  #define HTTP_SERVER_CGI_PARAM_MAX_LEN 31
217 #elif (HTTP_SERVER_CGI_PARAM_MAX_LEN < 7)
218  #error HTTP_SERVER_CGI_PARAM_MAX_LEN parameter is not valid
219 #endif
220 
221 //Maximum recursion limit
222 #ifndef HTTP_SERVER_SSI_MAX_RECURSION
223  #define HTTP_SERVER_SSI_MAX_RECURSION 3
224 #elif (HTTP_SERVER_SSI_MAX_RECURSION < 1 || HTTP_SERVER_SSI_MAX_RECURSION > 8)
225  #error HTTP_SERVER_SSI_MAX_RECURSION parameter is not valid
226 #endif
227 
228 //Maximum age for static resources
229 #ifndef HTTP_SERVER_MAX_AGE
230  #define HTTP_SERVER_MAX_AGE 0
231 #elif (HTTP_SERVER_MAX_AGE < 0)
232  #error HTTP_SERVER_MAX_AGE parameter is not valid
233 #endif
234 
235 //Nonce cache size
236 #ifndef HTTP_SERVER_NONCE_CACHE_SIZE
237  #define HTTP_SERVER_NONCE_CACHE_SIZE 8
238 #elif (HTTP_SERVER_NONCE_CACHE_SIZE < 1)
239  #error HTTP_SERVER_NONCE_CACHE_SIZE parameter is not valid
240 #endif
241 
242 //Lifetime of nonces
243 #ifndef HTTP_SERVER_NONCE_LIFETIME
244  #define HTTP_SERVER_NONCE_LIFETIME 60000
245 #elif (HTTP_SERVER_NONCE_LIFETIME < 1000)
246  #error HTTP_SERVER_NONCE_LIFETIME parameter is not valid
247 #endif
248 
249 //Nonce size
250 #ifndef HTTP_SERVER_NONCE_SIZE
251  #define HTTP_SERVER_NONCE_SIZE 16
252 #elif (HTTP_SERVER_NONCE_SIZE < 1)
253  #error HTTP_SERVER_NONCE_SIZE parameter is not valid
254 #endif
255 
256 //Maximum length for boundary string
257 #ifndef HTTP_SERVER_BOUNDARY_MAX_LEN
258  #define HTTP_SERVER_BOUNDARY_MAX_LEN 70
259 #elif (HTTP_SERVER_BOUNDARY_MAX_LEN < 1)
260  #error HTTP_SERVER_BOUNDARY_MAX_LEN parameter is not valid
261 #endif
262 
263 //Maximum length for cookies
264 #ifndef HTTP_SERVER_COOKIE_MAX_LEN
265  #define HTTP_SERVER_COOKIE_MAX_LEN 256
266 #elif (HTTP_SERVER_COOKIE_MAX_LEN < 1)
267  #error HTTP_SERVER_COOKIE_MAX_LEN parameter is not valid
268 #endif
269 
270 //Application specific context
271 #ifndef HTTP_SERVER_PRIVATE_CONTEXT
272  #define HTTP_SERVER_PRIVATE_CONTEXT
273 #endif
274 
275 //File system support?
276 #if (HTTP_SERVER_FS_SUPPORT == ENABLED)
277  #include "fs_port.h"
278 #else
279  #include "resource_manager.h"
280 #endif
281 
282 //TLS supported?
283 #if (HTTP_SERVER_TLS_SUPPORT == ENABLED)
284  #include "core/crypto.h"
285  #include "tls.h"
286  #include "tls_ticket.h"
287 #endif
288 
289 //Basic authentication supported?
290 #if (HTTP_SERVER_BASIC_AUTH_SUPPORT == ENABLED)
291  #include "core/crypto.h"
292  #include "encoding/base64.h"
293 #endif
294 
295 //Digest authentication supported?
296 #if (HTTP_SERVER_DIGEST_AUTH_SUPPORT == ENABLED)
297  #include "core/crypto.h"
298  #include "hash/md5.h"
299 #endif
300 
301 //WebSocket supported?
302 #if (HTTP_SERVER_WEB_SOCKET_SUPPORT == ENABLED)
303  #include "core/crypto.h"
304  #include "encoding/base64.h"
305 #endif
306 
307 //HTTP port number
308 #define HTTP_PORT 80
309 //HTTPS port number (HTTP over TLS)
310 #define HTTPS_PORT 443
311 
312 //Forward declaration of HttpServerContext structure
313 struct _HttpServerContext;
314 #define HttpServerContext struct _HttpServerContext
315 
316 //Forward declaration of HttpConnection structure
317 struct _HttpConnection;
318 #define HttpConnection struct _HttpConnection
319 
320 //C++ guard
321 #ifdef __cplusplus
322 extern "C" {
323 #endif
324 
325 
326 /**
327  * @brief Access status
328  **/
329 
330 typedef enum
331 {
337 
338 
339 /**
340  * @brief HTTP connection states
341  **/
342 
343 typedef enum
344 {
353 } HttpConnState;
354 
355 
356 //The HTTP_FLAG_BREAK macro causes the httpReadStream() function to stop
357 //reading data whenever the specified break character is encountered
358 #define HTTP_FLAG_BREAK(c) (HTTP_FLAG_BREAK_CHAR | LSB(c))
359 
360 
361 //TLS supported?
362 #if (HTTP_SERVER_TLS_SUPPORT == ENABLED)
363 
364 /**
365  * @brief TLS initialization callback function
366  **/
367 
368 typedef error_t (*TlsInitCallback)(HttpConnection *connection,
370 
371 #endif
372 
373 
374 /**
375  * @brief Random data generation callback function
376  **/
377 
378 typedef error_t (*HttpRandCallback)(uint8_t *data, size_t length);
379 
380 
381 /**
382  * @brief HTTP authentication callback function
383  **/
384 
386  const char_t *user, const char_t *uri);
387 
388 
389 /**
390  * @brief CGI callback function
391  **/
392 
393 typedef error_t (*HttpCgiCallback)(HttpConnection *connection,
394  const char_t *param);
395 
396 
397 /**
398  * @brief HTTP request callback function
399  **/
400 
402  const char_t *uri);
403 
404 
405 /**
406  * @brief URI not found callback function
407  **/
408 
410  const char_t *uri);
411 
412 
413 /**
414  * @brief HTTP status code
415  **/
416 
417 typedef struct
418 {
420  const char_t message[28];
422 
423 
424 /**
425  * @brief Authorization header
426  **/
427 
428 typedef struct
429 {
430  bool_t found; ///<The Authorization header has been found
431  HttpAuthMode mode; ///<Authentication scheme
432  char_t user[HTTP_SERVER_USERNAME_MAX_LEN + 1]; ///<User name
433 #if (HTTP_SERVER_BASIC_AUTH_SUPPORT == ENABLED)
434  const char_t *password; ///<Password
435 #endif
436 #if (HTTP_SERVER_DIGEST_AUTH_SUPPORT == ENABLED)
437  const char_t *realm;
438  const char_t *nonce; ///<Server nonce
439  const char_t *uri; ///<Digest URI
440  const char_t *qop;
441  const char_t *nc; ///<Nonce count
442  const char_t *cnonce; ///<Client nonce
443  const char_t *response;
444  const char_t *opaque;
445 #endif
447 
448 
449 /**
450  * @brief Authenticate header
451  **/
452 
453 typedef struct
454 {
455  HttpAuthMode mode; ///<Authentication scheme
456 #if (HTTP_SERVER_DIGEST_AUTH_SUPPORT == ENABLED)
457  bool_t stale; ///<STALE flag
458 #endif
460 
461 
462 /**
463  * @brief HTTP request
464  **/
465 
466 typedef struct
467 {
468  uint_t version; ///<HTTP version number
469  char_t method[HTTP_SERVER_METHOD_MAX_LEN + 1]; ///<HTTP method
470  char_t uri[HTTP_SERVER_URI_MAX_LEN + 1]; ///<Resource identifier
471  char_t queryString[HTTP_SERVER_QUERY_STRING_MAX_LEN + 1]; ///<Query string
472  char_t host[HTTP_SERVER_HOST_MAX_LEN + 1]; ///<Host name
476  size_t byteCount;
479 #if (HTTP_SERVER_BASIC_AUTH_SUPPORT == ENABLED || HTTP_SERVER_DIGEST_AUTH_SUPPORT == ENABLED)
480  HttpAuthorizationHeader auth; ///<Authorization header
481 #endif
482 #if (HTTP_SERVER_WEB_SOCKET_SUPPORT == ENABLED)
486 #endif
487 #if (HTTP_SERVER_GZIP_TYPE_SUPPORT == ENABLED)
489 #endif
490 #if (HTTP_SERVER_MULTIPART_TYPE_SUPPORT == ENABLED)
491  char_t boundary[HTTP_SERVER_BOUNDARY_MAX_LEN + 1]; ///<Boundary string
492  size_t boundaryLength; ///<Boundary string length
493 #endif
494 #if (HTTP_SERVER_COOKIE_SUPPORT == ENABLED)
495  char_t cookie[HTTP_SERVER_COOKIE_MAX_LEN + 1]; ///<Cookie header field
496 #endif
497 } HttpRequest;
498 
499 
500 /**
501  * @brief HTTP response
502  **/
503 
504 typedef struct
505 {
506  uint_t version; ///<HTTP version number
507  uint_t statusCode; ///<HTTP status code
511  const char_t *location;
515  size_t byteCount;
516 #if (HTTP_SERVER_BASIC_AUTH_SUPPORT == ENABLED || HTTP_SERVER_DIGEST_AUTH_SUPPORT == ENABLED)
517  HttpAuthenticateHeader auth; ///<Authenticate header
518 #endif
519 #if (HTTP_SERVER_GZIP_TYPE_SUPPORT == ENABLED)
521 #endif
522 #if (HTTP_SERVER_COOKIE_SUPPORT == ENABLED)
523  char_t setCookie[HTTP_SERVER_COOKIE_MAX_LEN + 1]; ///<Set-Cookie header field
524 #endif
525 } HttpResponse;
526 
527 
528 /**
529  * @brief HTTP server settings
530  **/
531 
532 typedef struct
533 {
534  NetInterface *interface; ///<Underlying network interface
535  uint16_t port; ///<HTTP server port number
536  uint_t backlog; ///<Maximum length of the pending connection queue
537  uint_t maxConnections; ///<Maximum number of client connections
538  HttpConnection *connections; ///<Client connections
539  char_t rootDirectory[HTTP_SERVER_ROOT_DIR_MAX_LEN + 1]; ///<Web root directory
540  char_t defaultDocument[HTTP_SERVER_DEFAULT_DOC_MAX_LEN + 1]; ///<Default home page
541 #if (HTTP_SERVER_TLS_SUPPORT == ENABLED)
542  bool_t useTls; ///<Deprecated flag
543  TlsInitCallback tlsInitCallback; ///<TLS initialization callback function
544 #endif
545 #if (HTTP_SERVER_BASIC_AUTH_SUPPORT == ENABLED || HTTP_SERVER_DIGEST_AUTH_SUPPORT == ENABLED)
546  HttpRandCallback randCallback; ///<Random data generation callback function
547  HttpAuthCallback authCallback; ///<HTTP authentication callback function
548 #endif
549  HttpCgiCallback cgiCallback; ///<CGI callback function
550  HttpRequestCallback requestCallback; ///<HTTP request callback function
551  HttpUriNotFoundCallback uriNotFoundCallback; ///<URI not found callback function
553 
554 
555 /**
556  * @brief Nonce cache entry
557  **/
558 
559 typedef struct
560 {
561  char_t nonce[HTTP_SERVER_NONCE_SIZE * 2 + 1]; ///<Nonce
562  uint32_t count; ///<Nonce count
563  systime_t timestamp; ///<Time stamp to manage entry lifetime
565 
566 
567 /**
568  * @brief HTTP server context
569  **/
570 
572 {
573  HttpServerSettings settings; ///<User settings
574  OsTask *taskHandle; ///<Listener task handle
575  OsSemaphore semaphore; ///<Semaphore limiting the number of connections
576  Socket *socket; ///<Listening socket
577  HttpConnection *connections; ///<Client connections
578 #if (HTTP_SERVER_TLS_SUPPORT == ENABLED && TLS_TICKET_SUPPORT == ENABLED)
579  TlsTicketContext tlsTicketContext; ///<TLS ticket encryption context
580 #endif
581 #if (HTTP_SERVER_DIGEST_AUTH_SUPPORT == ENABLED)
582  OsMutex nonceCacheMutex; ///<Mutex preventing simultaneous access to the nonce cache
584 #endif
585 };
586 
587 
588 /**
589  * @brief HTTP connection
590  *
591  * An HttpConnection instance represents one
592  * transaction with an HTTP client
593  *
594  **/
595 
597 {
598  HttpServerSettings *settings; ///<Reference to the HTTP server settings
599  HttpServerContext *serverContext; ///<Reference to the HTTP server context
600  OsTask *taskHandle; ///<Client task handle
603  Socket *socket; ///<Socket
604 #if (HTTP_SERVER_TLS_SUPPORT == ENABLED)
605  TlsContext *tlsContext; ///<TLS context
606 #endif
607  HttpRequest request; ///<Incoming HTTP request header
608  HttpResponse response; ///<HTTP response header
609  HttpAccessStatus status; ///<Access status
611  uint32_t dummy; ///<Force alignment of the buffer on 32-bit boundaries
612  char_t buffer[HTTP_SERVER_BUFFER_SIZE]; ///<Memory buffer for input/output operations
613 #if (NET_RTOS_SUPPORT == DISABLED)
614  HttpConnState state; ///<Connection state
616  size_t bufferPos;
617  size_t bufferLen;
618  uint8_t *bodyStart;
619  size_t bodyPos;
620  size_t bodyLen;
621 #endif
622  HTTP_SERVER_PRIVATE_CONTEXT ///<Application specific context
623 };
624 
625 
626 //HTTP server related functions
630 
631 void httpListenerTask(void *param);
632 void httpConnectionTask(void *param);
633 
635 
637  void *data, size_t size, size_t *received, uint_t flags);
638 
640  const void *data, size_t length);
641 
643 
644 error_t httpSendResponse(HttpConnection *connection, const char_t *uri);
645 
647  uint_t statusCode, const char_t *message);
648 
650  uint_t statusCode, const char_t *uri);
651 
652 //HTTP authentication related functions
654  const char_t *password, HttpAuthMode mode);
655 
656 //WebSocket related functions
659 
660 //Miscellaneous functions
662  char_t *output, size_t outputSize);
663 
664 //C++ guard
665 #ifdef __cplusplus
666 }
667 #endif
668 
669 #endif
void httpServerGetDefaultSettings(HttpServerSettings *settings)
Initialize settings with default values.
Definition: http_server.c:63
size_t contentLength
Definition: http_server.h:475
uint8_t length
Definition: dtls_misc.h:149
error_t(* HttpCgiCallback)(HttpConnection *connection, const char_t *param)
CGI callback function.
Definition: http_server.h:393
int bool_t
Definition: compiler_port.h:49
HttpConnection * connections
Client connections.
Definition: http_server.h:577
OsSemaphore semaphore
Semaphore limiting the number of connections.
Definition: http_server.h:575
bool_t firstChunk
Definition: http_server.h:477
HttpAuthorizationHeader auth
Authorization header.
Definition: http_server.h:480
WebSocket * httpUpgradeToWebSocket(HttpConnection *connection)
Upgrade an existing HTTP connection to a WebSocket.
Definition: http_server.c:1244
#define HTTP_SERVER_NONCE_SIZE
Definition: http_server.h:251
error_t(* TlsInitCallback)(HttpConnection *connection, TlsContext *tlsContext)
TLS initialization callback function.
Definition: http_server.h:368
#define HTTP_SERVER_COOKIE_MAX_LEN
Definition: http_server.h:265
HttpResponse response
HTTP response header.
Definition: http_server.h:608
#define WEB_SOCKET_CLIENT_KEY_SIZE
Definition: web_socket.h:171
bool_t useTls
Deprecated flag.
Definition: http_server.h:542
HttpServerSettings settings
User settings.
Definition: http_server.h:573
#define HTTP_SERVER_PRIVATE_CONTEXT
Definition: http_server.h:272
systime_t timestamp
Time stamp to manage entry lifetime.
Definition: http_server.h:563
HttpRandCallback randCallback
Random data generation callback function.
Definition: http_server.h:546
WebSocket API (client and server)
bool_t connectionUpgrade
Definition: http_server.h:484
bool_t upgradeWebSocket
Definition: http_server.h:483
#define HttpServerContext
Definition: http_server.h:314
Event object.
HttpRequestCallback requestCallback
HTTP request callback function.
Definition: http_server.h:550
#define HTTP_SERVER_HOST_MAX_LEN
Definition: http_server.h:202
HttpConnection * connections
Client connections.
Definition: http_server.h:538
Authenticate header.
Definition: http_server.h:453
HTTP connection.
Definition: http_server.h:596
#define HTTP_SERVER_URI_MAX_LEN
Definition: http_server.h:188
char_t cgiParam[HTTP_SERVER_CGI_PARAM_MAX_LEN+1]
CGI parameter.
Definition: http_server.h:610
@ HTTP_ACCESS_ALLOWED
Definition: http_server.h:333
HttpAccessStatus status
Access status.
Definition: http_server.h:609
Semaphore object.
OsEvent startEvent
Definition: http_server.h:601
error_t httpServerStart(HttpServerContext *context)
Start HTTP server.
Definition: http_server.c:208
bool_t chunkedEncoding
Definition: http_server.h:474
Socket * socket
Listening socket.
Definition: http_server.h:576
HttpRequest request
Incoming HTTP request header.
Definition: http_server.h:607
HTTP response.
Definition: http_server.h:504
bool_t chunkedEncoding
Definition: http_server.h:513
error_t(* HttpRequestCallback)(HttpConnection *connection, const char_t *uri)
HTTP request callback function.
Definition: http_server.h:401
Socket * socket
Socket.
Definition: http_server.h:603
const char_t * password
Password.
Definition: http_server.h:434
Session ticket encryption context.
Definition: tls_ticket.h:90
TlsInitCallback tlsInitCallback
TLS initialization callback function.
Definition: http_server.h:543
HttpAccessStatus(* HttpAuthCallback)(HttpConnection *connection, const char_t *user, const char_t *uri)
HTTP authentication callback function.
Definition: http_server.h:385
uint_t statusCode
HTTP status code.
Definition: http_server.h:507
error_t(* HttpUriNotFoundCallback)(HttpConnection *connection, const char_t *uri)
URI not found callback function.
Definition: http_server.h:409
@ HTTP_ACCESS_DIGEST_AUTH_REQUIRED
Definition: http_server.h:335
#define TlsContext
Definition: tls.h:36
@ HTTP_CONN_STATE_REQ_BODY
Definition: http_server.h:348
error_t
Error codes.
Definition: error.h:42
#define HTTP_SERVER_NONCE_CACHE_SIZE
Definition: http_server.h:237
#define HttpConnection
Definition: http_server.h:318
OsMutex nonceCacheMutex
Mutex preventing simultaneous access to the nonce cache.
Definition: http_server.h:582
@ HTTP_CONN_STATE_RESP_BODY
Definition: http_server.h:350
const char_t * realm
Definition: http_server.h:437
uint16_t statusCode
HTTP status code.
Definition: http_server.h:417
OsTask * taskHandle
Client task handle.
Definition: http_server.h:600
HttpAccessStatus
Access status.
Definition: http_server.h:330
#define HTTP_SERVER_ROOT_DIR_MAX_LEN
Definition: http_server.h:167
Nonce cache entry.
Definition: http_server.h:559
#define NetInterface
Definition: net.h:36
Task object.
uint32_t dummy
Force alignment of the buffer on 32-bit boundaries.
Definition: http_server.h:611
const char_t * opaque
Definition: http_server.h:444
@ HTTP_CONN_STATE_RESP_HEADER
Definition: http_server.h:349
Authorization header.
Definition: http_server.h:428
General definitions for cryptographic algorithms.
bool_t acceptGzipEncoding
Definition: http_server.h:488
Base64 encoding scheme.
HTTP server settings.
Definition: http_server.h:532
uint16_t port
HTTP server port number.
Definition: http_server.h:535
TlsContext * tlsContext
TLS context.
Definition: http_server.h:605
#define HTTP_SERVER_USERNAME_MAX_LEN
Definition: http_server.h:209
systime_t timestamp
Definition: http_server.h:615
HttpNonceCacheEntry nonceCache[HTTP_SERVER_NONCE_CACHE_SIZE]
Nonce cache.
Definition: http_server.h:583
const char_t * nc
Nonce count.
Definition: http_server.h:441
#define WebSocket
Definition: web_socket.h:177
HttpAuthMode mode
Authentication scheme.
Definition: http_server.h:455
HttpServerSettings * settings
Reference to the HTTP server settings.
Definition: http_server.h:598
uint8_t * bodyStart
Definition: http_server.h:618
File system abstraction layer.
TLS session tickets.
HTTP server context.
Definition: http_server.h:571
uint_t backlog
Maximum length of the pending connection queue.
Definition: http_server.h:536
HttpCgiCallback cgiCallback
CGI callback function.
Definition: http_server.h:549
bool_t found
The Authorization header has been found.
Definition: http_server.h:430
@ HTTP_CONN_STATE_REQ_HEADER
Definition: http_server.h:347
Mutex object.
uint32_t count
Nonce count.
Definition: http_server.h:562
Definitions common to HTTP client and server.
uint8_t flags
Definition: tcp.h:314
TlsTicketContext tlsTicketContext
TLS ticket encryption context.
Definition: http_server.h:579
char char_t
Definition: compiler_port.h:43
bool_t keepAlive
Definition: http_server.h:473
error_t httpCloseStream(HttpConnection *connection)
Close output stream.
Definition: http_server.c:831
const char_t * location
Definition: http_server.h:511
uint_t version
HTTP version number.
Definition: http_server.h:468
@ HTTP_CONN_STATE_REQ_LINE
Definition: http_server.h:346
error_t httpReadStream(HttpConnection *connection, void *data, size_t size, size_t *received, uint_t flags)
Read data from client request.
Definition: http_server.c:660
uint_t version
HTTP version number.
Definition: http_server.h:506
size_t contentLength
Definition: http_server.h:514
HTTP request.
Definition: http_server.h:466
error_t httpWriteStream(HttpConnection *connection, const void *data, size_t length)
Write data to the client.
Definition: http_server.c:766
error_t httpSendRedirectResponse(HttpConnection *connection, uint_t statusCode, const char_t *uri)
Send redirect response to the client.
Definition: http_server.c:1130
void httpConnectionTask(void *param)
Task that services requests from an active connection.
Definition: http_server.c:323
@ HTTP_CONN_STATE_SHUTDOWN
Definition: http_server.h:351
MD5 (Message-Digest Algorithm)
HttpAuthCallback authCallback
HTTP authentication callback function.
Definition: http_server.h:547
error_t httpSendResponse(HttpConnection *connection, const char_t *uri)
Send HTTP response.
Definition: http_server.c:859
#define HTTP_SERVER_QUERY_STRING_MAX_LEN
Definition: http_server.h:195
bool_t httpCheckPassword(HttpConnection *connection, const char_t *password, HttpAuthMode mode)
Password verification.
#define HTTP_SERVER_METHOD_MAX_LEN
Definition: http_server.h:181
#define Socket
Definition: socket.h:36
uint_t maxConnections
Maximum number of client connections.
Definition: http_server.h:537
error_t(* HttpRandCallback)(uint8_t *data, size_t length)
Random data generation callback function.
Definition: http_server.h:378
OsTask * taskHandle
Listener task handle.
Definition: http_server.h:574
NetInterface * interface
Underlying network interface.
Definition: http_server.h:534
const char_t * contentType
Definition: http_server.h:512
uint8_t message[]
Definition: chap.h:152
const char_t * cnonce
Client nonce.
Definition: http_server.h:442
char_t buffer[HTTP_SERVER_BUFFER_SIZE]
Memory buffer for input/output operations.
Definition: http_server.h:612
HttpConnState state
Connection state.
Definition: http_server.h:614
TLS (Transport Layer Security)
uint8_t cookie[]
Definition: dtls_misc.h:203
Socket API.
size_t byteCount
Definition: http_server.h:515
bool_t gzipEncoding
Definition: http_server.h:520
bool_t lastChunk
Definition: http_server.h:478
size_t boundaryLength
Boundary string length.
Definition: http_server.h:492
@ HTTP_CONN_STATE_IDLE
Definition: http_server.h:345
bool_t httpCheckWebSocketHandshake(HttpConnection *connection)
Check whether the client's handshake is valid.
Definition: http_server.c:1195
#define HTTP_SERVER_BUFFER_SIZE
Definition: http_server.h:160
#define HTTP_SERVER_DEFAULT_DOC_MAX_LEN
Definition: http_server.h:174
uint8_t mode
Definition: ntp_common.h:149
error_t httpWriteHeader(HttpConnection *connection)
Send HTTP response header.
Definition: http_server.c:627
@ HTTP_CONN_STATE_CLOSE
Definition: http_server.h:352
HttpAuthMode mode
Authentication scheme.
Definition: http_server.h:431
error_t httpDecodePercentEncodedString(const char_t *input, char_t *output, size_t outputSize)
Decode a percent-encoded string.
size_t byteCount
Definition: http_server.h:476
const char_t * qop
Definition: http_server.h:440
@ HTTP_ACCESS_DENIED
Definition: http_server.h:332
HttpServerContext * serverContext
Reference to the HTTP server context.
Definition: http_server.h:599
Embedded resource management.
bool_t keepAlive
Definition: http_server.h:508
unsigned int uint_t
Definition: compiler_port.h:45
HttpConnState
HTTP connection states.
Definition: http_server.h:343
uint8_t data[]
Definition: dtls_misc.h:176
void httpListenerTask(void *param)
HTTP server listener task.
Definition: http_server.c:250
error_t httpServerInit(HttpServerContext *context, const HttpServerSettings *settings)
HTTP server initialization.
Definition: http_server.c:110
HttpAuthMode
HTTP authentication schemes.
Definition: http_common.h:71
const char_t * nonce
Server nonce.
Definition: http_server.h:438
const char_t * response
Definition: http_server.h:443
const char_t * uri
Digest URI.
Definition: http_server.h:439
RTOS abstraction layer.
bool_t noCache
Definition: http_server.h:509
HttpAuthenticateHeader auth
Authenticate header.
Definition: http_server.h:517
uint32_t systime_t
Definition: compiler_port.h:46
#define HTTP_SERVER_BOUNDARY_MAX_LEN
Definition: http_server.h:258
@ HTTP_ACCESS_BASIC_AUTH_REQUIRED
Definition: http_server.h:334
#define HTTP_SERVER_CGI_PARAM_MAX_LEN
Definition: http_server.h:216
uint_t maxAge
Definition: http_server.h:510
bool_t stale
STALE flag.
Definition: http_server.h:457
error_t httpSendErrorResponse(HttpConnection *connection, uint_t statusCode, const char_t *message)
Send error response to the client.
Definition: http_server.c:1064
HttpUriNotFoundCallback uriNotFoundCallback
URI not found callback function.
Definition: http_server.h:551