Debian Codegraph @

crt/aws-c-http/include/aws/http/private/connection_impl.h

raw symbols SCIP
1 #ifndef AWS_HTTP_CONNECTION_IMPL_H
2 #define AWS_HTTP_CONNECTION_IMPL_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/http/connection.h>
10
11 #include <aws/http/private/http_impl.h>
12 #include <aws/http/server.h>
13
14 #include <aws/common/atomics.h>
15 #include <aws/io/channel.h>
16 #include <aws/io/channel_bootstrap.h>
17
18 struct aws_http_message;
19 struct aws_http_make_request_options;
20 struct aws_http_request_handler_options;
21 struct aws_http_stream;
22
23 /* vtable of functions that aws_http_connection uses to interact with external systems.
24 * tests override the vtable to mock those systems */
25 struct aws_http_connection_system_vtable {
26 int (*aws_client_bootstrap_new_socket_channel)(struct aws_socket_channel_bootstrap_options *options);
27 };
28
29 struct aws_http_connection_vtable {
30 struct aws_channel_handler_vtable channel_handler_vtable;
31
32 /* This is a callback I wish was in aws_channel_handler_vtable. */
33 void (*on_channel_handler_installed)(struct aws_channel_handler *handler, struct aws_channel_slot *slot);
34
35 struct aws_http_stream *(*make_request)(
36 struct aws_http_connection *client_connection,
37 const struct aws_http_make_request_options *options);
38
39 struct aws_http_stream *(*new_server_request_handler_stream)(
40 const struct aws_http_request_handler_options *options);
41 int (*stream_send_response)(struct aws_http_stream *stream, struct aws_http_message *response);
42 void (*close)(struct aws_http_connection *connection);
43 void (*stop_new_requests)(struct aws_http_connection *connection);
44 bool (*is_open)(const struct aws_http_connection *connection);
45 bool (*new_requests_allowed)(const struct aws_http_connection *connection);
46
47 /* HTTP/2 specific functions */
48 void (*update_window)(struct aws_http_connection *connection, uint32_t increment_size);
49 int (*change_settings)(
50 struct aws_http_connection *http2_connection,
51 const struct aws_http2_setting *settings_array,
52 size_t num_settings,
53 aws_http2_on_change_settings_complete_fn *on_completed,
54 void *user_data);
55 int (*send_ping)(
56 struct aws_http_connection *http2_connection,
57 const struct aws_byte_cursor *optional_opaque_data,
58 aws_http2_on_ping_complete_fn *on_completed,
59 void *user_data);
60 void (*send_goaway)(
61 struct aws_http_connection *http2_connection,
62 uint32_t http2_error,
63 bool allow_more_streams,
64 const struct aws_byte_cursor *optional_debug_data);
65 int (*get_sent_goaway)(
66 struct aws_http_connection *http2_connection,
67 uint32_t *out_http2_error,
68 uint32_t *out_last_stream_id);
69 int (*get_received_goaway)(
70 struct aws_http_connection *http2_connection,
71 uint32_t *out_http2_error,
72 uint32_t *out_last_stream_id);
73 void (*get_local_settings)(
74 const struct aws_http_connection *http2_connection,
75 struct aws_http2_setting out_settings[AWS_HTTP2_SETTINGS_COUNT]);
76 void (*get_remote_settings)(
77 const struct aws_http_connection *http2_connection,
78 struct aws_http2_setting out_settings[AWS_HTTP2_SETTINGS_COUNT]);
79 };
80
81 typedef int(aws_http_proxy_request_transform_fn)(struct aws_http_message *request, void *user_data);
82
83 /**
84 * Base class for connections.
85 * There are specific implementations for each HTTP version.
86 */
87 struct aws_http_connection {
88 const struct aws_http_connection_vtable *vtable;
89 struct aws_channel_handler channel_handler;
90 struct aws_channel_slot *channel_slot;
91 struct aws_allocator *alloc;
92 enum aws_http_version http_version;
93
94 aws_http_proxy_request_transform_fn *proxy_request_transform;
95 void *user_data;
96
97 /* Connection starts with 1 hold for the user.
98 * aws_http_streams will also acquire holds on their connection for the duration of their lifetime */
99 struct aws_atomic_var refcount;
100
101 /* Starts at either 1 or 2, increments by two with each new stream */
102 uint32_t next_stream_id;
103
104 union {
105 struct aws_http_connection_client_data {
106 uint64_t response_first_byte_timeout_ms;
107 } client;
108
109 struct aws_http_connection_server_data {
110 aws_http_on_incoming_request_fn *on_incoming_request;
111 aws_http_on_server_connection_shutdown_fn *on_shutdown;
112 } server;
113 } client_or_server_data;
114
115 /* On client connections, `client_data` points to client_or_server_data.client and `server_data` is null.
116 * Opposite is true on server connections */
117 struct aws_http_connection_client_data *client_data;
118 struct aws_http_connection_server_data *server_data;
119
120 bool stream_manual_window_management;
121 };
122
123 /* Gets a client connection up and running.
124 * Responsible for firing on_setup and on_shutdown callbacks. */
125 struct aws_http_client_bootstrap {
126 struct aws_allocator *alloc;
127 bool is_using_tls;
128 bool stream_manual_window_management;
129 bool prior_knowledge_http2;
130 size_t initial_window_size;
131 struct aws_http_connection_monitoring_options monitoring_options;
132 void *user_data;
133 aws_http_on_client_connection_setup_fn *on_setup;
134 aws_http_on_client_connection_shutdown_fn *on_shutdown;
135 aws_http_proxy_request_transform_fn *proxy_request_transform;
136 uint64_t response_first_byte_timeout_ms;
137
138 struct aws_http1_connection_options http1_options;
139 struct aws_http2_connection_options http2_options; /* allocated with bootstrap */
140 struct aws_hash_table *alpn_string_map; /* allocated with bootstrap */
141 struct aws_http_connection *connection;
142 };
143
144 AWS_EXTERN_C_BEGIN
145 AWS_HTTP_API
146 void aws_http_client_bootstrap_destroy(struct aws_http_client_bootstrap *bootstrap);
147
148 AWS_HTTP_API
149 void aws_http_connection_set_system_vtable(const struct aws_http_connection_system_vtable *system_vtable);
150
151 AWS_HTTP_API
152 int aws_http_client_connect_internal(
153 const struct aws_http_client_connection_options *options,
154 aws_http_proxy_request_transform_fn *proxy_request_transform);
155
156 /**
157 * Internal API for adding a reference to a connection
158 */
159 AWS_HTTP_API
160 void aws_http_connection_acquire(struct aws_http_connection *connection);
161
162 /**
163 * Allow tests to fake stats data
164 */
165 AWS_HTTP_API
166 struct aws_crt_statistics_http1_channel *aws_h1_connection_get_statistics(struct aws_http_connection *connection);
167
168 /**
169 * Gets the next available stream id within the connection. Valid for creating both h1 and h2 streams.
170 *
171 * This function is not thread-safe.
172 *
173 * Returns 0 if there was an error.
174 */
175 AWS_HTTP_API
176 uint32_t aws_http_connection_get_next_stream_id(struct aws_http_connection *connection);
177
178 /**
179 * Layers an http channel handler/connection onto a channel. Moved from internal to private so that the proxy
180 * logic could apply a new http connection/handler after tunneling proxy negotiation (into http) is finished.
181 * This is a synchronous operation.
182 *
183 * @param alloc memory allocator to use
184 * @param channel channel to apply the http handler/connection to
185 * @param is_server should the handler behave like an http server
186 * @param is_using_tls is tls is being used (do an alpn check of the to-the-left channel handler)
187 * @param manual_window_management is manual window management enabled
188 * @param prior_knowledge_http2 prior knowledge about http2 connection to be used
189 * @param initial_window_size what should the initial window size be
190 * @param alpn_string_map the customized ALPN string map from `struct aws_string *` to `enum aws_http_version`.
191 * @param http1_options http1 options
192 * @param http2_options http2 options
193 * @return a new http connection or NULL on failure
194 */
195 AWS_HTTP_API
196 struct aws_http_connection *aws_http_connection_new_channel_handler(
197 struct aws_allocator *alloc,
198 struct aws_channel *channel,
199 bool is_server,
200 bool is_using_tls,
201 bool manual_window_management,
202 bool prior_knowledge_http2,
203 size_t initial_window_size,
204 const struct aws_hash_table *alpn_string_map,
205 const struct aws_http1_connection_options *http1_options,
206 const struct aws_http2_connection_options *http2_options,
207 void *connection_user_data);
208
209 AWS_EXTERN_C_END
210
211 #endif /* AWS_HTTP_CONNECTION_IMPL_H */