Go to the documentation of this file.
31 #ifndef _HTTP_SERVER_H
32 #define _HTTP_SERVER_H
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_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
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
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
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
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
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
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
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
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
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
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
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
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
229 #ifndef HTTP_SERVER_CGI_PARAM_MAX_LEN
230 #define HTTP_SERVER_CGI_PARAM_MAX_LEN 31
231 #elif (HTTP_SERVER_CGI_PARAM_MAX_LEN < 7)
232 #error HTTP_SERVER_CGI_PARAM_MAX_LEN parameter is not valid
236 #ifndef HTTP_SERVER_SSI_MAX_RECURSION
237 #define HTTP_SERVER_SSI_MAX_RECURSION 3
238 #elif (HTTP_SERVER_SSI_MAX_RECURSION < 1 || HTTP_SERVER_SSI_MAX_RECURSION > 8)
239 #error HTTP_SERVER_SSI_MAX_RECURSION parameter is not valid
243 #ifndef HTTP_SERVER_MAX_AGE
244 #define HTTP_SERVER_MAX_AGE 0
245 #elif (HTTP_SERVER_MAX_AGE < 0)
246 #error HTTP_SERVER_MAX_AGE parameter is not valid
250 #ifndef HTTP_SERVER_NONCE_CACHE_SIZE
251 #define HTTP_SERVER_NONCE_CACHE_SIZE 8
252 #elif (HTTP_SERVER_NONCE_CACHE_SIZE < 1)
253 #error HTTP_SERVER_NONCE_CACHE_SIZE parameter is not valid
257 #ifndef HTTP_SERVER_NONCE_LIFETIME
258 #define HTTP_SERVER_NONCE_LIFETIME 60000
259 #elif (HTTP_SERVER_NONCE_LIFETIME < 1000)
260 #error HTTP_SERVER_NONCE_LIFETIME parameter is not valid
264 #ifndef HTTP_SERVER_NONCE_SIZE
265 #define HTTP_SERVER_NONCE_SIZE 16
266 #elif (HTTP_SERVER_NONCE_SIZE < 1)
267 #error HTTP_SERVER_NONCE_SIZE parameter is not valid
271 #ifndef HTTP_SERVER_BOUNDARY_MAX_LEN
272 #define HTTP_SERVER_BOUNDARY_MAX_LEN 70
273 #elif (HTTP_SERVER_BOUNDARY_MAX_LEN < 1)
274 #error HTTP_SERVER_BOUNDARY_MAX_LEN parameter is not valid
278 #ifndef HTTP_SERVER_COOKIE_MAX_LEN
279 #define HTTP_SERVER_COOKIE_MAX_LEN 256
280 #elif (HTTP_SERVER_COOKIE_MAX_LEN < 1)
281 #error HTTP_SERVER_COOKIE_MAX_LEN parameter is not valid
285 #ifndef HTTP_SERVER_PRIVATE_CONTEXT
286 #define HTTP_SERVER_PRIVATE_CONTEXT
290 #if (HTTP_SERVER_FS_SUPPORT == ENABLED)
297 #if (HTTP_SERVER_TLS_SUPPORT == ENABLED)
304 #if (HTTP_SERVER_BASIC_AUTH_SUPPORT == ENABLED)
310 #if (HTTP_SERVER_DIGEST_AUTH_SUPPORT == ENABLED)
316 #if (HTTP_SERVER_WEB_SOCKET_SUPPORT == ENABLED)
324 #define HTTPS_PORT 443
328 #define HttpServerContext struct _HttpServerContext
332 #define HttpConnection struct _HttpConnection
372 #define HTTP_FLAG_BREAK(c) (HTTP_FLAG_BREAK_CHAR | LSB(c))
376 #if (HTTP_SERVER_TLS_SUPPORT == ENABLED)
447 #if (HTTP_SERVER_BASIC_AUTH_SUPPORT == ENABLED)
450 #if (HTTP_SERVER_DIGEST_AUTH_SUPPORT == ENABLED)
470 #if (HTTP_SERVER_DIGEST_AUTH_SUPPORT == ENABLED)
493 #if (HTTP_SERVER_BASIC_AUTH_SUPPORT == ENABLED || HTTP_SERVER_DIGEST_AUTH_SUPPORT == ENABLED)
496 #if (HTTP_SERVER_WEB_SOCKET_SUPPORT == ENABLED)
501 #if (HTTP_SERVER_GZIP_TYPE_SUPPORT == ENABLED)
504 #if (HTTP_SERVER_MULTIPART_TYPE_SUPPORT == ENABLED)
508 #if (HTTP_SERVER_COOKIE_SUPPORT == ENABLED)
530 #if (HTTP_SERVER_BASIC_AUTH_SUPPORT == ENABLED || HTTP_SERVER_DIGEST_AUTH_SUPPORT == ENABLED)
533 #if (HTTP_SERVER_GZIP_TYPE_SUPPORT == ENABLED)
536 #if (HTTP_SERVER_COOKIE_SUPPORT == ENABLED)
558 #if (HTTP_SERVER_TLS_SUPPORT == ENABLED)
562 #if (HTTP_SERVER_BASIC_AUTH_SUPPORT == ENABLED || HTTP_SERVER_DIGEST_AUTH_SUPPORT == ENABLED)
596 #if (HTTP_SERVER_TLS_SUPPORT == ENABLED && TLS_TICKET_SUPPORT == ENABLED)
599 #if (HTTP_SERVER_DIGEST_AUTH_SUPPORT == ENABLED)
623 #if (HTTP_SERVER_TLS_SUPPORT == ENABLED)
632 #if (NET_RTOS_SUPPORT == DISABLED)
681 char_t *output,
size_t outputSize);
void httpServerGetDefaultSettings(HttpServerSettings *settings)
Initialize settings with default values.
error_t(* HttpCgiCallback)(HttpConnection *connection, const char_t *param)
CGI callback function.
HttpConnection * connections
Client connections.
OsSemaphore semaphore
Semaphore limiting the number of connections.
HttpAuthorizationHeader auth
Authorization header.
WebSocket * httpUpgradeToWebSocket(HttpConnection *connection)
Upgrade an existing HTTP connection to a WebSocket.
#define HTTP_SERVER_NONCE_SIZE
error_t(* TlsInitCallback)(HttpConnection *connection, TlsContext *tlsContext)
TLS initialization callback function.
#define HTTP_SERVER_COOKIE_MAX_LEN
HttpResponse response
HTTP response header.
#define WEB_SOCKET_CLIENT_KEY_SIZE
bool_t useTls
Deprecated flag.
HttpServerSettings settings
User settings.
#define HTTP_SERVER_PRIVATE_CONTEXT
systime_t timestamp
Time stamp to manage entry lifetime.
HttpRandCallback randCallback
Random data generation callback function.
WebSocket API (client and server)
#define HttpServerContext
HttpRequestCallback requestCallback
HTTP request callback function.
#define HTTP_SERVER_HOST_MAX_LEN
HttpConnection * connections
Client connections.
#define HTTP_SERVER_URI_MAX_LEN
char_t cgiParam[HTTP_SERVER_CGI_PARAM_MAX_LEN+1]
CGI parameter.
HttpAccessStatus status
Access status.
error_t httpServerStart(HttpServerContext *context)
Start HTTP server.
Socket * socket
Listening socket.
HttpRequest request
Incoming HTTP request header.
error_t(* HttpRequestCallback)(HttpConnection *connection, const char_t *uri)
HTTP request callback function.
Session ticket encryption context.
TlsInitCallback tlsInitCallback
TLS initialization callback function.
HttpAccessStatus(* HttpAuthCallback)(HttpConnection *connection, const char_t *user, const char_t *uri)
HTTP authentication callback function.
uint_t statusCode
HTTP status code.
error_t(* HttpUriNotFoundCallback)(HttpConnection *connection, const char_t *uri)
URI not found callback function.
@ HTTP_ACCESS_DIGEST_AUTH_REQUIRED
@ HTTP_CONN_STATE_REQ_BODY
#define HTTP_SERVER_NONCE_CACHE_SIZE
OsMutex nonceCacheMutex
Mutex preventing simultaneous access to the nonce cache.
OsTaskParameters taskParams
Task parameters.
@ HTTP_CONN_STATE_RESP_BODY
HttpAccessStatus
Access status.
#define HTTP_SERVER_ROOT_DIR_MAX_LEN
uint32_t dummy
Force alignment of the buffer on 32-bit boundaries.
@ HTTP_CONN_STATE_RESP_HEADER
General definitions for cryptographic algorithms.
bool_t acceptGzipEncoding
#define HTTP_SERVER_MAX_CONNECTIONS
uint16_t port
HTTP server port number.
TlsContext * tlsContext
TLS context.
#define HTTP_SERVER_USERNAME_MAX_LEN
HttpNonceCacheEntry nonceCache[HTTP_SERVER_NONCE_CACHE_SIZE]
Nonce cache.
HttpServerSettings * settings
Reference to the HTTP server settings.
OsTaskParameters listenerTask
Listener task parameters.
File system abstraction layer.
IpAddr ipAddr
HTTP server IP address.
uint_t backlog
Maximum length of the pending connection queue.
HttpCgiCallback cgiCallback
CGI callback function.
@ HTTP_CONN_STATE_REQ_HEADER
uint32_t count
Nonce count.
uint32_t systime_t
System time.
Definitions common to HTTP client and server.
OsTaskId taskId
Task identifier.
TlsTicketContext tlsTicketContext
TLS ticket encryption context.
error_t httpCloseStream(HttpConnection *connection)
Close output stream.
uint_t version
HTTP version number.
@ HTTP_CONN_STATE_REQ_LINE
error_t httpReadStream(HttpConnection *connection, void *data, size_t size, size_t *received, uint_t flags)
Read data from client request.
uint_t version
HTTP version number.
OsTaskParameters taskParams
Task parameters.
error_t httpWriteStream(HttpConnection *connection, const void *data, size_t length)
Write data to the client.
error_t httpSendRedirectResponse(HttpConnection *connection, uint_t statusCode, const char_t *uri)
Send redirect response to the client.
void httpConnectionTask(void *param)
Task that services requests from an active connection.
@ HTTP_CONN_STATE_SHUTDOWN
MD5 (Message-Digest Algorithm)
HttpAuthCallback authCallback
HTTP authentication callback function.
error_t httpSendResponse(HttpConnection *connection, const char_t *uri)
Send HTTP response.
#define HTTP_SERVER_QUERY_STRING_MAX_LEN
bool_t httpCheckPassword(HttpConnection *connection, const char_t *password, HttpAuthMode mode)
Password verification.
#define HTTP_SERVER_METHOD_MAX_LEN
uint_t maxConnections
Maximum number of client connections.
error_t(* HttpRandCallback)(uint8_t *data, size_t length)
Random data generation callback function.
NetInterface * interface
Underlying network interface.
const char_t * contentType
char_t buffer[HTTP_SERVER_BUFFER_SIZE]
Memory buffer for input/output operations.
HttpConnState state
Connection state.
TLS (Transport Layer Security)
OsTaskId taskId
Task identifier.
size_t boundaryLength
Boundary string length.
bool_t httpCheckWebSocketHandshake(HttpConnection *connection)
Check whether the client's handshake is valid.
#define HTTP_SERVER_BUFFER_SIZE
#define HTTP_SERVER_DEFAULT_DOC_MAX_LEN
error_t httpWriteHeader(HttpConnection *connection)
Send HTTP response header.
error_t httpDecodePercentEncodedString(const char_t *input, char_t *output, size_t outputSize)
Decode a percent-encoded string.
thread_t * OsTaskId
Task identifier.
HttpServerContext * serverContext
Reference to the HTTP server context.
Embedded resource management.
HttpConnState
HTTP connection states.
void httpListenerTask(void *param)
HTTP server listener task.
error_t httpServerInit(HttpServerContext *context, const HttpServerSettings *settings)
HTTP server initialization.
HttpAuthMode
HTTP authentication schemes.
HttpAuthenticateHeader auth
Authenticate header.
#define HTTP_SERVER_BOUNDARY_MAX_LEN
@ HTTP_ACCESS_BASIC_AUTH_REQUIRED
#define HTTP_SERVER_CGI_PARAM_MAX_LEN
error_t httpSendErrorResponse(HttpConnection *connection, uint_t statusCode, const char_t *message)
Send error response to the client.
HttpUriNotFoundCallback uriNotFoundCallback
URI not found callback function.