Debian Codegraph @

crt/aws-c-http/tests/test_h1_client.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 "stream_test_helper.h"
7 #include <aws/common/uuid.h>
8 #include <aws/http/private/h1_connection.h>
9 #include <aws/http/request_response.h>
10 #include <aws/http/status_code.h>
11 #include <aws/io/logging.h>
12 #include <aws/io/stream.h>
13 #include <aws/testing/io_testing_channel.h>
14
15 #ifdef _MSC_VER
16 # pragma warning(disable : 4204) /* non-constant aggregate initializer */
17 #endif
18
19 #define H1_CLIENT_TEST_CASE(NAME) \
20 AWS_TEST_CASE(NAME, s_test_##NAME); \
21 static int s_test_##NAME(struct aws_allocator *allocator, void *ctx)
22
23 static struct aws_http_message *s_new_default_get_request(struct aws_allocator *allocator) {
24 struct aws_http_message *request = aws_http_message_new_request(allocator);
25 AWS_FATAL_ASSERT(request);
26 AWS_FATAL_ASSERT(AWS_OP_SUCCESS == aws_http_message_set_request_method(request, aws_http_method_get));
27 AWS_FATAL_ASSERT(AWS_OP_SUCCESS == aws_http_message_set_request_path(request, aws_byte_cursor_from_c_str("/")));
28
29 return request;
30 }
31
32 static void s_destroy_stream_on_complete(struct aws_http_stream *stream, int error_code, void *user_data) {
33 (void)stream;
34 (void)error_code;
35 struct aws_input_stream *data_stream = user_data;
36 aws_input_stream_release(data_stream);
37 }
38
39 static struct aws_http1_chunk_options s_default_chunk_options(struct aws_input_stream *stream, size_t stream_size) {
40 struct aws_http1_chunk_options options;
41 AWS_ZERO_STRUCT(options);
42 options.chunk_data = stream;
43 options.chunk_data_size = stream_size;
44 options.on_complete = s_destroy_stream_on_complete;
45 options.user_data = stream;
46 return options;
47 }
48
49 static int s_write_termination_chunk(struct aws_allocator *allocator, struct aws_http_stream *stream) {
50 static const struct aws_byte_cursor empty_str = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("");
51 struct aws_input_stream *termination_marker = aws_input_stream_new_from_cursor(allocator, &empty_str);
52 ASSERT_NOT_NULL(termination_marker);
53 struct aws_http1_chunk_options options = s_default_chunk_options(termination_marker, empty_str.len);
54 ASSERT_SUCCESS(aws_http1_stream_write_chunk(stream, &options));
55 return AWS_OP_SUCCESS;
56 }
57
58 static struct aws_http_message *s_new_default_chunked_put_request(struct aws_allocator *allocator) {
59 struct aws_http_header headers[] = {
60 {
61 .name = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("Transfer-Encoding"),
62 .value = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("chunked"),
63 },
64 };
65
66 struct aws_http_message *request = aws_http_message_new_request(allocator);
67 AWS_FATAL_ASSERT(request);
68 AWS_FATAL_ASSERT(AWS_OP_SUCCESS == aws_http_message_set_request_method(request, aws_byte_cursor_from_c_str("PUT")));
69 AWS_FATAL_ASSERT(
70 AWS_OP_SUCCESS == aws_http_message_set_request_path(request, aws_byte_cursor_from_c_str("/plan.txt")));
71 AWS_FATAL_ASSERT(AWS_OP_SUCCESS == aws_http_message_add_header_array(request, headers, AWS_ARRAY_SIZE(headers)));
72
73 return request;
74 }
75
76 static struct aws_http_message *s_new_default_head_request(struct aws_allocator *allocator) {
77 struct aws_http_message *request = aws_http_message_new_request(allocator);
78 AWS_FATAL_ASSERT(request);
79 AWS_FATAL_ASSERT(AWS_OP_SUCCESS == aws_http_message_set_request_method(request, aws_http_method_head));
80 AWS_FATAL_ASSERT(AWS_OP_SUCCESS == aws_http_message_set_request_path(request, aws_byte_cursor_from_c_str("/")));
81
82 return request;
83 }
84
85 struct tester {
86 struct aws_allocator *alloc;
87 struct testing_channel testing_channel;
88 struct aws_http_connection *connection;
89 struct aws_logger logger;
90 bool manual_window_management;
91 };
92
93 struct tester_options {
94 bool manual_window_management;
95 size_t initial_stream_window_size;
96 size_t read_buffer_capacity;
97 };
98
99 static int s_tester_init_ex(struct tester *tester, struct aws_allocator *alloc, const struct tester_options *options) {
100 aws_http_library_init(alloc);
101
102 AWS_ZERO_STRUCT(*tester);
103
104 tester->alloc = alloc;
105
106 struct aws_logger_standard_options logger_options = {
107 .level = AWS_LOG_LEVEL_DEBUG,
108 .file = stderr,
109 };
110 ASSERT_SUCCESS(aws_logger_init_standard(&tester->logger, tester->alloc, &logger_options));
111 aws_logger_set(&tester->logger);
112
113 struct aws_testing_channel_options test_channel_options = {.clock_fn = aws_high_res_clock_get_ticks};
114 ASSERT_SUCCESS(testing_channel_init(&tester->testing_channel, alloc, &test_channel_options));
115
116 struct aws_http1_connection_options http1_options;
117 AWS_ZERO_STRUCT(http1_options);
118 http1_options.read_buffer_capacity = options->read_buffer_capacity;
119
120 tester->connection = aws_http_connection_new_http1_1_client(
121 alloc, options->manual_window_management, options->initial_stream_window_size, &http1_options);
122 ASSERT_NOT_NULL(tester->connection);
123
124 struct aws_channel_slot *slot = aws_channel_slot_new(tester->testing_channel.channel);
125 ASSERT_NOT_NULL(slot);
126 ASSERT_SUCCESS(aws_channel_slot_insert_end(tester->testing_channel.channel, slot));
127 ASSERT_SUCCESS(aws_channel_slot_set_handler(slot, &tester->connection->channel_handler));
128 tester->connection->vtable->on_channel_handler_installed(&tester->connection->channel_handler, slot);
129
130 testing_channel_drain_queued_tasks(&tester->testing_channel);
131
132 return AWS_OP_SUCCESS;
133 }
134
135 static int s_tester_init(struct tester *tester, struct aws_allocator *alloc) {
136 struct tester_options options = {
137 .manual_window_management = false,
138 };
139 return s_tester_init_ex(tester, alloc, &options);
140 }
141
142 static int s_tester_clean_up(struct tester *tester) {
143 aws_http_connection_release(tester->connection);
144 ASSERT_SUCCESS(testing_channel_clean_up(&tester->testing_channel));
145 aws_http_library_clean_up();
146 aws_logger_clean_up(&tester->logger);
147 return AWS_OP_SUCCESS;
148 }
149
150 /* Check that we can set and tear down the `tester` used by all other tests in this file */
151 H1_CLIENT_TEST_CASE(h1_client_sanity_check) {
152 (void)ctx;
153 struct tester tester;
154 ASSERT_SUCCESS(s_tester_init(&tester, allocator));
155
156 ASSERT_SUCCESS(s_tester_clean_up(&tester));
157 return AWS_OP_SUCCESS;
158 }
159
160 /* Send 1 line request, doesn't care about response */
161 H1_CLIENT_TEST_CASE(h1_client_request_send_1liner) {
162 (void)ctx;
163 struct tester tester;
164 ASSERT_SUCCESS(s_tester_init(&tester, allocator));
165
166 /* send request */
167 struct aws_http_make_request_options opt = {
168 .self_size = sizeof(opt),
169 .request = s_new_default_get_request(allocator),
170 };
171 struct aws_http_stream *stream = aws_http_connection_make_request(tester.connection, &opt);
172 ASSERT_NOT_NULL(stream);
173 ASSERT_SUCCESS(aws_http_stream_activate(stream));
174
175 testing_channel_drain_queued_tasks(&tester.testing_channel);
176
177 /* check result */
178 const char *expected = "GET / HTTP/1.1\r\n"
179 "\r\n";
180 ASSERT_SUCCESS(testing_channel_check_written_message_str(&tester.testing_channel, expected));
181
182 /* clean up */
183 aws_http_message_destroy(opt.request);
184 aws_http_stream_release(stream);
185
186 ASSERT_SUCCESS(s_tester_clean_up(&tester));
187 return AWS_OP_SUCCESS;
188 }
189
190 H1_CLIENT_TEST_CASE(h1_client_request_send_headers) {
191 (void)ctx;
192 struct tester tester;
193 ASSERT_SUCCESS(s_tester_init(&tester, allocator));
194
195 /* send request */
196 struct aws_http_header headers[] = {
197 {
198 .name = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("Host"),
199 .value = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("example.com"),
200 },
201 {
202 .name = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("Accept"),
203 .value = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("*/*"),
204 },
205 };
206
207 struct aws_http_message *request = s_new_default_get_request(allocator);
208 ASSERT_NOT_NULL(request);
209 ASSERT_SUCCESS(aws_http_message_add_header_array(request, headers, AWS_ARRAY_SIZE(headers)));
210
211 struct aws_http_make_request_options opt = {
212 .self_size = sizeof(opt),
213 .request = request,
214 };
215 struct aws_http_stream *stream = aws_http_connection_make_request(tester.connection, &opt);
216 ASSERT_NOT_NULL(stream);
217 ASSERT_SUCCESS(aws_http_stream_activate(stream));
218
219 testing_channel_drain_queued_tasks(&tester.testing_channel);
220
221 /* check result */
222 const char *expected = "GET / HTTP/1.1\r\n"
223 "Host: example.com\r\n"
224 "Accept: */*\r\n"
225 "\r\n";
226 ASSERT_SUCCESS(testing_channel_check_written_message_str(&tester.testing_channel, expected));
227
228 /* clean up */
229 aws_http_message_destroy(request);
230 aws_http_stream_release(stream);
231
232 ASSERT_SUCCESS(s_tester_clean_up(&tester));
233 return AWS_OP_SUCCESS;
234 }
235
236 H1_CLIENT_TEST_CASE(h1_client_request_send_body) {
237 (void)ctx;
238 struct tester tester;
239 ASSERT_SUCCESS(s_tester_init(&tester, allocator));
240
241 /* send request */
242 static const struct aws_byte_cursor body = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("write more tests");
243 struct aws_input_stream *body_stream = aws_input_stream_new_from_cursor(allocator, &body);
244
245 struct aws_http_header headers[] = {
246 {
247 .name = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("Content-Length"),
248 .value = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("16"),
249 },
250 };
251
252 struct aws_http_message *request = aws_http_message_new_request(allocator);
253 ASSERT_NOT_NULL(request);
254 ASSERT_SUCCESS(aws_http_message_set_request_method(request, aws_byte_cursor_from_c_str("PUT")));
255 ASSERT_SUCCESS(aws_http_message_set_request_path(request, aws_byte_cursor_from_c_str("/plan.txt")));
256 aws_http_message_add_header_array(request, headers, AWS_ARRAY_SIZE(headers));
257 aws_http_message_set_body_stream(request, body_stream);
258
259 struct aws_http_make_request_options opt = {
260 .self_size = sizeof(opt),
261 .request = request,
262 };
263 struct aws_http_stream *stream = aws_http_connection_make_request(tester.connection, &opt);
264 ASSERT_NOT_NULL(stream);
265 aws_http_stream_activate(stream);
266
267 testing_channel_drain_queued_tasks(&tester.testing_channel);
268
269 /* check result */
270 const char *expected = "PUT /plan.txt HTTP/1.1\r\n"
271 "Content-Length: 16\r\n"
272 "\r\n"
273 "write more tests";
274 ASSERT_SUCCESS(testing_channel_check_written_messages_str(&tester.testing_channel, allocator, expected));
275
276 /* clean up */
277 aws_input_stream_release(body_stream);
278 aws_http_message_destroy(request);
279 aws_http_stream_release(stream);
280
281 ASSERT_SUCCESS(s_tester_clean_up(&tester));
282 return AWS_OP_SUCCESS;
283 }
284
285 H1_CLIENT_TEST_CASE(h1_client_request_send_body_chunked) {
286 (void)ctx;
287 struct tester tester;
288 ASSERT_SUCCESS(s_tester_init(&tester, allocator));
289
290 /* send request */
291 struct aws_http_message *request = s_new_default_chunked_put_request(allocator);
292 struct aws_http_make_request_options opt = {
293 .self_size = sizeof(opt),
294 .request = request,
295 };
296 struct aws_http_stream *stream = aws_http_connection_make_request(tester.connection, &opt);
297 ASSERT_NOT_NULL(stream);
298
299 /* Initialize and send the stream chunks */
300 static const struct aws_byte_cursor body = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("write more tests");
301 struct aws_input_stream *body_stream = aws_input_stream_new_from_cursor(allocator, &body);
302 struct aws_http1_chunk_options options = s_default_chunk_options(body_stream, body.len);
303 ASSERT_SUCCESS(aws_http_stream_activate(stream));
304 ASSERT_SUCCESS(aws_http1_stream_write_chunk(stream, &options));
305 ASSERT_SUCCESS(s_write_termination_chunk(allocator, stream));
306
307 testing_channel_drain_queued_tasks(&tester.testing_channel);
308
309 /* check result */
310 const char *expected = "PUT /plan.txt HTTP/1.1\r\n"
311 "Transfer-Encoding: chunked\r\n"
312 "\r\n"
313 "10\r\n"
314 "write more tests"
315 "\r\n"
316 "0\r\n"
317 "\r\n";
318
319 ASSERT_SUCCESS(testing_channel_check_written_messages_str(&tester.testing_channel, allocator, expected));
320
321 /* clean up */
322 aws_http_message_destroy(request);
323 aws_http_stream_release(stream);
324
325 ASSERT_SUCCESS(s_tester_clean_up(&tester));
326 return AWS_OP_SUCCESS;
327 }
328
329 H1_CLIENT_TEST_CASE(h1_client_request_send_body_chunked_and_streaming) {
330 (void)ctx;
331 struct tester tester;
332 ASSERT_SUCCESS(s_tester_init(&tester, allocator));
333
334 /* send request */
335 static const struct aws_byte_cursor body = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("write more tests");
336 struct aws_input_stream *body_stream = aws_input_stream_new_from_cursor(allocator, &body);
337
338 struct aws_http_header headers[] = {
339 {
340 .name = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("Host"),
341 .value = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("amazon.com"),
342 },
343 {
344 .name = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("Transfer-Encoding"),
345 .value = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("chunked"),
346 },
347 };
348
349 struct aws_http_message *request = aws_http_message_new_request(allocator);
350 ASSERT_NOT_NULL(request);
351 ASSERT_SUCCESS(aws_http_message_set_request_method(request, aws_byte_cursor_from_c_str("PUT")));
352 ASSERT_SUCCESS(aws_http_message_set_request_path(request, aws_byte_cursor_from_c_str("/plan.txt")));
353 aws_http_message_add_header_array(request, headers, AWS_ARRAY_SIZE(headers));
354 aws_http_message_set_body_stream(request, body_stream);
355
356 struct aws_http_make_request_options opt = {
357 .self_size = sizeof(opt),
358 .request = request,
359 };
360 struct aws_http_stream *stream = aws_http_connection_make_request(tester.connection, &opt);
361 ASSERT_NOT_NULL(stream);
362 aws_http_stream_activate(stream);
363
364 testing_channel_drain_queued_tasks(&tester.testing_channel);
365
366 /* check result */
367 const char *expected = "PUT /plan.txt HTTP/1.1\r\n"
368 "Host: amazon.com\r\n"
369 "Transfer-Encoding: chunked\r\n"
370 "\r\n"
371 "00000010\r\n" /* implementation currently pads chunk-size */
372 "write more tests"
373 "\r\n"
374 "0\r\n"
375 "\r\n";
376
377 ASSERT_SUCCESS(testing_channel_check_written_messages_str(&tester.testing_channel, allocator, expected));
378
379 /* clean up */
380 aws_input_stream_release(body_stream);
381 aws_http_message_destroy(request);
382 aws_http_stream_release(stream);
383
384 ASSERT_SUCCESS(s_tester_clean_up(&tester));
385 return AWS_OP_SUCCESS;
386 }
387
388 H1_CLIENT_TEST_CASE(h1_client_request_send_empty_body_chunked_and_streaming) {
389 (void)ctx;
390 struct tester tester;
391 ASSERT_SUCCESS(s_tester_init(&tester, allocator));
392
393 /* send request */
394 static const struct aws_byte_cursor body = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("");
395 struct aws_input_stream *body_stream = aws_input_stream_new_from_cursor(allocator, &body);
396
397 struct aws_http_header headers[] = {
398 {
399 .name = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("Host"),
400 .value = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("amazon.com"),
401 },
402 {
403 .name = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("Transfer-Encoding"),
404 .value = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("chunked"),
405 },
406 };
407
408 struct aws_http_message *request = aws_http_message_new_request(allocator);
409 ASSERT_NOT_NULL(request);
410 ASSERT_SUCCESS(aws_http_message_set_request_method(request, aws_byte_cursor_from_c_str("PUT")));
411 ASSERT_SUCCESS(aws_http_message_set_request_path(request, aws_byte_cursor_from_c_str("/plan.txt")));
412 aws_http_message_add_header_array(request, headers, AWS_ARRAY_SIZE(headers));
413 aws_http_message_set_body_stream(request, body_stream);
414
415 struct aws_http_make_request_options opt = {
416 .self_size = sizeof(opt),
417 .request = request,
418 };
419 struct aws_http_stream *stream = aws_http_connection_make_request(tester.connection, &opt);
420 ASSERT_NOT_NULL(stream);
421 aws_http_stream_activate(stream);
422
423 testing_channel_drain_queued_tasks(&tester.testing_channel);
424
425 /* check result */
426 const char *expected = "PUT /plan.txt HTTP/1.1\r\n"
427 "Host: amazon.com\r\n"
428 "Transfer-Encoding: chunked\r\n"
429 "\r\n"
430 "0\r\n"
431 "\r\n";
432
433 ASSERT_SUCCESS(testing_channel_check_written_messages_str(&tester.testing_channel, allocator, expected));
434
435 /* clean up */
436 aws_input_stream_release(body_stream);
437 aws_http_message_destroy(request);
438 aws_http_stream_release(stream);
439
440 ASSERT_SUCCESS(s_tester_clean_up(&tester));
441 return AWS_OP_SUCCESS;
442 }
443
444 /**
445 * An input stream that can stall (provide 0 bytes from read()) for a while.
446 * Not thread safe. Set current_cursor to un-stall it. Set is_eof to end it.
447 */
448 struct stalling_input_stream {
449 struct aws_input_stream base;
450 struct aws_allocator *allocator;
451 struct aws_byte_cursor current_cursor;
452 bool is_eof;
453 };
454
455 static int s_stalling_input_stream_read(struct aws_input_stream *stream, struct aws_byte_buf *dest) {
456 struct stalling_input_stream *impl = AWS_CONTAINER_OF(stream, struct stalling_input_stream, base);
457
458 aws_byte_buf_write_to_capacity(dest, &impl->current_cursor);
459
460 return AWS_OP_SUCCESS;
461 }
462
463 static int s_stalling_input_stream_get_status(struct aws_input_stream *stream, struct aws_stream_status *status) {
464 struct stalling_input_stream *impl = AWS_CONTAINER_OF(stream, struct stalling_input_stream, base);
465
466 status->is_end_of_stream = impl->is_eof && impl->current_cursor.len == 0;
467 status->is_valid = true;
468
469 return AWS_OP_SUCCESS;
470 }
471
472 static void s_stalling_input_stream_add_data(struct aws_input_stream *stream, const char *data) {
473 struct stalling_input_stream *impl = AWS_CONTAINER_OF(stream, struct stalling_input_stream, base);
474 AWS_FATAL_ASSERT(impl->current_cursor.len == 0);
475 impl->current_cursor = aws_byte_cursor_from_c_str(data);
476 }
477
478 static void s_stalling_input_stream_set_eof(struct aws_input_stream *stream) {
479 struct stalling_input_stream *impl = AWS_CONTAINER_OF(stream, struct stalling_input_stream, base);
480 impl->is_eof = true;
481 }
482
483 static struct aws_input_stream_vtable s_stalling_input_stream_vtable = {
484 .read = s_stalling_input_stream_read,
485 .get_status = s_stalling_input_stream_get_status,
486 };
487
488 static void s_stalling_input_stream_destroy(struct stalling_input_stream *impl) {
489 aws_mem_release(impl->allocator, impl);
490 }
491
492 static struct aws_input_stream *s_stalling_input_stream_new(struct aws_allocator *allocator) {
493 struct stalling_input_stream *impl = aws_mem_calloc(allocator, 1, sizeof(struct stalling_input_stream));
494 impl->allocator = allocator;
495 impl->base.impl = impl;
496 impl->base.vtable = &s_stalling_input_stream_vtable;
497 aws_ref_count_init(&impl->base.ref_count, impl, (aws_simple_completion_callback *)s_stalling_input_stream_destroy);
498 return &impl->base;
499 }
500
501 /* Send request, with chunked and streaming body, where the body stream "stalls"
502 * and doesn't fill the available space */
503 H1_CLIENT_TEST_CASE(h1_client_request_send_stalled_body_chunked_and_streaming) {
504 (void)ctx;
505 struct tester tester;
506 ASSERT_SUCCESS(s_tester_init(&tester, allocator));
507
508 /* send request */
509 struct aws_input_stream *stalling_stream = s_stalling_input_stream_new(allocator);
510
511 struct aws_http_header headers[] = {
512 {
513 .name = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("Host"),
514 .value = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("amazon.com"),
515 },
516 {
517 .name = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("Transfer-Encoding"),
518 .value = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("chunked"),
519 },
520 };
521
522 struct aws_http_message *request = aws_http_message_new_request(allocator);
523 ASSERT_NOT_NULL(request);
524 ASSERT_SUCCESS(aws_http_message_set_request_method(request, aws_byte_cursor_from_c_str("PUT")));
525 ASSERT_SUCCESS(aws_http_message_set_request_path(request, aws_byte_cursor_from_c_str("/plan.txt")));
526 aws_http_message_add_header_array(request, headers, AWS_ARRAY_SIZE(headers));
527 aws_http_message_set_body_stream(request, stalling_stream);
528
529 struct aws_http_make_request_options opt = {
530 .self_size = sizeof(opt),
531 .request = request,
532 };
533 struct aws_http_stream *stream = aws_http_connection_make_request(tester.connection, &opt);
534 ASSERT_NOT_NULL(stream);
535 aws_http_stream_activate(stream);
536
537 /* Stream is currently stalled. After 1 event-loop tick, only the request head should have be written. */
538 testing_channel_run_currently_queued_tasks(&tester.testing_channel);
539 ASSERT_SUCCESS(testing_channel_check_written_messages_str(
540 &tester.testing_channel,
541 allocator,
542 /*expected*/
543 "PUT /plan.txt HTTP/1.1\r\n"
544 "Host: amazon.com\r\n"
545 "Transfer-Encoding: chunked\r\n"
546 "\r\n"));
547
548 /* Execute a few event-loop ticks. No more data should be written */
549 testing_channel_run_currently_queued_tasks(&tester.testing_channel);
550 testing_channel_run_currently_queued_tasks(&tester.testing_channel);
551 testing_channel_run_currently_queued_tasks(&tester.testing_channel);
552 ASSERT_TRUE(aws_linked_list_empty(testing_channel_get_written_message_queue(&tester.testing_channel)));
553
554 /* Now stream a bit of data... */
555 s_stalling_input_stream_add_data(stalling_stream, "baby's first chunk");
556 testing_channel_run_currently_queued_tasks(&tester.testing_channel);
557 ASSERT_SUCCESS(testing_channel_check_written_messages_str(
558 &tester.testing_channel,
559 allocator,
560 /*expected*/
561 "00000012\r\n"
562 "baby's first chunk"
563 "\r\n"));
564
565 /* Execute a few event-loop ticks. No more data should be written */
566 testing_channel_run_currently_queued_tasks(&tester.testing_channel);
567 testing_channel_run_currently_queued_tasks(&tester.testing_channel);
568 testing_channel_run_currently_queued_tasks(&tester.testing_channel);
569 ASSERT_TRUE(aws_linked_list_empty(testing_channel_get_written_message_queue(&tester.testing_channel)));
570
571 /* Now stream exactly 1 byte more... */
572 s_stalling_input_stream_add_data(stalling_stream, "z");
573 testing_channel_run_currently_queued_tasks(&tester.testing_channel);
574 ASSERT_SUCCESS(testing_channel_check_written_messages_str(
575 &tester.testing_channel,
576 allocator,
577 /*expected*/
578 "00000001\r\n"
579 "z"
580 "\r\n"));
581
582 /* Finally, end the stream, the request should finish sending... */
583 s_stalling_input_stream_set_eof(stalling_stream);
584 testing_channel_drain_queued_tasks(&tester.testing_channel);
585 ASSERT_SUCCESS(testing_channel_check_written_messages_str(
586 &tester.testing_channel,
587 allocator,
588 /*expected*/
589 "0\r\n"
590 "\r\n"));
591
592 /* clean up */
593 aws_input_stream_release(stalling_stream);
594 aws_http_message_destroy(request);
595 aws_http_stream_release(stream);
596
597 ASSERT_SUCCESS(s_tester_clean_up(&tester));
598 return AWS_OP_SUCCESS;
599 }
600
601 int chunked_test_helper(
602 const struct aws_byte_cursor *body,
603 struct aws_http_headers *trailers,
604 const char *expected,
605 struct tester tester,
606 struct aws_allocator *allocator) {
607
608 /* send request */
609 struct aws_http_message *request = s_new_default_chunked_put_request(allocator);
610 struct aws_http_make_request_options opt = {
611 .self_size = sizeof(opt),
612 .request = request,
613 };
614 struct aws_http_stream *stream = aws_http_connection_make_request(tester.connection, &opt);
615 ASSERT_NOT_NULL(stream);
616
617 /* Initialize and send the stream chunks */
618 ASSERT_SUCCESS(aws_http_stream_activate(stream));
619 if (body != NULL) {
620 struct aws_input_stream *body_stream = aws_input_stream_new_from_cursor(allocator, body);
621 struct aws_http1_chunk_options options = s_default_chunk_options(body_stream, body->len);
622 ASSERT_SUCCESS(aws_http1_stream_write_chunk(stream, &options));
623 }
624 ASSERT_SUCCESS(aws_http1_stream_add_chunked_trailer(stream, trailers));
625 ASSERT_SUCCESS(s_write_termination_chunk(allocator, stream));
626
627 testing_channel_drain_queued_tasks(&tester.testing_channel);
628
629 /* check result */
630 ASSERT_SUCCESS(testing_channel_check_written_messages_str(&tester.testing_channel, allocator, expected));
631
632 /* clean up */
633 aws_http_message_destroy(request);
634 aws_http_stream_release(stream);
635 return AWS_OP_SUCCESS;
636 }
637
638 int chunked_trailer_succeed(
639 const struct aws_byte_cursor *body,
640 struct aws_http_headers *trailers,
641 struct tester tester,
642 struct aws_allocator *allocator) {
643
644 /* send request */
645 struct aws_http_message *request = s_new_default_chunked_put_request(allocator);
646 struct aws_http_make_request_options opt = {
647 .self_size = sizeof(opt),
648 .request = request,
649 };
650 struct aws_http_stream *stream = aws_http_connection_make_request(tester.connection, &opt);
651 ASSERT_NOT_NULL(stream);
652
653 /* Initialize and send the stream chunks */
654 ASSERT_SUCCESS(aws_http_stream_activate(stream));
655 if (body != NULL) {
656 struct aws_input_stream *body_stream = aws_input_stream_new_from_cursor(allocator, body);
657 struct aws_http1_chunk_options options = s_default_chunk_options(body_stream, body->len);
658 ASSERT_SUCCESS(aws_http1_stream_write_chunk(stream, &options));
659 }
660
661 /* kind of gross, but good enough for now */
662 int err = aws_http1_stream_add_chunked_trailer(stream, trailers);
663 if (err) {
664 aws_http_message_destroy(request);
665 aws_http_stream_release(stream);
666 return err;
667 }
668 ASSERT_SUCCESS(s_write_termination_chunk(allocator, stream));
669
670 testing_channel_drain_queued_tasks(&tester.testing_channel);
671
672 /* clean up */
673 aws_http_message_destroy(request);
674 aws_http_stream_release(stream);
675 return AWS_OP_SUCCESS;
676 }
677
678 H1_CLIENT_TEST_CASE(h1_client_request_send_chunked_trailer) {
679 (void)ctx;
680 struct tester tester;
681 ASSERT_SUCCESS(s_tester_init(&tester, allocator));
682 struct aws_http_headers *trailers = aws_http_headers_new(allocator);
683 const struct aws_http_header trailer = {
684 .name = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("chunked"),
685 .value = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("trailer"),
686 };
687 const struct aws_http_header trailer1 = {
688 .name = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("another"),
689 .value = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("test"),
690 };
691 aws_http_headers_add_header(trailers, &trailer);
692 aws_http_headers_add_header(trailers, &trailer1);
693
694 /* Initialize and send the stream chunks */
695 static const struct aws_byte_cursor body = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("write more tests");
696
697 const char *expected = "PUT /plan.txt HTTP/1.1\r\n"
698 "Transfer-Encoding: chunked\r\n"
699 "\r\n"
700 "10\r\n"
701 "write more tests"
702 "\r\n"
703 "0\r\n"
704 "chunked: trailer\r\n"
705 "another: test\r\n"
706 "\r\n";
707
708 ASSERT_SUCCESS(chunked_test_helper(&body, trailers, expected, tester, allocator));
709 /* clean up */
710 aws_http_headers_release(trailers);
711 ASSERT_SUCCESS(s_tester_clean_up(&tester));
712 return AWS_OP_SUCCESS;
713 }
714
715 H1_CLIENT_TEST_CASE(h1_client_request_send_empty_chunked_trailer) {
716 (void)ctx;
717 struct tester tester;
718 ASSERT_SUCCESS(s_tester_init(&tester, allocator));
719 struct aws_http_headers *trailers = aws_http_headers_new(allocator);
720
721 /* Initialize and send the stream chunks */
722 static const struct aws_byte_cursor body = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("write more tests");
723
724 const char *expected = "PUT /plan.txt HTTP/1.1\r\n"
725 "Transfer-Encoding: chunked\r\n"
726 "\r\n"
727 "10\r\n"
728 "write more tests"
729 "\r\n"
730 "0\r\n"
731 "\r\n";
732
733 ASSERT_SUCCESS(chunked_test_helper(&body, trailers, expected, tester, allocator));
734 /* clean up */
735 aws_http_headers_release(trailers);
736 ASSERT_SUCCESS(s_tester_clean_up(&tester));
737 return AWS_OP_SUCCESS;
738 }
739
740 H1_CLIENT_TEST_CASE(h1_client_request_forbidden_trailer) {
741 (void)ctx;
742 struct tester tester;
743 ASSERT_SUCCESS(s_tester_init(&tester, allocator));
744
745 struct aws_http_headers *success = aws_http_headers_new(allocator);
746 aws_http_headers_add_header(
747 success,
748 &(struct aws_http_header){
749 .name = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("should"),
750 .value = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("succeed"),
751 });
752 struct aws_http_headers *transfer_encoding = aws_http_headers_new(allocator);
753 aws_http_headers_add_header(
754 transfer_encoding,
755 &(struct aws_http_header){
756 .name = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("Transfer-Encoding"),
757 .value = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("gzip, chunked"),
758 });
759 struct aws_http_headers *content_length = aws_http_headers_new(allocator);
760 aws_http_headers_add_header(
761 content_length,
762 &(struct aws_http_header){
763 .name = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("Content-Length"),
764 .value = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("3495"),
765 });
766 struct aws_http_headers *host = aws_http_headers_new(allocator);
767 aws_http_headers_add_header(
768 host,
769 &(struct aws_http_header){
770 .name = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("Host"),
771 .value = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("www.example.org"),
772 });
773 struct aws_http_headers *cache_control = aws_http_headers_new(allocator);
774 aws_http_headers_add_header(
775 cache_control,
776 &(struct aws_http_header){
777 .name = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("Cache-Control"),
778 .value = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("private"),
779 });
780 struct aws_http_headers *expect = aws_http_headers_new(allocator);
781 aws_http_headers_add_header(
782 expect,
783 &(struct aws_http_header){
784 .name = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("Expect"),
785 .value = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("100-continue"),
786 });
787 struct aws_http_headers *max_forwards = aws_http_headers_new(allocator);
788 aws_http_headers_add_header(
789 max_forwards,
790 &(struct aws_http_header){
791 .name = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("max-forwards"),
792 .value = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("123"),
793 });
794 struct aws_http_headers *pragma = aws_http_headers_new(allocator);
795 aws_http_headers_add_header(
796 pragma,
797 &(struct aws_http_header){
798 .name = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("pragma"),
799 .value = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("no-cache"),
800 });
801 struct aws_http_headers *range = aws_http_headers_new(allocator);
802 aws_http_headers_add_header(
803 range,
804 &(struct aws_http_header){
805 .name = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("range"),
806 .value = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("bytes=0-1023"),
807 });
808 struct aws_http_headers *te = aws_http_headers_new(allocator);
809 aws_http_headers_add_header(
810 te,
811 &(struct aws_http_header){
812 .name = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("te"),
813 .value = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("trailers, deflate;q=0.5"),
814 });
815 struct aws_http_headers *www_authenticate = aws_http_headers_new(allocator);
816 aws_http_headers_add_header(
817 www_authenticate,
818 &(struct aws_http_header){
819 .name = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("www-authenticate"),
820 .value = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL(
821 "Newauth realm=\"apps\", type=1,title=\"Login to \"apps\"\", Basic realm=\"simple\""),
822 });
823 struct aws_http_headers *authorization = aws_http_headers_new(allocator);
824 aws_http_headers_add_header(
825 authorization,
826 &(struct aws_http_header){
827 .name = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("authorization"),
828 .value = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("credentials"),
829 });
830 struct aws_http_headers *proxy_authenticate = aws_http_headers_new(allocator);
831 aws_http_headers_add_header(
832 proxy_authenticate,
833 &(struct aws_http_header){
834 .name = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("proxy-authenticate"),
835 .value = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("Basic YWxhZGRpbjpvcGVuc2VzYW1l"),
836 });
837 struct aws_http_headers *proxy_authorization = aws_http_headers_new(allocator);
838 aws_http_headers_add_header(
839 proxy_authorization,
840 &(struct aws_http_header){
841 .name = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("proxy-authorization"),
842 .value = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("credentials"),
843 });
844 struct aws_http_headers *set_cookie = aws_http_headers_new(allocator);
845 aws_http_headers_add_header(
846 set_cookie,
847 &(struct aws_http_header){
848 .name = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("set-cookie"),
849 .value = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("sessionId=38afes7a8"),
850 });
851 struct aws_http_headers *cookie = aws_http_headers_new(allocator);
852 aws_http_headers_add_header(
853 cookie,
854 &(struct aws_http_header){
855 .name = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("cookie"),
856 .value =
857 AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("PHPSESSID=298zf09hf012fh2; csrftoken=u32t4o3tb3gg43; _gat=1"),
858 });
859 struct aws_http_headers *age = aws_http_headers_new(allocator);
860 aws_http_headers_add_header(
861 age,
862 &(struct aws_http_header){
863 .name = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("age"),
864 .value = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("24"),
865 });
866 struct aws_http_headers *expires = aws_http_headers_new(allocator);
867 aws_http_headers_add_header(
868 expires,
869 &(struct aws_http_header){
870 .name = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("expires"),
871 .value = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("Wed, 21 Oct 2015 07:28:00 GMT"),
872 });
873 struct aws_http_headers *date = aws_http_headers_new(allocator);
874 aws_http_headers_add_header(
875 date,
876 &(struct aws_http_header){
877 .name = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("date"),
878 .value = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("Wed, 21 Oct 2015 07:28:00 GMT"),
879 });
880 struct aws_http_headers *location = aws_http_headers_new(allocator);
881 aws_http_headers_add_header(
882 location,
883 &(struct aws_http_header){
884 .name = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("location"),
885 .value = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("/index.html"),
886 });
887 struct aws_http_headers *retry_after = aws_http_headers_new(allocator);
888 aws_http_headers_add_header(
889 retry_after,
890 &(struct aws_http_header){
891 .name = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("retry-after"),
892 .value = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("120"),
893 });
894 struct aws_http_headers *vary = aws_http_headers_new(allocator);
895 aws_http_headers_add_header(
896 vary,
897 &(struct aws_http_header){
898 .name = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("vary"),
899 .value = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("User-Agent"),
900 });
901
902 struct aws_http_headers *warning = aws_http_headers_new(allocator);
903 aws_http_headers_add_header(
904 warning,
905 &(struct aws_http_header){
906 .name = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("warning"),
907 .value = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("110 anderson/1.3.37 \"Response is stale\""),
908 });
909 struct aws_http_headers *content_encoding = aws_http_headers_new(allocator);
910 aws_http_headers_add_header(
911 content_encoding,
912 &(struct aws_http_header){
913 .name = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("content-encoding"),
914 .value = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("gzip"),
915 });
916
917 struct aws_http_headers *content_type = aws_http_headers_new(allocator);
918 aws_http_headers_add_header(
919 content_type,
920 &(struct aws_http_header){
921 .name = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("content-type"),
922 .value = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("text/html"),
923 });
924 struct aws_http_headers *content_range = aws_http_headers_new(allocator);
925 aws_http_headers_add_header(
926 content_range,
927 &(struct aws_http_header){
928 .name = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("content-range"),
929 .value = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("bytes 200-1000/67589"),
930 });
931
932 struct aws_http_headers *trailer = aws_http_headers_new(allocator);
933 aws_http_headers_add_header(
934 trailer,
935 &(struct aws_http_header){
936 .name = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("trailer"),
937 .value = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("Expires"),
938 });
939
940 ASSERT_SUCCESS(chunked_trailer_succeed(NULL, success, tester, allocator));
941 ASSERT_ERROR(
942 AWS_ERROR_HTTP_INVALID_HEADER_FIELD, chunked_trailer_succeed(NULL, transfer_encoding, tester, allocator));
943 ASSERT_ERROR(AWS_ERROR_HTTP_INVALID_HEADER_FIELD, chunked_trailer_succeed(NULL, content_length, tester, allocator));
944 ASSERT_ERROR(AWS_ERROR_HTTP_INVALID_HEADER_FIELD, chunked_trailer_succeed(NULL, host, tester, allocator));
945 ASSERT_ERROR(AWS_ERROR_HTTP_INVALID_HEADER_FIELD, chunked_trailer_succeed(NULL, cache_control, tester, allocator));
946 ASSERT_ERROR(AWS_ERROR_HTTP_INVALID_HEADER_FIELD, chunked_trailer_succeed(NULL, expect, tester, allocator));
947 ASSERT_ERROR(AWS_ERROR_HTTP_INVALID_HEADER_FIELD, chunked_trailer_succeed(NULL, max_forwards, tester, allocator));
948 ASSERT_ERROR(AWS_ERROR_HTTP_INVALID_HEADER_FIELD, chunked_trailer_succeed(NULL, pragma, tester, allocator));
949 ASSERT_ERROR(AWS_ERROR_HTTP_INVALID_HEADER_FIELD, chunked_trailer_succeed(NULL, range, tester, allocator));
950 ASSERT_ERROR(AWS_ERROR_HTTP_INVALID_HEADER_FIELD, chunked_trailer_succeed(NULL, te, tester, allocator));
951 ASSERT_ERROR(
952 AWS_ERROR_HTTP_INVALID_HEADER_FIELD, chunked_trailer_succeed(NULL, www_authenticate, tester, allocator));
953 ASSERT_ERROR(AWS_ERROR_HTTP_INVALID_HEADER_FIELD, chunked_trailer_succeed(NULL, authorization, tester, allocator));
954 ASSERT_ERROR(
955 AWS_ERROR_HTTP_INVALID_HEADER_FIELD, chunked_trailer_succeed(NULL, proxy_authenticate, tester, allocator));
956 ASSERT_ERROR(
957 AWS_ERROR_HTTP_INVALID_HEADER_FIELD, chunked_trailer_succeed(NULL, proxy_authorization, tester, allocator));
958 ASSERT_ERROR(AWS_ERROR_HTTP_INVALID_HEADER_FIELD, chunked_trailer_succeed(NULL, set_cookie, tester, allocator));
959 ASSERT_ERROR(AWS_ERROR_HTTP_INVALID_HEADER_FIELD, chunked_trailer_succeed(NULL, cookie, tester, allocator));
960 ASSERT_ERROR(AWS_ERROR_HTTP_INVALID_HEADER_FIELD, chunked_trailer_succeed(NULL, age, tester, allocator));
961 ASSERT_ERROR(AWS_ERROR_HTTP_INVALID_HEADER_FIELD, chunked_trailer_succeed(NULL, expires, tester, allocator));
962 ASSERT_ERROR(AWS_ERROR_HTTP_INVALID_HEADER_FIELD, chunked_trailer_succeed(NULL, date, tester, allocator));
963 ASSERT_ERROR(AWS_ERROR_HTTP_INVALID_HEADER_FIELD, chunked_trailer_succeed(NULL, location, tester, allocator));
964 ASSERT_ERROR(AWS_ERROR_HTTP_INVALID_HEADER_FIELD, chunked_trailer_succeed(NULL, retry_after, tester, allocator));
965 ASSERT_ERROR(AWS_ERROR_HTTP_INVALID_HEADER_FIELD, chunked_trailer_succeed(NULL, vary, tester, allocator));
966 ASSERT_ERROR(AWS_ERROR_HTTP_INVALID_HEADER_FIELD, chunked_trailer_succeed(NULL, warning, tester, allocator));
967 ASSERT_ERROR(
968 AWS_ERROR_HTTP_INVALID_HEADER_FIELD, chunked_trailer_succeed(NULL, content_encoding, tester, allocator));
969 ASSERT_ERROR(AWS_ERROR_HTTP_INVALID_HEADER_FIELD, chunked_trailer_succeed(NULL, content_type, tester, allocator));
970 ASSERT_ERROR(AWS_ERROR_HTTP_INVALID_HEADER_FIELD, chunked_trailer_succeed(NULL, content_range, tester, allocator));
971 ASSERT_ERROR(AWS_ERROR_HTTP_INVALID_HEADER_FIELD, chunked_trailer_succeed(NULL, trailer, tester, allocator));
972 /* clean up */
973 aws_http_headers_release(success);
974 aws_http_headers_release(transfer_encoding);
975 aws_http_headers_release(content_length);
976 aws_http_headers_release(host);
977 aws_http_headers_release(cache_control);
978 aws_http_headers_release(expect);
979 aws_http_headers_release(max_forwards);
980 aws_http_headers_release(pragma);
981 aws_http_headers_release(range);
982 aws_http_headers_release(te);
983 aws_http_headers_release(www_authenticate);
984 aws_http_headers_release(authorization);
985 aws_http_headers_release(proxy_authenticate);
986 aws_http_headers_release(proxy_authorization);
987 aws_http_headers_release(set_cookie);
988 aws_http_headers_release(cookie);
989 aws_http_headers_release(age);
990 aws_http_headers_release(expires);
991 aws_http_headers_release(date);
992 aws_http_headers_release(location);
993 aws_http_headers_release(retry_after);
994 aws_http_headers_release(vary);
995 aws_http_headers_release(warning);
996 aws_http_headers_release(content_encoding);
997 aws_http_headers_release(content_type);
998 aws_http_headers_release(content_range);
999 aws_http_headers_release(trailer);
1000 ASSERT_SUCCESS(s_tester_clean_up(&tester));
1001 return AWS_OP_SUCCESS;
1002 }
1003
1004 H1_CLIENT_TEST_CASE(h1_client_request_send_chunked_extensions) {
1005 (void)ctx;
1006 struct tester tester;
1007 ASSERT_SUCCESS(s_tester_init(&tester, allocator));
1008
1009 /* send request */
1010 struct aws_http_message *request = s_new_default_chunked_put_request(allocator);
1011 struct aws_http_make_request_options opt = {
1012 .self_size = sizeof(opt),
1013 .request = request,
1014 };
1015 struct aws_http_stream *stream = aws_http_connection_make_request(tester.connection, &opt);
1016 ASSERT_NOT_NULL(stream);
1017 ASSERT_SUCCESS(aws_http_stream_activate(stream));
1018
1019 /* Initialize and send the stream chunks */
1020 static const struct aws_byte_cursor body = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("write more tests");
1021
1022 /* create a chunk with a single extension */
1023 struct aws_input_stream *body_stream = aws_input_stream_new_from_cursor(allocator, &body);
1024 struct aws_http1_chunk_options options = s_default_chunk_options(body_stream, body.len);
1025 struct aws_http1_chunk_extension single_extension[] = {
1026 {
1027 .key = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("foo"),
1028 .value = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("bar"),
1029 },
1030 };
1031 options.extensions = (struct aws_http1_chunk_extension *)&single_extension;
1032 options.num_extensions = AWS_ARRAY_SIZE(single_extension);
1033 ASSERT_SUCCESS(aws_http1_stream_write_chunk(stream, &options));
1034
1035 /* create a chunk with a multiple_single extensions */
1036 static const struct aws_byte_cursor multi_ext_body = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("write more tests");
1037 struct aws_input_stream *multi_ext_body_stream = aws_input_stream_new_from_cursor(allocator, &multi_ext_body);
1038 struct aws_http1_chunk_options multi_ext_opts = s_default_chunk_options(multi_ext_body_stream, multi_ext_body.len);
1039 struct aws_http1_chunk_extension multi_extension[] = {
1040 {
1041 .key = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("foo"),
1042 .value = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("bar"),
1043 },
1044 {
1045 .key = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("baz"),
1046 .value = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("cux"),
1047 },
1048 };
1049 multi_ext_opts.extensions = (struct aws_http1_chunk_extension *)&multi_extension;
1050 multi_ext_opts.num_extensions = AWS_ARRAY_SIZE(multi_extension);
1051 ASSERT_SUCCESS(aws_http1_stream_write_chunk(stream, &multi_ext_opts));
1052
1053 /* terminate the stream */
1054 ASSERT_SUCCESS(s_write_termination_chunk(allocator, stream));
1055
1056 /* Run it! */
1057 testing_channel_drain_queued_tasks(&tester.testing_channel);
1058
1059 /* check result */
1060 const char *expected = "PUT /plan.txt HTTP/1.1\r\n"
1061 "Transfer-Encoding: chunked\r\n"
1062 "\r\n"
1063 "10;foo=bar\r\n"
1064 "write more tests"
1065 "\r\n"
1066 "10;foo=bar;baz=cux\r\n"
1067 "write more tests"
1068 "\r\n"
1069 "0\r\n"
1070 "\r\n";
1071
1072 ASSERT_SUCCESS(testing_channel_check_written_messages_str(&tester.testing_channel, allocator, expected));
1073
1074 /* clean up */
1075 aws_http_message_destroy(request);
1076 aws_http_stream_release(stream);
1077
1078 ASSERT_SUCCESS(s_tester_clean_up(&tester));
1079 return AWS_OP_SUCCESS;
1080 }
1081
1082 struct chunk_writer_data {
1083 size_t num_chunks;
1084 const char **payloads;
1085 struct aws_http_stream *stream;
1086 struct aws_allocator *allocator;
1087 long delay_between_writes_ns;
1088 };
1089
1090 H1_CLIENT_TEST_CASE(h1_client_request_waits_for_chunks) {
1091 (void)ctx;
1092 struct tester tester;
1093 ASSERT_SUCCESS(s_tester_init(&tester, allocator));
1094
1095 /* send request with Transfer-Encoding: chunked and body stream */
1096 struct aws_http_message *request = s_new_default_chunked_put_request(allocator);
1097 struct aws_http_make_request_options opt = {
1098 .self_size = sizeof(opt),
1099 .request = request,
1100 };
1101 struct aws_http_stream *stream = aws_http_connection_make_request(tester.connection, &opt);
1102 ASSERT_NOT_NULL(stream);
1103 /* activate stream *before* sending any data. */
1104 ASSERT_SUCCESS(aws_http_stream_activate(stream));
1105
1106 char *payloads[] = {"write more tests", "write more tests", ""};
1107 struct chunk_writer_data chunk_data = {
1108 .num_chunks = sizeof(payloads) / sizeof(payloads[0]),
1109 .payloads = (const char **)&payloads,
1110 .stream = stream,
1111 .allocator = allocator,
1112 .delay_between_writes_ns = 10000,
1113 };
1114
1115 /* write and pause, in a loop. This exercises the rescheduling path. */
1116 for (size_t i = 0; i < chunk_data.num_chunks; ++i) {
1117 testing_channel_drain_queued_tasks(&tester.testing_channel);
1118 ASSERT_FALSE(
1119 aws_task_scheduler_has_tasks(&tester.testing_channel.loop_impl->scheduler, NULL),
1120 "Everything should be paused when no chunks are pending");
1121
1122 struct aws_byte_cursor body = aws_byte_cursor_from_c_str(chunk_data.payloads[i]);
1123 struct aws_input_stream *body_stream = aws_input_stream_new_from_cursor(chunk_data.allocator, &body);
1124 struct aws_http1_chunk_options options = s_default_chunk_options(body_stream, body.len);
1125 ASSERT_SUCCESS(aws_http1_stream_write_chunk(stream, &options));
1126
1127 testing_channel_drain_queued_tasks(&tester.testing_channel);
1128 ASSERT_FALSE(
1129 aws_task_scheduler_has_tasks(&tester.testing_channel.loop_impl->scheduler, NULL),
1130 "Everything should be paused when no chunks are pending");
1131 }
1132
1133 /* check result */
1134 const char *expected = "PUT /plan.txt HTTP/1.1\r\n"
1135 "Transfer-Encoding: chunked\r\n"
1136 "\r\n"
1137 "10\r\n"
1138 "write more tests"
1139 "\r\n"
1140 "10\r\n"
1141 "write more tests"
1142 "\r\n"
1143 "0\r\n"
1144 "\r\n";
1145
1146 /* check result */
1147 ASSERT_SUCCESS(testing_channel_check_written_messages(
1148 &tester.testing_channel, allocator, aws_byte_cursor_from_c_str(expected)));
1149
1150 /* clean up */
1151 aws_http_message_destroy(request);
1152 aws_http_stream_release(stream);
1153
1154 ASSERT_SUCCESS(s_tester_clean_up(&tester));
1155 return AWS_OP_SUCCESS;
1156 }
1157
1158 struct chunk_that_writes_another {
1159 struct aws_allocator *allocator;
1160 struct aws_input_stream *body_data;
1161 };
1162
1163 static void s_on_chunk_complete_write_another_chunk(struct aws_http_stream *stream, int error_code, void *user_data) {
1164 AWS_FATAL_ASSERT(0 == error_code);
1165 struct chunk_that_writes_another *chunk = user_data;
1166
1167 aws_input_stream_release(chunk->body_data);
1168
1169 const struct aws_byte_cursor chunk2_body = aws_byte_cursor_from_c_str("chunk 2.");
1170 struct aws_input_stream *chunk2_body_stream = aws_input_stream_new_from_cursor(chunk->allocator, &chunk2_body);
1171 AWS_FATAL_ASSERT(chunk2_body_stream != NULL);
1172
1173 struct aws_http1_chunk_options chunk2_options = s_default_chunk_options(chunk2_body_stream, chunk2_body.len);
1174 AWS_FATAL_ASSERT(AWS_OP_SUCCESS == aws_http1_stream_write_chunk(stream, &chunk2_options));
1175 }
1176
1177 /* Test that it's safe to start a new chunk from the chunk-complete callback */
1178 H1_CLIENT_TEST_CASE(h1_client_request_send_chunk_from_chunk_complete_callback) {
1179 (void)ctx;
1180 struct tester tester;
1181 ASSERT_SUCCESS(s_tester_init(&tester, allocator));
1182
1183 /* send request with Transfer-Encoding: chunked and body stream */
1184 struct aws_http_message *request = s_new_default_chunked_put_request(allocator);
1185 struct aws_http_make_request_options opt = {
1186 .self_size = sizeof(opt),
1187 .request = request,
1188 };
1189 struct aws_http_stream *stream = aws_http_connection_make_request(tester.connection, &opt);
1190 ASSERT_NOT_NULL(stream);
1191
1192 /* activate stream *before* sending any data. */
1193 aws_http_stream_activate(stream);
1194
1195 /* write chunk 1 */
1196 const struct aws_byte_cursor chunk1_body = aws_byte_cursor_from_c_str("chunk 1.");
1197 struct aws_input_stream *chunk1_body_stream = aws_input_stream_new_from_cursor(allocator, &chunk1_body);
1198 ASSERT_NOT_NULL(chunk1_body_stream);
1199
1200 struct chunk_that_writes_another chunk1_userdata = {
1201 .allocator = allocator,
1202 .body_data = chunk1_body_stream,
1203 };
1204
1205 struct aws_http1_chunk_options chunk1_options = {
1206 .chunk_data = chunk1_body_stream,
1207 .chunk_data_size = chunk1_body.len,
1208 .on_complete = s_on_chunk_complete_write_another_chunk,
1209 .user_data = &chunk1_userdata,
1210 };
1211
1212 ASSERT_SUCCESS(aws_http1_stream_write_chunk(stream, &chunk1_options));
1213
1214 /* run tasks, the 1st chunk should complete, which writes the 2nd chunk,
1215 * which should also complete */
1216 testing_channel_drain_queued_tasks(&tester.testing_channel);
1217
1218 const char *expected = "PUT /plan.txt HTTP/1.1\r\n"
1219 "Transfer-Encoding: chunked\r\n"
1220 "\r\n"
1221 "8\r\n"
1222 "chunk 1."
1223 "\r\n"
1224 "8\r\n"
1225 "chunk 2."
1226 "\r\n";
1227 ASSERT_SUCCESS(testing_channel_check_written_messages_str(&tester.testing_channel, allocator, expected));
1228
1229 /* clean up */
1230 aws_http_message_destroy(request);
1231 aws_http_stream_release(stream);
1232 ASSERT_SUCCESS(s_tester_clean_up(&tester));
1233 return AWS_OP_SUCCESS;
1234 }
1235
1236 /* Regression test: Once upon a time there was a bug where the outgoing_stream_task got double-scheduled.
1237 * The situation was:
1238 * - An aws_io_message had been written to the channel, but not yet completed.
1239 * - The encoder was paused, waiting for more chunks.
1240 * Then at more-or-less the same time:
1241 * - The aws_io_message finished writing, which resulted in the outgoing_stream_task getting rescheduled.
1242 * - A new chunk was added, which resulted in the outgoing_stream_task getting rescheduled.
1243 * And then the task's linked_list_node got all screwed up and we crashed iterating a list. */
1244 H1_CLIENT_TEST_CASE(h1_client_request_write_chunk_as_write_completes_regression) {
1245 (void)ctx;
1246 struct tester tester;
1247 ASSERT_SUCCESS(s_tester_init(&tester, allocator));
1248
1249 /* To repro this bug, testing channel must wait to mark aws_io_messages complete */
1250 testing_channel_complete_written_messages_immediately(&tester.testing_channel, false, 0);
1251
1252 /* Send request */
1253 struct aws_http_message *request = s_new_default_chunked_put_request(allocator);
1254 struct aws_http_make_request_options opt = {
1255 .self_size = sizeof(opt),
1256 .request = request,
1257 };
1258 struct aws_http_stream *stream = aws_http_connection_make_request(tester.connection, &opt);
1259 ASSERT_NOT_NULL(stream);
1260 ASSERT_SUCCESS(aws_http_stream_activate(stream));
1261
1262 /* Drain tasks. The head of the request gets written to aws_io_message and sent down channel.
1263 * The outgoing_stream_task should be paused waiting for more chunks. */
1264 testing_channel_drain_queued_tasks(&tester.testing_channel);
1265
1266 /* Write a chunk. When bug occurred, this would reschedule the outgoing_stream_task */
1267 static const struct aws_byte_cursor body = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("write more tests");
1268 struct aws_input_stream *body_stream = aws_input_stream_new_from_cursor(allocator, &body);
1269 struct aws_http1_chunk_options options = s_default_chunk_options(body_stream, body.len);
1270 ASSERT_SUCCESS(aws_http1_stream_write_chunk(stream, &options));
1271 ASSERT_SUCCESS(s_write_termination_chunk(allocator, stream));
1272
1273 /* Have the previously sent aws_io_message complete.
1274 * When bug occurred, this would ALSO reschedule the outgoing_stream_task */
1275 struct aws_linked_list *written_msgs = testing_channel_get_written_message_queue(&tester.testing_channel);
1276 for (struct aws_linked_list_node *node = aws_linked_list_begin(written_msgs);
1277 node != aws_linked_list_end(written_msgs);
1278 node = aws_linked_list_next(node)) {
1279
1280 struct aws_io_message *msg = AWS_CONTAINER_OF(node, struct aws_io_message, queueing_handle);
1281 msg->on_completion(tester.testing_channel.channel, msg, 0, msg->user_data);
1282 msg->on_completion = NULL;
1283 }
1284
1285 /* Run the scheduler. When bug occurred, this would crash */
1286 testing_channel_drain_queued_tasks(&tester.testing_channel);
1287
1288 /* check result */
1289 const char *expected = "PUT /plan.txt HTTP/1.1\r\n"
1290 "Transfer-Encoding: chunked\r\n"
1291 "\r\n"
1292 "10\r\n"
1293 "write more tests"
1294 "\r\n"
1295 "0\r\n"
1296 "\r\n";
1297
1298 ASSERT_SUCCESS(testing_channel_check_written_messages_str(&tester.testing_channel, allocator, expected));
1299
1300 /* clean up */
1301 aws_http_message_destroy(request);
1302 aws_http_stream_release(stream);
1303
1304 ASSERT_SUCCESS(s_tester_clean_up(&tester));
1305 return AWS_OP_SUCCESS;
1306 }
1307
1308 H1_CLIENT_TEST_CASE(h1_client_request_content_length_0_ok) {
1309 (void)ctx;
1310 struct tester tester;
1311 ASSERT_SUCCESS(s_tester_init(&tester, allocator));
1312
1313 /* send request with Content-Length: 0 and NO body stream */
1314 struct aws_http_header headers[] = {
1315 {
1316 .name = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("Content-Length"),
1317 .value = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("0"),
1318 },
1319 };
1320
1321 struct aws_http_message *request = aws_http_message_new_request(allocator);
1322 ASSERT_NOT_NULL(request);
1323 ASSERT_SUCCESS(aws_http_message_set_request_method(request, aws_byte_cursor_from_c_str("PUT")));
1324 ASSERT_SUCCESS(aws_http_message_set_request_path(request, aws_byte_cursor_from_c_str("/plan.txt")));
1325 aws_http_message_add_header_array(request, headers, AWS_ARRAY_SIZE(headers));
1326 struct aws_http_make_request_options opt = {
1327 .self_size = sizeof(opt),
1328 .request = request,
1329 };
1330 struct aws_http_stream *stream = aws_http_connection_make_request(tester.connection, &opt);
1331 ASSERT_NOT_NULL(stream);
1332 ASSERT_SUCCESS(aws_http_stream_activate(stream));
1333
1334 testing_channel_drain_queued_tasks(&tester.testing_channel);
1335
1336 /* check result */
1337 const char *expected = "PUT /plan.txt HTTP/1.1\r\n"
1338 "Content-Length: 0\r\n"
1339 "\r\n";
1340 ASSERT_SUCCESS(testing_channel_check_written_message_str(&tester.testing_channel, expected));
1341 aws_http_stream_release(stream);
1342
1343 /* send Content-Length: 0 request again, but this time with a body stream whose length is 0 */
1344 static const struct aws_byte_cursor body = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("");
1345 struct aws_input_stream *body_stream = aws_input_stream_new_from_cursor(allocator, &body);
1346 aws_http_message_set_body_stream(request, body_stream);
1347
1348 stream = aws_http_connection_make_request(tester.connection, &opt);
1349 ASSERT_NOT_NULL(stream);
1350 ASSERT_SUCCESS(aws_http_stream_activate(stream));
1351
1352 testing_channel_drain_queued_tasks(&tester.testing_channel);
1353
1354 /* check result */
1355 ASSERT_SUCCESS(testing_channel_check_written_message_str(&tester.testing_channel, expected));
1356
1357 /* clean up */
1358 aws_input_stream_release(body_stream);
1359 aws_http_message_destroy(request);
1360 aws_http_stream_release(stream);
1361
1362 ASSERT_SUCCESS(s_tester_clean_up(&tester));
1363 return AWS_OP_SUCCESS;
1364 }
1365
1366 H1_CLIENT_TEST_CASE(h1_client_request_send_chunk_size_0_ok) {
1367 (void)ctx;
1368 struct tester tester;
1369 ASSERT_SUCCESS(s_tester_init(&tester, allocator));
1370
1371 /* Send request with Transfer-Encoding: chunked and an empty body stream. */
1372 struct aws_http_message *request = s_new_default_chunked_put_request(allocator);
1373 struct aws_http_make_request_options opt = {
1374 .self_size = sizeof(opt),
1375 .request = request,
1376 };
1377
1378 struct aws_http_stream *stream = aws_http_connection_make_request(tester.connection, &opt);
1379 ASSERT_NOT_NULL(stream);
1380 ASSERT_SUCCESS(aws_http_stream_activate(stream));
1381
1382 ASSERT_SUCCESS(s_write_termination_chunk(allocator, stream));
1383 testing_channel_drain_queued_tasks(&tester.testing_channel);
1384 /* check result */
1385 const char *expected = "PUT /plan.txt HTTP/1.1\r\n"
1386 "Transfer-Encoding: chunked\r\n"
1387 "\r\n"
1388 "0\r\n"
1389 "\r\n";
1390
1391 ASSERT_SUCCESS(testing_channel_check_written_messages_str(&tester.testing_channel, allocator, expected));
1392
1393 /* clean up */
1394 aws_http_message_destroy(request);
1395 aws_http_stream_release(stream);
1396
1397 ASSERT_SUCCESS(s_tester_clean_up(&tester));
1398 return AWS_OP_SUCCESS;
1399 }
1400
1401 H1_CLIENT_TEST_CASE(h1_client_request_send_chunk_size_0_with_extensions_ok) {
1402 (void)ctx;
1403 struct tester tester;
1404 ASSERT_SUCCESS(s_tester_init(&tester, allocator));
1405
1406 /* Send request with Transfer-Encoding: chunked and an empty body stream. */
1407 struct aws_http_message *request = s_new_default_chunked_put_request(allocator);
1408 struct aws_http_make_request_options opt = {
1409 .self_size = sizeof(opt),
1410 .request = request,
1411 };
1412
1413 struct aws_http_stream *stream = aws_http_connection_make_request(tester.connection, &opt);
1414 ASSERT_NOT_NULL(stream);
1415 ASSERT_SUCCESS(aws_http_stream_activate(stream));
1416
1417 static const struct aws_byte_cursor empty_str = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("");
1418 struct aws_input_stream *termination_marker = aws_input_stream_new_from_cursor(allocator, &empty_str);
1419 struct aws_http1_chunk_options options = s_default_chunk_options(termination_marker, empty_str.len);
1420 struct aws_http1_chunk_extension single_extension[] = {
1421 {
1422 .key = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("foo"),
1423 .value = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("bar"),
1424 },
1425 {
1426 .key = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("baz"),
1427 .value = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("cux"),
1428 },
1429 };
1430 options.extensions = (struct aws_http1_chunk_extension *)&single_extension;
1431 options.num_extensions = AWS_ARRAY_SIZE(single_extension);
1432 ASSERT_SUCCESS(aws_http1_stream_write_chunk(stream, &options));
1433
1434 testing_channel_drain_queued_tasks(&tester.testing_channel);
1435 /* check result */
1436 const char *expected = "PUT /plan.txt HTTP/1.1\r\n"
1437 "Transfer-Encoding: chunked\r\n"
1438 "\r\n"
1439 "0;foo=bar;baz=cux\r\n"
1440 "\r\n";
1441
1442 ASSERT_SUCCESS(testing_channel_check_written_messages_str(&tester.testing_channel, allocator, expected));
1443
1444 /* clean up */
1445 aws_http_message_destroy(request);
1446 aws_http_stream_release(stream);
1447
1448 ASSERT_SUCCESS(s_tester_clean_up(&tester));
1449 return AWS_OP_SUCCESS;
1450 }
1451
1452 /* Send a request (with streaming body and defined Content-Length) whose body doesn't fit in a single aws_io_message */
1453 H1_CLIENT_TEST_CASE(h1_client_request_send_large_body) {
1454 (void)ctx;
1455 struct tester tester;
1456 ASSERT_SUCCESS(s_tester_init(&tester, allocator));
1457
1458 /* send request with large body full of random data */
1459 size_t body_len = 1024 * 1024 * 1; /* 1MB */
1460 struct aws_byte_buf body_buf;
1461 ASSERT_SUCCESS(aws_byte_buf_init(&body_buf, allocator, body_len));
1462 while (body_buf.len < body_len) {
1463 int r = rand();
1464 aws_byte_buf_write_be32(&body_buf, (uint32_t)r);
1465 }
1466
1467 const struct aws_byte_cursor body = aws_byte_cursor_from_buf(&body_buf);
1468 struct aws_input_stream *body_stream = aws_input_stream_new_from_cursor(allocator, &body);
1469
1470 char content_length_value[100];
1471 snprintf(content_length_value, sizeof(content_length_value), "%zu", body_len);
1472 struct aws_http_header headers[] = {
1473 {
1474 .name = aws_byte_cursor_from_c_str("Content-Length"),
1475 .value = aws_byte_cursor_from_c_str(content_length_value),
1476 },
1477 };
1478
1479 struct aws_http_message *request = aws_http_message_new_request(allocator);
1480 ASSERT_NOT_NULL(request);
1481 ASSERT_SUCCESS(aws_http_message_set_request_method(request, aws_byte_cursor_from_c_str("PUT")));
1482 ASSERT_SUCCESS(aws_http_message_set_request_path(request, aws_byte_cursor_from_c_str("/large.txt")));
1483 aws_http_message_add_header_array(request, headers, AWS_ARRAY_SIZE(headers));
1484 aws_http_message_set_body_stream(request, body_stream);
1485
1486 struct aws_http_make_request_options opt = {
1487 .self_size = sizeof(opt),
1488 .request = request,
1489 };
1490 struct aws_http_stream *stream = aws_http_connection_make_request(tester.connection, &opt);
1491 ASSERT_NOT_NULL(stream);
1492 ASSERT_SUCCESS(aws_http_stream_activate(stream));
1493
1494 /* check result */
1495 const char *expected_head_fmt = "PUT /large.txt HTTP/1.1\r\n"
1496 "Content-Length: %zu\r\n"
1497 "\r\n";
1498 char expected_head[1024];
1499 int expected_head_len = snprintf(expected_head, sizeof(expected_head), expected_head_fmt, body_len);
1500
1501 struct aws_byte_buf expected_buf;
1502 ASSERT_SUCCESS(aws_byte_buf_init(&expected_buf, allocator, body_len + expected_head_len));
1503 ASSERT_TRUE(aws_byte_buf_write(&expected_buf, (uint8_t *)expected_head, expected_head_len));
1504 ASSERT_TRUE(aws_byte_buf_write_from_whole_buffer(&expected_buf, body_buf));
1505
1506 testing_channel_drain_queued_tasks(&tester.testing_channel);
1507 ASSERT_SUCCESS(testing_channel_check_written_messages(
1508 &tester.testing_channel, allocator, aws_byte_cursor_from_buf(&expected_buf)));
1509
1510 /* clean up */
1511 aws_input_stream_release(body_stream);
1512 aws_http_message_destroy(request);
1513 aws_http_stream_release(stream);
1514
1515 ASSERT_SUCCESS(s_tester_clean_up(&tester));
1516
1517 aws_byte_buf_clean_up(&body_buf);
1518 aws_byte_buf_clean_up(&expected_buf);
1519 return AWS_OP_SUCCESS;
1520 }
1521
1522 static int s_check_chunked_streaming_request(
1523 struct aws_byte_cursor actual_full_request_message,
1524 struct aws_byte_cursor expected_head,
1525 struct aws_byte_cursor expected_body_data) {
1526
1527 /* check request head */
1528 ASSERT_TRUE(actual_full_request_message.len >= expected_head.len);
1529 struct aws_byte_cursor actual_chunked_body = actual_full_request_message;
1530 struct aws_byte_cursor actual_next = aws_byte_cursor_advance(&actual_chunked_body, expected_head.len);
1531 ASSERT_BIN_ARRAYS_EQUALS(expected_head.ptr, expected_head.len, actual_next.ptr, actual_next.len);
1532
1533 /* parse body chunks.
1534 * (currently, when doing streaming body chunks, we never use chunk extensions) */
1535 struct aws_byte_cursor crlf_cursor = aws_byte_cursor_from_c_str("\r\n");
1536 while (true) {
1537 /* Each chunk is prefixed with: CHUNK-LENGTH-IN-HEX CRLF */
1538 struct aws_byte_cursor next_crlf;
1539 ASSERT_SUCCESS(aws_byte_cursor_find_exact(&actual_chunked_body, &crlf_cursor, &next_crlf));
1540 size_t hex_len = next_crlf.ptr - actual_chunked_body.ptr;
1541 actual_next = aws_byte_cursor_advance(&actual_chunked_body, hex_len);
1542 uint64_t chunk_len;
1543 ASSERT_SUCCESS(aws_byte_cursor_utf8_parse_u64_hex(actual_next, &chunk_len));
1544 actual_next = aws_byte_cursor_advance(&actual_chunked_body, 2);
1545 ASSERT_CURSOR_VALUE_CSTRING_EQUALS(actual_next, "\r\n");
1546
1547 /* length of 0 indicates last chunk*/
1548 if (chunk_len == 0) {
1549 ASSERT_UINT_EQUALS(0, expected_body_data.len, "message ended early");
1550 break;
1551 }
1552
1553 /* check that chunk's data matches what's next in expected_body_data */
1554 ASSERT_TRUE(chunk_len <= actual_chunked_body.len);
1555 ASSERT_TRUE(chunk_len <= expected_body_data.len);
1556 actual_next = aws_byte_cursor_advance(&actual_chunked_body, (size_t)chunk_len);
1557 ASSERT_BIN_ARRAYS_EQUALS(expected_body_data.ptr, (size_t)chunk_len, actual_next.ptr, actual_next.len);
1558 aws_byte_cursor_advance(&expected_body_data, (size_t)chunk_len);
1559
1560 /* CRLF at end of each chunk */
1561 actual_next = aws_byte_cursor_advance(&actual_chunked_body, 2);
1562 ASSERT_CURSOR_VALUE_CSTRING_EQUALS(actual_next, "\r\n", "expected CRLF at end of chunk");
1563 }
1564
1565 /* parse trailer
1566 * (currently, when doing streaming body chunks, trailer is always empty) */
1567 actual_next = aws_byte_cursor_advance(&actual_chunked_body, 2);
1568 ASSERT_CURSOR_VALUE_CSTRING_EQUALS(actual_next, "\r\n", "expected CRLF after empty chunk trailer");
1569
1570 /* assert there's nothing left over */
1571 ASSERT_UINT_EQUALS(0, actual_chunked_body.len, "extra bytes at end of message");
1572 return AWS_OP_SUCCESS;
1573 }
1574
1575 /* Send a request (with streaming body and chunked encoding) whose body doesn't fit in a single aws_io_message */
1576 H1_CLIENT_TEST_CASE(h1_client_request_send_large_body_chunked_and_streaming) {
1577 (void)ctx;
1578 struct tester tester;
1579 ASSERT_SUCCESS(s_tester_init(&tester, allocator));
1580
1581 /* send request with large body full of random data */
1582 size_t body_len = 1024 * 1024 * 1; /* 1MB */
1583 struct aws_byte_buf body_buf;
1584 ASSERT_SUCCESS(aws_byte_buf_init(&body_buf, allocator, body_len));
1585 while (body_buf.len < body_len) {
1586 int r = rand();
1587 aws_byte_buf_write_be32(&body_buf, (uint32_t)r);
1588 }
1589
1590 const struct aws_byte_cursor body = aws_byte_cursor_from_buf(&body_buf);
1591 struct aws_input_stream *body_stream = aws_input_stream_new_from_cursor(allocator, &body);
1592
1593 struct aws_http_header headers[] = {
1594 {
1595 .name = aws_byte_cursor_from_c_str("Transfer-Encoding"),
1596 .value = aws_byte_cursor_from_c_str("chunked"),
1597 },
1598 };
1599
1600 struct aws_http_message *request = aws_http_message_new_request(allocator);
1601 ASSERT_NOT_NULL(request);
1602 ASSERT_SUCCESS(aws_http_message_set_request_method(request, aws_byte_cursor_from_c_str("PUT")));
1603 ASSERT_SUCCESS(aws_http_message_set_request_path(request, aws_byte_cursor_from_c_str("/large.txt")));
1604 aws_http_message_add_header_array(request, headers, AWS_ARRAY_SIZE(headers));
1605 aws_http_message_set_body_stream(request, body_stream);
1606
1607 struct aws_http_make_request_options opt = {
1608 .self_size = sizeof(opt),
1609 .request = request,
1610 };
1611 struct aws_http_stream *stream = aws_http_connection_make_request(tester.connection, &opt);
1612 ASSERT_NOT_NULL(stream);
1613 ASSERT_SUCCESS(aws_http_stream_activate(stream));
1614
1615 /* check result */
1616 const char *expected_head = "PUT /large.txt HTTP/1.1\r\n"
1617 "Transfer-Encoding: chunked\r\n"
1618 "\r\n";
1619
1620 testing_channel_drain_queued_tasks(&tester.testing_channel);
1621
1622 struct aws_byte_buf written_buf;
1623 ASSERT_SUCCESS(aws_byte_buf_init(&written_buf, allocator, body_len * 2));
1624 ASSERT_SUCCESS(testing_channel_drain_written_messages(&tester.testing_channel, &written_buf));
1625
1626 ASSERT_SUCCESS(s_check_chunked_streaming_request(
1627 aws_byte_cursor_from_buf(&written_buf) /*actual_full_request_message*/,
1628 aws_byte_cursor_from_c_str(expected_head),
1629 body /*expected_body_data*/));
1630
1631 /* clean up */
1632 aws_input_stream_release(body_stream);
1633 aws_http_message_destroy(request);
1634 aws_http_stream_release(stream);
1635 aws_byte_buf_clean_up(&body_buf);
1636 aws_byte_buf_clean_up(&written_buf);
1637
1638 ASSERT_SUCCESS(s_tester_clean_up(&tester));
1639
1640 aws_thread_current_sleep(100);
1641 return AWS_OP_SUCCESS;
1642 }
1643
1644 static int s_parse_chunked_extensions(
1645 const char *extensions,
1646 struct aws_http1_chunk_extension *expected_extensions,
1647 size_t num_extensions) {
1648
1649 size_t i;
1650 for (i = 0; i < num_extensions; ++i) {
1651 struct aws_http1_chunk_extension *expected_extension = expected_extensions + i;
1652 /* parse the key */
1653 char *key_val_delimiter = strchr(extensions, '=');
1654 if (NULL == key_val_delimiter) {
1655 return false;
1656 }
1657 *key_val_delimiter = '\0';
1658 struct aws_byte_cursor key = aws_byte_cursor_from_c_str(extensions);
1659 ASSERT_BIN_ARRAYS_EQUALS(expected_extension->key.ptr, expected_extension->key.len, key.ptr, key.len);
1660 extensions = key_val_delimiter + 1;
1661
1662 /* parse the value */
1663 char *val_end_delimiter = strchr(extensions, ';');
1664 if (NULL != val_end_delimiter) {
1665 *val_end_delimiter = '\0';
1666 }
1667 struct aws_byte_cursor value = aws_byte_cursor_from_c_str(extensions++);
1668 ASSERT_BIN_ARRAYS_EQUALS(expected_extension->value.ptr, expected_extension->value.len, value.ptr, value.len);
1669 extensions = val_end_delimiter + 1;
1670 }
1671 if (i == num_extensions) {
1672 return AWS_OP_SUCCESS;
1673 } else {
1674 return AWS_OP_ERR;
1675 }
1676 }
1677
1678 static int s_can_parse_as_chunked_encoding(
1679 struct aws_allocator *allocator,
1680 struct aws_byte_buf *chunked_http_request_headers_and_body,
1681 struct aws_byte_buf *expected_head,
1682 struct aws_http1_chunk_extension *expected_extensions,
1683 size_t num_extensions,
1684 char body_char) {
1685
1686 /* Check that the HTTP header matches the expected value */
1687 ASSERT_TRUE(chunked_http_request_headers_and_body->len > expected_head->len);
1688 ASSERT_BIN_ARRAYS_EQUALS(
1689 expected_head->buffer, expected_head->len, chunked_http_request_headers_and_body->buffer, expected_head->len);
1690
1691 /* move the cursor past the head and enter the chunked body */
1692 struct aws_byte_cursor request_cursor = aws_byte_cursor_from_buf(chunked_http_request_headers_and_body);
1693 aws_byte_cursor_advance(&request_cursor, expected_head->len);
1694 struct aws_byte_cursor crlf_cursor = aws_byte_cursor_from_c_str("\r\n");
1695 struct aws_byte_cursor match_cursor;
1696 /* Provide a max iterations in case of a bug the test doesn't infinite loop but fails fast. */
1697 int max_iter = 128;
1698 int i = 0;
1699 /* 3MB to hold a massive chunked extension size */
1700 char *chunk_ascii_hex = aws_mem_calloc(allocator, 3, 1024 * 1024);
1701 for (i = 0; i < max_iter; ++i) {
1702 ASSERT_SUCCESS(aws_byte_cursor_find_exact(&request_cursor, &crlf_cursor, &match_cursor));
1703 memset(chunk_ascii_hex, 0, 3 * 1024 * 1024);
1704 memcpy(chunk_ascii_hex, (char *)request_cursor.ptr, match_cursor.ptr - request_cursor.ptr);
1705 char *chunk_ext_start = strchr(chunk_ascii_hex, ';');
1706 if (NULL != chunk_ext_start) {
1707 /* write a null character over where the first ';' of the stream so that strtol finds the right hex size. */
1708 *chunk_ext_start = '\0';
1709 if (0 < num_extensions) {
1710 ++chunk_ext_start;
1711 ASSERT_SUCCESS(s_parse_chunked_extensions(chunk_ext_start, expected_extensions, num_extensions));
1712 }
1713 }
1714 long chunk_size = strtol((char *)chunk_ascii_hex, 0, 16);
1715 long total_chunk_size_with_overhead =
1716 (long)(match_cursor.ptr - request_cursor.ptr /* size of the chunk in ascii hex */
1717 + crlf_cursor.len /* size of the crlf */
1718 + chunk_size /* size of the payload */
1719 + crlf_cursor.len); /* size of the chunk terminating crlf */
1720
1721 /* 0 length chunk signals end of stream. Check for the terminatino string and exit with success */
1722 if (0 == chunk_size) {
1723 struct aws_byte_cursor terminate_cursor = aws_byte_cursor_from_c_str("0\r\n\r\n");
1724 ASSERT_TRUE(aws_byte_cursor_eq(&request_cursor, &terminate_cursor));
1725 break;
1726 }
1727
1728 /* The buffer should be filled with the character specified for the whole length of the chunk */
1729 for (int j = (int)(match_cursor.ptr - request_cursor.ptr + crlf_cursor.len); j < chunk_size; ++j) {
1730 ASSERT_TRUE(body_char == (char)request_cursor.ptr[j]);
1731 }
1732 /* advance to the next chunk */
1733 aws_byte_cursor_advance(&request_cursor, total_chunk_size_with_overhead);
1734 }
1735 aws_mem_release(allocator, chunk_ascii_hex);
1736 /* Test that we didn't exit the loop due to hitting the max iterations */
1737 ASSERT_TRUE(i < (max_iter - 1));
1738
1739 return AWS_OP_SUCCESS;
1740 }
1741
1742 /* Send a request (using the write_chunk() API) whose body doesn't fit in a single aws_io_message */
1743 H1_CLIENT_TEST_CASE(h1_client_request_send_large_body_chunked) {
1744 (void)ctx;
1745 struct tester tester;
1746 ASSERT_SUCCESS(s_tester_init(&tester, allocator));
1747
1748 struct aws_http_header headers[] = {
1749 {
1750 .name = aws_byte_cursor_from_c_str("Transfer-Encoding"),
1751 .value = aws_byte_cursor_from_c_str("chunked"),
1752 },
1753 };
1754
1755 struct aws_http_message *request = aws_http_message_new_request(allocator);
1756 ASSERT_NOT_NULL(request);
1757 ASSERT_SUCCESS(aws_http_message_set_request_method(request, aws_byte_cursor_from_c_str("PUT")));
1758 ASSERT_SUCCESS(aws_http_message_set_request_path(request, aws_byte_cursor_from_c_str("/large.txt")));
1759 aws_http_message_add_header_array(request, headers, AWS_ARRAY_SIZE(headers));
1760
1761 struct aws_http_make_request_options opt = {
1762 .self_size = sizeof(opt),
1763 .request = request,
1764 };
1765 struct aws_http_stream *stream = aws_http_connection_make_request(tester.connection, &opt);
1766 ASSERT_NOT_NULL(stream);
1767 ASSERT_SUCCESS(aws_http_stream_activate(stream));
1768
1769 /* Initialize and send the stream chunks */
1770 /* send request with large body full of data */
1771 size_t body_len = 1024 * 1024 * 1; /* 1MB */
1772 struct aws_byte_buf body_buf;
1773 ASSERT_SUCCESS(aws_byte_buf_init(&body_buf, allocator, body_len));
1774 char body_char = 'z';
1775 while (body_buf.len < body_len) {
1776 aws_byte_buf_write_u8(&body_buf, body_char);
1777 }
1778
1779 const struct aws_byte_cursor body = aws_byte_cursor_from_buf(&body_buf);
1780 struct aws_input_stream *body_stream = aws_input_stream_new_from_cursor(allocator, &body);
1781 struct aws_http1_chunk_options options = s_default_chunk_options(body_stream, body.len);
1782
1783 ASSERT_SUCCESS(aws_http1_stream_write_chunk(stream, &options));
1784 /* this call will trigger a pause/wake internally after a large write */
1785 testing_channel_drain_queued_tasks(&tester.testing_channel);
1786 ASSERT_SUCCESS(s_write_termination_chunk(allocator, stream));
1787
1788 /* check result */
1789 const char expected_head_fmt[] = "PUT /large.txt HTTP/1.1\r\n"
1790 "Transfer-Encoding: chunked\r\n"
1791 "\r\n";
1792 struct aws_byte_buf expected_head_buf = aws_byte_buf_from_c_str((char *)&expected_head_fmt);
1793
1794 testing_channel_drain_queued_tasks(&tester.testing_channel);
1795
1796 struct aws_byte_buf written_buf;
1797 ASSERT_SUCCESS(aws_byte_buf_init(&written_buf, allocator, body_len * 2));
1798 ASSERT_SUCCESS(testing_channel_drain_written_messages(&tester.testing_channel, &written_buf));
1799
1800 ASSERT_SUCCESS(s_can_parse_as_chunked_encoding(allocator, &written_buf, &expected_head_buf, NULL, 0, body_char));
1801
1802 /* clean up */
1803 aws_http_message_destroy(request);
1804 aws_http_stream_release(stream);
1805
1806 ASSERT_SUCCESS(s_tester_clean_up(&tester));
1807
1808 aws_byte_buf_clean_up(&body_buf);
1809 aws_byte_buf_clean_up(&expected_head_buf);
1810 aws_byte_buf_clean_up(&written_buf);
1811 return AWS_OP_SUCCESS;
1812 }
1813
1814 H1_CLIENT_TEST_CASE(h1_client_request_send_large_chunk_extensions) {
1815 (void)ctx;
1816 struct tester tester;
1817 ASSERT_SUCCESS(s_tester_init(&tester, allocator));
1818
1819 struct aws_http_header headers[] = {
1820 {
1821 .name = aws_byte_cursor_from_c_str("Transfer-Encoding"),
1822 .value = aws_byte_cursor_from_c_str("chunked"),
1823 },
1824 };
1825
1826 struct aws_http_message *request = aws_http_message_new_request(allocator);
1827 ASSERT_NOT_NULL(request);
1828 ASSERT_SUCCESS(aws_http_message_set_request_method(request, aws_byte_cursor_from_c_str("PUT")));
1829 ASSERT_SUCCESS(aws_http_message_set_request_path(request, aws_byte_cursor_from_c_str("/large.txt")));
1830 aws_http_message_add_header_array(request, headers, AWS_ARRAY_SIZE(headers));
1831
1832 struct aws_http_make_request_options opt = {
1833 .self_size = sizeof(opt),
1834 .request = request,
1835 };
1836 struct aws_http_stream *stream = aws_http_connection_make_request(tester.connection, &opt);
1837 ASSERT_NOT_NULL(stream);
1838 ASSERT_SUCCESS(aws_http_stream_activate(stream));
1839
1840 /* Initialize and send the stream chunks */
1841 /* send request with large body full of data */
1842 size_t body_len = 1024 * 1024 * 1; /* 1MB */
1843 struct aws_byte_buf body_buf;
1844 ASSERT_SUCCESS(aws_byte_buf_init(&body_buf, allocator, body_len));
1845 char body_char = 'z';
1846 while (body_buf.len < body_len) {
1847 aws_byte_buf_write_u8(&body_buf, body_char);
1848 }
1849
1850 const struct aws_byte_cursor body = aws_byte_cursor_from_buf(&body_buf);
1851 struct aws_input_stream *body_stream = aws_input_stream_new_from_cursor(allocator, &body);
1852 struct aws_http1_chunk_options options = s_default_chunk_options(body_stream, body.len);
1853 /* No one should ever be using 1MB extensions. In fact, it is a DDoS vector to your server and you should protect
1854 * against it for any sort of production software. That said, the spec doesn't place a size limit on how much the
1855 * client can send. For this test, we have a 1MB key and a 1MB value in each pair respectively to test that the
1856 * state machine can fill across the key/value larger than the size of a message in the channel. */
1857 struct aws_http1_chunk_extension extensions[] = {
1858 {
1859 .key = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("foo"),
1860 .value = aws_byte_cursor_from_buf(&body_buf),
1861 },
1862 {
1863 .key = aws_byte_cursor_from_buf(&body_buf),
1864 .value = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("bar"),
1865 },
1866 };
1867 options.extensions = (struct aws_http1_chunk_extension *)&extensions;
1868 options.num_extensions = AWS_ARRAY_SIZE(extensions);
1869
1870 ASSERT_SUCCESS(aws_http1_stream_write_chunk(stream, &options));
1871 /* this call will trigger a pause/wake internally after a large write */
1872 testing_channel_drain_queued_tasks(&tester.testing_channel);
1873 ASSERT_SUCCESS(s_write_termination_chunk(allocator, stream));
1874
1875 /* check result */
1876 const char expected_head_fmt[] = "PUT /large.txt HTTP/1.1\r\n"
1877 "Transfer-Encoding: chunked\r\n"
1878 "\r\n";
1879 struct aws_byte_buf expected_head_buf = aws_byte_buf_from_c_str((char *)&expected_head_fmt);
1880
1881 struct aws_http1_chunk_extension expected_extensions[] = {
1882 {
1883 .key = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("foo"),
1884 .value = aws_byte_cursor_from_buf(&body_buf),
1885 },
1886 {
1887 .key = aws_byte_cursor_from_buf(&body_buf),
1888 .value = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("bar"),
1889 },
1890 };
1891
1892 testing_channel_drain_queued_tasks(&tester.testing_channel);
1893
1894 struct aws_byte_buf written_buf;
1895 ASSERT_SUCCESS(aws_byte_buf_init(&written_buf, allocator, body_len * 2));
1896 ASSERT_SUCCESS(testing_channel_drain_written_messages(&tester.testing_channel, &written_buf));
1897
1898 ASSERT_SUCCESS(s_can_parse_as_chunked_encoding(
1899 allocator, &written_buf, &expected_head_buf, expected_extensions, AWS_ARRAY_SIZE(extensions), body_char));
1900
1901 /* clean up */
1902 aws_http_message_destroy(request);
1903 aws_http_stream_release(stream);
1904
1905 ASSERT_SUCCESS(s_tester_clean_up(&tester));
1906
1907 aws_byte_buf_clean_up(&body_buf);
1908 aws_byte_buf_clean_up(&expected_head_buf);
1909 aws_byte_buf_clean_up(&written_buf);
1910 return AWS_OP_SUCCESS;
1911 }
1912
1913 /* Send a request whose headers don't fit in a single aws_io_message */
1914 H1_CLIENT_TEST_CASE(h1_client_request_send_large_head) {
1915 (void)ctx;
1916 struct tester tester;
1917 ASSERT_SUCCESS(s_tester_init(&tester, allocator));
1918
1919 /* Generate headers while filling in contents of `expected` buffer */
1920 struct aws_http_header headers[1000];
1921 size_t num_headers = AWS_ARRAY_SIZE(headers);
1922 AWS_ZERO_STRUCT(headers);
1923
1924 struct aws_byte_buf expected;
1925 aws_byte_buf_init(&expected, allocator, num_headers * 128); /* approx capacity */
1926
1927 struct aws_byte_cursor request_line = aws_byte_cursor_from_c_str("GET / HTTP/1.1\r\n");
1928 ASSERT_TRUE(aws_byte_buf_write_from_whole_cursor(&expected, request_line));
1929
1930 /* Each header just has a UUID for its name and value */
1931 for (size_t i = 0; i < num_headers; ++i) {
1932 struct aws_http_header *header = headers + i;
1933
1934 /* Point to where the UUID is going to be written in the `expected` buffer */
1935 header->name = aws_byte_cursor_from_array(expected.buffer + expected.len, AWS_UUID_STR_LEN - 1);
1936 header->value = header->name;
1937
1938 struct aws_uuid uuid;
1939 ASSERT_SUCCESS(aws_uuid_init(&uuid));
1940
1941 ASSERT_SUCCESS(aws_uuid_to_str(&uuid, &expected));
1942 ASSERT_TRUE(aws_byte_buf_write(&expected, (uint8_t *)": ", 2));
1943 ASSERT_SUCCESS(aws_uuid_to_str(&uuid, &expected));
1944 ASSERT_TRUE(aws_byte_buf_write(&expected, (uint8_t *)"\r\n", 2));
1945 }
1946
1947 ASSERT_TRUE(aws_byte_buf_write(&expected, (uint8_t *)"\r\n", 2));
1948
1949 struct aws_http_message *request = s_new_default_get_request(allocator);
1950 ASSERT_SUCCESS(aws_http_message_add_header_array(request, headers, AWS_ARRAY_SIZE(headers)));
1951
1952 /* send request */
1953 struct aws_http_make_request_options opt = {
1954 .self_size = sizeof(opt),
1955 .request = request,
1956 };
1957 struct aws_http_stream *stream = aws_http_connection_make_request(tester.connection, &opt);
1958 ASSERT_NOT_NULL(stream);
1959 ASSERT_SUCCESS(aws_http_stream_activate(stream));
1960
1961 /* check result */
1962 testing_channel_drain_queued_tasks(&tester.testing_channel);
1963 ASSERT_SUCCESS(testing_channel_check_written_messages(
1964 &tester.testing_channel, allocator, aws_byte_cursor_from_buf(&expected)));
1965
1966 /* clean up */
1967 aws_http_message_destroy(request);
1968 aws_http_stream_release(stream);
1969
1970 ASSERT_SUCCESS(s_tester_clean_up(&tester));
1971
1972 aws_byte_buf_clean_up(&expected);
1973 return AWS_OP_SUCCESS;
1974 }
1975
1976 /* Check that if many requests are made (pipelining) they all get sent */
1977 H1_CLIENT_TEST_CASE(h1_client_request_send_multiple) {
1978 (void)ctx;
1979 struct tester tester;
1980 ASSERT_SUCCESS(s_tester_init(&tester, allocator));
1981
1982 /* send requests */
1983 struct aws_http_make_request_options opt = {
1984 .self_size = sizeof(opt),
1985 .request = s_new_default_get_request(allocator),
1986 };
1987
1988 struct aws_http_stream *streams[3];
1989 size_t num_streams = AWS_ARRAY_SIZE(streams);
1990 for (size_t i = 0; i < num_streams; ++i) {
1991 streams[i] = aws_http_connection_make_request(tester.connection, &opt);
1992 ASSERT_NOT_NULL(streams[i]);
1993 ASSERT_SUCCESS(aws_http_stream_activate(streams[i]));
1994 }
1995
1996 testing_channel_drain_queued_tasks(&tester.testing_channel);
1997
1998 /* Ensure the request can be destroyed after request is sent */
1999 aws_http_message_destroy(opt.request);
2000
2001 /* check result */
2002 const char *expected = "GET / HTTP/1.1\r\n"
2003 "\r\n"
2004 "GET / HTTP/1.1\r\n"
2005 "\r\n"
2006 "GET / HTTP/1.1\r\n"
2007 "\r\n";
2008 ASSERT_SUCCESS(testing_channel_check_written_messages_str(&tester.testing_channel, allocator, expected));
2009
2010 /* clean up */
2011 for (size_t i = 0; i < num_streams; ++i) {
2012 aws_http_stream_release(streams[i]);
2013 }
2014
2015 ASSERT_SUCCESS(s_tester_clean_up(&tester));
2016 return AWS_OP_SUCCESS;
2017 }
2018
2019 /* Check that if many requests are made (pipelining) they all get sent */
2020 H1_CLIENT_TEST_CASE(h1_client_request_send_multiple_chunked_encoding) {
2021 (void)ctx;
2022 struct tester tester;
2023 ASSERT_SUCCESS(s_tester_init(&tester, allocator));
2024
2025 /* send requests */
2026 struct aws_http_make_request_options opt = {
2027 .self_size = sizeof(opt),
2028 .request = s_new_default_chunked_put_request(allocator),
2029 };
2030
2031 struct aws_http_stream *streams[3];
2032 struct aws_byte_buf index_strs[AWS_ARRAY_SIZE(streams)];
2033 size_t num_streams = AWS_ARRAY_SIZE(streams);
2034 for (size_t i = 0; i < num_streams; ++i) {
2035 streams[i] = aws_http_connection_make_request(tester.connection, &opt);
2036 ASSERT_NOT_NULL(streams[i]);
2037 ASSERT_SUCCESS(aws_byte_buf_init(&index_strs[i], allocator, 4));
2038 index_strs[i].len = snprintf((char *)index_strs[i].buffer, index_strs[i].capacity, "%03zu", i);
2039 ASSERT_SUCCESS(aws_http_stream_activate(streams[i]));
2040 }
2041
2042 /* All streams will pause and wait for data */
2043 testing_channel_drain_queued_tasks(&tester.testing_channel);
2044
2045 /* Write to all the streams */
2046 for (size_t i = 0; i < num_streams; ++i) {
2047 static const struct aws_byte_cursor body = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("write more tests");
2048 struct aws_byte_cursor index_str_cursor = aws_byte_cursor_from_buf(&index_strs[i]);
2049
2050 struct aws_input_stream *body_stream = aws_input_stream_new_from_cursor(allocator, &body);
2051 struct aws_input_stream *index_stream = aws_input_stream_new_from_cursor(allocator, &index_str_cursor);
2052
2053 struct aws_http1_chunk_options options_1 = s_default_chunk_options(body_stream, body.len);
2054 struct aws_http1_chunk_options options_2 = s_default_chunk_options(index_stream, index_str_cursor.len);
2055
2056 ASSERT_SUCCESS(aws_http1_stream_write_chunk(streams[i], &options_1));
2057 ASSERT_SUCCESS(aws_http1_stream_write_chunk(streams[i], &options_2));
2058 ASSERT_SUCCESS(s_write_termination_chunk(allocator, streams[i]));
2059 }
2060
2061 testing_channel_drain_queued_tasks(&tester.testing_channel);
2062 /* Ensure the request can be destroyed after request is sent */
2063 aws_http_message_destroy(opt.request);
2064
2065 /* check result */
2066 const char *expected = "PUT /plan.txt HTTP/1.1\r\n"
2067 "Transfer-Encoding: chunked\r\n"
2068 "\r\n"
2069 "10\r\n"
2070 "write more tests"
2071 "\r\n"
2072 "3\r\n"
2073 "000"
2074 "\r\n"
2075 "0\r\n"
2076 "\r\n"
2077 "PUT /plan.txt HTTP/1.1\r\n"
2078 "Transfer-Encoding: chunked\r\n"
2079 "\r\n"
2080 "10\r\n"
2081 "write more tests"
2082 "\r\n"
2083 "3\r\n"
2084 "001"
2085 "\r\n"
2086 "0\r\n"
2087 "\r\n"
2088 "PUT /plan.txt HTTP/1.1\r\n"
2089 "Transfer-Encoding: chunked\r\n"
2090 "\r\n"
2091 "10\r\n"
2092 "write more tests"
2093 "\r\n"
2094 "3\r\n"
2095 "002"
2096 "\r\n"
2097 "0\r\n"
2098 "\r\n";
2099
2100 testing_channel_drain_queued_tasks(&tester.testing_channel);
2101 ASSERT_SUCCESS(testing_channel_check_written_messages_str(&tester.testing_channel, allocator, expected));
2102
2103 /* clean up */
2104 for (size_t i = 0; i < num_streams; ++i) {
2105 aws_http_stream_release(streams[i]);
2106 aws_byte_buf_clean_up(&index_strs[i]);
2107 }
2108
2109 ASSERT_SUCCESS(s_tester_clean_up(&tester));
2110 return AWS_OP_SUCCESS;
2111 }
2112
2113 static int s_stream_tester_init(
2114 struct client_stream_tester *tester,
2115 struct tester *main_tester,
2116 struct aws_http_message *request) {
2117
2118 struct client_stream_tester_options options = {
2119 .request = request,
2120 .connection = main_tester->connection,
2121 };
2122 return client_stream_tester_init(tester, main_tester->alloc, &options);
2123 }
2124
2125 H1_CLIENT_TEST_CASE(h1_client_stream_release_after_complete) {
2126 (void)ctx;
2127 struct tester tester;
2128 ASSERT_SUCCESS(s_tester_init(&tester, allocator));
2129
2130 /* send request */
2131 struct aws_http_message *request = s_new_default_get_request(allocator);
2132
2133 struct client_stream_tester stream_tester;
2134 ASSERT_SUCCESS(s_stream_tester_init(&stream_tester, &tester, request));
2135
2136 testing_channel_drain_queued_tasks(&tester.testing_channel);
2137
2138 /* Ensure the request can be destroyed after request is sent */
2139 aws_http_message_destroy(request);
2140
2141 /* send response */
2142 ASSERT_SUCCESS(testing_channel_push_read_str(&tester.testing_channel, "HTTP/1.1 204 No Content\r\n\r\n"));
2143
2144 testing_channel_drain_queued_tasks(&tester.testing_channel);
2145
2146 /* check result */
2147 ASSERT_TRUE(stream_tester.complete);
2148 ASSERT_FALSE(stream_tester.destroyed);
2149
2150 aws_http_stream_release(stream_tester.stream);
2151 stream_tester.stream = NULL;
2152 ASSERT_TRUE(stream_tester.destroyed);
2153
2154 /* clean up */
2155 client_stream_tester_clean_up(&stream_tester);
2156 ASSERT_SUCCESS(s_tester_clean_up(&tester));
2157 return AWS_OP_SUCCESS;
2158 }
2159
2160 H1_CLIENT_TEST_CASE(h1_client_stream_release_before_complete) {
2161 (void)ctx;
2162 struct tester tester;
2163 ASSERT_SUCCESS(s_tester_init(&tester, allocator));
2164
2165 /* send request */
2166 struct aws_http_message *request = s_new_default_get_request(allocator);
2167
2168 struct client_stream_tester stream_tester;
2169 ASSERT_SUCCESS(s_stream_tester_init(&stream_tester, &tester, request));
2170 aws_http_stream_release(stream_tester.stream);
2171 stream_tester.stream = NULL;
2172 ASSERT_FALSE(stream_tester.destroyed);
2173
2174 testing_channel_drain_queued_tasks(&tester.testing_channel);
2175
2176 /* Ensure the request can be destroyed after request is sent */
2177 aws_http_message_destroy(request);
2178
2179 /* send response */
2180 ASSERT_SUCCESS(testing_channel_push_read_str(&tester.testing_channel, "HTTP/1.1 204 No Content\r\n\r\n"));
2181
2182 testing_channel_drain_queued_tasks(&tester.testing_channel);
2183
2184 /* check result */
2185 ASSERT_TRUE(stream_tester.complete);
2186 ASSERT_TRUE(stream_tester.destroyed);
2187
2188 /* clean up */
2189 client_stream_tester_clean_up(&stream_tester);
2190 ASSERT_SUCCESS(s_tester_clean_up(&tester));
2191 return AWS_OP_SUCCESS;
2192 }
2193
2194 H1_CLIENT_TEST_CASE(h1_client_response_get_1liner) {
2195 (void)ctx;
2196 struct tester tester;
2197 ASSERT_SUCCESS(s_tester_init(&tester, allocator));
2198
2199 /* send request */
2200 struct aws_http_message *request = s_new_default_get_request(allocator);
2201
2202 struct client_stream_tester stream_tester;
2203 ASSERT_SUCCESS(s_stream_tester_init(&stream_tester, &tester, request));
2204
2205 testing_channel_drain_queued_tasks(&tester.testing_channel);
2206
2207 /* Ensure the request can be destroyed after request is sent */
2208 aws_http_message_destroy(request);
2209
2210 /* send response */
2211 ASSERT_SUCCESS(testing_channel_push_read_str(&tester.testing_channel, "HTTP/1.1 204 No Content\r\n\r\n"));
2212
2213 testing_channel_drain_queued_tasks(&tester.testing_channel);
2214
2215 /* check result */
2216 ASSERT_TRUE(stream_tester.complete);
2217 ASSERT_INT_EQUALS(AWS_ERROR_SUCCESS, stream_tester.on_complete_error_code);
2218 ASSERT_INT_EQUALS(204, stream_tester.response_status);
2219 ASSERT_UINT_EQUALS(0, aws_http_headers_count(stream_tester.response_headers));
2220 ASSERT_UINT_EQUALS(0, stream_tester.response_body.len);
2221
2222 /* clean up */
2223 client_stream_tester_clean_up(&stream_tester);
2224 ASSERT_SUCCESS(s_tester_clean_up(&tester));
2225 return AWS_OP_SUCCESS;
2226 }
2227
2228 static int s_check_header(const struct aws_http_headers *headers, size_t i, const char *name_str, const char *value) {
2229
2230 size_t headers_num = aws_http_headers_count(headers);
2231 ASSERT_TRUE(i < headers_num);
2232 struct aws_http_header header;
2233 ASSERT_SUCCESS(aws_http_headers_get_index(headers, i, &header));
2234 ASSERT_TRUE(aws_byte_cursor_eq_c_str(&header.name, name_str));
2235 ASSERT_TRUE(aws_byte_cursor_eq_c_str(&header.value, value));
2236
2237 return AWS_OP_SUCCESS;
2238 }
2239
2240 static int s_check_info_response_header(
2241 const struct client_stream_tester *stream_tester,
2242 size_t response_i,
2243 size_t header_i,
2244 const char *name_str,
2245 const char *value) {
2246
2247 ASSERT_TRUE(response_i < stream_tester->num_info_responses);
2248 const struct aws_http_headers *headers =
2249 aws_http_message_get_const_headers(stream_tester->info_responses[response_i]);
2250 return s_check_header(headers, header_i, name_str, value);
2251 }
2252
2253 H1_CLIENT_TEST_CASE(h1_client_response_get_headers) {
2254 (void)ctx;
2255 struct tester tester;
2256 ASSERT_SUCCESS(s_tester_init(&tester, allocator));
2257
2258 /* send request */
2259 struct aws_http_message *request = s_new_default_get_request(allocator);
2260
2261 struct client_stream_tester stream_tester;
2262 ASSERT_SUCCESS(s_stream_tester_init(&stream_tester, &tester, request));
2263
2264 testing_channel_drain_queued_tasks(&tester.testing_channel);
2265
2266 /* Ensure the request can be destroyed after request is sent */
2267 aws_http_message_destroy(request);
2268
2269 /* send response */
2270 ASSERT_SUCCESS(testing_channel_push_read_str(
2271 &tester.testing_channel,
2272 "HTTP/1.1 308 Permanent Redirect\r\n"
2273 "Date: Fri, 01 Mar 2019 17:18:55 GMT\r\n"
2274 "Location: /index.html\r\n"
2275 "\r\n"));
2276
2277 testing_channel_drain_queued_tasks(&tester.testing_channel);
2278
2279 /* check result */
2280 ASSERT_TRUE(stream_tester.complete);
2281 ASSERT_INT_EQUALS(AWS_ERROR_SUCCESS, stream_tester.on_complete_error_code);
2282 ASSERT_INT_EQUALS(308, stream_tester.response_status);
2283 ASSERT_UINT_EQUALS(2, aws_http_headers_count(stream_tester.response_headers));
2284 ASSERT_SUCCESS(s_check_header(stream_tester.response_headers, 0, "Date", "Fri, 01 Mar 2019 17:18:55 GMT"));
2285 ASSERT_SUCCESS(s_check_header(stream_tester.response_headers, 1, "Location", "/index.html"));
2286 ASSERT_UINT_EQUALS(0, stream_tester.response_body.len);
2287
2288 /* clean up */
2289 client_stream_tester_clean_up(&stream_tester);
2290 ASSERT_SUCCESS(s_tester_clean_up(&tester));
2291 return AWS_OP_SUCCESS;
2292 }
2293
2294 H1_CLIENT_TEST_CASE(h1_client_response_get_body) {
2295 (void)ctx;
2296 struct tester tester;
2297 ASSERT_SUCCESS(s_tester_init(&tester, allocator));
2298
2299 /* send request */
2300 struct aws_http_message *request = s_new_default_get_request(allocator);
2301
2302 struct client_stream_tester stream_tester;
2303 ASSERT_SUCCESS(s_stream_tester_init(&stream_tester, &tester, request));
2304
2305 testing_channel_drain_queued_tasks(&tester.testing_channel);
2306
2307 /* Ensure the request can be destroyed after request is sent */
2308 aws_http_message_destroy(request);
2309
2310 /* send response */
2311 ASSERT_SUCCESS(testing_channel_push_read_str(
2312 &tester.testing_channel,
2313 "HTTP/1.1 200 OK\r\n"
2314 "Content-Length: 9\r\n"
2315 "\r\n"
2316 "Call Momo"));
2317
2318 testing_channel_drain_queued_tasks(&tester.testing_channel);
2319
2320 /* check result */
2321 ASSERT_TRUE(stream_tester.complete);
2322 ASSERT_INT_EQUALS(AWS_ERROR_SUCCESS, stream_tester.on_complete_error_code);
2323 ASSERT_INT_EQUALS(200, stream_tester.response_status);
2324 ASSERT_UINT_EQUALS(1, aws_http_headers_count(stream_tester.response_headers));
2325 ASSERT_SUCCESS(s_check_header(stream_tester.response_headers, 0, "Content-Length", "9"));
2326 ASSERT_TRUE(aws_byte_buf_eq_c_str(&stream_tester.response_body, "Call Momo"));
2327
2328 ASSERT_TRUE(stream_tester.metrics.receive_end_timestamp_ns > 0);
2329 ASSERT_TRUE(stream_tester.metrics.receive_start_timestamp_ns > 0);
2330 ASSERT_TRUE(stream_tester.metrics.receive_end_timestamp_ns > stream_tester.metrics.receive_start_timestamp_ns);
2331 ASSERT_TRUE(
2332 stream_tester.metrics.receiving_duration_ns ==
2333 stream_tester.metrics.receive_end_timestamp_ns - stream_tester.metrics.receive_start_timestamp_ns);
2334 ASSERT_TRUE(stream_tester.metrics.send_start_timestamp_ns > 0);
2335 ASSERT_TRUE(stream_tester.metrics.send_end_timestamp_ns > 0);
2336 ASSERT_TRUE(stream_tester.metrics.send_end_timestamp_ns > stream_tester.metrics.send_start_timestamp_ns);
2337 ASSERT_TRUE(
2338 stream_tester.metrics.sending_duration_ns ==
2339 stream_tester.metrics.send_end_timestamp_ns - stream_tester.metrics.send_start_timestamp_ns);
2340 ASSERT_TRUE(stream_tester.metrics.stream_id == stream_tester.stream->id);
2341
2342 /* clean up */
2343 client_stream_tester_clean_up(&stream_tester);
2344 ASSERT_SUCCESS(s_tester_clean_up(&tester));
2345 return AWS_OP_SUCCESS;
2346 }
2347
2348 static int s_test_expected_no_body_response(struct aws_allocator *allocator, int status_int, bool head_request) {
2349
2350 struct tester tester;
2351 ASSERT_SUCCESS(s_tester_init(&tester, allocator));
2352
2353 /* send request */
2354 struct aws_http_message *request =
2355 head_request ? s_new_default_head_request(allocator) : s_new_default_get_request(allocator);
2356
2357 struct client_stream_tester stream_tester;
2358 ASSERT_SUCCESS(s_stream_tester_init(&stream_tester, &tester, request));
2359
2360 testing_channel_drain_queued_tasks(&tester.testing_channel);
2361
2362 /* Ensure the request can be destroyed after request is sent */
2363 aws_http_message_destroy(request);
2364
2365 /* form response */
2366 struct aws_byte_cursor status_text = aws_byte_cursor_from_c_str(aws_http_status_text(status_int));
2367 char c_status_text[100];
2368 memcpy(c_status_text, status_text.ptr, status_text.len);
2369 c_status_text[status_text.len] = '\0';
2370 char response_text[500];
2371 char *response_headers = "Content-Length: 9\r\n"
2372 "\r\n";
2373 snprintf(response_text, sizeof(response_text), "HTTP/1.1 %d %s\r\n%s", status_int, c_status_text, response_headers);
2374 /* send response */
2375 ASSERT_SUCCESS(testing_channel_push_read_str(&tester.testing_channel, response_text));
2376
2377 testing_channel_drain_queued_tasks(&tester.testing_channel);
2378
2379 /* check result */
2380 ASSERT_TRUE(stream_tester.complete);
2381 ASSERT_INT_EQUALS(AWS_ERROR_SUCCESS, stream_tester.on_complete_error_code);
2382 ASSERT_INT_EQUALS(status_int, stream_tester.response_status);
2383 ASSERT_UINT_EQUALS(1, aws_http_headers_count(stream_tester.response_headers));
2384 ASSERT_SUCCESS(s_check_header(stream_tester.response_headers, 0, "Content-Length", "9"));
2385
2386 /* clean up */
2387 client_stream_tester_clean_up(&stream_tester);
2388 ASSERT_SUCCESS(s_tester_clean_up(&tester));
2389
2390 return AWS_OP_SUCCESS;
2391 }
2392
2393 H1_CLIENT_TEST_CASE(h1_client_response_get_no_body_for_head_request) {
2394 (void)ctx;
2395 ASSERT_SUCCESS(s_test_expected_no_body_response(allocator, 200, true));
2396 return AWS_OP_SUCCESS;
2397 }
2398
2399 H1_CLIENT_TEST_CASE(h1_client_response_get_no_body_from_304) {
2400 (void)ctx;
2401 ASSERT_SUCCESS(s_test_expected_no_body_response(allocator, 304, false));
2402 return AWS_OP_SUCCESS;
2403 }
2404
2405 H1_CLIENT_TEST_CASE(h1_client_response_get_100) {
2406 (void)ctx;
2407 struct tester tester;
2408 ASSERT_SUCCESS(s_tester_init(&tester, allocator));
2409
2410 /* send request */
2411 struct aws_http_message *request = s_new_default_get_request(allocator);
2412
2413 struct client_stream_tester stream_tester;
2414 ASSERT_SUCCESS(s_stream_tester_init(&stream_tester, &tester, request));
2415
2416 testing_channel_drain_queued_tasks(&tester.testing_channel);
2417
2418 /* Ensure the request can be destroyed after request is sent */
2419 aws_http_message_destroy(request);
2420
2421 /* send response */
2422 ASSERT_SUCCESS(testing_channel_push_read_str(
2423 &tester.testing_channel,
2424 "HTTP/1.1 100 Continue\r\n"
2425 "Date: Fri, 01 Mar 2019 17:18:55 GMT\r\n"
2426 "\r\n"
2427 "HTTP/1.1 200 OK\r\n"
2428 "Content-Length: 9\r\n"
2429 "\r\n"
2430 "Call Momo"));
2431
2432 testing_channel_drain_queued_tasks(&tester.testing_channel);
2433
2434 /* check result */
2435 ASSERT_TRUE(stream_tester.complete);
2436 ASSERT_INT_EQUALS(AWS_ERROR_SUCCESS, stream_tester.on_complete_error_code);
2437 ASSERT_INT_EQUALS(200, stream_tester.response_status);
2438
2439 ASSERT_UINT_EQUALS(1, stream_tester.num_info_responses);
2440 int info_response_status;
2441 ASSERT_SUCCESS(aws_http_message_get_response_status(stream_tester.info_responses[0], &info_response_status));
2442 ASSERT_INT_EQUALS(100, info_response_status);
2443 ASSERT_SUCCESS(s_check_info_response_header(&stream_tester, 0, 0, "Date", "Fri, 01 Mar 2019 17:18:55 GMT"));
2444 ASSERT_UINT_EQUALS(1, aws_http_headers_count(stream_tester.response_headers));
2445 ASSERT_SUCCESS(s_check_header(stream_tester.response_headers, 0, "Content-Length", "9"));
2446
2447 ASSERT_TRUE(aws_byte_buf_eq_c_str(&stream_tester.response_body, "Call Momo"));
2448
2449 /* clean up */
2450 client_stream_tester_clean_up(&stream_tester);
2451 ASSERT_SUCCESS(s_tester_clean_up(&tester));
2452 return AWS_OP_SUCCESS;
2453 }
2454
2455 /* Check that a response spread across multiple aws_io_messages comes through */
2456 H1_CLIENT_TEST_CASE(h1_client_response_get_1_from_multiple_io_messages) {
2457 (void)ctx;
2458 struct tester tester;
2459 ASSERT_SUCCESS(s_tester_init(&tester, allocator));
2460
2461 /* send request */
2462 struct aws_http_message *request = s_new_default_get_request(allocator);
2463
2464 struct client_stream_tester stream_tester;
2465 ASSERT_SUCCESS(s_stream_tester_init(&stream_tester, &tester, request));
2466
2467 testing_channel_drain_queued_tasks(&tester.testing_channel);
2468
2469 /* Ensure the request can be destroyed after request is sent */
2470 aws_http_message_destroy(request);
2471
2472 /* send response with each byte in its own aws_io_message */
2473 const char *response_str = "HTTP/1.1 200 OK\r\n"
2474 "Content-Length: 9\r\n"
2475 "\r\n"
2476 "Call Momo";
2477 size_t response_str_len = strlen(response_str);
2478 for (size_t i = 0; i < response_str_len; ++i) {
2479 ASSERT_SUCCESS(
2480 testing_channel_push_read_data(&tester.testing_channel, aws_byte_cursor_from_array(response_str + i, 1)));
2481 }
2482
2483 testing_channel_drain_queued_tasks(&tester.testing_channel);
2484
2485 /* check result */
2486 ASSERT_TRUE(stream_tester.complete);
2487 ASSERT_INT_EQUALS(AWS_ERROR_SUCCESS, stream_tester.on_complete_error_code);
2488 ASSERT_INT_EQUALS(200, stream_tester.response_status);
2489 ASSERT_UINT_EQUALS(1, aws_http_headers_count(stream_tester.response_headers));
2490 ASSERT_SUCCESS(s_check_header(stream_tester.response_headers, 0, "Content-Length", "9"));
2491 ASSERT_TRUE(aws_byte_buf_eq_c_str(&stream_tester.response_body, "Call Momo"));
2492
2493 /* clean up */
2494 client_stream_tester_clean_up(&stream_tester);
2495 ASSERT_SUCCESS(s_tester_clean_up(&tester));
2496 return AWS_OP_SUCCESS;
2497 }
2498
2499 /* Check that multiple responses in a single aws_io_message all come through */
2500 H1_CLIENT_TEST_CASE(h1_client_response_get_multiple_from_1_io_message) {
2501 (void)ctx;
2502 struct tester tester;
2503 ASSERT_SUCCESS(s_tester_init(&tester, allocator));
2504
2505 /* send requests */
2506 struct aws_http_message *request = s_new_default_get_request(allocator);
2507
2508 struct client_stream_tester stream_testers[3];
2509 for (size_t i = 0; i < AWS_ARRAY_SIZE(stream_testers); ++i) {
2510 ASSERT_SUCCESS(s_stream_tester_init(&stream_testers[i], &tester, request));
2511 }
2512 testing_channel_drain_queued_tasks(&tester.testing_channel);
2513
2514 /* Ensure the request can be destroyed after request is sent */
2515 aws_http_message_destroy(request);
2516
2517 /* send all responses in a single aws_io_message */
2518 ASSERT_SUCCESS(testing_channel_push_read_str(
2519 &tester.testing_channel,
2520 "HTTP/1.1 204 No Content\r\n\r\n"
2521 "HTTP/1.1 204 No Content\r\n\r\n"
2522 "HTTP/1.1 204 No Content\r\n\r\n"));
2523
2524 testing_channel_drain_queued_tasks(&tester.testing_channel);
2525
2526 /* check results */
2527 for (size_t i = 0; i < AWS_ARRAY_SIZE(stream_testers); ++i) {
2528 ASSERT_TRUE(stream_testers[i].complete);
2529 ASSERT_INT_EQUALS(AWS_ERROR_SUCCESS, stream_testers[i].on_complete_error_code);
2530 ASSERT_INT_EQUALS(204, stream_testers[i].response_status);
2531 ASSERT_UINT_EQUALS(0, aws_http_headers_count(stream_testers[i].response_headers));
2532 ASSERT_UINT_EQUALS(0, stream_testers[i].response_body.len);
2533
2534 client_stream_tester_clean_up(&stream_testers[i]);
2535 }
2536
2537 ASSERT_SUCCESS(s_tester_clean_up(&tester));
2538 return AWS_OP_SUCCESS;
2539 }
2540
2541 H1_CLIENT_TEST_CASE(h1_client_response_with_bad_data_shuts_down_connection) {
2542 (void)ctx;
2543 struct tester tester;
2544 ASSERT_SUCCESS(s_tester_init(&tester, allocator));
2545
2546 /* send request */
2547 struct aws_http_message *request = s_new_default_get_request(allocator);
2548
2549 struct client_stream_tester stream_tester;
2550 ASSERT_SUCCESS(s_stream_tester_init(&stream_tester, &tester, request));
2551
2552 testing_channel_drain_queued_tasks(&tester.testing_channel);
2553
2554 /* Ensure the request can be destroyed after request is sent */
2555 aws_http_message_destroy(request);
2556
2557 /* send response */
2558 ASSERT_SUCCESS(testing_channel_push_read_str_ignore_errors(&tester.testing_channel, "Mmmm garbage data\r\n\r\n"));
2559
2560 testing_channel_drain_queued_tasks(&tester.testing_channel);
2561
2562 /* check result */
2563 ASSERT_TRUE(stream_tester.complete);
2564 ASSERT_INT_EQUALS(AWS_ERROR_HTTP_PROTOCOL_ERROR, stream_tester.on_complete_error_code);
2565
2566 /* clean up */
2567 client_stream_tester_clean_up(&stream_tester);
2568 ASSERT_SUCCESS(s_tester_clean_up(&tester));
2569 return AWS_OP_SUCCESS;
2570 }
2571
2572 /* Test case is: 1 request has been sent. Then 2 responses arrive in 1 io message.
2573 * The 1st request should complete just fine, then the connection should shutdown with error */
2574 H1_CLIENT_TEST_CASE(h1_client_response_with_too_much_data_shuts_down_connection) {
2575 (void)ctx;
2576 struct tester tester;
2577 ASSERT_SUCCESS(s_tester_init(&tester, allocator));
2578
2579 /* send 1 request */
2580 struct aws_http_message *request = s_new_default_get_request(allocator);
2581
2582 struct client_stream_tester stream_tester;
2583 ASSERT_SUCCESS(s_stream_tester_init(&stream_tester, &tester, request));
2584 testing_channel_drain_queued_tasks(&tester.testing_channel);
2585
2586 /* Ensure the request can be destroyed after request is sent */
2587 aws_http_message_destroy(request);
2588
2589 /* send 2 responses in a single aws_io_message. */
2590 ASSERT_SUCCESS(testing_channel_push_read_str_ignore_errors(
2591 &tester.testing_channel,
2592 "HTTP/1.1 204 No Content\r\n\r\n"
2593 "HTTP/1.1 204 No Content\r\n\r\n"));
2594
2595 testing_channel_drain_queued_tasks(&tester.testing_channel);
2596
2597 /* 1st response should have come across successfully */
2598 ASSERT_TRUE(stream_tester.complete);
2599 ASSERT_INT_EQUALS(AWS_ERROR_SUCCESS, stream_tester.on_complete_error_code);
2600 ASSERT_INT_EQUALS(204, stream_tester.response_status);
2601 ASSERT_UINT_EQUALS(0, aws_http_headers_count(stream_tester.response_headers));
2602 ASSERT_UINT_EQUALS(0, stream_tester.response_body.len);
2603 client_stream_tester_clean_up(&stream_tester);
2604
2605 /* extra data should have caused channel shutdown */
2606 testing_channel_drain_queued_tasks(&tester.testing_channel);
2607 ASSERT_TRUE(testing_channel_is_shutdown_completed(&tester.testing_channel));
2608 ASSERT_TRUE(testing_channel_get_shutdown_error_code(&tester.testing_channel) != AWS_ERROR_SUCCESS);
2609
2610 ASSERT_SUCCESS(s_tester_clean_up(&tester));
2611 return AWS_OP_SUCCESS;
2612 }
2613
2614 struct slow_body_sender {
2615 struct aws_input_stream base;
2616 struct aws_stream_status status;
2617 struct aws_byte_cursor cursor;
2618 size_t delay_ticks; /* Don't send anything the first N ticks */
2619 size_t bytes_per_tick; /* Don't send more than N bytes per tick */
2620 };
2621
2622 static int s_slow_stream_read(struct aws_input_stream *stream, struct aws_byte_buf *dest) {
2623 struct slow_body_sender *sender = AWS_CONTAINER_OF(stream, struct slow_body_sender, base);
2624
2625 size_t dst_available = dest->capacity - dest->len;
2626 size_t writing = 0;
2627 if (sender->delay_ticks > 0) {
2628 sender->delay_ticks--;
2629 } else {
2630 writing = sender->cursor.len;
2631
2632 if (dst_available < writing) {
2633 writing = dst_available;
2634 }
2635
2636 if ((sender->bytes_per_tick < writing) && (sender->bytes_per_tick > 0)) {
2637 writing = sender->bytes_per_tick;
2638 }
2639 }
2640
2641 aws_byte_buf_write(dest, sender->cursor.ptr, writing);
2642 aws_byte_cursor_advance(&sender->cursor, writing);
2643
2644 if (sender->cursor.len == 0) {
2645 sender->status.is_end_of_stream = true;
2646 }
2647
2648 return AWS_OP_SUCCESS;
2649 }
2650 static int s_slow_stream_get_status(struct aws_input_stream *stream, struct aws_stream_status *status) {
2651 struct slow_body_sender *sender = AWS_CONTAINER_OF(stream, struct slow_body_sender, base);
2652 *status = sender->status;
2653 return AWS_OP_SUCCESS;
2654 }
2655 static int s_slow_stream_get_length(struct aws_input_stream *stream, int64_t *out_length) {
2656 struct slow_body_sender *sender = AWS_CONTAINER_OF(stream, struct slow_body_sender, base);
2657 *out_length = sender->cursor.len;
2658 return AWS_OP_SUCCESS;
2659 }
2660 static void s_slow_stream_destroy(struct aws_input_stream *stream) {
2661 (void)stream;
2662 }
2663
2664 static struct aws_input_stream_vtable s_slow_stream_vtable = {
2665 .seek = NULL,
2666 .read = s_slow_stream_read,
2667 .get_status = s_slow_stream_get_status,
2668 .get_length = s_slow_stream_get_length,
2669 };
2670
2671 static void s_slow_body_sender_init(struct slow_body_sender *body_sender) {
2672 /* set up request whose body won't send immediately */
2673 struct aws_input_stream empty_stream_base;
2674 AWS_ZERO_STRUCT(empty_stream_base);
2675 body_sender->base = empty_stream_base;
2676 body_sender->status.is_end_of_stream = false;
2677 body_sender->status.is_valid = true;
2678 struct aws_byte_cursor body = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("write more tests");
2679 body_sender->cursor = body;
2680 body_sender->delay_ticks = 5;
2681 body_sender->bytes_per_tick = 1;
2682
2683 body_sender->base.vtable = &s_slow_stream_vtable;
2684 aws_ref_count_init(
2685 &body_sender->base.ref_count, &body_sender, (aws_simple_completion_callback *)s_slow_stream_destroy);
2686 }
2687
2688 /* It should be fine to receive a response before the request has finished sending */
2689 H1_CLIENT_TEST_CASE(h1_client_response_arrives_before_request_done_sending_is_ok) {
2690 (void)ctx;
2691 struct tester tester;
2692 ASSERT_SUCCESS(s_tester_init(&tester, allocator));
2693
2694 /* set up request whose body won't send immediately */
2695 struct slow_body_sender body_sender;
2696 AWS_ZERO_STRUCT(body_sender);
2697 s_slow_body_sender_init(&body_sender);
2698 struct aws_input_stream *body_stream = &body_sender.base;
2699
2700 struct aws_http_header headers[] = {
2701 {
2702 .name = aws_byte_cursor_from_c_str("Content-Length"),
2703 .value = aws_byte_cursor_from_c_str("16"),
2704 },
2705 };
2706
2707 struct aws_http_message *request = aws_http_message_new_request(allocator);
2708 ASSERT_NOT_NULL(request);
2709 ASSERT_SUCCESS(aws_http_message_set_request_method(request, aws_byte_cursor_from_c_str("PUT")));
2710 ASSERT_SUCCESS(aws_http_message_set_request_path(request, aws_byte_cursor_from_c_str("/plan.txt")));
2711 ASSERT_SUCCESS(aws_http_message_add_header_array(request, headers, AWS_ARRAY_SIZE(headers)));
2712 aws_http_message_set_body_stream(request, body_stream);
2713
2714 struct client_stream_tester stream_tester;
2715 ASSERT_SUCCESS(s_stream_tester_init(&stream_tester, &tester, request));
2716
2717 /* send head of request */
2718 testing_channel_run_currently_queued_tasks(&tester.testing_channel);
2719
2720 /* Ensure the request can be destroyed after request is sent */
2721 aws_http_message_destroy(request);
2722 aws_input_stream_release(body_stream);
2723
2724 /* send response */
2725 ASSERT_SUCCESS(testing_channel_push_read_str(&tester.testing_channel, "HTTP/1.1 200 OK\r\n\r\n"));
2726
2727 /* tick loop until body finishes sending.*/
2728 while (body_sender.cursor.len > 0) {
2729 /* on_complete shouldn't fire until all outgoing data sent AND all incoming data received */
2730 ASSERT_FALSE(stream_tester.complete);
2731
2732 testing_channel_run_currently_queued_tasks(&tester.testing_channel);
2733 }
2734
2735 /* flush any further work so that stream completes */
2736 testing_channel_drain_queued_tasks(&tester.testing_channel);
2737
2738 /* check result */
2739 const char *expected = "PUT /plan.txt HTTP/1.1\r\n"
2740 "Content-Length: 16\r\n"
2741 "\r\n"
2742 "write more tests";
2743 ASSERT_SUCCESS(testing_channel_check_written_messages_str(&tester.testing_channel, allocator, expected));
2744
2745 ASSERT_TRUE(stream_tester.complete);
2746 ASSERT_INT_EQUALS(AWS_ERROR_SUCCESS, stream_tester.on_complete_error_code);
2747 ASSERT_INT_EQUALS(200, stream_tester.response_status);
2748 ASSERT_UINT_EQUALS(0, aws_http_headers_count(stream_tester.response_headers));
2749 ASSERT_UINT_EQUALS(0, stream_tester.response_body.len);
2750
2751 /* clean up */
2752 client_stream_tester_clean_up(&stream_tester);
2753 ASSERT_SUCCESS(s_tester_clean_up(&tester));
2754 return AWS_OP_SUCCESS;
2755 }
2756
2757 /* It should be fine to receive a response before the request has finished sending */
2758 H1_CLIENT_TEST_CASE(h1_client_response_arrives_before_request_chunks_done_sending_is_ok) {
2759 (void)ctx;
2760 struct tester tester;
2761 ASSERT_SUCCESS(s_tester_init(&tester, allocator));
2762
2763 /* set up request whose body won't send immediately */
2764 struct aws_input_stream empty_stream_base;
2765 AWS_ZERO_STRUCT(empty_stream_base);
2766 struct slow_body_sender body_sender = {
2767 .base = empty_stream_base,
2768 .status =
2769 {
2770 .is_end_of_stream = false,
2771 .is_valid = true,
2772 },
2773 .cursor = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("write more tests"),
2774 .delay_ticks = 5,
2775 .bytes_per_tick = 1,
2776 };
2777 body_sender.base.vtable = &s_slow_stream_vtable;
2778 aws_ref_count_init(
2779 &body_sender.base.ref_count, &body_sender, (aws_simple_completion_callback *)s_slow_stream_destroy);
2780
2781 struct aws_input_stream *body_stream = &body_sender.base;
2782
2783 struct aws_http_message *request = s_new_default_chunked_put_request(allocator);
2784 struct client_stream_tester stream_tester;
2785 ASSERT_SUCCESS(s_stream_tester_init(&stream_tester, &tester, request));
2786
2787 /* send head of request */
2788 testing_channel_run_currently_queued_tasks(&tester.testing_channel);
2789
2790 /* send response */
2791 ASSERT_SUCCESS(testing_channel_push_read_str(&tester.testing_channel, "HTTP/1.1 200 OK\r\n\r\n"));
2792
2793 testing_channel_run_currently_queued_tasks(&tester.testing_channel);
2794
2795 struct aws_http1_chunk_options options = s_default_chunk_options(body_stream, body_sender.cursor.len);
2796 options.on_complete = NULL; /* The stream_tester takes care of the stream deletion */
2797 ASSERT_SUCCESS(aws_http1_stream_write_chunk(stream_tester.stream, &options));
2798 ASSERT_SUCCESS(s_write_termination_chunk(allocator, stream_tester.stream));
2799
2800 /* Ensure the request can be destroyed after request is sent */
2801 aws_http_message_destroy(request);
2802 aws_input_stream_release(body_stream);
2803
2804 /* tick loop until body finishes sending.*/
2805 while (body_sender.cursor.len > 0) {
2806 /* on_complete shouldn't fire until all outgoing data sent AND all incoming data received */
2807 ASSERT_FALSE(stream_tester.complete);
2808 testing_channel_run_currently_queued_tasks(&tester.testing_channel);
2809 }
2810
2811 /* flush any further work so that stream completes */
2812 testing_channel_drain_queued_tasks(&tester.testing_channel);
2813
2814 /* check result */
2815 const char *expected = "PUT /plan.txt HTTP/1.1\r\n"
2816 "Transfer-Encoding: chunked\r\n"
2817 "\r\n"
2818 "10\r\n"
2819 "write more tests"
2820 "\r\n"
2821 "0\r\n"
2822 "\r\n";
2823
2824 ASSERT_SUCCESS(testing_channel_check_written_messages_str(&tester.testing_channel, allocator, expected));
2825
2826 ASSERT_TRUE(stream_tester.complete);
2827 ASSERT_INT_EQUALS(AWS_ERROR_SUCCESS, stream_tester.on_complete_error_code);
2828 ASSERT_INT_EQUALS(200, stream_tester.response_status);
2829 ASSERT_UINT_EQUALS(0, aws_http_headers_count(stream_tester.response_headers));
2830 ASSERT_UINT_EQUALS(0, stream_tester.response_body.len);
2831
2832 /* clean up */
2833 client_stream_tester_clean_up(&stream_tester);
2834 ASSERT_SUCCESS(s_tester_clean_up(&tester));
2835 return AWS_OP_SUCCESS;
2836 }
2837
2838 /* Response data arrives, but there was no outstanding request */
2839 H1_CLIENT_TEST_CASE(h1_client_response_without_request_shuts_down_connection) {
2840 (void)ctx;
2841 struct tester tester;
2842 ASSERT_SUCCESS(s_tester_init(&tester, allocator));
2843
2844 ASSERT_SUCCESS(testing_channel_push_read_str_ignore_errors(&tester.testing_channel, "HTTP/1.1 200 OK\r\n\r\n"));
2845 testing_channel_drain_queued_tasks(&tester.testing_channel);
2846
2847 ASSERT_TRUE(testing_channel_is_shutdown_completed(&tester.testing_channel));
2848 ASSERT_TRUE(testing_channel_get_shutdown_error_code(&tester.testing_channel) != AWS_ERROR_SUCCESS);
2849
2850 /* clean up */
2851 ASSERT_SUCCESS(s_tester_clean_up(&tester));
2852 return AWS_OP_SUCCESS;
2853 }
2854
2855 /* A response with the "Connection: close" header should result in the connection shutting down
2856 * after the stream completes. */
2857 H1_CLIENT_TEST_CASE(h1_client_response_close_header_ends_connection) {
2858 (void)ctx;
2859 struct tester tester;
2860 ASSERT_SUCCESS(s_tester_init(&tester, allocator));
2861
2862 /* send request */
2863 struct aws_http_message *request = s_new_default_get_request(allocator);
2864
2865 struct client_stream_tester stream_tester;
2866 ASSERT_SUCCESS(s_stream_tester_init(&stream_tester, &tester, request));
2867
2868 testing_channel_drain_queued_tasks(&tester.testing_channel);
2869
2870 /* Ensure the request can be destroyed after request is sent */
2871 aws_http_message_destroy(request);
2872
2873 /* send response */
2874 ASSERT_SUCCESS(testing_channel_push_read_str(
2875 &tester.testing_channel,
2876 "HTTP/1.1 200 OK\r\n"
2877 "Connection: close\r\n"
2878 "\r\n"));
2879
2880 testing_channel_drain_queued_tasks(&tester.testing_channel);
2881
2882 /* Response should come across successfully
2883 * but connection should be closing when the stream-complete callback fires */
2884 ASSERT_TRUE(stream_tester.complete);
2885 ASSERT_INT_EQUALS(AWS_ERROR_SUCCESS, stream_tester.on_complete_error_code);
2886 ASSERT_INT_EQUALS(200, stream_tester.response_status);
2887 ASSERT_FALSE(stream_tester.on_complete_connection_is_open);
2888
2889 /* Connection should have shut down cleanly after delivering response */
2890 ASSERT_TRUE(testing_channel_is_shutdown_completed(&tester.testing_channel));
2891 ASSERT_INT_EQUALS(AWS_ERROR_SUCCESS, testing_channel_get_shutdown_error_code(&tester.testing_channel));
2892
2893 /* clean up */
2894 client_stream_tester_clean_up(&stream_tester);
2895 ASSERT_SUCCESS(s_tester_clean_up(&tester));
2896 return AWS_OP_SUCCESS;
2897 }
2898
2899 /* A request with the "Connection: close" header should result in the connection shutting down
2900 * after the stream completes. */
2901 H1_CLIENT_TEST_CASE(h1_client_request_close_header_ends_connection) {
2902 (void)ctx;
2903 struct tester tester;
2904 ASSERT_SUCCESS(s_tester_init(&tester, allocator));
2905
2906 /* Request has "Connection: close" header */
2907 struct aws_http_message *request = s_new_default_get_request(allocator);
2908 struct aws_http_header headers[] = {
2909 {
2910 .name = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("Host"),
2911 .value = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("example.com"),
2912 },
2913 {
2914 .name = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("Connection"),
2915 .value = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("close"),
2916 },
2917 };
2918 ASSERT_SUCCESS(aws_http_message_add_header_array(request, headers, AWS_ARRAY_SIZE(headers)));
2919
2920 /* Set up response tester, which sends the request as a side-effect */
2921 struct client_stream_tester stream_tester;
2922 ASSERT_SUCCESS(s_stream_tester_init(&stream_tester, &tester, request));
2923
2924 testing_channel_drain_queued_tasks(&tester.testing_channel);
2925
2926 /* Check that request was sent */
2927 const char *expected = "GET / HTTP/1.1\r\n"
2928 "Host: example.com\r\n"
2929 "Connection: close\r\n"
2930 "\r\n";
2931 ASSERT_SUCCESS(testing_channel_check_written_message_str(&tester.testing_channel, expected));
2932
2933 /* Connection shouldn't be "open" at this point, but it also shouldn't shut down until response is received */
2934 ASSERT_FALSE(aws_http_connection_is_open(tester.connection));
2935 ASSERT_FALSE(testing_channel_is_shutdown_completed(&tester.testing_channel));
2936
2937 /* Send response */
2938 ASSERT_SUCCESS(testing_channel_push_read_str(
2939 &tester.testing_channel,
2940 "HTTP/1.1 200 OK\r\n"
2941 "\r\n"));
2942
2943 testing_channel_drain_queued_tasks(&tester.testing_channel);
2944
2945 /* Response should come across successfully */
2946 ASSERT_TRUE(stream_tester.complete);
2947 ASSERT_INT_EQUALS(AWS_ERROR_SUCCESS, stream_tester.on_complete_error_code);
2948 ASSERT_INT_EQUALS(200, stream_tester.response_status);
2949 ASSERT_FALSE(stream_tester.on_complete_connection_is_open);
2950
2951 /* Connection should have shut down cleanly after delivering response */
2952 ASSERT_TRUE(testing_channel_is_shutdown_completed(&tester.testing_channel));
2953 ASSERT_INT_EQUALS(AWS_ERROR_SUCCESS, testing_channel_get_shutdown_error_code(&tester.testing_channel));
2954
2955 /* clean up */
2956 aws_http_message_destroy(request);
2957 client_stream_tester_clean_up(&stream_tester);
2958 ASSERT_SUCCESS(s_tester_clean_up(&tester));
2959 return AWS_OP_SUCCESS;
2960 }
2961
2962 /* While pipelining 3 requests, and 2nd response has a "Connection: close" header.
2963 * 2 requests should complete successfully and the connection should close. */
2964 H1_CLIENT_TEST_CASE(h1_client_response_close_header_with_pipelining) {
2965 (void)ctx;
2966 struct tester tester;
2967 ASSERT_SUCCESS(s_tester_init(&tester, allocator));
2968
2969 /* Send 3 requests before receiving any responses */
2970 enum { NUM_STREAMS = 3 };
2971 struct aws_http_message *requests[NUM_STREAMS];
2972 struct client_stream_tester stream_testers[NUM_STREAMS];
2973 for (size_t i = 0; i < NUM_STREAMS; ++i) {
2974 requests[i] = s_new_default_get_request(allocator);
2975 ASSERT_SUCCESS(s_stream_tester_init(&stream_testers[i], &tester, requests[i]));
2976 };
2977
2978 testing_channel_drain_queued_tasks(&tester.testing_channel);
2979
2980 /* Send "Connection: close" header in 2nd response.
2981 * Do not send 3rd response. */
2982 ASSERT_SUCCESS(testing_channel_push_read_str(
2983 &tester.testing_channel,
2984 /* Response 1 */
2985 "HTTP/1.1 200 OK\r\n"
2986 "\r\n"
2987 /* Response 2 */
2988 "HTTP/1.1 200 OK\r\n"
2989 "Connection: close\r\n"
2990 "\r\n"));
2991
2992 testing_channel_drain_queued_tasks(&tester.testing_channel);
2993
2994 { /* First stream should be successful, and connection should be open when it completes */
2995 const struct client_stream_tester *first = &stream_testers[0];
2996 ASSERT_TRUE(first->complete);
2997 ASSERT_INT_EQUALS(AWS_ERROR_SUCCESS, first->on_complete_error_code);
2998 ASSERT_INT_EQUALS(200, first->response_status);
2999 ASSERT_TRUE(first->on_complete_connection_is_open);
3000 }
3001
3002 { /* Second stream should be successful, BUT connection should NOT be open when it completes */
3003 const struct client_stream_tester *second = &stream_testers[1];
3004 ASSERT_TRUE(second->complete);
3005 ASSERT_INT_EQUALS(AWS_ERROR_SUCCESS, second->on_complete_error_code);
3006 ASSERT_INT_EQUALS(200, second->response_status);
3007 ASSERT_FALSE(second->on_complete_connection_is_open);
3008 }
3009
3010 { /* Third stream should complete with error, since connection should close after 2nd stream completes. */
3011 const struct client_stream_tester *third = &stream_testers[2];
3012 ASSERT_TRUE(third->complete);
3013 ASSERT_INT_EQUALS(AWS_ERROR_HTTP_CONNECTION_CLOSED, third->on_complete_error_code);
3014 ASSERT_FALSE(third->on_complete_connection_is_open);
3015 }
3016
3017 /* Connection should have shut down after delivering response.
3018 * Not going to check error_code because it's pretty ambiguous what it ought to be in this circumstance */
3019 ASSERT_TRUE(testing_channel_is_shutdown_completed(&tester.testing_channel));
3020
3021 /* clean up */
3022 for (size_t i = 0; i < NUM_STREAMS; ++i) {
3023 aws_http_message_destroy(requests[i]);
3024 client_stream_tester_clean_up(&stream_testers[i]);
3025 }
3026
3027 ASSERT_SUCCESS(s_tester_clean_up(&tester));
3028 return AWS_OP_SUCCESS;
3029 }
3030
3031 /* While pipelining 3 requests, and 2nd request has a "Connection: close" header.
3032 * 2 requests should complete successfully and the connection should close. */
3033 H1_CLIENT_TEST_CASE(h1_client_request_close_header_with_pipelining) {
3034 (void)ctx;
3035 struct tester tester;
3036 ASSERT_SUCCESS(s_tester_init(&tester, allocator));
3037
3038 /* Queue up 3 requests, where the middle request has a "Connection: close" header */
3039 enum { NUM_STREAMS = 3 };
3040 struct aws_http_message *requests[NUM_STREAMS];
3041 struct client_stream_tester stream_testers[NUM_STREAMS];
3042 for (size_t i = 0; i < NUM_STREAMS; ++i) {
3043 requests[i] = s_new_default_get_request(allocator);
3044
3045 if (i == 1) {
3046 struct aws_http_header close_header = {
3047 .name = aws_byte_cursor_from_c_str("Connection"),
3048 .value = aws_byte_cursor_from_c_str("close"),
3049 };
3050 ASSERT_SUCCESS(aws_http_message_add_header(requests[i], close_header));
3051 }
3052
3053 /* Response tester sends requests as a side-effect */
3054 ASSERT_SUCCESS(s_stream_tester_init(&stream_testers[i], &tester, requests[i]));
3055 };
3056
3057 testing_channel_drain_queued_tasks(&tester.testing_channel);
3058
3059 /* Check that ONLY first 2 requests were sent */
3060 const char *expected = "GET / HTTP/1.1\r\n"
3061 "\r\n"
3062 "GET / HTTP/1.1\r\n"
3063 "Connection: close\r\n"
3064 "\r\n";
3065 ASSERT_SUCCESS(testing_channel_check_written_messages_str(&tester.testing_channel, allocator, expected));
3066
3067 /* Send 2 responses. */
3068 ASSERT_SUCCESS(testing_channel_push_read_str(
3069 &tester.testing_channel,
3070 /* Response 1 */
3071 "HTTP/1.1 200 OK\r\n"
3072 "\r\n"
3073 /* Response 2 */
3074 "HTTP/1.1 200 OK\r\n"
3075 "\r\n"));
3076
3077 testing_channel_drain_queued_tasks(&tester.testing_channel);
3078
3079 { /* First stream should be successful */
3080 const struct client_stream_tester *first = &stream_testers[0];
3081 ASSERT_TRUE(first->complete);
3082 ASSERT_INT_EQUALS(AWS_ERROR_SUCCESS, first->on_complete_error_code);
3083 ASSERT_INT_EQUALS(200, first->response_status);
3084 }
3085
3086 { /* Second stream should be successful */
3087 const struct client_stream_tester *second = &stream_testers[1];
3088 ASSERT_TRUE(second->complete);
3089 ASSERT_INT_EQUALS(AWS_ERROR_SUCCESS, second->on_complete_error_code);
3090 ASSERT_INT_EQUALS(200, second->response_status);
3091 }
3092
3093 { /* Third stream should complete with error, since connection should close after 2nd stream completes. */
3094 const struct client_stream_tester *third = &stream_testers[2];
3095 ASSERT_TRUE(third->complete);
3096 ASSERT_INT_EQUALS(AWS_ERROR_HTTP_CONNECTION_CLOSED, third->on_complete_error_code);
3097 }
3098
3099 /* Connection should have shut down after delivering second response.
3100 * Not going to check error_code because it's pretty ambiguous what it ought to be in this circumstance */
3101 ASSERT_TRUE(testing_channel_is_shutdown_completed(&tester.testing_channel));
3102
3103 /* clean up */
3104 for (size_t i = 0; i < NUM_STREAMS; ++i) {
3105 aws_http_message_destroy(requests[i]);
3106 client_stream_tester_clean_up(&stream_testers[i]);
3107 }
3108
3109 ASSERT_SUCCESS(s_tester_clean_up(&tester));
3110 return AWS_OP_SUCCESS;
3111 }
3112
3113 /* While pipelining 3 requests, and 2nd request has a "Connection: close" header.
3114 * 2 requests should complete successfully and the connection should close. */
3115 H1_CLIENT_TEST_CASE(h1_client_request_close_header_with_chunked_encoding_and_pipelining) {
3116 (void)ctx;
3117 struct tester tester;
3118 ASSERT_SUCCESS(s_tester_init(&tester, allocator));
3119
3120 /* Queue up 3 requests, where the middle request has a "Connection: close" header */
3121 enum { NUM_STREAMS = 3 };
3122 struct aws_http_message *requests[NUM_STREAMS];
3123 struct client_stream_tester stream_testers[NUM_STREAMS];
3124 for (size_t i = 0; i < NUM_STREAMS; ++i) {
3125 requests[i] = s_new_default_chunked_put_request(allocator);
3126
3127 if (i == 1) {
3128 struct aws_http_header close_header = {
3129 .name = aws_byte_cursor_from_c_str("Connection"),
3130 .value = aws_byte_cursor_from_c_str("close"),
3131 };
3132 ASSERT_SUCCESS(aws_http_message_add_header(requests[i], close_header));
3133 }
3134
3135 /* Response tester sends requests as a side-effect */
3136 ASSERT_SUCCESS(s_stream_tester_init(&stream_testers[i], &tester, requests[i]));
3137 };
3138
3139 testing_channel_drain_queued_tasks(&tester.testing_channel);
3140
3141 /* Write to all the streams */
3142 for (size_t i = 0; i < NUM_STREAMS; ++i) {
3143 static const struct aws_byte_cursor body = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("write more tests");
3144 struct aws_input_stream *body_stream = aws_input_stream_new_from_cursor(allocator, &body);
3145 struct aws_http1_chunk_options options = s_default_chunk_options(body_stream, body.len);
3146 ASSERT_SUCCESS(aws_http1_stream_write_chunk(stream_testers[i].stream, &options));
3147 ASSERT_SUCCESS(s_write_termination_chunk(allocator, stream_testers[i].stream));
3148 }
3149
3150 testing_channel_drain_queued_tasks(&tester.testing_channel);
3151
3152 /* Check that ONLY first 2 requests were sent */
3153 const char *expected = "PUT /plan.txt HTTP/1.1\r\n"
3154 "Transfer-Encoding: chunked\r\n"
3155 "\r\n"
3156 "10\r\n"
3157 "write more tests"
3158 "\r\n"
3159 "0\r\n"
3160 "\r\n"
3161 "PUT /plan.txt HTTP/1.1\r\n"
3162 "Transfer-Encoding: chunked\r\n"
3163 "Connection: close\r\n"
3164 "\r\n"
3165 "10\r\n"
3166 "write more tests"
3167 "\r\n"
3168 "0\r\n"
3169 "\r\n";
3170
3171 ASSERT_SUCCESS(testing_channel_check_written_messages_str(&tester.testing_channel, allocator, expected));
3172
3173 /* Send 2 responses. */
3174 ASSERT_SUCCESS(testing_channel_push_read_str(
3175 &tester.testing_channel,
3176 /* Response 1 */
3177 "HTTP/1.1 200 OK\r\n"
3178 "\r\n"
3179 /* Response 2 */
3180 "HTTP/1.1 200 OK\r\n"
3181 "\r\n"));
3182
3183 testing_channel_drain_queued_tasks(&tester.testing_channel);
3184
3185 { /* First stream should be successful */
3186 const struct client_stream_tester *first = &stream_testers[0];
3187 ASSERT_TRUE(first->complete);
3188 ASSERT_INT_EQUALS(AWS_ERROR_SUCCESS, first->on_complete_error_code);
3189 ASSERT_INT_EQUALS(200, first->response_status);
3190 }
3191
3192 { /* Second stream should be successful */
3193 const struct client_stream_tester *second = &stream_testers[1];
3194 ASSERT_TRUE(second->complete);
3195 ASSERT_INT_EQUALS(AWS_ERROR_SUCCESS, second->on_complete_error_code);
3196 ASSERT_INT_EQUALS(200, second->response_status);
3197 }
3198
3199 { /* Third stream should complete with error, since connection should close after 2nd stream completes. */
3200 const struct client_stream_tester *third = &stream_testers[2];
3201 ASSERT_TRUE(third->complete);
3202 ASSERT_INT_EQUALS(AWS_ERROR_HTTP_CONNECTION_CLOSED, third->on_complete_error_code);
3203 }
3204
3205 /* Connection should have shut down after delivering second response.
3206 * Not going to check error_code because it's pretty ambiguous what it ought to be in this circumstance */
3207 ASSERT_TRUE(testing_channel_is_shutdown_completed(&tester.testing_channel));
3208
3209 /* clean up */
3210 for (size_t i = 0; i < NUM_STREAMS; ++i) {
3211 aws_http_message_destroy(requests[i]);
3212 client_stream_tester_clean_up(&stream_testers[i]);
3213 }
3214
3215 ASSERT_SUCCESS(s_tester_clean_up(&tester));
3216 return AWS_OP_SUCCESS;
3217 }
3218
3219 /* Test that the stream window rules are respected. These rules are:
3220 * - Each new stream's window starts at initial_stream_window_size.
3221 * - Only body data counts against the stream's window.
3222 * - The stream will not receive more body data than its window allows.
3223 * - Any future streams on the same connection also start with initial_stream_window_size,
3224 * they should not be affected if a previous stream had a very small or very large window when it ended.
3225 */
3226 H1_CLIENT_TEST_CASE(h1_client_respects_stream_window) {
3227 (void)ctx;
3228
3229 /* This test only checks that the stream window is respected.
3230 * We're not testing the connection window, so just use a giant buffer */
3231 struct tester_options tester_opts = {
3232 .manual_window_management = true,
3233 .initial_stream_window_size = 5,
3234 .read_buffer_capacity = SIZE_MAX,
3235 };
3236 struct tester tester;
3237 ASSERT_SUCCESS(s_tester_init_ex(&tester, allocator, &tester_opts));
3238
3239 /**
3240 * Request/Response 1
3241 */
3242
3243 struct aws_http_message *request = s_new_default_get_request(allocator);
3244
3245 struct client_stream_tester stream_tester1;
3246 ASSERT_SUCCESS(s_stream_tester_init(&stream_tester1, &tester, request));
3247 testing_channel_drain_queued_tasks(&tester.testing_channel);
3248
3249 /* send response whose body is 2X the initial_stream_window_size */
3250 const char *response_str = "HTTP/1.1 200 OK\r\n"
3251 "Content-Length: 10\r\n"
3252 "\r\n"
3253 "0123456789";
3254 ASSERT_SUCCESS(testing_channel_push_read_str(&tester.testing_channel, response_str));
3255 testing_channel_drain_queued_tasks(&tester.testing_channel);
3256
3257 /* stream window should reach 0 before entire body has been received */
3258 ASSERT_BIN_ARRAYS_EQUALS("01234", 5, stream_tester1.response_body.buffer, stream_tester1.response_body.len);
3259 struct aws_h1_window_stats window_stats = aws_h1_connection_window_stats(tester.connection);
3260 ASSERT_TRUE(window_stats.has_incoming_stream);
3261 ASSERT_UINT_EQUALS(0, window_stats.stream_window);
3262
3263 /* open window just enough to get the rest of the body */
3264 aws_http_stream_update_window(stream_tester1.stream, 5);
3265 testing_channel_drain_queued_tasks(&tester.testing_channel);
3266
3267 ASSERT_BIN_ARRAYS_EQUALS("0123456789", 10, stream_tester1.response_body.buffer, stream_tester1.response_body.len);
3268 ASSERT_TRUE(stream_tester1.complete);
3269 ASSERT_SUCCESS(stream_tester1.on_complete_error_code);
3270 client_stream_tester_clean_up(&stream_tester1);
3271
3272 /**
3273 * Stream 2.
3274 * Send same request/response as before.
3275 * Everything should work fine, even though the previous stream left off with 0 window.
3276 */
3277 struct client_stream_tester stream_tester2;
3278 ASSERT_SUCCESS(s_stream_tester_init(&stream_tester2, &tester, request));
3279 testing_channel_drain_queued_tasks(&tester.testing_channel);
3280
3281 ASSERT_SUCCESS(testing_channel_push_read_str(&tester.testing_channel, response_str));
3282 testing_channel_drain_queued_tasks(&tester.testing_channel);
3283
3284 ASSERT_BIN_ARRAYS_EQUALS("01234", 5, stream_tester2.response_body.buffer, stream_tester2.response_body.len);
3285 window_stats = aws_h1_connection_window_stats(tester.connection);
3286 ASSERT_TRUE(window_stats.has_incoming_stream);
3287 ASSERT_UINT_EQUALS(0, window_stats.stream_window);
3288
3289 /**
3290 * Stream 3.
3291 * Stress pipelining by sending the 3rd request and response before stream 2
3292 * has opened its window enough to complete.
3293 */
3294 struct client_stream_tester stream_tester3;
3295 ASSERT_SUCCESS(s_stream_tester_init(&stream_tester3, &tester, request));
3296 testing_channel_drain_queued_tasks(&tester.testing_channel);
3297
3298 ASSERT_SUCCESS(testing_channel_push_read_str(&tester.testing_channel, response_str));
3299 testing_channel_drain_queued_tasks(&tester.testing_channel);
3300
3301 /* now open stream 2's window TO THE MAX to complete it. */
3302 aws_http_stream_update_window(stream_tester2.stream, SIZE_MAX);
3303 testing_channel_drain_queued_tasks(&tester.testing_channel);
3304
3305 ASSERT_BIN_ARRAYS_EQUALS("0123456789", 10, stream_tester2.response_body.buffer, stream_tester2.response_body.len);
3306 ASSERT_TRUE(stream_tester2.complete);
3307 ASSERT_SUCCESS(stream_tester2.on_complete_error_code);
3308 client_stream_tester_clean_up(&stream_tester2);
3309
3310 /* even though stream2 completed with a WIDE OPEN window, stream3's window should be at the initial size */
3311 ASSERT_BIN_ARRAYS_EQUALS("01234", 5, stream_tester3.response_body.buffer, stream_tester3.response_body.len);
3312 window_stats = aws_h1_connection_window_stats(tester.connection);
3313 ASSERT_TRUE(window_stats.has_incoming_stream);
3314 ASSERT_UINT_EQUALS(0, window_stats.stream_window);
3315
3316 /* finish up stream 3 */
3317 aws_http_stream_update_window(stream_tester3.stream, 100);
3318 testing_channel_drain_queued_tasks(&tester.testing_channel);
3319
3320 ASSERT_BIN_ARRAYS_EQUALS("0123456789", 10, stream_tester3.response_body.buffer, stream_tester3.response_body.len);
3321 ASSERT_TRUE(stream_tester3.complete);
3322 ASSERT_SUCCESS(stream_tester3.on_complete_error_code);
3323 client_stream_tester_clean_up(&stream_tester3);
3324
3325 /* clean up */
3326 aws_http_message_destroy(request);
3327 ASSERT_SUCCESS(s_tester_clean_up(&tester));
3328 return AWS_OP_SUCCESS;
3329 }
3330
3331 /* This tests the specific way that HTTP/1 manages its connection window. */
3332 H1_CLIENT_TEST_CASE(h1_client_connection_window_with_buffer) {
3333 (void)ctx;
3334
3335 struct tester_options tester_opts = {
3336 .manual_window_management = true,
3337 .initial_stream_window_size = 0,
3338 .read_buffer_capacity = 100,
3339 };
3340 struct tester tester;
3341 ASSERT_SUCCESS(s_tester_init_ex(&tester, allocator, &tester_opts));
3342
3343 struct aws_http_message *request = s_new_default_get_request(allocator);
3344 struct client_stream_tester stream_tester;
3345 ASSERT_SUCCESS(s_stream_tester_init(&stream_tester, &tester, request));
3346 testing_channel_drain_queued_tasks(&tester.testing_channel);
3347
3348 /* confirm starting stats before any data received.
3349 * connection window should match buffer capacity. */
3350 struct aws_h1_window_stats window_stats = aws_h1_connection_window_stats(tester.connection);
3351 ASSERT_UINT_EQUALS(100, window_stats.buffer_capacity);
3352 ASSERT_UINT_EQUALS(0, window_stats.buffer_pending_bytes);
3353 ASSERT_UINT_EQUALS(100, window_stats.connection_window);
3354 ASSERT_UINT_EQUALS(0, window_stats.recent_window_increments);
3355
3356 if (window_stats.has_incoming_stream) {
3357 /* It's an implementation detail whether the incoming stream ptr is set at this point,
3358 * but if it is, it should use initial-window-size*/
3359 ASSERT_UINT_EQUALS(0, window_stats.stream_window);
3360 }
3361
3362 /* send 49 byte response 1 byte at a time, so we can see the message queue in action */
3363 const char *response_str = "HTTP/1.1 200 OK\r\n"
3364 "Content-Length: 10\r\n"
3365 "\r\n"
3366 "0123456789";
3367 struct aws_byte_cursor response_cursor = aws_byte_cursor_from_c_str(response_str);
3368 while (response_cursor.len > 0) {
3369 struct aws_byte_cursor one_byte = aws_byte_cursor_advance(&response_cursor, 1);
3370 ASSERT_SUCCESS(testing_channel_push_read_data(&tester.testing_channel, one_byte));
3371 }
3372 testing_channel_drain_queued_tasks(&tester.testing_channel);
3373
3374 /* The stream should not have received any body, since its initial-window-size is zero.
3375 * At time of writing, the connection would not process headers if stream window was zero,
3376 * but that might change in the future, so not testing window stats here. */
3377 ASSERT_UINT_EQUALS(0, stream_tester.response_body.len);
3378
3379 /* Open the stream window by 1 byte and check stats.
3380 * 40 bytes should be processed: 39 bytes of headers and metadata + 1 byte of body data */
3381 aws_http_stream_update_window(stream_tester.stream, 1);
3382 testing_channel_drain_queued_tasks(&tester.testing_channel);
3383
3384 ASSERT_UINT_EQUALS(1, stream_tester.response_body.len);
3385
3386 window_stats = aws_h1_connection_window_stats(tester.connection);
3387 ASSERT_UINT_EQUALS(
3388 40, window_stats.recent_window_increments); /* window incremented to account for processed data */
3389 ASSERT_UINT_EQUALS(100, window_stats.buffer_capacity);
3390 ASSERT_UINT_EQUALS(9, window_stats.buffer_pending_bytes);
3391 ASSERT_UINT_EQUALS(91, window_stats.connection_window);
3392 ASSERT_TRUE(window_stats.has_incoming_stream);
3393 ASSERT_UINT_EQUALS(0, window_stats.stream_window);
3394
3395 /* Open stream window enough to finish */
3396 aws_http_stream_update_window(stream_tester.stream, 9);
3397 testing_channel_drain_queued_tasks(&tester.testing_channel);
3398
3399 ASSERT_UINT_EQUALS(10, stream_tester.response_body.len);
3400 ASSERT_TRUE(stream_tester.complete);
3401 ASSERT_SUCCESS(stream_tester.on_complete_error_code);
3402
3403 window_stats = aws_h1_connection_window_stats(tester.connection);
3404 ASSERT_UINT_EQUALS(9, window_stats.recent_window_increments); /* window incremented to account for processed data */
3405 ASSERT_UINT_EQUALS(100, window_stats.buffer_capacity);
3406 ASSERT_UINT_EQUALS(0, window_stats.buffer_pending_bytes);
3407 ASSERT_UINT_EQUALS(100, window_stats.connection_window);
3408 ASSERT_FALSE(window_stats.has_incoming_stream);
3409
3410 /* clean up */
3411 client_stream_tester_clean_up(&stream_tester);
3412 aws_http_message_release(request);
3413 ASSERT_SUCCESS(s_tester_clean_up(&tester));
3414 return AWS_OP_SUCCESS;
3415 }
3416
3417 /* Test a connection with read_buffer_capacity < initial_window_size */
3418 H1_CLIENT_TEST_CASE(h1_client_connection_window_with_small_buffer) {
3419 (void)ctx;
3420
3421 struct tester_options tester_opts = {
3422 .manual_window_management = true,
3423 .initial_stream_window_size = 10,
3424 .read_buffer_capacity = 5,
3425 };
3426 struct tester tester;
3427 ASSERT_SUCCESS(s_tester_init_ex(&tester, allocator, &tester_opts));
3428
3429 struct aws_http_message *request = s_new_default_get_request(allocator);
3430 struct client_stream_tester stream_tester;
3431 ASSERT_SUCCESS(s_stream_tester_init(&stream_tester, &tester, request));
3432 testing_channel_drain_queued_tasks(&tester.testing_channel);
3433
3434 /* can't send response all at once because channel-window is too small. */
3435 const char *response_head_str = "HTTP/1.1 200 OK\r\n"
3436 "Content-Length: 20\r\n"
3437 "\r\n";
3438 const char *response_body_str = "0123456789"
3439 "ABCDEFGHIJ";
3440
3441 /* send response head in little increments, it should flow through to the stream no problem */
3442 struct aws_byte_cursor response_cursor = aws_byte_cursor_from_c_str(response_head_str);
3443 while (response_cursor.len > 0) {
3444 struct aws_byte_cursor one_byte = aws_byte_cursor_advance(&response_cursor, 1);
3445 ASSERT_SUCCESS(testing_channel_push_read_data(&tester.testing_channel, one_byte));
3446 testing_channel_drain_queued_tasks(&tester.testing_channel);
3447 }
3448
3449 ASSERT_UINT_EQUALS(0, stream_tester.response_body.len);
3450
3451 /* send enough body data that stream's window is reduced to zero. */
3452 response_cursor = aws_byte_cursor_from_c_str(response_body_str);
3453 for (int i = 0; i < 10; ++i) {
3454 struct aws_byte_cursor one_byte = aws_byte_cursor_advance(&response_cursor, 1);
3455 ASSERT_SUCCESS(testing_channel_push_read_data(&tester.testing_channel, one_byte));
3456 testing_channel_drain_queued_tasks(&tester.testing_channel);
3457 }
3458
3459 ASSERT_UINT_EQUALS(10, stream_tester.response_body.len);
3460
3461 struct aws_h1_window_stats window_stats = aws_h1_connection_window_stats(tester.connection);
3462 ASSERT_UINT_EQUALS(0, window_stats.stream_window);
3463 ASSERT_UINT_EQUALS(0, window_stats.buffer_pending_bytes);
3464 ASSERT_UINT_EQUALS(5, window_stats.buffer_capacity);
3465
3466 /* now that stream's window is 0, further data should fill the connection's read-buffer */
3467 for (int i = 0; i < 5; ++i) {
3468 struct aws_byte_cursor one_byte = aws_byte_cursor_advance(&response_cursor, 1);
3469 ASSERT_SUCCESS(testing_channel_push_read_data(&tester.testing_channel, one_byte));
3470 }
3471 testing_channel_drain_queued_tasks(&tester.testing_channel);
3472
3473 window_stats = aws_h1_connection_window_stats(tester.connection);
3474 ASSERT_UINT_EQUALS(5, window_stats.buffer_pending_bytes);
3475
3476 /* open the stream's window enough to finish the response, the buffered bytes should be consumed */
3477 aws_http_stream_update_window(stream_tester.stream, SIZE_MAX);
3478 testing_channel_drain_queued_tasks(&tester.testing_channel);
3479
3480 ASSERT_UINT_EQUALS(15, stream_tester.response_body.len);
3481
3482 window_stats = aws_h1_connection_window_stats(tester.connection);
3483 ASSERT_UINT_EQUALS(0, window_stats.buffer_pending_bytes);
3484
3485 /* send the remainder of the response */
3486 while (response_cursor.len > 0) {
3487 struct aws_byte_cursor one_byte = aws_byte_cursor_advance(&response_cursor, 1);
3488 ASSERT_SUCCESS(testing_channel_push_read_data(&tester.testing_channel, one_byte));
3489 testing_channel_drain_queued_tasks(&tester.testing_channel);
3490 }
3491 ASSERT_UINT_EQUALS(20, stream_tester.response_body.len);
3492 ASSERT_TRUE(stream_tester.complete);
3493 ASSERT_UINT_EQUALS(0, stream_tester.on_complete_error_code);
3494
3495 /* clean up */
3496 client_stream_tester_clean_up(&stream_tester);
3497 aws_http_message_release(request);
3498 ASSERT_SUCCESS(s_tester_clean_up(&tester));
3499 return AWS_OP_SUCCESS;
3500 }
3501
3502 static void s_on_complete(struct aws_http_stream *stream, int error_code, void *user_data) {
3503 (void)stream;
3504 int *completion_error_code = user_data;
3505 *completion_error_code = error_code;
3506 }
3507
3508 static int s_test_content_length_mismatch_is_error(
3509 struct aws_allocator *allocator,
3510 const char *body,
3511 const char *wrong_length) {
3512
3513 struct tester tester;
3514 ASSERT_SUCCESS(s_tester_init(&tester, allocator));
3515
3516 /* send request whose Content-Length does not match body length */
3517 const struct aws_byte_cursor body_cur = aws_byte_cursor_from_c_str(body);
3518 struct aws_input_stream *body_stream = aws_input_stream_new_from_cursor(allocator, &body_cur);
3519
3520 struct aws_http_header headers[] = {
3521 {
3522 .name = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("Content-Length"),
3523 .value = aws_byte_cursor_from_c_str(wrong_length),
3524 },
3525 };
3526
3527 struct aws_http_message *request = aws_http_message_new_request(allocator);
3528 ASSERT_NOT_NULL(request);
3529 ASSERT_SUCCESS(aws_http_message_set_request_method(request, aws_byte_cursor_from_c_str("PUT")));
3530 ASSERT_SUCCESS(aws_http_message_set_request_path(request, aws_byte_cursor_from_c_str("/plan.txt")));
3531 aws_http_message_add_header_array(request, headers, AWS_ARRAY_SIZE(headers));
3532 aws_http_message_set_body_stream(request, body_stream);
3533
3534 int completion_error_code = 0;
3535
3536 struct aws_http_make_request_options opt = {
3537 .self_size = sizeof(opt),
3538 .request = request,
3539 .on_complete = s_on_complete,
3540 .user_data = &completion_error_code,
3541 };
3542 struct aws_http_stream *stream = aws_http_connection_make_request(tester.connection, &opt);
3543 ASSERT_NOT_NULL(stream);
3544 ASSERT_SUCCESS(aws_http_stream_activate(stream));
3545 testing_channel_drain_queued_tasks(&tester.testing_channel);
3546
3547 /* check result */
3548 ASSERT_INT_EQUALS(AWS_ERROR_HTTP_OUTGOING_STREAM_LENGTH_INCORRECT, completion_error_code);
3549
3550 /* clean up */
3551 aws_input_stream_release(body_stream);
3552 aws_http_message_destroy(request);
3553 aws_http_stream_release(stream);
3554
3555 ASSERT_SUCCESS(s_tester_clean_up(&tester));
3556 return AWS_OP_SUCCESS;
3557 }
3558
3559 H1_CLIENT_TEST_CASE(h1_client_request_content_length_too_small_is_error) {
3560 (void)ctx;
3561 return s_test_content_length_mismatch_is_error(allocator, "I am very long", "1");
3562 }
3563
3564 H1_CLIENT_TEST_CASE(h1_client_request_content_length_too_large_is_error) {
3565 (void)ctx;
3566 return s_test_content_length_mismatch_is_error(allocator, "I am very short", "999");
3567 }
3568
3569 static int s_test_chunk_length_mismatch_is_error(
3570 struct aws_allocator *allocator,
3571 const char *body,
3572 size_t wrong_length) {
3573
3574 struct tester tester;
3575 ASSERT_SUCCESS(s_tester_init(&tester, allocator));
3576
3577 const struct aws_byte_cursor body_cur = aws_byte_cursor_from_c_str(body);
3578 struct aws_input_stream *body_stream = aws_input_stream_new_from_cursor(allocator, &body_cur);
3579
3580 /* send request */
3581 struct aws_http_message *request = s_new_default_chunked_put_request(allocator);
3582
3583 int completion_error_code = 0;
3584 struct aws_http_make_request_options opt = {
3585 .self_size = sizeof(opt),
3586 .request = request,
3587 .on_complete = s_on_complete,
3588 .user_data = &completion_error_code,
3589 };
3590
3591 struct aws_http_stream *stream = aws_http_connection_make_request(tester.connection, &opt);
3592 ASSERT_NOT_NULL(stream);
3593 ASSERT_SUCCESS(aws_http_stream_activate(stream));
3594
3595 /* Initialize with an off by one body length */
3596 struct aws_http1_chunk_options options = s_default_chunk_options(body_stream, wrong_length);
3597 ASSERT_SUCCESS(aws_http1_stream_write_chunk(stream, &options));
3598 ASSERT_SUCCESS(s_write_termination_chunk(allocator, stream));
3599
3600 testing_channel_drain_queued_tasks(&tester.testing_channel);
3601
3602 /* check result */
3603 ASSERT_INT_EQUALS(AWS_ERROR_HTTP_OUTGOING_STREAM_LENGTH_INCORRECT, completion_error_code);
3604
3605 /* clean up */
3606 aws_http_message_destroy(request);
3607 aws_http_stream_release(stream);
3608
3609 ASSERT_SUCCESS(s_tester_clean_up(&tester));
3610 return AWS_OP_SUCCESS;
3611 }
3612
3613 H1_CLIENT_TEST_CASE(h1_client_request_chunk_size_too_small_is_error) {
3614 (void)ctx;
3615 return s_test_chunk_length_mismatch_is_error(allocator, "I am very long", 2);
3616 }
3617
3618 H1_CLIENT_TEST_CASE(h1_client_request_chunk_size_too_large_is_error) {
3619 (void)ctx;
3620 return s_test_chunk_length_mismatch_is_error(allocator, "I am very short", 999);
3621 }
3622
3623 H1_CLIENT_TEST_CASE(h1_client_request_chunks_cancelled_by_channel_shutdown) {
3624 (void)ctx;
3625 struct tester tester;
3626 ASSERT_SUCCESS(s_tester_init(&tester, allocator));
3627
3628 /* send request */
3629 struct aws_http_message *request = s_new_default_chunked_put_request(allocator);
3630 int completion_error_code = 0;
3631 struct aws_http_make_request_options opt = {
3632 .self_size = sizeof(opt),
3633 .request = request,
3634 .user_data = &completion_error_code,
3635 .on_complete = s_on_complete,
3636 };
3637
3638 struct aws_http_stream *stream = aws_http_connection_make_request(tester.connection, &opt);
3639 ASSERT_NOT_NULL(stream);
3640 ASSERT_SUCCESS(aws_http_stream_activate(stream));
3641 const struct aws_byte_cursor body_cur = aws_byte_cursor_from_c_str("write more tests");
3642 struct aws_input_stream *body_stream = aws_input_stream_new_from_cursor(allocator, &body_cur);
3643
3644 /* This will "pause" the connection loop as there is an empty stream. */
3645 testing_channel_drain_queued_tasks(&tester.testing_channel);
3646
3647 /* Now write 2 chunks. The chunk memory should be automatically released when the http stream is destroyed. */
3648 struct aws_http1_chunk_options options = s_default_chunk_options(body_stream, body_cur.len);
3649 ASSERT_SUCCESS(aws_http1_stream_write_chunk(stream, &options));
3650 ASSERT_SUCCESS(s_write_termination_chunk(allocator, stream));
3651
3652 /* Ensure the request can be destroyed after request is sent */
3653 aws_http_message_destroy(opt.request);
3654
3655 /* shutdown channel before request completes */
3656 aws_channel_shutdown(tester.testing_channel.channel, AWS_ERROR_SUCCESS);
3657 testing_channel_drain_queued_tasks(&tester.testing_channel);
3658
3659 /* even though the channel shut down with error_code 0,
3660 * the stream should not get code 0 because it did not complete successfully */
3661 ASSERT_TRUE(completion_error_code != AWS_ERROR_SUCCESS);
3662
3663 /* clean up */
3664 aws_http_stream_release(stream);
3665
3666 ASSERT_SUCCESS(s_tester_clean_up(&tester));
3667 return AWS_OP_SUCCESS;
3668 }
3669
3670 /* If channel shuts down before any response is received, the request should complete with an error */
3671 H1_CLIENT_TEST_CASE(h1_client_request_cancelled_by_channel_shutdown_before_response) {
3672 (void)ctx;
3673 struct tester tester;
3674 ASSERT_SUCCESS(s_tester_init(&tester, allocator));
3675
3676 int completion_error_code = 0;
3677
3678 /* send request */
3679 struct aws_http_make_request_options opt = {
3680 .self_size = sizeof(opt),
3681 .request = s_new_default_get_request(allocator),
3682 .user_data = &completion_error_code,
3683 .on_complete = s_on_complete,
3684 };
3685 struct aws_http_stream *stream = aws_http_connection_make_request(tester.connection, &opt);
3686 ASSERT_NOT_NULL(stream);
3687 ASSERT_SUCCESS(aws_http_stream_activate(stream));
3688
3689 testing_channel_drain_queued_tasks(&tester.testing_channel);
3690
3691 /* Ensure the request can be destroyed after request is sent */
3692 aws_http_message_destroy(opt.request);
3693
3694 /* shutdown channel before request completes */
3695 aws_channel_shutdown(tester.testing_channel.channel, AWS_ERROR_SUCCESS);
3696 testing_channel_drain_queued_tasks(&tester.testing_channel);
3697
3698 /* even though the channel shut down with error_code 0,
3699 * the stream should not get code 0 because it did not complete successfully */
3700 ASSERT_TRUE(completion_error_code != AWS_ERROR_SUCCESS);
3701
3702 /* clean up */
3703 aws_http_stream_release(stream);
3704
3705 ASSERT_SUCCESS(s_tester_clean_up(&tester));
3706 return AWS_OP_SUCCESS;
3707 }
3708
3709 /* If channel shuts down in the middle of a response, the request should complete with an error */
3710 H1_CLIENT_TEST_CASE(h1_client_request_cancelled_by_channel_shutdown_mid_response) {
3711 (void)ctx;
3712 struct tester tester;
3713 ASSERT_SUCCESS(s_tester_init(&tester, allocator));
3714
3715 /* send request */
3716 struct aws_http_message *request = s_new_default_get_request(allocator);
3717
3718 struct client_stream_tester stream_tester;
3719 ASSERT_SUCCESS(s_stream_tester_init(&stream_tester, &tester, request));
3720
3721 testing_channel_drain_queued_tasks(&tester.testing_channel);
3722
3723 /* Ensure the request can be destroyed after request is sent */
3724 aws_http_message_destroy(request);
3725
3726 /* send response that is 1 byte short of being complete */
3727 const char *response_str = "HTTP/1.1 200 OK\r\n"
3728 "Content-Length: 4\r\n"
3729 "\r\n"
3730 "123";
3731 ASSERT_SUCCESS(testing_channel_push_read_str(&tester.testing_channel, response_str));
3732
3733 /* shutdown channel while response is 1 byte short of being complete */
3734 aws_channel_shutdown(tester.testing_channel.channel, AWS_ERROR_SUCCESS);
3735 testing_channel_drain_queued_tasks(&tester.testing_channel);
3736
3737 /* the request should complete with error, having only received a partial response */
3738 ASSERT_TRUE(stream_tester.complete);
3739 ASSERT_INT_EQUALS(AWS_ERROR_HTTP_CONNECTION_CLOSED, stream_tester.on_complete_error_code);
3740 ASSERT_UINT_EQUALS(3, stream_tester.response_body.len);
3741
3742 /* clean up */
3743 client_stream_tester_clean_up(&stream_tester);
3744 ASSERT_SUCCESS(s_tester_clean_up(&tester));
3745 return AWS_OP_SUCCESS;
3746 }
3747
3748 H1_CLIENT_TEST_CASE(h1_client_multiple_requests_cancelled_by_channel_shutdown) {
3749 (void)ctx;
3750 struct tester tester;
3751 ASSERT_SUCCESS(s_tester_init(&tester, allocator));
3752
3753 struct aws_http_stream *streams[3];
3754 int completion_error_codes[3];
3755 memset(completion_error_codes, 0, sizeof(completion_error_codes));
3756
3757 struct aws_http_make_request_options opt = {
3758 .self_size = sizeof(opt),
3759 .request = s_new_default_get_request(allocator),
3760 .on_complete = s_on_complete,
3761 };
3762
3763 for (int i = 0; i < 2; ++i) {
3764 opt.user_data = &completion_error_codes[i];
3765 streams[i] = aws_http_connection_make_request(tester.connection, &opt);
3766 ASSERT_NOT_NULL(streams[i]);
3767 ASSERT_SUCCESS(aws_http_stream_activate(streams[i]));
3768 }
3769
3770 /* 2 streams are now in-progress */
3771 testing_channel_drain_queued_tasks(&tester.testing_channel);
3772
3773 /* Make 1 more stream that's still locked away in the pending queue */
3774 opt.user_data = &completion_error_codes[2];
3775 streams[2] = aws_http_connection_make_request(tester.connection, &opt);
3776 ASSERT_NOT_NULL(streams[2]);
3777 ASSERT_SUCCESS(aws_http_stream_activate(streams[2]));
3778
3779 /* shutdown channel */
3780 aws_channel_shutdown(tester.testing_channel.channel, AWS_ERROR_SUCCESS);
3781 testing_channel_drain_queued_tasks(&tester.testing_channel);
3782
3783 /* Ensure the request can be destroyed after request is sent */
3784 aws_http_message_destroy(opt.request);
3785
3786 /* check results */
3787 for (int i = 0; i < 3; ++i) {
3788 ASSERT_TRUE(completion_error_codes[i] != AWS_ERROR_SUCCESS);
3789 aws_http_stream_release(streams[i]);
3790 }
3791
3792 ASSERT_SUCCESS(s_tester_clean_up(&tester));
3793 return AWS_OP_SUCCESS;
3794 }
3795
3796 H1_CLIENT_TEST_CASE(h1_client_new_request_fails_if_channel_shut_down) {
3797 (void)ctx;
3798 struct tester tester;
3799 ASSERT_SUCCESS(s_tester_init(&tester, allocator));
3800
3801 aws_channel_shutdown(tester.testing_channel.channel, AWS_ERROR_SUCCESS);
3802 /* wait for shutdown complete */
3803 testing_channel_drain_queued_tasks(&tester.testing_channel);
3804
3805 /* send request */
3806 struct aws_http_make_request_options opt = {
3807 .self_size = sizeof(opt),
3808 .request = s_new_default_get_request(allocator),
3809 };
3810
3811 struct aws_http_stream *stream = aws_http_connection_make_request(tester.connection, &opt);
3812 ASSERT_NULL(stream);
3813
3814 ASSERT_INT_EQUALS(aws_last_error(), AWS_ERROR_HTTP_CONNECTION_CLOSED);
3815
3816 aws_http_message_destroy(opt.request);
3817 ASSERT_SUCCESS(s_tester_clean_up(&tester));
3818 return AWS_OP_SUCCESS;
3819 }
3820
3821 enum request_callback {
3822 REQUEST_CALLBACK_OUTGOING_BODY,
3823 REQUEST_CALLBACK_INCOMING_HEADERS,
3824 REQUEST_CALLBACK_INCOMING_HEADERS_DONE,
3825 REQUEST_CALLBACK_INCOMING_BODY,
3826 REQUEST_CALLBACK_COMPLETE,
3827 REQUEST_CALLBACK_COUNT,
3828 };
3829
3830 static const int ERROR_FROM_CALLBACK_ERROR_CODE = (int)0xBEEFCAFE;
3831
3832 struct error_from_callback_tester {
3833 struct aws_input_stream base;
3834 enum request_callback error_at;
3835 int callback_counts[REQUEST_CALLBACK_COUNT];
3836 bool has_errored;
3837 struct aws_stream_status status;
3838 int on_complete_error_code;
3839 struct aws_allocator *alloc;
3840 };
3841
3842 static int s_error_from_callback_common(
3843 struct error_from_callback_tester *error_tester,
3844 enum request_callback current_callback) {
3845
3846 error_tester->callback_counts[current_callback]++;
3847
3848 /* After error code returned, no more callbacks should fire (except for on_complete) */
3849 AWS_FATAL_ASSERT(!error_tester->has_errored);
3850 AWS_FATAL_ASSERT(current_callback <= error_tester->error_at);
3851 if (current_callback == error_tester->error_at) {
3852 error_tester->has_errored = true;
3853 return aws_raise_error(ERROR_FROM_CALLBACK_ERROR_CODE);
3854 }
3855
3856 return AWS_OP_SUCCESS;
3857 }
3858
3859 static int s_error_from_outgoing_body_read(struct aws_input_stream *body, struct aws_byte_buf *dest) {
3860
3861 (void)dest;
3862
3863 struct error_from_callback_tester *error_tester = AWS_CONTAINER_OF(body, struct error_from_callback_tester, base);
3864 if (s_error_from_callback_common(error_tester, REQUEST_CALLBACK_OUTGOING_BODY)) {
3865 return AWS_OP_ERR;
3866 }
3867
3868 /* If the common fn was successful, write out some data and end the stream */
3869 ASSERT_TRUE(aws_byte_buf_write(dest, (const uint8_t *)"abcd", 4));
3870 error_tester->status.is_end_of_stream = true;
3871 return AWS_OP_SUCCESS;
3872 }
3873
3874 static int s_error_from_outgoing_body_get_status(struct aws_input_stream *body, struct aws_stream_status *status) {
3875 struct error_from_callback_tester *error_tester = AWS_CONTAINER_OF(body, struct error_from_callback_tester, base);
3876 *status = error_tester->status;
3877 return AWS_OP_SUCCESS;
3878 }
3879
3880 static void s_error_from_outgoing_body_destroy(struct aws_input_stream *stream) {
3881 (void)stream;
3882 }
3883
3884 static struct aws_input_stream_vtable s_error_from_outgoing_body_vtable = {
3885 .seek = NULL,
3886 .read = s_error_from_outgoing_body_read,
3887 .get_status = s_error_from_outgoing_body_get_status,
3888 .get_length = NULL,
3889 };
3890
3891 static int s_error_from_incoming_headers(
3892 struct aws_http_stream *stream,
3893 enum aws_http_header_block header_block,
3894 const struct aws_http_header *header_array,
3895 size_t num_headers,
3896 void *user_data) {
3897
3898 (void)stream;
3899 (void)header_block;
3900 (void)header_array;
3901 (void)num_headers;
3902 return s_error_from_callback_common(user_data, REQUEST_CALLBACK_INCOMING_HEADERS);
3903 }
3904
3905 static int s_error_from_incoming_headers_done(
3906 struct aws_http_stream *stream,
3907 enum aws_http_header_block header_block,
3908 void *user_data) {
3909 (void)stream;
3910 (void)header_block;
3911 return s_error_from_callback_common(user_data, REQUEST_CALLBACK_INCOMING_HEADERS_DONE);
3912 }
3913
3914 static int s_error_from_incoming_body(
3915 struct aws_http_stream *stream,
3916 const struct aws_byte_cursor *data,
3917 void *user_data) {
3918
3919 (void)stream;
3920 (void)data;
3921 return s_error_from_callback_common(user_data, REQUEST_CALLBACK_INCOMING_BODY);
3922 }
3923
3924 static void s_error_tester_on_stream_complete(struct aws_http_stream *stream, int error_code, void *user_data) {
3925 (void)stream;
3926 struct error_from_callback_tester *error_tester = user_data;
3927 error_tester->callback_counts[REQUEST_CALLBACK_COMPLETE]++;
3928 error_tester->on_complete_error_code = error_code;
3929 }
3930
3931 static int s_test_error_from_callback(struct aws_allocator *allocator, enum request_callback error_at) {
3932 struct tester tester;
3933 ASSERT_SUCCESS(s_tester_init(&tester, allocator));
3934
3935 struct aws_input_stream empty_stream_base;
3936 AWS_ZERO_STRUCT(empty_stream_base);
3937 struct error_from_callback_tester error_tester = {
3938 .base = empty_stream_base,
3939 .error_at = error_at,
3940 .status =
3941 {
3942 .is_valid = true,
3943 .is_end_of_stream = false,
3944 },
3945 };
3946 error_tester.base.vtable = &s_error_from_outgoing_body_vtable;
3947 aws_ref_count_init(
3948 &error_tester.base.ref_count,
3949 &error_tester,
3950 (aws_simple_completion_callback *)s_error_from_outgoing_body_destroy);
3951 struct aws_input_stream *error_from_outgoing_body_stream = &error_tester.base;
3952 /* send request */
3953 struct aws_http_header headers[] = {
3954 {
3955 .name = aws_byte_cursor_from_c_str("Content-Length"),
3956 .value = aws_byte_cursor_from_c_str("4"),
3957 },
3958 };
3959
3960 struct aws_http_message *request = aws_http_message_new_request(allocator);
3961 ASSERT_NOT_NULL(request);
3962 ASSERT_SUCCESS(aws_http_message_set_request_method(request, aws_http_method_post));
3963 ASSERT_SUCCESS(aws_http_message_set_request_path(request, aws_byte_cursor_from_c_str("/")));
3964 ASSERT_SUCCESS(aws_http_message_add_header_array(request, headers, AWS_ARRAY_SIZE(headers)));
3965 aws_http_message_set_body_stream(request, error_from_outgoing_body_stream);
3966
3967 struct aws_http_make_request_options opt = {
3968 .self_size = sizeof(opt),
3969 .request = request,
3970 .on_response_headers = s_error_from_incoming_headers,
3971 .on_response_header_block_done = s_error_from_incoming_headers_done,
3972 .on_response_body = s_error_from_incoming_body,
3973 .on_complete = s_error_tester_on_stream_complete,
3974 .user_data = &error_tester,
3975 };
3976
3977 struct aws_http_stream *stream = aws_http_connection_make_request(tester.connection, &opt);
3978 ASSERT_NOT_NULL(stream);
3979 ASSERT_SUCCESS(aws_http_stream_activate(stream));
3980
3981 testing_channel_drain_queued_tasks(&tester.testing_channel);
3982
3983 /* Ensure the request can be destroyed after request is sent */
3984 aws_http_message_destroy(opt.request);
3985 aws_input_stream_release(error_from_outgoing_body_stream);
3986
3987 /* send response */
3988 ASSERT_SUCCESS(testing_channel_push_read_str_ignore_errors(
3989 &tester.testing_channel,
3990 "HTTP/1.1 200 OK\r\n"
3991 "Transfer-Encoding: chunked\r\n"
3992 "Date: Fri, 01 Mar 2019 17:18:55 GMT\r\n"
3993 "\r\n"
3994 "3\r\n"
3995 "two\r\n"
3996 "6\r\n"
3997 "chunks\r\n"
3998 "0\r\n"
3999 "\r\n"));
4000
4001 testing_channel_drain_queued_tasks(&tester.testing_channel);
4002
4003 /* check that callbacks were invoked before error_at, but not after */
4004 for (int i = 0; i < REQUEST_CALLBACK_COMPLETE; ++i) {
4005 if (i <= error_at) {
4006 ASSERT_TRUE(error_tester.callback_counts[i] > 0);
4007 } else {
4008 ASSERT_INT_EQUALS(0, error_tester.callback_counts[i]);
4009 }
4010 }
4011
4012 /* the on_complete callback should always fire though, and should receive the proper error_code */
4013 ASSERT_INT_EQUALS(1, error_tester.callback_counts[REQUEST_CALLBACK_COMPLETE]);
4014 ASSERT_INT_EQUALS(ERROR_FROM_CALLBACK_ERROR_CODE, error_tester.on_complete_error_code);
4015
4016 aws_http_stream_release(stream);
4017 ASSERT_SUCCESS(s_tester_clean_up(&tester));
4018 return AWS_OP_SUCCESS;
4019 }
4020
4021 H1_CLIENT_TEST_CASE(h1_client_error_from_outgoing_body_callback_stops_decoder) {
4022 (void)ctx;
4023 ASSERT_SUCCESS(s_test_error_from_callback(allocator, REQUEST_CALLBACK_OUTGOING_BODY));
4024 return AWS_OP_SUCCESS;
4025 }
4026
4027 H1_CLIENT_TEST_CASE(h1_client_error_from_incoming_headers_callback_stops_decoder) {
4028 (void)ctx;
4029 ASSERT_SUCCESS(s_test_error_from_callback(allocator, REQUEST_CALLBACK_INCOMING_HEADERS));
4030 return AWS_OP_SUCCESS;
4031 }
4032
4033 H1_CLIENT_TEST_CASE(h1_client_error_from_incoming_headers_done_callback_stops_decoder) {
4034 (void)ctx;
4035 ASSERT_SUCCESS(s_test_error_from_callback(allocator, REQUEST_CALLBACK_INCOMING_HEADERS_DONE));
4036 return AWS_OP_SUCCESS;
4037 }
4038
4039 H1_CLIENT_TEST_CASE(h1_client_error_from_incoming_body_callback_stops_decoder) {
4040 (void)ctx;
4041 ASSERT_SUCCESS(s_test_error_from_callback(allocator, REQUEST_CALLBACK_INCOMING_BODY));
4042 return AWS_OP_SUCCESS;
4043 }
4044
4045 /* After aws_http_connection_close() is called, aws_http_connection_is_open() should return false,
4046 * even if both calls were made from outside the event-loop thread. */
4047 H1_CLIENT_TEST_CASE(h1_client_close_from_off_thread_makes_not_open) {
4048 (void)ctx;
4049 struct tester tester;
4050 ASSERT_SUCCESS(s_tester_init(&tester, allocator));
4051
4052 testing_channel_set_is_on_users_thread(&tester.testing_channel, false);
4053
4054 ASSERT_TRUE(aws_http_connection_is_open(tester.connection));
4055 aws_http_connection_close(tester.connection);
4056 ASSERT_FALSE(aws_http_connection_is_open(tester.connection));
4057
4058 testing_channel_set_is_on_users_thread(&tester.testing_channel, true);
4059
4060 ASSERT_SUCCESS(s_tester_clean_up(&tester));
4061 return AWS_OP_SUCCESS;
4062 }
4063
4064 H1_CLIENT_TEST_CASE(h1_client_close_from_on_thread_makes_not_open) {
4065 (void)ctx;
4066 struct tester tester;
4067 ASSERT_SUCCESS(s_tester_init(&tester, allocator));
4068
4069 testing_channel_set_is_on_users_thread(&tester.testing_channel, false);
4070 ASSERT_TRUE(aws_http_connection_is_open(tester.connection));
4071
4072 testing_channel_set_is_on_users_thread(&tester.testing_channel, true);
4073 aws_http_connection_close(tester.connection);
4074
4075 testing_channel_set_is_on_users_thread(&tester.testing_channel, false);
4076 ASSERT_FALSE(aws_http_connection_is_open(tester.connection));
4077
4078 testing_channel_set_is_on_users_thread(&tester.testing_channel, true);
4079
4080 ASSERT_SUCCESS(s_tester_clean_up(&tester));
4081 return AWS_OP_SUCCESS;
4082 }
4083
4084 struct s_callback_invoked {
4085 bool destroy_invoked;
4086 bool complete_invoked;
4087 };
4088
4089 static void s_unactivated_stream_cleans_up_on_destroy(void *data) {
4090 struct s_callback_invoked *callback_data = data;
4091 callback_data->destroy_invoked = true;
4092 }
4093
4094 static void s_unactivated_stream_complete(struct aws_http_stream *stream, int error_code, void *data) {
4095 (void)stream;
4096 (void)error_code;
4097 struct s_callback_invoked *callback_data = data;
4098 callback_data->complete_invoked = true;
4099 }
4100
4101 H1_CLIENT_TEST_CASE(h1_client_unactivated_stream_cleans_up) {
4102 (void)ctx;
4103 struct tester tester;
4104 ASSERT_SUCCESS(s_tester_init(&tester, allocator));
4105 ASSERT_TRUE(aws_http_connection_is_open(tester.connection));
4106
4107 struct aws_http_message *request = aws_http_message_new_request(allocator);
4108 ASSERT_SUCCESS(aws_http_message_set_request_method(request, aws_byte_cursor_from_c_str("GET")));
4109 ASSERT_SUCCESS(aws_http_message_set_request_path(request, aws_byte_cursor_from_c_str("/")));
4110 struct s_callback_invoked callback_data = {0};
4111
4112 struct aws_http_make_request_options options = {
4113 .self_size = sizeof(struct aws_http_make_request_options),
4114 .request = request,
4115 .on_destroy = s_unactivated_stream_cleans_up_on_destroy,
4116 .on_complete = s_unactivated_stream_complete,
4117 .user_data = &callback_data,
4118 };
4119
4120 struct aws_http_stream *stream = aws_http_connection_make_request(tester.connection, &options);
4121 aws_http_message_release(request);
4122 ASSERT_NOT_NULL(stream);
4123 /* we do not activate, that is the test. */
4124 ASSERT_FALSE(callback_data.destroy_invoked);
4125 ASSERT_FALSE(callback_data.complete_invoked);
4126 aws_http_stream_release(stream);
4127 /* Only destroy invoked, the complete was not invoked */
4128 ASSERT_TRUE(callback_data.destroy_invoked);
4129 ASSERT_FALSE(callback_data.complete_invoked);
4130 aws_http_connection_close(tester.connection);
4131 ASSERT_SUCCESS(s_tester_clean_up(&tester));
4132 return AWS_OP_SUCCESS;
4133 }
4134
4135 struct protocol_switcher {
4136 /* Settings */
4137 struct tester *tester;
4138 size_t downstream_handler_window_size;
4139 const char *data_after_upgrade_response;
4140 bool install_downstream_handler;
4141
4142 /* Results */
4143 int upgrade_response_status;
4144 bool has_installed_downstream_handler;
4145 };
4146
4147 static int s_switch_protocols_on_response_header_block_done(
4148 struct aws_http_stream *stream,
4149 enum aws_http_header_block header_block,
4150 void *user_data) {
4151 (void)header_block;
4152
4153 struct protocol_switcher *switcher = user_data;
4154 aws_http_stream_get_incoming_response_status(stream, &switcher->upgrade_response_status);
4155
4156 /* install downstream hander */
4157 if (switcher->install_downstream_handler &&
4158 (switcher->upgrade_response_status == AWS_HTTP_STATUS_CODE_101_SWITCHING_PROTOCOLS)) {
4159
4160 int err = testing_channel_install_downstream_handler(
4161 &switcher->tester->testing_channel, switcher->downstream_handler_window_size);
4162 if (!err) {
4163 switcher->has_installed_downstream_handler = true;
4164 }
4165 }
4166
4167 return AWS_OP_SUCCESS;
4168 }
4169
4170 /* Send "Connection: Upgrade" request and receive "101 Switching Protocols" response.
4171 * Optionally, install a downstream handler when response is received
4172 */
4173 static int s_switch_protocols(struct protocol_switcher *switcher) {
4174 /* send upgrade request */
4175 struct aws_http_header request_headers[] = {
4176 {
4177 .name = aws_byte_cursor_from_c_str("Connection"),
4178 .value = aws_byte_cursor_from_c_str("Upgrade"),
4179 },
4180 {
4181 .name = aws_byte_cursor_from_c_str("Upgrade"),
4182 .value = aws_byte_cursor_from_c_str("MyProtocol"),
4183 },
4184 };
4185
4186 struct aws_http_message *request = aws_http_message_new_request(switcher->tester->alloc);
4187 ASSERT_NOT_NULL(request);
4188 ASSERT_SUCCESS(aws_http_message_set_request_method(request, aws_http_method_get));
4189 ASSERT_SUCCESS(aws_http_message_set_request_path(request, aws_byte_cursor_from_c_str("/")));
4190 ASSERT_SUCCESS(aws_http_message_add_header_array(request, request_headers, AWS_ARRAY_SIZE(request_headers)));
4191
4192 struct aws_http_make_request_options upgrade_request = {
4193 .self_size = sizeof(upgrade_request),
4194 .request = request,
4195 .user_data = switcher,
4196 .on_response_header_block_done = s_switch_protocols_on_response_header_block_done,
4197 };
4198
4199 struct aws_http_stream *upgrade_stream =
4200 aws_http_connection_make_request(switcher->tester->connection, &upgrade_request);
4201 ASSERT_NOT_NULL(upgrade_stream);
4202 ASSERT_SUCCESS(aws_http_stream_activate(upgrade_stream));
4203 testing_channel_drain_queued_tasks(&switcher->tester->testing_channel);
4204
4205 /* Ensure the request can be destroyed after request is sent */
4206 aws_http_message_destroy(upgrade_request.request);
4207
4208 /* clear all messages written thus far to the testing-channel */
4209 while (!aws_linked_list_empty(testing_channel_get_written_message_queue(&switcher->tester->testing_channel))) {
4210 struct aws_linked_list_node *node =
4211 aws_linked_list_pop_front(testing_channel_get_written_message_queue(&switcher->tester->testing_channel));
4212 struct aws_io_message *msg = AWS_CONTAINER_OF(node, struct aws_io_message, queueing_handle);
4213 aws_mem_release(msg->allocator, msg);
4214 }
4215
4216 /* send upgrade response (followed by any extra data) */
4217 struct aws_byte_cursor response = aws_byte_cursor_from_c_str("HTTP/1.1 101 Switching Protocols\r\n"
4218 "Upgrade: MyProtocol\r\n"
4219 "\r\n");
4220 struct aws_byte_cursor extra_data = aws_byte_cursor_from_c_str(switcher->data_after_upgrade_response);
4221 struct aws_byte_buf sending_buf;
4222 ASSERT_SUCCESS(aws_byte_buf_init(&sending_buf, switcher->tester->alloc, response.len + extra_data.len));
4223 ASSERT_TRUE(aws_byte_buf_write_from_whole_cursor(&sending_buf, response));
4224 if (extra_data.len) {
4225 ASSERT_TRUE(aws_byte_buf_write_from_whole_cursor(&sending_buf, extra_data));
4226 }
4227
4228 ASSERT_SUCCESS(
4229 testing_channel_push_read_data(&switcher->tester->testing_channel, aws_byte_cursor_from_buf(&sending_buf)));
4230
4231 /* wait for response to complete, and check results */
4232 testing_channel_drain_queued_tasks(&switcher->tester->testing_channel);
4233 ASSERT_INT_EQUALS(101, switcher->upgrade_response_status);
4234
4235 /* if we wanted downstream handler installed, ensure that happened */
4236 if (switcher->install_downstream_handler) {
4237 ASSERT_TRUE(switcher->has_installed_downstream_handler);
4238 }
4239
4240 /* cleanup */
4241 aws_byte_buf_clean_up(&sending_buf);
4242 aws_http_stream_release(upgrade_stream);
4243 return AWS_OP_SUCCESS;
4244 }
4245
4246 H1_CLIENT_TEST_CASE(h1_client_new_request_allowed) {
4247 (void)ctx;
4248 struct tester tester;
4249 ASSERT_SUCCESS(s_tester_init(&tester, allocator));
4250
4251 /* prepare request */
4252 struct aws_http_make_request_options options = {
4253 .self_size = sizeof(options),
4254 .request = s_new_default_get_request(allocator),
4255 };
4256
4257 /* validate the new request is allowed for now */
4258 ASSERT_TRUE(aws_http_connection_new_requests_allowed(tester.connection));
4259
4260 /* switch protocols */
4261 struct protocol_switcher switcher = {
4262 .tester = &tester,
4263 .install_downstream_handler = true,
4264 };
4265 ASSERT_SUCCESS(s_switch_protocols(&switcher));
4266
4267 /* validate the new request is not allowed anymore when goaway received */
4268 ASSERT_FALSE(aws_http_connection_new_requests_allowed(tester.connection));
4269 /* Make new request will fail */
4270 ASSERT_NULL(aws_http_connection_make_request(tester.connection, &options));
4271 ASSERT_UINT_EQUALS(AWS_ERROR_HTTP_SWITCHED_PROTOCOLS, aws_last_error());
4272
4273 /* close connection */
4274 aws_http_connection_close(tester.connection);
4275 /* Make new request will fail */
4276 ASSERT_NULL(aws_http_connection_make_request(tester.connection, &options));
4277 ASSERT_UINT_EQUALS(AWS_ERROR_HTTP_CONNECTION_CLOSED, aws_last_error());
4278
4279 /* clean up */
4280 aws_http_message_destroy(options.request);
4281 ASSERT_SUCCESS(s_tester_clean_up(&tester));
4282 return AWS_OP_SUCCESS;
4283 }
4284
4285 H1_CLIENT_TEST_CASE(h1_client_midchannel_sanity_check) {
4286 (void)ctx;
4287 struct tester tester;
4288 ASSERT_SUCCESS(s_tester_init(&tester, allocator));
4289
4290 struct protocol_switcher switcher = {
4291 .tester = &tester,
4292 .install_downstream_handler = true,
4293 };
4294 ASSERT_SUCCESS(s_switch_protocols(&switcher));
4295
4296 /* clean up */
4297 ASSERT_SUCCESS(s_tester_clean_up(&tester));
4298 return AWS_OP_SUCCESS;
4299 }
4300
4301 /* confirm data passes through http-handler untouched in the read direction */
4302 H1_CLIENT_TEST_CASE(h1_client_midchannel_read) {
4303 (void)ctx;
4304 struct tester tester;
4305 ASSERT_SUCCESS(s_tester_init(&tester, allocator));
4306
4307 struct protocol_switcher switcher = {
4308 .tester = &tester,
4309 .install_downstream_handler = true,
4310 .downstream_handler_window_size = SIZE_MAX,
4311 };
4312 ASSERT_SUCCESS(s_switch_protocols(&switcher));
4313
4314 const char *test_str = "inmyprotocolspacesarestrictlyforbidden";
4315 ASSERT_SUCCESS(testing_channel_push_read_str(&tester.testing_channel, test_str));
4316 testing_channel_drain_queued_tasks(&tester.testing_channel);
4317 ASSERT_SUCCESS(testing_channel_check_midchannel_read_messages_str(&tester.testing_channel, allocator, test_str));
4318
4319 ASSERT_SUCCESS(s_tester_clean_up(&tester));
4320 return AWS_OP_SUCCESS;
4321 }
4322
4323 /* confirm that, if new-protocol-data arrives packed into the same aws_io_message as the upgrade response,
4324 * that data is properly passed dowstream. */
4325 H1_CLIENT_TEST_CASE(h1_client_midchannel_read_immediately) {
4326 (void)ctx;
4327 struct tester tester;
4328 ASSERT_SUCCESS(s_tester_init(&tester, allocator));
4329
4330 const char *test_str = "inmyprotocoleverythingwillbebetter";
4331
4332 struct protocol_switcher switcher = {
4333 .tester = &tester,
4334 .install_downstream_handler = true,
4335 .downstream_handler_window_size = SIZE_MAX,
4336 .data_after_upgrade_response = test_str, /* Note extra data */
4337 };
4338 ASSERT_SUCCESS(s_switch_protocols(&switcher));
4339
4340 ASSERT_SUCCESS(testing_channel_check_midchannel_read_messages_str(&tester.testing_channel, allocator, test_str));
4341
4342 ASSERT_SUCCESS(s_tester_clean_up(&tester));
4343 return AWS_OP_SUCCESS;
4344 }
4345
4346 /* Have a tiny downstream read-window and increment it in little chunks. */
4347 H1_CLIENT_TEST_CASE(h1_client_midchannel_read_with_small_downstream_window) {
4348 (void)ctx;
4349 struct tester tester;
4350 ASSERT_SUCCESS(s_tester_init(&tester, allocator));
4351
4352 struct protocol_switcher switcher = {
4353 .tester = &tester,
4354 .install_downstream_handler = true,
4355 .downstream_handler_window_size = 1 /* Note tiny starting window. */,
4356 };
4357 ASSERT_SUCCESS(s_switch_protocols(&switcher));
4358
4359 const char *test_str = "inmyprotocolcapitallettersarethedevil";
4360 ASSERT_SUCCESS(testing_channel_push_read_str(&tester.testing_channel, test_str));
4361
4362 /* open window in tiny increments */
4363 for (size_t i = 0; i < strlen(test_str); ++i) {
4364 ASSERT_SUCCESS(testing_channel_increment_read_window(&tester.testing_channel, 1));
4365 testing_channel_drain_queued_tasks(&tester.testing_channel);
4366 }
4367
4368 /* ensure that the handler actually sent multiple messages */
4369 size_t num_read_messages = 0;
4370 struct aws_linked_list *list = testing_channel_get_read_message_queue(&tester.testing_channel);
4371 struct aws_linked_list_node *node = aws_linked_list_front(list);
4372 while (node != aws_linked_list_end(list)) {
4373 num_read_messages++;
4374 node = aws_linked_list_next(node);
4375 }
4376 ASSERT_TRUE(num_read_messages > 1);
4377
4378 ASSERT_SUCCESS(testing_channel_check_midchannel_read_messages_str(&tester.testing_channel, allocator, test_str));
4379
4380 /* cleanup */
4381 ASSERT_SUCCESS(s_tester_clean_up(&tester));
4382 return AWS_OP_SUCCESS;
4383 }
4384
4385 /* confirm data passes through http-handler untouched in the write direction */
4386 H1_CLIENT_TEST_CASE(h1_client_midchannel_write) {
4387 (void)ctx;
4388 struct tester tester;
4389 ASSERT_SUCCESS(s_tester_init(&tester, allocator));
4390
4391 struct protocol_switcher switcher = {
4392 .tester = &tester,
4393 .install_downstream_handler = true,
4394 .downstream_handler_window_size = SIZE_MAX,
4395 };
4396 ASSERT_SUCCESS(s_switch_protocols(&switcher));
4397
4398 const char *test_str = "inmyprotocolthereisnomoney";
4399 testing_channel_push_write_str(&tester.testing_channel, test_str);
4400 testing_channel_drain_queued_tasks(&tester.testing_channel);
4401 ASSERT_SUCCESS(testing_channel_check_written_messages_str(&tester.testing_channel, allocator, test_str));
4402
4403 ASSERT_SUCCESS(s_tester_clean_up(&tester));
4404 return AWS_OP_SUCCESS;
4405 }
4406
4407 /* Test that, when HTTP is a midchannel handler, it will continue processing aws_io_messages write messages
4408 * in the time between shutdown-in-the-read-direction and shutdown-in-the-write-direction */
4409 static const char *s_write_after_shutdown_in_read_dir_str = "inmyprotocolfrowningisnotallowed";
4410
4411 static void s_downstream_handler_write_on_shutdown(
4412 enum aws_channel_direction dir,
4413 int error_code,
4414 bool free_scarce_resources_immediately,
4415 void *user_data) {
4416
4417 (void)error_code;
4418 (void)free_scarce_resources_immediately;
4419
4420 struct tester *tester = user_data;
4421
4422 if (dir == AWS_CHANNEL_DIR_WRITE) {
4423 testing_channel_push_write_str(&tester->testing_channel, s_write_after_shutdown_in_read_dir_str);
4424 }
4425 }
4426
4427 H1_CLIENT_TEST_CASE(h1_client_midchannel_write_continues_after_shutdown_in_read_dir) {
4428 (void)ctx;
4429 struct tester tester;
4430 ASSERT_SUCCESS(s_tester_init(&tester, allocator));
4431
4432 struct protocol_switcher switcher = {
4433 .tester = &tester,
4434 .install_downstream_handler = true,
4435 .downstream_handler_window_size = SIZE_MAX,
4436 };
4437 ASSERT_SUCCESS(s_switch_protocols(&switcher));
4438
4439 /* Downstream handler will write data while shutting down in write direction */
4440 testing_channel_set_downstream_handler_shutdown_callback(
4441 &tester.testing_channel, s_downstream_handler_write_on_shutdown, &tester);
4442
4443 /* Shutdown cannel */
4444 aws_channel_shutdown(tester.testing_channel.channel, AWS_ERROR_SUCCESS);
4445 testing_channel_drain_queued_tasks(&tester.testing_channel);
4446
4447 /* Did the late message get through? */
4448 ASSERT_SUCCESS(testing_channel_check_written_messages_str(
4449 &tester.testing_channel, tester.alloc, s_write_after_shutdown_in_read_dir_str));
4450
4451 ASSERT_SUCCESS(s_tester_clean_up(&tester));
4452 return AWS_OP_SUCCESS;
4453 }
4454
4455 static void s_on_message_write_complete_save_error_code(
4456 struct aws_channel *channel,
4457 struct aws_io_message *message,
4458 int err_code,
4459 void *user_data) {
4460
4461 (void)channel;
4462 (void)message;
4463 int *save = user_data;
4464 *save = err_code;
4465 }
4466
4467 /* Ensure that things fail if a downstream handler is installed without switching protocols.
4468 * This test is weird in that failure must occur, but we're not prescriptive about where it occurs. */
4469 H1_CLIENT_TEST_CASE(h1_client_midchannel_requires_switching_protocols) {
4470 (void)ctx;
4471 struct tester tester;
4472 ASSERT_SUCCESS(s_tester_init(&tester, allocator));
4473
4474 /* The act of installing the downstream handler might fail */
4475 int err = testing_channel_install_downstream_handler(&tester.testing_channel, SIZE_MAX);
4476 if (err) {
4477 goto installation_failed;
4478 }
4479
4480 /* Sending the message might fail */
4481 int msg_completion_error_code = 0;
4482 struct aws_io_message *msg = aws_channel_acquire_message_from_pool(
4483 tester.testing_channel.channel, AWS_IO_MESSAGE_APPLICATION_DATA, SIZE_MAX);
4484 ASSERT_NOT_NULL(msg);
4485 msg->on_completion = s_on_message_write_complete_save_error_code;
4486 msg->user_data = &msg_completion_error_code;
4487
4488 err = testing_channel_push_write_message(&tester.testing_channel, msg);
4489 if (err) {
4490 aws_mem_release(msg->allocator, msg);
4491 goto push_message_failed;
4492 }
4493
4494 /* The message might fail to reach the socket */
4495 testing_channel_drain_queued_tasks(&tester.testing_channel);
4496 if (msg_completion_error_code) {
4497 goto message_completion_failed;
4498 }
4499
4500 /* This is bad, we should have failed by now */
4501 ASSERT_TRUE(false);
4502
4503 message_completion_failed:
4504 push_message_failed:
4505 installation_failed:
4506
4507 ASSERT_SUCCESS(s_tester_clean_up(&tester));
4508 return AWS_OP_SUCCESS;
4509 }
4510
4511 H1_CLIENT_TEST_CASE(h1_client_switching_protocols_fails_pending_requests) {
4512 (void)ctx;
4513 struct tester tester;
4514 ASSERT_SUCCESS(s_tester_init(&tester, allocator));
4515
4516 /* queue a connection upgrade request */
4517 struct aws_http_header headers[] = {
4518 {
4519 .name = aws_byte_cursor_from_c_str("Connection"),
4520 .value = aws_byte_cursor_from_c_str("Upgrade"),
4521 },
4522 {
4523 .name = aws_byte_cursor_from_c_str("Upgrade"),
4524 .value = aws_byte_cursor_from_c_str("MyProtocol"),
4525 },
4526 };
4527
4528 struct aws_http_message *upgrade_request = aws_http_message_new_request(allocator);
4529 ASSERT_NOT_NULL(upgrade_request);
4530 ASSERT_SUCCESS(aws_http_message_set_request_method(upgrade_request, aws_http_method_get));
4531 ASSERT_SUCCESS(aws_http_message_set_request_path(upgrade_request, aws_byte_cursor_from_c_str("/")));
4532 ASSERT_SUCCESS(aws_http_message_add_header_array(upgrade_request, headers, AWS_ARRAY_SIZE(headers)));
4533
4534 struct client_stream_tester upgrade_stream;
4535 ASSERT_SUCCESS(s_stream_tester_init(&upgrade_stream, &tester, upgrade_request));
4536
4537 /* queue another request behind it */
4538 struct aws_http_message *next_request = s_new_default_get_request(allocator);
4539
4540 struct client_stream_tester next_stream;
4541 ASSERT_SUCCESS(s_stream_tester_init(&next_stream, &tester, next_request));
4542
4543 /* send upgrade response */
4544 testing_channel_drain_queued_tasks(&tester.testing_channel);
4545
4546 /* Ensure the request can be destroyed after request is sent */
4547 aws_http_message_destroy(upgrade_request);
4548 aws_http_message_destroy(next_request);
4549
4550 ASSERT_SUCCESS(testing_channel_push_read_str(
4551 &tester.testing_channel,
4552 "HTTP/1.1 101 Switching Protocols\r\n"
4553 "Upgrade: MyProtocol\r\n"
4554 "\r\n"));
4555
4556 testing_channel_drain_queued_tasks(&tester.testing_channel);
4557
4558 /* confirm that the next request was cancelled */
4559 ASSERT_TRUE(next_stream.complete);
4560 ASSERT_TRUE(next_stream.on_complete_error_code != AWS_OP_SUCCESS);
4561
4562 /* clean up */
4563 client_stream_tester_clean_up(&upgrade_stream);
4564 client_stream_tester_clean_up(&next_stream);
4565 ASSERT_SUCCESS(s_tester_clean_up(&tester));
4566 return AWS_OP_SUCCESS;
4567 }
4568
4569 H1_CLIENT_TEST_CASE(h1_client_switching_protocols_fails_subsequent_requests) {
4570 (void)ctx;
4571 struct tester tester;
4572 ASSERT_SUCCESS(s_tester_init(&tester, allocator));
4573
4574 /* Successfully switch protocols */
4575 struct protocol_switcher switcher = {
4576 .tester = &tester,
4577 .install_downstream_handler = true,
4578 };
4579 ASSERT_SUCCESS(s_switch_protocols(&switcher));
4580
4581 /* Attempting to send a request after this should fail. */
4582 struct aws_http_message *request = s_new_default_get_request(allocator);
4583
4584 struct client_stream_tester stream_tester;
4585 int err = s_stream_tester_init(&stream_tester, &tester, request);
4586 if (err) {
4587 ASSERT_INT_EQUALS(AWS_ERROR_HTTP_SWITCHED_PROTOCOLS, aws_last_error());
4588 } else {
4589 testing_channel_drain_queued_tasks(&tester.testing_channel);
4590 ASSERT_TRUE(stream_tester.complete);
4591 ASSERT_INT_EQUALS(AWS_ERROR_HTTP_SWITCHED_PROTOCOLS, stream_tester.on_complete_error_code);
4592 }
4593
4594 /* clean up */
4595 aws_http_message_destroy(request);
4596 client_stream_tester_clean_up(&stream_tester);
4597 ASSERT_SUCCESS(s_tester_clean_up(&tester));
4598 return AWS_OP_SUCCESS;
4599 }
4600
4601 H1_CLIENT_TEST_CASE(h1_client_switching_protocols_requires_downstream_handler) {
4602 (void)ctx;
4603 struct tester tester;
4604 ASSERT_SUCCESS(s_tester_init(&tester, allocator));
4605
4606 /* Successfully switch protocols, but don't install downstream handler. */
4607 struct protocol_switcher switcher = {
4608 .tester = &tester,
4609 .install_downstream_handler = false,
4610 };
4611
4612 ASSERT_SUCCESS(s_switch_protocols(&switcher));
4613
4614 /* If new data arrives and no downstream handler is installed to deal with it, the connection should shut down. */
4615 ASSERT_SUCCESS(
4616 testing_channel_push_read_str_ignore_errors(&tester.testing_channel, "herecomesnewprotocoldatachoochoo"));
4617
4618 testing_channel_drain_queued_tasks(&tester.testing_channel);
4619 ASSERT_TRUE(testing_channel_is_shutdown_completed(&tester.testing_channel));
4620 ASSERT_TRUE(testing_channel_get_shutdown_error_code(&tester.testing_channel) != AWS_ERROR_SUCCESS);
4621
4622 /* clean up */
4623 ASSERT_SUCCESS(s_tester_clean_up(&tester));
4624 return AWS_OP_SUCCESS;
4625 }
4626
4627 H1_CLIENT_TEST_CASE(h1_client_connection_close_before_request_finishes) {
4628 (void)ctx;
4629 struct tester tester;
4630 ASSERT_SUCCESS(s_tester_init(&tester, allocator));
4631
4632 /* set up request whose body won't send immediately */
4633 struct slow_body_sender body_sender;
4634 AWS_ZERO_STRUCT(body_sender);
4635 s_slow_body_sender_init(&body_sender);
4636 struct aws_input_stream *body_stream = &body_sender.base;
4637
4638 struct aws_http_header headers[] = {
4639 {
4640 .name = aws_byte_cursor_from_c_str("Content-Length"),
4641 .value = aws_byte_cursor_from_c_str("16"),
4642 },
4643 };
4644
4645 struct aws_http_message *request = aws_http_message_new_request(allocator);
4646 ASSERT_NOT_NULL(request);
4647 ASSERT_SUCCESS(aws_http_message_set_request_method(request, aws_byte_cursor_from_c_str("PUT")));
4648 ASSERT_SUCCESS(aws_http_message_set_request_path(request, aws_byte_cursor_from_c_str("/plan.txt")));
4649 ASSERT_SUCCESS(aws_http_message_add_header_array(request, headers, AWS_ARRAY_SIZE(headers)));
4650 aws_http_message_set_body_stream(request, body_stream);
4651
4652 struct client_stream_tester stream_tester;
4653 ASSERT_SUCCESS(s_stream_tester_init(&stream_tester, &tester, request));
4654
4655 /* send head of request */
4656 testing_channel_run_currently_queued_tasks(&tester.testing_channel);
4657
4658 /* Ensure the request can be destroyed after request is sent */
4659 aws_http_message_destroy(request);
4660 aws_input_stream_release(body_stream);
4661
4662 /* send close connection response */
4663 ASSERT_SUCCESS(testing_channel_push_read_str(
4664 &tester.testing_channel,
4665 "HTTP/1.1 404 Not Found\r\n"
4666 "Date: Fri, 01 Mar 2019 17:18:55 GMT\r\n"
4667 "\r\n"));
4668
4669 testing_channel_run_currently_queued_tasks(&tester.testing_channel);
4670
4671 aws_channel_shutdown(tester.testing_channel.channel, AWS_ERROR_SUCCESS);
4672 /* Wait for channel to finish shutdown */
4673 testing_channel_drain_queued_tasks(&tester.testing_channel);
4674 /* check result, should not receive any body */
4675 const char *expected = "PUT /plan.txt HTTP/1.1\r\n"
4676 "Content-Length: 16\r\n"
4677 "\r\n";
4678 ASSERT_SUCCESS(testing_channel_check_written_messages_str(&tester.testing_channel, allocator, expected));
4679
4680 ASSERT_TRUE(stream_tester.complete);
4681 ASSERT_INT_EQUALS(AWS_ERROR_SUCCESS, stream_tester.on_complete_error_code);
4682
4683 /* clean up */
4684 client_stream_tester_clean_up(&stream_tester);
4685 ASSERT_SUCCESS(s_tester_clean_up(&tester));
4686 return AWS_OP_SUCCESS;
4687 }
4688
4689 H1_CLIENT_TEST_CASE(h1_client_stream_cancel) {
4690 (void)ctx;
4691 struct tester tester;
4692 ASSERT_SUCCESS(s_tester_init(&tester, allocator));
4693
4694 /* set up request whose body won't send immediately */
4695 struct slow_body_sender body_sender;
4696 AWS_ZERO_STRUCT(body_sender);
4697 s_slow_body_sender_init(&body_sender);
4698 struct aws_input_stream *body_stream = &body_sender.base;
4699
4700 struct aws_http_header headers[] = {
4701 {
4702 .name = aws_byte_cursor_from_c_str("Content-Length"),
4703 .value = aws_byte_cursor_from_c_str("16"),
4704 },
4705 };
4706
4707 struct aws_http_message *request = aws_http_message_new_request(allocator);
4708 ASSERT_NOT_NULL(request);
4709 ASSERT_SUCCESS(aws_http_message_set_request_method(request, aws_byte_cursor_from_c_str("PUT")));
4710 ASSERT_SUCCESS(aws_http_message_set_request_path(request, aws_byte_cursor_from_c_str("/plan.txt")));
4711 ASSERT_SUCCESS(aws_http_message_add_header_array(request, headers, AWS_ARRAY_SIZE(headers)));
4712 aws_http_message_set_body_stream(request, body_stream);
4713
4714 struct client_stream_tester stream_tester;
4715 ASSERT_SUCCESS(s_stream_tester_init(&stream_tester, &tester, request));
4716
4717 /* send head of request */
4718 testing_channel_run_currently_queued_tasks(&tester.testing_channel);
4719
4720 /* Ensure the request can be destroyed after request is sent */
4721 aws_http_message_destroy(request);
4722 aws_input_stream_release(body_stream);
4723
4724 /* Something absurd */
4725 aws_http_stream_cancel(stream_tester.stream, AWS_ERROR_COND_VARIABLE_ERROR_UNKNOWN);
4726 /* The second call will take not action */
4727 aws_http_stream_cancel(stream_tester.stream, AWS_ERROR_SUCCESS);
4728 /* Wait for channel to finish shutdown */
4729 testing_channel_drain_queued_tasks(&tester.testing_channel);
4730 /* check result, should not receive any body */
4731 const char *expected = "PUT /plan.txt HTTP/1.1\r\n"
4732 "Content-Length: 16\r\n"
4733 "\r\n";
4734 ASSERT_SUCCESS(testing_channel_check_written_messages_str(&tester.testing_channel, allocator, expected));
4735
4736 ASSERT_TRUE(stream_tester.complete);
4737 ASSERT_INT_EQUALS(AWS_ERROR_COND_VARIABLE_ERROR_UNKNOWN, stream_tester.on_complete_error_code);
4738
4739 /* clean up */
4740 client_stream_tester_clean_up(&stream_tester);
4741 ASSERT_SUCCESS(s_tester_clean_up(&tester));
4742 return AWS_OP_SUCCESS;
4743 }
4744
4745 /* When response has `connection: close` any further request body should not be sent. */
4746 H1_CLIENT_TEST_CASE(h1_client_response_close_connection_before_request_finishes) {
4747 (void)ctx;
4748 struct tester tester;
4749 ASSERT_SUCCESS(s_tester_init(&tester, allocator));
4750 /* Okay to set a timeout */
4751 size_t connection_response_first_byte_timeout_ms = 200;
4752 tester.connection->client_data->response_first_byte_timeout_ms = connection_response_first_byte_timeout_ms;
4753
4754 /* set up request whose body won't send immediately */
4755 struct slow_body_sender body_sender;
4756 AWS_ZERO_STRUCT(body_sender);
4757 s_slow_body_sender_init(&body_sender);
4758 struct aws_input_stream *body_stream = &body_sender.base;
4759
4760 struct aws_http_header headers[] = {
4761 {
4762 .name = aws_byte_cursor_from_c_str("Content-Length"),
4763 .value = aws_byte_cursor_from_c_str("16"),
4764 },
4765 };
4766
4767 struct aws_http_message *request = aws_http_message_new_request(allocator);
4768 ASSERT_NOT_NULL(request);
4769 ASSERT_SUCCESS(aws_http_message_set_request_method(request, aws_byte_cursor_from_c_str("PUT")));
4770 ASSERT_SUCCESS(aws_http_message_set_request_path(request, aws_byte_cursor_from_c_str("/plan.txt")));
4771 ASSERT_SUCCESS(aws_http_message_add_header_array(request, headers, AWS_ARRAY_SIZE(headers)));
4772 aws_http_message_set_body_stream(request, body_stream);
4773
4774 struct client_stream_tester stream_tester;
4775 ASSERT_SUCCESS(s_stream_tester_init(&stream_tester, &tester, request));
4776
4777 /* send head of request */
4778 testing_channel_run_currently_queued_tasks(&tester.testing_channel);
4779
4780 /* Ensure the request can be destroyed after request is sent */
4781 aws_http_message_release(request);
4782 aws_input_stream_release(body_stream);
4783
4784 /* send close connection response */
4785 ASSERT_SUCCESS(testing_channel_push_read_str(
4786 &tester.testing_channel,
4787 "HTTP/1.1 404 Not Found\r\n"
4788 "Date: Fri, 01 Mar 2019 17:18:55 GMT\r\n"
4789 "Connection: close\r\n"
4790 "\r\n"));
4791
4792 testing_channel_drain_queued_tasks(&tester.testing_channel);
4793 /* check result, should not receive any body */
4794 const char *expected = "PUT /plan.txt HTTP/1.1\r\n"
4795 "Content-Length: 16\r\n"
4796 "\r\n";
4797 ASSERT_SUCCESS(testing_channel_check_written_messages_str(&tester.testing_channel, allocator, expected));
4798 /* Check if the testing channel has shut down. */
4799 ASSERT_TRUE(testing_channel_is_shutdown_completed(&tester.testing_channel));
4800
4801 ASSERT_TRUE(stream_tester.complete);
4802 ASSERT_INT_EQUALS(AWS_ERROR_SUCCESS, stream_tester.on_complete_error_code);
4803
4804 /* clean up */
4805 client_stream_tester_clean_up(&stream_tester);
4806 ASSERT_SUCCESS(s_tester_clean_up(&tester));
4807 return AWS_OP_SUCCESS;
4808 }
4809
4810 H1_CLIENT_TEST_CASE(h1_client_response_first_byte_timeout_connection) {
4811 (void)ctx;
4812 struct tester tester;
4813 ASSERT_SUCCESS(s_tester_init(&tester, allocator));
4814 /* with test channel, we don't use bootstrap to propagate the settings. Hack around it by set the setting directly
4815 */
4816 size_t connection_response_first_byte_timeout_ms = 200;
4817 tester.connection->client_data->response_first_byte_timeout_ms = connection_response_first_byte_timeout_ms;
4818
4819 /* send request */
4820 struct aws_http_header headers[] = {
4821 {
4822 .name = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("Host"),
4823 .value = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("example.com"),
4824 },
4825 {
4826 .name = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("Accept"),
4827 .value = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("*/*"),
4828 },
4829 };
4830
4831 struct aws_http_message *request = s_new_default_get_request(allocator);
4832 ASSERT_NOT_NULL(request);
4833 ASSERT_SUCCESS(aws_http_message_add_header_array(request, headers, AWS_ARRAY_SIZE(headers)));
4834 struct client_stream_tester stream_tester;
4835 ASSERT_SUCCESS(s_stream_tester_init(&stream_tester, &tester, request));
4836
4837 testing_channel_drain_queued_tasks(&tester.testing_channel);
4838
4839 /* Sleep to trigger the timeout */
4840 aws_thread_current_sleep(aws_timestamp_convert(
4841 connection_response_first_byte_timeout_ms + 1, AWS_TIMESTAMP_MILLIS, AWS_TIMESTAMP_NANOS, NULL));
4842
4843 testing_channel_drain_queued_tasks(&tester.testing_channel);
4844 /* Check if the testing channel has shut down. */
4845 ASSERT_TRUE(testing_channel_is_shutdown_completed(&tester.testing_channel));
4846 ASSERT_TRUE(testing_channel_is_shutdown_completed(&tester.testing_channel));
4847
4848 ASSERT_TRUE(stream_tester.complete);
4849 ASSERT_INT_EQUALS(AWS_ERROR_HTTP_RESPONSE_FIRST_BYTE_TIMEOUT, stream_tester.on_complete_error_code);
4850
4851 /* clean up */
4852 aws_http_message_release(request);
4853 client_stream_tester_clean_up(&stream_tester);
4854
4855 ASSERT_SUCCESS(s_tester_clean_up(&tester));
4856 return AWS_OP_SUCCESS;
4857 }
4858
4859 H1_CLIENT_TEST_CASE(h1_client_response_first_byte_timeout_request_override) {
4860 (void)ctx;
4861 struct tester tester;
4862 ASSERT_SUCCESS(s_tester_init(&tester, allocator));
4863 /* with test channel, we don't use bootstrap to propagate the settings. Hack around it by set the setting directly
4864 */
4865 size_t connection_response_first_byte_timeout_ms = 1000;
4866 tester.connection->client_data->response_first_byte_timeout_ms = connection_response_first_byte_timeout_ms;
4867
4868 /* send request */
4869 struct aws_http_header headers[] = {
4870 {
4871 .name = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("Host"),
4872 .value = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("example.com"),
4873 },
4874 {
4875 .name = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("Accept"),
4876 .value = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("*/*"),
4877 },
4878 };
4879
4880 struct aws_http_message *request = s_new_default_get_request(allocator);
4881 ASSERT_NOT_NULL(request);
4882 ASSERT_SUCCESS(aws_http_message_add_header_array(request, headers, AWS_ARRAY_SIZE(headers)));
4883
4884 size_t response_first_byte_timeout_ms = 100;
4885
4886 int completion_error_code = 0;
4887 struct aws_http_make_request_options opt = {
4888 .self_size = sizeof(opt),
4889 .request = request,
4890 .response_first_byte_timeout_ms = response_first_byte_timeout_ms,
4891 .on_complete = s_on_complete,
4892 .user_data = &completion_error_code,
4893 };
4894 struct aws_http_stream *stream = aws_http_connection_make_request(tester.connection, &opt);
4895 ASSERT_NOT_NULL(stream);
4896 ASSERT_SUCCESS(aws_http_stream_activate(stream));
4897
4898 testing_channel_drain_queued_tasks(&tester.testing_channel);
4899
4900 aws_thread_current_sleep(
4901 aws_timestamp_convert(response_first_byte_timeout_ms + 1, AWS_TIMESTAMP_MILLIS, AWS_TIMESTAMP_NANOS, NULL));
4902
4903 testing_channel_drain_queued_tasks(&tester.testing_channel);
4904 /* Check if the testing channel has shut down. */
4905 ASSERT_TRUE(testing_channel_is_shutdown_completed(&tester.testing_channel));
4906
4907 ASSERT_INT_EQUALS(AWS_ERROR_HTTP_RESPONSE_FIRST_BYTE_TIMEOUT, completion_error_code);
4908
4909 /* clean up */
4910 aws_http_message_release(request);
4911 aws_http_stream_release(stream);
4912
4913 ASSERT_SUCCESS(s_tester_clean_up(&tester));
4914 return AWS_OP_SUCCESS;
4915 }
4916
4917 /**
4918 * Once upon a time, when the connection received data from the channel, we buffer the data in the connection level,
4919 and
4920 * then send it to the stream.
4921 * When stream has no window left to receive the data from connection, the data will be kept in the buffer.
4922 * If the connection starts to shutdown before stream opens its window, the buffered data will be throw away because
4923 of
4924 * shutdown process.
4925 * But, the connection actually received the full response, which is an unexpected behavior for the
4926 * stream to report connection close with error.
4927 */
4928 H1_CLIENT_TEST_CASE(h1_client_connection_close_before_request_finishes_with_buffer) {
4929 (void)ctx;
4930 struct tester_options tester_opts = {
4931 .manual_window_management = true,
4932 .initial_stream_window_size = 5,
4933 .read_buffer_capacity = SIZE_MAX,
4934 };
4935 struct tester tester;
4936 ASSERT_SUCCESS(s_tester_init_ex(&tester, allocator, &tester_opts));
4937
4938 struct aws_http_message *request = aws_http_message_new_request(allocator);
4939 ASSERT_NOT_NULL(request);
4940 ASSERT_SUCCESS(aws_http_message_set_request_method(request, aws_byte_cursor_from_c_str("GET")));
4941 ASSERT_SUCCESS(aws_http_message_set_request_path(request, aws_byte_cursor_from_c_str("/plan.txt")));
4942
4943 struct client_stream_tester stream_tester;
4944 ASSERT_SUCCESS(s_stream_tester_init(&stream_tester, &tester, request));
4945
4946 /* send head of request */
4947 testing_channel_run_currently_queued_tasks(&tester.testing_channel);
4948
4949 /* Ensure the request can be destroyed after request is sent */
4950 aws_http_message_destroy(request);
4951
4952 /* send close connection response */
4953 ASSERT_SUCCESS(testing_channel_push_read_str(
4954 &tester.testing_channel,
4955 "HTTP/1.1 200 OK\r\n"
4956 "Content-Length: 9\r\n"
4957 "\r\n"
4958 "Call Momo"));
4959
4960 /* All the response data has been processed, buffered in the connection level. */
4961 testing_channel_run_currently_queued_tasks(&tester.testing_channel);
4962
4963 /* Some handler starts the shutdown process, while the stream still has 0 window left. (eg: socket reads EOF, TLS
4964 * reads graceful shutdown) */
4965 aws_channel_shutdown(tester.testing_channel.channel, AWS_ERROR_UNKNOWN);
4966 testing_channel_drain_queued_tasks(&tester.testing_channel);
4967 /* We should not complete the stream, since the window for the stream is still 0. */
4968 ASSERT_FALSE(stream_tester.complete);
4969
4970 /* Updated the window after shutdown happens */
4971 aws_http_stream_update_window(stream_tester.stream, 5);
4972 /* Wait for channel to finish shutdown */
4973 testing_channel_drain_queued_tasks(&tester.testing_channel);
4974
4975 /* check result */
4976 ASSERT_TRUE(stream_tester.complete);
4977 ASSERT_INT_EQUALS(AWS_ERROR_SUCCESS, stream_tester.on_complete_error_code);
4978 ASSERT_INT_EQUALS(200, stream_tester.response_status);
4979 ASSERT_UINT_EQUALS(1, aws_http_headers_count(stream_tester.response_headers));
4980 ASSERT_SUCCESS(s_check_header(stream_tester.response_headers, 0, "Content-Length", "9"));
4981 ASSERT_TRUE(aws_byte_buf_eq_c_str(&stream_tester.response_body, "Call Momo"));
4982
4983 /* clean up */
4984 client_stream_tester_clean_up(&stream_tester);
4985 ASSERT_SUCCESS(s_tester_clean_up(&tester));
4986 return AWS_OP_SUCCESS;
4987 }
4988
4989 H1_CLIENT_TEST_CASE(h1_client_connection_close_before_request_finishes_with_buffer_incomplete_response) {
4990 (void)ctx;
4991 struct tester_options tester_opts = {
4992 .manual_window_management = true,
4993 .initial_stream_window_size = 5,
4994 .read_buffer_capacity = SIZE_MAX,
4995 };
4996 struct tester tester;
4997 ASSERT_SUCCESS(s_tester_init_ex(&tester, allocator, &tester_opts));
4998
4999 struct aws_http_message *request = aws_http_message_new_request(allocator);
5000 ASSERT_NOT_NULL(request);
5001 ASSERT_SUCCESS(aws_http_message_set_request_method(request, aws_byte_cursor_from_c_str("GET")));
5002 ASSERT_SUCCESS(aws_http_message_set_request_path(request, aws_byte_cursor_from_c_str("/plan.txt")));
5003
5004 struct client_stream_tester stream_tester;
5005 ASSERT_SUCCESS(s_stream_tester_init(&stream_tester, &tester, request));
5006
5007 /* send head of request */
5008 testing_channel_run_currently_queued_tasks(&tester.testing_channel);
5009
5010 /* Ensure the request can be destroyed after request is sent */
5011 aws_http_message_destroy(request);
5012
5013 /* Send the incomplete response. */
5014 ASSERT_SUCCESS(testing_channel_push_read_str(
5015 &tester.testing_channel,
5016 "HTTP/1.1 200 OK\r\n"
5017 "Content-Length: 9\r\n"
5018 "\r\n"
5019 "Call Mo"));
5020
5021 /* All the response data has been processed, buffered in the connection level. */
5022 testing_channel_run_currently_queued_tasks(&tester.testing_channel);
5023
5024 /* Some handler starts the shutdown process, while the stream still has 0 window left. (eg: socket reads EOF, TLS
5025 * reads graceful shutdown) */
5026 aws_channel_shutdown(tester.testing_channel.channel, AWS_ERROR_UNIMPLEMENTED);
5027 testing_channel_drain_queued_tasks(&tester.testing_channel);
5028 /* We should not complete the stream, since the window for the stream is still 0. */
5029 ASSERT_FALSE(stream_tester.complete);
5030
5031 /* Updated the window after shutdown happens */
5032 aws_http_stream_update_window(stream_tester.stream, 5);
5033 /* Wait for channel to finish shutdown */
5034 testing_channel_drain_queued_tasks(&tester.testing_channel);
5035
5036 /* check result */
5037 ASSERT_TRUE(stream_tester.complete);
5038 /* Fail */
5039 ASSERT_INT_EQUALS(AWS_ERROR_UNIMPLEMENTED, stream_tester.on_complete_error_code);
5040 ASSERT_INT_EQUALS(200, stream_tester.response_status);
5041 ASSERT_UINT_EQUALS(1, aws_http_headers_count(stream_tester.response_headers));
5042 ASSERT_SUCCESS(s_check_header(stream_tester.response_headers, 0, "Content-Length", "9"));
5043 /* Incomplete response received */
5044 ASSERT_TRUE(aws_byte_buf_eq_c_str(&stream_tester.response_body, "Call Mo"));
5045
5046 /* clean up */
5047 client_stream_tester_clean_up(&stream_tester);
5048 ASSERT_SUCCESS(s_tester_clean_up(&tester));
5049 return AWS_OP_SUCCESS;
5050 }
5051
5052 static int s_h1_client_connection_close_before_request_finishes_with_buffer_force_shutdown_helper(
5053 struct aws_allocator *allocator,
5054 bool connection_close) {
5055 struct tester_options tester_opts = {
5056 .manual_window_management = true,
5057 .initial_stream_window_size = 5,
5058 .read_buffer_capacity = SIZE_MAX,
5059 };
5060 struct tester tester;
5061 ASSERT_SUCCESS(s_tester_init_ex(&tester, allocator, &tester_opts));
5062
5063 struct aws_http_message *request = aws_http_message_new_request(allocator);
5064 ASSERT_NOT_NULL(request);
5065 ASSERT_SUCCESS(aws_http_message_set_request_method(request, aws_byte_cursor_from_c_str("GET")));
5066 ASSERT_SUCCESS(aws_http_message_set_request_path(request, aws_byte_cursor_from_c_str("/plan.txt")));
5067
5068 struct client_stream_tester stream_tester;
5069 ASSERT_SUCCESS(s_stream_tester_init(&stream_tester, &tester, request));
5070
5071 /* send head of request */
5072 testing_channel_run_currently_queued_tasks(&tester.testing_channel);
5073
5074 /* Ensure the request can be destroyed after request is sent */
5075 aws_http_message_destroy(request);
5076
5077 /* Send the incomplete response. */
5078 ASSERT_SUCCESS(testing_channel_push_read_str(
5079 &tester.testing_channel,
5080 "HTTP/1.1 200 OK\r\n"
5081 "Content-Length: 9\r\n"
5082 "\r\n"
5083 "Call Mo"));
5084
5085 /* All the response data has been processed, buffered in the connection level. */
5086 testing_channel_run_currently_queued_tasks(&tester.testing_channel);
5087
5088 /* Some handler starts the shutdown process, while the stream still has 0 window left. (eg: socket reads EOF, TLS
5089 * reads graceful shutdown) */
5090 aws_channel_shutdown(tester.testing_channel.channel, AWS_ERROR_UNIMPLEMENTED);
5091 testing_channel_drain_queued_tasks(&tester.testing_channel);
5092 /* We should not complete the stream, since the window for the stream is still 0. */
5093 ASSERT_FALSE(stream_tester.complete);
5094
5095 /* Don't update the window, just cancel the stream should also complete the stream. */
5096 if (connection_close) {
5097 aws_http_connection_close(tester.connection);
5098 } else {
5099 /* Stream cancel error will be override as the connection shutdown happens before cancel. */
5100 aws_http_stream_cancel(stream_tester.stream, AWS_ERROR_UNKNOWN);
5101 }
5102 /* Wait for channel to finish shutdown */
5103 testing_channel_drain_queued_tasks(&tester.testing_channel);
5104
5105 /* check result */
5106 ASSERT_TRUE(stream_tester.complete);
5107 /* Fail */
5108 ASSERT_INT_EQUALS(AWS_ERROR_UNIMPLEMENTED, stream_tester.on_complete_error_code);
5109 ASSERT_INT_EQUALS(200, stream_tester.response_status);
5110 ASSERT_UINT_EQUALS(1, aws_http_headers_count(stream_tester.response_headers));
5111 ASSERT_SUCCESS(s_check_header(stream_tester.response_headers, 0, "Content-Length", "9"));
5112 /* Incomplete response received */
5113 ASSERT_TRUE(aws_byte_buf_eq_c_str(&stream_tester.response_body, "Call "));
5114
5115 /* clean up */
5116 client_stream_tester_clean_up(&stream_tester);
5117 ASSERT_SUCCESS(s_tester_clean_up(&tester));
5118 return AWS_OP_SUCCESS;
5119 }
5120
5121 H1_CLIENT_TEST_CASE(h1_client_connection_close_before_request_finishes_with_buffer_force_shutdown) {
5122 (void)ctx;
5123 return s_h1_client_connection_close_before_request_finishes_with_buffer_force_shutdown_helper(allocator, true);
5124 }
5125
5126 H1_CLIENT_TEST_CASE(h1_client_connection_close_before_request_finishes_with_buffer_stream_cancel) {
5127 (void)ctx;
5128 return s_h1_client_connection_close_before_request_finishes_with_buffer_force_shutdown_helper(allocator, false);
5129 }
5130
5131 /* Test helper for write_data callback tracking */
5132 struct write_data_callback_tester {
5133 int num_callbacks;
5134 int last_error_code;
5135 };
5136
5137 static void s_on_write_data_complete(struct aws_http_stream *stream, int error_code, void *user_data) {
5138 (void)stream;
5139 struct write_data_callback_tester *callback_tester = user_data;
5140 callback_tester->num_callbacks++;
5141 callback_tester->last_error_code = error_code;
5142 }
5143
5144 struct write_data_test_fixture {
5145 struct tester tester;
5146 struct client_stream_tester stream_tester;
5147 };
5148
5149 static int s_write_data_test_setup(
5150 struct write_data_test_fixture *fixture,
5151 struct aws_allocator *allocator,
5152 const struct aws_http_header *headers,
5153 size_t num_headers,
5154 bool use_manual_data_writes) {
5155
5156 ASSERT_SUCCESS(s_tester_init(&fixture->tester, allocator));
5157
5158 struct aws_http_message *request = aws_http_message_new_request(allocator);
5159 ASSERT_NOT_NULL(request);
5160 ASSERT_SUCCESS(aws_http_message_set_request_method(request, aws_byte_cursor_from_c_str("POST")));
5161 ASSERT_SUCCESS(aws_http_message_set_request_path(request, aws_byte_cursor_from_c_str("/upload")));
5162 ASSERT_SUCCESS(aws_http_message_add_header_array(request, headers, num_headers));
5163
5164 ASSERT_SUCCESS(client_stream_tester_init(
5165 &fixture->stream_tester,
5166 allocator,
5167 &(struct client_stream_tester_options){
5168 .request = request,
5169 .connection = fixture->tester.connection,
5170 .use_manual_data_writes = use_manual_data_writes,
5171 }));
5172
5173 testing_channel_drain_queued_tasks(&fixture->tester.testing_channel);
5174 aws_http_message_destroy(request);
5175 return AWS_OP_SUCCESS;
5176 }
5177
5178 static int s_write_data_test_teardown(struct write_data_test_fixture *fixture) {
5179 client_stream_tester_clean_up(&fixture->stream_tester);
5180 ASSERT_SUCCESS(s_tester_clean_up(&fixture->tester));
5181 return AWS_OP_SUCCESS;
5182 }
5183
5184 /* Test: Single chunk write with Content-Length */
5185 H1_CLIENT_TEST_CASE(h1_client_write_data_single_chunk) {
5186 (void)ctx;
5187 struct write_data_test_fixture fixture;
5188 struct aws_http_header headers[] = {
5189 {.name = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("Content-Length"),
5190 .value = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("5")},
5191 };
5192 ASSERT_SUCCESS(s_write_data_test_setup(&fixture, allocator, headers, AWS_ARRAY_SIZE(headers), true));
5193
5194 struct aws_byte_cursor data = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("hello");
5195 struct aws_input_stream *input_stream = aws_input_stream_new_from_cursor(allocator, &data);
5196 ASSERT_NOT_NULL(input_stream);
5197
5198 struct write_data_callback_tester callback_tester = {0};
5199 struct aws_http_stream_write_data_options write_options = {
5200 .data = input_stream,
5201 .end_stream = true,
5202 .on_complete = s_on_write_data_complete,
5203 .user_data = &callback_tester,
5204 };
5205
5206 ASSERT_SUCCESS(aws_http_stream_write_data(fixture.stream_tester.stream, &write_options));
5207 testing_channel_drain_queued_tasks(&fixture.tester.testing_channel);
5208
5209 ASSERT_SUCCESS(testing_channel_check_written_messages_str(
5210 &fixture.tester.testing_channel, allocator, "POST /upload HTTP/1.1\r\nContent-Length: 5\r\n\r\nhello"));
5211
5212 ASSERT_INT_EQUALS(1, callback_tester.num_callbacks);
5213 ASSERT_INT_EQUALS(AWS_ERROR_SUCCESS, callback_tester.last_error_code);
5214
5215 ASSERT_SUCCESS(testing_channel_push_read_str(&fixture.tester.testing_channel, "HTTP/1.1 200 OK\r\n\r\n"));
5216 testing_channel_drain_queued_tasks(&fixture.tester.testing_channel);
5217
5218 ASSERT_TRUE(fixture.stream_tester.complete);
5219 ASSERT_INT_EQUALS(200, fixture.stream_tester.response_status);
5220
5221 aws_input_stream_release(input_stream);
5222 ASSERT_SUCCESS(s_write_data_test_teardown(&fixture));
5223 return AWS_OP_SUCCESS;
5224 }
5225
5226 /* Test: Multiple chunk writes with Content-Length */
5227 H1_CLIENT_TEST_CASE(h1_client_write_data_multiple_chunks) {
5228 (void)ctx;
5229 struct write_data_test_fixture fixture;
5230 struct aws_http_header headers[] = {
5231 {.name = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("Content-Length"),
5232 .value = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("10")},
5233 };
5234 ASSERT_SUCCESS(s_write_data_test_setup(&fixture, allocator, headers, AWS_ARRAY_SIZE(headers), true));
5235
5236 /* Write first chunk */
5237 struct aws_byte_cursor data1 = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("hello");
5238 struct aws_input_stream *input_stream1 = aws_input_stream_new_from_cursor(allocator, &data1);
5239 ASSERT_NOT_NULL(input_stream1);
5240
5241 struct write_data_callback_tester callback_tester1 = {0};
5242 struct aws_http_stream_write_data_options write_options1 = {
5243 .data = input_stream1,
5244 .end_stream = false,
5245 .on_complete = s_on_write_data_complete,
5246 .user_data = &callback_tester1,
5247 };
5248 ASSERT_SUCCESS(aws_http_stream_write_data(fixture.stream_tester.stream, &write_options1));
5249 testing_channel_drain_queued_tasks(&fixture.tester.testing_channel);
5250
5251 /* Write second chunk */
5252 struct aws_byte_cursor data2 = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("world");
5253 struct aws_input_stream *input_stream2 = aws_input_stream_new_from_cursor(allocator, &data2);
5254 ASSERT_NOT_NULL(input_stream2);
5255
5256 struct write_data_callback_tester callback_tester2 = {0};
5257 struct aws_http_stream_write_data_options write_options2 = {
5258 .data = input_stream2,
5259 .end_stream = true,
5260 .on_complete = s_on_write_data_complete,
5261 .user_data = &callback_tester2,
5262 };
5263 ASSERT_SUCCESS(aws_http_stream_write_data(fixture.stream_tester.stream, &write_options2));
5264 testing_channel_drain_queued_tasks(&fixture.tester.testing_channel);
5265
5266 ASSERT_SUCCESS(testing_channel_check_written_messages_str(
5267 &fixture.tester.testing_channel, allocator, "POST /upload HTTP/1.1\r\nContent-Length: 10\r\n\r\nhelloworld"));
5268
5269 ASSERT_INT_EQUALS(1, callback_tester1.num_callbacks);
5270 ASSERT_INT_EQUALS(AWS_ERROR_SUCCESS, callback_tester1.last_error_code);
5271 ASSERT_INT_EQUALS(1, callback_tester2.num_callbacks);
5272 ASSERT_INT_EQUALS(AWS_ERROR_SUCCESS, callback_tester2.last_error_code);
5273
5274 ASSERT_SUCCESS(testing_channel_push_read_str(&fixture.tester.testing_channel, "HTTP/1.1 200 OK\r\n\r\n"));
5275 testing_channel_drain_queued_tasks(&fixture.tester.testing_channel);
5276
5277 ASSERT_TRUE(fixture.stream_tester.complete);
5278 ASSERT_INT_EQUALS(200, fixture.stream_tester.response_status);
5279
5280 aws_input_stream_release(input_stream1);
5281 aws_input_stream_release(input_stream2);
5282 ASSERT_SUCCESS(s_write_data_test_teardown(&fixture));
5283 return AWS_OP_SUCCESS;
5284 }
5285
5286 /* Test: Validation error - manual writes not enabled */
5287 H1_CLIENT_TEST_CASE(h1_client_write_data_not_enabled) {
5288 (void)ctx;
5289 struct write_data_test_fixture fixture;
5290 ASSERT_SUCCESS(s_write_data_test_setup(&fixture, allocator, NULL, 0, false));
5291
5292 struct aws_byte_cursor data = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("hello");
5293 struct aws_input_stream *input_stream = aws_input_stream_new_from_cursor(allocator, &data);
5294 ASSERT_NOT_NULL(input_stream);
5295 struct aws_http_stream_write_data_options write_options = {
5296 .data = input_stream,
5297 .end_stream = true,
5298 };
5299
5300 ASSERT_FAILS(aws_http_stream_write_data(fixture.stream_tester.stream, &write_options));
5301 ASSERT_INT_EQUALS(AWS_ERROR_HTTP_MANUAL_WRITE_NOT_ENABLED, aws_last_error());
5302
5303 aws_input_stream_release(input_stream);
5304 ASSERT_SUCCESS(s_write_data_test_teardown(&fixture));
5305 return AWS_OP_SUCCESS;
5306 }
5307
5308 /* Test: Validation error - write after final write */
5309 H1_CLIENT_TEST_CASE(h1_client_write_data_after_final) {
5310 (void)ctx;
5311 struct write_data_test_fixture fixture;
5312 struct aws_http_header headers[] = {
5313 {.name = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("Content-Length"),
5314 .value = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("10")},
5315 };
5316 ASSERT_SUCCESS(s_write_data_test_setup(&fixture, allocator, headers, AWS_ARRAY_SIZE(headers), true));
5317
5318 struct aws_byte_cursor data1 = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("hello");
5319 struct aws_input_stream *input_stream1 = aws_input_stream_new_from_cursor(allocator, &data1);
5320 ASSERT_NOT_NULL(input_stream1);
5321 struct aws_http_stream_write_data_options write_options1 = {
5322 .data = input_stream1,
5323 .end_stream = true,
5324 };
5325 ASSERT_SUCCESS(aws_http_stream_write_data(fixture.stream_tester.stream, &write_options1));
5326
5327 struct aws_byte_cursor data2 = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("world");
5328 struct aws_input_stream *input_stream2 = aws_input_stream_new_from_cursor(allocator, &data2);
5329 ASSERT_NOT_NULL(input_stream2);
5330 struct aws_http_stream_write_data_options write_options2 = {
5331 .data = input_stream2,
5332 .end_stream = false,
5333 };
5334 ASSERT_FAILS(aws_http_stream_write_data(fixture.stream_tester.stream, &write_options2));
5335 ASSERT_INT_EQUALS(AWS_ERROR_HTTP_MANUAL_WRITE_HAS_COMPLETED, aws_last_error());
5336
5337 aws_input_stream_release(input_stream1);
5338 aws_input_stream_release(input_stream2);
5339 ASSERT_SUCCESS(s_write_data_test_teardown(&fixture));
5340 return AWS_OP_SUCCESS;
5341 }
5342
5343 /* Test: Content-Length mismatch - too much data */
5344 H1_CLIENT_TEST_CASE(h1_client_write_data_exceeds_content_length) {
5345 (void)ctx;
5346 struct write_data_test_fixture fixture;
5347 struct aws_http_header headers[] = {
5348 {.name = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("Content-Length"),
5349 .value = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("3")},
5350 };
5351 ASSERT_SUCCESS(s_write_data_test_setup(&fixture, allocator, headers, AWS_ARRAY_SIZE(headers), true));
5352
5353 struct aws_byte_cursor data = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("hello");
5354 struct aws_input_stream *input_stream = aws_input_stream_new_from_cursor(allocator, &data);
5355 ASSERT_NOT_NULL(input_stream);
5356
5357 struct write_data_callback_tester callback_tester = {0};
5358 struct aws_http_stream_write_data_options write_options = {
5359 .data = input_stream,
5360 .end_stream = true,
5361 .on_complete = s_on_write_data_complete,
5362 .user_data = &callback_tester,
5363 };
5364
5365 ASSERT_SUCCESS(aws_http_stream_write_data(fixture.stream_tester.stream, &write_options));
5366 testing_channel_drain_queued_tasks(&fixture.tester.testing_channel);
5367
5368 ASSERT_INT_EQUALS(1, callback_tester.num_callbacks);
5369 ASSERT_INT_EQUALS(AWS_ERROR_HTTP_OUTGOING_STREAM_LENGTH_INCORRECT, callback_tester.last_error_code);
5370
5371 aws_input_stream_release(input_stream);
5372 ASSERT_SUCCESS(s_write_data_test_teardown(&fixture));
5373 return AWS_OP_SUCCESS;
5374 }
5375
5376 /* Test: Content-Length mismatch - too little data */
5377 H1_CLIENT_TEST_CASE(h1_client_write_data_less_than_content_length) {
5378 (void)ctx;
5379 struct write_data_test_fixture fixture;
5380 struct aws_http_header headers[] = {
5381 {.name = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("Content-Length"),
5382 .value = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("10")},
5383 };
5384 ASSERT_SUCCESS(s_write_data_test_setup(&fixture, allocator, headers, AWS_ARRAY_SIZE(headers), true));
5385
5386 struct aws_byte_cursor data = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("hello");
5387 struct aws_input_stream *input_stream = aws_input_stream_new_from_cursor(allocator, &data);
5388 ASSERT_NOT_NULL(input_stream);
5389
5390 struct aws_http_stream_write_data_options write_options = {
5391 .data = input_stream,
5392 .end_stream = true,
5393 };
5394
5395 ASSERT_SUCCESS(aws_http_stream_write_data(fixture.stream_tester.stream, &write_options));
5396 testing_channel_drain_queued_tasks(&fixture.tester.testing_channel);
5397
5398 ASSERT_TRUE(fixture.stream_tester.complete);
5399 ASSERT_INT_EQUALS(AWS_ERROR_HTTP_OUTGOING_STREAM_LENGTH_INCORRECT, fixture.stream_tester.on_complete_error_code);
5400
5401 aws_input_stream_release(input_stream);
5402 ASSERT_SUCCESS(s_write_data_test_teardown(&fixture));
5403 return AWS_OP_SUCCESS;
5404 }
5405
5406 /* Test write_data with chunked encoding - single write with end_stream=true
5407 * should automatically send the termination chunk (0\r\n\r\n) */
5408 H1_CLIENT_TEST_CASE(h1_client_write_data_chunked_single) {
5409 (void)ctx;
5410 struct write_data_test_fixture fixture;
5411 struct aws_http_header headers[] = {
5412 {.name = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("Transfer-Encoding"),
5413 .value = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("chunked")},
5414 };
5415 ASSERT_SUCCESS(s_write_data_test_setup(&fixture, allocator, headers, AWS_ARRAY_SIZE(headers), true));
5416
5417 struct aws_byte_cursor data = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("write more tests");
5418 struct aws_input_stream *input_stream = aws_input_stream_new_from_cursor(allocator, &data);
5419
5420 struct write_data_callback_tester callback_tester = {0};
5421 struct aws_http_stream_write_data_options write_options = {
5422 .data = input_stream,
5423 .end_stream = true,
5424 .on_complete = s_on_write_data_complete,
5425 .user_data = &callback_tester,
5426 };
5427 ASSERT_SUCCESS(aws_http_stream_write_data(fixture.stream_tester.stream, &write_options));
5428
5429 testing_channel_drain_queued_tasks(&fixture.tester.testing_channel);
5430
5431 const char *expected = "POST /upload HTTP/1.1\r\n"
5432 "Transfer-Encoding: chunked\r\n"
5433 "\r\n"
5434 "10\r\n"
5435 "write more tests"
5436 "\r\n"
5437 "0\r\n"
5438 "\r\n";
5439 ASSERT_SUCCESS(testing_channel_check_written_messages_str(&fixture.tester.testing_channel, allocator, expected));
5440
5441 ASSERT_INT_EQUALS(1, callback_tester.num_callbacks);
5442 ASSERT_INT_EQUALS(AWS_ERROR_SUCCESS, callback_tester.last_error_code);
5443
5444 ASSERT_SUCCESS(testing_channel_push_read_str(&fixture.tester.testing_channel, "HTTP/1.1 200 OK\r\n\r\n"));
5445 testing_channel_drain_queued_tasks(&fixture.tester.testing_channel);
5446
5447 ASSERT_TRUE(fixture.stream_tester.complete);
5448 ASSERT_INT_EQUALS(200, fixture.stream_tester.response_status);
5449
5450 aws_input_stream_release(input_stream);
5451 ASSERT_SUCCESS(s_write_data_test_teardown(&fixture));
5452 return AWS_OP_SUCCESS;
5453 }
5454
5455 /* Test write_data with chunked encoding - multiple writes */
5456 H1_CLIENT_TEST_CASE(h1_client_write_data_chunked_multiple) {
5457 (void)ctx;
5458 struct write_data_test_fixture fixture;
5459 struct aws_http_header headers[] = {
5460 {.name = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("Transfer-Encoding"),
5461 .value = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("chunked")},
5462 };
5463 ASSERT_SUCCESS(s_write_data_test_setup(&fixture, allocator, headers, AWS_ARRAY_SIZE(headers), true));
5464
5465 /* First write */
5466 struct aws_byte_cursor data1 = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("write ");
5467 struct aws_input_stream *stream1 = aws_input_stream_new_from_cursor(allocator, &data1);
5468 struct write_data_callback_tester callback_tester1 = {0};
5469 struct aws_http_stream_write_data_options opts1 = {
5470 .data = stream1,
5471 .end_stream = false,
5472 .on_complete = s_on_write_data_complete,
5473 .user_data = &callback_tester1,
5474 };
5475 ASSERT_SUCCESS(aws_http_stream_write_data(fixture.stream_tester.stream, &opts1));
5476
5477 /* No-op write: NULL data, end_stream=false. Callback should still fire. */
5478 struct write_data_callback_tester callback_tester_noop = {0};
5479 struct aws_http_stream_write_data_options opts_noop = {
5480 .data = NULL,
5481 .end_stream = false,
5482 .on_complete = s_on_write_data_complete,
5483 .user_data = &callback_tester_noop,
5484 };
5485 ASSERT_SUCCESS(aws_http_stream_write_data(fixture.stream_tester.stream, &opts_noop));
5486
5487 /* Second write with end_stream=true, termination chunk should be sent automatically */
5488 struct aws_byte_cursor data2 = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("more tests");
5489 struct aws_input_stream *stream2 = aws_input_stream_new_from_cursor(allocator, &data2);
5490 struct write_data_callback_tester callback_tester2 = {0};
5491 struct aws_http_stream_write_data_options opts2 = {
5492 .data = stream2,
5493 .end_stream = true,
5494 .on_complete = s_on_write_data_complete,
5495 .user_data = &callback_tester2,
5496 };
5497 ASSERT_SUCCESS(aws_http_stream_write_data(fixture.stream_tester.stream, &opts2));
5498
5499 testing_channel_drain_queued_tasks(&fixture.tester.testing_channel);
5500
5501 const char *expected = "POST /upload HTTP/1.1\r\n"
5502 "Transfer-Encoding: chunked\r\n"
5503 "\r\n"
5504 "6\r\n"
5505 "write "
5506 "\r\n"
5507 "A\r\n"
5508 "more tests"
5509 "\r\n"
5510 "0\r\n"
5511 "\r\n";
5512 ASSERT_SUCCESS(testing_channel_check_written_messages_str(&fixture.tester.testing_channel, allocator, expected));
5513
5514 ASSERT_INT_EQUALS(1, callback_tester1.num_callbacks);
5515 ASSERT_INT_EQUALS(AWS_ERROR_SUCCESS, callback_tester1.last_error_code);
5516 ASSERT_INT_EQUALS(1, callback_tester_noop.num_callbacks);
5517 ASSERT_INT_EQUALS(AWS_ERROR_SUCCESS, callback_tester_noop.last_error_code);
5518 ASSERT_INT_EQUALS(1, callback_tester2.num_callbacks);
5519 ASSERT_INT_EQUALS(AWS_ERROR_SUCCESS, callback_tester2.last_error_code);
5520
5521 aws_input_stream_release(stream1);
5522 aws_input_stream_release(stream2);
5523 ASSERT_SUCCESS(s_write_data_test_teardown(&fixture));
5524 return AWS_OP_SUCCESS;
5525 }
5526
5527 /* Test: write_data with NULL data and end_stream=true on Content-Length stream.
5528 * Also verifies NULL data + end_stream=false is a no-op. */
5529 H1_CLIENT_TEST_CASE(h1_client_write_data_null_data_content_length) {
5530 (void)ctx;
5531 struct write_data_test_fixture fixture;
5532 struct aws_http_header headers[] = {
5533 {.name = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("Content-Length"),
5534 .value = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("5")},
5535 };
5536 ASSERT_SUCCESS(s_write_data_test_setup(&fixture, allocator, headers, AWS_ARRAY_SIZE(headers), true));
5537
5538 /* First write with end_stream=false */
5539 struct aws_byte_cursor data = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("hello");
5540 struct aws_input_stream *stream = aws_input_stream_new_from_cursor(allocator, &data);
5541 struct write_data_callback_tester callback_tester = {0};
5542 struct aws_http_stream_write_data_options write_opts = {
5543 .data = stream,
5544 .end_stream = false,
5545 .on_complete = s_on_write_data_complete,
5546 .user_data = &callback_tester,
5547 };
5548 ASSERT_SUCCESS(aws_http_stream_write_data(fixture.stream_tester.stream, &write_opts));
5549
5550 /* Second write NULL data without end_stream should be a no-op */
5551 struct aws_http_stream_write_data_options noop_options = {
5552 .data = NULL,
5553 .end_stream = false,
5554 .on_complete = s_on_write_data_complete,
5555 .user_data = &callback_tester,
5556 };
5557 ASSERT_SUCCESS(aws_http_stream_write_data(fixture.stream_tester.stream, &noop_options));
5558
5559 /* Third write NULL data with end_stream should complete the stream */
5560 struct aws_http_stream_write_data_options write_options = {
5561 .data = NULL,
5562 .end_stream = true,
5563 .on_complete = s_on_write_data_complete,
5564 .user_data = &callback_tester,
5565 };
5566 ASSERT_SUCCESS(aws_http_stream_write_data(fixture.stream_tester.stream, &write_options));
5567
5568 testing_channel_drain_queued_tasks(&fixture.tester.testing_channel);
5569
5570 ASSERT_SUCCESS(testing_channel_check_written_messages_str(
5571 &fixture.tester.testing_channel, allocator, "POST /upload HTTP/1.1\r\nContent-Length: 5\r\n\r\nhello"));
5572
5573 ASSERT_INT_EQUALS(3, callback_tester.num_callbacks);
5574 ASSERT_INT_EQUALS(AWS_ERROR_SUCCESS, callback_tester.last_error_code);
5575
5576 ASSERT_SUCCESS(testing_channel_push_read_str(&fixture.tester.testing_channel, "HTTP/1.1 200 OK\r\n\r\n"));
5577 testing_channel_drain_queued_tasks(&fixture.tester.testing_channel);
5578
5579 ASSERT_TRUE(fixture.stream_tester.complete);
5580 ASSERT_INT_EQUALS(200, fixture.stream_tester.response_status);
5581
5582 aws_input_stream_release(stream);
5583 ASSERT_SUCCESS(s_write_data_test_teardown(&fixture));
5584 return AWS_OP_SUCCESS;
5585 }
5586
5587 /* Test: write_data with NULL data and end_stream=true on chunked stream */
5588 H1_CLIENT_TEST_CASE(h1_client_write_data_null_data_chunked) {
5589 (void)ctx;
5590 struct write_data_test_fixture fixture;
5591 struct aws_http_header headers[] = {
5592 {.name = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("Transfer-Encoding"),
5593 .value = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("chunked")},
5594 };
5595 ASSERT_SUCCESS(s_write_data_test_setup(&fixture, allocator, headers, AWS_ARRAY_SIZE(headers), true));
5596
5597 struct write_data_callback_tester callback_tester = {0};
5598 struct aws_http_stream_write_data_options write_options = {
5599 .data = NULL,
5600 .end_stream = true,
5601 .on_complete = s_on_write_data_complete,
5602 .user_data = &callback_tester,
5603 };
5604 ASSERT_SUCCESS(aws_http_stream_write_data(fixture.stream_tester.stream, &write_options));
5605
5606 testing_channel_drain_queued_tasks(&fixture.tester.testing_channel);
5607
5608 const char *expected = "POST /upload HTTP/1.1\r\n"
5609 "Transfer-Encoding: chunked\r\n"
5610 "\r\n"
5611 "0\r\n"
5612 "\r\n";
5613 ASSERT_SUCCESS(testing_channel_check_written_messages_str(&fixture.tester.testing_channel, allocator, expected));
5614
5615 ASSERT_INT_EQUALS(1, callback_tester.num_callbacks);
5616 ASSERT_INT_EQUALS(AWS_ERROR_SUCCESS, callback_tester.last_error_code);
5617
5618 ASSERT_SUCCESS(testing_channel_push_read_str(&fixture.tester.testing_channel, "HTTP/1.1 200 OK\r\n\r\n"));
5619 testing_channel_drain_queued_tasks(&fixture.tester.testing_channel);
5620
5621 ASSERT_TRUE(fixture.stream_tester.complete);
5622 ASSERT_INT_EQUALS(200, fixture.stream_tester.response_status);
5623
5624 ASSERT_SUCCESS(s_write_data_test_teardown(&fixture));
5625 return AWS_OP_SUCCESS;
5626 }
5627
5628 /* Test: auto-add Transfer-Encoding: chunked when no Content-Length or Transfer-Encoding is set */
5629 H1_CLIENT_TEST_CASE(h1_client_write_data_auto_chunked) {
5630 (void)ctx;
5631 struct write_data_test_fixture fixture;
5632 /* No Content-Length or Transfer-Encoding header */
5633 ASSERT_SUCCESS(s_write_data_test_setup(&fixture, allocator, NULL, 0, true));
5634
5635 struct aws_byte_cursor data = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("hello");
5636 struct aws_input_stream *input_stream = aws_input_stream_new_from_cursor(allocator, &data);
5637
5638 struct aws_http_stream_write_data_options write_options = {
5639 .data = input_stream,
5640 .end_stream = true,
5641 };
5642 ASSERT_SUCCESS(aws_http_stream_write_data(fixture.stream_tester.stream, &write_options));
5643 testing_channel_drain_queued_tasks(&fixture.tester.testing_channel);
5644
5645 /* Transfer-Encoding: chunked should have been auto-added */
5646 const char *expected = "POST /upload HTTP/1.1\r\n"
5647 "Transfer-Encoding: chunked\r\n"
5648 "\r\n"
5649 "5\r\n"
5650 "hello"
5651 "\r\n"
5652 "0\r\n"
5653 "\r\n";
5654 ASSERT_SUCCESS(testing_channel_check_written_messages_str(&fixture.tester.testing_channel, allocator, expected));
5655
5656 aws_input_stream_release(input_stream);
5657 ASSERT_SUCCESS(s_write_data_test_teardown(&fixture));
5658 return AWS_OP_SUCCESS;
5659 }
5660
5661 /* Test: body stream + use_manual_data_writes is rejected */
5662 H1_CLIENT_TEST_CASE(h1_client_write_data_body_stream_conflict) {
5663 (void)ctx;
5664 struct tester tester;
5665 ASSERT_SUCCESS(s_tester_init(&tester, allocator));
5666
5667 struct aws_http_message *request = aws_http_message_new_request(allocator);
5668 ASSERT_NOT_NULL(request);
5669 ASSERT_SUCCESS(aws_http_message_set_request_method(request, aws_byte_cursor_from_c_str("POST")));
5670 ASSERT_SUCCESS(aws_http_message_set_request_path(request, aws_byte_cursor_from_c_str("/upload")));
5671
5672 struct aws_http_header headers[] = {
5673 {.name = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("Content-Length"),
5674 .value = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("5")},
5675 };
5676 ASSERT_SUCCESS(aws_http_message_add_header_array(request, headers, AWS_ARRAY_SIZE(headers)));
5677
5678 /* Set a body stream AND use_manual_data_writes — should conflict */
5679 struct aws_byte_cursor body = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("hello");
5680 struct aws_input_stream *body_stream = aws_input_stream_new_from_cursor(allocator, &body);
5681 aws_http_message_set_body_stream(request, body_stream);
5682
5683 struct aws_http_make_request_options request_options = {
5684 .self_size = sizeof(request_options),
5685 .request = request,
5686 .use_manual_data_writes = true,
5687 };
5688 struct aws_http_stream *stream = aws_http_connection_make_request(tester.connection, &request_options);
5689 ASSERT_NULL(stream);
5690 ASSERT_INT_EQUALS(AWS_ERROR_HTTP_INVALID_HEADER_FIELD, aws_last_error());
5691
5692 aws_input_stream_release(body_stream);
5693 aws_http_message_destroy(request);
5694 ASSERT_SUCCESS(s_tester_clean_up(&tester));
5695 return AWS_OP_SUCCESS;
5696 }
5697
5698 /* Test: NULL data with non-zero Content-Length should fail with length mismatch */
5699 H1_CLIENT_TEST_CASE(h1_client_write_data_null_data_nonzero_content_length) {
5700 (void)ctx;
5701 struct write_data_test_fixture fixture;
5702 struct aws_http_header headers[] = {
5703 {.name = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("Content-Length"),
5704 .value = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("5")},
5705 };
5706 ASSERT_SUCCESS(s_write_data_test_setup(&fixture, allocator, headers, AWS_ARRAY_SIZE(headers), true));
5707
5708 struct write_data_callback_tester callback_tester = {0};
5709 struct aws_http_stream_write_data_options write_options = {
5710 .data = NULL,
5711 .end_stream = true,
5712 .on_complete = s_on_write_data_complete,
5713 .user_data = &callback_tester,
5714 };
5715 ASSERT_SUCCESS(aws_http_stream_write_data(fixture.stream_tester.stream, &write_options));
5716 testing_channel_drain_queued_tasks(&fixture.tester.testing_channel);
5717
5718 ASSERT_TRUE(fixture.stream_tester.complete);
5719 ASSERT_INT_EQUALS(AWS_ERROR_HTTP_OUTGOING_STREAM_LENGTH_INCORRECT, fixture.stream_tester.on_complete_error_code);
5720
5721 ASSERT_SUCCESS(s_write_data_test_teardown(&fixture));
5722 return AWS_OP_SUCCESS;
5723 }