| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
#include <aws/http/connection.h> |
| 6 |
#include <aws/http/request_response.h> |
| 7 |
|
| 8 |
#include <aws/common/command_line_parser.h> |
| 9 |
#include <aws/common/condition_variable.h> |
| 10 |
#include <aws/common/hash_table.h> |
| 11 |
#include <aws/common/log_channel.h> |
| 12 |
#include <aws/common/log_formatter.h> |
| 13 |
#include <aws/common/log_writer.h> |
| 14 |
#include <aws/common/mutex.h> |
| 15 |
#include <aws/common/string.h> |
| 16 |
|
| 17 |
#include <aws/io/channel_bootstrap.h> |
| 18 |
#include <aws/io/event_loop.h> |
| 19 |
#include <aws/io/logging.h> |
| 20 |
#include <aws/io/shared_library.h> |
| 21 |
#include <aws/io/socket.h> |
| 22 |
#include <aws/io/stream.h> |
| 23 |
#include <aws/io/tls_channel_handler.h> |
| 24 |
#include <aws/io/uri.h> |
| 25 |
|
| 26 |
#include <inttypes.h> |
| 27 |
|
| 28 |
#ifdef _MSC_VER |
| 29 |
# pragma warning(disable : 4996) |
| 30 |
# pragma warning(disable : 4204) |
| 31 |
# pragma warning(disable : 4221) |
| 32 |
#endif |
| 33 |
|
| 34 |
#define ELASTICURL_VERSION "0.2.0" |
| 35 |
|
| 36 |
struct elasticurl_ctx { |
| 37 |
struct aws_allocator *allocator; |
| 38 |
const char *verb; |
| 39 |
struct aws_uri uri; |
| 40 |
struct aws_mutex mutex; |
| 41 |
struct aws_condition_variable c_var; |
| 42 |
bool response_code_written; |
| 43 |
const char *cacert; |
| 44 |
const char *capath; |
| 45 |
const char *cert; |
| 46 |
const char *key; |
| 47 |
int connect_timeout; |
| 48 |
const char *header_lines[10]; |
| 49 |
size_t header_line_count; |
| 50 |
FILE *input_file; |
| 51 |
struct aws_input_stream *input_body; |
| 52 |
struct aws_http_message *request; |
| 53 |
struct aws_http_connection *connection; |
| 54 |
const char *signing_library_path; |
| 55 |
struct aws_shared_library signing_library; |
| 56 |
const char *signing_function_name; |
| 57 |
struct aws_hash_table signing_context; |
| 58 |
aws_http_message_transform_fn *signing_function; |
| 59 |
const char *alpn; |
| 60 |
bool include_headers; |
| 61 |
bool insecure; |
| 62 |
FILE *output; |
| 63 |
const char *trace_file; |
| 64 |
enum aws_log_level log_level; |
| 65 |
enum aws_http_version required_http_version; |
| 66 |
bool exchange_completed; |
| 67 |
bool manual_write; |
| 68 |
bool manual_write_chunked; |
| 69 |
int64_t manual_write_content_length; |
| 70 |
struct aws_http_stream *stream; |
| 71 |
bool stream_ready; |
| 72 |
}; |
| 73 |
|
| 74 |
static void s_usage(int exit_code) { |
| 75 |
|
| 76 |
fprintf(stderr, "usage: elasticurl [options] url\n"); |
| 77 |
fprintf(stderr, " url: url to make a request to. The default is a GET request.\n"); |
| 78 |
fprintf(stderr, "\n Options:\n\n"); |
| 79 |
fprintf(stderr, " --cacert FILE: path to a CA certficate file.\n"); |
| 80 |
fprintf(stderr, " --capath PATH: path to a directory containing CA files.\n"); |
| 81 |
fprintf(stderr, " --cert FILE: path to a PEM encoded certificate to use with mTLS\n"); |
| 82 |
fprintf(stderr, " --key FILE: Path to a PEM encoded private key that matches cert.\n"); |
| 83 |
fprintf(stderr, " --connect-timeout INT: time in milliseconds to wait for a connection.\n"); |
| 84 |
fprintf(stderr, " -H, --header LINE: line to send as a header in format [header-key]: [header-value]\n"); |
| 85 |
fprintf(stderr, " -d, --data STRING: Data to POST or PUT\n"); |
| 86 |
fprintf(stderr, " --data-file FILE: File to read from file and POST or PUT\n"); |
| 87 |
fprintf(stderr, " -M, --method STRING: Http Method verb to use for the request\n"); |
| 88 |
fprintf(stderr, " -G, --get: uses GET for the verb.\n"); |
| 89 |
fprintf(stderr, " -P, --post: uses POST for the verb.\n"); |
| 90 |
fprintf(stderr, " -I, --head: uses HEAD for the verb.\n"); |
| 91 |
fprintf(stderr, " -i, --include: includes headers in output.\n"); |
| 92 |
fprintf(stderr, " -k, --insecure: turns off SSL/TLS validation.\n"); |
| 93 |
fprintf(stderr, " --signing-lib: path to a shared library with an exported signing function to use\n"); |
| 94 |
fprintf(stderr, " --signing-func: name of the signing function to use within the signing library\n"); |
| 95 |
fprintf( |
| 96 |
stderr, |
| 97 |
" --signing-context: key=value pair to pass to the signing function; may be used multiple times\n"); |
| 98 |
fprintf(stderr, " -o, --output FILE: dumps content-body to FILE instead of stdout.\n"); |
| 99 |
fprintf(stderr, " -t, --trace FILE: dumps logs to FILE instead of stderr.\n"); |
| 100 |
fprintf(stderr, " -v, --verbose: ERROR|INFO|DEBUG|TRACE: log level to configure. Default is none.\n"); |
| 101 |
fprintf(stderr, " --version: print the version of elasticurl.\n"); |
| 102 |
fprintf(stderr, " --http2: HTTP/2 connection required\n"); |
| 103 |
fprintf(stderr, " --http1_1: HTTP/1.1 connection required\n"); |
| 104 |
fprintf(stderr, " --manual-write: interactively write request body via stdin\n"); |
| 105 |
fprintf(stderr, " -h, --help\n"); |
| 106 |
fprintf(stderr, " Display this message and quit.\n"); |
| 107 |
exit(exit_code); |
| 108 |
} |
| 109 |
|
| 110 |
static struct aws_cli_option s_long_options[] = { |
| 111 |
{"cacert", AWS_CLI_OPTIONS_REQUIRED_ARGUMENT, NULL, 'a'}, |
| 112 |
{"capath", AWS_CLI_OPTIONS_REQUIRED_ARGUMENT, NULL, 'b'}, |
| 113 |
{"cert", AWS_CLI_OPTIONS_REQUIRED_ARGUMENT, NULL, 'c'}, |
| 114 |
{"key", AWS_CLI_OPTIONS_REQUIRED_ARGUMENT, NULL, 'e'}, |
| 115 |
{"connect-timeout", AWS_CLI_OPTIONS_REQUIRED_ARGUMENT, NULL, 'f'}, |
| 116 |
{"header", AWS_CLI_OPTIONS_REQUIRED_ARGUMENT, NULL, 'H'}, |
| 117 |
{"data", AWS_CLI_OPTIONS_REQUIRED_ARGUMENT, NULL, 'd'}, |
| 118 |
{"data-file", AWS_CLI_OPTIONS_REQUIRED_ARGUMENT, NULL, 'g'}, |
| 119 |
{"method", AWS_CLI_OPTIONS_REQUIRED_ARGUMENT, NULL, 'M'}, |
| 120 |
{"get", AWS_CLI_OPTIONS_NO_ARGUMENT, NULL, 'G'}, |
| 121 |
{"post", AWS_CLI_OPTIONS_NO_ARGUMENT, NULL, 'P'}, |
| 122 |
{"head", AWS_CLI_OPTIONS_NO_ARGUMENT, NULL, 'I'}, |
| 123 |
{"signing-lib", AWS_CLI_OPTIONS_REQUIRED_ARGUMENT, NULL, 'j'}, |
| 124 |
{"include", AWS_CLI_OPTIONS_NO_ARGUMENT, NULL, 'i'}, |
| 125 |
{"insecure", AWS_CLI_OPTIONS_NO_ARGUMENT, NULL, 'k'}, |
| 126 |
{"signing-func", AWS_CLI_OPTIONS_REQUIRED_ARGUMENT, NULL, 'l'}, |
| 127 |
{"signing-context", AWS_CLI_OPTIONS_REQUIRED_ARGUMENT, NULL, 'm'}, |
| 128 |
{"output", AWS_CLI_OPTIONS_REQUIRED_ARGUMENT, NULL, 'o'}, |
| 129 |
{"trace", AWS_CLI_OPTIONS_REQUIRED_ARGUMENT, NULL, 't'}, |
| 130 |
{"verbose", AWS_CLI_OPTIONS_REQUIRED_ARGUMENT, NULL, 'v'}, |
| 131 |
{"version", AWS_CLI_OPTIONS_NO_ARGUMENT, NULL, 'V'}, |
| 132 |
{"http2", AWS_CLI_OPTIONS_NO_ARGUMENT, NULL, 'w'}, |
| 133 |
{"http1_1", AWS_CLI_OPTIONS_NO_ARGUMENT, NULL, 'W'}, |
| 134 |
{"manual-write", AWS_CLI_OPTIONS_NO_ARGUMENT, NULL, 'n'}, |
| 135 |
{"help", AWS_CLI_OPTIONS_NO_ARGUMENT, NULL, 'h'}, |
| 136 |
|
| 137 |
{NULL, AWS_CLI_OPTIONS_NO_ARGUMENT, NULL, 0}, |
| 138 |
}; |
| 139 |
|
| 140 |
static int s_parse_signing_context( |
| 141 |
struct aws_hash_table *signing_context, |
| 142 |
struct aws_allocator *allocator, |
| 143 |
const char *context_argument) { |
| 144 |
(void)signing_context; |
| 145 |
(void)context_argument; |
| 146 |
|
| 147 |
char *delimiter = memchr(context_argument, ':', strlen(context_argument)); |
| 148 |
if (!delimiter) { |
| 149 |
fprintf(stderr, "invalid signing context line \"%s\".", context_argument); |
| 150 |
exit(1); |
| 151 |
} |
| 152 |
|
| 153 |
struct aws_string *key = |
| 154 |
aws_string_new_from_array(allocator, (const uint8_t *)context_argument, delimiter - context_argument); |
| 155 |
struct aws_string *value = |
| 156 |
aws_string_new_from_array(allocator, (const uint8_t *)delimiter + 1, strlen(delimiter + 1)); |
| 157 |
if (key == NULL || value == NULL) { |
| 158 |
fprintf(stderr, "failure allocating signing context kv pair"); |
| 159 |
exit(1); |
| 160 |
} |
| 161 |
|
| 162 |
aws_hash_table_put(signing_context, key, value, NULL); |
| 163 |
|
| 164 |
return AWS_OP_SUCCESS; |
| 165 |
} |
| 166 |
|
| 167 |
static void s_parse_options(int argc, char **argv, struct elasticurl_ctx *ctx) { |
| 168 |
bool uri_found = false; |
| 169 |
while (true) { |
| 170 |
int option_index = 0; |
| 171 |
int c = |
| 172 |
aws_cli_getopt_long(argc, argv, "a:b:c:e:f:H:d:g:j:l:m:M:GPHiko:t:v:VwWnh", s_long_options, &option_index); |
| 173 |
if (c == -1) { |
| 174 |
break; |
| 175 |
} |
| 176 |
|
| 177 |
switch (c) { |
| 178 |
case 0: |
| 179 |
|
| 180 |
break; |
| 181 |
case 'a': |
| 182 |
ctx->cacert = aws_cli_optarg; |
| 183 |
break; |
| 184 |
case 'b': |
| 185 |
ctx->capath = aws_cli_optarg; |
| 186 |
break; |
| 187 |
case 'c': |
| 188 |
ctx->cert = aws_cli_optarg; |
| 189 |
break; |
| 190 |
case 'e': |
| 191 |
ctx->key = aws_cli_optarg; |
| 192 |
break; |
| 193 |
case 'f': |
| 194 |
ctx->connect_timeout = atoi(aws_cli_optarg); |
| 195 |
break; |
| 196 |
case 'H': |
| 197 |
if (ctx->header_line_count >= sizeof(ctx->header_lines) / sizeof(const char *)) { |
| 198 |
fprintf(stderr, "currently only 10 header lines are supported.\n"); |
| 199 |
s_usage(1); |
| 200 |
} |
| 201 |
ctx->header_lines[ctx->header_line_count++] = aws_cli_optarg; |
| 202 |
break; |
| 203 |
case 'd': { |
| 204 |
struct aws_byte_cursor data_cursor = aws_byte_cursor_from_c_str(aws_cli_optarg); |
| 205 |
ctx->input_body = aws_input_stream_new_from_cursor(ctx->allocator, &data_cursor); |
| 206 |
break; |
| 207 |
} |
| 208 |
case 'g': |
| 209 |
ctx->input_file = fopen(aws_cli_optarg, "rb"); |
| 210 |
ctx->input_body = aws_input_stream_new_from_open_file(ctx->allocator, ctx->input_file); |
| 211 |
if (!ctx->input_file) { |
| 212 |
fprintf(stderr, "unable to open file %s.\n", aws_cli_optarg); |
| 213 |
s_usage(1); |
| 214 |
} |
| 215 |
break; |
| 216 |
case 'j': |
| 217 |
ctx->signing_library_path = aws_cli_optarg; |
| 218 |
if (aws_shared_library_init(&ctx->signing_library, aws_cli_optarg)) { |
| 219 |
fprintf(stderr, "unable to open signing library %s.\n", aws_cli_optarg); |
| 220 |
s_usage(1); |
| 221 |
} |
| 222 |
break; |
| 223 |
case 'l': |
| 224 |
ctx->signing_function_name = aws_cli_optarg; |
| 225 |
break; |
| 226 |
case 'm': |
| 227 |
if (s_parse_signing_context(&ctx->signing_context, ctx->allocator, aws_cli_optarg)) { |
| 228 |
fprintf(stderr, "error parsing signing context \"%s\"\n", aws_cli_optarg); |
| 229 |
s_usage(1); |
| 230 |
} |
| 231 |
break; |
| 232 |
case 'M': |
| 233 |
ctx->verb = aws_cli_optarg; |
| 234 |
break; |
| 235 |
case 'G': |
| 236 |
ctx->verb = "GET"; |
| 237 |
break; |
| 238 |
case 'P': |
| 239 |
ctx->verb = "POST"; |
| 240 |
break; |
| 241 |
case 'I': |
| 242 |
ctx->verb = "HEAD"; |
| 243 |
break; |
| 244 |
case 'i': |
| 245 |
ctx->include_headers = true; |
| 246 |
break; |
| 247 |
case 'k': |
| 248 |
ctx->insecure = true; |
| 249 |
break; |
| 250 |
case 'o': |
| 251 |
ctx->output = fopen(aws_cli_optarg, "wb"); |
| 252 |
|
| 253 |
if (!ctx->output) { |
| 254 |
fprintf(stderr, "unable to open file %s.\n", aws_cli_optarg); |
| 255 |
s_usage(1); |
| 256 |
} |
| 257 |
break; |
| 258 |
case 't': |
| 259 |
ctx->trace_file = aws_cli_optarg; |
| 260 |
break; |
| 261 |
case 'v': |
| 262 |
if (!strcmp(aws_cli_optarg, "TRACE")) { |
| 263 |
ctx->log_level = AWS_LL_TRACE; |
| 264 |
} else if (!strcmp(aws_cli_optarg, "INFO")) { |
| 265 |
ctx->log_level = AWS_LL_INFO; |
| 266 |
} else if (!strcmp(aws_cli_optarg, "DEBUG")) { |
| 267 |
ctx->log_level = AWS_LL_DEBUG; |
| 268 |
} else if (!strcmp(aws_cli_optarg, "ERROR")) { |
| 269 |
ctx->log_level = AWS_LL_ERROR; |
| 270 |
} else { |
| 271 |
fprintf(stderr, "unsupported log level %s.\n", aws_cli_optarg); |
| 272 |
s_usage(1); |
| 273 |
} |
| 274 |
break; |
| 275 |
case 'V': |
| 276 |
fprintf(stderr, "elasticurl %s\n", ELASTICURL_VERSION); |
| 277 |
exit(0); |
| 278 |
case 'w': |
| 279 |
ctx->alpn = "h2"; |
| 280 |
ctx->required_http_version = AWS_HTTP_VERSION_2; |
| 281 |
break; |
| 282 |
case 'W': |
| 283 |
ctx->alpn = "http/1.1"; |
| 284 |
ctx->required_http_version = AWS_HTTP_VERSION_1_1; |
| 285 |
break; |
| 286 |
case 'n': |
| 287 |
ctx->manual_write = true; |
| 288 |
break; |
| 289 |
case 'h': |
| 290 |
s_usage(0); |
| 291 |
break; |
| 292 |
case 0x02: { |
| 293 |
struct aws_byte_cursor uri_cursor = aws_byte_cursor_from_c_str(aws_cli_positional_arg); |
| 294 |
if (aws_uri_init_parse(&ctx->uri, ctx->allocator, &uri_cursor)) { |
| 295 |
fprintf( |
| 296 |
stderr, |
| 297 |
"Failed to parse uri %s with error %s\n", |
| 298 |
(char *)uri_cursor.ptr, |
| 299 |
aws_error_debug_str(aws_last_error())); |
| 300 |
s_usage(1); |
| 301 |
} |
| 302 |
uri_found = true; |
| 303 |
} break; |
| 304 |
default: |
| 305 |
fprintf(stderr, "Unknown option\n"); |
| 306 |
s_usage(1); |
| 307 |
} |
| 308 |
} |
| 309 |
|
| 310 |
if (ctx->signing_function_name != NULL) { |
| 311 |
if (ctx->signing_library_path == NULL) { |
| 312 |
fprintf( |
| 313 |
stderr, |
| 314 |
"To sign a request made by Elasticurl you must supply both a signing library path and a signing " |
| 315 |
"function name\n"); |
| 316 |
s_usage(1); |
| 317 |
} |
| 318 |
|
| 319 |
if (aws_shared_library_find_function( |
| 320 |
&ctx->signing_library, ctx->signing_function_name, (aws_generic_function *)&ctx->signing_function)) { |
| 321 |
fprintf( |
| 322 |
stderr, |
| 323 |
"Unable to find function %s in signing library %s", |
| 324 |
ctx->signing_function_name, |
| 325 |
ctx->signing_library_path); |
| 326 |
s_usage(1); |
| 327 |
} |
| 328 |
} |
| 329 |
|
| 330 |
if (ctx->input_body == NULL) { |
| 331 |
struct aws_byte_cursor empty_cursor; |
| 332 |
AWS_ZERO_STRUCT(empty_cursor); |
| 333 |
ctx->input_body = aws_input_stream_new_from_cursor(ctx->allocator, &empty_cursor); |
| 334 |
} |
| 335 |
|
| 336 |
if (!uri_found) { |
| 337 |
fprintf(stderr, "A URI for the request must be supplied.\n"); |
| 338 |
s_usage(1); |
| 339 |
} |
| 340 |
} |
| 341 |
|
| 342 |
static int s_on_incoming_body_fn(struct aws_http_stream *stream, const struct aws_byte_cursor *data, void *user_data) { |
| 343 |
|
| 344 |
(void)stream; |
| 345 |
struct elasticurl_ctx *app_ctx = user_data; |
| 346 |
|
| 347 |
fwrite(data->ptr, 1, data->len, app_ctx->output); |
| 348 |
|
| 349 |
return AWS_OP_SUCCESS; |
| 350 |
} |
| 351 |
|
| 352 |
static int s_on_incoming_headers_fn( |
| 353 |
struct aws_http_stream *stream, |
| 354 |
enum aws_http_header_block header_block, |
| 355 |
const struct aws_http_header *header_array, |
| 356 |
size_t num_headers, |
| 357 |
void *user_data) { |
| 358 |
|
| 359 |
struct elasticurl_ctx *app_ctx = user_data; |
| 360 |
(void)app_ctx; |
| 361 |
(void)stream; |
| 362 |
|
| 363 |
|
| 364 |
if (header_block == AWS_HTTP_HEADER_BLOCK_INFORMATIONAL) { |
| 365 |
return AWS_OP_SUCCESS; |
| 366 |
} |
| 367 |
|
| 368 |
if (app_ctx->include_headers) { |
| 369 |
if (!app_ctx->response_code_written) { |
| 370 |
int status = 0; |
| 371 |
aws_http_stream_get_incoming_response_status(stream, &status); |
| 372 |
fprintf(stdout, "Response Status: %d\n", status); |
| 373 |
app_ctx->response_code_written = true; |
| 374 |
} |
| 375 |
|
| 376 |
for (size_t i = 0; i < num_headers; ++i) { |
| 377 |
fwrite(header_array[i].name.ptr, 1, header_array[i].name.len, stdout); |
| 378 |
fprintf(stdout, ": "); |
| 379 |
fwrite(header_array[i].value.ptr, 1, header_array[i].value.len, stdout); |
| 380 |
fprintf(stdout, "\n"); |
| 381 |
} |
| 382 |
} |
| 383 |
|
| 384 |
return AWS_OP_SUCCESS; |
| 385 |
} |
| 386 |
|
| 387 |
static int s_on_incoming_header_block_done_fn( |
| 388 |
struct aws_http_stream *stream, |
| 389 |
enum aws_http_header_block header_block, |
| 390 |
void *user_data) { |
| 391 |
(void)stream; |
| 392 |
(void)header_block; |
| 393 |
(void)user_data; |
| 394 |
|
| 395 |
return AWS_OP_SUCCESS; |
| 396 |
} |
| 397 |
|
| 398 |
static void s_on_stream_complete_fn(struct aws_http_stream *stream, int error_code, void *user_data) { |
| 399 |
(void)error_code; |
| 400 |
(void)user_data; |
| 401 |
aws_http_stream_release(stream); |
| 402 |
} |
| 403 |
|
| 404 |
static struct aws_http_message *s_build_http_request( |
| 405 |
struct elasticurl_ctx *app_ctx, |
| 406 |
enum aws_http_version protocol_version) { |
| 407 |
|
| 408 |
struct aws_http_message *request = protocol_version == AWS_HTTP_VERSION_2 |
| 409 |
? aws_http2_message_new_request(app_ctx->allocator) |
| 410 |
: aws_http_message_new_request(app_ctx->allocator); |
| 411 |
if (request == NULL) { |
| 412 |
fprintf(stderr, "failed to allocate request\n"); |
| 413 |
exit(1); |
| 414 |
} |
| 415 |
|
| 416 |
aws_http_message_set_request_method(request, aws_byte_cursor_from_c_str(app_ctx->verb)); |
| 417 |
if (app_ctx->uri.path_and_query.len != 0) { |
| 418 |
aws_http_message_set_request_path(request, app_ctx->uri.path_and_query); |
| 419 |
} else { |
| 420 |
aws_http_message_set_request_path(request, aws_byte_cursor_from_c_str("/")); |
| 421 |
} |
| 422 |
|
| 423 |
if (protocol_version == AWS_HTTP_VERSION_2) { |
| 424 |
struct aws_http_headers *h2_headers = aws_http_message_get_headers(request); |
| 425 |
aws_http2_headers_set_request_scheme(h2_headers, app_ctx->uri.scheme); |
| 426 |
aws_http2_headers_set_request_authority(h2_headers, app_ctx->uri.host_name); |
| 427 |
} else { |
| 428 |
struct aws_http_header host_header = { |
| 429 |
.name = aws_byte_cursor_from_c_str("host"), |
| 430 |
.value = app_ctx->uri.host_name, |
| 431 |
}; |
| 432 |
aws_http_message_add_header(request, host_header); |
| 433 |
} |
| 434 |
struct aws_http_header accept_header = { |
| 435 |
.name = aws_byte_cursor_from_c_str("accept"), |
| 436 |
.value = aws_byte_cursor_from_c_str("*/*"), |
| 437 |
}; |
| 438 |
aws_http_message_add_header(request, accept_header); |
| 439 |
struct aws_http_header user_agent_header = { |
| 440 |
.name = aws_byte_cursor_from_c_str("user-agent"), |
| 441 |
.value = aws_byte_cursor_from_c_str("elasticurl 1.0, Powered by the AWS Common Runtime."), |
| 442 |
}; |
| 443 |
aws_http_message_add_header(request, user_agent_header); |
| 444 |
|
| 445 |
if (app_ctx->manual_write) { |
| 446 |
|
| 447 |
*/ |
| 448 |
if (app_ctx->manual_write_chunked && protocol_version != AWS_HTTP_VERSION_2) { |
| 449 |
struct aws_http_header te_header = { |
| 450 |
.name = aws_byte_cursor_from_c_str("transfer-encoding"), |
| 451 |
.value = aws_byte_cursor_from_c_str("chunked"), |
| 452 |
}; |
| 453 |
aws_http_message_add_header(request, te_header); |
| 454 |
} else if (!app_ctx->manual_write_chunked) { |
| 455 |
char content_length[64]; |
| 456 |
AWS_ZERO_ARRAY(content_length); |
| 457 |
snprintf(content_length, sizeof(content_length), "%" PRIi64, app_ctx->manual_write_content_length); |
| 458 |
struct aws_http_header cl_header = { |
| 459 |
.name = aws_byte_cursor_from_c_str("content-length"), |
| 460 |
.value = aws_byte_cursor_from_c_str(content_length), |
| 461 |
}; |
| 462 |
aws_http_message_add_header(request, cl_header); |
| 463 |
} |
| 464 |
} else if (app_ctx->input_body) { |
| 465 |
int64_t data_len = 0; |
| 466 |
if (aws_input_stream_get_length(app_ctx->input_body, &data_len)) { |
| 467 |
fprintf(stderr, "failed to get length of input stream.\n"); |
| 468 |
exit(1); |
| 469 |
} |
| 470 |
|
| 471 |
if (data_len > 0) { |
| 472 |
char content_length[64]; |
| 473 |
AWS_ZERO_ARRAY(content_length); |
| 474 |
snprintf(content_length, sizeof(content_length), "%" PRIi64, data_len); |
| 475 |
struct aws_http_header content_length_header = { |
| 476 |
.name = aws_byte_cursor_from_c_str("content-length"), |
| 477 |
.value = aws_byte_cursor_from_c_str(content_length), |
| 478 |
}; |
| 479 |
aws_http_message_add_header(request, content_length_header); |
| 480 |
aws_http_message_set_body_stream(request, app_ctx->input_body); |
| 481 |
} |
| 482 |
} |
| 483 |
|
| 484 |
AWS_ASSERT(app_ctx->header_line_count <= 10); |
| 485 |
for (size_t i = 0; i < app_ctx->header_line_count; ++i) { |
| 486 |
char *delimiter = memchr(app_ctx->header_lines[i], ':', strlen(app_ctx->header_lines[i])); |
| 487 |
|
| 488 |
if (!delimiter) { |
| 489 |
fprintf(stderr, "invalid header line %s configured.", app_ctx->header_lines[i]); |
| 490 |
exit(1); |
| 491 |
} |
| 492 |
|
| 493 |
struct aws_http_header custom_header = { |
| 494 |
.name = aws_byte_cursor_from_array(app_ctx->header_lines[i], delimiter - app_ctx->header_lines[i]), |
| 495 |
.value = aws_byte_cursor_from_c_str(delimiter + 1), |
| 496 |
}; |
| 497 |
aws_http_message_add_header(request, custom_header); |
| 498 |
} |
| 499 |
|
| 500 |
return request; |
| 501 |
} |
| 502 |
|
| 503 |
static void s_on_signing_complete(struct aws_http_message *request, int error_code, void *user_data); |
| 504 |
|
| 505 |
static void s_on_client_connection_setup(struct aws_http_connection *connection, int error_code, void *user_data) { |
| 506 |
struct elasticurl_ctx *app_ctx = user_data; |
| 507 |
|
| 508 |
if (error_code) { |
| 509 |
fprintf(stderr, "Connection failed with error %s\n", aws_error_debug_str(error_code)); |
| 510 |
aws_mutex_lock(&app_ctx->mutex); |
| 511 |
app_ctx->exchange_completed = true; |
| 512 |
aws_mutex_unlock(&app_ctx->mutex); |
| 513 |
aws_condition_variable_notify_all(&app_ctx->c_var); |
| 514 |
return; |
| 515 |
} |
| 516 |
|
| 517 |
if (app_ctx->required_http_version) { |
| 518 |
if (aws_http_connection_get_version(connection) != app_ctx->required_http_version) { |
| 519 |
fprintf(stderr, "Error. The requested HTTP version, %s, is not supported by the peer.", app_ctx->alpn); |
| 520 |
exit(1); |
| 521 |
} |
| 522 |
} |
| 523 |
|
| 524 |
app_ctx->connection = connection; |
| 525 |
app_ctx->request = s_build_http_request(app_ctx, aws_http_connection_get_version(connection)); |
| 526 |
|
| 527 |
|
| 528 |
if (app_ctx->signing_function) { |
| 529 |
app_ctx->signing_function(app_ctx->request, &app_ctx->signing_context, s_on_signing_complete, app_ctx); |
| 530 |
} else { |
| 531 |
|
| 532 |
s_on_signing_complete(app_ctx->request, AWS_ERROR_SUCCESS, app_ctx); |
| 533 |
} |
| 534 |
} |
| 535 |
|
| 536 |
static void s_on_signing_complete(struct aws_http_message *request, int error_code, void *user_data) { |
| 537 |
struct elasticurl_ctx *app_ctx = user_data; |
| 538 |
|
| 539 |
AWS_FATAL_ASSERT(request == app_ctx->request); |
| 540 |
|
| 541 |
if (error_code) { |
| 542 |
fprintf(stderr, "Signing failure\n"); |
| 543 |
exit(1); |
| 544 |
} |
| 545 |
|
| 546 |
struct aws_http_make_request_options final_request = { |
| 547 |
.self_size = sizeof(final_request), |
| 548 |
.user_data = app_ctx, |
| 549 |
.request = app_ctx->request, |
| 550 |
.on_response_headers = s_on_incoming_headers_fn, |
| 551 |
.on_response_header_block_done = s_on_incoming_header_block_done_fn, |
| 552 |
.on_response_body = s_on_incoming_body_fn, |
| 553 |
.on_complete = s_on_stream_complete_fn, |
| 554 |
.use_manual_data_writes = app_ctx->manual_write, |
| 555 |
}; |
| 556 |
|
| 557 |
app_ctx->response_code_written = false; |
| 558 |
|
| 559 |
struct aws_http_stream *stream = aws_http_connection_make_request(app_ctx->connection, &final_request); |
| 560 |
if (!stream) { |
| 561 |
fprintf(stderr, "failed to create request."); |
| 562 |
exit(1); |
| 563 |
} |
| 564 |
aws_http_stream_activate(stream); |
| 565 |
|
| 566 |
if (app_ctx->manual_write) { |
| 567 |
|
| 568 |
app_ctx->stream = stream; |
| 569 |
aws_mutex_lock(&app_ctx->mutex); |
| 570 |
app_ctx->stream_ready = true; |
| 571 |
aws_mutex_unlock(&app_ctx->mutex); |
| 572 |
aws_condition_variable_notify_all(&app_ctx->c_var); |
| 573 |
} |
| 574 |
|
| 575 |
|
| 576 |
aws_http_connection_release(app_ctx->connection); |
| 577 |
app_ctx->connection = NULL; |
| 578 |
} |
| 579 |
|
| 580 |
static void s_on_client_connection_shutdown(struct aws_http_connection *connection, int error_code, void *user_data) { |
| 581 |
(void)error_code; |
| 582 |
(void)connection; |
| 583 |
struct elasticurl_ctx *app_ctx = user_data; |
| 584 |
|
| 585 |
aws_mutex_lock(&app_ctx->mutex); |
| 586 |
app_ctx->exchange_completed = true; |
| 587 |
aws_mutex_unlock(&app_ctx->mutex); |
| 588 |
aws_condition_variable_notify_all(&app_ctx->c_var); |
| 589 |
} |
| 590 |
|
| 591 |
static bool s_completion_predicate(void *arg) { |
| 592 |
struct elasticurl_ctx *app_ctx = arg; |
| 593 |
return app_ctx->exchange_completed; |
| 594 |
} |
| 595 |
|
| 596 |
static bool s_stream_ready_predicate(void *arg) { |
| 597 |
struct elasticurl_ctx *app_ctx = arg; |
| 598 |
return app_ctx->stream_ready || app_ctx->exchange_completed; |
| 599 |
} |
| 600 |
|
| 601 |
struct manual_write_ctx { |
| 602 |
struct aws_allocator *allocator; |
| 603 |
uint8_t *data; |
| 604 |
struct aws_input_stream *stream; |
| 605 |
}; |
| 606 |
|
| 607 |
static void s_on_manual_write_complete(struct aws_http_stream *stream, int error_code, void *user_data) { |
| 608 |
(void)stream; |
| 609 |
(void)error_code; |
| 610 |
struct manual_write_ctx *ctx = user_data; |
| 611 |
aws_input_stream_release(ctx->stream); |
| 612 |
aws_mem_release(ctx->allocator, ctx->data); |
| 613 |
aws_mem_release(ctx->allocator, ctx); |
| 614 |
} |
| 615 |
|
| 616 |
static void s_manual_write_loop(struct elasticurl_ctx *app_ctx) { |
| 617 |
|
| 618 |
aws_mutex_lock(&app_ctx->mutex); |
| 619 |
aws_condition_variable_wait_pred(&app_ctx->c_var, &app_ctx->mutex, s_stream_ready_predicate, app_ctx); |
| 620 |
aws_mutex_unlock(&app_ctx->mutex); |
| 621 |
|
| 622 |
if (app_ctx->exchange_completed) { |
| 623 |
return; |
| 624 |
} |
| 625 |
|
| 626 |
int64_t bytes_sent = 0; |
| 627 |
char line_buf[4096]; |
| 628 |
|
| 629 |
fprintf(stderr, "Enter data (empty line to finish):\n"); |
| 630 |
while (fgets(line_buf, sizeof(line_buf), stdin)) { |
| 631 |
|
| 632 |
size_t len = strlen(line_buf); |
| 633 |
if (len > 0 && line_buf[len - 1] == '\n') { |
| 634 |
line_buf[--len] = '\0'; |
| 635 |
} |
| 636 |
|
| 637 |
|
| 638 |
if (len == 0) { |
| 639 |
break; |
| 640 |
} |
| 641 |
|
| 642 |
|
| 643 |
uint8_t *heap_data = aws_mem_calloc(app_ctx->allocator, 1, len); |
| 644 |
memcpy(heap_data, line_buf, len); |
| 645 |
|
| 646 |
struct aws_byte_cursor data_cursor = aws_byte_cursor_from_array(heap_data, len); |
| 647 |
struct aws_input_stream *data_stream = |
| 648 |
aws_input_stream_new_from_cursor(app_ctx->allocator, &data_cursor); |
| 649 |
|
| 650 |
struct manual_write_ctx *write_ctx = aws_mem_calloc(app_ctx->allocator, 1, sizeof(struct manual_write_ctx)); |
| 651 |
write_ctx->allocator = app_ctx->allocator; |
| 652 |
write_ctx->data = heap_data; |
| 653 |
write_ctx->stream = data_stream; |
| 654 |
|
| 655 |
struct aws_http_stream_write_data_options write_opts = { |
| 656 |
.data = data_stream, |
| 657 |
.end_stream = false, |
| 658 |
.on_complete = s_on_manual_write_complete, |
| 659 |
.user_data = write_ctx, |
| 660 |
}; |
| 661 |
|
| 662 |
if (aws_http_stream_write_data(app_ctx->stream, &write_opts)) { |
| 663 |
fprintf(stderr, "write_data failed: %s\n", aws_error_debug_str(aws_last_error())); |
| 664 |
aws_input_stream_release(data_stream); |
| 665 |
aws_mem_release(app_ctx->allocator, heap_data); |
| 666 |
aws_mem_release(app_ctx->allocator, write_ctx); |
| 667 |
break; |
| 668 |
} |
| 669 |
|
| 670 |
bytes_sent += (int64_t)len; |
| 671 |
fprintf(stderr, "Sent %zu bytes (total: %" PRIi64 ")\n", len, bytes_sent); |
| 672 |
} |
| 673 |
|
| 674 |
|
| 675 |
struct aws_byte_cursor empty_cursor = aws_byte_cursor_from_c_str(""); |
| 676 |
struct aws_input_stream *empty_stream = |
| 677 |
aws_input_stream_new_from_cursor(app_ctx->allocator, &empty_cursor); |
| 678 |
|
| 679 |
struct aws_http_stream_write_data_options final_opts = { |
| 680 |
.data = empty_stream, |
| 681 |
.end_stream = true, |
| 682 |
}; |
| 683 |
|
| 684 |
if (aws_http_stream_write_data(app_ctx->stream, &final_opts)) { |
| 685 |
fprintf(stderr, "final write_data failed: %s\n", aws_error_debug_str(aws_last_error())); |
| 686 |
} else { |
| 687 |
fprintf(stderr, "Stream complete. Sent %" PRIi64 " bytes.\n", bytes_sent); |
| 688 |
} |
| 689 |
|
| 690 |
aws_input_stream_release(empty_stream); |
| 691 |
} |
| 692 |
|
| 693 |
int main(int argc, char **argv) { |
| 694 |
struct aws_allocator *allocator = aws_default_allocator(); |
| 695 |
|
| 696 |
aws_http_library_init(allocator); |
| 697 |
|
| 698 |
struct elasticurl_ctx app_ctx; |
| 699 |
AWS_ZERO_STRUCT(app_ctx); |
| 700 |
app_ctx.allocator = allocator; |
| 701 |
app_ctx.c_var = (struct aws_condition_variable)AWS_CONDITION_VARIABLE_INIT; |
| 702 |
app_ctx.connect_timeout = 3000; |
| 703 |
app_ctx.output = stdout; |
| 704 |
app_ctx.verb = "GET"; |
| 705 |
app_ctx.alpn = "h2;http/1.1"; |
| 706 |
aws_mutex_init(&app_ctx.mutex); |
| 707 |
aws_hash_table_init( |
| 708 |
&app_ctx.signing_context, |
| 709 |
allocator, |
| 710 |
10, |
| 711 |
aws_hash_string, |
| 712 |
aws_hash_callback_string_eq, |
| 713 |
aws_hash_callback_string_destroy, |
| 714 |
aws_hash_callback_string_destroy); |
| 715 |
|
| 716 |
s_parse_options(argc, argv, &app_ctx); |
| 717 |
|
| 718 |
|
| 719 |
if (app_ctx.manual_write) { |
| 720 |
if (!strcmp(app_ctx.verb, "POST")) { |
| 721 |
fprintf(stderr, "Only POST requests allowed for manual_writes. Exiting... \n"); |
| 722 |
return 1; |
| 723 |
} |
| 724 |
fprintf(stderr, "Manual write mode enabled.\n"); |
| 725 |
fprintf(stderr, "Content-Length (leave empty for chunked transfer encoding): "); |
| 726 |
char cl_buf[64]; |
| 727 |
if (fgets(cl_buf, sizeof(cl_buf), stdin) && cl_buf[0] != '\n') { |
| 728 |
app_ctx.manual_write_content_length = (int64_t)atoll(cl_buf); |
| 729 |
app_ctx.manual_write_chunked = false; |
| 730 |
fprintf(stderr, "Using Content-Length: %" PRIi64 "\n", app_ctx.manual_write_content_length); |
| 731 |
} else { |
| 732 |
app_ctx.manual_write_chunked = true; |
| 733 |
fprintf(stderr, "Using chunked transfer encoding.\n"); |
| 734 |
} |
| 735 |
} |
| 736 |
|
| 737 |
struct aws_logger logger; |
| 738 |
AWS_ZERO_STRUCT(logger); |
| 739 |
|
| 740 |
if (app_ctx.log_level) { |
| 741 |
struct aws_logger_standard_options options = { |
| 742 |
.level = app_ctx.log_level, |
| 743 |
}; |
| 744 |
|
| 745 |
if (app_ctx.trace_file) { |
| 746 |
options.filename = app_ctx.trace_file; |
| 747 |
} else { |
| 748 |
options.file = stderr; |
| 749 |
} |
| 750 |
|
| 751 |
if (aws_logger_init_standard(&logger, allocator, &options)) { |
| 752 |
fprintf(stderr, "Failed to initialize logger with error %s\n", aws_error_debug_str(aws_last_error())); |
| 753 |
exit(1); |
| 754 |
} |
| 755 |
|
| 756 |
aws_logger_set(&logger); |
| 757 |
} |
| 758 |
|
| 759 |
bool use_tls = true; |
| 760 |
uint32_t port = 443; |
| 761 |
|
| 762 |
if (!app_ctx.uri.scheme.len && (app_ctx.uri.port == 80 || app_ctx.uri.port == 8080)) { |
| 763 |
use_tls = false; |
| 764 |
} else { |
| 765 |
if (aws_byte_cursor_eq_c_str_ignore_case(&app_ctx.uri.scheme, "http")) { |
| 766 |
use_tls = false; |
| 767 |
} |
| 768 |
} |
| 769 |
|
| 770 |
struct aws_tls_ctx *tls_ctx = NULL; |
| 771 |
struct aws_tls_ctx_options tls_ctx_options; |
| 772 |
AWS_ZERO_STRUCT(tls_ctx_options); |
| 773 |
struct aws_tls_connection_options tls_connection_options; |
| 774 |
AWS_ZERO_STRUCT(tls_connection_options); |
| 775 |
struct aws_tls_connection_options *tls_options = NULL; |
| 776 |
|
| 777 |
if (use_tls) { |
| 778 |
if (app_ctx.cert && app_ctx.key) { |
| 779 |
if (aws_tls_ctx_options_init_client_mtls_from_path( |
| 780 |
&tls_ctx_options, allocator, app_ctx.cert, app_ctx.key)) { |
| 781 |
fprintf( |
| 782 |
stderr, |
| 783 |
"Failed to load %s and %s with error %s.", |
| 784 |
app_ctx.cert, |
| 785 |
app_ctx.key, |
| 786 |
aws_error_debug_str(aws_last_error())); |
| 787 |
exit(1); |
| 788 |
} |
| 789 |
} |
| 790 |
#ifdef _WIN32 |
| 791 |
else if (app_ctx.cert && !app_ctx.key) { |
| 792 |
aws_tls_ctx_options_init_client_mtls_from_system_path(&tls_ctx_options, allocator, app_ctx.cert); |
| 793 |
} |
| 794 |
#endif |
| 795 |
else { |
| 796 |
aws_tls_ctx_options_init_default_client(&tls_ctx_options, allocator); |
| 797 |
} |
| 798 |
|
| 799 |
if (app_ctx.capath || app_ctx.cacert) { |
| 800 |
if (aws_tls_ctx_options_override_default_trust_store_from_path( |
| 801 |
&tls_ctx_options, app_ctx.capath, app_ctx.cacert)) { |
| 802 |
fprintf( |
| 803 |
stderr, |
| 804 |
"Failed to load %s and %s with error %s", |
| 805 |
app_ctx.capath, |
| 806 |
app_ctx.cacert, |
| 807 |
aws_error_debug_str(aws_last_error())); |
| 808 |
exit(1); |
| 809 |
} |
| 810 |
} |
| 811 |
|
| 812 |
if (app_ctx.insecure) { |
| 813 |
aws_tls_ctx_options_set_verify_peer(&tls_ctx_options, false); |
| 814 |
} |
| 815 |
|
| 816 |
if (aws_tls_ctx_options_set_alpn_list(&tls_ctx_options, app_ctx.alpn)) { |
| 817 |
fprintf(stderr, "Failed to load alpn list with error %s.", aws_error_debug_str(aws_last_error())); |
| 818 |
exit(1); |
| 819 |
} |
| 820 |
|
| 821 |
tls_ctx = aws_tls_client_ctx_new(allocator, &tls_ctx_options); |
| 822 |
|
| 823 |
if (!tls_ctx) { |
| 824 |
fprintf(stderr, "Failed to initialize TLS context with error %s.", aws_error_debug_str(aws_last_error())); |
| 825 |
exit(1); |
| 826 |
} |
| 827 |
|
| 828 |
aws_tls_connection_options_init_from_ctx(&tls_connection_options, tls_ctx); |
| 829 |
if (aws_tls_connection_options_set_server_name(&tls_connection_options, allocator, &app_ctx.uri.host_name)) { |
| 830 |
fprintf(stderr, "Failed to set servername with error %s.", aws_error_debug_str(aws_last_error())); |
| 831 |
exit(1); |
| 832 |
} |
| 833 |
tls_options = &tls_connection_options; |
| 834 |
|
| 835 |
if (app_ctx.uri.port) { |
| 836 |
port = app_ctx.uri.port; |
| 837 |
} |
| 838 |
} else { |
| 839 |
port = 80; |
| 840 |
if (app_ctx.uri.port) { |
| 841 |
port = app_ctx.uri.port; |
| 842 |
} |
| 843 |
} |
| 844 |
|
| 845 |
struct aws_event_loop_group *el_group = aws_event_loop_group_new_default(allocator, 1, NULL); |
| 846 |
|
| 847 |
struct aws_host_resolver_default_options resolver_options = { |
| 848 |
.el_group = el_group, |
| 849 |
.max_entries = 8, |
| 850 |
}; |
| 851 |
|
| 852 |
struct aws_host_resolver *resolver = aws_host_resolver_new_default(allocator, &resolver_options); |
| 853 |
|
| 854 |
struct aws_client_bootstrap_options bootstrap_options = { |
| 855 |
.event_loop_group = el_group, |
| 856 |
.host_resolver = resolver, |
| 857 |
}; |
| 858 |
struct aws_client_bootstrap *bootstrap = aws_client_bootstrap_new(allocator, &bootstrap_options); |
| 859 |
|
| 860 |
struct aws_socket_options socket_options = { |
| 861 |
.type = AWS_SOCKET_STREAM, |
| 862 |
.connect_timeout_ms = (uint32_t)app_ctx.connect_timeout, |
| 863 |
.keep_alive_timeout_sec = 0, |
| 864 |
.keepalive = false, |
| 865 |
.keep_alive_interval_sec = 0, |
| 866 |
}; |
| 867 |
|
| 868 |
struct aws_http_client_connection_options http_client_options = { |
| 869 |
.self_size = sizeof(struct aws_http_client_connection_options), |
| 870 |
.socket_options = &socket_options, |
| 871 |
.allocator = allocator, |
| 872 |
.port = port, |
| 873 |
.host_name = app_ctx.uri.host_name, |
| 874 |
.bootstrap = bootstrap, |
| 875 |
.initial_window_size = SIZE_MAX, |
| 876 |
.tls_options = tls_options, |
| 877 |
.user_data = &app_ctx, |
| 878 |
.on_setup = s_on_client_connection_setup, |
| 879 |
.on_shutdown = s_on_client_connection_shutdown, |
| 880 |
}; |
| 881 |
if (app_ctx.required_http_version == AWS_HTTP_VERSION_2 && !use_tls) { |
| 882 |
|
| 883 |
http_client_options.prior_knowledge_http2 = true; |
| 884 |
} |
| 885 |
aws_http_client_connect(&http_client_options); |
| 886 |
if (app_ctx.manual_write) { |
| 887 |
s_manual_write_loop(&app_ctx); |
| 888 |
} |
| 889 |
aws_mutex_lock(&app_ctx.mutex); |
| 890 |
aws_condition_variable_wait_pred(&app_ctx.c_var, &app_ctx.mutex, s_completion_predicate, &app_ctx); |
| 891 |
aws_mutex_unlock(&app_ctx.mutex); |
| 892 |
|
| 893 |
aws_client_bootstrap_release(bootstrap); |
| 894 |
aws_host_resolver_release(resolver); |
| 895 |
aws_event_loop_group_release(el_group); |
| 896 |
|
| 897 |
if (tls_ctx) { |
| 898 |
aws_tls_connection_options_clean_up(&tls_connection_options); |
| 899 |
aws_tls_ctx_release(tls_ctx); |
| 900 |
aws_tls_ctx_options_clean_up(&tls_ctx_options); |
| 901 |
} |
| 902 |
|
| 903 |
aws_http_library_clean_up(); |
| 904 |
|
| 905 |
if (app_ctx.log_level) { |
| 906 |
aws_logger_clean_up(&logger); |
| 907 |
} |
| 908 |
|
| 909 |
aws_uri_clean_up(&app_ctx.uri); |
| 910 |
|
| 911 |
aws_http_message_destroy(app_ctx.request); |
| 912 |
|
| 913 |
aws_shared_library_clean_up(&app_ctx.signing_library); |
| 914 |
|
| 915 |
if (app_ctx.output != stdout) { |
| 916 |
fclose(app_ctx.output); |
| 917 |
} |
| 918 |
|
| 919 |
if (app_ctx.input_body) { |
| 920 |
aws_input_stream_release(app_ctx.input_body); |
| 921 |
} |
| 922 |
|
| 923 |
if (app_ctx.input_file) { |
| 924 |
fclose(app_ctx.input_file); |
| 925 |
} |
| 926 |
|
| 927 |
aws_hash_table_clean_up(&app_ctx.signing_context); |
| 928 |
|
| 929 |
return 0; |
| 930 |
} |