aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/restricted/aws/aws-crt-cpp/source/http/HttpConnectionManager.cpp
blob: 7a13a2bdb10ae1ddda9a94025c282af24ca8ae86 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
/**
 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
 * SPDX-License-Identifier: Apache-2.0.
 */
#include <aws/crt/Api.h>
#include <aws/crt/http/HttpConnectionManager.h>
#include <aws/crt/http/HttpProxyStrategy.h>

#include <algorithm>
#include <aws/http/connection_manager.h>

namespace Aws
{
    namespace Crt
    {
        namespace Http
        {
            struct ConnectionManagerCallbackArgs
            {
                ConnectionManagerCallbackArgs() = default;
                OnClientConnectionAvailable m_onClientConnectionAvailable;
                std::shared_ptr<HttpClientConnectionManager> m_connectionManager;
            };

            void HttpClientConnectionManager::s_shutdownCompleted(void *userData) noexcept
            {
                HttpClientConnectionManager *connectionManager =
                    reinterpret_cast<HttpClientConnectionManager *>(userData);
                connectionManager->m_shutdownPromise.set_value();
            }

            HttpClientConnectionManagerOptions::HttpClientConnectionManagerOptions() noexcept
                : ConnectionOptions(), MaxConnections(1), EnableBlockingShutdown(false)
            {
            }

            std::shared_ptr<HttpClientConnectionManager> HttpClientConnectionManager::NewClientConnectionManager(
                const HttpClientConnectionManagerOptions &connectionManagerOptions,
                Allocator *allocator) noexcept
            {
                const Optional<Io::TlsConnectionOptions> &tlsOptions =
                    connectionManagerOptions.ConnectionOptions.TlsOptions;

                if (tlsOptions && !(*tlsOptions))
                {
                    AWS_LOGF_ERROR(
                        AWS_LS_HTTP_GENERAL,
                        "Cannot create HttpClientConnectionManager: ConnectionOptions contain invalid TLSOptions.");
                    aws_raise_error(AWS_ERROR_INVALID_ARGUMENT);
                    return nullptr;
                }

                const Crt::Optional<Crt::Http::HttpClientConnectionProxyOptions> &proxyOptions =
                    connectionManagerOptions.ConnectionOptions.ProxyOptions;

                if (proxyOptions && proxyOptions->TlsOptions && !(*proxyOptions->TlsOptions))
                {
                    AWS_LOGF_ERROR(
                        AWS_LS_HTTP_GENERAL,
                        "Cannot create HttpClientConnectionManager: ProxyOptions has ConnectionOptions that contain "
                        "invalid TLSOptions.");
                    aws_raise_error(AWS_ERROR_INVALID_ARGUMENT);
                    return nullptr;
                }

                auto *toSeat = static_cast<HttpClientConnectionManager *>(
                    aws_mem_acquire(allocator, sizeof(HttpClientConnectionManager)));
                if (toSeat)
                {
                    toSeat = new (toSeat) HttpClientConnectionManager(connectionManagerOptions, allocator);
                    return std::shared_ptr<HttpClientConnectionManager>(
                        toSeat, [allocator](HttpClientConnectionManager *manager) { Delete(manager, allocator); });
                }

                return nullptr;
            }

            HttpClientConnectionManager::HttpClientConnectionManager(
                const HttpClientConnectionManagerOptions &options,
                Allocator *allocator) noexcept
                : m_allocator(allocator), m_connectionManager(nullptr), m_options(options), m_releaseInvoked(false)
            {
                const auto &connectionOptions = m_options.ConnectionOptions;
                AWS_FATAL_ASSERT(connectionOptions.HostName.size() > 0);
                AWS_FATAL_ASSERT(connectionOptions.Port > 0);

                aws_http_connection_manager_options managerOptions;
                AWS_ZERO_STRUCT(managerOptions);

                if (connectionOptions.Bootstrap != nullptr)
                {
                    managerOptions.bootstrap = connectionOptions.Bootstrap->GetUnderlyingHandle();
                }
                else
                {
                    managerOptions.bootstrap =
                        ApiHandle::GetOrCreateStaticDefaultClientBootstrap()->GetUnderlyingHandle();
                }

                managerOptions.port = connectionOptions.Port;
                managerOptions.max_connections = m_options.MaxConnections;
                managerOptions.socket_options = &connectionOptions.SocketOptions.GetImpl();
                managerOptions.initial_window_size = connectionOptions.InitialWindowSize;

                if (options.EnableBlockingShutdown)
                {
                    managerOptions.shutdown_complete_callback = s_shutdownCompleted;
                    managerOptions.shutdown_complete_user_data = this;
                }
                else
                {
                    m_shutdownPromise.set_value();
                }

                aws_http_proxy_options proxyOptions;
                AWS_ZERO_STRUCT(proxyOptions);
                if (connectionOptions.ProxyOptions)
                {
                    /* This is verified by HttpClientConnectionManager::NewClientConnectionManager */
                    AWS_FATAL_ASSERT(
                        !connectionOptions.ProxyOptions->TlsOptions || *connectionOptions.ProxyOptions->TlsOptions);

                    const auto &proxyOpts = connectionOptions.ProxyOptions.value();
                    proxyOpts.InitializeRawProxyOptions(proxyOptions);

                    managerOptions.proxy_options = &proxyOptions;
                }

                if (connectionOptions.TlsOptions)
                {
                    /* This is verified by HttpClientConnectionManager::NewClientConnectionManager */
                    AWS_FATAL_ASSERT(*connectionOptions.TlsOptions);

                    managerOptions.tls_connection_options =
                        const_cast<aws_tls_connection_options *>(connectionOptions.TlsOptions->GetUnderlyingHandle());
                }
                managerOptions.host = aws_byte_cursor_from_c_str(connectionOptions.HostName.c_str());

                m_connectionManager = aws_http_connection_manager_new(allocator, &managerOptions);
            }

            HttpClientConnectionManager::~HttpClientConnectionManager()
            {
                if (!m_releaseInvoked)
                {
                    aws_http_connection_manager_release(m_connectionManager);
                    m_shutdownPromise.get_future().get();
                }
                m_connectionManager = nullptr;
            }

            bool HttpClientConnectionManager::AcquireConnection(
                const OnClientConnectionAvailable &onClientConnectionAvailable) noexcept
            {
                auto connectionManagerCallbackArgs = Aws::Crt::New<ConnectionManagerCallbackArgs>(m_allocator);
                if (!connectionManagerCallbackArgs)
                {
                    return false;
                }

                connectionManagerCallbackArgs->m_connectionManager = shared_from_this();
                connectionManagerCallbackArgs->m_onClientConnectionAvailable = onClientConnectionAvailable;

                aws_http_connection_manager_acquire_connection(
                    m_connectionManager, s_onConnectionSetup, connectionManagerCallbackArgs);
                return true;
            }

            std::future<void> HttpClientConnectionManager::InitiateShutdown() noexcept
            {
                m_releaseInvoked = true;
                aws_http_connection_manager_release(m_connectionManager);
                return m_shutdownPromise.get_future();
            }

            class ManagedConnection final : public HttpClientConnection
            {
              public:
                ManagedConnection(
                    aws_http_connection *connection,
                    std::shared_ptr<HttpClientConnectionManager> connectionManager)
                    : HttpClientConnection(connection, connectionManager->m_allocator),
                      m_connectionManager(std::move(connectionManager))
                {
                }

                ~ManagedConnection() override
                {
                    if (m_connection)
                    {
                        aws_http_connection_manager_release_connection(
                            m_connectionManager->m_connectionManager, m_connection);
                        m_connection = nullptr;
                    }
                }

              private:
                std::shared_ptr<HttpClientConnectionManager> m_connectionManager;
            };

            void HttpClientConnectionManager::s_onConnectionSetup(
                aws_http_connection *connection,
                int errorCode,
                void *userData) noexcept
            {
                auto callbackArgs = static_cast<ConnectionManagerCallbackArgs *>(userData);
                std::shared_ptr<HttpClientConnectionManager> manager = callbackArgs->m_connectionManager;
                auto callback = std::move(callbackArgs->m_onClientConnectionAvailable);

                Delete(callbackArgs, manager->m_allocator);

                if (errorCode)
                {
                    callback(nullptr, errorCode);
                    return;
                }

                auto allocator = manager->m_allocator;
                auto connectionRawObj = Aws::Crt::New<ManagedConnection>(manager->m_allocator, connection, manager);

                if (!connectionRawObj)
                {
                    aws_http_connection_manager_release_connection(manager->m_connectionManager, connection);
                    callback(nullptr, AWS_ERROR_OOM);
                    return;
                }
                auto connectionObj = std::shared_ptr<ManagedConnection>(
                    connectionRawObj,
                    [allocator](ManagedConnection *managedConnection) { Delete(managedConnection, allocator); });

                callback(connectionObj, AWS_OP_SUCCESS);
            }

        } // namespace Http
    }     // namespace Crt
} // namespace Aws