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