Skip to content

Commit a783191

Browse files
Allow setting size when serving static files
1 parent 02c286c commit a783191

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

subsys/net/lib/http/Kconfig

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ if HTTP_SERVER
4444

4545
config HTTP_SERVER_STACK_SIZE
4646
int "HTTP server thread stack size"
47+
default 4096 if FILE_SYSTEM
4748
default 3072
4849
help
4950
HTTP server thread stack size for processing RX/TX events.
@@ -219,6 +220,17 @@ config HTTP_SERVER_COMPRESSION
219220
5. deflate -> .zz
220221
6. File without compression
221222

223+
config HTTP_SERVER_STATIC_FS_RESPONSE_SIZE
224+
int "Size of static file system response buffer"
225+
depends on FILE_SYSTEM
226+
default 1024
227+
help
228+
The size of a single chunk when serving static files from the file system.
229+
This config value must be large enough to hold the headers in a single chunk.
230+
If set to 0, the server will use the minimal viable buffer size for the response.
231+
Please note that it is allocated on the stack of the HTTP server thread,
232+
so CONFIG_HTTP_SERVER_STACK_SIZE has to be sufficiently large.
233+
222234
endif
223235

224236
# Hidden option to avoid having multiple individual options that are ORed together

subsys/net/lib/http/http_server_http1.c

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -482,12 +482,23 @@ int handle_http1_static_fs_resource(struct http_resource_detail_static_fs *stati
482482
sizeof("Content-Length: 01234567890123456789\r\n")
483483
#define CONTENT_ENCODING_HEADER_SIZE \
484484
sizeof(CONTENT_ENCODING_HEADER) + HTTP_COMPRESSION_MAX_STRING_LEN + sizeof("\r\n")
485+
486+
/* Calculate the minimum required size */
485487
#define STATIC_FS_RESPONSE_SIZE \
486488
COND_CODE_1( \
487489
IS_ENABLED(CONFIG_HTTP_SERVER_COMPRESSION), \
488490
(STATIC_FS_RESPONSE_BASE_SIZE + CONTENT_ENCODING_HEADER_SIZE), \
489491
(STATIC_FS_RESPONSE_BASE_SIZE))
490492

493+
// Issue in Zephyr: https://github.com/zephyrproject-rtos/zephyr/issues/92921
494+
#if CONFIG_HTTP_SERVER_STATIC_FS_RESPONSE_SIZE > 0
495+
BUILD_ASSERT(CONFIG_HTTP_SERVER_STATIC_FS_RESPONSE_SIZE >= STATIC_FS_RESPONSE_SIZE,
496+
"CONFIG_HTTP_SERVER_STATIC_FS_RESPONSE_SIZE must be at least "
497+
"large enough to hold HTTP headers");
498+
#undef STATIC_FS_RESPONSE_SIZE
499+
#define STATIC_FS_RESPONSE_SIZE CONFIG_HTTP_SERVER_STATIC_FS_RESPONSE_SIZE
500+
#endif
501+
491502
enum http_compression chosen_compression = 0;
492503
int len;
493504
int remaining;
@@ -919,7 +930,7 @@ int handle_http1_request(struct http_client_ctx *client)
919930

920931
parsed = http_parser_execute(&client->parser, &client->parser_settings,
921932
client->cursor, client->data_len);
922-
933+
923934
if (parsed > client->data_len) {
924935
LOG_ERR("HTTP/1 parser error, too much data consumed");
925936
ret = -EBADMSG;

0 commit comments

Comments
 (0)