Debian Codegraph @

crt/aws-c-http/source/strutil.c

raw symbols SCIP
1 /**
2 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3 * SPDX-License-Identifier: Apache-2.0.
4 */
5 #include <aws/http/private/strutil.h>
6
7 static struct aws_byte_cursor s_trim(struct aws_byte_cursor cursor, const bool trim_table[256]) {
8 /* trim leading whitespace */
9 size_t i;
10 for (i = 0; i < cursor.len; ++i) {
11 const uint8_t c = cursor.ptr[i];
12 if (!trim_table[c]) {
13 break;
14 }
15 }
16 cursor.ptr += i;
17 cursor.len -= i;
18
19 /* trim trailing whitespace */
20 for (; cursor.len; --cursor.len) {
21 const uint8_t c = cursor.ptr[cursor.len - 1];
22 if (!trim_table[c]) {
23 break;
24 }
25 }
26
27 return cursor;
28 }
29
30 static const bool s_http_whitespace_table[256] = {
31 [' '] = true,
32 ['\t'] = true,
33 };
34
35 struct aws_byte_cursor aws_strutil_trim_http_whitespace(struct aws_byte_cursor cursor) {
36 return s_trim(cursor, s_http_whitespace_table);
37 }
38
39 /* RFC7230 section 3.2.6:
40 * token = 1*tchar
41 * tchar = "!" / "#" / "$" / "%" / "&" / "'" / "*"
42 * / "+" / "-" / "." / "^" / "_" / "`" / "|" / "~"
43 * / DIGIT / ALPHA
44 */
45 static const bool s_http_token_table[256] = {
46 ['!'] = true, ['#'] = true, ['$'] = true, ['%'] = true, ['&'] = true, ['\''] = true, ['*'] = true, ['+'] = true,
47 ['-'] = true, ['.'] = true, ['^'] = true, ['_'] = true, ['`'] = true, ['|'] = true, ['~'] = true,
48
49 ['0'] = true, ['1'] = true, ['2'] = true, ['3'] = true, ['4'] = true, ['5'] = true, ['6'] = true, ['7'] = true,
50 ['8'] = true, ['9'] = true,
51
52 ['A'] = true, ['B'] = true, ['C'] = true, ['D'] = true, ['E'] = true, ['F'] = true, ['G'] = true, ['H'] = true,
53 ['I'] = true, ['J'] = true, ['K'] = true, ['L'] = true, ['M'] = true, ['N'] = true, ['O'] = true, ['P'] = true,
54 ['Q'] = true, ['R'] = true, ['S'] = true, ['T'] = true, ['U'] = true, ['V'] = true, ['W'] = true, ['X'] = true,
55 ['Y'] = true, ['Z'] = true,
56
57 ['a'] = true, ['b'] = true, ['c'] = true, ['d'] = true, ['e'] = true, ['f'] = true, ['g'] = true, ['h'] = true,
58 ['i'] = true, ['j'] = true, ['k'] = true, ['l'] = true, ['m'] = true, ['n'] = true, ['o'] = true, ['p'] = true,
59 ['q'] = true, ['r'] = true, ['s'] = true, ['t'] = true, ['u'] = true, ['v'] = true, ['w'] = true, ['x'] = true,
60 ['y'] = true, ['z'] = true,
61 };
62
63 /* Same as above, but with uppercase characters removed */
64 static const bool s_http_lowercase_token_table[256] = {
65 ['!'] = true, ['#'] = true, ['$'] = true, ['%'] = true, ['&'] = true, ['\''] = true, ['*'] = true, ['+'] = true,
66 ['-'] = true, ['.'] = true, ['^'] = true, ['_'] = true, ['`'] = true, ['|'] = true, ['~'] = true,
67
68 ['0'] = true, ['1'] = true, ['2'] = true, ['3'] = true, ['4'] = true, ['5'] = true, ['6'] = true, ['7'] = true,
69 ['8'] = true, ['9'] = true,
70
71 ['a'] = true, ['b'] = true, ['c'] = true, ['d'] = true, ['e'] = true, ['f'] = true, ['g'] = true, ['h'] = true,
72 ['i'] = true, ['j'] = true, ['k'] = true, ['l'] = true, ['m'] = true, ['n'] = true, ['o'] = true, ['p'] = true,
73 ['q'] = true, ['r'] = true, ['s'] = true, ['t'] = true, ['u'] = true, ['v'] = true, ['w'] = true, ['x'] = true,
74 ['y'] = true, ['z'] = true,
75 };
76
77 static bool s_is_token(struct aws_byte_cursor token, const bool token_table[256]) {
78 if (token.len == 0) {
79 return false;
80 }
81
82 for (size_t i = 0; i < token.len; ++i) {
83 const uint8_t c = token.ptr[i];
84 if (token_table[c] == false) {
85 return false;
86 }
87 }
88
89 return true;
90 }
91
92 bool aws_strutil_is_http_token(struct aws_byte_cursor token) {
93 return s_is_token(token, s_http_token_table);
94 }
95
96 bool aws_strutil_is_lowercase_http_token(struct aws_byte_cursor token) {
97 return s_is_token(token, s_http_lowercase_token_table);
98 }
99
100 /* clang-format off */
101 /**
102 * Table with true for all octets allowed in field-content,
103 * as defined in RFC7230 section 3.2 and 3.2.6 and RFC5234 appendix-B.1:
104 *
105 * field-content = field-vchar [ 1*( SP / HTAB ) field-vchar ]
106 * field-vchar = VCHAR / obs-text
107 * VCHAR = %x21-7E ; visible (printing) characters
108 * obs-text = %x80-FF
109 */
110 static const bool s_http_field_content_table[256] = {
111 /* clang-format off */
112
113 /* whitespace */
114 ['\t'] = true, [' '] = true,
115
116 /* VCHAR = 0x21-7E */
117 [0x21] = true, [0x22] = true, [0x23] = true, [0x24] = true, [0x25] = true, [0x26] = true, [0x27] = true,
118 [0x28] = true, [0x29] = true, [0x2A] = true, [0x2B] = true, [0x2C] = true, [0x2D] = true, [0x2E] = true,
119 [0x2F] = true, [0x30] = true, [0x31] = true, [0x32] = true, [0x33] = true, [0x34] = true, [0x35] = true,
120 [0x36] = true, [0x37] = true, [0x38] = true, [0x39] = true, [0x3A] = true, [0x3B] = true, [0x3C] = true,
121 [0x3D] = true, [0x3E] = true, [0x3F] = true, [0x40] = true, [0x41] = true, [0x42] = true, [0x43] = true,
122 [0x44] = true, [0x45] = true, [0x46] = true, [0x47] = true, [0x48] = true, [0x49] = true, [0x4A] = true,
123 [0x4B] = true, [0x4C] = true, [0x4D] = true, [0x4E] = true, [0x4F] = true, [0x50] = true, [0x51] = true,
124 [0x52] = true, [0x53] = true, [0x54] = true, [0x55] = true, [0x56] = true, [0x57] = true, [0x58] = true,
125 [0x59] = true, [0x5A] = true, [0x5B] = true, [0x5C] = true, [0x5D] = true, [0x5E] = true, [0x5F] = true,
126 [0x60] = true, [0x61] = true, [0x62] = true, [0x63] = true, [0x64] = true, [0x65] = true, [0x66] = true,
127 [0x67] = true, [0x68] = true, [0x69] = true, [0x6A] = true, [0x6B] = true, [0x6C] = true, [0x6D] = true,
128 [0x6E] = true, [0x6F] = true, [0x70] = true, [0x71] = true, [0x72] = true, [0x73] = true, [0x74] = true,
129 [0x75] = true, [0x76] = true, [0x77] = true, [0x78] = true, [0x79] = true, [0x7A] = true, [0x7B] = true,
130 [0x7C] = true, [0x7D] = true, [0x7E] = true,
131
132 /* obs-text = %x80-FF */
133 [0x80] = true, [0x81] = true, [0x82] = true, [0x83] = true, [0x84] = true, [0x85] = true, [0x86] = true,
134 [0x87] = true, [0x88] = true, [0x89] = true, [0x8A] = true, [0x8B] = true, [0x8C] = true, [0x8D] = true,
135 [0x8E] = true, [0x8F] = true, [0x90] = true, [0x91] = true, [0x92] = true, [0x93] = true, [0x94] = true,
136 [0x95] = true, [0x96] = true, [0x97] = true, [0x98] = true, [0x99] = true, [0x9A] = true, [0x9B] = true,
137 [0x9C] = true, [0x9D] = true, [0x9E] = true, [0x9F] = true, [0xA0] = true, [0xA1] = true, [0xA2] = true,
138 [0xA3] = true, [0xA4] = true, [0xA5] = true, [0xA6] = true, [0xA7] = true, [0xA8] = true, [0xA9] = true,
139 [0xAA] = true, [0xAB] = true, [0xAC] = true, [0xAD] = true, [0xAE] = true, [0xAF] = true, [0xB0] = true,
140 [0xB1] = true, [0xB2] = true, [0xB3] = true, [0xB4] = true, [0xB5] = true, [0xB6] = true, [0xB7] = true,
141 [0xB8] = true, [0xB9] = true, [0xBA] = true, [0xBB] = true, [0xBC] = true, [0xBD] = true, [0xBE] = true,
142 [0xBF] = true, [0xC0] = true, [0xC1] = true, [0xC2] = true, [0xC3] = true, [0xC4] = true, [0xC5] = true,
143 [0xC6] = true, [0xC7] = true, [0xC8] = true, [0xC9] = true, [0xCA] = true, [0xCB] = true, [0xCC] = true,
144 [0xCD] = true, [0xCE] = true, [0xCF] = true, [0xD0] = true, [0xD1] = true, [0xD2] = true, [0xD3] = true,
145 [0xD4] = true, [0xD5] = true, [0xD6] = true, [0xD7] = true, [0xD8] = true, [0xD9] = true, [0xDA] = true,
146 [0xDB] = true, [0xDC] = true, [0xDD] = true, [0xDE] = true, [0xDF] = true, [0xE0] = true, [0xE1] = true,
147 [0xE2] = true, [0xE3] = true, [0xE4] = true, [0xE5] = true, [0xE6] = true, [0xE7] = true, [0xE8] = true,
148 [0xE9] = true, [0xEA] = true, [0xEB] = true, [0xEC] = true, [0xED] = true, [0xEE] = true, [0xEF] = true,
149 [0xF0] = true, [0xF1] = true, [0xF2] = true, [0xF3] = true, [0xF4] = true, [0xF5] = true, [0xF6] = true,
150 [0xF7] = true, [0xF8] = true, [0xF9] = true, [0xFA] = true, [0xFB] = true, [0xFC] = true, [0xFD] = true,
151 [0xFE] = true, [0xFF] = true,
152 /* clang-format on */
153 };
154
155 /**
156 * From RFC7230 section 3.2:
157 * field-value = *( field-content / obs-fold )
158 * field-content = field-vchar [ 1*( SP / HTAB ) field-vchar ]
159 *
160 * But we're forbidding obs-fold
161 */
162 bool aws_strutil_is_http_field_value(struct aws_byte_cursor cursor) {
163 if (cursor.len == 0) {
164 return true;
165 }
166
167 /* first and last char cannot be whitespace */
168 const uint8_t first_c = cursor.ptr[0];
169 const uint8_t last_c = cursor.ptr[cursor.len - 1];
170 if (s_http_whitespace_table[first_c] || s_http_whitespace_table[last_c]) {
171 return false;
172 }
173
174 /* ensure every char is legal field-content */
175 size_t i = 0;
176 do {
177 const uint8_t c = cursor.ptr[i++];
178 if (s_http_field_content_table[c] == false) {
179 return false;
180 }
181 } while (i < cursor.len);
182
183 return true;
184 }
185
186 /**
187 * From RFC7230 section 3.1.2:
188 * reason-phrase = *( HTAB / SP / VCHAR / obs-text )
189 * VCHAR = %x21-7E ; visible (printing) characters
190 * obs-text = %x80-FF
191 */
192 bool aws_strutil_is_http_reason_phrase(struct aws_byte_cursor cursor) {
193 for (size_t i = 0; i < cursor.len; ++i) {
194 const uint8_t c = cursor.ptr[i];
195 /* the field-content table happens to allow the exact same characters as reason-phrase */
196 if (s_http_field_content_table[c] == false) {
197 return false;
198 }
199 }
200 return true;
201 }
202
203 bool aws_strutil_is_http_request_target(struct aws_byte_cursor cursor) {
204 if (cursor.len == 0) {
205 return false;
206 }
207
208 /* TODO: Actually check the complete grammar as defined in RFC7230 5.3 and
209 * RFC3986. Currently this just checks whether the sequence is blatantly illegal */
210 size_t i = 0;
211 do {
212 const uint8_t c = cursor.ptr[i++];
213 /* everything <= ' ' is non-visible ascii*/
214 if (c <= ' ') {
215 return false;
216 }
217 } while (i < cursor.len);
218
219 return true;
220 }
221
222 bool aws_strutil_is_http_pseudo_header_name(struct aws_byte_cursor cursor) {
223 if (cursor.len == 0) {
224 return false;
225 }
226 const uint8_t c = cursor.ptr[0];
227 if (c != ':') {
228 /* short cut */
229 return false;
230 }
231 return true;
232 }