Zephyr API Documentation 4.5.99
A Scalable Open Source RTOS
Loading...
Searching...
No Matches
hpack.h
Go to the documentation of this file.
1
4
5/*
6 * Copyright (c) 2023 Emna Rekik
7 * Copyright (c) 2024 Nordic Semiconductor ASA
8 *
9 * SPDX-License-Identifier: Apache-2.0
10 */
11
12#ifndef ZEPHYR_INCLUDE_NET_HTTP_HPACK_H_
13#define ZEPHYR_INCLUDE_NET_HTTP_HPACK_H_
14
15#include <stdbool.h>
16#include <stddef.h>
17#include <stdint.h>
18
27
28#ifdef __cplusplus
29extern "C" {
30#endif
31
33
34enum http_hpack_static_key {
35 HTTP_SERVER_HPACK_INVALID = 0,
36 HTTP_SERVER_HPACK_AUTHORITY = 1,
37 HTTP_SERVER_HPACK_METHOD_GET = 2,
38 HTTP_SERVER_HPACK_METHOD_POST = 3,
39 HTTP_SERVER_HPACK_PATH_ROOT = 4,
40 HTTP_SERVER_HPACK_PATH_INDEX = 5,
41 HTTP_SERVER_HPACK_SCHEME_HTTP = 6,
42 HTTP_SERVER_HPACK_SCHEME_HTTPS = 7,
43 HTTP_SERVER_HPACK_STATUS_200 = 8,
44 HTTP_SERVER_HPACK_STATUS_204 = 9,
45 HTTP_SERVER_HPACK_STATUS_206 = 10,
46 HTTP_SERVER_HPACK_STATUS_304 = 11,
47 HTTP_SERVER_HPACK_STATUS_400 = 12,
48 HTTP_SERVER_HPACK_STATUS_404 = 13,
49 HTTP_SERVER_HPACK_STATUS_500 = 14,
50 HTTP_SERVER_HPACK_ACCEPT_CHARSET = 15,
51 HTTP_SERVER_HPACK_ACCEPT_ENCODING = 16,
52 HTTP_SERVER_HPACK_ACCEPT_LANGUAGE = 17,
53 HTTP_SERVER_HPACK_ACCEPT_RANGES = 18,
54 HTTP_SERVER_HPACK_ACCEPT = 19,
55 HTTP_SERVER_HPACK_ACCESS_CONTROL_ALLOW_ORIGIN = 20,
56 HTTP_SERVER_HPACK_AGE = 21,
57 HTTP_SERVER_HPACK_ALLOW = 22,
58 HTTP_SERVER_HPACK_AUTHORIZATION = 23,
59 HTTP_SERVER_HPACK_CACHE_CONTROL = 24,
60 HTTP_SERVER_HPACK_CONTENT_DISPOSITION = 25,
61 HTTP_SERVER_HPACK_CONTENT_ENCODING = 26,
62 HTTP_SERVER_HPACK_CONTENT_LANGUAGE = 27,
63 HTTP_SERVER_HPACK_CONTENT_LENGTH = 28,
64 HTTP_SERVER_HPACK_CONTENT_LOCATION = 29,
65 HTTP_SERVER_HPACK_CONTENT_RANGE = 30,
66 HTTP_SERVER_HPACK_CONTENT_TYPE = 31,
67 HTTP_SERVER_HPACK_COOKIE = 32,
68 HTTP_SERVER_HPACK_DATE = 33,
69 HTTP_SERVER_HPACK_ETAG = 34,
70 HTTP_SERVER_HPACK_EXPECT = 35,
71 HTTP_SERVER_HPACK_EXPIRES = 36,
72 HTTP_SERVER_HPACK_FROM = 37,
73 HTTP_SERVER_HPACK_HOST = 38,
74 HTTP_SERVER_HPACK_IF_MATCH = 39,
75 HTTP_SERVER_HPACK_IF_MODIFIED_SINCE = 40,
76 HTTP_SERVER_HPACK_IF_NONE_MATCH = 41,
77 HTTP_SERVER_HPACK_IF_RANGE = 42,
78 HTTP_SERVER_HPACK_IF_UNMODIFIED_SINCE = 43,
79 HTTP_SERVER_HPACK_LAST_MODIFIED = 44,
80 HTTP_SERVER_HPACK_LINK = 45,
81 HTTP_SERVER_HPACK_LOCATION = 46,
82 HTTP_SERVER_HPACK_MAX_FORWARDS = 47,
83 HTTP_SERVER_HPACK_PROXY_AUTHENTICATE = 48,
84 HTTP_SERVER_HPACK_PROXY_AUTHORIZATION = 49,
85 HTTP_SERVER_HPACK_RANGE = 50,
86 HTTP_SERVER_HPACK_REFERER = 51,
87 HTTP_SERVER_HPACK_REFRESH = 52,
88 HTTP_SERVER_HPACK_RETRY_AFTER = 53,
89 HTTP_SERVER_HPACK_SERVER = 54,
90 HTTP_SERVER_HPACK_SET_COOKIE = 55,
91 HTTP_SERVER_HPACK_STRICT_TRANSPORT_SECURITY = 56,
92 HTTP_SERVER_HPACK_TRANSFER_ENCODING = 57,
93 HTTP_SERVER_HPACK_USER_AGENT = 58,
94 HTTP_SERVER_HPACK_VARY = 59,
95 HTTP_SERVER_HPACK_VIA = 60,
96 HTTP_SERVER_HPACK_WWW_AUTHENTICATE = 61,
97};
98
99/* TODO Kconfig */
100#define HTTP2_HEADER_FIELD_MAX_LEN 256
101
102/* Last index of the static table, the first index of the dynamic table is one above it. */
103#define HTTP_HPACK_STATIC_TABLE_LAST_INDEX HTTP_SERVER_HPACK_WWW_AUTHENTICATE
104
105/* Dynamic table size, in bytes, a decoder has to accept until its own
106 * SETTINGS_HEADER_TABLE_SIZE is acknowledged (RFC 7541, Section 4.2 and
107 * RFC 9113, Section 6.5.2).
108 */
109#define HTTP_HPACK_DYNAMIC_TABLE_SIZE 4096U
110
111/* Size of a dynamic table entry on top of its name and value (RFC 7541, Section 4.1). */
112#define HTTP_HPACK_DYNAMIC_TABLE_ENTRY_OVERHEAD 32U
113
114#if defined(CONFIG_HTTP_SERVER_HPACK_DYNAMIC_TABLE_COUNT)
115#define HTTP_HPACK_DYNAMIC_TABLE_COUNT CONFIG_HTTP_SERVER_HPACK_DYNAMIC_TABLE_COUNT
116#else
117#define HTTP_HPACK_DYNAMIC_TABLE_COUNT 0
118#endif
119
120#if defined(CONFIG_HTTP_SERVER)
121#define HTTP_SERVER_HUFFMAN_DECODE_BUFFER_SIZE CONFIG_HTTP_SERVER_HUFFMAN_DECODE_BUFFER_SIZE
122#else
123#define HTTP_SERVER_HUFFMAN_DECODE_BUFFER_SIZE 0
124#endif
125
127
135
140
143
148
154
156 const char *name;
157
159 const char *value;
160
162 size_t name_len;
163
165 size_t value_len;
166
168 uint8_t buf[HTTP_SERVER_HUFFMAN_DECODE_BUFFER_SIZE];
169
171 size_t datalen;
172};
173
175
177struct http_hpack_dynamic_table {
182 uint8_t data[HTTP_HPACK_DYNAMIC_TABLE_SIZE];
184 uint16_t data_len;
186 uint16_t count;
188 uint32_t size;
190 uint32_t max_size;
192 bool in_use;
193};
194
195int http_hpack_huffman_decode(const uint8_t *encoded_buf, size_t encoded_len,
196 uint8_t *buf, size_t buflen);
197int http_hpack_huffman_encode(const uint8_t *str, size_t str_len,
198 uint8_t *buf, size_t buflen);
199/* Decodes one entry. table may be NULL, then a field referring to the
200 * dynamic table is consumed and reported as unresolved and a field to be
201 * added to the table is not stored.
202 */
203int http_hpack_decode_header(const uint8_t *buf, size_t datalen,
204 struct http_hpack_header_buf *header,
205 struct http_hpack_dynamic_table *table);
206/* Whether the entry starting at buf adds a field to the dynamic table. */
207bool http_hpack_entry_adds_to_table(const uint8_t *buf, size_t datalen);
208/* Takes a table from the pool, NULL when none is free. */
209struct http_hpack_dynamic_table *http_hpack_dynamic_table_alloc(void);
210/* Returns a table to the pool. NULL is accepted. */
211void http_hpack_dynamic_table_free(struct http_hpack_dynamic_table *table);
212int http_hpack_encode_header(uint8_t *buf, size_t buflen,
213 struct http_hpack_header_buf *header);
214int http_hpack_encode_field(uint8_t *buf, size_t buflen, struct http_hpack_header_buf *header,
215 const char *name, const char *value);
216
218
219#ifdef __cplusplus
220}
221#endif
222
226
227#endif /* ZEPHYR_INCLUDE_NET_HTTP_HPACK_H_ */
http_hpack_entry_type
Kind of HPACK entry decoded.
Definition hpack.h:129
@ HTTP_HPACK_ENTRY_TABLE_SIZE_UPDATE
A dynamic table size update, carrying no field.
Definition hpack.h:133
@ HTTP_HPACK_ENTRY_FIELD
A header field.
Definition hpack.h:131
__UINT32_TYPE__ uint32_t
Definition stdint.h:90
__UINT8_TYPE__ uint8_t
Definition stdint.h:88
__UINT16_TYPE__ uint16_t
Definition stdint.h:89
HTTP2 header field with decoding buffer.
Definition hpack.h:137
enum http_hpack_entry_type type
Kind of the entry decoded last.
Definition hpack.h:139
bool unresolved
Whether the field referred to a dynamic table entry while the decoder has no dynamic table.
Definition hpack.h:153
size_t value_len
Length of the decoded header field value.
Definition hpack.h:165
uint8_t buf[HTTP_SERVER_HUFFMAN_DECODE_BUFFER_SIZE]
Encoding/Decoding buffer.
Definition hpack.h:168
bool oversized
Whether a string of the field was larger than the decoding buffer and was dropped.
Definition hpack.h:147
const char * value
A pointer to the decoded header field value.
Definition hpack.h:159
size_t datalen
Length of the data in the decoding buffer.
Definition hpack.h:171
uint32_t table_size
Dynamic table size requested by a size update entry.
Definition hpack.h:142
const char * name
A pointer to the decoded header field name.
Definition hpack.h:156
size_t name_len
Length of the decoded header field name.
Definition hpack.h:162