Debian Codegraph @

crt/aws-c-http/tests/test_h1_encoder.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
6 #include <aws/http/private/h1_encoder.h>
7
8 #include <aws/common/array_list.h>
9 #include <aws/io/logging.h>
10 #include <aws/testing/aws_test_harness.h>
11
12 #include <aws/io/stream.h>
13 #include <ctype.h>
14 #include <stdio.h>
15
16 #define H1_ENCODER_TEST_CASE(NAME) \
17 AWS_TEST_CASE(NAME, s_test_##NAME); \
18 static int s_test_##NAME(struct aws_allocator *allocator, void *ctx)
19
20 static const struct aws_http_header s_typical_request_headers[] = {
21 {
22 .name = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("Host"),
23 .value = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("amazon.com"),
24 },
25 };
26
27 static struct aws_logger s_logger;
28
29 static void s_test_init(struct aws_allocator *allocator) {
30 aws_http_library_init(allocator);
31
32 struct aws_logger_standard_options logger_options = {
33 .level = AWS_LOG_LEVEL_TRACE,
34 .file = stderr,
35 };
36
37 aws_logger_init_standard(&s_logger, allocator, &logger_options);
38 aws_logger_set(&s_logger);
39 }
40
41 static void s_test_clean_up(void) {
42 aws_http_library_clean_up();
43 aws_logger_clean_up(&s_logger);
44 }
45
46 static int s_test_bad_request(
47 struct aws_allocator *allocator,
48 const char *method,
49 const char *path,
50 const struct aws_http_header *header_array,
51 size_t header_count,
52 int expected_error);
53
54 H1_ENCODER_TEST_CASE(h1_encoder_content_length_header) {
55 (void)ctx;
56 s_test_init(allocator);
57 struct aws_h1_encoder encoder;
58 aws_h1_encoder_init(&encoder, allocator);
59
60 /* request to send - we won't actually send it, we want to validate headers are set correctly. */
61 static const struct aws_byte_cursor body = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("write more tests");
62 struct aws_input_stream *body_stream = aws_input_stream_new_from_cursor(allocator, &body);
63
64 struct aws_http_header headers[] = {
65 {
66 .name = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("Host"),
67 .value = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("amazon.com"),
68 },
69 {
70 .name = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("Content-Length"),
71 .value = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("16"),
72 },
73 };
74
75 struct aws_http_message *request = aws_http_message_new_request(allocator);
76 ASSERT_SUCCESS(aws_http_message_set_request_method(request, aws_byte_cursor_from_c_str("PUT")));
77 ASSERT_SUCCESS(aws_http_message_set_request_path(request, aws_byte_cursor_from_c_str("/")));
78 aws_http_message_add_header_array(request, headers, AWS_ARRAY_SIZE(headers));
79 aws_http_message_set_body_stream(request, body_stream);
80
81 struct aws_linked_list chunk_list;
82 aws_linked_list_init(&chunk_list);
83
84 struct aws_h1_encoder_message encoder_message;
85 ASSERT_SUCCESS(
86 aws_h1_encoder_message_init_from_request(&encoder_message, allocator, request, &chunk_list, NULL, false));
87
88 ASSERT_FALSE(encoder_message.has_chunked_encoding_header);
89 ASSERT_FALSE(encoder_message.has_connection_close_header);
90 ASSERT_UINT_EQUALS(body.len, encoder_message.content_length);
91
92 aws_input_stream_release(body_stream);
93 aws_http_message_destroy(request);
94 aws_h1_encoder_message_clean_up(&encoder_message);
95 aws_h1_encoder_clean_up(&encoder);
96 s_test_clean_up();
97 return AWS_OP_SUCCESS;
98 }
99
100 H1_ENCODER_TEST_CASE(h1_encoder_transfer_encoding_chunked_header) {
101 (void)ctx;
102 s_test_init(allocator);
103 struct aws_h1_encoder encoder;
104 aws_h1_encoder_init(&encoder, allocator);
105
106 /* request to send - we won't actually send it, we want to validate headers are set correctly. */
107
108 struct aws_http_header headers[] = {
109 {
110 .name = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("Host"),
111 .value = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("amazon.com"),
112 },
113 {
114 .name = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("Transfer-Encoding"),
115 .value = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("chunked"),
116 },
117 };
118
119 struct aws_http_message *request = aws_http_message_new_request(allocator);
120 ASSERT_SUCCESS(aws_http_message_set_request_method(request, aws_byte_cursor_from_c_str("PUT")));
121 ASSERT_SUCCESS(aws_http_message_set_request_path(request, aws_byte_cursor_from_c_str("/")));
122 aws_http_message_add_header_array(request, headers, AWS_ARRAY_SIZE(headers));
123
124 struct aws_linked_list chunk_list;
125 aws_linked_list_init(&chunk_list);
126
127 struct aws_h1_encoder_message encoder_message;
128 ASSERT_SUCCESS(
129 aws_h1_encoder_message_init_from_request(&encoder_message, allocator, request, &chunk_list, NULL, false));
130
131 ASSERT_TRUE(encoder_message.has_chunked_encoding_header);
132 ASSERT_FALSE(encoder_message.has_connection_close_header);
133 ASSERT_UINT_EQUALS(0, encoder_message.content_length);
134
135 aws_http_message_destroy(request);
136 aws_h1_encoder_message_clean_up(&encoder_message);
137 aws_h1_encoder_clean_up(&encoder);
138 s_test_clean_up();
139 return AWS_OP_SUCCESS;
140 }
141
142 /* Ensure we can handle multiple "Transfer-Encoding" headers.
143 * Per RFC-9110 5.3, we should treat it the same as if we got 1 header that had the values in a list */
144 H1_ENCODER_TEST_CASE(h1_encoder_transfer_encoding_chunked_header_with_multiple_encodings) {
145 (void)ctx;
146 s_test_init(allocator);
147 struct aws_h1_encoder encoder;
148 aws_h1_encoder_init(&encoder, allocator);
149
150 /* request to send - we won't actually send it, we want to validate headers are set correctly. */
151
152 struct aws_http_header headers[] = {
153 {
154 .name = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("Host"),
155 .value = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("amazon.com"),
156 },
157 {
158 .name = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("Transfer-Encoding"),
159 .value = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("gzip"),
160 },
161 {
162 .name = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("Transfer-Encoding"),
163 .value = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("chunked"),
164 },
165 };
166
167 struct aws_http_message *request = aws_http_message_new_request(allocator);
168 ASSERT_SUCCESS(aws_http_message_set_request_method(request, aws_byte_cursor_from_c_str("PUT")));
169 ASSERT_SUCCESS(aws_http_message_set_request_path(request, aws_byte_cursor_from_c_str("/")));
170 aws_http_message_add_header_array(request, headers, AWS_ARRAY_SIZE(headers));
171
172 struct aws_linked_list chunk_list;
173 aws_linked_list_init(&chunk_list);
174
175 struct aws_h1_encoder_message encoder_message;
176 ASSERT_SUCCESS(
177 aws_h1_encoder_message_init_from_request(&encoder_message, allocator, request, &chunk_list, NULL, false));
178
179 ASSERT_TRUE(encoder_message.has_chunked_encoding_header);
180 ASSERT_FALSE(encoder_message.has_connection_close_header);
181 ASSERT_UINT_EQUALS(0, encoder_message.content_length);
182
183 aws_http_message_destroy(request);
184 aws_h1_encoder_message_clean_up(&encoder_message);
185 aws_h1_encoder_clean_up(&encoder);
186 s_test_clean_up();
187 return AWS_OP_SUCCESS;
188 }
189
190 H1_ENCODER_TEST_CASE(h1_encoder_case_insensitive_header_names) {
191 (void)ctx;
192 s_test_init(allocator);
193 struct aws_h1_encoder encoder;
194 aws_h1_encoder_init(&encoder, allocator);
195
196 /* request to send - we won't actually send it, we want to validate headers are set correctly. */
197
198 struct aws_http_header headers[] = {
199 {
200 .name = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("Host"),
201 .value = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("amazon.com"),
202 },
203 {
204 .name = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("traNsfeR-EncODIng"),
205 .value = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("chunked"),
206 },
207 };
208
209 struct aws_http_message *request = aws_http_message_new_request(allocator);
210 ASSERT_SUCCESS(aws_http_message_set_request_method(request, aws_byte_cursor_from_c_str("PUT")));
211 ASSERT_SUCCESS(aws_http_message_set_request_path(request, aws_byte_cursor_from_c_str("/")));
212 aws_http_message_add_header_array(request, headers, AWS_ARRAY_SIZE(headers));
213
214 struct aws_linked_list chunk_list;
215 aws_linked_list_init(&chunk_list);
216
217 struct aws_h1_encoder_message encoder_message;
218 ASSERT_SUCCESS(
219 aws_h1_encoder_message_init_from_request(&encoder_message, allocator, request, &chunk_list, NULL, false));
220
221 ASSERT_TRUE(encoder_message.has_chunked_encoding_header);
222 ASSERT_FALSE(encoder_message.has_connection_close_header);
223 ASSERT_UINT_EQUALS(0, encoder_message.content_length);
224
225 aws_http_message_destroy(request);
226 aws_h1_encoder_message_clean_up(&encoder_message);
227 aws_h1_encoder_clean_up(&encoder);
228 s_test_clean_up();
229 return AWS_OP_SUCCESS;
230 }
231
232 H1_ENCODER_TEST_CASE(h1_encoder_rejects_transfer_encoding_header_without_chunked) {
233 (void)ctx;
234 const struct aws_http_header headers[] = {
235 {
236 .name = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("Host"),
237 .value = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("amazon.com"),
238 },
239 {
240 .name = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("Transfer-Encoding"),
241 .value = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("gzip"), /* should contain "chunked" */
242 },
243 };
244 return s_test_bad_request(
245 allocator,
246 "PUT" /*method*/,
247 "/" /*path*/,
248 headers /*header_array*/,
249 AWS_ARRAY_SIZE(headers) /*header_count*/,
250 AWS_ERROR_HTTP_INVALID_HEADER_VALUE /*expected_error*/);
251 }
252
253 H1_ENCODER_TEST_CASE(h1_encoder_rejects_transfer_encoding_header_not_ending_in_chunked) {
254 (void)ctx;
255 struct aws_http_header headers[] = {
256 {
257 .name = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("Host"),
258 .value = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("amazon.com"),
259 },
260 {
261 .name = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("Transfer-Encoding"),
262 .value = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("chunked"), /* chunked must be last in list of codings */
263 },
264 {
265 .name = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("Transfer-Encoding"),
266 .value = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("gzip"),
267 },
268 };
269 return s_test_bad_request(
270 allocator,
271 "PUT" /*method*/,
272 "/" /*path*/,
273 headers /*header_array*/,
274 AWS_ARRAY_SIZE(headers) /*header_count*/,
275 AWS_ERROR_HTTP_INVALID_HEADER_VALUE /*expected_error*/);
276 }
277
278 H1_ENCODER_TEST_CASE(h1_encoder_transfer_encoding_chunked_across_multiple_headers) {
279 (void)ctx;
280 s_test_init(allocator);
281 struct aws_h1_encoder encoder;
282 aws_h1_encoder_init(&encoder, allocator);
283
284 /* request to send - we won't actually send it, we want to validate headers are set correctly. */
285
286 struct aws_http_header headers[] = {
287 {
288 .name = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("Host"),
289 .value = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("amazon.com"),
290 },
291 {
292 .name = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("Transfer-Encoding"),
293 .value = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("gzip, chunked"),
294 },
295 };
296
297 struct aws_http_message *request = aws_http_message_new_request(allocator);
298 ASSERT_SUCCESS(aws_http_message_set_request_method(request, aws_byte_cursor_from_c_str("PUT")));
299 ASSERT_SUCCESS(aws_http_message_set_request_path(request, aws_byte_cursor_from_c_str("/")));
300 aws_http_message_add_header_array(request, headers, AWS_ARRAY_SIZE(headers));
301
302 struct aws_linked_list chunk_list;
303 aws_linked_list_init(&chunk_list);
304
305 struct aws_h1_encoder_message encoder_message;
306 ASSERT_SUCCESS(
307 aws_h1_encoder_message_init_from_request(&encoder_message, allocator, request, &chunk_list, NULL, false));
308
309 ASSERT_TRUE(encoder_message.has_chunked_encoding_header);
310 ASSERT_FALSE(encoder_message.has_connection_close_header);
311 ASSERT_UINT_EQUALS(0, encoder_message.content_length);
312
313 aws_http_message_destroy(request);
314 aws_h1_encoder_message_clean_up(&encoder_message);
315 aws_h1_encoder_clean_up(&encoder);
316 s_test_clean_up();
317 return AWS_OP_SUCCESS;
318 }
319
320 H1_ENCODER_TEST_CASE(h1_encoder_rejects_transfer_encoding_chunked_header_combined_with_content_length) {
321 (void)ctx;
322 struct aws_http_header headers[] = {
323 {
324 .name = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("Host"),
325 .value = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("amazon.com"),
326 },
327 {
328 .name = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("Transfer-Encoding"),
329 .value = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("chunked"),
330 },
331 {
332 .name = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("Content-Length"),
333 .value = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("16"),
334 },
335 };
336 return s_test_bad_request(
337 allocator,
338 "PUT" /*method*/,
339 "/" /*path*/,
340 headers /*header_array*/,
341 AWS_ARRAY_SIZE(headers) /*header_count*/,
342 AWS_ERROR_HTTP_INVALID_HEADER_FIELD /*expected_error*/);
343 }
344
345 H1_ENCODER_TEST_CASE(h1_encoder_rejects_transfer_encoding_header_when_chunked_not_final_encoding) {
346 (void)ctx;
347 struct aws_http_header headers[] = {
348 {
349 .name = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("Host"),
350 .value = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("amazon.com"),
351 },
352 {
353 .name = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("Transfer-Encoding"),
354 .value = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("chunked,gzip"), /* must end with chunked */
355 },
356 };
357 return s_test_bad_request(
358 allocator,
359 "PUT" /*method*/,
360 "/" /*path*/,
361 headers /*header_array*/,
362 AWS_ARRAY_SIZE(headers) /*header_count*/,
363 AWS_ERROR_HTTP_INVALID_HEADER_VALUE /*expected_error*/);
364 }
365
366 static int s_test_bad_request(
367 struct aws_allocator *allocator,
368 const char *method,
369 const char *path,
370 const struct aws_http_header *header_array,
371 size_t header_count,
372 int expected_error) {
373
374 s_test_init(allocator);
375
376 struct aws_http_message *request = aws_http_message_new_request(allocator);
377 ASSERT_NOT_NULL(request);
378 if (method) {
379 ASSERT_SUCCESS(aws_http_message_set_request_method(request, aws_byte_cursor_from_c_str(method)));
380 }
381 if (path) {
382 ASSERT_SUCCESS(aws_http_message_set_request_path(request, aws_byte_cursor_from_c_str(path)));
383 }
384 if (header_array) {
385 ASSERT_SUCCESS(aws_http_message_add_header_array(request, header_array, header_count));
386 }
387
388 struct aws_linked_list chunk_list;
389 aws_linked_list_init(&chunk_list);
390
391 struct aws_h1_encoder_message encoder_message;
392
393 ASSERT_ERROR(
394 expected_error,
395 aws_h1_encoder_message_init_from_request(&encoder_message, allocator, request, &chunk_list, NULL, false));
396
397 aws_http_message_destroy(request);
398 aws_h1_encoder_message_clean_up(&encoder_message);
399 s_test_clean_up();
400 return AWS_OP_SUCCESS;
401 }
402
403 H1_ENCODER_TEST_CASE(h1_encoder_rejects_bad_method) {
404 (void)ctx;
405 return s_test_bad_request(
406 allocator,
407 "G@T" /*method*/,
408 "/" /*path*/,
409 s_typical_request_headers /*header_array*/,
410 AWS_ARRAY_SIZE(s_typical_request_headers) /*header_count*/,
411 AWS_ERROR_HTTP_INVALID_METHOD /*expected_error*/);
412 }
413
414 H1_ENCODER_TEST_CASE(h1_encoder_rejects_missing_method) {
415 (void)ctx;
416 return s_test_bad_request(
417 allocator,
418 NULL /*method*/,
419 "/" /*path*/,
420 s_typical_request_headers /*header_array*/,
421 AWS_ARRAY_SIZE(s_typical_request_headers) /*header_count*/,
422 AWS_ERROR_HTTP_INVALID_METHOD /*expected_error*/);
423 }
424
425 H1_ENCODER_TEST_CASE(h1_encoder_rejects_bad_path) {
426 (void)ctx;
427 return s_test_bad_request(
428 allocator,
429 "GET" /*method*/,
430 "/\r\n/index.html" /*path*/,
431 s_typical_request_headers /*header_array*/,
432 AWS_ARRAY_SIZE(s_typical_request_headers) /*header_count*/,
433 AWS_ERROR_HTTP_INVALID_PATH /*expected_error*/);
434 }
435
436 H1_ENCODER_TEST_CASE(h1_encoder_rejects_missing_path) {
437 (void)ctx;
438 return s_test_bad_request(
439 allocator,
440 "GET" /*method*/,
441 NULL /*path*/,
442 s_typical_request_headers /*header_array*/,
443 AWS_ARRAY_SIZE(s_typical_request_headers) /*header_count*/,
444 AWS_ERROR_HTTP_INVALID_PATH /*expected_error*/);
445 }
446
447 H1_ENCODER_TEST_CASE(h1_encoder_rejects_bad_header_name) {
448 (void)ctx;
449 const struct aws_http_header headers[] = {
450 {
451 .name = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("Host"),
452 .value = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("amazon.com"),
453 },
454 {
455 .name = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("Line-\r\n-Folds"),
456 .value = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("bad header name"),
457 },
458 };
459
460 return s_test_bad_request(
461 allocator,
462 "GET" /*method*/,
463 "/" /*path*/,
464 headers /*header_array*/,
465 AWS_ARRAY_SIZE(headers) /*header_count*/,
466 AWS_ERROR_HTTP_INVALID_HEADER_NAME /*expected_error*/);
467 }
468
469 H1_ENCODER_TEST_CASE(h1_encoder_rejects_bad_header_value) {
470 (void)ctx;
471 const struct aws_http_header headers[] = {
472 {
473 .name = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("Host"),
474 .value = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("amazon.com"),
475 },
476 {
477 .name = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("X-Line-Folds-Are-Bad-Mkay"),
478 .value = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("item1,\r\n item2"),
479 },
480 };
481
482 return s_test_bad_request(
483 allocator,
484 "GET" /*method*/,
485 "/" /*path*/,
486 headers /*header_array*/,
487 AWS_ARRAY_SIZE(headers) /*header_count*/,
488 AWS_ERROR_HTTP_INVALID_HEADER_VALUE /*expected_error*/);
489 }