Skip to content

net: lib: http: allow configuring response chunk size #92971

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions doc/connectivity/networking/api/http_server.rst
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,10 @@ content type text/html.

HTTP_SERVER_CONTENT_TYPE(json, "application/json")

When serving files from a static filesystem, the response chunk size can be configured
using the :kconfig:option:`CONFIG_HTTP_SERVER_STATIC_FS_RESPONSE_SIZE` Kconfig option.
This determines the size of individual chunks when transmitting file content to clients.

Dynamic resources
=================

Expand Down
12 changes: 12 additions & 0 deletions subsys/net/lib/http/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ if HTTP_SERVER

config HTTP_SERVER_STACK_SIZE
int "HTTP server thread stack size"
default 4096 if FILE_SYSTEM
default 3072
help
HTTP server thread stack size for processing RX/TX events.
Expand Down Expand Up @@ -219,6 +220,17 @@ config HTTP_SERVER_COMPRESSION
5. deflate -> .zz
6. File without compression

config HTTP_SERVER_STATIC_FS_RESPONSE_SIZE
int "Size of static file system response buffer"
depends on FILE_SYSTEM
default 1024
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No stack overflow with such a large buffer? I wonder if it's needed to adjust the defaults for the server's stack size.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that it is indeed too small.
My current setting (and it's enough for sending things in 2KB chunks in both direction and serving ca. 1MB files) is
CONFIG_HTTP_SERVER_STACK_SIZE=8192

I'd probably suggest a default of at least 4096 (now it is 3072)

Copy link
Contributor Author

@AndreyDodonov-EH AndreyDodonov-EH Jul 10, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd probably go for defaults 4096 for HTTP_SERVER_STACK_SIZE and 512 for HTTP_SERVER_STATIC_FS_RESPONSE_SIZE

Copy link
Contributor

@rlubos rlubos Jul 10, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could have a conditional default 4096 if FILE_SYSTEM (just make sure it's first before the unconditional one). Current stack default was fine so far I think (i've seen no issues), so if we increase the stack by 1KB, I think we could have the buffer set to 1024.

help
The size of a single chunk when serving static files from the file system.
This config value must be large enough to hold the headers in a single chunk.
If set to 0, the server will use the minimal viable buffer size for the response.
Please note that it is allocated on the stack of the HTTP server thread,
so CONFIG_HTTP_SERVER_STACK_SIZE has to be sufficiently large.

endif

# Hidden option to avoid having multiple individual options that are ORed together
Expand Down
8 changes: 8 additions & 0 deletions subsys/net/lib/http/http_server_http1.c
Original file line number Diff line number Diff line change
Expand Up @@ -482,11 +482,19 @@ int handle_http1_static_fs_resource(struct http_resource_detail_static_fs *stati
sizeof("Content-Length: 01234567890123456789\r\n")
#define CONTENT_ENCODING_HEADER_SIZE \
sizeof(CONTENT_ENCODING_HEADER) + HTTP_COMPRESSION_MAX_STRING_LEN + sizeof("\r\n")
/* Calculate the minimum size required for the headers */
#define STATIC_FS_RESPONSE_SIZE \
COND_CODE_1( \
IS_ENABLED(CONFIG_HTTP_SERVER_COMPRESSION), \
(STATIC_FS_RESPONSE_BASE_SIZE + CONTENT_ENCODING_HEADER_SIZE), \
(STATIC_FS_RESPONSE_BASE_SIZE))
#if CONFIG_HTTP_SERVER_STATIC_FS_RESPONSE_SIZE > 0
BUILD_ASSERT(CONFIG_HTTP_SERVER_STATIC_FS_RESPONSE_SIZE >= STATIC_FS_RESPONSE_SIZE,
"CONFIG_HTTP_SERVER_STATIC_FS_RESPONSE_SIZE must be at least "
"large enough to hold HTTP headers");
#undef STATIC_FS_RESPONSE_SIZE
#define STATIC_FS_RESPONSE_SIZE CONFIG_HTTP_SERVER_STATIC_FS_RESPONSE_SIZE
#endif

enum http_compression chosen_compression = 0;
int len;
Expand Down