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