Debian Codegraph @

crt/aws-c-http/include/aws/http/proxy.h

raw symbols SCIP
1 #ifndef AWS_PROXY_H
2 #define AWS_PROXY_H
3
4 /**
5 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
6 * SPDX-License-Identifier: Apache-2.0.
7 */
8
9 #include <aws/common/ref_count.h>
10 #include <aws/http/http.h>
11 #include <aws/http/request_response.h>
12 #include <aws/http/status_code.h>
13
14 AWS_PUSH_SANE_WARNING_LEVEL
15
16 struct aws_http_client_connection_options;
17 struct aws_http_connection_manager_options;
18
19 struct aws_http_message;
20 struct aws_http_header;
21
22 struct aws_http_proxy_config;
23 struct aws_http_proxy_negotiator;
24 struct aws_http_proxy_strategy;
25
26 struct aws_socket_channel_bootstrap_options;
27
28 /**
29 * @Deprecated - Supported proxy authentication modes. Superceded by proxy strategy.
30 */
31 enum aws_http_proxy_authentication_type {
32 AWS_HPAT_NONE = 0,
33 AWS_HPAT_BASIC,
34 };
35
36 enum aws_http_proxy_env_var_type {
37 /**
38 * Default.
39 * Disable reading from environment variable for proxy.
40 */
41 AWS_HPEV_DISABLE = 0,
42 /**
43 * Enable get proxy URL from environment variable, when the manual proxy options of connection manager is not set.
44 * env HTTPS_PROXY/https_proxy will be checked when the main connection use tls.
45 * env HTTP_PROXY/http_proxy will be checked when the main connection NOT use tls.
46 * env NO_PROXY/no_proxy will be checked to bypass proxy if the host match the pattern.
47 * Check `aws_http_host_matches_no_proxy` for detail. This function can also be used with a direct no_proxy
48 * parameter. The lower case version has precedence.
49 */
50 AWS_HPEV_ENABLE,
51 };
52
53 /**
54 * Supported proxy connection types
55 */
56 enum aws_http_proxy_connection_type {
57 /**
58 * Deprecated, but 0-valued for backwards compatibility
59 *
60 * If tls options are provided (for the main connection) then treat the proxy as a tunneling proxy
61 * If tls options are not provided (for the main connection), then treat the proxy as a forwarding proxy
62 */
63 AWS_HPCT_HTTP_LEGACY = 0,
64
65 /**
66 * Use the proxy to forward http requests. Attempting to use both this mode and TLS on the tunnel destination
67 * is a configuration error.
68 */
69 AWS_HPCT_HTTP_FORWARD,
70
71 /**
72 * Use the proxy to establish a connection to a remote endpoint via a CONNECT request through the proxy.
73 * Works for both plaintext and tls connections.
74 */
75 AWS_HPCT_HTTP_TUNNEL,
76 };
77
78 /*
79 * Configuration for using proxy from environment variable.
80 * Zero out as default settings.
81 */
82 struct proxy_env_var_settings {
83 enum aws_http_proxy_env_var_type env_var_type;
84 /*
85 * Optional.
86 * If not set:
87 * If tls options are provided (for the main connection) use tunnel proxy type
88 * If tls options are not provided (for the main connection) use forward proxy type
89 */
90 enum aws_http_proxy_connection_type connection_type;
91 /*
92 * Optional.
93 * If not set, a default tls option will be created. when https used for Local to proxy connection.
94 * Must be distinct from the the tls_connection_options from aws_http_connection_manager_options
95 */
96 const struct aws_tls_connection_options *tls_options;
97 };
98
99 struct aws_http_proxy_strategy;
100
101 /**
102 * Options for http proxy server usage
103 */
104 struct aws_http_proxy_options {
105
106 /**
107 * Type of proxy connection to make
108 */
109 enum aws_http_proxy_connection_type connection_type;
110
111 /**
112 * Proxy host to connect to
113 */
114 struct aws_byte_cursor host;
115
116 /**
117 * Port to make the proxy connection to
118 */
119 uint32_t port;
120
121 /**
122 * Optional.
123 * TLS configuration for the Local <-> Proxy connection
124 * Must be distinct from the the TLS options in the parent aws_http_connection_options struct
125 */
126 const struct aws_tls_connection_options *tls_options;
127
128 /**
129 * Optional
130 * Advanced option that allows the user to create a custom strategy that gives low-level control of
131 * certain logical flows within the proxy logic.
132 *
133 * For tunneling proxies it allows custom retry and adaptive negotiation of CONNECT requests.
134 * For forwarding proxies it allows custom request transformations.
135 */
136 struct aws_http_proxy_strategy *proxy_strategy;
137
138 /**
139 * @Deprecated - What type of proxy authentication to use, if any.
140 * Replaced by instantiating a proxy_strategy
141 */
142 enum aws_http_proxy_authentication_type auth_type;
143
144 /**
145 * @Deprecated - Optional user name to use for basic authentication
146 * Replaced by instantiating a proxy_strategy via aws_http_proxy_strategy_new_basic_auth()
147 */
148 struct aws_byte_cursor auth_username;
149
150 /**
151 * @Deprecated - Optional password to use for basic authentication
152 * Replaced by instantiating a proxy_strategy via aws_http_proxy_strategy_new_basic_auth()
153 */
154 struct aws_byte_cursor auth_password;
155
156 /**
157 * Optional
158 * No proxy hosts - Comma seperated list of hosts for which not to use a proxy, if one is specified.
159 */
160 struct aws_byte_cursor no_proxy_hosts;
161 };
162
163 /**
164 * Synchronous (for now) callback function to fetch a token used in modifying CONNECT requests
165 */
166 typedef struct aws_string *(aws_http_proxy_negotiation_get_token_sync_fn)(void *user_data, int *out_error_code);
167
168 /**
169 * Synchronous (for now) callback function to fetch a token used in modifying CONNECT request. Includes a (byte string)
170 * context intended to be used as part of a challenge-response flow.
171 */
172 typedef struct aws_string *(
173 aws_http_proxy_negotiation_get_challenge_token_sync_fn)(void *user_data,
174 const struct aws_byte_cursor *challenge_context,
175 int *out_error_code);
176
177 /**
178 * Proxy negotiation logic must call this function to indicate an unsuccessful outcome
179 */
180 typedef void(aws_http_proxy_negotiation_terminate_fn)(
181 struct aws_http_message *message,
182 int error_code,
183 void *internal_proxy_user_data);
184
185 /**
186 * Proxy negotiation logic must call this function to forward the potentially-mutated request back to the proxy
187 * connection logic.
188 */
189 typedef void(aws_http_proxy_negotiation_http_request_forward_fn)(
190 struct aws_http_message *message,
191 void *internal_proxy_user_data);
192
193 /**
194 * User-supplied transform callback which implements the proxy request flow and ultimately, across all execution
195 * pathways, invokes either the terminate function or the forward function appropriately.
196 *
197 * For tunneling proxy connections, this request flow transform only applies to the CONNECT stage of proxy
198 * connection establishment.
199 *
200 * For forwarding proxy connections, this request flow transform applies to every single http request that goes
201 * out on the connection.
202 *
203 * Forwarding proxy connections cannot yet support a truly async request transform without major surgery on http
204 * stream creation, so for now, we split into an async version (for tunneling proxies) and a separate
205 * synchronous version for forwarding proxies. Also forwarding proxies are a kind of legacy dead-end in some
206 * sense.
207 *
208 */
209 typedef void(aws_http_proxy_negotiation_http_request_transform_async_fn)(
210 struct aws_http_proxy_negotiator *proxy_negotiator,
211 struct aws_http_message *message,
212 aws_http_proxy_negotiation_terminate_fn *negotiation_termination_callback,
213 aws_http_proxy_negotiation_http_request_forward_fn *negotiation_http_request_forward_callback,
214 void *internal_proxy_user_data);
215
216 typedef int(aws_http_proxy_negotiation_http_request_transform_fn)(
217 struct aws_http_proxy_negotiator *proxy_negotiator,
218 struct aws_http_message *message);
219
220 /**
221 * Tunneling proxy connections only. A callback that lets the negotiator examine the headers in the
222 * response to the most recent CONNECT request as they arrive.
223 */
224 typedef int(aws_http_proxy_negotiation_connect_on_incoming_headers_fn)(
225 struct aws_http_proxy_negotiator *proxy_negotiator,
226 enum aws_http_header_block header_block,
227 const struct aws_http_header *header_array,
228 size_t num_headers);
229
230 /**
231 * Tunneling proxy connections only. A callback that lets the negotiator examine the status code of the
232 * response to the most recent CONNECT request.
233 */
234 typedef int(aws_http_proxy_negotiator_connect_status_fn)(
235 struct aws_http_proxy_negotiator *proxy_negotiator,
236 enum aws_http_status_code status_code);
237
238 /**
239 * Tunneling proxy connections only. A callback that lets the negotiator examine the body of the response
240 * to the most recent CONNECT request.
241 */
242 typedef int(aws_http_proxy_negotiator_connect_on_incoming_body_fn)(
243 struct aws_http_proxy_negotiator *proxy_negotiator,
244 const struct aws_byte_cursor *data);
245
246 /*
247 * Control value that lets the http proxy implementation know if and how to retry a CONNECT request based on
248 * the proxy negotiator's state.
249 */
250 enum aws_http_proxy_negotiation_retry_directive {
251 /*
252 * Stop trying to connect through the proxy and give up.
253 */
254 AWS_HPNRD_STOP,
255
256 /*
257 * Establish a new connection to the proxy before making the next CONNECT request
258 */
259 AWS_HPNRD_NEW_CONNECTION,
260
261 /*
262 * Reuse the existing connection to make the next CONNECT request
263 */
264 AWS_HPNRD_CURRENT_CONNECTION,
265 };
266
267 typedef enum aws_http_proxy_negotiation_retry_directive(aws_http_proxy_negotiator_get_retry_directive_fn)(
268 struct aws_http_proxy_negotiator *proxy_negotiator);
269
270 /**
271 * Vtable for forwarding-based proxy negotiators
272 */
273 struct aws_http_proxy_negotiator_forwarding_vtable {
274 aws_http_proxy_negotiation_http_request_transform_fn *forward_request_transform;
275 };
276
277 /**
278 * Vtable for tunneling-based proxy negotiators
279 */
280 struct aws_http_proxy_negotiator_tunnelling_vtable {
281 aws_http_proxy_negotiation_http_request_transform_async_fn *connect_request_transform;
282
283 aws_http_proxy_negotiation_connect_on_incoming_headers_fn *on_incoming_headers_callback;
284 aws_http_proxy_negotiator_connect_status_fn *on_status_callback;
285 aws_http_proxy_negotiator_connect_on_incoming_body_fn *on_incoming_body_callback;
286
287 aws_http_proxy_negotiator_get_retry_directive_fn *get_retry_directive;
288 };
289
290 /*
291 * Base definition of a proxy negotiator.
292 *
293 * A negotiator works differently based on what kind of proxy connection is being asked for:
294 *
295 * (1) Tunneling - In a tunneling proxy connection, the connect_request_transform is invoked on every CONNECT request.
296 * The connect_request_transform implementation *MUST*, in turn, eventually call one of the terminate or forward
297 * functions it gets supplied with.
298 *
299 * Every CONNECT request, if a response is obtained, will properly invoke the response handling callbacks supplied
300 * in the tunneling vtable.
301 *
302 * (2) Forwarding - In a forwarding proxy connection, the forward_request_transform is invoked on every request sent out
303 * on the connection.
304 */
305 struct aws_http_proxy_negotiator {
306 struct aws_ref_count ref_count;
307
308 void *impl;
309
310 union {
311 struct aws_http_proxy_negotiator_forwarding_vtable *forwarding_vtable;
312 struct aws_http_proxy_negotiator_tunnelling_vtable *tunnelling_vtable;
313 } strategy_vtable;
314 };
315
316 /*********************************************************************************************/
317
318 typedef struct aws_http_proxy_negotiator *(
319 aws_http_proxy_strategy_create_negotiator_fn)(struct aws_http_proxy_strategy *proxy_strategy,
320 struct aws_allocator *allocator);
321
322 struct aws_http_proxy_strategy_vtable {
323 aws_http_proxy_strategy_create_negotiator_fn *create_negotiator;
324 };
325
326 struct aws_http_proxy_strategy {
327 struct aws_ref_count ref_count;
328 struct aws_http_proxy_strategy_vtable *vtable;
329 void *impl;
330 enum aws_http_proxy_connection_type proxy_connection_type;
331 };
332
333 /*
334 * Options necessary to create a basic authentication proxy strategy
335 */
336 struct aws_http_proxy_strategy_basic_auth_options {
337
338 /* type of proxy connection being established, must be forwarding or tunnel */
339 enum aws_http_proxy_connection_type proxy_connection_type;
340
341 /* user name to use in basic authentication */
342 struct aws_byte_cursor user_name;
343
344 /* password to use in basic authentication */
345 struct aws_byte_cursor password;
346 };
347
348 /*
349 * Options necessary to create a (synchronous) kerberos authentication proxy strategy
350 */
351 struct aws_http_proxy_strategy_tunneling_kerberos_options {
352
353 aws_http_proxy_negotiation_get_token_sync_fn *get_token;
354
355 void *get_token_user_data;
356 };
357
358 /*
359 * Options necessary to create a (synchronous) ntlm authentication proxy strategy
360 */
361 struct aws_http_proxy_strategy_tunneling_ntlm_options {
362
363 aws_http_proxy_negotiation_get_token_sync_fn *get_token;
364
365 aws_http_proxy_negotiation_get_challenge_token_sync_fn *get_challenge_token;
366
367 void *get_challenge_token_user_data;
368 };
369
370 /*
371 * Options necessary to create an adaptive sequential strategy that tries one or more of kerberos and ntlm (in that
372 * order, if both are active). If an options struct is NULL, then that strategy will not be used.
373 */
374 struct aws_http_proxy_strategy_tunneling_adaptive_options {
375 /*
376 * If non-null, will insert a kerberos proxy strategy into the adaptive sequence
377 */
378 struct aws_http_proxy_strategy_tunneling_kerberos_options *kerberos_options;
379
380 /*
381 * If non-null will insert an ntlm proxy strategy into the adaptive sequence
382 */
383 struct aws_http_proxy_strategy_tunneling_ntlm_options *ntlm_options;
384 };
385
386 /*
387 * Options necessary to create a sequential proxy strategy.
388 */
389 struct aws_http_proxy_strategy_tunneling_sequence_options {
390 struct aws_http_proxy_strategy **strategies;
391
392 uint32_t strategy_count;
393 };
394
395 AWS_EXTERN_C_BEGIN
396
397 /**
398 * Take a reference to an http proxy negotiator
399 * @param proxy_negotiator negotiator to take a reference to
400 * @return the strategy
401 */
402 AWS_HTTP_API
403 struct aws_http_proxy_negotiator *aws_http_proxy_negotiator_acquire(struct aws_http_proxy_negotiator *proxy_negotiator);
404
405 /**
406 * Release a reference to an http proxy negotiator
407 * @param proxy_negotiator negotiator to release a reference to
408 */
409 AWS_HTTP_API
410 void aws_http_proxy_negotiator_release(struct aws_http_proxy_negotiator *proxy_negotiator);
411
412 /**
413 * Creates a new proxy negotiator from a proxy strategy
414 * @param allocator memory allocator to use
415 * @param strategy strategy to creation a new negotiator for
416 * @return a new proxy negotiator if successful, otherwise NULL
417 */
418 AWS_HTTP_API
419 struct aws_http_proxy_negotiator *aws_http_proxy_strategy_create_negotiator(
420 struct aws_http_proxy_strategy *strategy,
421 struct aws_allocator *allocator);
422
423 /**
424 * Take a reference to an http proxy strategy
425 * @param proxy_strategy strategy to take a reference to
426 * @return the strategy
427 */
428 AWS_HTTP_API
429 struct aws_http_proxy_strategy *aws_http_proxy_strategy_acquire(struct aws_http_proxy_strategy *proxy_strategy);
430
431 /**
432 * Release a reference to an http proxy strategy
433 * @param proxy_strategy strategy to release a reference to
434 */
435 AWS_HTTP_API
436 void aws_http_proxy_strategy_release(struct aws_http_proxy_strategy *proxy_strategy);
437
438 /**
439 * A constructor for a proxy strategy that performs basic authentication by adding the appropriate
440 * header and header value to requests or CONNECT requests.
441 *
442 * @param allocator memory allocator to use
443 * @param config basic authentication configuration info
444 * @return a new proxy strategy if successfully constructed, otherwise NULL
445 */
446 AWS_HTTP_API
447 struct aws_http_proxy_strategy *aws_http_proxy_strategy_new_basic_auth(
448 struct aws_allocator *allocator,
449 struct aws_http_proxy_strategy_basic_auth_options *config);
450
451 /**
452 * Constructor for an adaptive tunneling proxy strategy. This strategy attempts a vanilla CONNECT and if that
453 * fails it may make followup CONNECT attempts using kerberos or ntlm tokens, based on configuration and proxy
454 * response properties.
455 *
456 * @param allocator memory allocator to use
457 * @param config configuration options for the strategy
458 * @return a new proxy strategy if successfully constructed, otherwise NULL
459 */
460 AWS_HTTP_API
461 struct aws_http_proxy_strategy *aws_http_proxy_strategy_new_tunneling_adaptive(
462 struct aws_allocator *allocator,
463 struct aws_http_proxy_strategy_tunneling_adaptive_options *config);
464
465 /*
466 * aws_http_proxy_config is the persistent, memory-managed version of aws_http_proxy_options
467 *
468 * This is a set of APIs for creating, destroying and converting between them
469 */
470
471 /**
472 * Create a persistent proxy configuration from http connection options
473 * @param allocator memory allocator to use
474 * @param options http connection options to source proxy configuration from
475 * @return
476 */
477 AWS_HTTP_API
478 struct aws_http_proxy_config *aws_http_proxy_config_new_from_connection_options(
479 struct aws_allocator *allocator,
480 const struct aws_http_client_connection_options *options);
481
482 /**
483 * Create a persistent proxy configuration from http connection manager options
484 * @param allocator memory allocator to use
485 * @param options http connection manager options to source proxy configuration from
486 * @return
487 */
488 AWS_HTTP_API
489 struct aws_http_proxy_config *aws_http_proxy_config_new_from_manager_options(
490 struct aws_allocator *allocator,
491 const struct aws_http_connection_manager_options *options);
492
493 /**
494 * Create a persistent proxy configuration from non-persistent proxy options. The resulting
495 * proxy configuration assumes a tunneling connection type.
496 *
497 * @param allocator memory allocator to use
498 * @param options http proxy options to source proxy configuration from
499 * @return
500 */
501 AWS_HTTP_API
502 struct aws_http_proxy_config *aws_http_proxy_config_new_tunneling_from_proxy_options(
503 struct aws_allocator *allocator,
504 const struct aws_http_proxy_options *options);
505
506 /**
507 * Create a persistent proxy configuration from non-persistent proxy options.
508 * Legacy connection type of proxy options will be rejected.
509 *
510 * @param allocator memory allocator to use
511 * @param options http proxy options to source proxy configuration from
512 * @return
513 */
514 AWS_HTTP_API
515 struct aws_http_proxy_config *aws_http_proxy_config_new_from_proxy_options(
516 struct aws_allocator *allocator,
517 const struct aws_http_proxy_options *options);
518
519 /**
520 * Create a persistent proxy configuration from non-persistent proxy options.
521 *
522 * @param allocator memory allocator to use
523 * @param options http proxy options to source proxy configuration from
524 * @param is_tls_connection tls connection info of the main connection to determine connection_type
525 * when the connection_type is legacy.
526 * @return
527 */
528 AWS_HTTP_API
529 struct aws_http_proxy_config *aws_http_proxy_config_new_from_proxy_options_with_tls_info(
530 struct aws_allocator *allocator,
531 const struct aws_http_proxy_options *proxy_options,
532 bool is_tls_connection);
533
534 /**
535 * Clones an existing proxy configuration. A refactor could remove this (do a "move" between the old and new user
536 * data in the one spot it's used) but that should wait until we have better test cases for the logic where this
537 * gets invoked (ntlm/kerberos chains).
538 *
539 * @param allocator memory allocator to use
540 * @param proxy_config http proxy configuration to clone
541 * @return
542 */
543 AWS_HTTP_API
544 struct aws_http_proxy_config *aws_http_proxy_config_new_clone(
545 struct aws_allocator *allocator,
546 const struct aws_http_proxy_config *proxy_config);
547
548 /**
549 * Destroys an http proxy configuration
550 * @param config http proxy configuration to destroy
551 */
552 AWS_HTTP_API
553 void aws_http_proxy_config_destroy(struct aws_http_proxy_config *config);
554
555 /**
556 * Initializes non-persistent http proxy options from a persistent http proxy configuration
557 * @param options http proxy options to initialize
558 * @param config the http proxy config to use as an initialization source
559 */
560 AWS_HTTP_API
561 void aws_http_proxy_options_init_from_config(
562 struct aws_http_proxy_options *options,
563 const struct aws_http_proxy_config *config);
564
565 /**
566 * Establish an arbitrary protocol connection through an http proxy via tunneling CONNECT. Alpn is
567 * not required for this connection process to succeed, but we encourage its use if available.
568 *
569 * @param channel_options configuration options for the socket level connection
570 * @param proxy_options configuration options for the proxy connection
571 *
572 * @return AWS_OP_SUCCESS if the asynchronous channel kickoff succeeded, AWS_OP_ERR otherwise
573 */
574 AWS_HTTP_API int aws_http_proxy_new_socket_channel(
575 struct aws_socket_channel_bootstrap_options *channel_options,
576 const struct aws_http_proxy_options *proxy_options);
577
578 AWS_EXTERN_C_END
579 AWS_POP_SANE_WARNING_LEVEL
580
581 #endif /* AWS_PROXY_STRATEGY_H */