Skip to content

Commit 96ffbc4

Browse files
Allow setting size when serving static files
1 parent e0f03f0 commit 96ffbc4

File tree

2 files changed

+25
-5
lines changed

2 files changed

+25
-5
lines changed

subsys/net/lib/http/Kconfig

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

4343
config HTTP_SERVER_STACK_SIZE
4444
int "HTTP server thread stack size"
45+
default 4096 if FILE_SYSTEM
4546
default 3072
4647
help
4748
HTTP server thread stack size for processing RX/TX events.
@@ -217,6 +218,17 @@ config HTTP_SERVER_COMPRESSION
217218
5. deflate -> .zz
218219
6. File without compression
219220

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

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

subsys/net/lib/http/http_server_http1.c

Lines changed: 13 additions & 5 deletions
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;
@@ -554,18 +565,15 @@ int handle_http1_static_fs_resource(struct http_resource_detail_static_fs *stati
554565
client->http1_headers_sent = true;
555566

556567
/* read and send file */
557-
// Issue in Zephyr: https://github.com/zephyrproject-rtos/zephyr/issues/92921
558-
char body_response[2036];
559-
LOG_WRN("STATIC_FS_RESPONSE_SIZE=%d", STATIC_FS_RESPONSE_SIZE);
560568
remaining = file_size;
561569
while (remaining > 0) {
562-
len = fs_read(&file, body_response, sizeof(body_response));
570+
len = fs_read(&file, http_response, sizeof(http_response));
563571
if (len < 0) {
564572
LOG_ERR("Filesystem read error (%d)", len);
565573
goto close;
566574
}
567575

568-
ret = http_server_sendall(client, body_response, len);
576+
ret = http_server_sendall(client, http_response, len);
569577
if (ret < 0) {
570578
goto close;
571579
}

0 commit comments

Comments
 (0)