Skip to content

Commit 1bafd4e

Browse files
authored
Proxy support (aws#55)
* Update APIs to match H1B revision * Misc updates
1 parent c308022 commit 1bafd4e

19 files changed

+572
-259
lines changed

include/aws/crt/Api.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,5 +60,6 @@ namespace Aws
6060

6161
AWS_CRT_CPP_API const char *ErrorDebugString(int error) noexcept;
6262
AWS_CRT_CPP_API int LastError() noexcept;
63+
AWS_CRT_CPP_API int LastErrorOrUnknown() noexcept;
6364
} // namespace Crt
6465
} // namespace Aws

include/aws/crt/Types.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ namespace Aws
4545

4646
namespace Io
4747
{
48-
using SocketOptions = aws_socket_options;
4948
using IStream = std::basic_istream<char, std::char_traits<char>>;
5049
} // namespace Io
5150

include/aws/crt/http/HttpConnection.h

Lines changed: 228 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
#include <aws/crt/Types.h>
2020
#include <aws/crt/io/Bootstrap.h>
21+
#include <aws/crt/io/SocketOptions.h>
2122
#include <aws/crt/io/TlsOptions.h>
2223

2324
#include <functional>
@@ -99,7 +100,7 @@ namespace Aws
99100
/**
100101
* POD structure used for setting up an Http Request
101102
*/
102-
struct HttpRequestOptions
103+
struct AWS_CRT_CPP_API HttpRequestOptions
103104
{
104105

105106
HttpRequest *request;
@@ -181,7 +182,7 @@ namespace Aws
181182
friend class HttpClientConnection;
182183
};
183184

184-
class HttpClientStream final : public HttpStream
185+
class AWS_CRT_CPP_API HttpClientStream final : public HttpStream
185186
{
186187
public:
187188
~HttpClientStream() = default;
@@ -202,49 +203,251 @@ namespace Aws
202203
friend class HttpClientConnection;
203204
};
204205

205-
struct HttpClientConnectionOptions
206+
/**
207+
* Mirror of the aws_http_proxy_authentication_type C enum - designates what kind of
208+
* authentication, if any, to use when connecting to a proxy server
209+
*/
210+
enum class AwsHttpProxyAuthenticationType
211+
{
212+
None = AWS_HPAT_NONE,
213+
Basic = AWS_HPAT_BASIC,
214+
};
215+
216+
/**
217+
* Configuration structure that holds all proxy-related http connection options
218+
*/
219+
class AWS_CRT_CPP_API HttpClientConnectionProxyOptions
206220
{
221+
public:
222+
HttpClientConnectionProxyOptions();
223+
HttpClientConnectionProxyOptions(const HttpClientConnectionProxyOptions &rhs) = default;
224+
HttpClientConnectionProxyOptions(HttpClientConnectionProxyOptions &&rhs) = default;
225+
226+
HttpClientConnectionProxyOptions &operator=(const HttpClientConnectionProxyOptions &rhs) = default;
227+
HttpClientConnectionProxyOptions &operator=(HttpClientConnectionProxyOptions &&rhs) = default;
228+
229+
~HttpClientConnectionProxyOptions() = default;
230+
231+
/**
232+
* Sets the name of the proxy server to connect through.
233+
* This value must be set.
234+
*/
235+
void SetHostName(const String &hostName) noexcept { m_hostName = hostName; }
236+
237+
/**
238+
* Returns the name of the proxy server to connect through.
239+
*/
240+
const String &GetHostName() const noexcept { return m_hostName; }
241+
242+
/**
243+
* Sets the port of the proxy server to connect to.
244+
* This value must be set.
245+
*/
246+
void SetPort(uint16_t port) noexcept { m_port = port; }
247+
248+
/**
249+
* Gets the port of the proxy server to connect to.
250+
*/
251+
uint16_t GetPort() const noexcept { return m_port; }
252+
253+
/**
254+
* Sets the TLS options for the proxy connection.
255+
*/
256+
void SetTlsOptions(const Io::TlsConnectionOptions &options) noexcept { m_tlsOptions = options; }
257+
258+
/**
259+
* Gets the TLS options for the proxy connection.
260+
*/
261+
const Io::TlsConnectionOptions *GetTlsOptions() const noexcept
262+
{
263+
return m_tlsOptions.has_value() ? &m_tlsOptions.value() : nullptr;
264+
}
265+
266+
/**
267+
* Sets what kind of authentication approach to use when connecting to the proxy
268+
*/
269+
void SetAuthenticationType(AwsHttpProxyAuthenticationType authType) noexcept { m_authType = authType; }
270+
271+
/**
272+
* Gets what kind of authentication approach to use when connecting to the proxy
273+
*/
274+
AwsHttpProxyAuthenticationType GetAuthenticationType() const noexcept { return m_authType; }
275+
276+
/**
277+
* Sets the username to use when connecting to the proxy via basic authentication
278+
*/
279+
void SetBasicAuthUsername(const String &username) noexcept { m_basicAuthUsername = username; }
280+
281+
/**
282+
* Gets the username to use when connecting to the proxy via basic authentication
283+
*/
284+
const String &GetBasicAuthUsername() const noexcept { return m_basicAuthUsername; }
285+
286+
/**
287+
* Sets the password to use when connecting to the proxy via basic authentication
288+
*/
289+
void SetBasicAuthPassword(const String &password) noexcept { m_basicAuthPassword = password; }
290+
291+
/**
292+
* Gets the password to use when connecting to the proxy via basic authentication
293+
*/
294+
const String &GetBasicAuthPassword() const noexcept { return m_basicAuthPassword; }
295+
296+
private:
297+
String m_hostName;
298+
uint16_t m_port;
299+
Optional<Io::TlsConnectionOptions> m_tlsOptions;
300+
AwsHttpProxyAuthenticationType m_authType;
301+
String m_basicAuthUsername;
302+
String m_basicAuthPassword;
303+
};
304+
305+
/**
306+
* Configuration structure holding all options relating to http connection establishment
307+
*/
308+
class AWS_CRT_CPP_API HttpClientConnectionOptions
309+
{
310+
public:
207311
HttpClientConnectionOptions();
208-
Allocator *allocator;
312+
HttpClientConnectionOptions(const HttpClientConnectionOptions &rhs) = default;
313+
HttpClientConnectionOptions(HttpClientConnectionOptions &&rhs) = default;
314+
315+
~HttpClientConnectionOptions() = default;
316+
317+
HttpClientConnectionOptions &operator=(const HttpClientConnectionOptions &rhs) = default;
318+
HttpClientConnectionOptions &operator=(HttpClientConnectionOptions &&rhs) = default;
209319

210320
/**
211-
* Client bootstrap to use for setting up and tearing down connections.
321+
* Sets the client bootstrap to use for setting up and tearing down connections.
322+
* This value must be set.
212323
*/
213-
Io::ClientBootstrap *bootstrap;
324+
void SetBootstrap(Io::ClientBootstrap *bootstrap) noexcept { m_bootstrap = bootstrap; }
325+
214326
/**
215-
* `initialWindowSize` will set the TCP read window
216-
* allowed for Http 1.1 connections and Initial Windows for H2 connections.
327+
* Gets the client bootstrap to use for setting up and tearing down connections.
217328
*/
218-
size_t initialWindowSize;
329+
Io::ClientBootstrap *GetBootstrap() const noexcept { return m_bootstrap; }
330+
219331
/**
220-
* See `OnConnectionSetup` for more info. This value cannot be empty.
332+
* Sets the TCP read window allowed for Http 1.1 connections and Initial Windows for H2 connections.
221333
*/
222-
OnConnectionSetup onConnectionSetup;
334+
void SetInitialWindowSize(size_t initialWindowSize) noexcept
335+
{
336+
m_initialWindowSize = initialWindowSize;
337+
}
338+
339+
/**
340+
* Gets the TCP read window allowed for Http 1.1 connections and Initial Windows for H2 connections.
341+
*/
342+
size_t GetInitialWindowSize() const noexcept { return m_initialWindowSize; }
343+
223344
/**
224-
* See `OnConnectionShutdown` for more info. This value cannot be empty.
345+
* Sets the callback invoked on connection establishment, whether success or failure.
346+
* See `OnConnectionSetup` for more info.
347+
* This value must be set.
225348
*/
226-
OnConnectionShutdown onConnectionShutdown;
349+
void SetOnConnectionSetupCallback(const OnConnectionSetup &callback) noexcept
350+
{
351+
m_onConnectionSetup = callback;
352+
}
227353

228354
/**
229-
* hostname to connect to.
355+
* Gets the callback invoked on connection establishment, whether success or failure.
356+
* See `OnConnectionSetup` for more info.
230357
*/
231-
ByteCursor hostName;
358+
const OnConnectionSetup &GetOnConnectionSetupCallback() const noexcept { return m_onConnectionSetup; }
232359

233360
/**
234-
* port on host to connect to.
361+
* Sets the callback invoked on connection shutdown.
362+
* See `OnConnectionShutdown` for more info.
363+
* This value must be set.
235364
*/
236-
uint16_t port;
365+
void SetOnConnectionShutdownCallback(const OnConnectionShutdown &callback) noexcept
366+
{
367+
m_onConnectionShutdown = callback;
368+
}
237369

238370
/**
239-
* socket options to use for connection.
371+
* Gets the callback invoked on connection shutdown.
372+
* See `OnConnectionShutdown` for more info.
240373
*/
241-
Io::SocketOptions *socketOptions;
374+
const OnConnectionShutdown &GetOnConnectionShutdownCallback() const noexcept
375+
{
376+
return m_onConnectionShutdown;
377+
}
242378

243379
/**
244-
* Tls options to use. If null, and http (plain-text) connection will be attempted. Otherwise,
245-
* https will be used.
380+
* Sets the name of the http server to connect to.
381+
* This value must be set.
246382
*/
247-
Io::TlsConnectionOptions *tlsConnOptions;
383+
void SetHostName(const String &hostName) noexcept { m_hostName = hostName; }
384+
385+
/**
386+
* Gets the name of the http server to connect to.
387+
*/
388+
const String &GetHostName() const noexcept { return m_hostName; }
389+
390+
/**
391+
* Sets the port of the http server to connect to.
392+
* This value must be set.
393+
*/
394+
void SetPort(uint16_t port) noexcept { m_port = port; }
395+
396+
/**
397+
* Gets the port of the http server to connect to
398+
*/
399+
uint16_t GetPort() const noexcept { return m_port; }
400+
401+
/**
402+
* Sets the socket options of the connection.
403+
* This value must be set.
404+
*/
405+
void SetSocketOptions(const Io::SocketOptions &options) noexcept { m_socketOptions = options; }
406+
407+
/**
408+
* Gets the socket options of the connection.
409+
*/
410+
const Io::SocketOptions &GetSocketOptions() const noexcept { return m_socketOptions; }
411+
412+
/**
413+
* Sets the TLS options for the http connection.
414+
*/
415+
void SetTlsOptions(const Io::TlsConnectionOptions &options) noexcept { m_tlsOptions = options; }
416+
417+
/**
418+
* Gets the TLS options for the http connection.
419+
*/
420+
const Io::TlsConnectionOptions *GetTlsOptions() const noexcept
421+
{
422+
return m_tlsOptions.has_value() ? &m_tlsOptions.value() : nullptr;
423+
}
424+
425+
/**
426+
* Sets the proxy options for the http connection.
427+
*/
428+
void SetProxyOptions(const HttpClientConnectionProxyOptions &options) noexcept
429+
{
430+
m_proxyOptions = options;
431+
}
432+
433+
/**
434+
* Gets the proxy options for the http connection.
435+
*/
436+
const HttpClientConnectionProxyOptions *GetProxyOptions() const noexcept
437+
{
438+
return m_proxyOptions.has_value() ? &m_proxyOptions.value() : nullptr;
439+
}
440+
441+
private:
442+
Io::ClientBootstrap *m_bootstrap;
443+
size_t m_initialWindowSize;
444+
OnConnectionSetup m_onConnectionSetup;
445+
OnConnectionShutdown m_onConnectionShutdown;
446+
String m_hostName;
447+
uint16_t m_port;
448+
Io::SocketOptions m_socketOptions;
449+
Optional<Io::TlsConnectionOptions> m_tlsOptions;
450+
Optional<HttpClientConnectionProxyOptions> m_proxyOptions;
248451
};
249452

250453
/**
@@ -295,7 +498,9 @@ namespace Aws
295498
* be invoked. On success, `onConnectionSetup` will be called, either with a connection, or an
296499
* errorCode.
297500
*/
298-
static bool CreateConnection(const HttpClientConnectionOptions &connectionOptions) noexcept;
501+
static bool CreateConnection(
502+
const HttpClientConnectionOptions &connectionOptions,
503+
Allocator *allocator) noexcept;
299504

300505
protected:
301506
HttpClientConnection(aws_http_connection *m_connection, Allocator *allocator) noexcept;

0 commit comments

Comments
 (0)