@@ -59,11 +59,13 @@ struct elasticurl_ctx {
59
59
size_t header_line_count ;
60
60
FILE * input_file ;
61
61
struct aws_input_stream * input_body ;
62
+ struct aws_http_message * request ;
63
+ struct aws_http_connection * connection ;
62
64
const char * signing_library_path ;
63
65
struct aws_shared_library signing_library ;
64
66
const char * signing_function_name ;
65
67
struct aws_hash_table signing_context ;
66
- aws_transform_http_request_fn * signing_function ;
68
+ aws_http_message_transform_fn * signing_function ;
67
69
bool include_headers ;
68
70
bool insecure ;
69
71
FILE * output ;
@@ -320,48 +322,17 @@ static void s_parse_options(int argc, char **argv, struct elasticurl_ctx *ctx) {
320
322
}
321
323
}
322
324
323
- static void s_on_incoming_body_fn (
324
- struct aws_http_stream * stream ,
325
- const struct aws_byte_cursor * data ,
326
- /* NOLINTNEXTLINE(readability-non-const-parameter) */
327
- size_t * out_window_update_size ,
328
- void * user_data ) {
325
+ static int s_on_incoming_body_fn (struct aws_http_stream * stream , const struct aws_byte_cursor * data , void * user_data ) {
329
326
330
327
(void )stream ;
331
- (void )out_window_update_size ;
332
328
struct elasticurl_ctx * app_ctx = user_data ;
333
329
334
330
fwrite (data -> ptr , 1 , data -> len , app_ctx -> output );
335
- }
336
-
337
- enum aws_http_outgoing_body_state s_stream_outgoing_body_fn (
338
- struct aws_http_stream * stream ,
339
- struct aws_byte_buf * buf ,
340
- void * user_data ) {
341
- (void )stream ;
342
- struct elasticurl_ctx * app_ctx = user_data ;
343
-
344
- if (!app_ctx -> input_body ) {
345
- return AWS_HTTP_OUTGOING_BODY_DONE ;
346
- }
347
331
348
- struct aws_stream_status status ;
349
- if (aws_input_stream_get_status (app_ctx -> input_body , & status )) {
350
- return AWS_HTTP_OUTGOING_BODY_DONE ;
351
- }
352
-
353
- if (status .is_end_of_stream || !status .is_valid ) {
354
- return AWS_HTTP_OUTGOING_BODY_DONE ;
355
- }
356
-
357
- if (aws_input_stream_read (app_ctx -> input_body , buf )) {
358
- return AWS_HTTP_OUTGOING_BODY_DONE ;
359
- }
360
-
361
- return AWS_HTTP_OUTGOING_BODY_IN_PROGRESS ;
332
+ return AWS_OP_SUCCESS ;
362
333
}
363
334
364
- static void s_on_incoming_headers_fn (
335
+ static int s_on_incoming_headers_fn (
365
336
struct aws_http_stream * stream ,
366
337
const struct aws_http_header * header_array ,
367
338
size_t num_headers ,
@@ -385,12 +356,16 @@ static void s_on_incoming_headers_fn(
385
356
fprintf (stdout , "\n" );
386
357
}
387
358
}
359
+
360
+ return AWS_OP_SUCCESS ;
388
361
}
389
362
390
- static void s_on_incoming_header_block_done_fn (struct aws_http_stream * stream , bool has_body , void * user_data ) {
363
+ static int s_on_incoming_header_block_done_fn (struct aws_http_stream * stream , bool has_body , void * user_data ) {
391
364
(void )stream ;
392
365
(void )has_body ;
393
366
(void )user_data ;
367
+
368
+ return AWS_OP_SUCCESS ;
394
369
}
395
370
396
371
static void s_on_stream_complete_fn (struct aws_http_stream * stream , int error_code , void * user_data ) {
@@ -399,28 +374,26 @@ static void s_on_stream_complete_fn(struct aws_http_stream *stream, int error_co
399
374
aws_http_stream_release (stream );
400
375
}
401
376
402
- static struct aws_http_request * s_build_http_request (struct elasticurl_ctx * app_ctx ) {
403
- struct aws_http_request * request = aws_http_request_new (app_ctx -> allocator );
377
+ static struct aws_http_message * s_build_http_request (struct elasticurl_ctx * app_ctx ) {
378
+ struct aws_http_message * request = aws_http_message_new_request (app_ctx -> allocator );
404
379
if (request == NULL ) {
405
380
fprintf (stderr , "failed to allocate request\n" );
406
381
exit (1 );
407
382
}
408
383
409
- aws_http_request_set_method (request , aws_byte_cursor_from_c_str (app_ctx -> verb ));
410
- aws_http_request_set_path (request , app_ctx -> uri .path_and_query );
411
- aws_http_request_set_body_stream (request , app_ctx -> input_body );
412
-
384
+ aws_http_message_set_request_method (request , aws_byte_cursor_from_c_str (app_ctx -> verb ));
385
+ aws_http_message_set_request_path (request , app_ctx -> uri .path_and_query );
413
386
struct aws_http_header accept_header = {.name = aws_byte_cursor_from_c_str ("accept" ),
414
387
.value = aws_byte_cursor_from_c_str ("*/*" )};
415
- aws_http_request_add_header (request , accept_header );
388
+ aws_http_message_add_header (request , accept_header );
416
389
417
390
struct aws_http_header host_header = {.name = aws_byte_cursor_from_c_str ("host" ), .value = app_ctx -> uri .host_name };
418
- aws_http_request_add_header (request , host_header );
391
+ aws_http_message_add_header (request , host_header );
419
392
420
393
struct aws_http_header user_agent_header = {
421
394
.name = aws_byte_cursor_from_c_str ("user-agent" ),
422
395
.value = aws_byte_cursor_from_c_str ("elasticurl 1.0, Powered by the AWS Common Runtime." )};
423
- aws_http_request_add_header (request , user_agent_header );
396
+ aws_http_message_add_header (request , user_agent_header );
424
397
425
398
if (app_ctx -> input_body ) {
426
399
int64_t data_len = 0 ;
@@ -435,7 +408,8 @@ static struct aws_http_request *s_build_http_request(struct elasticurl_ctx *app_
435
408
sprintf (content_length , "%" PRIi64 , data_len );
436
409
struct aws_http_header content_length_header = {.name = aws_byte_cursor_from_c_str ("content-length" ),
437
410
.value = aws_byte_cursor_from_c_str (content_length )};
438
- aws_http_request_add_header (request , content_length_header );
411
+ aws_http_message_add_header (request , content_length_header );
412
+ aws_http_message_set_body_stream (request , app_ctx -> input_body );
439
413
}
440
414
}
441
415
@@ -451,12 +425,14 @@ static struct aws_http_request *s_build_http_request(struct elasticurl_ctx *app_
451
425
struct aws_http_header custom_header = {
452
426
.name = aws_byte_cursor_from_array (app_ctx -> header_lines [i ], delimiter - app_ctx -> header_lines [i ]),
453
427
.value = aws_byte_cursor_from_c_str (delimiter + 1 )};
454
- aws_http_request_add_header (request , custom_header );
428
+ aws_http_message_add_header (request , custom_header );
455
429
}
456
430
457
431
return request ;
458
432
}
459
433
434
+ static void s_on_signing_complete (struct aws_http_message * request , int error_code , void * user_data );
435
+
460
436
static void s_on_client_connection_setup (struct aws_http_connection * connection , int error_code , void * user_data ) {
461
437
struct elasticurl_ctx * app_ctx = user_data ;
462
438
@@ -469,52 +445,58 @@ static void s_on_client_connection_setup(struct aws_http_connection *connection,
469
445
return ;
470
446
}
471
447
472
- struct aws_http_request * request = s_build_http_request (app_ctx );
448
+ app_ctx -> connection = connection ;
449
+ app_ctx -> request = s_build_http_request (app_ctx );
473
450
451
+ /* If async signing function is set, invoke it. It must invoke the signing complete callback when it's done. */
474
452
if (app_ctx -> signing_function ) {
475
- if ( app_ctx -> signing_function (request , app_ctx -> allocator , & app_ctx -> signing_context )) {
476
- fprintf ( stderr , "Signing failure\n" );
477
- exit ( 1 );
478
- }
453
+ app_ctx -> signing_function (app_ctx -> request , & app_ctx -> signing_context , s_on_signing_complete , app_ctx );
454
+ } else {
455
+ /* If no signing function, proceed immediately to next step. */
456
+ s_on_signing_complete ( app_ctx -> request , AWS_ERROR_SUCCESS , app_ctx );
479
457
}
458
+ }
480
459
481
- size_t final_header_count = aws_http_request_get_header_count (request );
460
+ static void s_on_signing_complete (struct aws_http_message * request , int error_code , void * user_data ) {
461
+ struct elasticurl_ctx * app_ctx = user_data ;
462
+
463
+ AWS_FATAL_ASSERT (request == app_ctx -> request );
464
+
465
+ if (error_code ) {
466
+ fprintf (stderr , "Signing failure\n" );
467
+ exit (1 );
468
+ }
469
+
470
+ size_t final_header_count = aws_http_message_get_header_count (app_ctx -> request );
482
471
483
472
struct aws_http_header headers [20 ];
484
473
AWS_ASSERT (final_header_count <= AWS_ARRAY_SIZE (headers ));
485
474
AWS_ZERO_ARRAY (headers );
486
475
for (size_t i = 0 ; i < final_header_count ; ++ i ) {
487
- aws_http_request_get_header ( request , & headers [i ], i );
476
+ aws_http_message_get_header ( app_ctx -> request , & headers [i ], i );
488
477
}
489
478
490
- struct aws_http_request_options final_request ;
491
- AWS_ZERO_STRUCT (final_request );
492
- final_request .self_size = sizeof (struct aws_http_request_options );
493
- final_request .uri = app_ctx -> uri .path_and_query ;
494
- final_request .user_data = app_ctx ;
495
- final_request .client_connection = connection ;
496
- final_request .method = aws_byte_cursor_from_c_str (app_ctx -> verb );
497
- final_request .header_array = headers ;
498
- final_request .num_headers = aws_http_request_get_header_count (request );
499
- final_request .on_response_headers = s_on_incoming_headers_fn ;
500
- final_request .on_response_header_block_done = s_on_incoming_header_block_done_fn ;
501
- final_request .on_response_body = s_on_incoming_body_fn ;
502
- final_request .on_complete = s_on_stream_complete_fn ;
503
- if (app_ctx -> input_body ) {
504
- final_request .stream_outgoing_body = s_stream_outgoing_body_fn ;
505
- }
479
+ struct aws_http_make_request_options final_request = {
480
+ .self_size = sizeof (final_request ),
481
+ .user_data = app_ctx ,
482
+ .request = app_ctx -> request ,
483
+ .on_response_headers = s_on_incoming_headers_fn ,
484
+ .on_response_header_block_done = s_on_incoming_header_block_done_fn ,
485
+ .on_response_body = s_on_incoming_body_fn ,
486
+ .on_complete = s_on_stream_complete_fn ,
487
+ };
506
488
507
489
app_ctx -> response_code_written = false;
508
490
509
- struct aws_http_stream * stream = aws_http_stream_new_client_request ( & final_request );
491
+ struct aws_http_stream * stream = aws_http_connection_make_request ( app_ctx -> connection , & final_request );
510
492
if (!stream ) {
511
493
fprintf (stderr , "failed to create request." );
512
494
exit (1 );
513
495
}
514
496
515
- aws_http_connection_release ( connection );
516
-
517
- aws_http_request_destroy ( request ) ;
497
+ /* Connection will stay alive until stream completes */
498
+ aws_http_connection_release ( app_ctx -> connection );
499
+ app_ctx -> connection = NULL ;
518
500
}
519
501
520
502
static void s_on_client_connection_shutdown (struct aws_http_connection * connection , int error_code , void * user_data ) {
@@ -728,6 +710,8 @@ int main(int argc, char **argv) {
728
710
729
711
aws_uri_clean_up (& app_ctx .uri );
730
712
713
+ aws_http_message_destroy (app_ctx .request );
714
+
731
715
aws_shared_library_clean_up (& app_ctx .signing_library );
732
716
733
717
if (app_ctx .output != stdout ) {
0 commit comments