net: lib: http_server: add static fs resource

adds filesystem as a resource for the http_server which serves static
(gzipped) files from a filesystem to the client.

Signed-off-by: Gerhard Jörges <joerges@metratec.com>
This commit is contained in:
Gerhard Jörges 2024-07-26 10:54:42 +02:00 committed by Carles Cufí
commit e55278a87b
9 changed files with 351 additions and 9 deletions

View file

@ -187,6 +187,38 @@ following code to the application's ``CMakeLists.txt`` file:
where ``src/index.html`` is the location of the webpage to be compressed.
Static filesystem resources
===========================
Static filesystem resource content is defined build-time and is immutable. The following
example shows how the path can be defined as a static resource in the application:
.. code-block:: c
struct http_resource_detail_static_fs static_fs_resource_detail = {
.common = {
.type = HTTP_RESOURCE_TYPE_STATIC_FS,
.bitmask_of_supported_http_methods = BIT(HTTP_GET),
},
.fs_path = "/lfs1/www",
};
HTTP_RESOURCE_DEFINE(static_fs_resource, my_service, "*", &static_fs_resource_detail);
All files located in /lfs1/www are made available to the client. If a file is
gzipped, .gz must be appended to the file name (e.g. index.html.gz), then the
server delivers index.html.gz when the client requests index.html and adds gzip
content-encoding to the HTTP header.
The content type is evaluated based on the file extension. The server supports
.html, .js, .css, .jpg, .png and .svg. More content types can be provided with the
:c:macro:`HTTP_SERVER_CONTENT_TYPE` macro. All other files are provided with the
content type text/html.
.. code-block:: c
HTTP_SERVER_CONTENT_TYPE(json, "application/json")
Dynamic resources
=================