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
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
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
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
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
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
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
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
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
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
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
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
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
132#ifndef HTTP_SERVER_PRIORITY
133 #define HTTP_SERVER_PRIORITY OS_TASK_PRIORITY_NORMAL
137#ifndef HTTP_SERVER_TIMEOUT
138 #define HTTP_SERVER_TIMEOUT 10000
139#elif (HTTP_SERVER_TIMEOUT < 1000)
140 #error HTTP_SERVER_TIMEOUT parameter is not valid
145#ifndef HTTP_SERVER_IDLE_TIMEOUT
146 #define HTTP_SERVER_IDLE_TIMEOUT 5000
147#elif (HTTP_SERVER_IDLE_TIMEOUT < 1000)
148 #error HTTP_SERVER_IDLE_TIMEOUT parameter is not valid
152#ifndef HTTP_SERVER_BACKLOG
153 #define HTTP_SERVER_BACKLOG 4
154#elif (HTTP_SERVER_BACKLOG < 1)
155 #error HTTP_SERVER_BACKLOG parameter is not valid
159#ifndef HTTP_SERVER_MAX_REQUESTS
160 #define HTTP_SERVER_MAX_REQUESTS 1000
161#elif (HTTP_SERVER_MAX_REQUESTS < 1)
162 #error HTTP_SERVER_MAX_REQUESTS parameter is not valid
166#ifndef HTTP_SERVER_BUFFER_SIZE
167 #define HTTP_SERVER_BUFFER_SIZE 1024
168#elif (HTTP_SERVER_BUFFER_SIZE < 128)
169 #error HTTP_SERVER_BUFFER_SIZE parameter is not valid
173#ifndef HTTP_SERVER_ROOT_DIR_MAX_LEN
174 #define HTTP_SERVER_ROOT_DIR_MAX_LEN 31
175#elif (HTTP_SERVER_ROOT_DIR_MAX_LEN < 7)
176 #error HTTP_SERVER_ROOT_DIR_MAX_LEN parameter is not valid
180#ifndef HTTP_SERVER_DEFAULT_DOC_MAX_LEN
181 #define HTTP_SERVER_DEFAULT_DOC_MAX_LEN 31
182#elif (HTTP_SERVER_DEFAULT_DOC_MAX_LEN < 7)
183 #error HTTP_SERVER_DEFAULT_DOC_MAX_LEN parameter is not valid
187#ifndef HTTP_SERVER_METHOD_MAX_LEN
188 #define HTTP_SERVER_METHOD_MAX_LEN 7
189#elif (HTTP_SERVER_METHOD_MAX_LEN < 1)
190 #error HTTP_SERVER_METHOD_MAX_LEN parameter is not valid
194#ifndef HTTP_SERVER_URI_MAX_LEN
195 #define HTTP_SERVER_URI_MAX_LEN 255
196#elif (HTTP_SERVER_URI_MAX_LEN < 31)
197 #error HTTP_SERVER_URI_MAX_LEN parameter is not valid
201#ifndef HTTP_SERVER_QUERY_STRING_MAX_LEN
202 #define HTTP_SERVER_QUERY_STRING_MAX_LEN 255
203#elif (HTTP_SERVER_QUERY_STRING_MAX_LEN < 7)
204 #error HTTP_SERVER_QUERY_STRING_MAX_LEN parameter is not valid
208#ifndef HTTP_SERVER_HOST_MAX_LEN
209 #define HTTP_SERVER_HOST_MAX_LEN 31
210#elif (HTTP_SERVER_HOST_MAX_LEN < 7)
211 #error HTTP_SERVER_HOST_MAX_LEN parameter is not valid
215#ifndef HTTP_SERVER_USERNAME_MAX_LEN
216 #define HTTP_SERVER_USERNAME_MAX_LEN 31
217#elif (HTTP_SERVER_USERNAME_MAX_LEN < 7)
218 #error HTTP_SERVER_USERNAME_MAX_LEN parameter is not valid
222#ifndef HTTP_SERVER_CGI_PARAM_MAX_LEN
223 #define HTTP_SERVER_CGI_PARAM_MAX_LEN 31
224#elif (HTTP_SERVER_CGI_PARAM_MAX_LEN < 7)
225 #error HTTP_SERVER_CGI_PARAM_MAX_LEN parameter is not valid
229#ifndef HTTP_SERVER_SSI_MAX_RECURSION
230 #define HTTP_SERVER_SSI_MAX_RECURSION 3
231#elif (HTTP_SERVER_SSI_MAX_RECURSION < 1 || HTTP_SERVER_SSI_MAX_RECURSION > 8)
232 #error HTTP_SERVER_SSI_MAX_RECURSION parameter is not valid
236#ifndef HTTP_SERVER_MAX_AGE
237 #define HTTP_SERVER_MAX_AGE 0
238#elif (HTTP_SERVER_MAX_AGE < 0)
239 #error HTTP_SERVER_MAX_AGE parameter is not valid
243#ifndef HTTP_SERVER_NONCE_CACHE_SIZE
244 #define HTTP_SERVER_NONCE_CACHE_SIZE 8
245#elif (HTTP_SERVER_NONCE_CACHE_SIZE < 1)
246 #error HTTP_SERVER_NONCE_CACHE_SIZE parameter is not valid
250#ifndef HTTP_SERVER_NONCE_LIFETIME
251 #define HTTP_SERVER_NONCE_LIFETIME 60000
252#elif (HTTP_SERVER_NONCE_LIFETIME < 1000)
253 #error HTTP_SERVER_NONCE_LIFETIME parameter is not valid
257#ifndef HTTP_SERVER_NONCE_SIZE
258 #define HTTP_SERVER_NONCE_SIZE 16
259#elif (HTTP_SERVER_NONCE_SIZE < 1)
260 #error HTTP_SERVER_NONCE_SIZE parameter is not valid
264#ifndef HTTP_SERVER_BOUNDARY_MAX_LEN
265 #define HTTP_SERVER_BOUNDARY_MAX_LEN 70
266#elif (HTTP_SERVER_BOUNDARY_MAX_LEN < 1)
267 #error HTTP_SERVER_BOUNDARY_MAX_LEN parameter is not valid
271#ifndef HTTP_SERVER_COOKIE_MAX_LEN
272 #define HTTP_SERVER_COOKIE_MAX_LEN 256
273#elif (HTTP_SERVER_COOKIE_MAX_LEN < 1)
274 #error HTTP_SERVER_COOKIE_MAX_LEN parameter is not valid
278#ifndef HTTP_SERVER_PRIVATE_CONTEXT
279 #define HTTP_SERVER_PRIVATE_CONTEXT
283#if (HTTP_SERVER_FS_SUPPORT == ENABLED)
290#if (HTTP_SERVER_TLS_SUPPORT == ENABLED)
291 #include "core/crypto.h"
293 #include "tls_ticket.h"
297#if (HTTP_SERVER_BASIC_AUTH_SUPPORT == ENABLED)
298 #include "core/crypto.h"
299 #include "encoding/base64.h"
303#if (HTTP_SERVER_DIGEST_AUTH_SUPPORT == ENABLED)
304 #include "core/crypto.h"
305 #include "hash/md5.h"
309#if (HTTP_SERVER_WEB_SOCKET_SUPPORT == ENABLED)
310 #include "core/crypto.h"
311 #include "encoding/base64.h"
317#define HTTPS_PORT 443
321#define HttpServerContext struct _HttpServerContext
325#define HttpConnection struct _HttpConnection
339 HTTP_ACCESS_DENIED = 0,
340 HTTP_ACCESS_ALLOWED = 1,
341 HTTP_ACCESS_BASIC_AUTH_REQUIRED = 2,
342 HTTP_ACCESS_DIGEST_AUTH_REQUIRED = 3
352 HTTP_CONN_STATE_IDLE = 0,
353 HTTP_CONN_STATE_REQ_LINE = 1,
354 HTTP_CONN_STATE_REQ_HEADER = 2,
355 HTTP_CONN_STATE_REQ_BODY = 3,
356 HTTP_CONN_STATE_RESP_HEADER = 4,
357 HTTP_CONN_STATE_RESP_BODY = 5,
358 HTTP_CONN_STATE_SHUTDOWN = 6,
359 HTTP_CONN_STATE_CLOSE = 7
365#define HTTP_FLAG_BREAK(c) (HTTP_FLAG_BREAK_CHAR | LSB(c))
369#if (HTTP_SERVER_TLS_SUPPORT == ENABLED)
393 const char_t *user,
const char_t *uri);
401 const char_t *param);
427 const char_t message[28];
439 char_t user[HTTP_SERVER_USERNAME_MAX_LEN + 1];
440#if (HTTP_SERVER_BASIC_AUTH_SUPPORT == ENABLED)
443#if (HTTP_SERVER_DIGEST_AUTH_SUPPORT == ENABLED)
451 const char_t *opaque;
463#if (HTTP_SERVER_DIGEST_AUTH_SUPPORT == ENABLED)
476 char_t method[HTTP_SERVER_METHOD_MAX_LEN + 1];
477 char_t uri[HTTP_SERVER_URI_MAX_LEN + 1];
478 char_t queryString[HTTP_SERVER_QUERY_STRING_MAX_LEN + 1];
479 char_t host[HTTP_SERVER_HOST_MAX_LEN + 1];
481 bool_t chunkedEncoding;
482 size_t contentLength;
486#if (HTTP_SERVER_BASIC_AUTH_SUPPORT == ENABLED || HTTP_SERVER_DIGEST_AUTH_SUPPORT == ENABLED)
489#if (HTTP_SERVER_WEB_SOCKET_SUPPORT == ENABLED)
490 bool_t upgradeWebSocket;
491 bool_t connectionUpgrade;
492 char_t clientKey[WEB_SOCKET_CLIENT_KEY_SIZE + 1];
494#if (HTTP_SERVER_GZIP_TYPE_SUPPORT == ENABLED)
495 bool_t acceptGzipEncoding;
497#if (HTTP_SERVER_MULTIPART_TYPE_SUPPORT == ENABLED)
498 char_t boundary[HTTP_SERVER_BOUNDARY_MAX_LEN + 1];
501#if (HTTP_SERVER_COOKIE_SUPPORT == ENABLED)
502 char_t cookie[HTTP_SERVER_COOKIE_MAX_LEN + 1];
518 const char_t *location;
519 const char_t *contentType;
520 bool_t chunkedEncoding;
521 size_t contentLength;
523#if (HTTP_SERVER_BASIC_AUTH_SUPPORT == ENABLED || HTTP_SERVER_DIGEST_AUTH_SUPPORT == ENABLED)
526#if (HTTP_SERVER_GZIP_TYPE_SUPPORT == ENABLED)
529#if (HTTP_SERVER_COOKIE_SUPPORT == ENABLED)
530 char_t setCookie[HTTP_SERVER_COOKIE_MAX_LEN + 1];
547 char_t rootDirectory[HTTP_SERVER_ROOT_DIR_MAX_LEN + 1];
548 char_t defaultDocument[HTTP_SERVER_DEFAULT_DOC_MAX_LEN + 1];
549#if (HTTP_SERVER_TLS_SUPPORT == ENABLED)
553#if (HTTP_SERVER_BASIC_AUTH_SUPPORT == ENABLED || HTTP_SERVER_DIGEST_AUTH_SUPPORT == ENABLED)
569 char_t nonce[HTTP_SERVER_NONCE_SIZE * 2 + 1];
584#if (OS_STATIC_TASK_SUPPORT == ENABLED)
590#if (HTTP_SERVER_TLS_SUPPORT == ENABLED && TLS_TICKET_SUPPORT == ENABLED)
593#if (HTTP_SERVER_DIGEST_AUTH_SUPPORT == ENABLED)
615#if (OS_STATIC_TASK_SUPPORT == ENABLED)
620#if (HTTP_SERVER_TLS_SUPPORT == ENABLED)
626 char_t
cgiParam[HTTP_SERVER_CGI_PARAM_MAX_LEN + 1];
629#if (NET_RTOS_SUPPORT == DISABLED)
638 HTTP_SERVER_PRIVATE_CONTEXT
645error_t httpServerStart(HttpServerContext *context);
647void httpListenerTask(
void *param);
648void httpConnectionTask(
void *param);
650error_t httpWriteHeader(HttpConnection *connection);
652error_t httpReadStream(HttpConnection *connection,
653 void *data,
size_t size,
size_t *received, uint_t flags);
655error_t httpWriteStream(HttpConnection *connection,
656 const void *data,
size_t length);
658error_t httpCloseStream(HttpConnection *connection);
660error_t httpSendResponse(HttpConnection *connection,
const char_t *uri);
662error_t httpSendErrorResponse(HttpConnection *connection,
663 uint_t statusCode,
const char_t *message);
665error_t httpSendRedirectResponse(HttpConnection *connection,
666 uint_t statusCode,
const char_t *uri);
669bool_t httpCheckPassword(HttpConnection *connection,
673bool_t httpCheckWebSocketHandshake(HttpConnection *connection);
674WebSocket *httpUpgradeToWebSocket(HttpConnection *connection);
677error_t httpDecodePercentEncodedString(
const char_t *input,
678 char_t *output,
size_t outputSize);
error_t
Error codes.
Definition error.h:43
File system abstraction layer.
Definitions common to HTTP client and server.
HttpAuthMode
HTTP authentication schemes.
Definition http_common.h:72
error_t(* HttpUriNotFoundCallback)(HttpConnection *connection, const char_t *uri)
URI not found callback function.
Definition http_server.h:416
HttpAccessStatus
Access status.
Definition http_server.h:338
HttpConnState
HTTP connection states.
Definition http_server.h:351
error_t(* HttpCgiCallback)(HttpConnection *connection, const char_t *param)
CGI callback function.
Definition http_server.h:400
HttpAccessStatus(* HttpAuthCallback)(HttpConnection *connection, const char_t *user, const char_t *uri)
HTTP authentication callback function.
Definition http_server.h:392
error_t(* TlsInitCallback)(HttpConnection *connection, TlsContext *tlsContext)
TLS initialization callback function.
Definition http_server.h:375
error_t(* HttpRandCallback)(uint8_t *data, size_t length)
Random data generation callback function.
Definition http_server.h:385
error_t(* HttpRequestCallback)(HttpConnection *connection, const char_t *uri)
HTTP request callback function.
Definition http_server.h:408
uint_t OsMutex
Mutex object.
Definition os_port_none.h:118
uint_t OsEvent
Event object.
Definition os_port_none.h:104
uint_t OsSemaphore
Semaphore object.
Definition os_port_none.h:111
uint_t OsTaskId
Task identifier.
Definition os_port_none.h:97
uint32_t systime_t
System time.
Definition os_port_none.h:90
Embedded resource management.
Nonce cache entry.
Definition http_server.h:568
systime_t timestamp
Time stamp to manage entry lifetime.
Definition http_server.h:571
uint32_t count
Nonce count.
Definition http_server.h:570
HTTP request.
Definition http_server.h:474
HttpAuthorizationHeader auth
Authorization header.
Definition http_server.h:487
uint_t version
HTTP version number.
Definition http_server.h:475
size_t boundaryLength
Boundary string length.
Definition http_server.h:499
HTTP response.
Definition http_server.h:512
uint_t version
HTTP version number.
Definition http_server.h:513
uint_t statusCode
HTTP status code.
Definition http_server.h:514
HttpAuthenticateHeader auth
Authenticate header.
Definition http_server.h:524
HTTP server settings.
Definition http_server.h:540
uint_t maxConnections
Maximum number of client connections.
Definition http_server.h:545
NetInterface * interface
Underlying network interface.
Definition http_server.h:541
HttpAuthCallback authCallback
HTTP authentication callback function.
Definition http_server.h:555
TlsInitCallback tlsInitCallback
TLS initialization callback function.
Definition http_server.h:551
uint_t backlog
Maximum length of the pending connection queue.
Definition http_server.h:544
bool_t useTls
Deprecated flag.
Definition http_server.h:550
HttpConnection * connections
Client connections.
Definition http_server.h:546
HttpCgiCallback cgiCallback
CGI callback function.
Definition http_server.h:557
uint16_t port
HTTP server port number.
Definition http_server.h:542
IpAddr ipAddr
HTTP server IP address.
Definition http_server.h:543
HttpRequestCallback requestCallback
HTTP request callback function.
Definition http_server.h:558
HttpUriNotFoundCallback uriNotFoundCallback
URI not found callback function.
Definition http_server.h:559
HttpRandCallback randCallback
Random data generation callback function.
Definition http_server.h:554
HTTP status code.
Definition http_server.h:425
IP network address.
Definition ip.h:72
HTTP connection.
Definition http_server.h:609
HttpServerSettings * settings
Reference to the HTTP server settings.
Definition http_server.h:610
Socket * socket
Socket.
Definition http_server.h:619
HttpConnState state
Connection state.
Definition http_server.h:630
OsTaskTcb taskTcb
Task control block.
Definition http_server.h:616
char_t cgiParam[HTTP_SERVER_CGI_PARAM_MAX_LEN+1]
CGI parameter.
Definition http_server.h:626
HttpRequest request
Incoming HTTP request header.
Definition http_server.h:623
OsStackType taskStack[HTTP_SERVER_STACK_SIZE]
Task stack.
Definition http_server.h:617
char_t buffer[HTTP_SERVER_BUFFER_SIZE]
Memory buffer for input/output operations.
Definition http_server.h:628
HttpResponse response
HTTP response header.
Definition http_server.h:624
HttpAccessStatus status
Access status.
Definition http_server.h:625
TlsContext * tlsContext
TLS context.
Definition http_server.h:621
HttpServerContext * serverContext
Reference to the HTTP server context.
Definition http_server.h:611
uint32_t dummy
Force alignment of the buffer on 32-bit boundaries.
Definition http_server.h:627
OsTaskId taskId
Task identifier.
Definition http_server.h:614
HTTP server context.
Definition http_server.h:580
OsTaskId taskId
Task identifier.
Definition http_server.h:583
TlsTicketContext tlsTicketContext
TLS ticket encryption context.
Definition http_server.h:591
HttpConnection * connections
Client connections.
Definition http_server.h:589
OsTaskTcb taskTcb
Task control block.
Definition http_server.h:585
OsMutex nonceCacheMutex
Mutex preventing simultaneous access to the nonce cache.
Definition http_server.h:594
HttpNonceCacheEntry nonceCache[HTTP_SERVER_NONCE_CACHE_SIZE]
Nonce cache.
Definition http_server.h:595
OsSemaphore semaphore
Semaphore limiting the number of connections.
Definition http_server.h:582
Socket * socket
Listening socket.
Definition http_server.h:588
OsStackType taskStack[HTTP_SERVER_STACK_SIZE]
Task stack.
Definition http_server.h:586
HttpServerSettings settings
User settings.
Definition http_server.h:581
WebSocket API (client and server)