net: lib: http_server: Initial HTTP server support
Original code developed as a GSoC 2023 project by Emna Rekik. Code refactored in order to provide better bisectability as the origical commits were not bisectable. The server supports static and dynamic resources, managed by HTTP_SERVICE/HTTP_RESOURCE macros. Fixes #59685 Fixes #59686 Fixes #59688 Fixes #59690 Fixes #59670 Fixes #59700 Fixes #59684 Fixes #59693 Fixes #59693 Fixes #59694 Fixes #59699 Fixes #59696 Fixes #59688 Fixes #59690 Fixes #59670 Fixes #59700 Fixes #59685 Fixes #59686 Fixes #59688 Fixes #59691 Signed-off-by: Emna Rekik <emna.rekik007@gmail.com> Signed-off-by: Jukka Rissanen <jukka.rissanen@nordicsemi.no> Signed-off-by: Robert Lubos <robert.lubos@nordicsemi.no>
This commit is contained in:
parent
318dcb6336
commit
4b157b9099
|
@ -16,6 +16,7 @@
|
|||
|
||||
#if defined(CONFIG_HTTP_SERVER)
|
||||
ITERABLE_SECTION_ROM(http_service_desc, Z_LINK_ITERABLE_SUBALIGN)
|
||||
ITERABLE_SECTION_ROM(http_resource_desc, Z_LINK_ITERABLE_SUBALIGN)
|
||||
#endif
|
||||
|
||||
#if defined(CONFIG_COAP_SERVER)
|
||||
|
|
52
include/zephyr/net/http/frame.h
Normal file
52
include/zephyr/net/http/frame.h
Normal file
|
@ -0,0 +1,52 @@
|
|||
/*
|
||||
* Copyright (c) 2023, Emna Rekik
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
#ifndef ZEPHYR_INCLUDE_NET_HTTP_SERVER_FRAME_H_
|
||||
#define ZEPHYR_INCLUDE_NET_HTTP_SERVER_FRAME_H_
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
enum http_frame_type {
|
||||
HTTP_SERVER_DATA_FRAME = 0x00,
|
||||
HTTP_SERVER_HEADERS_FRAME = 0x01,
|
||||
HTTP_SERVER_PRIORITY_FRAME = 0x02,
|
||||
HTTP_SERVER_RST_STREAM_FRAME = 0x03,
|
||||
HTTP_SERVER_SETTINGS_FRAME = 0x04,
|
||||
HTTP_SERVER_PUSH_PROMISE_FRAME = 0x05,
|
||||
HTTP_SERVER_PING_FRAME = 0x06,
|
||||
HTTP_SERVER_GOAWAY_FRAME = 0x07,
|
||||
HTTP_SERVER_WINDOW_UPDATE_FRAME = 0x08,
|
||||
HTTP_SERVER_CONTINUATION_FRAME = 0x09
|
||||
};
|
||||
|
||||
#define HTTP_SERVER_HPACK_METHOD 0
|
||||
#define HTTP_SERVER_HPACK_PATH 1
|
||||
|
||||
#define HTTP_SERVER_FLAG_SETTINGS_ACK 0x1
|
||||
#define HTTP_SERVER_FLAG_END_HEADERS 0x4
|
||||
#define HTTP_SERVER_FLAG_END_STREAM 0x1
|
||||
|
||||
#define HTTP_SERVER_FRAME_HEADER_SIZE 9
|
||||
#define HTTP_SERVER_FRAME_LENGTH_OFFSET 0
|
||||
#define HTTP_SERVER_FRAME_TYPE_OFFSET 3
|
||||
#define HTTP_SERVER_FRAME_FLAGS_OFFSET 4
|
||||
#define HTTP_SERVER_FRAME_STREAM_ID_OFFSET 5
|
||||
|
||||
struct http_settings_field {
|
||||
uint16_t id;
|
||||
uint32_t value;
|
||||
} __packed;
|
||||
|
||||
enum http_settings {
|
||||
HTTP_SETTINGS_HEADER_TABLE_SIZE = 1,
|
||||
HTTP_SETTINGS_ENABLE_PUSH = 2,
|
||||
HTTP_SETTINGS_MAX_CONCURRENT_STREAMS = 3,
|
||||
HTTP_SETTINGS_INITIAL_WINDOW_SIZE = 4,
|
||||
HTTP_SETTINGS_MAX_FRAME_SIZE = 5,
|
||||
HTTP_SETTINGS_MAX_HEADER_LIST_SIZE = 6,
|
||||
};
|
||||
|
||||
#endif
|
|
@ -57,6 +57,8 @@ enum http_method {
|
|||
HTTP_MKCALENDAR = 30, /**< MKCALENDAR */
|
||||
HTTP_LINK = 31, /**< LINK */
|
||||
HTTP_UNLINK = 32, /**< UNLINK */
|
||||
|
||||
HTTP_METHOD_END_VALUE /* keep this the last value */
|
||||
};
|
||||
|
||||
#ifdef __cplusplus
|
||||
|
|
174
include/zephyr/net/http/server.h
Normal file
174
include/zephyr/net/http/server.h
Normal file
|
@ -0,0 +1,174 @@
|
|||
/*
|
||||
* Copyright (c) 2023, Emna Rekik
|
||||
* Copyright (c) 2024 Nordic Semiconductor ASA
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
#ifndef ZEPHYR_INCLUDE_NET_HTTP_SERVER_H_
|
||||
#define ZEPHYR_INCLUDE_NET_HTTP_SERVER_H_
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#include <zephyr/kernel.h>
|
||||
#include <zephyr/net/http/parser.h>
|
||||
#include <zephyr/net/http/hpack.h>
|
||||
#include <zephyr/net/socket.h>
|
||||
|
||||
#define HTTP_SERVER_CLIENT_BUFFER_SIZE CONFIG_HTTP_SERVER_CLIENT_BUFFER_SIZE
|
||||
#define HTTP_SERVER_MAX_STREAMS CONFIG_HTTP_SERVER_MAX_STREAMS
|
||||
#define HTTP_SERVER_MAX_CONTENT_TYPE_LEN CONFIG_HTTP_SERVER_MAX_CONTENT_TYPE_LENGTH
|
||||
|
||||
#define HTTP2_PREFACE "PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n"
|
||||
|
||||
enum http_resource_type {
|
||||
HTTP_RESOURCE_TYPE_STATIC,
|
||||
HTTP_RESOURCE_TYPE_DYNAMIC,
|
||||
};
|
||||
|
||||
struct http_resource_detail {
|
||||
uint32_t bitmask_of_supported_http_methods;
|
||||
enum http_resource_type type;
|
||||
int path_len; /* length of the URL path */
|
||||
const char *content_encoding;
|
||||
};
|
||||
BUILD_ASSERT(NUM_BITS(
|
||||
sizeof(((struct http_resource_detail *)0)->bitmask_of_supported_http_methods))
|
||||
>= (HTTP_METHOD_END_VALUE - 1));
|
||||
|
||||
struct http_resource_detail_static {
|
||||
struct http_resource_detail common;
|
||||
const void *static_data;
|
||||
size_t static_data_len;
|
||||
};
|
||||
|
||||
struct http_client_ctx;
|
||||
|
||||
/** Indicates the status of the currently processed piece of data. */
|
||||
enum http_data_status {
|
||||
/** Transaction aborted, data incomplete. */
|
||||
HTTP_SERVER_DATA_ABORTED = -1,
|
||||
/** Transaction incomplete, more data expected. */
|
||||
HTTP_SERVER_DATA_MORE = 0,
|
||||
/** Final data fragment in current transaction. */
|
||||
HTTP_SERVER_DATA_FINAL = 1,
|
||||
};
|
||||
|
||||
/**
|
||||
* @typedef http_resource_dynamic_cb_t
|
||||
* @brief Callback used when data is received. Data to be sent to client
|
||||
* can be specified.
|
||||
*
|
||||
* @param client HTTP context information for this client connection.
|
||||
* @param status HTTP data status, indicate whether more data is expected or not.
|
||||
* @param data_buffer Data received.
|
||||
* @param data_len Amount of data received.
|
||||
* @param user_data User specified data.
|
||||
*
|
||||
* @return >0 amount of data to be sent to client, let server to call this
|
||||
* function again when new data is received.
|
||||
* 0 nothing to sent to client, close the connection
|
||||
* <0 error, close the connection.
|
||||
*/
|
||||
typedef int (*http_resource_dynamic_cb_t)(struct http_client_ctx *client,
|
||||
enum http_data_status status,
|
||||
uint8_t *data_buffer,
|
||||
size_t data_len,
|
||||
void *user_data);
|
||||
|
||||
struct http_resource_detail_dynamic {
|
||||
struct http_resource_detail common;
|
||||
http_resource_dynamic_cb_t cb;
|
||||
uint8_t *data_buffer;
|
||||
size_t data_buffer_len;
|
||||
struct http_client_ctx *holder;
|
||||
void *user_data;
|
||||
};
|
||||
|
||||
struct http_resource_detail_rest {
|
||||
struct http_resource_detail common;
|
||||
};
|
||||
|
||||
enum http_stream_state {
|
||||
HTTP_SERVER_STREAM_IDLE,
|
||||
HTTP_SERVER_STREAM_RESERVED_LOCAL,
|
||||
HTTP_SERVER_STREAM_RESERVED_REMOTE,
|
||||
HTTP_SERVER_STREAM_OPEN,
|
||||
HTTP_SERVER_STREAM_HALF_CLOSED_LOCAL,
|
||||
HTTP_SERVER_STREAM_HALF_CLOSED_REMOTE,
|
||||
HTTP_SERVER_STREAM_CLOSED
|
||||
};
|
||||
|
||||
enum http_server_state {
|
||||
HTTP_SERVER_FRAME_HEADER_STATE,
|
||||
HTTP_SERVER_PREFACE_STATE,
|
||||
HTTP_SERVER_REQUEST_STATE,
|
||||
HTTP_SERVER_FRAME_DATA_STATE,
|
||||
HTTP_SERVER_FRAME_HEADERS_STATE,
|
||||
HTTP_SERVER_FRAME_SETTINGS_STATE,
|
||||
HTTP_SERVER_FRAME_PRIORITY_STATE,
|
||||
HTTP_SERVER_FRAME_WINDOW_UPDATE_STATE,
|
||||
HTTP_SERVER_FRAME_CONTINUATION_STATE,
|
||||
HTTP_SERVER_FRAME_PING_STATE,
|
||||
HTTP_SERVER_FRAME_RST_STREAM_STATE,
|
||||
HTTP_SERVER_FRAME_GOAWAY_STATE,
|
||||
HTTP_SERVER_DONE_STATE,
|
||||
};
|
||||
|
||||
enum http1_parser_state {
|
||||
HTTP1_INIT_HEADER_STATE,
|
||||
HTTP1_WAITING_HEADER_STATE,
|
||||
HTTP1_RECEIVING_HEADER_STATE,
|
||||
HTTP1_RECEIVED_HEADER_STATE,
|
||||
HTTP1_RECEIVING_DATA_STATE,
|
||||
HTTP1_MESSAGE_COMPLETE_STATE,
|
||||
};
|
||||
|
||||
#define HTTP_SERVER_INITIAL_WINDOW_SIZE 65536
|
||||
|
||||
struct http_stream_ctx {
|
||||
int stream_id;
|
||||
enum http_stream_state stream_state;
|
||||
int window_size; /**< Stream-level window size. */
|
||||
};
|
||||
|
||||
struct http_frame {
|
||||
uint32_t length;
|
||||
uint32_t stream_identifier;
|
||||
uint8_t type;
|
||||
uint8_t flags;
|
||||
uint8_t *payload;
|
||||
};
|
||||
|
||||
struct http_client_ctx {
|
||||
int fd;
|
||||
bool preface_sent;
|
||||
bool has_upgrade_header;
|
||||
unsigned char buffer[HTTP_SERVER_CLIENT_BUFFER_SIZE];
|
||||
unsigned char *cursor; /**< Cursor indicating currently processed byte. */
|
||||
size_t data_len; /**< Data left to process in the buffer. */
|
||||
int window_size; /**< Connection-level window size. */
|
||||
enum http_server_state server_state;
|
||||
struct http_frame current_frame;
|
||||
struct http_resource_detail *current_detail;
|
||||
struct http_hpack_header_buf header_field;
|
||||
struct http_stream_ctx streams[HTTP_SERVER_MAX_STREAMS];
|
||||
struct http_parser_settings parser_settings;
|
||||
struct http_parser parser;
|
||||
unsigned char url_buffer[CONFIG_HTTP_SERVER_MAX_URL_LENGTH];
|
||||
unsigned char content_type[CONFIG_HTTP_SERVER_MAX_CONTENT_TYPE_LENGTH];
|
||||
size_t content_len;
|
||||
enum http_method method;
|
||||
enum http1_parser_state parser_state;
|
||||
int http1_frag_data_len;
|
||||
bool headers_sent;
|
||||
struct k_work_delayable inactivity_timer;
|
||||
};
|
||||
|
||||
/* Starts the HTTP2 server */
|
||||
int http_server_start(void);
|
||||
|
||||
/* Stops the HTTP2 server */
|
||||
int http_server_stop(void);
|
||||
|
||||
#endif
|
|
@ -44,7 +44,7 @@ struct http_resource_desc {
|
|||
const STRUCT_SECTION_ITERABLE_ALTERNATE(http_resource_desc_##_service, http_resource_desc, \
|
||||
_name) = { \
|
||||
.resource = _resource, \
|
||||
.detail = (_detail), \
|
||||
.detail = (void *)(_detail), \
|
||||
}
|
||||
|
||||
struct http_service_desc {
|
||||
|
@ -55,12 +55,14 @@ struct http_service_desc {
|
|||
size_t backlog;
|
||||
struct http_resource_desc *res_begin;
|
||||
struct http_resource_desc *res_end;
|
||||
#if defined(CONFIG_NET_SOCKETS_SOCKOPT_TLS)
|
||||
const sec_tag_t *sec_tag_list;
|
||||
size_t sec_tag_list_size;
|
||||
#endif
|
||||
};
|
||||
|
||||
#define __z_http_service_define(_name, _host, _port, _concurrent, _backlog, _detail, _res_begin, \
|
||||
_res_end, ...) \
|
||||
#define __z_http_service_define(_name, _host, _port, _concurrent, _backlog, _detail, _res_begin, \
|
||||
_res_end, ...) \
|
||||
static const STRUCT_SECTION_ITERABLE(http_service_desc, _name) = { \
|
||||
.host = _host, \
|
||||
.port = (uint16_t *)(_port), \
|
||||
|
@ -69,10 +71,12 @@ struct http_service_desc {
|
|||
.backlog = (_backlog), \
|
||||
.res_begin = (_res_begin), \
|
||||
.res_end = (_res_end), \
|
||||
.sec_tag_list = COND_CODE_0(NUM_VA_ARGS_LESS_1(__VA_ARGS__), (NULL), \
|
||||
(GET_ARG_N(1, __VA_ARGS__))), \
|
||||
.sec_tag_list_size = COND_CODE_0(NUM_VA_ARGS_LESS_1(__VA_ARGS__), (0), \
|
||||
(GET_ARG_N(1, GET_ARGS_LESS_N(1, __VA_ARGS__)))), \
|
||||
COND_CODE_1(CONFIG_NET_SOCKETS_SOCKOPT_TLS, \
|
||||
(.sec_tag_list = COND_CODE_0(NUM_VA_ARGS_LESS_1(__VA_ARGS__), (NULL), \
|
||||
(GET_ARG_N(1, __VA_ARGS__))),), ()) \
|
||||
COND_CODE_1(CONFIG_NET_SOCKETS_SOCKOPT_TLS, \
|
||||
(.sec_tag_list_size = COND_CODE_0(NUM_VA_ARGS_LESS_1(__VA_ARGS__), (0),\
|
||||
(GET_ARG_N(1, GET_ARGS_LESS_N(1, __VA_ARGS__))))), ())\
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -27,7 +27,7 @@ if (CONFIG_DNS_RESOLVER
|
|||
add_subdirectory(dns)
|
||||
endif()
|
||||
|
||||
if(CONFIG_HTTP_PARSER_URL OR CONFIG_HTTP_PARSER OR CONFIG_HTTP_CLIENT)
|
||||
if(CONFIG_HTTP)
|
||||
add_subdirectory(http)
|
||||
endif()
|
||||
|
||||
|
|
|
@ -11,3 +11,8 @@ zephyr_include_directories(${ZEPHYR_BASE}/subsys/net/ip)
|
|||
zephyr_library_sources_ifdef(CONFIG_HTTP_PARSER http_parser.c)
|
||||
zephyr_library_sources_ifdef(CONFIG_HTTP_PARSER_URL http_parser_url.c)
|
||||
zephyr_library_sources_ifdef(CONFIG_HTTP_CLIENT http_client.c)
|
||||
zephyr_library_sources_ifdef(CONFIG_HTTP_SERVER http_server_core.c
|
||||
http_server_http1.c
|
||||
http_server_http2.c
|
||||
http_hpack.c
|
||||
http_huffman.c)
|
||||
|
|
|
@ -32,13 +32,96 @@ config HTTP_CLIENT
|
|||
|
||||
config HTTP_SERVER
|
||||
bool "HTTP Server [EXPERIMENTAL]"
|
||||
select WARN_EXPERIMENTAL
|
||||
select HTTP_PARSER
|
||||
select HTTP_PARSER_URL
|
||||
select EXPERIMENTAL
|
||||
help
|
||||
HTTP server support.
|
||||
Note: this is a work-in-progress
|
||||
HTTP1 and HTTP2 server support.
|
||||
|
||||
if HTTP_SERVER
|
||||
|
||||
config HTTP_SERVER_STACK_SIZE
|
||||
int "HTTP server thread stack size"
|
||||
default 3072
|
||||
help
|
||||
HTTP server thread stack size for processing RX/TX events.
|
||||
|
||||
config HTTP_SERVER_NUM_SERVICES
|
||||
int "Number of HTTP Server Instances"
|
||||
default 1
|
||||
range 1 100
|
||||
help
|
||||
This setting determines the number of http services that the server supports.
|
||||
|
||||
config HTTP_SERVER_MAX_CLIENTS
|
||||
int "Max number of HTTP/2 clients"
|
||||
default 3
|
||||
range 1 100
|
||||
help
|
||||
This setting determines the maximum number of HTTP/2 clients that the server can handle at once.
|
||||
|
||||
config HTTP_SERVER_MAX_STREAMS
|
||||
int "Max number of HTTP/2 streams"
|
||||
default 10
|
||||
range 1 100
|
||||
help
|
||||
This setting determines the maximum number of HTTP/2 streams for each client.
|
||||
|
||||
config HTTP_SERVER_CLIENT_BUFFER_SIZE
|
||||
int "Client Buffer Size"
|
||||
default 256
|
||||
range 64 1024
|
||||
help
|
||||
This setting determines the buffer size for each client.
|
||||
|
||||
config HTTP_SERVER_HUFFMAN_DECODE_BUFFER_SIZE
|
||||
int "Size of the buffer used for decoding Huffman-encoded strings"
|
||||
default 256
|
||||
range 64 1024
|
||||
help
|
||||
Size of the buffer used for decoding Huffman-encoded strings when
|
||||
processing HPACK compressed headers. This effectively limits the
|
||||
maximum length of an individual HTTP header supported.
|
||||
|
||||
config HTTP_SERVER_MAX_URL_LENGTH
|
||||
int "Maximum HTTP URL Length"
|
||||
default 256
|
||||
range 32 2048
|
||||
help
|
||||
This setting determines the maximum length of the HTTP URL that the server can process.
|
||||
|
||||
config HTTP_SERVER_MAX_CONTENT_TYPE_LENGTH
|
||||
int "Maximum HTTP Content-Type Length"
|
||||
default 64
|
||||
range 1 128
|
||||
help
|
||||
This setting determines the maximum length of the HTTP Content-Length field.
|
||||
|
||||
config HTTP_SERVER_CLIENT_INACTIVITY_TIMEOUT
|
||||
int "Client inactivity timeout (seconds)"
|
||||
default 10
|
||||
range 1 86400
|
||||
help
|
||||
This timeout specifies maximum time the client may remain inactive
|
||||
(i. e. not sending or receiving any data) before the server drops the
|
||||
connection.
|
||||
|
||||
endif
|
||||
|
||||
# Hidden option to avoid having multiple individual options that are ORed together
|
||||
config HTTP
|
||||
bool
|
||||
depends on (HTTP_PARSER_URL || HTTP_PARSER || HTTP_CLIENT || HTTP_SERVER)
|
||||
default y
|
||||
|
||||
module = NET_HTTP
|
||||
module-dep = NET_LOG
|
||||
module-str = Log level for HTTP client library
|
||||
module-help = Enables HTTP client code to output debug messages.
|
||||
source "subsys/net/Kconfig.template.log_config.net"
|
||||
|
||||
module = NET_HTTP_SERVER
|
||||
module-dep = NET_LOG
|
||||
module-str = Log level for HTTP server library
|
||||
module-help = Enables HTTP server code to output debug messages.
|
||||
source "subsys/net/Kconfig.template.log_config.net"
|
||||
|
|
18
subsys/net/lib/http/headers/mlog.h
Normal file
18
subsys/net/lib/http/headers/mlog.h
Normal file
|
@ -0,0 +1,18 @@
|
|||
/*
|
||||
* Copyright (c) 2023, Emna Rekik
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
#ifndef MLOG_H_
|
||||
#define MLOG_H_
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
#define LOG_MODULE_REGISTER(x, y)
|
||||
|
||||
#define LOG_INF(fmt, args...) printf("I: " fmt "\n", ##args)
|
||||
#define LOG_ERR(fmt, args...) printf("E: " fmt "\n", ##args)
|
||||
#define LOG_DBG(fmt, args...) printf("D: " fmt "\n", ##args)
|
||||
|
||||
#endif
|
45
subsys/net/lib/http/headers/server_internal.h
Normal file
45
subsys/net/lib/http/headers/server_internal.h
Normal file
|
@ -0,0 +1,45 @@
|
|||
/*
|
||||
* Copyright (c) 2023, Emna Rekik
|
||||
* Copyright (c) 2023 Nordic Semiconductor ASA
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
#ifndef HTTP_SERVER_INTERNAL_H_
|
||||
#define HTTP_SERVER_INTERNAL_H_
|
||||
|
||||
#include <stdbool.h>
|
||||
|
||||
#include <zephyr/net/http/server.h>
|
||||
#include <zephyr/net/http/service.h>
|
||||
#include <zephyr/net/http/status.h>
|
||||
#include <zephyr/net/http/hpack.h>
|
||||
#include <zephyr/net/http/frame.h>
|
||||
|
||||
/* HTTP1/HTTP2 state handling */
|
||||
int handle_http_frame_rst_frame(struct http_client_ctx *client);
|
||||
int handle_http_frame_goaway(struct http_client_ctx *client);
|
||||
int handle_http_frame_settings(struct http_client_ctx *client);
|
||||
int handle_http_frame_priority(struct http_client_ctx *client);
|
||||
int handle_http_frame_continuation(struct http_client_ctx *client);
|
||||
int handle_http_frame_window_update(struct http_client_ctx *client);
|
||||
int handle_http_frame_header(struct http_client_ctx *client);
|
||||
int handle_http_frame_headers(struct http_client_ctx *client);
|
||||
int handle_http_frame_data(struct http_client_ctx *client);
|
||||
int handle_http1_request(struct http_client_ctx *client);
|
||||
int handle_http1_to_http2_upgrade(struct http_client_ctx *client);
|
||||
|
||||
int enter_http1_request(struct http_client_ctx *client);
|
||||
int enter_http2_request(struct http_client_ctx *client);
|
||||
int enter_http_done_state(struct http_client_ctx *client);
|
||||
|
||||
/* Others */
|
||||
struct http_resource_detail *get_resource_detail(const char *path, int *len);
|
||||
int http_server_sendall(struct http_client_ctx *client, const void *buf, size_t len);
|
||||
void http_client_timer_restart(struct http_client_ctx *client);
|
||||
|
||||
/* TODO Could be static, but currently used in tests. */
|
||||
int parse_http_frame_header(struct http_client_ctx *client);
|
||||
const char *get_frame_type_name(enum http_frame_type type);
|
||||
|
||||
#endif /* HTTP_SERVER_INTERNAL_H_ */
|
|
@ -11,7 +11,7 @@
|
|||
*/
|
||||
|
||||
#include <zephyr/logging/log.h>
|
||||
LOG_MODULE_REGISTER(net_http, CONFIG_NET_HTTP_LOG_LEVEL);
|
||||
LOG_MODULE_REGISTER(net_http_client, CONFIG_NET_HTTP_LOG_LEVEL);
|
||||
|
||||
#include <zephyr/kernel.h>
|
||||
#include <string.h>
|
||||
|
|
723
subsys/net/lib/http/http_server_core.c
Normal file
723
subsys/net/lib/http/http_server_core.c
Normal file
|
@ -0,0 +1,723 @@
|
|||
/*
|
||||
* Copyright (c) 2023, Emna Rekik
|
||||
* Copyright (c) 2024 Nordic Semiconductor ASA
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
#include <errno.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <strings.h>
|
||||
|
||||
#include <zephyr/kernel.h>
|
||||
#include <zephyr/logging/log.h>
|
||||
#include <zephyr/net/http/service.h>
|
||||
#include <zephyr/net/net_ip.h>
|
||||
#include <zephyr/net/socket.h>
|
||||
#include <zephyr/net/tls_credentials.h>
|
||||
#include <zephyr/posix/sys/eventfd.h>
|
||||
|
||||
LOG_MODULE_REGISTER(net_http_server, CONFIG_NET_HTTP_SERVER_LOG_LEVEL);
|
||||
|
||||
#include "../../ip/net_private.h"
|
||||
#include "headers/server_internal.h"
|
||||
|
||||
#if defined(CONFIG_NET_TC_THREAD_COOPERATIVE)
|
||||
/* Lowest priority cooperative thread */
|
||||
#define THREAD_PRIORITY K_PRIO_COOP(CONFIG_NUM_COOP_PRIORITIES - 1)
|
||||
#else
|
||||
#define THREAD_PRIORITY K_PRIO_PREEMPT(CONFIG_NUM_PREEMPT_PRIORITIES - 1)
|
||||
#endif
|
||||
|
||||
#define INVALID_SOCK -1
|
||||
#define INACTIVITY_TIMEOUT K_SECONDS(CONFIG_HTTP_SERVER_CLIENT_INACTIVITY_TIMEOUT)
|
||||
|
||||
#define HTTP_SERVER_MAX_SERVICES CONFIG_HTTP_SERVER_NUM_SERVICES
|
||||
#define HTTP_SERVER_MAX_CLIENTS CONFIG_HTTP_SERVER_MAX_CLIENTS
|
||||
#define HTTP_SERVER_SOCK_COUNT (1 + HTTP_SERVER_MAX_SERVICES + HTTP_SERVER_MAX_CLIENTS)
|
||||
|
||||
struct http_server_ctx {
|
||||
int num_clients;
|
||||
int listen_fds; /* max value of 1 + MAX_SERVICES */
|
||||
|
||||
/* First pollfd is eventfd that can be used to stop the server,
|
||||
* then we have the server listen sockets,
|
||||
* and then the accepted sockets.
|
||||
*/
|
||||
struct zsock_pollfd fds[HTTP_SERVER_SOCK_COUNT];
|
||||
struct http_client_ctx clients[HTTP_SERVER_MAX_CLIENTS];
|
||||
};
|
||||
|
||||
static struct http_server_ctx server_ctx;
|
||||
static K_SEM_DEFINE(server_start, 0, 1);
|
||||
static bool server_running;
|
||||
|
||||
int http_server_init(struct http_server_ctx *ctx)
|
||||
{
|
||||
int proto;
|
||||
int failed = 0, count = 0;
|
||||
int svc_count;
|
||||
socklen_t len;
|
||||
int fd, af, i;
|
||||
struct sockaddr_storage addr_storage;
|
||||
const union {
|
||||
struct sockaddr *addr;
|
||||
struct sockaddr_in *addr4;
|
||||
struct sockaddr_in6 *addr6;
|
||||
} addr = {
|
||||
.addr = (struct sockaddr *)&addr_storage
|
||||
};
|
||||
|
||||
HTTP_SERVICE_COUNT(&svc_count);
|
||||
|
||||
/* Initialize fds */
|
||||
memset(ctx->fds, 0, sizeof(ctx->fds));
|
||||
memset(ctx->clients, 0, sizeof(ctx->clients));
|
||||
|
||||
for (i = 0; i < ARRAY_SIZE(ctx->fds); i++) {
|
||||
ctx->fds[i].fd = INVALID_SOCK;
|
||||
}
|
||||
|
||||
/* Create an eventfd that can be used to trigger events during polling */
|
||||
fd = eventfd(0, 0);
|
||||
if (fd < 0) {
|
||||
fd = -errno;
|
||||
LOG_ERR("eventfd failed (%d)", fd);
|
||||
return fd;
|
||||
}
|
||||
|
||||
ctx->fds[count].fd = fd;
|
||||
ctx->fds[count].events = ZSOCK_POLLIN;
|
||||
count++;
|
||||
|
||||
HTTP_SERVICE_FOREACH(svc) {
|
||||
/* set the default address (in6addr_any / INADDR_ANY are all 0) */
|
||||
memset(&addr_storage, 0, sizeof(struct sockaddr_storage));
|
||||
|
||||
/* Set up the server address struct according to address family */
|
||||
if (IS_ENABLED(CONFIG_NET_IPV6) &&
|
||||
zsock_inet_pton(AF_INET6, svc->host, &addr.addr6->sin6_addr) == 1) {
|
||||
af = AF_INET6;
|
||||
len = sizeof(*addr.addr6);
|
||||
|
||||
addr.addr6->sin6_family = AF_INET6;
|
||||
addr.addr6->sin6_port = htons(*svc->port);
|
||||
} else if (IS_ENABLED(CONFIG_NET_IPV4) &&
|
||||
zsock_inet_pton(AF_INET, svc->host, &addr.addr4->sin_addr) == 1) {
|
||||
af = AF_INET;
|
||||
len = sizeof(*addr.addr4);
|
||||
|
||||
addr.addr4->sin_family = AF_INET;
|
||||
addr.addr4->sin_port = htons(*svc->port);
|
||||
} else if (IS_ENABLED(CONFIG_NET_IPV6)) {
|
||||
/* prefer IPv6 if both IPv6 and IPv4 are supported */
|
||||
af = AF_INET6;
|
||||
len = sizeof(*addr.addr6);
|
||||
|
||||
addr.addr6->sin6_family = AF_INET6;
|
||||
addr.addr6->sin6_port = htons(*svc->port);
|
||||
} else if (IS_ENABLED(CONFIG_NET_IPV4)) {
|
||||
af = AF_INET;
|
||||
len = sizeof(*addr.addr4);
|
||||
|
||||
addr.addr4->sin_family = AF_INET;
|
||||
addr.addr4->sin_port = htons(*svc->port);
|
||||
} else {
|
||||
LOG_ERR("Neither IPv4 nor IPv6 is enabled");
|
||||
failed++;
|
||||
break;
|
||||
}
|
||||
|
||||
/* Create a socket */
|
||||
if (COND_CODE_1(CONFIG_NET_SOCKETS_SOCKOPT_TLS,
|
||||
(svc->sec_tag_list != NULL),
|
||||
(0))) {
|
||||
proto = IPPROTO_TLS_1_2;
|
||||
} else {
|
||||
proto = IPPROTO_TCP;
|
||||
}
|
||||
|
||||
fd = zsock_socket(af, SOCK_STREAM, proto);
|
||||
if (fd < 0) {
|
||||
LOG_ERR("socket: %d", errno);
|
||||
failed++;
|
||||
continue;
|
||||
}
|
||||
|
||||
#if defined(CONFIG_NET_SOCKETS_SOCKOPT_TLS)
|
||||
if (svc->sec_tag_list != NULL) {
|
||||
if (zsock_setsockopt(fd, SOL_TLS, TLS_SEC_TAG_LIST,
|
||||
svc->sec_tag_list,
|
||||
svc->sec_tag_list_size) < 0) {
|
||||
LOG_ERR("setsockopt: %d", errno);
|
||||
zsock_close(fd);
|
||||
continue;
|
||||
}
|
||||
|
||||
if (zsock_setsockopt(fd, SOL_TLS, TLS_HOSTNAME, "localhost",
|
||||
sizeof("localhost")) < 0) {
|
||||
LOG_ERR("setsockopt: %d", errno);
|
||||
zsock_close(fd);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
if (zsock_setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &(int){1},
|
||||
sizeof(int)) < 0) {
|
||||
LOG_ERR("setsockopt: %d", errno);
|
||||
zsock_close(fd);
|
||||
continue;
|
||||
}
|
||||
|
||||
if (zsock_bind(fd, addr.addr, len) < 0) {
|
||||
LOG_ERR("bind: %d", errno);
|
||||
failed++;
|
||||
zsock_close(fd);
|
||||
continue;
|
||||
}
|
||||
|
||||
if (*svc->port == 0) {
|
||||
/* ephemeral port - read back the port number */
|
||||
len = sizeof(addr_storage);
|
||||
if (zsock_getsockname(fd, addr.addr, &len) < 0) {
|
||||
LOG_ERR("getsockname: %d", errno);
|
||||
zsock_close(fd);
|
||||
continue;
|
||||
}
|
||||
|
||||
*svc->port = ntohs(addr.addr4->sin_port);
|
||||
}
|
||||
|
||||
if (zsock_listen(fd, HTTP_SERVER_MAX_CLIENTS) < 0) {
|
||||
LOG_ERR("listen: %d", errno);
|
||||
failed++;
|
||||
zsock_close(fd);
|
||||
continue;
|
||||
}
|
||||
|
||||
LOG_DBG("Initialized HTTP Service %s:%u", svc->host, *svc->port);
|
||||
|
||||
ctx->fds[count].fd = fd;
|
||||
ctx->fds[count].events = ZSOCK_POLLIN;
|
||||
count++;
|
||||
}
|
||||
|
||||
if (failed >= svc_count) {
|
||||
LOG_ERR("All services failed (%d)", failed);
|
||||
return -ESRCH;
|
||||
}
|
||||
|
||||
ctx->listen_fds = count;
|
||||
ctx->num_clients = 0;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int accept_new_client(int server_fd)
|
||||
{
|
||||
int new_socket;
|
||||
socklen_t addrlen;
|
||||
struct sockaddr_storage sa;
|
||||
|
||||
memset(&sa, 0, sizeof(sa));
|
||||
addrlen = sizeof(sa);
|
||||
|
||||
new_socket = zsock_accept(server_fd, (struct sockaddr *)&sa, &addrlen);
|
||||
if (new_socket < 0) {
|
||||
new_socket = -errno;
|
||||
LOG_DBG("[%d] accept failed (%d)", server_fd, new_socket);
|
||||
return new_socket;
|
||||
}
|
||||
|
||||
LOG_DBG("New client from %s:%d",
|
||||
net_sprint_addr(sa.ss_family, &net_sin((struct sockaddr *)&sa)->sin_addr),
|
||||
ntohs(net_sin((struct sockaddr *)&sa)->sin_port));
|
||||
|
||||
return new_socket;
|
||||
}
|
||||
|
||||
static int close_all_sockets(struct http_server_ctx *ctx)
|
||||
{
|
||||
zsock_close(ctx->fds[0].fd); /* close eventfd */
|
||||
ctx->fds[0].fd = -1;
|
||||
|
||||
for (int i = 1; i < ARRAY_SIZE(ctx->fds); i++) {
|
||||
if (ctx->fds[i].fd < 0) {
|
||||
continue;
|
||||
}
|
||||
|
||||
zsock_close(ctx->fds[i].fd);
|
||||
ctx->fds[i].fd = -1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void client_release_resources(struct http_client_ctx *client)
|
||||
{
|
||||
struct http_resource_detail *detail;
|
||||
struct http_resource_detail_dynamic *dynamic_detail;
|
||||
|
||||
HTTP_SERVICE_FOREACH(service) {
|
||||
HTTP_SERVICE_FOREACH_RESOURCE(service, resource) {
|
||||
detail = resource->detail;
|
||||
|
||||
if (detail->type != HTTP_RESOURCE_TYPE_DYNAMIC) {
|
||||
continue;
|
||||
}
|
||||
|
||||
dynamic_detail = (struct http_resource_detail_dynamic *)detail;
|
||||
|
||||
if (dynamic_detail->holder != client) {
|
||||
continue;
|
||||
}
|
||||
|
||||
/* If the client still holds the resource at this point,
|
||||
* it means the transaction was not complete. Release
|
||||
* the resource and notify application.
|
||||
*/
|
||||
dynamic_detail->holder = NULL;
|
||||
|
||||
if (dynamic_detail->cb == NULL) {
|
||||
continue;
|
||||
}
|
||||
|
||||
dynamic_detail->cb(client, HTTP_SERVER_DATA_ABORTED,
|
||||
NULL, 0, dynamic_detail->user_data);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void close_client_connection(struct http_client_ctx *client)
|
||||
{
|
||||
int i;
|
||||
struct k_work_sync sync;
|
||||
|
||||
__ASSERT_NO_MSG(IS_ARRAY_ELEMENT(server_ctx.clients, client));
|
||||
|
||||
k_work_cancel_delayable_sync(&client->inactivity_timer, &sync);
|
||||
zsock_close(client->fd);
|
||||
client_release_resources(client);
|
||||
|
||||
server_ctx.num_clients--;
|
||||
|
||||
for (i = server_ctx.listen_fds; i < ARRAY_SIZE(server_ctx.fds); i++) {
|
||||
if (server_ctx.fds[i].fd == client->fd) {
|
||||
server_ctx.fds[i].fd = INVALID_SOCK;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
memset(client, 0, sizeof(struct http_client_ctx));
|
||||
client->fd = INVALID_SOCK;
|
||||
|
||||
}
|
||||
|
||||
static void client_timeout(struct k_work *work)
|
||||
{
|
||||
struct k_work_delayable *dwork = k_work_delayable_from_work(work);
|
||||
struct http_client_ctx *client =
|
||||
CONTAINER_OF(dwork, struct http_client_ctx, inactivity_timer);
|
||||
|
||||
LOG_DBG("Client %p timeout", client);
|
||||
|
||||
/* Shutdown the socket. This will be detected by poll() and a proper
|
||||
* cleanup will proceed.
|
||||
*/
|
||||
(void)zsock_shutdown(client->fd, ZSOCK_SHUT_RD);
|
||||
}
|
||||
|
||||
void http_client_timer_restart(struct http_client_ctx *client)
|
||||
{
|
||||
__ASSERT_NO_MSG(IS_ARRAY_ELEMENT(server_ctx.clients, client));
|
||||
|
||||
k_work_reschedule(&client->inactivity_timer, INACTIVITY_TIMEOUT);
|
||||
}
|
||||
|
||||
static void init_client_ctx(struct http_client_ctx *client, int new_socket)
|
||||
{
|
||||
client->fd = new_socket;
|
||||
client->data_len = 0;
|
||||
client->server_state = HTTP_SERVER_PREFACE_STATE;
|
||||
client->has_upgrade_header = false;
|
||||
client->preface_sent = false;
|
||||
client->window_size = HTTP_SERVER_INITIAL_WINDOW_SIZE;
|
||||
|
||||
memset(client->buffer, 0, sizeof(client->buffer));
|
||||
memset(client->url_buffer, 0, sizeof(client->url_buffer));
|
||||
k_work_init_delayable(&client->inactivity_timer, client_timeout);
|
||||
http_client_timer_restart(client);
|
||||
|
||||
ARRAY_FOR_EACH(client->streams, i) {
|
||||
client->streams[i].stream_state = HTTP_SERVER_STREAM_IDLE;
|
||||
client->streams[i].stream_id = 0;
|
||||
}
|
||||
}
|
||||
|
||||
static int handle_http_preface(struct http_client_ctx *client)
|
||||
{
|
||||
LOG_DBG("HTTP_SERVER_PREFACE_STATE.");
|
||||
|
||||
if (client->data_len < sizeof(HTTP2_PREFACE) - 1) {
|
||||
/* We don't have full preface yet, get more data. */
|
||||
return -EAGAIN;
|
||||
}
|
||||
|
||||
if (strncmp(client->cursor, HTTP2_PREFACE, sizeof(HTTP2_PREFACE) - 1) != 0) {
|
||||
return enter_http1_request(client);
|
||||
}
|
||||
|
||||
return enter_http2_request(client);
|
||||
}
|
||||
|
||||
static int handle_http_done(struct http_client_ctx *client)
|
||||
{
|
||||
LOG_DBG("HTTP_SERVER_DONE_STATE");
|
||||
|
||||
close_client_connection(client);
|
||||
|
||||
return -EAGAIN;
|
||||
}
|
||||
|
||||
int enter_http_done_state(struct http_client_ctx *client)
|
||||
{
|
||||
close_client_connection(client);
|
||||
|
||||
client->server_state = HTTP_SERVER_DONE_STATE;
|
||||
|
||||
return -EAGAIN;
|
||||
}
|
||||
|
||||
static int handle_http_request(struct http_client_ctx *client)
|
||||
{
|
||||
int ret = -EINVAL;
|
||||
|
||||
client->cursor = client->buffer;
|
||||
|
||||
do {
|
||||
switch (client->server_state) {
|
||||
case HTTP_SERVER_FRAME_DATA_STATE:
|
||||
ret = handle_http_frame_data(client);
|
||||
break;
|
||||
case HTTP_SERVER_PREFACE_STATE:
|
||||
ret = handle_http_preface(client);
|
||||
break;
|
||||
case HTTP_SERVER_REQUEST_STATE:
|
||||
ret = handle_http1_request(client);
|
||||
break;
|
||||
case HTTP_SERVER_FRAME_HEADER_STATE:
|
||||
ret = handle_http_frame_header(client);
|
||||
break;
|
||||
case HTTP_SERVER_FRAME_HEADERS_STATE:
|
||||
ret = handle_http_frame_headers(client);
|
||||
break;
|
||||
case HTTP_SERVER_FRAME_CONTINUATION_STATE:
|
||||
ret = handle_http_frame_continuation(client);
|
||||
break;
|
||||
case HTTP_SERVER_FRAME_SETTINGS_STATE:
|
||||
ret = handle_http_frame_settings(client);
|
||||
break;
|
||||
case HTTP_SERVER_FRAME_WINDOW_UPDATE_STATE:
|
||||
ret = handle_http_frame_window_update(client);
|
||||
break;
|
||||
case HTTP_SERVER_FRAME_RST_STREAM_STATE:
|
||||
ret = handle_http_frame_rst_frame(client);
|
||||
break;
|
||||
case HTTP_SERVER_FRAME_GOAWAY_STATE:
|
||||
ret = handle_http_frame_goaway(client);
|
||||
break;
|
||||
case HTTP_SERVER_FRAME_PRIORITY_STATE:
|
||||
ret = handle_http_frame_priority(client);
|
||||
break;
|
||||
case HTTP_SERVER_DONE_STATE:
|
||||
ret = handle_http_done(client);
|
||||
break;
|
||||
default:
|
||||
ret = handle_http_done(client);
|
||||
break;
|
||||
}
|
||||
} while (ret >= 0 && client->data_len > 0);
|
||||
|
||||
if (ret < 0 && ret != -EAGAIN) {
|
||||
return ret;
|
||||
}
|
||||
|
||||
if (client->data_len > 0) {
|
||||
/* Move any remaining data in the buffer. */
|
||||
memmove(client->buffer, client->cursor, client->data_len);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int http_server_run(struct http_server_ctx *ctx)
|
||||
{
|
||||
struct http_client_ctx *client;
|
||||
eventfd_t value;
|
||||
bool found_slot;
|
||||
int new_socket;
|
||||
int ret, i, j;
|
||||
int sock_error;
|
||||
socklen_t optlen = sizeof(int);
|
||||
|
||||
value = 0;
|
||||
|
||||
while (1) {
|
||||
ret = zsock_poll(ctx->fds, HTTP_SERVER_SOCK_COUNT, -1);
|
||||
if (ret < 0) {
|
||||
ret = -errno;
|
||||
LOG_DBG("poll failed (%d)", ret);
|
||||
return ret;
|
||||
}
|
||||
|
||||
if (ret == 0) {
|
||||
/* should not happen because timeout is -1 */
|
||||
break;
|
||||
}
|
||||
|
||||
if (ret == 1 && ctx->fds[0].revents) {
|
||||
eventfd_read(ctx->fds[0].fd, &value);
|
||||
LOG_DBG("Received stop event. exiting ..");
|
||||
goto closing;
|
||||
}
|
||||
|
||||
for (i = 1; i < ARRAY_SIZE(ctx->fds); i++) {
|
||||
if (ctx->fds[i].fd < 0) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (ctx->fds[i].revents & ZSOCK_POLLHUP) {
|
||||
if (i >= ctx->listen_fds) {
|
||||
LOG_DBG("Client #%d has disconnected",
|
||||
i - ctx->listen_fds);
|
||||
|
||||
client = &ctx->clients[i - ctx->listen_fds];
|
||||
close_client_connection(client);
|
||||
}
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
if (ctx->fds[i].revents & ZSOCK_POLLERR) {
|
||||
(void)zsock_getsockopt(ctx->fds[i].fd, SOL_SOCKET,
|
||||
SO_ERROR, &sock_error, &optlen);
|
||||
LOG_DBG("Error on fd %d %d", ctx->fds[i].fd, sock_error);
|
||||
|
||||
if (i >= ctx->listen_fds) {
|
||||
client = &ctx->clients[i - ctx->listen_fds];
|
||||
close_client_connection(client);
|
||||
continue;
|
||||
}
|
||||
|
||||
/* Listening socket error, abort. */
|
||||
LOG_ERR("Listening socket error, aborting.");
|
||||
return -sock_error;
|
||||
|
||||
}
|
||||
|
||||
if (!(ctx->fds[i].revents & ZSOCK_POLLIN)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
/* First check if we have something to accept */
|
||||
if (i < ctx->listen_fds) {
|
||||
new_socket = accept_new_client(ctx->fds[i].fd);
|
||||
if (new_socket < 0) {
|
||||
ret = -errno;
|
||||
LOG_DBG("accept: %d", ret);
|
||||
continue;
|
||||
}
|
||||
|
||||
found_slot = false;
|
||||
|
||||
for (j = ctx->listen_fds; j < ARRAY_SIZE(ctx->fds); j++) {
|
||||
if (ctx->fds[j].fd != INVALID_SOCK) {
|
||||
continue;
|
||||
}
|
||||
|
||||
ctx->fds[j].fd = new_socket;
|
||||
ctx->fds[j].events = ZSOCK_POLLIN;
|
||||
ctx->fds[j].revents = 0;
|
||||
|
||||
ctx->num_clients++;
|
||||
|
||||
LOG_DBG("Init client #%d", j - ctx->listen_fds);
|
||||
|
||||
init_client_ctx(&ctx->clients[j - ctx->listen_fds],
|
||||
new_socket);
|
||||
found_slot = true;
|
||||
break;
|
||||
}
|
||||
|
||||
if (!found_slot) {
|
||||
LOG_DBG("No free slot found.");
|
||||
zsock_close(new_socket);
|
||||
}
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
/* Client sock */
|
||||
client = &ctx->clients[i - ctx->listen_fds];
|
||||
|
||||
ret = zsock_recv(client->fd, client->buffer + client->data_len,
|
||||
sizeof(client->buffer) - client->data_len, 0);
|
||||
if (ret <= 0) {
|
||||
if (ret == 0) {
|
||||
LOG_DBG("Connection closed by peer for client #%d",
|
||||
i - ctx->listen_fds);
|
||||
} else {
|
||||
ret = -errno;
|
||||
LOG_DBG("ERROR reading from socket (%d)", ret);
|
||||
}
|
||||
|
||||
close_client_connection(client);
|
||||
continue;
|
||||
}
|
||||
|
||||
client->data_len += ret;
|
||||
|
||||
http_client_timer_restart(client);
|
||||
|
||||
ret = handle_http_request(client);
|
||||
if (ret < 0 && ret != -EAGAIN) {
|
||||
if (ret == -ENOTCONN) {
|
||||
LOG_DBG("Client closed connection while handling request");
|
||||
} else {
|
||||
LOG_ERR("HTTP request handling error (%d)", ret);
|
||||
}
|
||||
close_client_connection(client);
|
||||
} else if (client->data_len == sizeof(client->buffer)) {
|
||||
/* If the RX buffer is still full after parsing,
|
||||
* it means we won't be able to handle this request
|
||||
* with the current buffer size.
|
||||
*/
|
||||
LOG_ERR("RX buffer too small to handle request");
|
||||
close_client_connection(client);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
|
||||
closing:
|
||||
/* Close all client connections and the server socket */
|
||||
return close_all_sockets(ctx);
|
||||
}
|
||||
|
||||
/* Compare two strings where the terminator is either "\0" or "?" */
|
||||
static int compare_strings(const char *s1, const char *s2)
|
||||
{
|
||||
while ((*s1 && *s2) && (*s1 == *s2) && (*s1 != '?')) {
|
||||
s1++;
|
||||
s2++;
|
||||
}
|
||||
|
||||
/* Check if both strings have reached their terminators or '?' */
|
||||
if ((*s1 == '\0' || *s1 == '?') && (*s2 == '\0' || *s2 == '?')) {
|
||||
return 0; /* Strings are equal */
|
||||
}
|
||||
|
||||
return 1; /* Strings are not equal */
|
||||
}
|
||||
|
||||
struct http_resource_detail *get_resource_detail(const char *path,
|
||||
int *path_len)
|
||||
{
|
||||
HTTP_SERVICE_FOREACH(service) {
|
||||
HTTP_SERVICE_FOREACH_RESOURCE(service, resource) {
|
||||
if (compare_strings(path, resource->resource) == 0) {
|
||||
NET_DBG("Got match for %s", resource->resource);
|
||||
|
||||
*path_len = strlen(resource->resource);
|
||||
return resource->detail;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
NET_DBG("No match for %s", path);
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
int http_server_sendall(struct http_client_ctx *client, const void *buf, size_t len)
|
||||
{
|
||||
while (len) {
|
||||
ssize_t out_len = zsock_send(client->fd, buf, len, 0);
|
||||
|
||||
if (out_len < 0) {
|
||||
return -errno;
|
||||
}
|
||||
|
||||
buf = (const char *)buf + out_len;
|
||||
len -= out_len;
|
||||
|
||||
http_client_timer_restart(client);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int http_server_start(void)
|
||||
{
|
||||
if (server_running) {
|
||||
LOG_DBG("HTTP server already started");
|
||||
return -EALREADY;
|
||||
}
|
||||
|
||||
server_running = true;
|
||||
k_sem_give(&server_start);
|
||||
|
||||
LOG_DBG("Starting HTTP server");
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int http_server_stop(void)
|
||||
{
|
||||
if (!server_running) {
|
||||
LOG_DBG("HTTP server already stopped");
|
||||
return -EALREADY;
|
||||
}
|
||||
|
||||
server_running = false;
|
||||
k_sem_reset(&server_start);
|
||||
eventfd_write(server_ctx.fds[0].fd, 1);
|
||||
|
||||
LOG_DBG("Stopping HTTP server");
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void http_server_thread(void *p1, void *p2, void *p3)
|
||||
{
|
||||
int ret;
|
||||
|
||||
ARG_UNUSED(p1);
|
||||
ARG_UNUSED(p2);
|
||||
ARG_UNUSED(p3);
|
||||
|
||||
while (true) {
|
||||
k_sem_take(&server_start, K_FOREVER);
|
||||
|
||||
while (server_running) {
|
||||
ret = http_server_init(&server_ctx);
|
||||
if (ret < 0) {
|
||||
LOG_ERR("Failed to initialize HTTP2 server");
|
||||
return;
|
||||
}
|
||||
|
||||
ret = http_server_run(&server_ctx);
|
||||
if (server_running) {
|
||||
LOG_INF("Re-starting server (%d)", ret);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
K_THREAD_DEFINE(http_server_tid, CONFIG_HTTP_SERVER_STACK_SIZE,
|
||||
http_server_thread, NULL, NULL, NULL, THREAD_PRIORITY, 0, 0);
|
491
subsys/net/lib/http/http_server_http1.c
Normal file
491
subsys/net/lib/http/http_server_http1.c
Normal file
|
@ -0,0 +1,491 @@
|
|||
/*
|
||||
* Copyright (c) 2023, Emna Rekik
|
||||
* Copyright (c) 2024 Nordic Semiconductor ASA
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
#include <errno.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <strings.h>
|
||||
|
||||
#include <zephyr/kernel.h>
|
||||
#include <zephyr/logging/log.h>
|
||||
#include <zephyr/net/http/service.h>
|
||||
|
||||
LOG_MODULE_DECLARE(net_http_server, CONFIG_NET_HTTP_SERVER_LOG_LEVEL);
|
||||
|
||||
#include "headers/server_internal.h"
|
||||
|
||||
#define TEMP_BUF_LEN 64
|
||||
|
||||
static const char final_chunk[] = "0\r\n\r\n";
|
||||
static const char *crlf = &final_chunk[3];
|
||||
|
||||
static int handle_http1_static_resource(
|
||||
struct http_resource_detail_static *static_detail,
|
||||
struct http_client_ctx *client)
|
||||
{
|
||||
#define RESPONSE_TEMPLATE \
|
||||
"HTTP/1.1 200 OK\r\n" \
|
||||
"Content-Type: text/html\r\n" \
|
||||
"Content-Length: %d\r\n"
|
||||
|
||||
/* Add couple of bytes to total response */
|
||||
char http_response[sizeof(RESPONSE_TEMPLATE) +
|
||||
sizeof("Content-Encoding: 01234567890123456789\r\n") +
|
||||
sizeof("xxxx") +
|
||||
sizeof("\r\n")];
|
||||
const char *data;
|
||||
int len;
|
||||
int ret;
|
||||
|
||||
if (static_detail->common.bitmask_of_supported_http_methods & BIT(HTTP_GET)) {
|
||||
data = static_detail->static_data;
|
||||
len = static_detail->static_data_len;
|
||||
|
||||
if (static_detail->common.content_encoding != NULL &&
|
||||
static_detail->common.content_encoding[0] != '\0') {
|
||||
snprintk(http_response, sizeof(http_response),
|
||||
RESPONSE_TEMPLATE "Content-Encoding: %s\r\n\r\n",
|
||||
len, static_detail->common.content_encoding);
|
||||
} else {
|
||||
snprintk(http_response, sizeof(http_response),
|
||||
RESPONSE_TEMPLATE "\r\n", len);
|
||||
}
|
||||
|
||||
ret = http_server_sendall(client, http_response,
|
||||
strlen(http_response));
|
||||
if (ret < 0) {
|
||||
return ret;
|
||||
}
|
||||
|
||||
ret = http_server_sendall(client, data, len);
|
||||
if (ret < 0) {
|
||||
return ret;
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
#define RESPONSE_TEMPLATE_CHUNKED \
|
||||
"HTTP/1.1 200 OK\r\n" \
|
||||
"Content-Type: text/html\r\n" \
|
||||
"Transfer-Encoding: chunked\r\n\r\n"
|
||||
|
||||
#define RESPONSE_TEMPLATE_DYNAMIC \
|
||||
"HTTP/1.1 200 OK\r\n" \
|
||||
"Content-Type: text/html\r\n\r\n" \
|
||||
|
||||
static int dynamic_get_req(struct http_resource_detail_dynamic *dynamic_detail,
|
||||
struct http_client_ctx *client)
|
||||
{
|
||||
/* offset tells from where the GET params start */
|
||||
int ret, remaining, offset = dynamic_detail->common.path_len;
|
||||
char *ptr;
|
||||
char tmp[TEMP_BUF_LEN];
|
||||
|
||||
ret = http_server_sendall(client, RESPONSE_TEMPLATE_CHUNKED,
|
||||
sizeof(RESPONSE_TEMPLATE_CHUNKED) - 1);
|
||||
if (ret < 0) {
|
||||
return ret;
|
||||
}
|
||||
|
||||
remaining = strlen(&client->url_buffer[dynamic_detail->common.path_len]);
|
||||
|
||||
/* Pass URL to the client */
|
||||
while (1) {
|
||||
int copy_len, send_len;
|
||||
enum http_data_status status;
|
||||
|
||||
ptr = &client->url_buffer[offset];
|
||||
copy_len = MIN(remaining, dynamic_detail->data_buffer_len);
|
||||
|
||||
memcpy(dynamic_detail->data_buffer, ptr, copy_len);
|
||||
|
||||
if (copy_len == remaining) {
|
||||
status = HTTP_SERVER_DATA_FINAL;
|
||||
} else {
|
||||
status = HTTP_SERVER_DATA_MORE;
|
||||
}
|
||||
|
||||
send_len = dynamic_detail->cb(client, status,
|
||||
dynamic_detail->data_buffer,
|
||||
copy_len, dynamic_detail->user_data);
|
||||
if (send_len > 0) {
|
||||
ret = snprintk(tmp, sizeof(tmp), "%x\r\n", send_len);
|
||||
ret = http_server_sendall(client, tmp, ret);
|
||||
if (ret < 0) {
|
||||
return ret;
|
||||
}
|
||||
|
||||
ret = http_server_sendall(client,
|
||||
dynamic_detail->data_buffer,
|
||||
send_len);
|
||||
if (ret < 0) {
|
||||
return ret;
|
||||
}
|
||||
|
||||
(void)http_server_sendall(client, crlf, 2);
|
||||
|
||||
offset += copy_len;
|
||||
remaining -= copy_len;
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
dynamic_detail->holder = NULL;
|
||||
|
||||
ret = http_server_sendall(client, final_chunk,
|
||||
sizeof(final_chunk) - 1);
|
||||
if (ret < 0) {
|
||||
return ret;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int dynamic_post_req(struct http_resource_detail_dynamic *dynamic_detail,
|
||||
struct http_client_ctx *client)
|
||||
{
|
||||
/* offset tells from where the POST params start */
|
||||
char *start = client->cursor;
|
||||
int ret, remaining = client->data_len, offset = 0;
|
||||
int copy_len;
|
||||
char *ptr;
|
||||
char tmp[TEMP_BUF_LEN];
|
||||
|
||||
if (start == NULL) {
|
||||
return -ENOENT;
|
||||
}
|
||||
|
||||
if (!client->headers_sent) {
|
||||
ret = http_server_sendall(client, RESPONSE_TEMPLATE_CHUNKED,
|
||||
sizeof(RESPONSE_TEMPLATE_CHUNKED) - 1);
|
||||
if (ret < 0) {
|
||||
return ret;
|
||||
}
|
||||
client->headers_sent = true;
|
||||
}
|
||||
|
||||
copy_len = MIN(remaining, dynamic_detail->data_buffer_len);
|
||||
while (copy_len > 0) {
|
||||
enum http_data_status status;
|
||||
int send_len;
|
||||
|
||||
ptr = &start[offset];
|
||||
|
||||
memcpy(dynamic_detail->data_buffer, ptr, copy_len);
|
||||
|
||||
if (copy_len == remaining &&
|
||||
client->parser_state == HTTP1_MESSAGE_COMPLETE_STATE) {
|
||||
status = HTTP_SERVER_DATA_FINAL;
|
||||
} else {
|
||||
status = HTTP_SERVER_DATA_MORE;
|
||||
}
|
||||
|
||||
send_len = dynamic_detail->cb(client, status,
|
||||
dynamic_detail->data_buffer,
|
||||
copy_len, dynamic_detail->user_data);
|
||||
if (send_len > 0) {
|
||||
ret = snprintk(tmp, sizeof(tmp), "%x\r\n", send_len);
|
||||
ret = http_server_sendall(client, tmp, ret);
|
||||
if (ret < 0) {
|
||||
return ret;
|
||||
}
|
||||
|
||||
ret = http_server_sendall(client,
|
||||
dynamic_detail->data_buffer,
|
||||
send_len);
|
||||
if (ret < 0) {
|
||||
return ret;
|
||||
}
|
||||
|
||||
(void)http_server_sendall(client, crlf, 2);
|
||||
|
||||
offset += copy_len;
|
||||
remaining -= copy_len;
|
||||
}
|
||||
|
||||
copy_len = MIN(remaining, dynamic_detail->data_buffer_len);
|
||||
}
|
||||
|
||||
if (client->parser_state == HTTP1_MESSAGE_COMPLETE_STATE) {
|
||||
ret = http_server_sendall(client, final_chunk,
|
||||
sizeof(final_chunk) - 1);
|
||||
if (ret < 0) {
|
||||
return ret;
|
||||
}
|
||||
|
||||
dynamic_detail->holder = NULL;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int handle_http1_dynamic_resource(
|
||||
struct http_resource_detail_dynamic *dynamic_detail,
|
||||
struct http_client_ctx *client)
|
||||
{
|
||||
uint32_t user_method;
|
||||
int ret;
|
||||
|
||||
if (dynamic_detail->cb == NULL) {
|
||||
return -ESRCH;
|
||||
}
|
||||
|
||||
user_method = dynamic_detail->common.bitmask_of_supported_http_methods;
|
||||
|
||||
if (!(BIT(client->method) & user_method)) {
|
||||
return -ENOPROTOOPT;
|
||||
}
|
||||
|
||||
if (dynamic_detail->holder != NULL && dynamic_detail->holder != client) {
|
||||
static const char conflict_response[] =
|
||||
"HTTP/1.1 409 Conflict\r\n\r\n";
|
||||
|
||||
ret = http_server_sendall(client, conflict_response,
|
||||
sizeof(conflict_response) - 1);
|
||||
if (ret < 0) {
|
||||
LOG_DBG("Cannot write to socket (%d)", ret);
|
||||
return ret;
|
||||
}
|
||||
|
||||
return enter_http_done_state(client);
|
||||
}
|
||||
|
||||
dynamic_detail->holder = client;
|
||||
|
||||
switch (client->method) {
|
||||
case HTTP_HEAD:
|
||||
if (user_method & BIT(HTTP_HEAD)) {
|
||||
ret = http_server_sendall(
|
||||
client, RESPONSE_TEMPLATE_DYNAMIC,
|
||||
sizeof(RESPONSE_TEMPLATE_DYNAMIC) - 1);
|
||||
if (ret < 0) {
|
||||
return ret;
|
||||
}
|
||||
|
||||
dynamic_detail->holder = NULL;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
case HTTP_GET:
|
||||
/* For GET request, we do not pass any data to the app but let the app
|
||||
* send data to the peer.
|
||||
*/
|
||||
if (user_method & BIT(HTTP_GET)) {
|
||||
return dynamic_get_req(dynamic_detail, client);
|
||||
}
|
||||
|
||||
goto not_supported;
|
||||
|
||||
case HTTP_POST:
|
||||
if (user_method & BIT(HTTP_POST)) {
|
||||
return dynamic_post_req(dynamic_detail, client);
|
||||
}
|
||||
|
||||
goto not_supported;
|
||||
|
||||
not_supported:
|
||||
default:
|
||||
LOG_DBG("HTTP method %s (%d) not supported.",
|
||||
http_method_str(client->method),
|
||||
client->method);
|
||||
|
||||
return -ENOTSUP;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int on_header_field(struct http_parser *parser, const char *at,
|
||||
size_t length)
|
||||
{
|
||||
struct http_client_ctx *ctx = CONTAINER_OF(parser,
|
||||
struct http_client_ctx,
|
||||
parser);
|
||||
|
||||
ctx->parser_state = HTTP1_RECEIVING_HEADER_STATE;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int on_headers_complete(struct http_parser *parser)
|
||||
{
|
||||
struct http_client_ctx *ctx = CONTAINER_OF(parser,
|
||||
struct http_client_ctx,
|
||||
parser);
|
||||
|
||||
ctx->parser_state = HTTP1_RECEIVED_HEADER_STATE;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int on_url(struct http_parser *parser, const char *at, size_t length)
|
||||
{
|
||||
struct http_client_ctx *ctx = CONTAINER_OF(parser,
|
||||
struct http_client_ctx,
|
||||
parser);
|
||||
size_t offset = strlen(ctx->url_buffer);
|
||||
|
||||
ctx->parser_state = HTTP1_WAITING_HEADER_STATE;
|
||||
|
||||
if (offset + length > sizeof(ctx->url_buffer) - 1) {
|
||||
LOG_DBG("URL too long to handle");
|
||||
return -EMSGSIZE;
|
||||
}
|
||||
|
||||
memcpy(ctx->url_buffer + offset, at, length);
|
||||
offset += length;
|
||||
ctx->url_buffer[offset] = '\0';
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int on_body(struct http_parser *parser, const char *at, size_t length)
|
||||
{
|
||||
struct http_client_ctx *ctx = CONTAINER_OF(parser,
|
||||
struct http_client_ctx,
|
||||
parser);
|
||||
|
||||
ctx->parser_state = HTTP1_RECEIVING_DATA_STATE;
|
||||
|
||||
ctx->http1_frag_data_len += length;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int on_message_complete(struct http_parser *parser)
|
||||
{
|
||||
struct http_client_ctx *ctx = CONTAINER_OF(parser,
|
||||
struct http_client_ctx,
|
||||
parser);
|
||||
|
||||
ctx->parser_state = HTTP1_MESSAGE_COMPLETE_STATE;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int enter_http1_request(struct http_client_ctx *client)
|
||||
{
|
||||
client->server_state = HTTP_SERVER_REQUEST_STATE;
|
||||
|
||||
http_parser_init(&client->parser, HTTP_REQUEST);
|
||||
http_parser_settings_init(&client->parser_settings);
|
||||
|
||||
client->parser_settings.on_header_field = on_header_field;
|
||||
client->parser_settings.on_headers_complete = on_headers_complete;
|
||||
client->parser_settings.on_url = on_url;
|
||||
client->parser_settings.on_body = on_body;
|
||||
client->parser_settings.on_message_complete = on_message_complete;
|
||||
client->parser_state = HTTP1_INIT_HEADER_STATE;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int handle_http1_request(struct http_client_ctx *client)
|
||||
{
|
||||
int ret, path_len = 0;
|
||||
struct http_resource_detail *detail;
|
||||
bool skip_headers = (client->parser_state < HTTP1_RECEIVING_DATA_STATE);
|
||||
size_t parsed;
|
||||
|
||||
LOG_DBG("HTTP_SERVER_REQUEST");
|
||||
|
||||
client->http1_frag_data_len = 0;
|
||||
|
||||
parsed = http_parser_execute(&client->parser, &client->parser_settings,
|
||||
client->cursor, client->data_len);
|
||||
|
||||
if (parsed > client->data_len) {
|
||||
LOG_ERR("HTTP/1 parser error, too much data consumed");
|
||||
return -EBADMSG;
|
||||
}
|
||||
|
||||
if (client->parser.http_errno != HPE_OK) {
|
||||
LOG_ERR("HTTP/1 parsing error, %d", client->parser.http_errno);
|
||||
return -EBADMSG;
|
||||
}
|
||||
|
||||
if (client->parser_state < HTTP1_RECEIVED_HEADER_STATE) {
|
||||
client->cursor += parsed;
|
||||
client->data_len -= parsed;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
client->method = client->parser.method;
|
||||
client->has_upgrade_header = client->parser.upgrade;
|
||||
|
||||
if (skip_headers) {
|
||||
LOG_DBG("Requested URL: %s", client->url_buffer);
|
||||
|
||||
size_t frag_headers_len;
|
||||
|
||||
if (parsed < client->http1_frag_data_len) {
|
||||
return -EBADMSG;
|
||||
}
|
||||
|
||||
frag_headers_len = parsed - client->http1_frag_data_len;
|
||||
parsed -= frag_headers_len;
|
||||
|
||||
client->cursor += frag_headers_len;
|
||||
client->data_len -= frag_headers_len;
|
||||
}
|
||||
|
||||
if (client->has_upgrade_header) {
|
||||
return handle_http1_to_http2_upgrade(client);
|
||||
}
|
||||
|
||||
detail = get_resource_detail(client->url_buffer, &path_len);
|
||||
if (detail != NULL) {
|
||||
detail->path_len = path_len;
|
||||
|
||||
if (detail->type == HTTP_RESOURCE_TYPE_STATIC) {
|
||||
ret = handle_http1_static_resource(
|
||||
(struct http_resource_detail_static *)detail,
|
||||
client);
|
||||
if (ret < 0) {
|
||||
return ret;
|
||||
}
|
||||
} else if (detail->type == HTTP_RESOURCE_TYPE_DYNAMIC) {
|
||||
ret = handle_http1_dynamic_resource(
|
||||
(struct http_resource_detail_dynamic *)detail,
|
||||
client);
|
||||
if (ret < 0) {
|
||||
return ret;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
static const char not_found_response[] =
|
||||
"HTTP/1.1 404 Not Found\r\n"
|
||||
"Content-Length: 9\r\n\r\n"
|
||||
"Not Found";
|
||||
|
||||
ret = http_server_sendall(client, not_found_response,
|
||||
sizeof(not_found_response) - 1);
|
||||
if (ret < 0) {
|
||||
LOG_DBG("Cannot write to socket (%d)", ret);
|
||||
return ret;
|
||||
}
|
||||
}
|
||||
|
||||
client->cursor += parsed;
|
||||
client->data_len -= parsed;
|
||||
|
||||
if (client->parser_state == HTTP1_MESSAGE_COMPLETE_STATE) {
|
||||
LOG_DBG("Connection closed client %p", client);
|
||||
enter_http_done_state(client);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
1243
subsys/net/lib/http/http_server_http2.c
Normal file
1243
subsys/net/lib/http/http_server_http2.c
Normal file
File diff suppressed because it is too large
Load diff
Loading…
Reference in a new issue