Skip to content

Commit 318dcb6

Browse files
rluboscarlescufi
authored andcommitted
net: lib: http_server: Add HPACK and Huffman code encoder/decoder
Add HTTP/2 helper libraries to encode and decode HPACK encoded headers, according to RFC7541. HPACK string encoding requires to support certain set of Huffman codes, therefore implement Huffman encoder/decoder as well. Signed-off-by: Robert Lubos <[email protected]>
1 parent 392f0ce commit 318dcb6

File tree

3 files changed

+1246
-0
lines changed

3 files changed

+1246
-0
lines changed

include/zephyr/net/http/hpack.h

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
/** @file
2+
* @brief HTTP HPACK
3+
*/
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_SERVER_HPACK_H_
13+
#define ZEPHYR_INCLUDE_NET_HTTP_SERVER_HPACK_H_
14+
15+
#include <stddef.h>
16+
#include <stdint.h>
17+
18+
/**
19+
* @brief HTTP HPACK
20+
* @defgroup http_hpack HTTP HPACK
21+
* @ingroup networking
22+
* @{
23+
*/
24+
25+
#ifdef __cplusplus
26+
extern "C" {
27+
#endif
28+
29+
enum http_hpack_static_key {
30+
HTTP_SERVER_HPACK_INVALID = 0,
31+
HTTP_SERVER_HPACK_AUTHORITY = 1,
32+
HTTP_SERVER_HPACK_METHOD_GET = 2,
33+
HTTP_SERVER_HPACK_METHOD_POST = 3,
34+
HTTP_SERVER_HPACK_PATH_ROOT = 4,
35+
HTTP_SERVER_HPACK_PATH_INDEX = 5,
36+
HTTP_SERVER_HPACK_SCHEME_HTTP = 6,
37+
HTTP_SERVER_HPACK_SCHEME_HTTPS = 7,
38+
HTTP_SERVER_HPACK_STATUS_200 = 8,
39+
HTTP_SERVER_HPACK_STATUS_204 = 9,
40+
HTTP_SERVER_HPACK_STATUS_206 = 10,
41+
HTTP_SERVER_HPACK_STATUS_304 = 11,
42+
HTTP_SERVER_HPACK_STATUS_400 = 12,
43+
HTTP_SERVER_HPACK_STATUS_404 = 13,
44+
HTTP_SERVER_HPACK_STATUS_500 = 14,
45+
HTTP_SERVER_HPACK_ACCEPT_CHARSET = 15,
46+
HTTP_SERVER_HPACK_ACCEPT_ENCODING = 16,
47+
HTTP_SERVER_HPACK_ACCEPT_LANGUAGE = 17,
48+
HTTP_SERVER_HPACK_ACCEPT_RANGES = 18,
49+
HTTP_SERVER_HPACK_ACCEPT = 19,
50+
HTTP_SERVER_HPACK_ACCESS_CONTROL_ALLOW_ORIGIN = 20,
51+
HTTP_SERVER_HPACK_AGE = 21,
52+
HTTP_SERVER_HPACK_ALLOW = 22,
53+
HTTP_SERVER_HPACK_AUTHORIZATION = 23,
54+
HTTP_SERVER_HPACK_CACHE_CONTROL = 24,
55+
HTTP_SERVER_HPACK_CONTENT_DISPOSITION = 25,
56+
HTTP_SERVER_HPACK_CONTENT_ENCODING = 26,
57+
HTTP_SERVER_HPACK_CONTENT_LANGUAGE = 27,
58+
HTTP_SERVER_HPACK_CONTENT_LENGTH = 28,
59+
HTTP_SERVER_HPACK_CONTENT_LOCATION = 29,
60+
HTTP_SERVER_HPACK_CONTENT_RANGE = 30,
61+
HTTP_SERVER_HPACK_CONTENT_TYPE = 31,
62+
HTTP_SERVER_HPACK_COOKIE = 32,
63+
HTTP_SERVER_HPACK_DATE = 33,
64+
HTTP_SERVER_HPACK_ETAG = 34,
65+
HTTP_SERVER_HPACK_EXPECT = 35,
66+
HTTP_SERVER_HPACK_EXPIRES = 36,
67+
HTTP_SERVER_HPACK_FROM = 37,
68+
HTTP_SERVER_HPACK_HOST = 38,
69+
HTTP_SERVER_HPACK_IF_MATCH = 39,
70+
HTTP_SERVER_HPACK_IF_MODIFIED_SINCE = 40,
71+
HTTP_SERVER_HPACK_IF_NONE_MATCH = 41,
72+
HTTP_SERVER_HPACK_IF_RANGE = 42,
73+
HTTP_SERVER_HPACK_IF_UNMODIFIED_SINCE = 43,
74+
HTTP_SERVER_HPACK_LAST_MODIFIED = 44,
75+
HTTP_SERVER_HPACK_LINK = 45,
76+
HTTP_SERVER_HPACK_LOCATION = 46,
77+
HTTP_SERVER_HPACK_MAX_FORWARDS = 47,
78+
HTTP_SERVER_HPACK_PROXY_AUTHENTICATE = 48,
79+
HTTP_SERVER_HPACK_PROXY_AUTHORIZATION = 49,
80+
HTTP_SERVER_HPACK_RANGE = 50,
81+
HTTP_SERVER_HPACK_REFERER = 51,
82+
HTTP_SERVER_HPACK_REFRESH = 52,
83+
HTTP_SERVER_HPACK_RETRY_AFTER = 53,
84+
HTTP_SERVER_HPACK_SERVER = 54,
85+
HTTP_SERVER_HPACK_SET_COOKIE = 55,
86+
HTTP_SERVER_HPACK_STRICT_TRANSPORT_SECURITY = 56,
87+
HTTP_SERVER_HPACK_TRANSFER_ENCODING = 57,
88+
HTTP_SERVER_HPACK_USER_AGENT = 58,
89+
HTTP_SERVER_HPACK_VARY = 59,
90+
HTTP_SERVER_HPACK_VIA = 60,
91+
HTTP_SERVER_HPACK_WWW_AUTHENTICATE = 61,
92+
};
93+
94+
/* TODO Kconfig */
95+
#define HTTP2_HEADER_FIELD_MAX_LEN 256
96+
97+
/** HTTP2 header field with decoding buffer. */
98+
struct http_hpack_header_buf {
99+
/** A pointer to the decoded header field name. */
100+
const char *name;
101+
102+
/** A pointer to the decoded header field value. */
103+
const char *value;
104+
105+
/** Length of the decoded header field name. */
106+
size_t name_len;
107+
108+
/** Length of the decoded header field value. */
109+
size_t value_len;
110+
111+
/** Encoding/Decoding buffer. Used with Huffman encoding/decoding. */
112+
uint8_t buf[CONFIG_HTTP_SERVER_HUFFMAN_DECODE_BUFFER_SIZE];
113+
114+
/** Length of the data in the decoding buffer. */
115+
size_t datalen;
116+
};
117+
118+
int http_hpack_huffman_decode(const uint8_t *encoded_buf, size_t encoded_len,
119+
uint8_t *buf, size_t buflen);
120+
int http_hpack_huffman_encode(const uint8_t *str, size_t str_len,
121+
uint8_t *buf, size_t buflen);
122+
int http_hpack_decode_header(const uint8_t *buf, size_t datalen,
123+
struct http_hpack_header_buf *header);
124+
int http_hpack_encode_header(uint8_t *buf, size_t buflen,
125+
struct http_hpack_header_buf *header);
126+
127+
#ifdef __cplusplus
128+
}
129+
#endif
130+
131+
/**
132+
* @}
133+
*/
134+
135+
#endif

0 commit comments

Comments
 (0)