aws-crt-cpp
C++ wrapper around the aws-c-* libraries. Provides Cross-Platform Transport Protocols and SSL/TLS implementations for C++.
HttpConnection.h
Go to the documentation of this file.
1 #pragma once
2 
6 #include <aws/http/connection.h>
7 #include <aws/http/proxy.h>
8 #include <aws/http/request_response.h>
9 
10 #include <aws/crt/Types.h>
11 #include <aws/crt/io/Bootstrap.h>
13 #include <aws/crt/io/Stream.h>
14 #include <aws/crt/io/TlsOptions.h>
15 
16 #include <functional>
17 #include <memory>
18 
19 namespace Aws
20 {
21  namespace Crt
22  {
23  namespace Io
24  {
25  class ClientBootstrap;
26  }
27 
28  namespace Http
29  {
30  class HttpClientConnection;
31  class HttpStream;
32  class HttpClientStream;
33  class HttpRequest;
34  class HttpProxyStrategy;
35  using HttpHeader = aws_http_header;
36 
43  using OnConnectionSetup =
44  std::function<void(const std::shared_ptr<HttpClientConnection> &connection, int errorCode)>;
45 
54  using OnConnectionShutdown = std::function<void(HttpClientConnection &connection, int errorCode)>;
55 
64  using OnIncomingHeaders = std::function<void(
65  HttpStream &stream,
66  enum aws_http_header_block headerBlock,
67  const HttpHeader *headersArray,
68  std::size_t headersCount)>;
69 
77  std::function<void(HttpStream &stream, enum aws_http_header_block block)>;
78 
85  using OnIncomingBody = std::function<void(HttpStream &stream, const ByteCursor &data)>;
86 
95  using OnStreamComplete = std::function<void(HttpStream &stream, int errorCode)>;
96 
101  {
106 
112 
117 
122 
142  bool UseManualDataWrites = false;
143  };
144 
149  class AWS_CRT_CPP_API HttpStream : public std::enable_shared_from_this<HttpStream>
150  {
151  public:
152  virtual ~HttpStream();
153  HttpStream(const HttpStream &) = delete;
154  HttpStream(HttpStream &&) = delete;
155  HttpStream &operator=(const HttpStream &) = delete;
156  HttpStream &operator=(HttpStream &&) = delete;
157 
161  HttpClientConnection &GetConnection() const noexcept;
162 
166  virtual int GetResponseStatusCode() const noexcept = 0;
167 
177  void UpdateWindow(std::size_t incrementSize) noexcept;
178 
179  protected:
180  aws_http_stream *m_stream;
181  std::shared_ptr<HttpClientConnection> m_connection;
182  HttpStream(const std::shared_ptr<HttpClientConnection> &connection) noexcept;
183 
184  private:
185  OnIncomingHeaders m_onIncomingHeaders;
186  OnIncomingHeadersBlockDone m_onIncomingHeadersBlockDone;
187  OnIncomingBody m_onIncomingBody;
188  OnStreamComplete m_onStreamComplete;
189 
190  static int s_onIncomingHeaders(
191  struct aws_http_stream *stream,
192  enum aws_http_header_block headerBlock,
193  const struct aws_http_header *headerArray,
194  size_t numHeaders,
195  void *userData) noexcept;
196  static int s_onIncomingHeaderBlockDone(
197  struct aws_http_stream *stream,
198  enum aws_http_header_block headerBlock,
199  void *userData) noexcept;
200  static int s_onIncomingBody(
201  struct aws_http_stream *stream,
202  const struct aws_byte_cursor *data,
203  void *userData) noexcept;
204  static void s_onStreamComplete(struct aws_http_stream *stream, int errorCode, void *userData) noexcept;
205 
206  friend class HttpClientConnection;
207  };
208 
210  {
211  ClientStreamCallbackData() : allocator(nullptr), stream(nullptr) {}
213  std::shared_ptr<HttpStream> stream;
214  };
215 
216  using OnWriteDataComplete = std::function<void(std::shared_ptr<HttpStream> &stream, int errorCode)>;
217 
222  {
223  public:
224  ~HttpClientStream();
225  HttpClientStream(const HttpClientStream &) = delete;
226  HttpClientStream(HttpClientStream &&) = delete;
227  HttpClientStream &operator=(const HttpClientStream &) = delete;
228  HttpClientStream &operator=(HttpClientStream &&) = delete;
229 
234  virtual int GetResponseStatusCode() const noexcept override;
235 
241  bool Activate() noexcept;
242 
243  int WriteData(
244  std::shared_ptr<Aws::Crt::Io::InputStream> stream,
245  const OnWriteDataComplete &onComplete,
246  bool endStream = false) noexcept;
247 
248  private:
249  HttpClientStream(const std::shared_ptr<HttpClientConnection> &connection) noexcept;
250 
251  ClientStreamCallbackData m_callbackData;
252  friend class HttpClientConnection;
253  };
254 
262  {
263  None,
264  Basic,
265  };
266 
272  {
280  Legacy = AWS_HPCT_HTTP_LEGACY,
281 
286  Forwarding = AWS_HPCT_HTTP_FORWARD,
287 
292  Tunneling = AWS_HPCT_HTTP_TUNNEL,
293  };
294 
299  {
300  public:
304 
307 
309 
318  void InitializeRawProxyOptions(struct aws_http_proxy_options &raw_options) const;
319 
325 
330  uint32_t Port;
331 
337 
342 
347  std::shared_ptr<HttpProxyStrategy> ProxyStrategy;
348 
358 
364 
370  };
371 
375  enum class ProxyEnvVarType
376  {
381  Disabled = AWS_HPEV_DISABLE,
390  Enabled = AWS_HPEV_ENABLE,
391  };
392 
397  {
398  public:
400  ProxyEnvVarOptions(const ProxyEnvVarOptions &rhs) = default;
401  ProxyEnvVarOptions(ProxyEnvVarOptions &&rhs) = default;
402 
403  ProxyEnvVarOptions &operator=(const ProxyEnvVarOptions &rhs) = default;
404  ProxyEnvVarOptions &operator=(ProxyEnvVarOptions &&rhs) = default;
405 
406  ~ProxyEnvVarOptions() = default;
407 
416  void InitializeRawProxyOptions(struct proxy_env_var_settings &raw_options) const;
417 
422 
430 
436  };
437 
442  {
443  public:
447 
448  ~HttpClientConnectionOptions() = default;
449 
450  HttpClientConnectionOptions &operator=(const HttpClientConnectionOptions &rhs) = default;
451  HttpClientConnectionOptions &operator=(HttpClientConnectionOptions &&rhs) = default;
452 
459 
464 
471 
478 
484 
489  uint32_t Port;
490 
496 
502 
508 
517  };
518 
519  enum class HttpVersion
520  {
521  Unknown = AWS_HTTP_VERSION_UNKNOWN,
522  Http1_0 = AWS_HTTP_VERSION_1_0,
523  Http1_1 = AWS_HTTP_VERSION_1_1,
524  Http2 = AWS_HTTP_VERSION_2,
525  };
526 
530  class AWS_CRT_CPP_API HttpClientConnection : public std::enable_shared_from_this<HttpClientConnection>
531  {
532  public:
533  virtual ~HttpClientConnection() = default;
534  HttpClientConnection(const HttpClientConnection &) = delete;
536  HttpClientConnection &operator=(const HttpClientConnection &) = delete;
537  HttpClientConnection &operator=(HttpClientConnection &&) = delete;
538 
551  std::shared_ptr<HttpClientStream> NewClientStream(const HttpRequestOptions &requestOptions) noexcept;
552 
556  bool IsOpen() const noexcept;
557 
565  void Close() noexcept;
566 
570  HttpVersion GetVersion() noexcept;
571 
575  int LastError() const noexcept { return m_lastError; }
576 
585  static bool CreateConnection(
586  const HttpClientConnectionOptions &connectionOptions,
587  Allocator *allocator) noexcept;
588 
589  protected:
590  HttpClientConnection(aws_http_connection *m_connection, Allocator *allocator) noexcept;
591  aws_http_connection *m_connection;
592 
593  private:
594  Allocator *m_allocator;
595  int m_lastError;
596 
597  static void s_onClientConnectionSetup(
598  struct aws_http_connection *connection,
599  int error_code,
600  void *user_data) noexcept;
601  static void s_onClientConnectionShutdown(
602  struct aws_http_connection *connection,
603  int error_code,
604  void *user_data) noexcept;
605  };
606 
607  } // namespace Http
608  } // namespace Crt
609 } // namespace Aws
String HostName
Definition: HttpConnection.h:483
String HostName
Definition: HttpConnection.h:324
OnIncomingHeadersBlockDone onIncomingHeadersBlockDone
Definition: HttpConnection.h:111
aws_http_stream * m_stream
Definition: HttpConnection.h:180
#define AWS_CRT_CPP_API
Definition: Exports.h:36
HttpRequest * request
Definition: HttpConnection.h:105
OnIncomingHeaders onIncomingHeaders
Definition: HttpConnection.h:110
std::shared_ptr< HttpStream > stream
Definition: HttpConnection.h:213
Definition: HttpConnection.h:100
OnConnectionShutdown OnConnectionShutdownCallback
Definition: HttpConnection.h:477
AwsHttpProxyConnectionType ProxyConnectionType
Definition: HttpConnection.h:341
Definition: HttpConnection.h:221
String BasicAuthUsername
Definition: HttpConnection.h:363
std::function< void(HttpClientConnection &connection, int errorCode)> OnConnectionShutdown
Definition: HttpConnection.h:54
String BasicAuthPassword
Definition: HttpConnection.h:369
std::basic_string< char, std::char_traits< char >, StlAllocator< char >> String
Definition: Types.h:45
std::shared_ptr< HttpProxyStrategy > ProxyStrategy
Definition: HttpConnection.h:347
bool ManualWindowManagement
Definition: HttpConnection.h:516
aws_allocator Allocator
Definition: Allocator.h:14
uint32_t Port
Definition: HttpConnection.h:489
Definition: HttpRequestResponse.h:109
Definition: HttpConnection.h:209
Definition: HttpConnection.h:298
Allocator * allocator
Definition: HttpConnection.h:212
Definition: HttpConnection.h:530
Optional< HttpClientConnectionProxyOptions > ProxyOptions
Definition: HttpConnection.h:507
std::function< void(std::shared_ptr< HttpStream > &stream, int errorCode)> OnWriteDataComplete
Definition: HttpConnection.h:216
AwsHttpProxyAuthenticationType AuthType
Definition: HttpConnection.h:357
Optional< Io::TlsConnectionOptions > TlsOptions
Definition: HttpConnection.h:336
Optional< Io::TlsConnectionOptions > TlsOptions
Definition: HttpConnection.h:435
size_t InitialWindowSize
Definition: HttpConnection.h:463
ProxyEnvVarType
Definition: HttpConnection.h:375
Definition: HttpConnection.h:441
AwsHttpProxyAuthenticationType
Definition: HttpConnection.h:261
std::function< void(HttpStream &stream, const ByteCursor &data)> OnIncomingBody
Definition: HttpConnection.h:85
std::function< void(HttpStream &stream, int errorCode)> OnStreamComplete
Definition: HttpConnection.h:95
ClientStreamCallbackData()
Definition: HttpConnection.h:211
Definition: Bootstrap.h:34
Io::ClientBootstrap * Bootstrap
Definition: HttpConnection.h:458
Definition: HttpConnection.h:149
std::function< void(const std::shared_ptr< HttpClientConnection > &connection, int errorCode)> OnConnectionSetup
Definition: HttpConnection.h:44
aws_http_header HttpHeader
Definition: HttpConnection.h:35
Io::SocketOptions SocketOptions
Definition: HttpConnection.h:495
AwsHttpProxyConnectionType connectionType
Definition: HttpConnection.h:429
std::function< void(HttpStream &stream, enum aws_http_header_block headerBlock, const HttpHeader *headersArray, std::size_t headersCount)> OnIncomingHeaders
Definition: HttpConnection.h:68
std::shared_ptr< HttpClientConnection > m_connection
Definition: HttpConnection.h:181
std::function< void(HttpStream &stream, enum aws_http_header_block block)> OnIncomingHeadersBlockDone
Definition: HttpConnection.h:77
OnStreamComplete onStreamComplete
Definition: HttpConnection.h:121
aws_http_connection * m_connection
Definition: HttpConnection.h:591
Definition: HttpConnection.h:396
HttpVersion
Definition: HttpConnection.h:519
int LastError() const noexcept
Definition: HttpConnection.h:575
OnIncomingBody onIncomingBody
Definition: HttpConnection.h:116
AwsHttpProxyConnectionType
Definition: HttpConnection.h:271
uint32_t Port
Definition: HttpConnection.h:330
OnConnectionSetup OnConnectionSetupCallback
Definition: HttpConnection.h:470
ProxyEnvVarType proxyEnvVarType
Definition: HttpConnection.h:421
Definition: SocketOptions.h:47
Definition: Allocator.h:10
Optional< Io::TlsConnectionOptions > TlsOptions
Definition: HttpConnection.h:501