Debian Codegraph @

crt/aws-c-http/source/proxy_connection.c

raw symbols SCIP
1 /**
2 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3 * SPDX-License-Identifier: Apache-2.0.
4 */
5
6 #include <aws/http/private/no_proxy.h>
7 #include <aws/http/private/proxy_impl.h>
8
9 #include <aws/common/encoding.h>
10 #include <aws/common/environment.h>
11 #include <aws/common/hash_table.h>
12 #include <aws/common/string.h>
13 #include <aws/http/connection_manager.h>
14 #include <aws/http/private/connection_impl.h>
15 #include <aws/http/request_response.h>
16 #include <aws/io/channel.h>
17 #include <aws/io/logging.h>
18 #include <aws/io/tls_channel_handler.h>
19 #include <aws/io/uri.h>
20
21 #ifdef _MSC_VER
22 # pragma warning(disable : 4204) /* non-constant aggregate initializer */
23 # pragma warning(disable : 4232) /* function pointer to dll symbol */
24 #endif
25
26 AWS_STATIC_STRING_FROM_LITERAL(s_host_header_name, "Host");
27 AWS_STATIC_STRING_FROM_LITERAL(s_proxy_connection_header_name, "Proxy-Connection");
28 AWS_STATIC_STRING_FROM_LITERAL(s_proxy_connection_header_value, "Keep-Alive");
29 AWS_STATIC_STRING_FROM_LITERAL(s_options_method, "OPTIONS");
30 AWS_STATIC_STRING_FROM_LITERAL(s_star_path, "*");
31
32 static const char *s_http_proxy_env_var = "HTTP_PROXY";
33 static const char *s_http_proxy_env_var_low = "http_proxy";
34 static const char *s_https_proxy_env_var = "HTTPS_PROXY";
35 static const char *s_https_proxy_env_var_low = "https_proxy";
36 static const char *s_no_proxy_env_var = "NO_PROXY";
37 static const char *s_no_proxy_env_var_low = "no_proxy";
38
39 #ifndef BYO_CRYPTO
40 AWS_STATIC_STRING_FROM_LITERAL(s_proxy_no_verify_peer_env_var, "AWS_PROXY_NO_VERIFY_PEER");
41 #endif
42
43 static struct aws_http_proxy_system_vtable s_default_vtable = {
44 .aws_channel_setup_client_tls = &aws_channel_setup_client_tls,
45 };
46
47 static struct aws_http_proxy_system_vtable *s_vtable = &s_default_vtable;
48
49 void aws_http_proxy_system_set_vtable(struct aws_http_proxy_system_vtable *vtable) {
50 s_vtable = vtable;
51 }
52
53 void aws_http_proxy_user_data_destroy(struct aws_http_proxy_user_data *user_data) {
54 if (user_data == NULL) {
55 return;
56 }
57 aws_hash_table_clean_up(&user_data->alpn_string_map);
58
59 /*
60 * For tunneling connections, this is now internal and never surfaced to the user, so it's our responsibility
61 * to clean up the last reference.
62 */
63 if (user_data->proxy_connection != NULL && user_data->proxy_config->connection_type == AWS_HPCT_HTTP_TUNNEL) {
64 aws_http_connection_release(user_data->proxy_connection);
65 user_data->proxy_connection = NULL;
66 }
67
68 aws_string_destroy(user_data->original_host);
69 if (user_data->proxy_config) {
70 aws_http_proxy_config_destroy(user_data->proxy_config);
71 }
72
73 if (user_data->original_tls_options) {
74 aws_tls_connection_options_clean_up(user_data->original_tls_options);
75 aws_mem_release(user_data->allocator, user_data->original_tls_options);
76 }
77
78 aws_http_proxy_negotiator_release(user_data->proxy_negotiator);
79
80 aws_client_bootstrap_release(user_data->original_bootstrap);
81
82 aws_mem_release(user_data->allocator, user_data);
83 }
84
85 struct aws_http_proxy_user_data *aws_http_proxy_user_data_new(
86 struct aws_allocator *allocator,
87 const struct aws_http_client_connection_options *orig_options,
88 aws_client_bootstrap_on_channel_event_fn *on_channel_setup,
89 aws_client_bootstrap_on_channel_event_fn *on_channel_shutdown) {
90
91 AWS_FATAL_ASSERT(orig_options->proxy_options != NULL);
92 /* make copy of options, and add defaults for missing optional structs */
93 struct aws_http_client_connection_options options = *orig_options;
94
95 struct aws_http1_connection_options default_http1_options;
96 AWS_ZERO_STRUCT(default_http1_options);
97 if (options.http1_options == NULL) {
98 options.http1_options = &default_http1_options;
99 }
100
101 struct aws_http2_connection_options default_http2_options;
102 AWS_ZERO_STRUCT(default_http2_options);
103 if (options.http2_options == NULL) {
104 options.http2_options = &default_http2_options;
105 }
106
107 struct aws_http2_setting *setting_array = NULL;
108 struct aws_http_proxy_user_data *user_data = NULL;
109 aws_mem_acquire_many(
110 options.allocator,
111 2,
112 &user_data,
113 sizeof(struct aws_http_proxy_user_data),
114 &setting_array,
115 options.http2_options->num_initial_settings * sizeof(struct aws_http2_setting));
116 AWS_ZERO_STRUCT(*user_data);
117
118 user_data->allocator = allocator;
119 user_data->state = AWS_PBS_SOCKET_CONNECT;
120 user_data->error_code = AWS_ERROR_SUCCESS;
121 user_data->connect_status_code = AWS_HTTP_STATUS_CODE_UNKNOWN;
122 user_data->original_bootstrap = aws_client_bootstrap_acquire(options.bootstrap);
123 if (options.socket_options != NULL) {
124 user_data->original_socket_options = *options.socket_options;
125 }
126 user_data->original_manual_window_management = options.manual_window_management;
127 user_data->original_initial_window_size = options.initial_window_size;
128
129 user_data->original_host = aws_string_new_from_cursor(allocator, &options.host_name);
130 if (user_data->original_host == NULL) {
131 goto on_error;
132 }
133
134 user_data->original_port = options.port;
135
136 user_data->proxy_config = aws_http_proxy_config_new_from_connection_options(allocator, &options);
137 if (user_data->proxy_config == NULL) {
138 goto on_error;
139 }
140
141 user_data->proxy_negotiator =
142 aws_http_proxy_strategy_create_negotiator(user_data->proxy_config->proxy_strategy, allocator);
143 if (user_data->proxy_negotiator == NULL) {
144 goto on_error;
145 }
146
147 if (options.tls_options) {
148 /* clone tls options, but redirect user data to what we're creating */
149 user_data->original_tls_options = aws_mem_calloc(allocator, 1, sizeof(struct aws_tls_connection_options));
150 if (user_data->original_tls_options == NULL ||
151 aws_tls_connection_options_copy(user_data->original_tls_options, options.tls_options)) {
152 goto on_error;
153 }
154
155 user_data->original_tls_options->user_data = user_data;
156 }
157
158 if (aws_http_alpn_map_init_copy(options.allocator, &user_data->alpn_string_map, options.alpn_string_map)) {
159 goto on_error;
160 }
161
162 user_data->original_http_on_setup = options.on_setup;
163 user_data->original_http_on_shutdown = options.on_shutdown;
164 user_data->original_channel_on_setup = on_channel_setup;
165 user_data->original_channel_on_shutdown = on_channel_shutdown;
166 user_data->requested_event_loop = options.requested_event_loop;
167 user_data->host_resolution_config = options.host_resolution_config;
168 user_data->prior_knowledge_http2 = options.prior_knowledge_http2;
169
170 /* one and only one setup callback must be valid */
171 AWS_FATAL_ASSERT((user_data->original_http_on_setup == NULL) != (user_data->original_channel_on_setup == NULL));
172
173 /* one and only one shutdown callback must be valid */
174 AWS_FATAL_ASSERT(
175 (user_data->original_http_on_shutdown == NULL) != (user_data->original_channel_on_shutdown == NULL));
176
177 /* callback set must be self-consistent. Technically the second check is redundant given the previous checks */
178 AWS_FATAL_ASSERT((user_data->original_http_on_setup == NULL) == (user_data->original_http_on_shutdown == NULL));
179 AWS_FATAL_ASSERT(
180 (user_data->original_channel_on_setup == NULL) == (user_data->original_channel_on_shutdown == NULL));
181
182 user_data->original_user_data = options.user_data;
183 user_data->original_http1_options = *options.http1_options;
184 user_data->original_http2_options = *options.http2_options;
185
186 /* keep a copy of the settings array if it's not NULL */
187 if (options.http2_options->num_initial_settings > 0) {
188 memcpy(
189 setting_array,
190 options.http2_options->initial_settings_array,
191 options.http2_options->num_initial_settings * sizeof(struct aws_http2_setting));
192 user_data->original_http2_options.initial_settings_array = setting_array;
193 }
194
195 return user_data;
196
197 on_error:
198
199 AWS_LOGF_ERROR(
200 AWS_LS_HTTP_CONNECTION,
201 "(STATIC) Proxy connection failed to create user data with error %d(%s)",
202 aws_last_error(),
203 aws_error_str(aws_last_error()));
204
205 aws_http_proxy_user_data_destroy(user_data);
206
207 return NULL;
208 }
209
210 struct aws_http_proxy_user_data *aws_http_proxy_user_data_new_reset_clone(
211 struct aws_allocator *allocator,
212 struct aws_http_proxy_user_data *old_user_data) {
213
214 AWS_FATAL_ASSERT(old_user_data != NULL);
215
216 struct aws_http2_setting *setting_array = NULL;
217 struct aws_http_proxy_user_data *user_data = NULL;
218 aws_mem_acquire_many(
219 allocator,
220 2,
221 &user_data,
222 sizeof(struct aws_http_proxy_user_data),
223 &setting_array,
224 old_user_data->original_http2_options.num_initial_settings * sizeof(struct aws_http2_setting));
225
226 AWS_ZERO_STRUCT(*user_data);
227 user_data->allocator = allocator;
228 user_data->state = AWS_PBS_SOCKET_CONNECT;
229 user_data->error_code = AWS_ERROR_SUCCESS;
230 user_data->connect_status_code = AWS_HTTP_STATUS_CODE_UNKNOWN;
231 user_data->original_bootstrap = aws_client_bootstrap_acquire(old_user_data->original_bootstrap);
232 user_data->original_socket_options = old_user_data->original_socket_options;
233 user_data->original_manual_window_management = old_user_data->original_manual_window_management;
234 user_data->original_initial_window_size = old_user_data->original_initial_window_size;
235 user_data->prior_knowledge_http2 = old_user_data->prior_knowledge_http2;
236
237 user_data->original_host = aws_string_new_from_string(allocator, old_user_data->original_host);
238 if (user_data->original_host == NULL) {
239 goto on_error;
240 }
241
242 user_data->original_port = old_user_data->original_port;
243
244 user_data->proxy_config = aws_http_proxy_config_new_clone(allocator, old_user_data->proxy_config);
245 if (user_data->proxy_config == NULL) {
246 goto on_error;
247 }
248
249 user_data->proxy_negotiator = aws_http_proxy_negotiator_acquire(old_user_data->proxy_negotiator);
250 if (user_data->proxy_negotiator == NULL) {
251 goto on_error;
252 }
253
254 if (old_user_data->original_tls_options) {
255 /* clone tls options, but redirect user data to what we're creating */
256 user_data->original_tls_options = aws_mem_calloc(allocator, 1, sizeof(struct aws_tls_connection_options));
257 if (user_data->original_tls_options == NULL ||
258 aws_tls_connection_options_copy(user_data->original_tls_options, old_user_data->original_tls_options)) {
259 goto on_error;
260 }
261
262 user_data->original_tls_options->user_data = user_data;
263 }
264
265 if (aws_http_alpn_map_init_copy(allocator, &user_data->alpn_string_map, &old_user_data->alpn_string_map)) {
266 goto on_error;
267 }
268
269 user_data->original_http_on_setup = old_user_data->original_http_on_setup;
270 user_data->original_http_on_shutdown = old_user_data->original_http_on_shutdown;
271 user_data->original_channel_on_setup = old_user_data->original_channel_on_setup;
272 user_data->original_channel_on_shutdown = old_user_data->original_channel_on_shutdown;
273 user_data->original_user_data = old_user_data->original_user_data;
274 user_data->original_http1_options = old_user_data->original_http1_options;
275 user_data->original_http2_options = old_user_data->original_http2_options;
276
277 /* keep a copy of the settings array if it's not NULL */
278 if (old_user_data->original_http2_options.num_initial_settings > 0) {
279 memcpy(
280 setting_array,
281 old_user_data->original_http2_options.initial_settings_array,
282 old_user_data->original_http2_options.num_initial_settings * sizeof(struct aws_http2_setting));
283 user_data->original_http2_options.initial_settings_array = setting_array;
284 }
285
286 return user_data;
287
288 on_error:
289
290 AWS_LOGF_ERROR(
291 AWS_LS_HTTP_CONNECTION,
292 "(STATIC) Proxy connection failed to create user data with error %d(%s)",
293 aws_last_error(),
294 aws_error_str(aws_last_error()));
295
296 aws_http_proxy_user_data_destroy(user_data);
297
298 return NULL;
299 }
300
301 /*
302 * Examines the proxy user data state and determines whether to make an http-interface setup callback
303 * or a raw channel setup callback
304 */
305 static void s_do_on_setup_callback(
306 struct aws_http_proxy_user_data *proxy_ud,
307 struct aws_http_connection *connection,
308 int error_code) {
309 if (proxy_ud->original_http_on_setup) {
310 proxy_ud->original_http_on_setup(connection, error_code, proxy_ud->original_user_data);
311 proxy_ud->original_http_on_setup = NULL;
312 }
313
314 if (proxy_ud->original_channel_on_setup) {
315 struct aws_channel *channel = NULL;
316 if (connection != NULL) {
317 channel = aws_http_connection_get_channel(connection);
318 }
319 proxy_ud->original_channel_on_setup(
320 proxy_ud->original_bootstrap, error_code, channel, proxy_ud->original_user_data);
321 proxy_ud->original_channel_on_setup = NULL;
322 }
323 }
324
325 /*
326 * Examines the proxy user data state and determines whether to make an http-interface shutdown callback
327 * or a raw channel shutdown callback
328 */
329 static void s_do_on_shutdown_callback(struct aws_http_proxy_user_data *proxy_ud, int error_code) {
330 AWS_FATAL_ASSERT(proxy_ud->proxy_connection);
331
332 if (proxy_ud->original_http_on_shutdown) {
333 AWS_FATAL_ASSERT(proxy_ud->final_connection);
334 proxy_ud->original_http_on_shutdown(proxy_ud->final_connection, error_code, proxy_ud->original_user_data);
335 proxy_ud->original_http_on_shutdown = NULL;
336 }
337
338 if (proxy_ud->original_channel_on_shutdown) {
339 struct aws_channel *channel = aws_http_connection_get_channel(proxy_ud->proxy_connection);
340 proxy_ud->original_channel_on_shutdown(
341 proxy_ud->original_bootstrap, error_code, channel, proxy_ud->original_user_data);
342 proxy_ud->original_channel_on_shutdown = NULL;
343 }
344 }
345
346 /*
347 * Connection callback used ONLY by forwarding http proxy connections. After this,
348 * the connection is live and the user is notified
349 */
350 static void s_aws_http_on_client_connection_http_forwarding_proxy_setup_fn(
351 struct aws_http_connection *connection,
352 int error_code,
353 void *user_data) {
354 struct aws_http_proxy_user_data *proxy_ud = user_data;
355
356 s_do_on_setup_callback(proxy_ud, connection, error_code);
357
358 if (error_code != AWS_ERROR_SUCCESS) {
359 aws_http_proxy_user_data_destroy(user_data);
360 } else {
361 /*
362 * The proxy connection and final connection are the same in forwarding proxy connections. This lets
363 * us unconditionally use fatal asserts on these being non-null regardless of proxy configuration.
364 */
365 proxy_ud->proxy_connection = connection;
366 proxy_ud->final_connection = connection;
367 proxy_ud->state = AWS_PBS_SUCCESS;
368 }
369 }
370
371 /*
372 * Connection shutdown callback used by both http and https proxy connections. Only invokes
373 * user shutdown if the connection was successfully established. Otherwise, it invokes
374 * the user setup function with an error.
375 */
376 static void s_aws_http_on_client_connection_http_proxy_shutdown_fn(
377 struct aws_http_connection *connection,
378 int error_code,
379 void *user_data) {
380
381 struct aws_http_proxy_user_data *proxy_ud = user_data;
382
383 if (proxy_ud->state == AWS_PBS_SUCCESS) {
384 AWS_LOGF_INFO(
385 AWS_LS_HTTP_CONNECTION,
386 "(%p) Proxy connection (channel %p) shutting down.",
387 (void *)connection,
388 (void *)aws_http_connection_get_channel(connection));
389 s_do_on_shutdown_callback(proxy_ud, error_code);
390 } else {
391 int ec = error_code;
392 if (ec == AWS_ERROR_SUCCESS) {
393 ec = proxy_ud->error_code;
394 }
395 if (ec == AWS_ERROR_SUCCESS) {
396 ec = AWS_ERROR_UNKNOWN;
397 }
398
399 AWS_LOGF_WARN(
400 AWS_LS_HTTP_CONNECTION,
401 "(%p) Error %d while connecting to \"%s\" via proxy.",
402 (void *)connection,
403 ec,
404 (char *)proxy_ud->original_host->bytes);
405
406 s_do_on_setup_callback(proxy_ud, NULL, ec);
407 }
408
409 aws_http_proxy_user_data_destroy(user_data);
410 }
411
412 /*
413 * On-any-error entry point that releases all resources involved in establishing the proxy connection.
414 * This must not be invoked any time after a successful setup callback.
415 */
416 static void s_aws_http_proxy_user_data_shutdown(struct aws_http_proxy_user_data *user_data) {
417
418 user_data->state = AWS_PBS_FAILURE;
419
420 if (user_data->proxy_connection == NULL) {
421 s_do_on_setup_callback(user_data, NULL, user_data->error_code);
422 aws_http_proxy_user_data_destroy(user_data);
423 return;
424 }
425
426 if (user_data->connect_stream) {
427 aws_http_stream_release(user_data->connect_stream);
428 user_data->connect_stream = NULL;
429 }
430
431 if (user_data->connect_request) {
432 aws_http_message_destroy(user_data->connect_request);
433 user_data->connect_request = NULL;
434 }
435
436 struct aws_http_connection *http_connection = user_data->proxy_connection;
437 user_data->proxy_connection = NULL;
438
439 aws_channel_shutdown(http_connection->channel_slot->channel, user_data->error_code);
440 aws_http_connection_release(http_connection);
441 }
442
443 static struct aws_http_message *s_build_h1_proxy_connect_request(struct aws_http_proxy_user_data *user_data) {
444 struct aws_http_message *request = aws_http_message_new_request(user_data->allocator);
445 if (request == NULL) {
446 return NULL;
447 }
448
449 struct aws_byte_buf path_buffer;
450 AWS_ZERO_STRUCT(path_buffer);
451
452 if (aws_http_message_set_request_method(request, aws_http_method_connect)) {
453 goto on_error;
454 }
455
456 if (aws_byte_buf_init(&path_buffer, user_data->allocator, user_data->original_host->len + 10)) {
457 goto on_error;
458 }
459
460 struct aws_byte_cursor host_cursor = aws_byte_cursor_from_string(user_data->original_host);
461 if (aws_byte_buf_append(&path_buffer, &host_cursor)) {
462 goto on_error;
463 }
464
465 struct aws_byte_cursor colon_cursor = aws_byte_cursor_from_c_str(":");
466 if (aws_byte_buf_append(&path_buffer, &colon_cursor)) {
467 goto on_error;
468 }
469
470 char port_str[20] = "\0";
471 snprintf(port_str, sizeof(port_str), "%u", user_data->original_port);
472 struct aws_byte_cursor port_cursor = aws_byte_cursor_from_c_str(port_str);
473 if (aws_byte_buf_append(&path_buffer, &port_cursor)) {
474 goto on_error;
475 }
476
477 struct aws_byte_cursor path_cursor = aws_byte_cursor_from_array(path_buffer.buffer, path_buffer.len);
478 if (aws_http_message_set_request_path(request, path_cursor)) {
479 goto on_error;
480 }
481
482 struct aws_http_header host_header = {
483 .name = aws_byte_cursor_from_string(s_host_header_name),
484 .value = aws_byte_cursor_from_array(path_buffer.buffer, path_buffer.len),
485 };
486 if (aws_http_message_add_header(request, host_header)) {
487 goto on_error;
488 }
489
490 struct aws_http_header keep_alive_header = {
491 .name = aws_byte_cursor_from_string(s_proxy_connection_header_name),
492 .value = aws_byte_cursor_from_string(s_proxy_connection_header_value),
493 };
494 if (aws_http_message_add_header(request, keep_alive_header)) {
495 goto on_error;
496 }
497
498 aws_byte_buf_clean_up(&path_buffer);
499
500 return request;
501
502 on_error:
503
504 AWS_LOGF_ERROR(
505 AWS_LS_HTTP_CONNECTION,
506 "(%p) TLS proxy connection failed to build CONNECT request with error %d(%s)",
507 (void *)user_data->proxy_connection,
508 aws_last_error(),
509 aws_error_str(aws_last_error()));
510
511 aws_byte_buf_clean_up(&path_buffer);
512 aws_http_message_destroy(request);
513
514 return NULL;
515 }
516
517 /*
518 * Builds the CONNECT request issued after proxy connection establishment, during the creation of
519 * tls-enabled proxy connections.
520 */
521 static struct aws_http_message *s_build_proxy_connect_request(struct aws_http_proxy_user_data *user_data) {
522 struct aws_http_connection *proxy_connection = user_data->proxy_connection;
523 switch (proxy_connection->http_version) {
524 case AWS_HTTP_VERSION_1_1:
525 return s_build_h1_proxy_connect_request(user_data);
526 default:
527 aws_raise_error(AWS_ERROR_HTTP_UNSUPPORTED_PROTOCOL);
528 return NULL;
529 }
530 }
531
532 static int s_aws_http_on_incoming_body_tunnel_proxy(
533 struct aws_http_stream *stream,
534 const struct aws_byte_cursor *data,
535 void *user_data) {
536 (void)stream;
537
538 struct aws_http_proxy_user_data *context = user_data;
539 aws_http_proxy_negotiator_connect_on_incoming_body_fn *on_incoming_body =
540 context->proxy_negotiator->strategy_vtable.tunnelling_vtable->on_incoming_body_callback;
541 if (on_incoming_body != NULL) {
542 (*on_incoming_body)(context->proxy_negotiator, data);
543 }
544
545 aws_http_stream_update_window(stream, data->len);
546
547 return AWS_OP_SUCCESS;
548 }
549
550 static int s_aws_http_on_response_headers_tunnel_proxy(
551 struct aws_http_stream *stream,
552 enum aws_http_header_block header_block,
553 const struct aws_http_header *header_array,
554 size_t num_headers,
555 void *user_data) {
556 (void)stream;
557
558 struct aws_http_proxy_user_data *context = user_data;
559 aws_http_proxy_negotiation_connect_on_incoming_headers_fn *on_incoming_headers =
560 context->proxy_negotiator->strategy_vtable.tunnelling_vtable->on_incoming_headers_callback;
561 if (on_incoming_headers != NULL) {
562 (*on_incoming_headers)(context->proxy_negotiator, header_block, header_array, num_headers);
563 }
564
565 return AWS_OP_SUCCESS;
566 }
567
568 /*
569 * Headers done callback for the CONNECT request made during tls proxy connections
570 */
571 static int s_aws_http_on_incoming_header_block_done_tunnel_proxy(
572 struct aws_http_stream *stream,
573 enum aws_http_header_block header_block,
574 void *user_data) {
575
576 struct aws_http_proxy_user_data *context = user_data;
577
578 if (header_block == AWS_HTTP_HEADER_BLOCK_MAIN) {
579 int status_code = AWS_HTTP_STATUS_CODE_UNKNOWN;
580 aws_http_stream_get_incoming_response_status(stream, &status_code);
581 context->connect_status_code = (enum aws_http_status_code)status_code;
582 if (context->connect_status_code != AWS_HTTP_STATUS_CODE_200_OK) {
583 AWS_LOGF_ERROR(
584 AWS_LS_HTTP_CONNECTION,
585 "(%p) Proxy CONNECT request failed with status code %d",
586 (void *)context->proxy_connection,
587 context->connect_status_code);
588 context->error_code = AWS_ERROR_HTTP_PROXY_CONNECT_FAILED;
589 }
590
591 aws_http_proxy_negotiator_connect_status_fn *on_status =
592 context->proxy_negotiator->strategy_vtable.tunnelling_vtable->on_status_callback;
593 if (on_status != NULL) {
594 (*on_status)(context->proxy_negotiator, context->connect_status_code);
595 }
596 }
597
598 return AWS_OP_SUCCESS;
599 }
600
601 static int s_aws_http_apply_http_connection_to_proxied_channel(struct aws_http_proxy_user_data *context) {
602 AWS_FATAL_ASSERT(context->proxy_connection != NULL);
603 AWS_FATAL_ASSERT(context->original_http_on_setup != NULL);
604
605 struct aws_channel *channel = aws_http_connection_get_channel(context->proxy_connection);
606
607 struct aws_http_connection *connection = aws_http_connection_new_channel_handler(
608 context->allocator,
609 channel,
610 false,
611 context->original_tls_options != NULL,
612 context->original_manual_window_management,
613 context->prior_knowledge_http2,
614 context->original_initial_window_size,
615 context->alpn_string_map.p_impl == NULL ? NULL : &context->alpn_string_map,
616 &context->original_http1_options,
617 &context->original_http2_options,
618 context->original_user_data);
619 if (connection == NULL) {
620 AWS_LOGF_ERROR(
621 AWS_LS_HTTP_CONNECTION,
622 "static: Failed to create the client connection object, error %d (%s).",
623 aws_last_error(),
624 aws_error_name(aws_last_error()));
625
626 return AWS_OP_ERR;
627 }
628
629 AWS_LOGF_INFO(
630 AWS_LS_HTTP_CONNECTION,
631 "id=%p: " PRInSTR " client connection established.",
632 (void *)connection,
633 AWS_BYTE_CURSOR_PRI(aws_http_version_to_str(connection->http_version)));
634
635 context->final_connection = connection;
636
637 return AWS_OP_SUCCESS;
638 }
639
640 static void s_do_final_proxied_channel_setup(struct aws_http_proxy_user_data *proxy_ud) {
641 if (proxy_ud->original_http_on_setup != NULL) {
642 /*
643 * If we're transitioning to http with http setup/shutdown callbacks, try to apply a new http connection to
644 * the channel
645 */
646 if (s_aws_http_apply_http_connection_to_proxied_channel(proxy_ud)) {
647 proxy_ud->error_code = aws_last_error();
648 s_aws_http_proxy_user_data_shutdown(proxy_ud);
649 return;
650 }
651
652 s_do_on_setup_callback(proxy_ud, proxy_ud->final_connection, AWS_ERROR_SUCCESS);
653 } else {
654 /*
655 * Otherwise invoke setup directly (which will end up being channel setup)
656 */
657 s_do_on_setup_callback(proxy_ud, proxy_ud->proxy_connection, AWS_ERROR_SUCCESS);
658 }
659
660 /* Tell user of successful connection. */
661 proxy_ud->state = AWS_PBS_SUCCESS;
662 }
663
664 /*
665 * Tls negotiation callback for tls proxy connections
666 */
667 static void s_on_origin_server_tls_negotation_result(
668 struct aws_channel_handler *handler,
669 struct aws_channel_slot *slot,
670 int error_code,
671 void *user_data) {
672
673 (void)handler;
674 (void)slot;
675
676 struct aws_http_proxy_user_data *context = user_data;
677 if (error_code != AWS_ERROR_SUCCESS) {
678 AWS_LOGF_ERROR(
679 AWS_LS_HTTP_CONNECTION,
680 "(%p) Proxy connection failed origin server TLS negotiation with error %d(%s)",
681 (void *)context->proxy_connection,
682 error_code,
683 aws_error_str(error_code));
684 context->error_code = error_code;
685 s_aws_http_proxy_user_data_shutdown(context);
686 return;
687 }
688
689 s_do_final_proxied_channel_setup(context);
690 }
691
692 static int s_create_tunneling_connection(struct aws_http_proxy_user_data *user_data);
693 static int s_make_proxy_connect_request(struct aws_http_proxy_user_data *user_data);
694
695 static void s_zero_callbacks(struct aws_http_proxy_user_data *proxy_ud) {
696 proxy_ud->original_http_on_shutdown = NULL;
697 proxy_ud->original_http_on_setup = NULL;
698 proxy_ud->original_channel_on_shutdown = NULL;
699 proxy_ud->original_channel_on_setup = NULL;
700 }
701
702 /*
703 * Stream done callback for the CONNECT request made during tls proxy connections
704 */
705 static void s_aws_http_on_stream_complete_tunnel_proxy(
706 struct aws_http_stream *stream,
707 int error_code,
708 void *user_data) {
709 struct aws_http_proxy_user_data *context = user_data;
710 AWS_FATAL_ASSERT(stream == context->connect_stream);
711
712 if (context->error_code == AWS_ERROR_SUCCESS && error_code != AWS_ERROR_SUCCESS) {
713 context->error_code = error_code;
714 }
715
716 if (context->error_code != AWS_ERROR_SUCCESS) {
717 context->error_code = AWS_ERROR_HTTP_PROXY_CONNECT_FAILED;
718 if (context->connect_status_code == AWS_HTTP_STATUS_CODE_407_PROXY_AUTHENTICATION_REQUIRED) {
719 enum aws_http_proxy_negotiation_retry_directive retry_directive =
720 aws_http_proxy_negotiator_get_retry_directive(context->proxy_negotiator);
721
722 if (retry_directive == AWS_HPNRD_NEW_CONNECTION) {
723 struct aws_http_proxy_user_data *new_context =
724 aws_http_proxy_user_data_new_reset_clone(context->allocator, context);
725 if (new_context != NULL && s_create_tunneling_connection(new_context) == AWS_OP_SUCCESS) {
726 /*
727 * We successfully kicked off a new connection. By NULLing the callbacks on the old one, we can
728 * shut it down quietly without the user being notified. The new connection will notify the user
729 * based on its success or failure.
730 */
731 s_zero_callbacks(context);
732 context->error_code = AWS_ERROR_HTTP_PROXY_CONNECT_FAILED_RETRYABLE;
733 }
734 } else if (retry_directive == AWS_HPNRD_CURRENT_CONNECTION) {
735 context->error_code = AWS_ERROR_SUCCESS;
736 if (s_make_proxy_connect_request(context) == AWS_OP_SUCCESS) {
737 return;
738 }
739 }
740 }
741
742 s_aws_http_proxy_user_data_shutdown(context);
743 return;
744 }
745
746 AWS_LOGF_INFO(
747 AWS_LS_HTTP_CONNECTION,
748 "(%p) Proxy connection made successful CONNECT request to \"%s\" via proxy",
749 (void *)context->proxy_connection,
750 context->original_host->bytes);
751
752 /*
753 * We're finished with these, let's release
754 */
755 aws_http_stream_release(stream);
756 context->connect_stream = NULL;
757 aws_http_message_destroy(context->connect_request);
758 context->connect_request = NULL;
759
760 AWS_LOGF_INFO(
761 AWS_LS_HTTP_CONNECTION, "(%p) Beginning TLS negotiation through proxy", (void *)context->proxy_connection);
762
763 if (context->original_tls_options != NULL) {
764 /*
765 * Perform TLS negotiation to the origin server through proxy
766 */
767 context->original_tls_options->on_negotiation_result = s_on_origin_server_tls_negotation_result;
768
769 context->state = AWS_PBS_TLS_NEGOTIATION;
770 struct aws_channel *channel = aws_http_connection_get_channel(context->proxy_connection);
771
772 struct aws_channel_slot *last_slot = aws_channel_get_first_slot(channel);
773 while (last_slot->adj_right != NULL) {
774 last_slot = last_slot->adj_right;
775 }
776
777 if (s_vtable->aws_channel_setup_client_tls(last_slot, context->original_tls_options)) {
778 AWS_LOGF_ERROR(
779 AWS_LS_HTTP_CONNECTION,
780 "(%p) Proxy connection failed to start TLS negotiation with error %d(%s)",
781 (void *)context->proxy_connection,
782 aws_last_error(),
783 aws_error_str(aws_last_error()));
784 s_aws_http_proxy_user_data_shutdown(context);
785 return;
786 }
787 } else {
788 s_do_final_proxied_channel_setup(context);
789 }
790 }
791
792 static void s_terminate_tunneling_connect(
793 struct aws_http_message *message,
794 int error_code,
795 void *internal_proxy_user_data) {
796 (void)message;
797
798 struct aws_http_proxy_user_data *proxy_ud = internal_proxy_user_data;
799
800 AWS_LOGF_ERROR(
801 AWS_LS_HTTP_CONNECTION,
802 "(%p) Tunneling proxy connection failed to create request stream for CONNECT request with error %d(%s)",
803 (void *)proxy_ud->proxy_connection,
804 error_code,
805 aws_error_str(error_code));
806
807 proxy_ud->error_code = error_code;
808 s_aws_http_proxy_user_data_shutdown(proxy_ud);
809 }
810
811 static void s_continue_tunneling_connect(struct aws_http_message *message, void *internal_proxy_user_data) {
812 struct aws_http_proxy_user_data *proxy_ud = internal_proxy_user_data;
813
814 struct aws_http_make_request_options request_options = {
815 .self_size = sizeof(request_options),
816 .request = message,
817 .user_data = proxy_ud,
818 .on_response_headers = s_aws_http_on_response_headers_tunnel_proxy,
819 .on_response_header_block_done = s_aws_http_on_incoming_header_block_done_tunnel_proxy,
820 .on_response_body = s_aws_http_on_incoming_body_tunnel_proxy,
821 .on_complete = s_aws_http_on_stream_complete_tunnel_proxy,
822 };
823
824 if (proxy_ud->connect_stream != NULL) {
825 aws_http_stream_release(proxy_ud->connect_stream);
826 }
827
828 proxy_ud->connect_stream = aws_http_connection_make_request(proxy_ud->proxy_connection, &request_options);
829 if (proxy_ud->connect_stream == NULL) {
830 goto on_error;
831 }
832
833 aws_http_stream_activate(proxy_ud->connect_stream);
834
835 return;
836
837 on_error:
838
839 s_aws_http_proxy_user_data_shutdown(proxy_ud);
840 }
841
842 /*
843 * Issues a CONNECT request on an http connection
844 */
845 static int s_make_proxy_connect_request(struct aws_http_proxy_user_data *user_data) {
846 if (user_data->connect_request != NULL) {
847 aws_http_message_destroy(user_data->connect_request);
848 user_data->connect_request = NULL;
849 }
850
851 user_data->connect_request = s_build_proxy_connect_request(user_data);
852 if (user_data->connect_request == NULL) {
853 return AWS_OP_ERR;
854 }
855
856 (*user_data->proxy_negotiator->strategy_vtable.tunnelling_vtable->connect_request_transform)(
857 user_data->proxy_negotiator,
858 user_data->connect_request,
859 s_terminate_tunneling_connect,
860 s_continue_tunneling_connect,
861 user_data);
862
863 return AWS_OP_SUCCESS;
864 }
865
866 /*
867 * Connection setup callback for tunneling proxy connections.
868 */
869 static void s_aws_http_on_client_connection_http_tunneling_proxy_setup_fn(
870 struct aws_http_connection *connection,
871 int error_code,
872 void *user_data) {
873
874 struct aws_http_proxy_user_data *proxy_ud = user_data;
875
876 proxy_ud->error_code = error_code;
877 if (error_code != AWS_ERROR_SUCCESS) {
878 goto on_error;
879 }
880
881 AWS_LOGF_INFO(AWS_LS_HTTP_CONNECTION, "(%p) Making CONNECT request to proxy", (void *)proxy_ud->proxy_connection);
882
883 proxy_ud->proxy_connection = connection;
884 proxy_ud->state = AWS_PBS_HTTP_CONNECT;
885 if (s_make_proxy_connect_request(proxy_ud)) {
886 goto on_error;
887 }
888
889 return;
890
891 on_error:
892
893 s_aws_http_proxy_user_data_shutdown(proxy_ud);
894 }
895
896 /*
897 * Checks for the special case when a request is an OPTIONS request with *
898 * path and no query params
899 */
900 static bool s_is_star_path_options_method(const struct aws_http_message *request) {
901 struct aws_byte_cursor method_cursor;
902 if (aws_http_message_get_request_method(request, &method_cursor)) {
903 return false;
904 }
905
906 struct aws_byte_cursor options_cursor = aws_byte_cursor_from_string(s_options_method);
907 if (!aws_byte_cursor_eq_ignore_case(&method_cursor, &options_cursor)) {
908 return false;
909 }
910
911 struct aws_byte_cursor path_cursor;
912 if (aws_http_message_get_request_path(request, &path_cursor)) {
913 return false;
914 }
915
916 struct aws_byte_cursor star_cursor = aws_byte_cursor_from_string(s_star_path);
917 if (!aws_byte_cursor_eq_ignore_case(&path_cursor, &star_cursor)) {
918 return false;
919 }
920
921 return true;
922 }
923
924 /*
925 * Modifies a requests uri by transforming it to absolute form according to
926 * section 5.3.2 of rfc 7230
927 *
928 * We do this by parsing the existing uri and then rebuilding it as an
929 * absolute resource path (using the original connection options)
930 */
931 int aws_http_rewrite_uri_for_proxy_request(
932 struct aws_http_message *request,
933 struct aws_http_proxy_user_data *proxy_user_data) {
934 int result = AWS_OP_ERR;
935
936 struct aws_uri target_uri;
937 AWS_ZERO_STRUCT(target_uri);
938
939 struct aws_byte_cursor path_cursor;
940 AWS_ZERO_STRUCT(path_cursor);
941
942 if (aws_http_message_get_request_path(request, &path_cursor)) {
943 goto done;
944 }
945
946 /* Pull out the original path/query */
947 struct aws_uri uri;
948 if (aws_uri_init_parse(&uri, proxy_user_data->allocator, &path_cursor)) {
949 goto done;
950 }
951
952 const struct aws_byte_cursor *actual_path_cursor = aws_uri_path(&uri);
953 const struct aws_byte_cursor *actual_query_cursor = aws_uri_query_string(&uri);
954
955 /* now rebuild the uri with scheme, host and port subbed in from the original connection options */
956 struct aws_uri_builder_options target_uri_builder;
957 AWS_ZERO_STRUCT(target_uri_builder);
958 target_uri_builder.scheme = aws_http_scheme_http;
959 target_uri_builder.path = *actual_path_cursor;
960 target_uri_builder.host_name = aws_byte_cursor_from_string(proxy_user_data->original_host);
961 target_uri_builder.port = proxy_user_data->original_port;
962 target_uri_builder.query_string = *actual_query_cursor;
963
964 if (aws_uri_init_from_builder_options(&target_uri, proxy_user_data->allocator, &target_uri_builder)) {
965 goto done;
966 }
967
968 struct aws_byte_cursor full_target_uri =
969 aws_byte_cursor_from_array(target_uri.uri_str.buffer, target_uri.uri_str.len);
970
971 /*
972 * By rfc 7230, Section 5.3.4, a star-pathed options request made through a proxy MUST be transformed (at the last
973 * proxy) back into a star-pathed request if the proxy request has an empty path and no query string. This
974 * is behavior we want to support. So from our side, we need to make sure that star-pathed options requests
975 * get translated into options requests with the authority as the uri and an empty path-query.
976 *
977 * Our URI transform always ends with a '/' which is technically not an empty path. To address this,
978 * the easiest thing to do is just detect if this was originally a star-pathed options request
979 * and drop the final '/' from the path.
980 */
981 if (s_is_star_path_options_method(request)) {
982 if (full_target_uri.len > 0 && *(full_target_uri.ptr + full_target_uri.len - 1) == '/') {
983 full_target_uri.len -= 1;
984 }
985 }
986
987 /* mutate the request with the new path value */
988 if (aws_http_message_set_request_path(request, full_target_uri)) {
989 goto done;
990 }
991
992 result = AWS_OP_SUCCESS;
993
994 done:
995
996 aws_uri_clean_up(&target_uri);
997 aws_uri_clean_up(&uri);
998
999 return result;
1000 }
1001
1002 /*
1003 * Plaintext proxy request transformation function
1004 *
1005 * Rewrites the target uri to absolute form and injects any desired headers
1006 */
1007 static int s_proxy_http_request_transform(struct aws_http_message *request, void *user_data) {
1008 struct aws_http_proxy_user_data *proxy_ud = user_data;
1009
1010 if (aws_http_rewrite_uri_for_proxy_request(request, proxy_ud)) {
1011 return AWS_OP_ERR;
1012 }
1013
1014 if ((*proxy_ud->proxy_negotiator->strategy_vtable.forwarding_vtable->forward_request_transform)(
1015 proxy_ud->proxy_negotiator, request)) {
1016 return AWS_OP_ERR;
1017 }
1018
1019 return AWS_OP_SUCCESS;
1020 }
1021
1022 /*
1023 * Top-level function to route a connection request through a proxy server, with no channel security
1024 */
1025 static int s_aws_http_client_connect_via_forwarding_proxy(const struct aws_http_client_connection_options *options) {
1026 AWS_FATAL_ASSERT(options->tls_options == NULL);
1027
1028 AWS_LOGF_INFO(
1029 AWS_LS_HTTP_CONNECTION,
1030 "(STATIC) Connecting to \"" PRInSTR "\" via proxy \"" PRInSTR "\"",
1031 AWS_BYTE_CURSOR_PRI(options->host_name),
1032 AWS_BYTE_CURSOR_PRI(options->proxy_options->host));
1033
1034 /* Create a wrapper user data that contains all proxy-related information, state, and user-facing callbacks */
1035 struct aws_http_proxy_user_data *proxy_user_data =
1036 aws_http_proxy_user_data_new(options->allocator, options, NULL, NULL);
1037 if (proxy_user_data == NULL) {
1038 return AWS_OP_ERR;
1039 }
1040
1041 AWS_FATAL_ASSERT(options->proxy_options != NULL);
1042
1043 /* Fill in a new connection options pointing at the proxy */
1044 struct aws_http_client_connection_options options_copy = *options;
1045
1046 options_copy.proxy_options = NULL;
1047 options_copy.host_name = options->proxy_options->host;
1048 options_copy.port = options->proxy_options->port;
1049 options_copy.user_data = proxy_user_data;
1050 options_copy.on_setup = s_aws_http_on_client_connection_http_forwarding_proxy_setup_fn;
1051 options_copy.on_shutdown = s_aws_http_on_client_connection_http_proxy_shutdown_fn;
1052 options_copy.tls_options = options->proxy_options->tls_options;
1053 options_copy.requested_event_loop = options->requested_event_loop;
1054 options_copy.host_resolution_config = options->host_resolution_config;
1055 options_copy.prior_knowledge_http2 = false; /* ToDo, expose the protocol specific config for proxy connection. */
1056
1057 int result = aws_http_client_connect_internal(&options_copy, s_proxy_http_request_transform);
1058 if (result == AWS_OP_ERR) {
1059 AWS_LOGF_ERROR(
1060 AWS_LS_HTTP_CONNECTION,
1061 "(STATIC) Proxy http connection failed client connect with error %d(%s)",
1062 aws_last_error(),
1063 aws_error_str(aws_last_error()));
1064
1065 aws_http_proxy_user_data_destroy(proxy_user_data);
1066 }
1067
1068 return result;
1069 }
1070
1071 static int s_create_tunneling_connection(struct aws_http_proxy_user_data *user_data) {
1072 struct aws_http_client_connection_options connect_options;
1073 AWS_ZERO_STRUCT(connect_options);
1074
1075 connect_options.self_size = sizeof(struct aws_http_client_connection_options);
1076 connect_options.allocator = user_data->allocator;
1077 connect_options.bootstrap = user_data->original_bootstrap;
1078 connect_options.host_name = aws_byte_cursor_from_buf(&user_data->proxy_config->host);
1079 connect_options.port = user_data->proxy_config->port;
1080 connect_options.socket_options = &user_data->original_socket_options;
1081 connect_options.tls_options = user_data->proxy_config->tls_options;
1082 connect_options.monitoring_options = NULL; /* ToDo */
1083 connect_options.manual_window_management = user_data->original_manual_window_management;
1084 connect_options.initial_window_size = user_data->original_initial_window_size;
1085 connect_options.user_data = user_data;
1086 connect_options.on_setup = s_aws_http_on_client_connection_http_tunneling_proxy_setup_fn;
1087 connect_options.on_shutdown = s_aws_http_on_client_connection_http_proxy_shutdown_fn;
1088 connect_options.http1_options = NULL; /* ToDo, expose the protocol specific config for proxy connection. */
1089 connect_options.http2_options = NULL; /* ToDo */
1090 connect_options.requested_event_loop = user_data->requested_event_loop;
1091 connect_options.host_resolution_config = user_data->host_resolution_config;
1092
1093 int result = aws_http_client_connect(&connect_options);
1094 if (result == AWS_OP_ERR) {
1095 AWS_LOGF_ERROR(
1096 AWS_LS_HTTP_CONNECTION,
1097 "(STATIC) Proxy tunnel connection failed client connect with error %d(%s)",
1098 aws_last_error(),
1099 aws_error_str(aws_last_error()));
1100 aws_http_proxy_user_data_destroy(user_data);
1101 }
1102
1103 return result;
1104 }
1105
1106 /*
1107 * Top-level function to route a connection through a proxy server via a CONNECT request
1108 */
1109 static int s_aws_http_client_connect_via_tunneling_proxy(
1110 const struct aws_http_client_connection_options *options,
1111 aws_client_bootstrap_on_channel_event_fn *on_channel_setup,
1112 aws_client_bootstrap_on_channel_event_fn *on_channel_shutdown) {
1113 AWS_FATAL_ASSERT(options->proxy_options != NULL);
1114
1115 AWS_LOGF_INFO(
1116 AWS_LS_HTTP_CONNECTION,
1117 "(STATIC) Connecting to \"" PRInSTR "\" through a tunnel via proxy \"" PRInSTR "\"",
1118 AWS_BYTE_CURSOR_PRI(options->host_name),
1119 AWS_BYTE_CURSOR_PRI(options->proxy_options->host));
1120
1121 /* Create a wrapper user data that contains all proxy-related information, state, and user-facing callbacks */
1122 struct aws_http_proxy_user_data *user_data =
1123 aws_http_proxy_user_data_new(options->allocator, options, on_channel_setup, on_channel_shutdown);
1124 if (user_data == NULL) {
1125 return AWS_OP_ERR;
1126 }
1127
1128 return s_create_tunneling_connection(user_data);
1129 }
1130
1131 static enum aws_http_proxy_connection_type s_determine_proxy_connection_type(
1132 enum aws_http_proxy_connection_type proxy_connection_type,
1133 bool is_tls_connection) {
1134 if (proxy_connection_type != AWS_HPCT_HTTP_LEGACY) {
1135 return proxy_connection_type;
1136 }
1137
1138 if (is_tls_connection) {
1139 return AWS_HPCT_HTTP_TUNNEL;
1140 } else {
1141 return AWS_HPCT_HTTP_FORWARD;
1142 }
1143 }
1144
1145 static int s_proxy_uri_init_from_env_variable(
1146 struct aws_allocator *allocator,
1147 const struct aws_http_client_connection_options *options,
1148 struct aws_uri *proxy_uri,
1149 bool *found) {
1150 struct aws_string *proxy_uri_string = NULL;
1151 *found = false;
1152
1153 /* First check if this host should bypass proxy using NO_PROXY */
1154 struct aws_byte_cursor host_cursor = options->host_name;
1155
1156 /* Get the NO_PROXY environment variable */
1157 struct aws_string *no_proxy_str = aws_get_env_nonempty(allocator, s_no_proxy_env_var_low);
1158 if (no_proxy_str == NULL) {
1159 no_proxy_str = aws_get_env_nonempty(allocator, s_no_proxy_env_var);
1160 }
1161
1162 if (no_proxy_str != NULL) {
1163 if (aws_http_host_matches_no_proxy(allocator, host_cursor, no_proxy_str)) {
1164 AWS_LOGF_DEBUG(
1165 AWS_LS_HTTP_CONNECTION,
1166 "Host \"" PRInSTR "\" found in no_proxy_hosts: \" %s \", bypassing proxy",
1167 AWS_BYTE_CURSOR_PRI(options->host_name),
1168 aws_string_c_str(no_proxy_str));
1169 aws_string_destroy(no_proxy_str);
1170 return AWS_OP_SUCCESS;
1171 }
1172 aws_string_destroy(no_proxy_str);
1173 }
1174
1175 if (options->tls_options) {
1176 proxy_uri_string = aws_get_env_nonempty(allocator, s_https_proxy_env_var_low);
1177 if (proxy_uri_string == NULL) {
1178 proxy_uri_string = aws_get_env_nonempty(allocator, s_https_proxy_env_var);
1179 }
1180 if (proxy_uri_string == NULL) {
1181 return AWS_OP_SUCCESS;
1182 }
1183 } else {
1184 proxy_uri_string = aws_get_env_nonempty(allocator, s_http_proxy_env_var_low);
1185 if (proxy_uri_string == NULL) {
1186 proxy_uri_string = aws_get_env_nonempty(allocator, s_http_proxy_env_var);
1187 }
1188 if (proxy_uri_string == NULL) {
1189 return AWS_OP_SUCCESS;
1190 }
1191 }
1192 struct aws_byte_cursor proxy_uri_cursor = aws_byte_cursor_from_string(proxy_uri_string);
1193 if (aws_uri_init_parse(proxy_uri, allocator, &proxy_uri_cursor)) {
1194 AWS_LOGF_ERROR(AWS_LS_HTTP_CONNECTION, "Could not parse found proxy URI.");
1195 aws_string_destroy(proxy_uri_string);
1196 return AWS_OP_ERR;
1197 }
1198 *found = true;
1199 aws_string_destroy(proxy_uri_string);
1200 return AWS_OP_SUCCESS;
1201 }
1202
1203 static int s_connect_proxy(const struct aws_http_client_connection_options *options) {
1204 if (aws_http_options_validate_proxy_configuration(options)) {
1205 return AWS_OP_ERR;
1206 }
1207
1208 if (options->proxy_options->no_proxy_hosts.len > 0) {
1209 struct aws_string *no_proxy_host_str =
1210 aws_string_new_from_cursor(options->allocator, &options->proxy_options->no_proxy_hosts);
1211 if (aws_http_host_matches_no_proxy(options->allocator, options->host_name, no_proxy_host_str)) {
1212 AWS_LOGF_DEBUG(
1213 AWS_LS_HTTP_CONNECTION,
1214 "Host \"" PRInSTR "\" found in NO_PROXY, bypassing proxy",
1215 AWS_BYTE_CURSOR_PRI(options->host_name));
1216 aws_string_destroy(no_proxy_host_str);
1217
1218 /* host matched no_proxy, connect without a proxy.: Fill in a new connection options with NULL proxy_options
1219 */
1220 struct aws_http_client_connection_options options_copy = *options;
1221 options_copy.proxy_options = NULL;
1222 return aws_http_client_connect_internal(&options_copy, NULL);
1223 }
1224 aws_string_destroy(no_proxy_host_str);
1225 }
1226
1227 enum aws_http_proxy_connection_type proxy_connection_type =
1228 s_determine_proxy_connection_type(options->proxy_options->connection_type, options->tls_options != NULL);
1229
1230 switch (proxy_connection_type) {
1231 case AWS_HPCT_HTTP_FORWARD:
1232 return s_aws_http_client_connect_via_forwarding_proxy(options);
1233
1234 case AWS_HPCT_HTTP_TUNNEL:
1235 return s_aws_http_client_connect_via_tunneling_proxy(options, NULL, NULL);
1236
1237 default:
1238 return aws_raise_error(AWS_ERROR_UNIMPLEMENTED);
1239 }
1240 }
1241
1242 static int s_setup_proxy_tls_env_variable(
1243 const struct aws_http_client_connection_options *options,
1244 struct aws_tls_connection_options *default_tls_connection_options,
1245 struct aws_http_proxy_options *proxy_options,
1246 struct aws_uri *proxy_uri) {
1247 (void)default_tls_connection_options;
1248 (void)proxy_uri;
1249 if (options->proxy_ev_settings->tls_options) {
1250 proxy_options->tls_options = options->proxy_ev_settings->tls_options;
1251 } else {
1252 #ifdef BYO_CRYPTO
1253 AWS_LOGF_ERROR(
1254 AWS_LS_HTTP_CONNECTION,
1255 "Failed making default TLS context because of BYO_CRYPTO, set up the tls_options for proxy_env_settings to "
1256 "make it work.");
1257 return aws_raise_error(AWS_ERROR_UNIMPLEMENTED);
1258 #else
1259 struct aws_tls_ctx *tls_ctx = NULL;
1260 struct aws_tls_ctx_options tls_ctx_options;
1261 AWS_ZERO_STRUCT(tls_ctx_options);
1262 /* create a default tls options */
1263 aws_tls_ctx_options_init_default_client(&tls_ctx_options, options->allocator);
1264 struct aws_string *proxy_no_verify_peer_string = NULL;
1265 if (aws_get_environment_value(
1266 options->allocator, s_proxy_no_verify_peer_env_var, &proxy_no_verify_peer_string) == AWS_OP_SUCCESS &&
1267 proxy_no_verify_peer_string != NULL) {
1268 /* turn off the peer verification, if setup from envrionment variable. Mostly for testing. */
1269 aws_tls_ctx_options_set_verify_peer(&tls_ctx_options, false);
1270 aws_string_destroy(proxy_no_verify_peer_string);
1271 }
1272 tls_ctx = aws_tls_client_ctx_new(options->allocator, &tls_ctx_options);
1273 aws_tls_ctx_options_clean_up(&tls_ctx_options);
1274 if (!tls_ctx) {
1275 AWS_LOGF_ERROR(AWS_LS_HTTP_CONNECTION, "Failed to create default TLS context.");
1276 return AWS_OP_ERR;
1277 }
1278 aws_tls_connection_options_init_from_ctx(default_tls_connection_options, tls_ctx);
1279 /* tls options hold a ref to the ctx */
1280 aws_tls_ctx_release(tls_ctx);
1281 if (aws_tls_connection_options_set_server_name(
1282 default_tls_connection_options, options->allocator, &proxy_uri->host_name)) {
1283 AWS_LOGF_ERROR(AWS_LS_HTTP_CONNECTION, "Failed set server name for TLS connection options.");
1284 return AWS_OP_ERR;
1285 }
1286 proxy_options->tls_options = default_tls_connection_options;
1287 #endif
1288 }
1289 return AWS_OP_SUCCESS;
1290 }
1291
1292 static int s_connect_proxy_via_env_variable(const struct aws_http_client_connection_options *options) {
1293 struct aws_http_proxy_options proxy_options;
1294 AWS_ZERO_STRUCT(proxy_options);
1295 struct aws_uri proxy_uri;
1296 AWS_ZERO_STRUCT(proxy_uri);
1297 struct aws_tls_connection_options default_tls_connection_options;
1298 AWS_ZERO_STRUCT(default_tls_connection_options);
1299 bool found = false;
1300 bool success = false;
1301 if (s_proxy_uri_init_from_env_variable(options->allocator, options, &proxy_uri, &found)) {
1302 /* Envrionment is set but failed to parse it */
1303 goto done;
1304 }
1305 if (found) {
1306 proxy_options.host = proxy_uri.host_name;
1307 proxy_options.port = proxy_uri.port;
1308 proxy_options.connection_type = options->proxy_ev_settings->connection_type;
1309 if (proxy_options.connection_type == AWS_HPCT_HTTP_LEGACY) {
1310 if (options->tls_options) {
1311 /* Use tunneling when main connection use TLS. */
1312 proxy_options.connection_type = AWS_HPCT_HTTP_TUNNEL;
1313 } else {
1314 /* Use forwarding proxy when main connection use clear text. */
1315 proxy_options.connection_type = AWS_HPCT_HTTP_FORWARD;
1316 }
1317 }
1318 if (aws_byte_cursor_eq_ignore_case(&proxy_uri.scheme, &aws_http_scheme_https)) {
1319 if (s_setup_proxy_tls_env_variable(options, &default_tls_connection_options, &proxy_options, &proxy_uri)) {
1320 goto done;
1321 }
1322 }
1323 /* Support basic authentication. */
1324 if (proxy_uri.password.len) {
1325 /* Has no empty password set */
1326 struct aws_http_proxy_strategy_basic_auth_options config = {
1327 .proxy_connection_type = proxy_options.connection_type,
1328 .user_name = proxy_uri.user,
1329 .password = proxy_uri.password,
1330 };
1331 proxy_options.proxy_strategy = aws_http_proxy_strategy_new_basic_auth(options->allocator, &config);
1332 }
1333 } else {
1334 success = true;
1335 goto done;
1336 }
1337 struct aws_http_client_connection_options copied_options = *options;
1338 copied_options.proxy_options = &proxy_options;
1339 if (s_connect_proxy(&copied_options)) {
1340 goto done;
1341 }
1342 success = true;
1343 done:
1344 aws_tls_connection_options_clean_up(&default_tls_connection_options);
1345 aws_http_proxy_strategy_release(proxy_options.proxy_strategy);
1346 aws_uri_clean_up(&proxy_uri);
1347 if (success && !found) {
1348 /* Successfully, but no envrionment variable found. Connect without proxy */
1349 return aws_http_client_connect_internal(options, NULL);
1350 }
1351 return success ? AWS_OP_SUCCESS : AWS_OP_ERR;
1352 }
1353
1354 /*
1355 * Dispatches a proxy-enabled connection request to the appropriate top-level connection function
1356 */
1357 int aws_http_client_connect_via_proxy(const struct aws_http_client_connection_options *options) {
1358 if (options->proxy_options == NULL && options->proxy_ev_settings &&
1359 options->proxy_ev_settings->env_var_type == AWS_HPEV_ENABLE) {
1360 return s_connect_proxy_via_env_variable(options);
1361 }
1362 return s_connect_proxy(options);
1363 }
1364
1365 static struct aws_http_proxy_config *s_aws_http_proxy_config_new(
1366 struct aws_allocator *allocator,
1367 const struct aws_http_proxy_options *proxy_options,
1368 enum aws_http_proxy_connection_type override_proxy_connection_type) {
1369 AWS_FATAL_ASSERT(proxy_options != NULL);
1370
1371 struct aws_http_proxy_config *config = aws_mem_calloc(allocator, 1, sizeof(struct aws_http_proxy_config));
1372 if (config == NULL) {
1373 return NULL;
1374 }
1375
1376 config->allocator = allocator;
1377 config->connection_type = override_proxy_connection_type;
1378
1379 if (aws_byte_buf_init_copy_from_cursor(&config->host, allocator, proxy_options->host)) {
1380 goto on_error;
1381 }
1382
1383 if (aws_byte_buf_init_copy_from_cursor(&config->no_proxy_hosts, allocator, proxy_options->no_proxy_hosts)) {
1384 goto on_error;
1385 }
1386
1387 if (proxy_options->tls_options) {
1388 config->tls_options = aws_mem_calloc(allocator, 1, sizeof(struct aws_tls_connection_options));
1389 if (aws_tls_connection_options_copy(config->tls_options, proxy_options->tls_options)) {
1390 goto on_error;
1391 }
1392 }
1393
1394 config->port = proxy_options->port;
1395
1396 if (proxy_options->proxy_strategy != NULL) {
1397 config->proxy_strategy = aws_http_proxy_strategy_acquire(proxy_options->proxy_strategy);
1398 } else if (proxy_options->auth_type == AWS_HPAT_BASIC) {
1399 struct aws_http_proxy_strategy_basic_auth_options basic_config;
1400 AWS_ZERO_STRUCT(basic_config);
1401
1402 basic_config.proxy_connection_type = override_proxy_connection_type;
1403 basic_config.user_name = proxy_options->auth_username;
1404 basic_config.password = proxy_options->auth_password;
1405
1406 config->proxy_strategy = aws_http_proxy_strategy_new_basic_auth(allocator, &basic_config);
1407 }
1408
1409 if (config->proxy_strategy == NULL) {
1410 switch (override_proxy_connection_type) {
1411 case AWS_HPCT_HTTP_FORWARD:
1412 config->proxy_strategy = aws_http_proxy_strategy_new_forwarding_identity(allocator);
1413 break;
1414
1415 case AWS_HPCT_HTTP_TUNNEL:
1416 config->proxy_strategy = aws_http_proxy_strategy_new_tunneling_one_time_identity(allocator);
1417 break;
1418
1419 default:
1420 break;
1421 }
1422
1423 if (config->proxy_strategy == NULL) {
1424 goto on_error;
1425 }
1426 }
1427
1428 return config;
1429
1430 on_error:
1431
1432 aws_http_proxy_config_destroy(config);
1433
1434 return NULL;
1435 }
1436
1437 struct aws_http_proxy_config *aws_http_proxy_config_new_from_connection_options(
1438 struct aws_allocator *allocator,
1439 const struct aws_http_client_connection_options *options) {
1440 AWS_FATAL_ASSERT(options != NULL);
1441 AWS_FATAL_ASSERT(options->proxy_options != NULL);
1442
1443 return s_aws_http_proxy_config_new(
1444 allocator,
1445 options->proxy_options,
1446 s_determine_proxy_connection_type(options->proxy_options->connection_type, options->tls_options != NULL));
1447 }
1448
1449 struct aws_http_proxy_config *aws_http_proxy_config_new_from_manager_options(
1450 struct aws_allocator *allocator,
1451 const struct aws_http_connection_manager_options *options) {
1452 AWS_FATAL_ASSERT(options != NULL);
1453 AWS_FATAL_ASSERT(options->proxy_options != NULL);
1454
1455 return s_aws_http_proxy_config_new(
1456 allocator,
1457 options->proxy_options,
1458 s_determine_proxy_connection_type(
1459 options->proxy_options->connection_type, options->tls_connection_options != NULL));
1460 }
1461
1462 struct aws_http_proxy_config *aws_http_proxy_config_new_tunneling_from_proxy_options(
1463 struct aws_allocator *allocator,
1464 const struct aws_http_proxy_options *proxy_options) {
1465
1466 return s_aws_http_proxy_config_new(allocator, proxy_options, AWS_HPCT_HTTP_TUNNEL);
1467 }
1468
1469 struct aws_http_proxy_config *aws_http_proxy_config_new_from_proxy_options(
1470 struct aws_allocator *allocator,
1471 const struct aws_http_proxy_options *proxy_options) {
1472 if (proxy_options->connection_type == AWS_HPCT_HTTP_LEGACY) {
1473 AWS_LOGF_ERROR(AWS_LS_HTTP_PROXY_NEGOTIATION, "LEGACY type is not supported to create proxy config");
1474 return NULL;
1475 }
1476
1477 return s_aws_http_proxy_config_new(allocator, proxy_options, proxy_options->connection_type);
1478 }
1479
1480 struct aws_http_proxy_config *aws_http_proxy_config_new_from_proxy_options_with_tls_info(
1481 struct aws_allocator *allocator,
1482 const struct aws_http_proxy_options *proxy_options,
1483 bool is_tls_connection) {
1484 AWS_FATAL_ASSERT(proxy_options != NULL);
1485
1486 return s_aws_http_proxy_config_new(
1487 allocator, proxy_options, s_determine_proxy_connection_type(proxy_options->connection_type, is_tls_connection));
1488 }
1489
1490 struct aws_http_proxy_config *aws_http_proxy_config_new_clone(
1491 struct aws_allocator *allocator,
1492 const struct aws_http_proxy_config *proxy_config) {
1493
1494 AWS_FATAL_ASSERT(proxy_config != NULL);
1495
1496 struct aws_http_proxy_config *config = aws_mem_calloc(allocator, 1, sizeof(struct aws_http_proxy_config));
1497 if (config == NULL) {
1498 return NULL;
1499 }
1500
1501 config->connection_type = proxy_config->connection_type;
1502
1503 if (aws_byte_buf_init_copy_from_cursor(&config->host, allocator, aws_byte_cursor_from_buf(&proxy_config->host))) {
1504 goto on_error;
1505 }
1506
1507 if (aws_byte_buf_init_copy_from_cursor(
1508 &config->no_proxy_hosts, allocator, aws_byte_cursor_from_buf(&proxy_config->no_proxy_hosts))) {
1509 goto on_error;
1510 }
1511
1512 if (proxy_config->tls_options) {
1513 config->tls_options = aws_mem_calloc(allocator, 1, sizeof(struct aws_tls_connection_options));
1514 if (aws_tls_connection_options_copy(config->tls_options, proxy_config->tls_options)) {
1515 goto on_error;
1516 }
1517 }
1518
1519 config->allocator = allocator;
1520 config->port = proxy_config->port;
1521 config->proxy_strategy = aws_http_proxy_strategy_acquire(proxy_config->proxy_strategy);
1522
1523 return config;
1524
1525 on_error:
1526
1527 aws_http_proxy_config_destroy(config);
1528
1529 return NULL;
1530 }
1531
1532 void aws_http_proxy_config_destroy(struct aws_http_proxy_config *config) {
1533 if (config == NULL) {
1534 return;
1535 }
1536
1537 aws_byte_buf_clean_up(&config->host);
1538 aws_byte_buf_clean_up(&config->no_proxy_hosts);
1539
1540 if (config->tls_options) {
1541 aws_tls_connection_options_clean_up(config->tls_options);
1542 aws_mem_release(config->allocator, config->tls_options);
1543 }
1544
1545 aws_http_proxy_strategy_release(config->proxy_strategy);
1546
1547 aws_mem_release(config->allocator, config);
1548 }
1549
1550 void aws_http_proxy_options_init_from_config(
1551 struct aws_http_proxy_options *options,
1552 const struct aws_http_proxy_config *config) {
1553 AWS_FATAL_ASSERT(options && config);
1554
1555 options->connection_type = config->connection_type;
1556 options->host = aws_byte_cursor_from_buf(&config->host);
1557 options->port = config->port;
1558 options->tls_options = config->tls_options;
1559 options->proxy_strategy = config->proxy_strategy;
1560 options->no_proxy_hosts = aws_byte_cursor_from_buf(&config->no_proxy_hosts);
1561 }
1562
1563 int aws_http_options_validate_proxy_configuration(const struct aws_http_client_connection_options *options) {
1564 if (options == NULL || options->proxy_options == NULL) {
1565 return aws_raise_error(AWS_ERROR_INVALID_ARGUMENT);
1566 }
1567
1568 #if defined(AWS_USE_SECITEM)
1569 if ((options->proxy_options || options->proxy_ev_settings)) {
1570 AWS_LOGF_ERROR(AWS_LS_HTTP_PROXY_NEGOTIATION, "HTTP proxy is not supported on Apple Network Framework.");
1571 return aws_raise_error(AWS_ERROR_PLATFORM_NOT_SUPPORTED);
1572 }
1573 #endif
1574
1575 enum aws_http_proxy_connection_type proxy_type = options->proxy_options->connection_type;
1576 if (proxy_type == AWS_HPCT_HTTP_FORWARD && options->tls_options != NULL) {
1577 return aws_raise_error(AWS_ERROR_INVALID_STATE);
1578 }
1579
1580 struct aws_http_proxy_strategy *proxy_strategy = options->proxy_options->proxy_strategy;
1581 if (proxy_strategy != NULL) {
1582 if (proxy_strategy->proxy_connection_type != proxy_type) {
1583 return aws_raise_error(AWS_ERROR_INVALID_STATE);
1584 }
1585 }
1586
1587 return AWS_OP_SUCCESS;
1588 }
1589
1590 struct aws_proxied_socket_channel_user_data {
1591 struct aws_allocator *allocator;
1592 struct aws_client_bootstrap *bootstrap;
1593 struct aws_channel *channel;
1594 aws_client_bootstrap_on_channel_event_fn *original_setup_callback;
1595 aws_client_bootstrap_on_channel_event_fn *original_shutdown_callback;
1596 void *original_user_data;
1597 };
1598
1599 static void s_proxied_socket_channel_user_data_destroy(struct aws_proxied_socket_channel_user_data *user_data) {
1600 if (user_data == NULL) {
1601 return;
1602 }
1603
1604 aws_client_bootstrap_release(user_data->bootstrap);
1605
1606 aws_mem_release(user_data->allocator, user_data);
1607 }
1608
1609 static struct aws_proxied_socket_channel_user_data *s_proxied_socket_channel_user_data_new(
1610 struct aws_allocator *allocator,
1611 struct aws_socket_channel_bootstrap_options *channel_options) {
1612 struct aws_proxied_socket_channel_user_data *user_data =
1613 aws_mem_calloc(allocator, 1, sizeof(struct aws_proxied_socket_channel_user_data));
1614 if (user_data == NULL) {
1615 return NULL;
1616 }
1617
1618 user_data->allocator = allocator;
1619 user_data->original_setup_callback = channel_options->setup_callback;
1620 user_data->original_shutdown_callback = channel_options->shutdown_callback;
1621 user_data->original_user_data = channel_options->user_data;
1622 user_data->bootstrap = aws_client_bootstrap_acquire(channel_options->bootstrap);
1623
1624 return user_data;
1625 }
1626
1627 static void s_http_proxied_socket_channel_setup(
1628 struct aws_client_bootstrap *bootstrap,
1629 int error_code,
1630 struct aws_channel *channel,
1631 void *user_data) {
1632
1633 (void)bootstrap;
1634 struct aws_proxied_socket_channel_user_data *proxied_user_data = user_data;
1635
1636 if (error_code != AWS_ERROR_SUCCESS || channel == NULL) {
1637 proxied_user_data->original_setup_callback(
1638 proxied_user_data->bootstrap, error_code, NULL, proxied_user_data->original_user_data);
1639 s_proxied_socket_channel_user_data_destroy(proxied_user_data);
1640 return;
1641 }
1642
1643 proxied_user_data->channel = channel;
1644
1645 proxied_user_data->original_setup_callback(
1646 proxied_user_data->bootstrap,
1647 AWS_ERROR_SUCCESS,
1648 proxied_user_data->channel,
1649 proxied_user_data->original_user_data);
1650 }
1651
1652 static void s_http_proxied_socket_channel_shutdown(
1653 struct aws_client_bootstrap *bootstrap,
1654 int error_code,
1655 struct aws_channel *channel,
1656 void *user_data) {
1657 (void)bootstrap;
1658 (void)channel;
1659 struct aws_proxied_socket_channel_user_data *proxied_user_data = user_data;
1660 proxied_user_data->original_shutdown_callback(
1661 proxied_user_data->bootstrap, error_code, proxied_user_data->channel, proxied_user_data->original_user_data);
1662
1663 s_proxied_socket_channel_user_data_destroy(proxied_user_data);
1664 }
1665
1666 int aws_http_proxy_new_socket_channel(
1667 struct aws_socket_channel_bootstrap_options *channel_options,
1668 const struct aws_http_proxy_options *proxy_options) {
1669
1670 AWS_FATAL_ASSERT(channel_options != NULL && channel_options->bootstrap != NULL);
1671 AWS_FATAL_ASSERT(proxy_options != NULL);
1672
1673 #if defined(AWS_USE_SECITEM)
1674 AWS_LOGF_ERROR(AWS_LS_HTTP_PROXY_NEGOTIATION, "HTTP proxy is not supported on Apple Network Framework.");
1675 return aws_raise_error(AWS_ERROR_PLATFORM_NOT_SUPPORTED);
1676 #endif
1677
1678 if (proxy_options->connection_type != AWS_HPCT_HTTP_TUNNEL) {
1679 AWS_LOGF_ERROR(
1680 AWS_LS_HTTP_PROXY_NEGOTIATION,
1681 "Creating a raw protocol channel through an http proxy requires a tunneling proxy "
1682 "configuration");
1683 return aws_raise_error(AWS_ERROR_INVALID_ARGUMENT);
1684 }
1685
1686 if (channel_options->tls_options == NULL) {
1687 AWS_LOGF_ERROR(
1688 AWS_LS_HTTP_PROXY_NEGOTIATION,
1689 "Creating a raw protocol channel through an http proxy requires tls to the endpoint");
1690 return aws_raise_error(AWS_ERROR_INVALID_ARGUMENT);
1691 }
1692
1693 struct aws_allocator *allocator = channel_options->bootstrap->allocator;
1694 struct aws_proxied_socket_channel_user_data *user_data =
1695 s_proxied_socket_channel_user_data_new(allocator, channel_options);
1696
1697 struct aws_http_client_connection_options http_connection_options = AWS_HTTP_CLIENT_CONNECTION_OPTIONS_INIT;
1698 http_connection_options.allocator = allocator;
1699 http_connection_options.bootstrap = channel_options->bootstrap;
1700 http_connection_options.host_name = aws_byte_cursor_from_c_str(channel_options->host_name);
1701 http_connection_options.port = channel_options->port;
1702 http_connection_options.socket_options = channel_options->socket_options;
1703 http_connection_options.tls_options = channel_options->tls_options;
1704 http_connection_options.proxy_options = proxy_options;
1705 http_connection_options.user_data = user_data;
1706 http_connection_options.on_setup = NULL; /* use channel callbacks, not http callbacks */
1707 http_connection_options.on_shutdown = NULL; /* use channel callbacks, not http callbacks */
1708 http_connection_options.requested_event_loop = channel_options->requested_event_loop;
1709 http_connection_options.host_resolution_config = channel_options->host_resolution_override_config;
1710
1711 if (s_aws_http_client_connect_via_tunneling_proxy(
1712 &http_connection_options, s_http_proxied_socket_channel_setup, s_http_proxied_socket_channel_shutdown)) {
1713 goto on_error;
1714 }
1715
1716 return AWS_OP_SUCCESS;
1717
1718 on_error:
1719
1720 s_proxied_socket_channel_user_data_destroy(user_data);
1721
1722 return AWS_OP_ERR;
1723 }