|
| 1 | +/* |
| 2 | + * Copyright (c) 2023, Emna Rekik |
| 3 | + * Copyright (c) 2024 Nordic Semiconductor ASA |
| 4 | + * |
| 5 | + * SPDX-License-Identifier: Apache-2.0 |
| 6 | + */ |
| 7 | + |
| 8 | +#ifndef ZEPHYR_INCLUDE_NET_HTTP_SERVER_H_ |
| 9 | +#define ZEPHYR_INCLUDE_NET_HTTP_SERVER_H_ |
| 10 | + |
| 11 | +#include <stdint.h> |
| 12 | + |
| 13 | +#include <zephyr/kernel.h> |
| 14 | +#include <zephyr/net/http/parser.h> |
| 15 | +#include <zephyr/net/http/hpack.h> |
| 16 | +#include <zephyr/net/socket.h> |
| 17 | + |
| 18 | +#define HTTP_SERVER_CLIENT_BUFFER_SIZE CONFIG_HTTP_SERVER_CLIENT_BUFFER_SIZE |
| 19 | +#define HTTP_SERVER_MAX_STREAMS CONFIG_HTTP_SERVER_MAX_STREAMS |
| 20 | +#define HTTP_SERVER_MAX_CONTENT_TYPE_LEN CONFIG_HTTP_SERVER_MAX_CONTENT_TYPE_LENGTH |
| 21 | + |
| 22 | +#define HTTP2_PREFACE "PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n" |
| 23 | + |
| 24 | +enum http_resource_type { |
| 25 | + HTTP_RESOURCE_TYPE_STATIC, |
| 26 | + HTTP_RESOURCE_TYPE_DYNAMIC, |
| 27 | +}; |
| 28 | + |
| 29 | +struct http_resource_detail { |
| 30 | + uint32_t bitmask_of_supported_http_methods; |
| 31 | + enum http_resource_type type; |
| 32 | + int path_len; /* length of the URL path */ |
| 33 | + const char *content_encoding; |
| 34 | +}; |
| 35 | +BUILD_ASSERT(NUM_BITS( |
| 36 | + sizeof(((struct http_resource_detail *)0)->bitmask_of_supported_http_methods)) |
| 37 | + >= (HTTP_METHOD_END_VALUE - 1)); |
| 38 | + |
| 39 | +struct http_resource_detail_static { |
| 40 | + struct http_resource_detail common; |
| 41 | + const void *static_data; |
| 42 | + size_t static_data_len; |
| 43 | +}; |
| 44 | + |
| 45 | +struct http_client_ctx; |
| 46 | + |
| 47 | +/** Indicates the status of the currently processed piece of data. */ |
| 48 | +enum http_data_status { |
| 49 | + /** Transaction aborted, data incomplete. */ |
| 50 | + HTTP_SERVER_DATA_ABORTED = -1, |
| 51 | + /** Transaction incomplete, more data expected. */ |
| 52 | + HTTP_SERVER_DATA_MORE = 0, |
| 53 | + /** Final data fragment in current transaction. */ |
| 54 | + HTTP_SERVER_DATA_FINAL = 1, |
| 55 | +}; |
| 56 | + |
| 57 | +/** |
| 58 | + * @typedef http_resource_dynamic_cb_t |
| 59 | + * @brief Callback used when data is received. Data to be sent to client |
| 60 | + * can be specified. |
| 61 | + * |
| 62 | + * @param client HTTP context information for this client connection. |
| 63 | + * @param status HTTP data status, indicate whether more data is expected or not. |
| 64 | + * @param data_buffer Data received. |
| 65 | + * @param data_len Amount of data received. |
| 66 | + * @param user_data User specified data. |
| 67 | + * |
| 68 | + * @return >0 amount of data to be sent to client, let server to call this |
| 69 | + * function again when new data is received. |
| 70 | + * 0 nothing to sent to client, close the connection |
| 71 | + * <0 error, close the connection. |
| 72 | + */ |
| 73 | +typedef int (*http_resource_dynamic_cb_t)(struct http_client_ctx *client, |
| 74 | + enum http_data_status status, |
| 75 | + uint8_t *data_buffer, |
| 76 | + size_t data_len, |
| 77 | + void *user_data); |
| 78 | + |
| 79 | +struct http_resource_detail_dynamic { |
| 80 | + struct http_resource_detail common; |
| 81 | + http_resource_dynamic_cb_t cb; |
| 82 | + uint8_t *data_buffer; |
| 83 | + size_t data_buffer_len; |
| 84 | + struct http_client_ctx *holder; |
| 85 | + void *user_data; |
| 86 | +}; |
| 87 | + |
| 88 | +struct http_resource_detail_rest { |
| 89 | + struct http_resource_detail common; |
| 90 | +}; |
| 91 | + |
| 92 | +enum http_stream_state { |
| 93 | + HTTP_SERVER_STREAM_IDLE, |
| 94 | + HTTP_SERVER_STREAM_RESERVED_LOCAL, |
| 95 | + HTTP_SERVER_STREAM_RESERVED_REMOTE, |
| 96 | + HTTP_SERVER_STREAM_OPEN, |
| 97 | + HTTP_SERVER_STREAM_HALF_CLOSED_LOCAL, |
| 98 | + HTTP_SERVER_STREAM_HALF_CLOSED_REMOTE, |
| 99 | + HTTP_SERVER_STREAM_CLOSED |
| 100 | +}; |
| 101 | + |
| 102 | +enum http_server_state { |
| 103 | + HTTP_SERVER_FRAME_HEADER_STATE, |
| 104 | + HTTP_SERVER_PREFACE_STATE, |
| 105 | + HTTP_SERVER_REQUEST_STATE, |
| 106 | + HTTP_SERVER_FRAME_DATA_STATE, |
| 107 | + HTTP_SERVER_FRAME_HEADERS_STATE, |
| 108 | + HTTP_SERVER_FRAME_SETTINGS_STATE, |
| 109 | + HTTP_SERVER_FRAME_PRIORITY_STATE, |
| 110 | + HTTP_SERVER_FRAME_WINDOW_UPDATE_STATE, |
| 111 | + HTTP_SERVER_FRAME_CONTINUATION_STATE, |
| 112 | + HTTP_SERVER_FRAME_PING_STATE, |
| 113 | + HTTP_SERVER_FRAME_RST_STREAM_STATE, |
| 114 | + HTTP_SERVER_FRAME_GOAWAY_STATE, |
| 115 | + HTTP_SERVER_DONE_STATE, |
| 116 | +}; |
| 117 | + |
| 118 | +enum http1_parser_state { |
| 119 | + HTTP1_INIT_HEADER_STATE, |
| 120 | + HTTP1_WAITING_HEADER_STATE, |
| 121 | + HTTP1_RECEIVING_HEADER_STATE, |
| 122 | + HTTP1_RECEIVED_HEADER_STATE, |
| 123 | + HTTP1_RECEIVING_DATA_STATE, |
| 124 | + HTTP1_MESSAGE_COMPLETE_STATE, |
| 125 | +}; |
| 126 | + |
| 127 | +#define HTTP_SERVER_INITIAL_WINDOW_SIZE 65536 |
| 128 | + |
| 129 | +struct http_stream_ctx { |
| 130 | + int stream_id; |
| 131 | + enum http_stream_state stream_state; |
| 132 | + int window_size; /**< Stream-level window size. */ |
| 133 | +}; |
| 134 | + |
| 135 | +struct http_frame { |
| 136 | + uint32_t length; |
| 137 | + uint32_t stream_identifier; |
| 138 | + uint8_t type; |
| 139 | + uint8_t flags; |
| 140 | + uint8_t *payload; |
| 141 | +}; |
| 142 | + |
| 143 | +struct http_client_ctx { |
| 144 | + int fd; |
| 145 | + bool preface_sent; |
| 146 | + bool has_upgrade_header; |
| 147 | + unsigned char buffer[HTTP_SERVER_CLIENT_BUFFER_SIZE]; |
| 148 | + unsigned char *cursor; /**< Cursor indicating currently processed byte. */ |
| 149 | + size_t data_len; /**< Data left to process in the buffer. */ |
| 150 | + int window_size; /**< Connection-level window size. */ |
| 151 | + enum http_server_state server_state; |
| 152 | + struct http_frame current_frame; |
| 153 | + struct http_resource_detail *current_detail; |
| 154 | + struct http_hpack_header_buf header_field; |
| 155 | + struct http_stream_ctx streams[HTTP_SERVER_MAX_STREAMS]; |
| 156 | + struct http_parser_settings parser_settings; |
| 157 | + struct http_parser parser; |
| 158 | + unsigned char url_buffer[CONFIG_HTTP_SERVER_MAX_URL_LENGTH]; |
| 159 | + unsigned char content_type[CONFIG_HTTP_SERVER_MAX_CONTENT_TYPE_LENGTH]; |
| 160 | + size_t content_len; |
| 161 | + enum http_method method; |
| 162 | + enum http1_parser_state parser_state; |
| 163 | + int http1_frag_data_len; |
| 164 | + bool headers_sent; |
| 165 | + struct k_work_delayable inactivity_timer; |
| 166 | +}; |
| 167 | + |
| 168 | +/* Starts the HTTP2 server */ |
| 169 | +int http_server_start(void); |
| 170 | + |
| 171 | +/* Stops the HTTP2 server */ |
| 172 | +int http_server_stop(void); |
| 173 | + |
| 174 | +#endif |
0 commit comments