| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
#include <aws/http/connection.h> |
| 7 |
#include <aws/http/private/h1_connection.h> |
| 8 |
#include <aws/http/private/request_response_impl.h> |
| 9 |
#include <aws/http/request_response.h> |
| 10 |
#include <aws/http/server.h> |
| 11 |
|
| 12 |
#include <aws/common/clock.h> |
| 13 |
#include <aws/common/condition_variable.h> |
| 14 |
#include <aws/common/log_writer.h> |
| 15 |
#include <aws/common/uuid.h> |
| 16 |
#include <aws/io/channel_bootstrap.h> |
| 17 |
#include <aws/io/logging.h> |
| 18 |
#include <aws/io/socket.h> |
| 19 |
#include <aws/io/stream.h> |
| 20 |
#include <aws/io/tls_channel_handler.h> |
| 21 |
#include <aws/testing/aws_test_harness.h> |
| 22 |
|
| 23 |
#include <aws/testing/io_testing_channel.h> |
| 24 |
|
| 25 |
#ifdef _MSC_VER |
| 26 |
# pragma warning(disable : 4204) |
| 27 |
#endif |
| 28 |
|
| 29 |
#define TEST_CASE(NAME) \ |
| 30 |
AWS_TEST_CASE(NAME, s_test_##NAME); \ |
| 31 |
static int s_test_##NAME(struct aws_allocator *allocator, void *ctx) |
| 32 |
|
| 33 |
struct tester_request { |
| 34 |
struct aws_http_stream *request_handler; |
| 35 |
|
| 36 |
|
| 37 |
struct aws_byte_buf storage; |
| 38 |
|
| 39 |
struct aws_byte_cursor method; |
| 40 |
struct aws_byte_cursor uri; |
| 41 |
struct aws_http_header headers[100]; |
| 42 |
size_t num_headers; |
| 43 |
|
| 44 |
bool header_done; |
| 45 |
size_t on_complete_cb_count; |
| 46 |
int on_complete_error_code; |
| 47 |
|
| 48 |
struct aws_byte_cursor body; |
| 49 |
|
| 50 |
struct aws_input_stream *response_body; |
| 51 |
}; |
| 52 |
|
| 53 |
|
| 54 |
static struct tester { |
| 55 |
struct aws_allocator *alloc; |
| 56 |
struct aws_logger logger; |
| 57 |
|
| 58 |
struct aws_http_connection *server_connection; |
| 59 |
struct testing_channel testing_channel; |
| 60 |
|
| 61 |
struct tester_request requests[100]; |
| 62 |
int request_num; |
| 63 |
|
| 64 |
bool server_connection_is_shutdown; |
| 65 |
|
| 66 |
} s_tester; |
| 67 |
|
| 68 |
static int s_tester_on_request_header( |
| 69 |
struct aws_http_stream *stream, |
| 70 |
enum aws_http_header_block header_block, |
| 71 |
const struct aws_http_header *header_array, |
| 72 |
size_t num_headers, |
| 73 |
void *user_data) { |
| 74 |
|
| 75 |
(void)stream; |
| 76 |
(void)header_block; |
| 77 |
struct tester_request *request = user_data; |
| 78 |
struct aws_byte_buf *storage = &request->storage; |
| 79 |
const struct aws_http_header *in_header = header_array; |
| 80 |
struct aws_http_header *my_header = request->headers + request->num_headers; |
| 81 |
for (size_t i = 0; i < num_headers; ++i) { |
| 82 |
|
| 83 |
*my_header = *in_header; |
| 84 |
|
| 85 |
my_header->name.ptr = storage->buffer + storage->len; |
| 86 |
AWS_FATAL_ASSERT(aws_byte_buf_write_from_whole_cursor(storage, in_header->name)); |
| 87 |
|
| 88 |
my_header->value.ptr = storage->buffer + storage->len; |
| 89 |
AWS_FATAL_ASSERT(aws_byte_buf_write_from_whole_cursor(storage, in_header->value)); |
| 90 |
|
| 91 |
in_header++; |
| 92 |
my_header++; |
| 93 |
request->num_headers++; |
| 94 |
} |
| 95 |
return AWS_OP_SUCCESS; |
| 96 |
} |
| 97 |
|
| 98 |
static int s_tester_on_request_header_block_done( |
| 99 |
struct aws_http_stream *stream, |
| 100 |
enum aws_http_header_block header_block, |
| 101 |
void *user_data) { |
| 102 |
(void)stream; |
| 103 |
(void)header_block; |
| 104 |
struct tester_request *request = user_data; |
| 105 |
if (header_block == AWS_HTTP_HEADER_BLOCK_MAIN) { |
| 106 |
AWS_FATAL_ASSERT(request->header_done == false); |
| 107 |
request->header_done = true; |
| 108 |
} |
| 109 |
struct aws_http_stream *r_handler = request->request_handler; |
| 110 |
AWS_FATAL_ASSERT(!aws_http_stream_get_incoming_request_method(r_handler, &request->method)); |
| 111 |
AWS_FATAL_ASSERT(!aws_http_stream_get_incoming_request_uri(r_handler, &request->uri)); |
| 112 |
return AWS_OP_SUCCESS; |
| 113 |
} |
| 114 |
|
| 115 |
static int s_tester_on_request_body( |
| 116 |
struct aws_http_stream *stream, |
| 117 |
const struct aws_byte_cursor *data, |
| 118 |
void *user_data) { |
| 119 |
|
| 120 |
(void)stream; |
| 121 |
struct tester_request *request = user_data; |
| 122 |
|
| 123 |
AWS_FATAL_ASSERT(request->header_done == true); |
| 124 |
|
| 125 |
|
| 126 |
AWS_FATAL_ASSERT(aws_byte_buf_write_from_whole_cursor(&request->storage, *data)); |
| 127 |
request->body.len += data->len; |
| 128 |
if (!request->body.ptr) { |
| 129 |
request->body.ptr = request->storage.buffer + request->storage.len - request->body.len; |
| 130 |
} |
| 131 |
|
| 132 |
return AWS_OP_SUCCESS; |
| 133 |
} |
| 134 |
|
| 135 |
static void s_tester_on_stream_complete(struct aws_http_stream *stream, int error_code, void *user_data) { |
| 136 |
struct tester_request *request = user_data; |
| 137 |
(void)stream; |
| 138 |
request->on_complete_cb_count++; |
| 139 |
request->on_complete_error_code = error_code; |
| 140 |
} |
| 141 |
|
| 142 |
|
| 143 |
static struct aws_http_stream *s_tester_on_incoming_request(struct aws_http_connection *connection, void *user_data) { |
| 144 |
|
| 145 |
struct aws_http_request_handler_options options = AWS_HTTP_REQUEST_HANDLER_OPTIONS_INIT; |
| 146 |
struct tester *tester = user_data; |
| 147 |
|
| 148 |
int index = tester->request_num; |
| 149 |
|
| 150 |
tester->requests[index].num_headers = 0; |
| 151 |
tester->requests[index].header_done = false; |
| 152 |
aws_byte_buf_init(&tester->requests[index].storage, tester->alloc, 1024 * 1024 * 1); |
| 153 |
|
| 154 |
options.user_data = &tester->requests[index]; |
| 155 |
options.server_connection = connection; |
| 156 |
options.on_request_headers = s_tester_on_request_header; |
| 157 |
options.on_request_header_block_done = s_tester_on_request_header_block_done; |
| 158 |
options.on_request_body = s_tester_on_request_body; |
| 159 |
options.on_complete = s_tester_on_stream_complete; |
| 160 |
tester->requests[index].request_handler = aws_http_stream_new_server_request_handler(&options); |
| 161 |
|
| 162 |
tester->request_num++; |
| 163 |
return tester->requests[index].request_handler; |
| 164 |
} |
| 165 |
|
| 166 |
static int s_tester_init(struct aws_allocator *alloc) { |
| 167 |
|
| 168 |
aws_http_library_init(alloc); |
| 169 |
|
| 170 |
AWS_ZERO_STRUCT(s_tester); |
| 171 |
|
| 172 |
s_tester.alloc = alloc; |
| 173 |
|
| 174 |
s_tester.request_num = 0; |
| 175 |
|
| 176 |
struct aws_logger_standard_options logger_options = { |
| 177 |
.level = AWS_LOG_LEVEL_TRACE, |
| 178 |
.file = stderr, |
| 179 |
}; |
| 180 |
ASSERT_SUCCESS(aws_logger_init_standard(&s_tester.logger, s_tester.alloc, &logger_options)); |
| 181 |
aws_logger_set(&s_tester.logger); |
| 182 |
|
| 183 |
struct aws_testing_channel_options test_channel_options = {.clock_fn = aws_high_res_clock_get_ticks}; |
| 184 |
ASSERT_SUCCESS(testing_channel_init(&s_tester.testing_channel, alloc, &test_channel_options)); |
| 185 |
|
| 186 |
struct aws_http1_connection_options http1_options; |
| 187 |
AWS_ZERO_STRUCT(http1_options); |
| 188 |
s_tester.server_connection = aws_http_connection_new_http1_1_server(alloc, true, SIZE_MAX, &http1_options); |
| 189 |
ASSERT_NOT_NULL(s_tester.server_connection); |
| 190 |
struct aws_http_server_connection_options options = AWS_HTTP_SERVER_CONNECTION_OPTIONS_INIT; |
| 191 |
options.connection_user_data = &s_tester; |
| 192 |
options.on_incoming_request = s_tester_on_incoming_request; |
| 193 |
|
| 194 |
ASSERT_SUCCESS(aws_http_connection_configure_server(s_tester.server_connection, &options)); |
| 195 |
|
| 196 |
struct aws_channel_slot *slot = aws_channel_slot_new(s_tester.testing_channel.channel); |
| 197 |
ASSERT_NOT_NULL(slot); |
| 198 |
ASSERT_SUCCESS(aws_channel_slot_insert_end(s_tester.testing_channel.channel, slot)); |
| 199 |
ASSERT_SUCCESS(aws_channel_slot_set_handler(slot, &s_tester.server_connection->channel_handler)); |
| 200 |
s_tester.server_connection->vtable->on_channel_handler_installed( |
| 201 |
&s_tester.server_connection->channel_handler, slot); |
| 202 |
|
| 203 |
testing_channel_drain_queued_tasks(&s_tester.testing_channel); |
| 204 |
|
| 205 |
return AWS_OP_SUCCESS; |
| 206 |
} |
| 207 |
|
| 208 |
static int s_server_request_clean_up(void) { |
| 209 |
for (int i = 0; i < s_tester.request_num; i++) { |
| 210 |
aws_http_stream_release(s_tester.requests[i].request_handler); |
| 211 |
aws_byte_buf_clean_up(&s_tester.requests[i].storage); |
| 212 |
aws_input_stream_release(s_tester.requests[i].response_body); |
| 213 |
} |
| 214 |
return AWS_OP_SUCCESS; |
| 215 |
} |
| 216 |
|
| 217 |
static int s_server_tester_clean_up(void) { |
| 218 |
s_server_request_clean_up(); |
| 219 |
aws_http_connection_release(s_tester.server_connection); |
| 220 |
ASSERT_SUCCESS(testing_channel_clean_up(&s_tester.testing_channel)); |
| 221 |
aws_http_library_clean_up(); |
| 222 |
aws_logger_clean_up(&s_tester.logger); |
| 223 |
|
| 224 |
return AWS_OP_SUCCESS; |
| 225 |
} |
| 226 |
|
| 227 |
|
| 228 |
static int s_send_message_cursor(struct aws_byte_cursor data) { |
| 229 |
|
| 230 |
struct aws_io_message *msg = aws_channel_acquire_message_from_pool( |
| 231 |
s_tester.testing_channel.channel, AWS_IO_MESSAGE_APPLICATION_DATA, data.len); |
| 232 |
ASSERT_NOT_NULL(msg); |
| 233 |
|
| 234 |
ASSERT_TRUE(aws_byte_buf_write_from_whole_cursor(&msg->message_data, data)); |
| 235 |
|
| 236 |
ASSERT_SUCCESS(testing_channel_push_read_message(&s_tester.testing_channel, msg)); |
| 237 |
|
| 238 |
return AWS_OP_SUCCESS; |
| 239 |
} |
| 240 |
|
| 241 |
static int s_send_message_c_str(const char *str) { |
| 242 |
return s_send_message_cursor(aws_byte_cursor_from_c_str(str)); |
| 243 |
} |
| 244 |
|
| 245 |
|
| 246 |
TEST_CASE(h1_server_sanity_check) { |
| 247 |
(void)ctx; |
| 248 |
ASSERT_SUCCESS(s_tester_init(allocator)); |
| 249 |
|
| 250 |
ASSERT_SUCCESS(s_server_tester_clean_up()); |
| 251 |
return AWS_OP_SUCCESS; |
| 252 |
} |
| 253 |
|
| 254 |
TEST_CASE(h1_server_receive_1line_request) { |
| 255 |
|
| 256 |
(void)ctx; |
| 257 |
ASSERT_SUCCESS(s_tester_init(allocator)); |
| 258 |
|
| 259 |
const char *incoming_request = "GET / HTTP/1.1\r\n" |
| 260 |
"\r\n"; |
| 261 |
ASSERT_SUCCESS(s_send_message_c_str(incoming_request)); |
| 262 |
testing_channel_drain_queued_tasks(&s_tester.testing_channel); |
| 263 |
|
| 264 |
ASSERT_TRUE(s_tester.request_num == 1); |
| 265 |
ASSERT_TRUE(aws_byte_cursor_eq_c_str(&s_tester.requests[0].method, "GET")); |
| 266 |
ASSERT_TRUE(aws_byte_cursor_eq_c_str(&s_tester.requests[0].uri, "/")); |
| 267 |
|
| 268 |
ASSERT_SUCCESS(s_server_tester_clean_up()); |
| 269 |
return AWS_OP_SUCCESS; |
| 270 |
} |
| 271 |
|
| 272 |
static int s_check_header(struct tester_request *request, size_t i, const char *name_str, const char *value) { |
| 273 |
|
| 274 |
ASSERT_TRUE(i < request->num_headers); |
| 275 |
struct aws_http_header *header = request->headers + i; |
| 276 |
ASSERT_TRUE(aws_byte_cursor_eq_c_str_ignore_case(&header->name, name_str)); |
| 277 |
ASSERT_TRUE(aws_byte_cursor_eq_c_str_ignore_case(&header->value, value)); |
| 278 |
|
| 279 |
return AWS_OP_SUCCESS; |
| 280 |
} |
| 281 |
|
| 282 |
TEST_CASE(h1_server_receive_headers) { |
| 283 |
|
| 284 |
(void)ctx; |
| 285 |
ASSERT_SUCCESS(s_tester_init(allocator)); |
| 286 |
|
| 287 |
const char *incoming_request = "GET / HTTP/1.1\r\n" |
| 288 |
"Host: example.com\r\n" |
| 289 |
"Accept: */*\r\n" |
| 290 |
"\r\n"; |
| 291 |
ASSERT_SUCCESS(s_send_message_c_str(incoming_request)); |
| 292 |
testing_channel_drain_queued_tasks(&s_tester.testing_channel); |
| 293 |
|
| 294 |
ASSERT_TRUE(s_tester.request_num == 1); |
| 295 |
|
| 296 |
struct tester_request request = s_tester.requests[0]; |
| 297 |
ASSERT_TRUE(request.num_headers == 2); |
| 298 |
ASSERT_SUCCESS(s_check_header(&request, 0, "Host", "example.com")); |
| 299 |
ASSERT_SUCCESS(s_check_header(&request, 1, "Accept", "*/*")); |
| 300 |
ASSERT_TRUE(aws_byte_cursor_eq_c_str(&request.method, "GET")); |
| 301 |
ASSERT_TRUE(aws_byte_cursor_eq_c_str(&request.uri, "/")); |
| 302 |
ASSERT_TRUE(request.body.len == 0); |
| 303 |
|
| 304 |
|
| 305 |
ASSERT_SUCCESS(s_server_tester_clean_up()); |
| 306 |
return AWS_OP_SUCCESS; |
| 307 |
} |
| 308 |
|
| 309 |
TEST_CASE(h1_server_receive_body) { |
| 310 |
(void)ctx; |
| 311 |
ASSERT_SUCCESS(s_tester_init(allocator)); |
| 312 |
|
| 313 |
const char *incoming_request = "PUT /plan.txt HTTP/1.1\r\n" |
| 314 |
"Content-Length: 16\r\n" |
| 315 |
"\r\n" |
| 316 |
"write more tests"; |
| 317 |
ASSERT_SUCCESS(s_send_message_c_str(incoming_request)); |
| 318 |
testing_channel_drain_queued_tasks(&s_tester.testing_channel); |
| 319 |
|
| 320 |
ASSERT_TRUE(s_tester.request_num == 1); |
| 321 |
|
| 322 |
struct tester_request request = s_tester.requests[0]; |
| 323 |
ASSERT_TRUE(request.num_headers == 1); |
| 324 |
ASSERT_SUCCESS(s_check_header(&request, 0, "Content-Length", "16")); |
| 325 |
ASSERT_TRUE(aws_byte_cursor_eq_c_str(&request.method, "PUT")); |
| 326 |
ASSERT_TRUE(aws_byte_cursor_eq_c_str(&request.uri, "/plan.txt")); |
| 327 |
ASSERT_TRUE(aws_byte_cursor_eq_c_str(&request.body, "write more tests")); |
| 328 |
|
| 329 |
|
| 330 |
ASSERT_SUCCESS(s_server_tester_clean_up()); |
| 331 |
return AWS_OP_SUCCESS; |
| 332 |
} |
| 333 |
|
| 334 |
TEST_CASE(h1_server_receive_1_request_from_multiple_io_messages) { |
| 335 |
(void)ctx; |
| 336 |
ASSERT_SUCCESS(s_tester_init(allocator)); |
| 337 |
|
| 338 |
const char *incoming_request = "PUT /plan.txt HTTP/1.1\r\n" |
| 339 |
"Content-Length: 16\r\n" |
| 340 |
"\r\n" |
| 341 |
"write more tests"; |
| 342 |
size_t str_len = strlen(incoming_request); |
| 343 |
for (size_t i = 0; i < str_len; ++i) { |
| 344 |
s_send_message_cursor(aws_byte_cursor_from_array(incoming_request + i, 1)); |
| 345 |
} |
| 346 |
testing_channel_drain_queued_tasks(&s_tester.testing_channel); |
| 347 |
|
| 348 |
ASSERT_TRUE(s_tester.request_num == 1); |
| 349 |
|
| 350 |
struct tester_request request = s_tester.requests[0]; |
| 351 |
ASSERT_TRUE(request.num_headers == 1); |
| 352 |
ASSERT_SUCCESS(s_check_header(&request, 0, "Content-Length", "16")); |
| 353 |
ASSERT_TRUE(aws_byte_cursor_eq_c_str(&request.method, "PUT")); |
| 354 |
ASSERT_TRUE(aws_byte_cursor_eq_c_str(&request.uri, "/plan.txt")); |
| 355 |
ASSERT_TRUE(aws_byte_cursor_eq_c_str(&request.body, "write more tests")); |
| 356 |
|
| 357 |
|
| 358 |
ASSERT_SUCCESS(s_server_tester_clean_up()); |
| 359 |
return AWS_OP_SUCCESS; |
| 360 |
} |
| 361 |
|
| 362 |
TEST_CASE(h1_server_receive_multiple_requests_from_1_io_messages) { |
| 363 |
(void)ctx; |
| 364 |
ASSERT_SUCCESS(s_tester_init(allocator)); |
| 365 |
|
| 366 |
const char *incoming_request = "PUT /plan.txt HTTP/1.1\r\n" |
| 367 |
"Content-Length: 16\r\n" |
| 368 |
"\r\n" |
| 369 |
"write more tests" |
| 370 |
"GET / HTTP/1.1\r\n" |
| 371 |
"\r\n"; |
| 372 |
ASSERT_SUCCESS(s_send_message_c_str(incoming_request)); |
| 373 |
testing_channel_drain_queued_tasks(&s_tester.testing_channel); |
| 374 |
|
| 375 |
ASSERT_TRUE(s_tester.request_num == 2); |
| 376 |
|
| 377 |
struct tester_request request = s_tester.requests[0]; |
| 378 |
ASSERT_TRUE(request.num_headers == 1); |
| 379 |
ASSERT_SUCCESS(s_check_header(&request, 0, "Content-Length", "16")); |
| 380 |
ASSERT_TRUE(aws_byte_cursor_eq_c_str(&request.method, "PUT")); |
| 381 |
ASSERT_TRUE(aws_byte_cursor_eq_c_str(&request.uri, "/plan.txt")); |
| 382 |
ASSERT_TRUE(aws_byte_cursor_eq_c_str(&request.body, "write more tests")); |
| 383 |
|
| 384 |
request = s_tester.requests[1]; |
| 385 |
ASSERT_TRUE(request.num_headers == 0); |
| 386 |
ASSERT_TRUE(aws_byte_cursor_eq_c_str(&request.method, "GET")); |
| 387 |
ASSERT_TRUE(aws_byte_cursor_eq_c_str(&request.uri, "/")); |
| 388 |
|
| 389 |
ASSERT_SUCCESS(s_server_tester_clean_up()); |
| 390 |
return AWS_OP_SUCCESS; |
| 391 |
} |
| 392 |
|
| 393 |
TEST_CASE(h1_server_receive_bad_request_shut_down_connection) { |
| 394 |
(void)ctx; |
| 395 |
ASSERT_SUCCESS(s_tester_init(allocator)); |
| 396 |
|
| 397 |
const char *incoming_request = "Mmmm garbage data\r\n\r\n"; |
| 398 |
ASSERT_SUCCESS(s_send_message_c_str(incoming_request)); |
| 399 |
testing_channel_drain_queued_tasks(&s_tester.testing_channel); |
| 400 |
|
| 401 |
ASSERT_TRUE(s_tester.request_num == 1); |
| 402 |
struct tester_request request = s_tester.requests[0]; |
| 403 |
ASSERT_TRUE(request.on_complete_cb_count == 1); |
| 404 |
ASSERT_INT_EQUALS(AWS_ERROR_HTTP_PROTOCOL_ERROR, request.on_complete_error_code); |
| 405 |
|
| 406 |
ASSERT_SUCCESS(s_server_tester_clean_up()); |
| 407 |
return AWS_OP_SUCCESS; |
| 408 |
} |
| 409 |
|
| 410 |
|
| 411 |
static int s_create_response( |
| 412 |
struct aws_http_message **out_response, |
| 413 |
int status_code, |
| 414 |
const struct aws_http_header *header_array, |
| 415 |
size_t num_headers, |
| 416 |
struct aws_input_stream *body) { |
| 417 |
|
| 418 |
struct aws_http_message *response = aws_http_message_new_response(s_tester.alloc); |
| 419 |
ASSERT_NOT_NULL(response); |
| 420 |
ASSERT_SUCCESS(aws_http_message_set_response_status(response, status_code)); |
| 421 |
if (num_headers) { |
| 422 |
ASSERT_SUCCESS(aws_http_message_add_header_array(response, header_array, num_headers)); |
| 423 |
} |
| 424 |
aws_http_message_set_body_stream(response, body); |
| 425 |
*out_response = response; |
| 426 |
return AWS_OP_SUCCESS; |
| 427 |
} |
| 428 |
|
| 429 |
TEST_CASE(h1_server_send_1line_response) { |
| 430 |
|
| 431 |
(void)ctx; |
| 432 |
ASSERT_SUCCESS(s_tester_init(allocator)); |
| 433 |
|
| 434 |
const char *incoming_request = "GET / HTTP/1.1\r\n" |
| 435 |
"\r\n"; |
| 436 |
ASSERT_SUCCESS(s_send_message_c_str(incoming_request)); |
| 437 |
testing_channel_drain_queued_tasks(&s_tester.testing_channel); |
| 438 |
|
| 439 |
ASSERT_TRUE(s_tester.request_num == 1); |
| 440 |
|
| 441 |
struct tester_request request = s_tester.requests[0]; |
| 442 |
|
| 443 |
struct aws_http_message *response; |
| 444 |
ASSERT_SUCCESS(s_create_response(&response, 204, NULL, 0, NULL)); |
| 445 |
ASSERT_SUCCESS(aws_http_stream_send_response(request.request_handler, response)); |
| 446 |
testing_channel_drain_queued_tasks(&s_tester.testing_channel); |
| 447 |
|
| 448 |
const char *expected = "HTTP/1.1 204 No Content\r\n" |
| 449 |
"\r\n"; |
| 450 |
|
| 451 |
ASSERT_SUCCESS(testing_channel_check_written_messages_str(&s_tester.testing_channel, allocator, expected)); |
| 452 |
|
| 453 |
aws_http_message_destroy(response); |
| 454 |
ASSERT_SUCCESS(s_server_tester_clean_up()); |
| 455 |
return AWS_OP_SUCCESS; |
| 456 |
} |
| 457 |
|
| 458 |
TEST_CASE(h1_server_send_response_headers) { |
| 459 |
|
| 460 |
(void)ctx; |
| 461 |
ASSERT_SUCCESS(s_tester_init(allocator)); |
| 462 |
|
| 463 |
const char *incoming_request = "GET / HTTP/1.1\r\n" |
| 464 |
"\r\n"; |
| 465 |
ASSERT_SUCCESS(s_send_message_c_str(incoming_request)); |
| 466 |
testing_channel_drain_queued_tasks(&s_tester.testing_channel); |
| 467 |
|
| 468 |
ASSERT_TRUE(s_tester.request_num == 1); |
| 469 |
|
| 470 |
struct tester_request request = s_tester.requests[0]; |
| 471 |
|
| 472 |
|
| 473 |
struct aws_http_header headers[] = { |
| 474 |
{ |
| 475 |
.name = aws_byte_cursor_from_c_str("Date"), |
| 476 |
.value = aws_byte_cursor_from_c_str("Fri, 01 Mar 2019 17:18:55 GMT"), |
| 477 |
}, |
| 478 |
{ |
| 479 |
.name = aws_byte_cursor_from_c_str("Location"), |
| 480 |
.value = aws_byte_cursor_from_c_str("/index.html"), |
| 481 |
}, |
| 482 |
}; |
| 483 |
struct aws_http_message *response; |
| 484 |
ASSERT_SUCCESS(s_create_response(&response, 308, headers, AWS_ARRAY_SIZE(headers), NULL)); |
| 485 |
ASSERT_SUCCESS(aws_http_stream_send_response(request.request_handler, response)); |
| 486 |
testing_channel_drain_queued_tasks(&s_tester.testing_channel); |
| 487 |
|
| 488 |
const char *expected = "HTTP/1.1 308 Permanent Redirect\r\n" |
| 489 |
"Date: Fri, 01 Mar 2019 17:18:55 GMT\r\n" |
| 490 |
"Location: /index.html\r\n" |
| 491 |
"\r\n"; |
| 492 |
|
| 493 |
ASSERT_SUCCESS(testing_channel_check_written_messages_str(&s_tester.testing_channel, allocator, expected)); |
| 494 |
|
| 495 |
aws_http_message_destroy(response); |
| 496 |
ASSERT_SUCCESS(s_server_tester_clean_up()); |
| 497 |
return AWS_OP_SUCCESS; |
| 498 |
} |
| 499 |
|
| 500 |
TEST_CASE(h1_server_send_response_body) { |
| 501 |
|
| 502 |
(void)ctx; |
| 503 |
ASSERT_SUCCESS(s_tester_init(allocator)); |
| 504 |
|
| 505 |
const char *incoming_request = "GET / HTTP/1.1\r\n" |
| 506 |
"\r\n"; |
| 507 |
ASSERT_SUCCESS(s_send_message_c_str(incoming_request)); |
| 508 |
testing_channel_drain_queued_tasks(&s_tester.testing_channel); |
| 509 |
|
| 510 |
ASSERT_TRUE(s_tester.request_num == 1); |
| 511 |
|
| 512 |
struct tester_request *request = s_tester.requests; |
| 513 |
|
| 514 |
|
| 515 |
struct aws_byte_cursor body_src = aws_byte_cursor_from_c_str("write more tests"); |
| 516 |
request->response_body = aws_input_stream_new_from_cursor(allocator, &body_src); |
| 517 |
ASSERT_NOT_NULL(request->response_body); |
| 518 |
struct aws_http_header headers[] = { |
| 519 |
{ |
| 520 |
.name = aws_byte_cursor_from_c_str("Date"), |
| 521 |
.value = aws_byte_cursor_from_c_str("Fri, 01 Mar 2019 17:18:55 GMT"), |
| 522 |
}, |
| 523 |
{ |
| 524 |
.name = aws_byte_cursor_from_c_str("Location"), |
| 525 |
.value = aws_byte_cursor_from_c_str("/index.html"), |
| 526 |
}, |
| 527 |
{ |
| 528 |
.name = aws_byte_cursor_from_c_str("Content-Length"), |
| 529 |
.value = aws_byte_cursor_from_c_str("16"), |
| 530 |
}, |
| 531 |
}; |
| 532 |
struct aws_http_message *response; |
| 533 |
ASSERT_SUCCESS(s_create_response(&response, 308, headers, AWS_ARRAY_SIZE(headers), request->response_body)); |
| 534 |
|
| 535 |
ASSERT_SUCCESS(aws_http_stream_send_response(request->request_handler, response)); |
| 536 |
testing_channel_drain_queued_tasks(&s_tester.testing_channel); |
| 537 |
|
| 538 |
const char *expected = "HTTP/1.1 308 Permanent Redirect\r\n" |
| 539 |
"Date: Fri, 01 Mar 2019 17:18:55 GMT\r\n" |
| 540 |
"Location: /index.html\r\n" |
| 541 |
"Content-Length: 16\r\n" |
| 542 |
"\r\n" |
| 543 |
"write more tests"; |
| 544 |
|
| 545 |
ASSERT_SUCCESS(testing_channel_check_written_messages_str(&s_tester.testing_channel, allocator, expected)); |
| 546 |
|
| 547 |
aws_http_message_destroy(response); |
| 548 |
ASSERT_SUCCESS(s_server_tester_clean_up()); |
| 549 |
return AWS_OP_SUCCESS; |
| 550 |
} |
| 551 |
|
| 552 |
static int s_test_send_expected_no_body_response(int status_int, bool head_request) { |
| 553 |
const char *incoming_request; |
| 554 |
if (head_request) { |
| 555 |
incoming_request = "HEAD / HTTP/1.1\r\n" |
| 556 |
"\r\n"; |
| 557 |
} else { |
| 558 |
incoming_request = "GET / HTTP/1.1\r\n" |
| 559 |
"\r\n"; |
| 560 |
} |
| 561 |
|
| 562 |
ASSERT_SUCCESS(s_send_message_c_str(incoming_request)); |
| 563 |
testing_channel_drain_queued_tasks(&s_tester.testing_channel); |
| 564 |
|
| 565 |
ASSERT_TRUE(s_tester.request_num == 1); |
| 566 |
|
| 567 |
struct tester_request *request = s_tester.requests; |
| 568 |
|
| 569 |
|
| 570 |
|
| 571 |
struct aws_http_header headers[] = { |
| 572 |
{ |
| 573 |
.name = aws_byte_cursor_from_c_str("Date"), |
| 574 |
.value = aws_byte_cursor_from_c_str("Fri, 01 Mar 2019 17:18:55 GMT"), |
| 575 |
}, |
| 576 |
{ |
| 577 |
.name = aws_byte_cursor_from_c_str("Location"), |
| 578 |
.value = aws_byte_cursor_from_c_str("/index.html"), |
| 579 |
}, |
| 580 |
{ |
| 581 |
.name = aws_byte_cursor_from_c_str("Content-Length"), |
| 582 |
.value = aws_byte_cursor_from_c_str("16"), |
| 583 |
}, |
| 584 |
}; |
| 585 |
struct aws_http_message *response; |
| 586 |
ASSERT_SUCCESS(s_create_response(&response, status_int, headers, AWS_ARRAY_SIZE(headers), NULL)); |
| 587 |
|
| 588 |
ASSERT_SUCCESS(aws_http_stream_send_response(request->request_handler, response)); |
| 589 |
testing_channel_drain_queued_tasks(&s_tester.testing_channel); |
| 590 |
|
| 591 |
char expected[500]; |
| 592 |
const char *expected_headers = "Date: Fri, 01 Mar 2019 17:18:55 GMT\r\n" |
| 593 |
"Location: /index.html\r\n" |
| 594 |
"Content-Length: 16\r\n" |
| 595 |
"\r\n"; |
| 596 |
|
| 597 |
struct aws_byte_cursor status_text = aws_byte_cursor_from_c_str(aws_http_status_text(status_int)); |
| 598 |
char c_status_text[100]; |
| 599 |
memcpy(c_status_text, status_text.ptr, status_text.len); |
| 600 |
c_status_text[status_text.len] = '\0'; |
| 601 |
snprintf(expected, sizeof(expected), "HTTP/1.1 %d %s\r\n%s", status_int, c_status_text, expected_headers); |
| 602 |
|
| 603 |
ASSERT_SUCCESS(testing_channel_check_written_messages_str(&s_tester.testing_channel, s_tester.alloc, expected)); |
| 604 |
|
| 605 |
aws_http_message_destroy(response); |
| 606 |
return AWS_OP_SUCCESS; |
| 607 |
} |
| 608 |
|
| 609 |
TEST_CASE(h1_server_send_response_to_HEAD_request) { |
| 610 |
(void)ctx; |
| 611 |
ASSERT_SUCCESS(s_tester_init(allocator)); |
| 612 |
ASSERT_SUCCESS(s_test_send_expected_no_body_response(308, true)); |
| 613 |
ASSERT_SUCCESS(s_server_tester_clean_up()); |
| 614 |
return AWS_OP_SUCCESS; |
| 615 |
} |
| 616 |
|
| 617 |
TEST_CASE(h1_server_send_304_response) { |
| 618 |
(void)ctx; |
| 619 |
ASSERT_SUCCESS(s_tester_init(allocator)); |
| 620 |
ASSERT_SUCCESS(s_test_send_expected_no_body_response(304, false)); |
| 621 |
ASSERT_SUCCESS(s_server_tester_clean_up()); |
| 622 |
return AWS_OP_SUCCESS; |
| 623 |
} |
| 624 |
|
| 625 |
TEST_CASE(h1_server_send_multiple_responses_in_order) { |
| 626 |
|
| 627 |
(void)ctx; |
| 628 |
ASSERT_SUCCESS(s_tester_init(allocator)); |
| 629 |
|
| 630 |
const char *incoming_request = "GET / HTTP/1.1\r\n" |
| 631 |
"\r\n" |
| 632 |
"GET / HTTP/1.1\r\n" |
| 633 |
"\r\n" |
| 634 |
"GET / HTTP/1.1\r\n" |
| 635 |
"\r\n"; |
| 636 |
ASSERT_SUCCESS(s_send_message_c_str(incoming_request)); |
| 637 |
testing_channel_drain_queued_tasks(&s_tester.testing_channel); |
| 638 |
|
| 639 |
ASSERT_TRUE(s_tester.request_num == 3); |
| 640 |
|
| 641 |
struct tester_request *request1 = s_tester.requests; |
| 642 |
struct tester_request *request2 = s_tester.requests + 1; |
| 643 |
struct tester_request *request3 = s_tester.requests + 2; |
| 644 |
|
| 645 |
|
| 646 |
|
| 647 |
struct aws_byte_cursor body_src = aws_byte_cursor_from_c_str("response1"); |
| 648 |
request1->response_body = aws_input_stream_new_from_cursor(allocator, &body_src); |
| 649 |
ASSERT_NOT_NULL(request1->response_body); |
| 650 |
struct aws_http_header headers[] = { |
| 651 |
{ |
| 652 |
.name = aws_byte_cursor_from_c_str("Content-Length"), |
| 653 |
.value = aws_byte_cursor_from_c_str("9"), |
| 654 |
}, |
| 655 |
}; |
| 656 |
struct aws_http_message *response1; |
| 657 |
ASSERT_SUCCESS(s_create_response(&response1, 200, headers, AWS_ARRAY_SIZE(headers), request1->response_body)); |
| 658 |
ASSERT_SUCCESS(aws_http_stream_send_response(request1->request_handler, response1)); |
| 659 |
|
| 660 |
|
| 661 |
body_src = aws_byte_cursor_from_c_str("response2"); |
| 662 |
request2->response_body = aws_input_stream_new_from_cursor(allocator, &body_src); |
| 663 |
ASSERT_NOT_NULL(request2->response_body); |
| 664 |
struct aws_http_message *response2; |
| 665 |
ASSERT_SUCCESS(s_create_response(&response2, 200, headers, AWS_ARRAY_SIZE(headers), request2->response_body)); |
| 666 |
ASSERT_SUCCESS(aws_http_stream_send_response(request2->request_handler, response2)); |
| 667 |
|
| 668 |
body_src = aws_byte_cursor_from_c_str("response3"); |
| 669 |
request3->response_body = aws_input_stream_new_from_cursor(allocator, &body_src); |
| 670 |
ASSERT_NOT_NULL(request3->response_body); |
| 671 |
struct aws_http_message *response3; |
| 672 |
ASSERT_SUCCESS(s_create_response(&response3, 200, headers, AWS_ARRAY_SIZE(headers), request3->response_body)); |
| 673 |
ASSERT_SUCCESS(aws_http_stream_send_response(request3->request_handler, response3)); |
| 674 |
testing_channel_drain_queued_tasks(&s_tester.testing_channel); |
| 675 |
|
| 676 |
|
| 677 |
const char *expected = "HTTP/1.1 200 OK\r\n" |
| 678 |
"Content-Length: 9\r\n" |
| 679 |
"\r\n" |
| 680 |
"response1" |
| 681 |
"HTTP/1.1 200 OK\r\n" |
| 682 |
"Content-Length: 9\r\n" |
| 683 |
"\r\n" |
| 684 |
"response2" |
| 685 |
"HTTP/1.1 200 OK\r\n" |
| 686 |
"Content-Length: 9\r\n" |
| 687 |
"\r\n" |
| 688 |
"response3"; |
| 689 |
|
| 690 |
ASSERT_SUCCESS(testing_channel_check_written_messages_str(&s_tester.testing_channel, allocator, expected)); |
| 691 |
|
| 692 |
aws_http_message_destroy(response1); |
| 693 |
aws_http_message_destroy(response2); |
| 694 |
aws_http_message_destroy(response3); |
| 695 |
|
| 696 |
ASSERT_SUCCESS(s_server_tester_clean_up()); |
| 697 |
|
| 698 |
ASSERT_TRUE(request1->on_complete_cb_count == 1); |
| 699 |
ASSERT_TRUE(request2->on_complete_cb_count == 1); |
| 700 |
ASSERT_TRUE(request3->on_complete_cb_count == 1); |
| 701 |
|
| 702 |
ASSERT_TRUE(request1->on_complete_error_code == AWS_ERROR_SUCCESS); |
| 703 |
ASSERT_TRUE(request2->on_complete_error_code == AWS_ERROR_SUCCESS); |
| 704 |
ASSERT_TRUE(request3->on_complete_error_code == AWS_ERROR_SUCCESS); |
| 705 |
return AWS_OP_SUCCESS; |
| 706 |
} |
| 707 |
|
| 708 |
TEST_CASE(h1_server_send_multiple_responses_out_of_order) { |
| 709 |
|
| 710 |
(void)ctx; |
| 711 |
ASSERT_SUCCESS(s_tester_init(allocator)); |
| 712 |
|
| 713 |
const char *incoming_request = "GET / HTTP/1.1\r\n" |
| 714 |
"\r\n" |
| 715 |
"GET / HTTP/1.1\r\n" |
| 716 |
"\r\n" |
| 717 |
"GET / HTTP/1.1\r\n" |
| 718 |
"\r\n"; |
| 719 |
ASSERT_SUCCESS(s_send_message_c_str(incoming_request)); |
| 720 |
testing_channel_drain_queued_tasks(&s_tester.testing_channel); |
| 721 |
|
| 722 |
ASSERT_TRUE(s_tester.request_num == 3); |
| 723 |
|
| 724 |
struct tester_request *request1 = s_tester.requests; |
| 725 |
ASSERT_TRUE(aws_byte_cursor_eq_c_str(&request1->method, "GET")); |
| 726 |
ASSERT_TRUE(aws_byte_cursor_eq_c_str(&request1->uri, "/")); |
| 727 |
struct tester_request *request2 = s_tester.requests + 1; |
| 728 |
ASSERT_TRUE(aws_byte_cursor_eq_c_str(&request2->method, "GET")); |
| 729 |
ASSERT_TRUE(aws_byte_cursor_eq_c_str(&request2->uri, "/")); |
| 730 |
struct tester_request *request3 = s_tester.requests + 2; |
| 731 |
ASSERT_TRUE(aws_byte_cursor_eq_c_str(&request3->method, "GET")); |
| 732 |
ASSERT_TRUE(aws_byte_cursor_eq_c_str(&request3->uri, "/")); |
| 733 |
|
| 734 |
|
| 735 |
|
| 736 |
struct aws_byte_cursor body_src = aws_byte_cursor_from_c_str("response1"); |
| 737 |
request1->response_body = aws_input_stream_new_from_cursor(allocator, &body_src); |
| 738 |
ASSERT_NOT_NULL(request1->response_body); |
| 739 |
struct aws_http_header headers[] = { |
| 740 |
{ |
| 741 |
.name = aws_byte_cursor_from_c_str("Content-Length"), |
| 742 |
.value = aws_byte_cursor_from_c_str("9"), |
| 743 |
}, |
| 744 |
}; |
| 745 |
struct aws_http_message *response1; |
| 746 |
ASSERT_SUCCESS(s_create_response(&response1, 200, headers, AWS_ARRAY_SIZE(headers), request1->response_body)); |
| 747 |
ASSERT_SUCCESS(aws_http_stream_send_response(request1->request_handler, response1)); |
| 748 |
|
| 749 |
|
| 750 |
body_src = aws_byte_cursor_from_c_str("response3"); |
| 751 |
request3->response_body = aws_input_stream_new_from_cursor(allocator, &body_src); |
| 752 |
ASSERT_NOT_NULL(request3->response_body); |
| 753 |
struct aws_http_message *response3; |
| 754 |
ASSERT_SUCCESS(s_create_response(&response3, 200, headers, AWS_ARRAY_SIZE(headers), request3->response_body)); |
| 755 |
ASSERT_SUCCESS(aws_http_stream_send_response(request3->request_handler, response3)); |
| 756 |
|
| 757 |
body_src = aws_byte_cursor_from_c_str("response2"); |
| 758 |
request2->response_body = aws_input_stream_new_from_cursor(allocator, &body_src); |
| 759 |
ASSERT_NOT_NULL(request2->response_body); |
| 760 |
struct aws_http_message *response2; |
| 761 |
ASSERT_SUCCESS(s_create_response(&response2, 200, headers, AWS_ARRAY_SIZE(headers), request2->response_body)); |
| 762 |
ASSERT_SUCCESS(aws_http_stream_send_response(request2->request_handler, response2)); |
| 763 |
|
| 764 |
testing_channel_drain_queued_tasks(&s_tester.testing_channel); |
| 765 |
|
| 766 |
|
| 767 |
const char *expected = "HTTP/1.1 200 OK\r\n" |
| 768 |
"Content-Length: 9\r\n" |
| 769 |
"\r\n" |
| 770 |
"response1" |
| 771 |
"HTTP/1.1 200 OK\r\n" |
| 772 |
"Content-Length: 9\r\n" |
| 773 |
"\r\n" |
| 774 |
"response2" |
| 775 |
"HTTP/1.1 200 OK\r\n" |
| 776 |
"Content-Length: 9\r\n" |
| 777 |
"\r\n" |
| 778 |
"response3"; |
| 779 |
|
| 780 |
ASSERT_SUCCESS(testing_channel_check_written_messages_str(&s_tester.testing_channel, allocator, expected)); |
| 781 |
|
| 782 |
aws_http_message_destroy(response1); |
| 783 |
aws_http_message_destroy(response2); |
| 784 |
aws_http_message_destroy(response3); |
| 785 |
|
| 786 |
ASSERT_SUCCESS(s_server_tester_clean_up()); |
| 787 |
|
| 788 |
ASSERT_TRUE(request1->on_complete_cb_count == 1); |
| 789 |
ASSERT_TRUE(request2->on_complete_cb_count == 1); |
| 790 |
ASSERT_TRUE(request3->on_complete_cb_count == 1); |
| 791 |
|
| 792 |
ASSERT_TRUE(request1->on_complete_error_code == AWS_ERROR_SUCCESS); |
| 793 |
ASSERT_TRUE(request2->on_complete_error_code == AWS_ERROR_SUCCESS); |
| 794 |
ASSERT_TRUE(request3->on_complete_error_code == AWS_ERROR_SUCCESS); |
| 795 |
return AWS_OP_SUCCESS; |
| 796 |
} |
| 797 |
|
| 798 |
TEST_CASE(h1_server_send_multiple_responses_out_of_order_only_one_sent) { |
| 799 |
|
| 800 |
(void)ctx; |
| 801 |
ASSERT_SUCCESS(s_tester_init(allocator)); |
| 802 |
|
| 803 |
const char *incoming_request = "GET / HTTP/1.1\r\n" |
| 804 |
"\r\n" |
| 805 |
"GET / HTTP/1.1\r\n" |
| 806 |
"\r\n" |
| 807 |
"GET / HTTP/1.1\r\n" |
| 808 |
"\r\n"; |
| 809 |
ASSERT_SUCCESS(s_send_message_c_str(incoming_request)); |
| 810 |
testing_channel_drain_queued_tasks(&s_tester.testing_channel); |
| 811 |
|
| 812 |
ASSERT_TRUE(s_tester.request_num == 3); |
| 813 |
|
| 814 |
struct tester_request *request1 = s_tester.requests; |
| 815 |
struct tester_request *request2 = s_tester.requests + 1; |
| 816 |
struct tester_request *request3 = s_tester.requests + 2; |
| 817 |
|
| 818 |
|
| 819 |
|
| 820 |
struct aws_byte_cursor body_src = aws_byte_cursor_from_c_str("response1"); |
| 821 |
request1->response_body = aws_input_stream_new_from_cursor(allocator, &body_src); |
| 822 |
ASSERT_NOT_NULL(request1->response_body); |
| 823 |
struct aws_http_header headers[] = { |
| 824 |
{ |
| 825 |
.name = aws_byte_cursor_from_c_str("Content-Length"), |
| 826 |
.value = aws_byte_cursor_from_c_str("9"), |
| 827 |
}, |
| 828 |
}; |
| 829 |
struct aws_http_message *response1; |
| 830 |
ASSERT_SUCCESS(s_create_response(&response1, 200, headers, AWS_ARRAY_SIZE(headers), request1->response_body)); |
| 831 |
ASSERT_SUCCESS(aws_http_stream_send_response(request1->request_handler, response1)); |
| 832 |
|
| 833 |
|
| 834 |
body_src = aws_byte_cursor_from_c_str("response3"); |
| 835 |
request3->response_body = aws_input_stream_new_from_cursor(allocator, &body_src); |
| 836 |
ASSERT_NOT_NULL(request3->response_body); |
| 837 |
struct aws_http_message *response3; |
| 838 |
ASSERT_SUCCESS(s_create_response(&response3, 200, headers, AWS_ARRAY_SIZE(headers), request1->response_body)); |
| 839 |
ASSERT_SUCCESS(aws_http_stream_send_response(request3->request_handler, response3)); |
| 840 |
|
| 841 |
|
| 842 |
testing_channel_drain_queued_tasks(&s_tester.testing_channel); |
| 843 |
|
| 844 |
|
| 845 |
const char *expected = "HTTP/1.1 200 OK\r\n" |
| 846 |
"Content-Length: 9\r\n" |
| 847 |
"\r\n" |
| 848 |
"response1"; |
| 849 |
|
| 850 |
ASSERT_SUCCESS(testing_channel_check_written_messages_str(&s_tester.testing_channel, allocator, expected)); |
| 851 |
|
| 852 |
aws_http_message_destroy(response1); |
| 853 |
aws_http_message_destroy(response3); |
| 854 |
|
| 855 |
ASSERT_SUCCESS(s_server_tester_clean_up()); |
| 856 |
|
| 857 |
ASSERT_TRUE(request1->on_complete_cb_count == 1); |
| 858 |
ASSERT_TRUE(request2->on_complete_cb_count == 1); |
| 859 |
ASSERT_TRUE(request3->on_complete_cb_count == 1); |
| 860 |
|
| 861 |
ASSERT_TRUE(request1->on_complete_error_code == AWS_ERROR_SUCCESS); |
| 862 |
|
| 863 |
ASSERT_TRUE(request2->on_complete_error_code == AWS_ERROR_HTTP_CONNECTION_CLOSED); |
| 864 |
ASSERT_TRUE(request3->on_complete_error_code == AWS_ERROR_HTTP_CONNECTION_CLOSED); |
| 865 |
return AWS_OP_SUCCESS; |
| 866 |
} |
| 867 |
|
| 868 |
TEST_CASE(h1_server_send_response_before_request_finished) { |
| 869 |
(void)ctx; |
| 870 |
ASSERT_SUCCESS(s_tester_init(allocator)); |
| 871 |
|
| 872 |
const char *incoming_request_part1 = "PUT /plan.txt HTTP/1.1\r\n" |
| 873 |
"Content-Length: 16\r\n" |
| 874 |
"\r\n" |
| 875 |
"write "; |
| 876 |
ASSERT_SUCCESS(s_send_message_c_str(incoming_request_part1)); |
| 877 |
testing_channel_drain_queued_tasks(&s_tester.testing_channel); |
| 878 |
|
| 879 |
ASSERT_TRUE(s_tester.request_num == 1); |
| 880 |
struct tester_request *request = s_tester.requests; |
| 881 |
struct aws_http_message *response; |
| 882 |
ASSERT_SUCCESS(s_create_response(&response, 200, NULL, 0, NULL)); |
| 883 |
ASSERT_SUCCESS(aws_http_stream_send_response(request->request_handler, response)); |
| 884 |
testing_channel_drain_queued_tasks(&s_tester.testing_channel); |
| 885 |
|
| 886 |
ASSERT_TRUE(request->on_complete_cb_count == 0); |
| 887 |
|
| 888 |
|
| 889 |
const char *expected = "HTTP/1.1 200 OK\r\n" |
| 890 |
"\r\n"; |
| 891 |
|
| 892 |
ASSERT_SUCCESS(testing_channel_check_written_messages_str(&s_tester.testing_channel, allocator, expected)); |
| 893 |
|
| 894 |
const char *incoming_request_part2 = "more tests" |
| 895 |
"GET / HTTP/1.1\r\n" |
| 896 |
"\r\n"; |
| 897 |
|
| 898 |
ASSERT_SUCCESS(s_send_message_c_str(incoming_request_part2)); |
| 899 |
testing_channel_drain_queued_tasks(&s_tester.testing_channel); |
| 900 |
|
| 901 |
|
| 902 |
ASSERT_TRUE(request->on_complete_cb_count == 1); |
| 903 |
ASSERT_TRUE(request->on_complete_error_code == AWS_ERROR_SUCCESS); |
| 904 |
|
| 905 |
ASSERT_SUCCESS(s_check_header(request, 0, "Content-Length", "16")); |
| 906 |
ASSERT_TRUE(aws_byte_cursor_eq_c_str(&request->method, "PUT")); |
| 907 |
ASSERT_TRUE(aws_byte_cursor_eq_c_str(&request->uri, "/plan.txt")); |
| 908 |
ASSERT_TRUE(aws_byte_cursor_eq_c_str(&request->body, "write more tests")); |
| 909 |
|
| 910 |
ASSERT_TRUE(s_tester.request_num == 2); |
| 911 |
request = s_tester.requests + 1; |
| 912 |
ASSERT_TRUE(aws_byte_cursor_eq_c_str(&request->method, "GET")); |
| 913 |
ASSERT_TRUE(aws_byte_cursor_eq_c_str(&request->uri, "/")); |
| 914 |
|
| 915 |
aws_http_message_destroy(response); |
| 916 |
ASSERT_SUCCESS(s_server_tester_clean_up()); |
| 917 |
return AWS_OP_SUCCESS; |
| 918 |
} |
| 919 |
|
| 920 |
|
| 921 |
|
| 922 |
static int s_check_multiple_messages(struct tester *tester, struct aws_byte_cursor expected, size_t *out_num_messages) { |
| 923 |
size_t num_messages = 0; |
| 924 |
|
| 925 |
struct aws_linked_list *msgs = testing_channel_get_written_message_queue(&tester->testing_channel); |
| 926 |
|
| 927 |
size_t progress = 0; |
| 928 |
size_t remaining = expected.len; |
| 929 |
|
| 930 |
while (remaining > 0) { |
| 931 |
|
| 932 |
if (aws_linked_list_empty(msgs)) { |
| 933 |
testing_channel_run_currently_queued_tasks(&tester->testing_channel); |
| 934 |
} |
| 935 |
|
| 936 |
|
| 937 |
ASSERT_TRUE(!aws_linked_list_empty(msgs)); |
| 938 |
struct aws_linked_list_node *node = aws_linked_list_pop_front(msgs); |
| 939 |
ASSERT_TRUE(aws_linked_list_empty(msgs)); |
| 940 |
|
| 941 |
num_messages++; |
| 942 |
|
| 943 |
struct aws_io_message *msg = AWS_CONTAINER_OF(node, struct aws_io_message, queueing_handle); |
| 944 |
|
| 945 |
|
| 946 |
ASSERT_TRUE(msg->message_data.len <= remaining); |
| 947 |
|
| 948 |
size_t comparing = msg->message_data.len < remaining ? msg->message_data.len : remaining; |
| 949 |
|
| 950 |
struct aws_byte_cursor compare_cur = aws_byte_cursor_from_array(expected.ptr + progress, comparing); |
| 951 |
ASSERT_TRUE(aws_byte_cursor_eq_byte_buf(&compare_cur, &msg->message_data)); |
| 952 |
|
| 953 |
aws_mem_release(msg->allocator, msg); |
| 954 |
|
| 955 |
progress += comparing; |
| 956 |
remaining -= comparing; |
| 957 |
} |
| 958 |
|
| 959 |
|
| 960 |
testing_channel_drain_queued_tasks(&tester->testing_channel); |
| 961 |
ASSERT_TRUE(aws_linked_list_empty(msgs)); |
| 962 |
|
| 963 |
*out_num_messages = num_messages; |
| 964 |
return AWS_OP_SUCCESS; |
| 965 |
} |
| 966 |
|
| 967 |
|
| 968 |
TEST_CASE(h1_server_send_response_large_body) { |
| 969 |
|
| 970 |
(void)ctx; |
| 971 |
ASSERT_SUCCESS(s_tester_init(allocator)); |
| 972 |
|
| 973 |
const char *incoming_request = "GET / HTTP/1.1\r\n" |
| 974 |
"\r\n"; |
| 975 |
ASSERT_SUCCESS(s_send_message_c_str(incoming_request)); |
| 976 |
testing_channel_drain_queued_tasks(&s_tester.testing_channel); |
| 977 |
|
| 978 |
ASSERT_TRUE(s_tester.request_num == 1); |
| 979 |
|
| 980 |
struct tester_request *request = s_tester.requests; |
| 981 |
|
| 982 |
|
| 983 |
size_t body_len = 1024 * 1024 * 1; |
| 984 |
struct aws_byte_buf body_buf; |
| 985 |
ASSERT_SUCCESS(aws_byte_buf_init(&body_buf, allocator, body_len)); |
| 986 |
while (body_buf.len < body_len) { |
| 987 |
int r = rand(); |
| 988 |
aws_byte_buf_write_be32(&body_buf, (uint32_t)r); |
| 989 |
} |
| 990 |
|
| 991 |
struct aws_byte_cursor body_src = aws_byte_cursor_from_buf(&body_buf); |
| 992 |
request->response_body = aws_input_stream_new_from_cursor(allocator, &body_src); |
| 993 |
ASSERT_NOT_NULL(request->response_body); |
| 994 |
|
| 995 |
char content_length_value[100]; |
| 996 |
snprintf(content_length_value, sizeof(content_length_value), "%zu", body_len); |
| 997 |
|
| 998 |
struct aws_http_header headers[] = { |
| 999 |
{ |
| 1000 |
.name = aws_byte_cursor_from_c_str("Content-Length"), |
| 1001 |
.value = aws_byte_cursor_from_c_str(content_length_value), |
| 1002 |
}, |
| 1003 |
}; |
| 1004 |
|
| 1005 |
struct aws_http_message *response; |
| 1006 |
ASSERT_SUCCESS(s_create_response(&response, 200, headers, AWS_ARRAY_SIZE(headers), request->response_body)); |
| 1007 |
ASSERT_SUCCESS(aws_http_stream_send_response(request->request_handler, response)); |
| 1008 |
|
| 1009 |
const char *expected_head_fmt = "HTTP/1.1 200 OK\r\n" |
| 1010 |
"Content-Length: %zu\r\n" |
| 1011 |
"\r\n"; |
| 1012 |
|
| 1013 |
char expected_head[1024]; |
| 1014 |
int expected_head_len = snprintf(expected_head, sizeof(expected_head), expected_head_fmt, body_len); |
| 1015 |
|
| 1016 |
struct aws_byte_buf expected_buf; |
| 1017 |
ASSERT_SUCCESS(aws_byte_buf_init(&expected_buf, allocator, body_len + expected_head_len)); |
| 1018 |
ASSERT_TRUE(aws_byte_buf_write(&expected_buf, (uint8_t *)expected_head, expected_head_len)); |
| 1019 |
ASSERT_TRUE(aws_byte_buf_write_from_whole_buffer(&expected_buf, body_buf)); |
| 1020 |
|
| 1021 |
size_t num_io_messages; |
| 1022 |
ASSERT_SUCCESS(s_check_multiple_messages(&s_tester, aws_byte_cursor_from_buf(&expected_buf), &num_io_messages)); |
| 1023 |
|
| 1024 |
ASSERT_TRUE(num_io_messages > 1); |
| 1025 |
|
| 1026 |
ASSERT_SUCCESS(s_server_tester_clean_up()); |
| 1027 |
aws_http_message_destroy(response); |
| 1028 |
aws_byte_buf_clean_up(&body_buf); |
| 1029 |
aws_byte_buf_clean_up(&expected_buf); |
| 1030 |
return AWS_OP_SUCCESS; |
| 1031 |
} |
| 1032 |
|
| 1033 |
|
| 1034 |
TEST_CASE(h1_server_send_response_large_head) { |
| 1035 |
|
| 1036 |
(void)ctx; |
| 1037 |
ASSERT_SUCCESS(s_tester_init(allocator)); |
| 1038 |
|
| 1039 |
const char *incoming_request = "GET / HTTP/1.1\r\n" |
| 1040 |
"\r\n"; |
| 1041 |
ASSERT_SUCCESS(s_send_message_c_str(incoming_request)); |
| 1042 |
testing_channel_drain_queued_tasks(&s_tester.testing_channel); |
| 1043 |
|
| 1044 |
ASSERT_TRUE(s_tester.request_num == 1); |
| 1045 |
|
| 1046 |
struct tester_request *request = s_tester.requests; |
| 1047 |
|
| 1048 |
|
| 1049 |
|
| 1050 |
|
| 1051 |
struct aws_http_header headers[1000]; |
| 1052 |
size_t num_headers = AWS_ARRAY_SIZE(headers); |
| 1053 |
AWS_ZERO_STRUCT(headers); |
| 1054 |
|
| 1055 |
struct aws_byte_buf expected; |
| 1056 |
aws_byte_buf_init(&expected, allocator, num_headers * 128); |
| 1057 |
|
| 1058 |
struct aws_byte_cursor request_line = aws_byte_cursor_from_c_str("HTTP/1.1 200 OK\r\n"); |
| 1059 |
ASSERT_TRUE(aws_byte_buf_write_from_whole_cursor(&expected, request_line)); |
| 1060 |
|
| 1061 |
|
| 1062 |
for (size_t i = 0; i < num_headers; ++i) { |
| 1063 |
struct aws_http_header *header = headers + i; |
| 1064 |
|
| 1065 |
|
| 1066 |
header->name = aws_byte_cursor_from_array(expected.buffer + expected.len, AWS_UUID_STR_LEN - 1); |
| 1067 |
header->value = header->name; |
| 1068 |
|
| 1069 |
struct aws_uuid uuid; |
| 1070 |
ASSERT_SUCCESS(aws_uuid_init(&uuid)); |
| 1071 |
|
| 1072 |
ASSERT_SUCCESS(aws_uuid_to_str(&uuid, &expected)); |
| 1073 |
ASSERT_TRUE(aws_byte_buf_write(&expected, (uint8_t *)": ", 2)); |
| 1074 |
ASSERT_SUCCESS(aws_uuid_to_str(&uuid, &expected)); |
| 1075 |
ASSERT_TRUE(aws_byte_buf_write(&expected, (uint8_t *)"\r\n", 2)); |
| 1076 |
} |
| 1077 |
|
| 1078 |
ASSERT_TRUE(aws_byte_buf_write(&expected, (uint8_t *)"\r\n", 2)); |
| 1079 |
|
| 1080 |
|
| 1081 |
struct aws_http_message *response; |
| 1082 |
ASSERT_SUCCESS(s_create_response(&response, 200, headers, num_headers, NULL)); |
| 1083 |
ASSERT_SUCCESS(aws_http_stream_send_response(request->request_handler, response)); |
| 1084 |
|
| 1085 |
|
| 1086 |
size_t num_io_messages; |
| 1087 |
ASSERT_SUCCESS(s_check_multiple_messages(&s_tester, aws_byte_cursor_from_buf(&expected), &num_io_messages)); |
| 1088 |
|
| 1089 |
ASSERT_TRUE(num_io_messages > 1); |
| 1090 |
|
| 1091 |
ASSERT_SUCCESS(s_server_tester_clean_up()); |
| 1092 |
aws_http_message_destroy(response); |
| 1093 |
aws_byte_buf_clean_up(&expected); |
| 1094 |
return AWS_OP_SUCCESS; |
| 1095 |
} |
| 1096 |
|
| 1097 |
TEST_CASE(h1_server_receive_close_header_ends_connection) { |
| 1098 |
(void)ctx; |
| 1099 |
(void)ctx; |
| 1100 |
ASSERT_SUCCESS(s_tester_init(allocator)); |
| 1101 |
|
| 1102 |
|
| 1103 |
const char *incoming_request = "GET / HTTP/1.1\r\n" |
| 1104 |
"Host: example.com\r\n" |
| 1105 |
"Connection: close\r\n" |
| 1106 |
"\r\n"; |
| 1107 |
ASSERT_SUCCESS(s_send_message_c_str(incoming_request)); |
| 1108 |
testing_channel_drain_queued_tasks(&s_tester.testing_channel); |
| 1109 |
|
| 1110 |
ASSERT_TRUE(s_tester.request_num == 1); |
| 1111 |
|
| 1112 |
|
| 1113 |
struct tester_request request = s_tester.requests[0]; |
| 1114 |
struct aws_http_message *response; |
| 1115 |
ASSERT_SUCCESS(s_create_response(&response, 200, NULL, 0, NULL)); |
| 1116 |
ASSERT_SUCCESS(aws_http_stream_send_response(request.request_handler, response)); |
| 1117 |
testing_channel_drain_queued_tasks(&s_tester.testing_channel); |
| 1118 |
|
| 1119 |
const char *expected = "HTTP/1.1 200 OK\r\n" |
| 1120 |
"\r\n"; |
| 1121 |
|
| 1122 |
ASSERT_SUCCESS(testing_channel_check_written_messages_str(&s_tester.testing_channel, allocator, expected)); |
| 1123 |
|
| 1124 |
|
| 1125 |
ASSERT_INT_EQUALS(AWS_ERROR_SUCCESS, s_tester.requests[0].on_complete_error_code); |
| 1126 |
|
| 1127 |
|
| 1128 |
ASSERT_TRUE(testing_channel_is_shutdown_completed(&s_tester.testing_channel)); |
| 1129 |
ASSERT_INT_EQUALS(AWS_ERROR_SUCCESS, testing_channel_get_shutdown_error_code(&s_tester.testing_channel)); |
| 1130 |
|
| 1131 |
|
| 1132 |
aws_http_message_destroy(response); |
| 1133 |
ASSERT_SUCCESS(s_server_tester_clean_up()); |
| 1134 |
return AWS_OP_SUCCESS; |
| 1135 |
} |
| 1136 |
|
| 1137 |
|
| 1138 |
TEST_CASE(h1_server_receive_close_header_more_requests_illegal) { |
| 1139 |
(void)ctx; |
| 1140 |
(void)ctx; |
| 1141 |
ASSERT_SUCCESS(s_tester_init(allocator)); |
| 1142 |
|
| 1143 |
|
| 1144 |
const char *incoming_request = "GET /first HTTP/1.1\r\n" |
| 1145 |
"Host: example.com\r\n" |
| 1146 |
"Connection: close\r\n" |
| 1147 |
"\r\n" |
| 1148 |
"GET /second HTTP/1.1\r\n" |
| 1149 |
"Host: example.com\r\n" |
| 1150 |
"\r\n"; |
| 1151 |
ASSERT_SUCCESS(s_send_message_c_str(incoming_request)); |
| 1152 |
testing_channel_drain_queued_tasks(&s_tester.testing_channel); |
| 1153 |
|
| 1154 |
|
| 1155 |
ASSERT_TRUE(s_tester.request_num == 1); |
| 1156 |
struct tester_request *request = &s_tester.requests[0]; |
| 1157 |
ASSERT_TRUE(aws_byte_cursor_eq_c_str(&request->uri, "/first")); |
| 1158 |
|
| 1159 |
|
| 1160 |
|
| 1161 |
|
| 1162 |
|
| 1163 |
|
| 1164 |
ASSERT_SUCCESS(s_server_tester_clean_up()); |
| 1165 |
return AWS_OP_SUCCESS; |
| 1166 |
} |
| 1167 |
|
| 1168 |
TEST_CASE(h1_server_send_close_header_ends_connection) { |
| 1169 |
(void)ctx; |
| 1170 |
(void)ctx; |
| 1171 |
ASSERT_SUCCESS(s_tester_init(allocator)); |
| 1172 |
|
| 1173 |
|
| 1174 |
const char *incoming_request = "GET / HTTP/1.1\r\n" |
| 1175 |
"Host: example.com\r\n" |
| 1176 |
"\r\n"; |
| 1177 |
ASSERT_SUCCESS(s_send_message_c_str(incoming_request)); |
| 1178 |
testing_channel_drain_queued_tasks(&s_tester.testing_channel); |
| 1179 |
|
| 1180 |
ASSERT_TRUE(s_tester.request_num == 1); |
| 1181 |
|
| 1182 |
struct tester_request request = s_tester.requests[0]; |
| 1183 |
|
| 1184 |
|
| 1185 |
struct aws_http_message *response; |
| 1186 |
struct aws_http_header headers[] = { |
| 1187 |
{ |
| 1188 |
.name = aws_byte_cursor_from_c_str("Connection"), |
| 1189 |
.value = aws_byte_cursor_from_c_str("close"), |
| 1190 |
}, |
| 1191 |
}; |
| 1192 |
ASSERT_SUCCESS(s_create_response(&response, 200, headers, AWS_ARRAY_SIZE(headers), NULL)); |
| 1193 |
ASSERT_SUCCESS(aws_http_stream_send_response(request.request_handler, response)); |
| 1194 |
testing_channel_drain_queued_tasks(&s_tester.testing_channel); |
| 1195 |
|
| 1196 |
const char *expected = "HTTP/1.1 200 OK\r\n" |
| 1197 |
"Connection: close\r\n" |
| 1198 |
"\r\n"; |
| 1199 |
|
| 1200 |
ASSERT_SUCCESS(testing_channel_check_written_messages_str(&s_tester.testing_channel, allocator, expected)); |
| 1201 |
|
| 1202 |
|
| 1203 |
ASSERT_INT_EQUALS(AWS_ERROR_SUCCESS, s_tester.requests[0].on_complete_error_code); |
| 1204 |
|
| 1205 |
|
| 1206 |
ASSERT_TRUE(testing_channel_is_shutdown_completed(&s_tester.testing_channel)); |
| 1207 |
ASSERT_INT_EQUALS(AWS_ERROR_SUCCESS, testing_channel_get_shutdown_error_code(&s_tester.testing_channel)); |
| 1208 |
|
| 1209 |
|
| 1210 |
aws_http_message_destroy(response); |
| 1211 |
ASSERT_SUCCESS(s_server_tester_clean_up()); |
| 1212 |
return AWS_OP_SUCCESS; |
| 1213 |
} |
| 1214 |
|
| 1215 |
|
| 1216 |
|
| 1217 |
|
| 1218 |
TEST_CASE(h1_server_send_close_header_with_pipelining) { |
| 1219 |
(void)ctx; |
| 1220 |
(void)ctx; |
| 1221 |
ASSERT_SUCCESS(s_tester_init(allocator)); |
| 1222 |
|
| 1223 |
|
| 1224 |
const char *incoming_request = "GET /first HTTP/1.1\r\n" |
| 1225 |
"Host: example.com\r\n" |
| 1226 |
"\r\n" |
| 1227 |
"GET /second HTTP/1.1\r\n" |
| 1228 |
"Host: example.com\r\n" |
| 1229 |
"\r\n" |
| 1230 |
"GET /third HTTP/1.1\r\n" |
| 1231 |
"Host: example.com\r\n" |
| 1232 |
"\r\n"; |
| 1233 |
ASSERT_SUCCESS(s_send_message_c_str(incoming_request)); |
| 1234 |
testing_channel_drain_queued_tasks(&s_tester.testing_channel); |
| 1235 |
|
| 1236 |
ASSERT_TRUE(s_tester.request_num == 3); |
| 1237 |
|
| 1238 |
|
| 1239 |
|
| 1240 |
struct aws_http_message *responses[3]; |
| 1241 |
struct aws_http_header close_headers[] = { |
| 1242 |
{ |
| 1243 |
.name = aws_byte_cursor_from_c_str("Connection"), |
| 1244 |
.value = aws_byte_cursor_from_c_str("close"), |
| 1245 |
}, |
| 1246 |
}; |
| 1247 |
|
| 1248 |
|
| 1249 |
|
| 1250 |
|
| 1251 |
for (int i = 2; i >= 0; --i) { |
| 1252 |
struct aws_http_header *headers = NULL; |
| 1253 |
size_t num_headers = 0; |
| 1254 |
if (i == 1) { |
| 1255 |
headers = close_headers; |
| 1256 |
num_headers = AWS_ARRAY_SIZE(close_headers); |
| 1257 |
} |
| 1258 |
ASSERT_SUCCESS(s_create_response(&responses[i], 200, headers, num_headers, NULL)); |
| 1259 |
ASSERT_SUCCESS(aws_http_stream_send_response(s_tester.requests[i].request_handler, responses[i])); |
| 1260 |
} |
| 1261 |
testing_channel_drain_queued_tasks(&s_tester.testing_channel); |
| 1262 |
|
| 1263 |
|
| 1264 |
|
| 1265 |
const char *expected = "HTTP/1.1 200 OK\r\n" |
| 1266 |
"\r\n" |
| 1267 |
"HTTP/1.1 200 OK\r\n" |
| 1268 |
"Connection: close\r\n" |
| 1269 |
"\r\n"; |
| 1270 |
|
| 1271 |
ASSERT_SUCCESS(testing_channel_check_written_messages_str(&s_tester.testing_channel, allocator, expected)); |
| 1272 |
|
| 1273 |
|
| 1274 |
ASSERT_INT_EQUALS(AWS_ERROR_SUCCESS, s_tester.requests[0].on_complete_error_code); |
| 1275 |
ASSERT_INT_EQUALS(AWS_ERROR_SUCCESS, s_tester.requests[1].on_complete_error_code); |
| 1276 |
ASSERT_INT_EQUALS(AWS_ERROR_HTTP_CONNECTION_CLOSED, s_tester.requests[2].on_complete_error_code); |
| 1277 |
|
| 1278 |
|
| 1279 |
ASSERT_TRUE(testing_channel_is_shutdown_completed(&s_tester.testing_channel)); |
| 1280 |
|
| 1281 |
|
| 1282 |
for (size_t i = 0; i < 3; ++i) { |
| 1283 |
aws_http_message_destroy(responses[i]); |
| 1284 |
} |
| 1285 |
ASSERT_SUCCESS(s_server_tester_clean_up()); |
| 1286 |
return AWS_OP_SUCCESS; |
| 1287 |
} |
| 1288 |
|
| 1289 |
|
| 1290 |
|
| 1291 |
|
| 1292 |
enum request_handler_callback { |
| 1293 |
REQUEST_HANDLER_CALLBACK_INCOMING_REQUEST, |
| 1294 |
REQUEST_HANDLER_CALLBACK_INCOMING_HEADERS, |
| 1295 |
REQUEST_HANDLER_CALLBACK_INCOMING_HEADERS_DONE, |
| 1296 |
REQUEST_HANDLER_CALLBACK_INCOMING_BODY, |
| 1297 |
REQUEST_HANDLER_CALLBACK_INCOMING_REQUEST_DONE, |
| 1298 |
REQUEST_HANDLER_CALLBACK_OUTGOING_BODY, |
| 1299 |
REQUEST_HANDLER_CALLBACK_COMPLETE, |
| 1300 |
REQUEST_HANDLER_CALLBACK_COUNT, |
| 1301 |
}; |
| 1302 |
|
| 1303 |
static const int ERROR_FROM_CALLBACK_ERROR_CODE = (int)0xBEEFCAFE; |
| 1304 |
|
| 1305 |
struct error_from_callback_tester { |
| 1306 |
struct aws_input_stream base; |
| 1307 |
enum request_handler_callback error_at; |
| 1308 |
int callback_counts[REQUEST_HANDLER_CALLBACK_COUNT]; |
| 1309 |
bool has_errored; |
| 1310 |
|
| 1311 |
struct aws_allocator *alloc; |
| 1312 |
struct aws_logger logger; |
| 1313 |
|
| 1314 |
struct aws_http_connection *server_connection; |
| 1315 |
struct testing_channel testing_channel; |
| 1316 |
|
| 1317 |
struct tester_request requests[100]; |
| 1318 |
|
| 1319 |
struct aws_stream_status outgoing_body_status; |
| 1320 |
int request_num; |
| 1321 |
int on_complete_error_code; |
| 1322 |
}; |
| 1323 |
|
| 1324 |
static int s_error_from_callback_common( |
| 1325 |
struct error_from_callback_tester *error_tester, |
| 1326 |
enum request_handler_callback current_callback) { |
| 1327 |
|
| 1328 |
error_tester->callback_counts[current_callback]++; |
| 1329 |
|
| 1330 |
|
| 1331 |
AWS_FATAL_ASSERT(!error_tester->has_errored); |
| 1332 |
AWS_FATAL_ASSERT(current_callback <= error_tester->error_at); |
| 1333 |
if (current_callback == error_tester->error_at) { |
| 1334 |
error_tester->has_errored = true; |
| 1335 |
return aws_raise_error(ERROR_FROM_CALLBACK_ERROR_CODE); |
| 1336 |
} |
| 1337 |
|
| 1338 |
return AWS_OP_SUCCESS; |
| 1339 |
} |
| 1340 |
|
| 1341 |
static int s_error_from_outgoing_body_read(struct aws_input_stream *body, struct aws_byte_buf *dest) { |
| 1342 |
|
| 1343 |
(void)dest; |
| 1344 |
|
| 1345 |
struct error_from_callback_tester *error_tester = AWS_CONTAINER_OF(body, struct error_from_callback_tester, base); |
| 1346 |
ASSERT_SUCCESS(s_error_from_callback_common(error_tester, REQUEST_HANDLER_CALLBACK_OUTGOING_BODY)); |
| 1347 |
|
| 1348 |
|
| 1349 |
ASSERT_TRUE(aws_byte_buf_write(dest, (const uint8_t *)"abcd", 4)); |
| 1350 |
error_tester->outgoing_body_status.is_end_of_stream = true; |
| 1351 |
return AWS_OP_SUCCESS; |
| 1352 |
} |
| 1353 |
|
| 1354 |
static int s_error_from_outgoing_body_get_status(struct aws_input_stream *body, struct aws_stream_status *status) { |
| 1355 |
struct error_from_callback_tester *error_tester = AWS_CONTAINER_OF(body, struct error_from_callback_tester, base); |
| 1356 |
*status = error_tester->outgoing_body_status; |
| 1357 |
return AWS_OP_SUCCESS; |
| 1358 |
} |
| 1359 |
|
| 1360 |
static void s_error_from_outgoing_body_destroy(struct aws_input_stream *body) { |
| 1361 |
(void)body; |
| 1362 |
} |
| 1363 |
|
| 1364 |
static struct aws_input_stream_vtable s_error_from_outgoing_body_vtable = { |
| 1365 |
.seek = NULL, |
| 1366 |
.read = s_error_from_outgoing_body_read, |
| 1367 |
.get_status = s_error_from_outgoing_body_get_status, |
| 1368 |
.get_length = NULL, |
| 1369 |
}; |
| 1370 |
|
| 1371 |
static int s_error_from_incoming_headers( |
| 1372 |
struct aws_http_stream *stream, |
| 1373 |
enum aws_http_header_block header_block, |
| 1374 |
const struct aws_http_header *header_array, |
| 1375 |
size_t num_headers, |
| 1376 |
void *user_data) { |
| 1377 |
|
| 1378 |
(void)stream; |
| 1379 |
(void)header_block; |
| 1380 |
(void)header_array; |
| 1381 |
(void)num_headers; |
| 1382 |
return s_error_from_callback_common(user_data, REQUEST_HANDLER_CALLBACK_INCOMING_HEADERS); |
| 1383 |
} |
| 1384 |
|
| 1385 |
static int s_error_from_incoming_headers_done( |
| 1386 |
struct aws_http_stream *stream, |
| 1387 |
enum aws_http_header_block header_block, |
| 1388 |
void *user_data) { |
| 1389 |
(void)stream; |
| 1390 |
(void)header_block; |
| 1391 |
return s_error_from_callback_common(user_data, REQUEST_HANDLER_CALLBACK_INCOMING_HEADERS_DONE); |
| 1392 |
} |
| 1393 |
|
| 1394 |
static int s_error_from_incoming_body( |
| 1395 |
struct aws_http_stream *stream, |
| 1396 |
const struct aws_byte_cursor *data, |
| 1397 |
void *user_data) { |
| 1398 |
|
| 1399 |
(void)stream; |
| 1400 |
(void)data; |
| 1401 |
return s_error_from_callback_common(user_data, REQUEST_HANDLER_CALLBACK_INCOMING_BODY); |
| 1402 |
} |
| 1403 |
|
| 1404 |
static int s_error_from_incoming_request_done(struct aws_http_stream *stream, void *user_data) { |
| 1405 |
(void)stream; |
| 1406 |
return s_error_from_callback_common(user_data, REQUEST_HANDLER_CALLBACK_INCOMING_REQUEST_DONE); |
| 1407 |
} |
| 1408 |
|
| 1409 |
static void s_error_tester_on_stream_complete(struct aws_http_stream *stream, int error_code, void *user_data) { |
| 1410 |
(void)stream; |
| 1411 |
struct error_from_callback_tester *error_tester = user_data; |
| 1412 |
error_tester->callback_counts[REQUEST_HANDLER_CALLBACK_COMPLETE]++; |
| 1413 |
error_tester->on_complete_error_code = error_code; |
| 1414 |
} |
| 1415 |
|
| 1416 |
static struct aws_http_stream *s_tester_close_on_incoming_request( |
| 1417 |
struct aws_http_connection *connection, |
| 1418 |
void *user_data) { |
| 1419 |
|
| 1420 |
struct aws_http_request_handler_options options = AWS_HTTP_REQUEST_HANDLER_OPTIONS_INIT; |
| 1421 |
struct error_from_callback_tester *tester = user_data; |
| 1422 |
|
| 1423 |
int index = tester->request_num; |
| 1424 |
|
| 1425 |
tester->requests[index].num_headers = 0; |
| 1426 |
tester->requests[index].header_done = false; |
| 1427 |
aws_byte_buf_init(&tester->requests[index].storage, tester->alloc, 1024 * 1024 * 1); |
| 1428 |
|
| 1429 |
options.server_connection = connection; |
| 1430 |
options.user_data = tester; |
| 1431 |
options.on_request_headers = s_error_from_incoming_headers; |
| 1432 |
options.on_request_header_block_done = s_error_from_incoming_headers_done; |
| 1433 |
options.on_request_body = s_error_from_incoming_body; |
| 1434 |
options.on_request_done = s_error_from_incoming_request_done; |
| 1435 |
options.on_complete = s_error_tester_on_stream_complete; |
| 1436 |
|
| 1437 |
struct aws_http_stream *stream = aws_http_stream_new_server_request_handler(&options); |
| 1438 |
AWS_FATAL_ASSERT(stream); |
| 1439 |
tester->requests[index].request_handler = stream; |
| 1440 |
|
| 1441 |
tester->request_num++; |
| 1442 |
|
| 1443 |
int err = s_error_from_callback_common(tester, REQUEST_HANDLER_CALLBACK_INCOMING_REQUEST); |
| 1444 |
if (err) { |
| 1445 |
return NULL; |
| 1446 |
} |
| 1447 |
|
| 1448 |
return stream; |
| 1449 |
} |
| 1450 |
|
| 1451 |
static int s_error_tester_init(struct aws_allocator *alloc, struct error_from_callback_tester *tester) { |
| 1452 |
|
| 1453 |
aws_http_library_init(alloc); |
| 1454 |
|
| 1455 |
tester->alloc = alloc; |
| 1456 |
s_tester.alloc = alloc; |
| 1457 |
|
| 1458 |
tester->request_num = 0; |
| 1459 |
tester->outgoing_body_status.is_valid = true; |
| 1460 |
|
| 1461 |
struct aws_logger_standard_options logger_options = { |
| 1462 |
.level = AWS_LOG_LEVEL_TRACE, |
| 1463 |
.file = stderr, |
| 1464 |
}; |
| 1465 |
ASSERT_SUCCESS(aws_logger_init_standard(&tester->logger, tester->alloc, &logger_options)); |
| 1466 |
aws_logger_set(&tester->logger); |
| 1467 |
|
| 1468 |
struct aws_testing_channel_options test_channel_options = {.clock_fn = aws_high_res_clock_get_ticks}; |
| 1469 |
ASSERT_SUCCESS(testing_channel_init(&tester->testing_channel, alloc, &test_channel_options)); |
| 1470 |
|
| 1471 |
struct aws_http1_connection_options http1_options; |
| 1472 |
AWS_ZERO_STRUCT(http1_options); |
| 1473 |
tester->server_connection = aws_http_connection_new_http1_1_server(alloc, true, SIZE_MAX, &http1_options); |
| 1474 |
ASSERT_NOT_NULL(tester->server_connection); |
| 1475 |
struct aws_http_server_connection_options options = AWS_HTTP_SERVER_CONNECTION_OPTIONS_INIT; |
| 1476 |
options.connection_user_data = tester; |
| 1477 |
options.on_incoming_request = s_tester_close_on_incoming_request; |
| 1478 |
|
| 1479 |
ASSERT_SUCCESS(aws_http_connection_configure_server(tester->server_connection, &options)); |
| 1480 |
|
| 1481 |
struct aws_channel_slot *slot = aws_channel_slot_new(tester->testing_channel.channel); |
| 1482 |
ASSERT_NOT_NULL(slot); |
| 1483 |
ASSERT_SUCCESS(aws_channel_slot_insert_end(tester->testing_channel.channel, slot)); |
| 1484 |
ASSERT_SUCCESS(aws_channel_slot_set_handler(slot, &tester->server_connection->channel_handler)); |
| 1485 |
tester->server_connection->vtable->on_channel_handler_installed(&tester->server_connection->channel_handler, slot); |
| 1486 |
|
| 1487 |
testing_channel_drain_queued_tasks(&tester->testing_channel); |
| 1488 |
|
| 1489 |
return AWS_OP_SUCCESS; |
| 1490 |
} |
| 1491 |
|
| 1492 |
static int s_server_close_request_clean_up(struct error_from_callback_tester *tester) { |
| 1493 |
for (int i = 0; i < tester->request_num; i++) { |
| 1494 |
aws_http_stream_release(tester->requests[i].request_handler); |
| 1495 |
aws_byte_buf_clean_up(&tester->requests[i].storage); |
| 1496 |
} |
| 1497 |
return AWS_OP_SUCCESS; |
| 1498 |
} |
| 1499 |
|
| 1500 |
static int s_server_error_tester_clean_up(struct error_from_callback_tester *tester) { |
| 1501 |
s_server_close_request_clean_up(tester); |
| 1502 |
aws_http_connection_release(tester->server_connection); |
| 1503 |
ASSERT_SUCCESS(testing_channel_clean_up(&tester->testing_channel)); |
| 1504 |
aws_http_library_clean_up(); |
| 1505 |
aws_logger_clean_up(&tester->logger); |
| 1506 |
|
| 1507 |
return AWS_OP_SUCCESS; |
| 1508 |
} |
| 1509 |
|
| 1510 |
static int s_send_message_cursor_close(struct aws_byte_cursor data, struct error_from_callback_tester *tester) { |
| 1511 |
|
| 1512 |
struct aws_io_message *msg = aws_channel_acquire_message_from_pool( |
| 1513 |
tester->testing_channel.channel, AWS_IO_MESSAGE_APPLICATION_DATA, data.len); |
| 1514 |
ASSERT_NOT_NULL(msg); |
| 1515 |
|
| 1516 |
ASSERT_TRUE(aws_byte_buf_write_from_whole_cursor(&msg->message_data, data)); |
| 1517 |
|
| 1518 |
ASSERT_SUCCESS(testing_channel_push_read_message(&tester->testing_channel, msg)); |
| 1519 |
|
| 1520 |
return AWS_OP_SUCCESS; |
| 1521 |
} |
| 1522 |
|
| 1523 |
static int s_test_error_from_callback(struct aws_allocator *allocator, enum request_handler_callback error_at) { |
| 1524 |
|
| 1525 |
struct error_from_callback_tester error_tester; |
| 1526 |
|
| 1527 |
AWS_ZERO_STRUCT(error_tester); |
| 1528 |
error_tester.error_at = error_at; |
| 1529 |
|
| 1530 |
ASSERT_SUCCESS(s_error_tester_init(allocator, &error_tester)); |
| 1531 |
|
| 1532 |
|
| 1533 |
const char *incoming_request = "POST / HTTP/1.1\r\n" |
| 1534 |
"Transfer-Encoding: chunked\r\n" |
| 1535 |
"\r\n" |
| 1536 |
"3\r\n" |
| 1537 |
"two\r\n" |
| 1538 |
"6\r\n" |
| 1539 |
"chunks\r\n" |
| 1540 |
"0\r\n" |
| 1541 |
"\r\n"; |
| 1542 |
ASSERT_SUCCESS(s_send_message_cursor_close(aws_byte_cursor_from_c_str(incoming_request), &error_tester)); |
| 1543 |
testing_channel_drain_queued_tasks(&error_tester.testing_channel); |
| 1544 |
|
| 1545 |
ASSERT_TRUE(error_tester.request_num == 1); |
| 1546 |
struct tester_request *request = error_tester.requests; |
| 1547 |
|
| 1548 |
|
| 1549 |
struct aws_http_header headers[] = { |
| 1550 |
{ |
| 1551 |
.name = aws_byte_cursor_from_c_str("Content-Length"), |
| 1552 |
.value = aws_byte_cursor_from_c_str("4"), |
| 1553 |
}, |
| 1554 |
}; |
| 1555 |
|
| 1556 |
error_tester.base.vtable = &s_error_from_outgoing_body_vtable; |
| 1557 |
aws_ref_count_init( |
| 1558 |
&error_tester.base.ref_count, |
| 1559 |
&error_tester, |
| 1560 |
(aws_simple_completion_callback *)s_error_from_outgoing_body_destroy); |
| 1561 |
|
| 1562 |
struct aws_input_stream *error_from_outgoing_body_stream = &error_tester.base; |
| 1563 |
|
| 1564 |
struct aws_http_message *response; |
| 1565 |
ASSERT_SUCCESS( |
| 1566 |
s_create_response(&response, 200, headers, AWS_ARRAY_SIZE(headers), error_from_outgoing_body_stream)); |
| 1567 |
|
| 1568 |
|
| 1569 |
aws_http_stream_send_response(request->request_handler, response); |
| 1570 |
|
| 1571 |
testing_channel_drain_queued_tasks(&error_tester.testing_channel); |
| 1572 |
|
| 1573 |
for (int i = 0; i < REQUEST_HANDLER_CALLBACK_COMPLETE; ++i) { |
| 1574 |
if (i <= error_at) { |
| 1575 |
ASSERT_TRUE(error_tester.callback_counts[i] > 0); |
| 1576 |
} else { |
| 1577 |
ASSERT_INT_EQUALS(0, error_tester.callback_counts[i]); |
| 1578 |
} |
| 1579 |
} |
| 1580 |
|
| 1581 |
|
| 1582 |
ASSERT_INT_EQUALS(1, error_tester.callback_counts[REQUEST_HANDLER_CALLBACK_COMPLETE]); |
| 1583 |
ASSERT_INT_EQUALS(ERROR_FROM_CALLBACK_ERROR_CODE, error_tester.on_complete_error_code); |
| 1584 |
|
| 1585 |
aws_http_message_destroy(response); |
| 1586 |
aws_input_stream_release(error_from_outgoing_body_stream); |
| 1587 |
ASSERT_SUCCESS(s_server_error_tester_clean_up(&error_tester)); |
| 1588 |
return AWS_OP_SUCCESS; |
| 1589 |
} |
| 1590 |
|
| 1591 |
TEST_CASE(h1_server_close_before_message_is_sent) { |
| 1592 |
(void)ctx; |
| 1593 |
|
| 1594 |
struct error_from_callback_tester error_tester; |
| 1595 |
|
| 1596 |
AWS_ZERO_STRUCT(error_tester); |
| 1597 |
|
| 1598 |
ASSERT_SUCCESS(s_error_tester_init(allocator, &error_tester)); |
| 1599 |
|
| 1600 |
|
| 1601 |
aws_http_connection_close(error_tester.server_connection); |
| 1602 |
testing_channel_drain_queued_tasks(&error_tester.testing_channel); |
| 1603 |
|
| 1604 |
|
| 1605 |
const char *incoming_request = "POST / HTTP/1.1\r\n" |
| 1606 |
"Transfer-Encoding: chunked\r\n" |
| 1607 |
"\r\n" |
| 1608 |
"3\r\n" |
| 1609 |
"two\r\n" |
| 1610 |
"6\r\n" |
| 1611 |
"chunks\r\n" |
| 1612 |
"0\r\n" |
| 1613 |
"\r\n"; |
| 1614 |
ASSERT_SUCCESS(s_send_message_cursor_close(aws_byte_cursor_from_c_str(incoming_request), &error_tester)); |
| 1615 |
testing_channel_drain_queued_tasks(&error_tester.testing_channel); |
| 1616 |
|
| 1617 |
|
| 1618 |
ASSERT_TRUE(error_tester.request_num == 0); |
| 1619 |
|
| 1620 |
|
| 1621 |
for (int i = 0; i < REQUEST_HANDLER_CALLBACK_COMPLETE; ++i) { |
| 1622 |
ASSERT_INT_EQUALS(0, error_tester.callback_counts[i]); |
| 1623 |
} |
| 1624 |
|
| 1625 |
ASSERT_SUCCESS(s_server_error_tester_clean_up(&error_tester)); |
| 1626 |
return AWS_OP_SUCCESS; |
| 1627 |
} |
| 1628 |
|
| 1629 |
TEST_CASE(h1_server_error_from_incoming_request_callback_stops_decoder) { |
| 1630 |
(void)ctx; |
| 1631 |
ASSERT_SUCCESS(s_test_error_from_callback(allocator, REQUEST_HANDLER_CALLBACK_INCOMING_REQUEST)); |
| 1632 |
return AWS_OP_SUCCESS; |
| 1633 |
} |
| 1634 |
|
| 1635 |
TEST_CASE(h1_server_error_from_incoming_headers_callback_stops_decoder) { |
| 1636 |
(void)ctx; |
| 1637 |
ASSERT_SUCCESS(s_test_error_from_callback(allocator, REQUEST_HANDLER_CALLBACK_INCOMING_HEADERS)); |
| 1638 |
return AWS_OP_SUCCESS; |
| 1639 |
} |
| 1640 |
|
| 1641 |
TEST_CASE(h1_server_error_from_incoming_headers_done_callback_stops_decoder) { |
| 1642 |
(void)ctx; |
| 1643 |
ASSERT_SUCCESS(s_test_error_from_callback(allocator, REQUEST_HANDLER_CALLBACK_INCOMING_HEADERS_DONE)); |
| 1644 |
return AWS_OP_SUCCESS; |
| 1645 |
} |
| 1646 |
|
| 1647 |
TEST_CASE(h1_server_error_from_incoming_body_callback_stops_decoder) { |
| 1648 |
(void)ctx; |
| 1649 |
ASSERT_SUCCESS(s_test_error_from_callback(allocator, REQUEST_HANDLER_CALLBACK_INCOMING_BODY)); |
| 1650 |
return AWS_OP_SUCCESS; |
| 1651 |
} |
| 1652 |
|
| 1653 |
TEST_CASE(h1_server_error_from_incoming_request_done_callback_stops_decoder) { |
| 1654 |
(void)ctx; |
| 1655 |
ASSERT_SUCCESS(s_test_error_from_callback(allocator, REQUEST_HANDLER_CALLBACK_INCOMING_REQUEST_DONE)); |
| 1656 |
return AWS_OP_SUCCESS; |
| 1657 |
} |
| 1658 |
TEST_CASE(h1_server_error_from_outgoing_body_callback_stops_sending) { |
| 1659 |
(void)ctx; |
| 1660 |
ASSERT_SUCCESS(s_test_error_from_callback(allocator, REQUEST_HANDLER_CALLBACK_OUTGOING_BODY)); |
| 1661 |
return AWS_OP_SUCCESS; |
| 1662 |
} |
| 1663 |
|
| 1664 |
|
| 1665 |
|
| 1666 |
TEST_CASE(h1_server_close_from_off_thread_makes_not_open) { |
| 1667 |
(void)ctx; |
| 1668 |
ASSERT_SUCCESS(s_tester_init(allocator)); |
| 1669 |
|
| 1670 |
testing_channel_set_is_on_users_thread(&s_tester.testing_channel, false); |
| 1671 |
|
| 1672 |
ASSERT_TRUE(aws_http_connection_is_open(s_tester.server_connection)); |
| 1673 |
aws_http_connection_close(s_tester.server_connection); |
| 1674 |
ASSERT_FALSE(aws_http_connection_is_open(s_tester.server_connection)); |
| 1675 |
|
| 1676 |
testing_channel_set_is_on_users_thread(&s_tester.testing_channel, true); |
| 1677 |
|
| 1678 |
ASSERT_SUCCESS(s_server_tester_clean_up()); |
| 1679 |
return AWS_OP_SUCCESS; |
| 1680 |
} |
| 1681 |
|
| 1682 |
TEST_CASE(h1_server_close_from_on_thread_makes_not_open) { |
| 1683 |
(void)ctx; |
| 1684 |
ASSERT_SUCCESS(s_tester_init(allocator)); |
| 1685 |
|
| 1686 |
testing_channel_set_is_on_users_thread(&s_tester.testing_channel, false); |
| 1687 |
ASSERT_TRUE(aws_http_connection_is_open(s_tester.server_connection)); |
| 1688 |
|
| 1689 |
testing_channel_set_is_on_users_thread(&s_tester.testing_channel, true); |
| 1690 |
aws_http_connection_close(s_tester.server_connection); |
| 1691 |
|
| 1692 |
testing_channel_set_is_on_users_thread(&s_tester.testing_channel, false); |
| 1693 |
ASSERT_FALSE(aws_http_connection_is_open(s_tester.server_connection)); |
| 1694 |
|
| 1695 |
testing_channel_set_is_on_users_thread(&s_tester.testing_channel, true); |
| 1696 |
|
| 1697 |
ASSERT_SUCCESS(s_server_tester_clean_up()); |
| 1698 |
return AWS_OP_SUCCESS; |
| 1699 |
} |