AWS IoT Device SDK C++ v2  1.43.1
AWS IoT Device SDK C++ v2
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 
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;
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();
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;
402 
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 
449 
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;
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
Aws::Crt::Http::HttpVersion
HttpVersion
Definition: HttpConnection.h:520
Aws::Crt::Http::HttpClientConnectionOptions::~HttpClientConnectionOptions
~HttpClientConnectionOptions()=default
Aws::Crt::Http::HttpRequestOptions::onIncomingBody
OnIncomingBody onIncomingBody
Definition: HttpConnection.h:116
Aws::Crt::Http::AwsHttpProxyConnectionType
AwsHttpProxyConnectionType
Definition: HttpConnection.h:272
Aws::Crt::Http::ClientStreamCallbackData::ClientStreamCallbackData
ClientStreamCallbackData()
Definition: HttpConnection.h:211
Aws::Crt::Http::HttpClientConnectionOptions::InitialWindowSize
size_t InitialWindowSize
Definition: HttpConnection.h:463
Aws::Crt::Http::ProxyEnvVarOptions::proxyEnvVarType
ProxyEnvVarType proxyEnvVarType
Definition: HttpConnection.h:421
Aws::Crt::Http::HttpStream::operator=
HttpStream & operator=(HttpStream &&)=delete
Aws::Crt::Http::HttpClientConnectionOptions::Port
uint32_t Port
Definition: HttpConnection.h:489
Aws::Crt::Http::ProxyEnvVarOptions::connectionType
AwsHttpProxyConnectionType connectionType
Definition: HttpConnection.h:429
Aws::Crt::Http::HttpClientConnectionOptions::SocketOptions
Io::SocketOptions SocketOptions
Definition: HttpConnection.h:495
Aws::Crt::Http::HttpClientConnectionOptions::HostName
String HostName
Definition: HttpConnection.h:483
Aws::Crt::Http::AwsHttpProxyAuthenticationType
AwsHttpProxyAuthenticationType
Definition: HttpConnection.h:262
Aws::Crt::Http::HttpRequestOptions::request
HttpRequest * request
Definition: HttpConnection.h:105
Aws::Crt::Http::HttpStream::HttpStream
HttpStream(HttpStream &&)=delete
TlsOptions.h
Aws::Crt::Mqtt5::OutboundTopicAliasBehaviorType::Default
@ Default
Aws::Crt::Http::HttpHeader
aws_http_header HttpHeader
Definition: HttpConnection.h:35
Aws::Crt::Http::OnIncomingHeaders
std::function< void(HttpStream &stream, enum aws_http_header_block headerBlock, const HttpHeader *headersArray, std::size_t headersCount)> OnIncomingHeaders
Definition: HttpConnection.h:68
Aws::Crt::Http::OnIncomingHeadersBlockDone
std::function< void(HttpStream &stream, enum aws_http_header_block block)> OnIncomingHeadersBlockDone
Definition: HttpConnection.h:77
Aws::Crt::Http::HttpRequest
Definition: HttpRequestResponse.h:110
Aws::Crt::Http::HttpClientConnectionProxyOptions::operator=
HttpClientConnectionProxyOptions & operator=(const HttpClientConnectionProxyOptions &rhs)=default
Aws::Crt::Http::HttpClientConnectionProxyOptions::HostName
String HostName
Definition: HttpConnection.h:324
Aws::Crt::Http::HttpClientConnectionOptions::operator=
HttpClientConnectionOptions & operator=(const HttpClientConnectionOptions &rhs)=default
Aws::Crt::LastError
AWS_CRT_CPP_API int LastError() noexcept
Definition: Api.cpp:464
Aws::Crt::Http::HttpClientConnection::~HttpClientConnection
virtual ~HttpClientConnection()=default
Aws::Crt::Http::OnStreamComplete
std::function< void(HttpStream &stream, int errorCode)> OnStreamComplete
Definition: HttpConnection.h:95
Aws::Crt::Http::HttpClientConnectionProxyOptions::~HttpClientConnectionProxyOptions
~HttpClientConnectionProxyOptions()=default
Aws::Crt::Http::OnConnectionSetup
std::function< void(const std::shared_ptr< HttpClientConnection > &connection, int errorCode)> OnConnectionSetup
Definition: HttpConnection.h:44
Aws::Crt::Http::HttpStream::operator=
HttpStream & operator=(const HttpStream &)=delete
Aws::Crt::Http::ClientStreamCallbackData::stream
std::shared_ptr< HttpStream > stream
Definition: HttpConnection.h:213
Aws::Crt::Http::HttpClientConnectionProxyOptions::ProxyConnectionType
AwsHttpProxyConnectionType ProxyConnectionType
Definition: HttpConnection.h:341
Aws::Crt::Http::ProxyEnvVarOptions::ProxyEnvVarOptions
ProxyEnvVarOptions(ProxyEnvVarOptions &&rhs)=default
Aws::Crt::Http::HttpVersion::Unknown
@ Unknown
Aws::Crt::Http::HttpClientConnectionOptions::ManualWindowManagement
bool ManualWindowManagement
Definition: HttpConnection.h:516
Aws::Crt::Http::HttpRequestOptions::onIncomingHeaders
OnIncomingHeaders onIncomingHeaders
Definition: HttpConnection.h:110
Aws::Crt::Http::HttpClientConnectionProxyOptions::HttpClientConnectionProxyOptions
HttpClientConnectionProxyOptions(HttpClientConnectionProxyOptions &&rhs)=default
Aws::Crt::Http::HttpClientConnectionOptions::HttpClientConnectionOptions
HttpClientConnectionOptions(const HttpClientConnectionOptions &rhs)=default
Aws::Crt::Http::HttpClientConnection::operator=
HttpClientConnection & operator=(HttpClientConnection &&)=delete
Aws::Crt::Http::HttpClientConnectionProxyOptions::TlsOptions
Optional< Io::TlsConnectionOptions > TlsOptions
Definition: HttpConnection.h:336
Aws::Crt::Http::ProxyEnvVarOptions
Definition: HttpConnection.h:397
Aws::Crt::Http::HttpClientConnectionProxyOptions::BasicAuthPassword
String BasicAuthPassword
Definition: HttpConnection.h:369
Aws::Crt::Http::HttpClientConnectionOptions::OnConnectionSetupCallback
OnConnectionSetup OnConnectionSetupCallback
Definition: HttpConnection.h:470
Aws::Crt::Http::ProxyEnvVarOptions::operator=
ProxyEnvVarOptions & operator=(ProxyEnvVarOptions &&rhs)=default
Aws::Crt::Http::HttpClientConnection::HttpClientConnection
HttpClientConnection(const HttpClientConnection &)=delete
Aws::Crt::Http::HttpClientConnectionOptions::operator=
HttpClientConnectionOptions & operator=(HttpClientConnectionOptions &&rhs)=default
Aws::Crt::Http::ClientStreamCallbackData::allocator
Allocator * allocator
Definition: HttpConnection.h:212
Aws::Crt::Http::HttpClientConnection::operator=
HttpClientConnection & operator=(const HttpClientConnection &)=delete
Aws::Crt::Http::ClientStreamCallbackData
Definition: HttpConnection.h:210
Aws::Crt::Http::AwsHttpProxyConnectionType::Legacy
@ Legacy
Aws::Crt::Optional
Definition: Optional.h:19
Aws::Crt::Http::HttpClientConnectionProxyOptions::ProxyStrategy
std::shared_ptr< HttpProxyStrategy > ProxyStrategy
Definition: HttpConnection.h:347
Aws::Crt::Http::HttpClientConnectionOptions
Definition: HttpConnection.h:442
Aws::Crt::Http::AwsHttpProxyAuthenticationType::None
@ None
Aws::Crt::Http::HttpClientStream::operator=
HttpClientStream & operator=(HttpClientStream &&)=delete
Aws::Crt::Http::HttpClientStream::operator=
HttpClientStream & operator=(const HttpClientStream &)=delete
Aws::Crt::Http::HttpClientConnection
Definition: HttpConnection.h:531
Types.h
Aws::Crt::Http::HttpClientConnectionProxyOptions::HttpClientConnectionProxyOptions
HttpClientConnectionProxyOptions(const HttpClientConnectionProxyOptions &rhs)=default
Aws::Crt::Http::HttpClientConnection::m_connection
aws_http_connection * m_connection
Definition: HttpConnection.h:591
Aws::Crt::Http::HttpClientConnectionProxyOptions::AuthType
AwsHttpProxyAuthenticationType AuthType
Definition: HttpConnection.h:357
Aws::Crt::Http::ProxyEnvVarOptions::TlsOptions
Optional< Io::TlsConnectionOptions > TlsOptions
Definition: HttpConnection.h:435
Aws::Crt::Http::OnConnectionShutdown
std::function< void(HttpClientConnection &connection, int errorCode)> OnConnectionShutdown
Definition: HttpConnection.h:54
Aws::Crt::Http::HttpClientStream::HttpClientStream
HttpClientStream(const HttpClientStream &)=delete
Aws::Crt::Http::HttpClientConnectionOptions::HttpClientConnectionOptions
HttpClientConnectionOptions(HttpClientConnectionOptions &&rhs)=default
Aws::Crt::Http::HttpClientConnection::HttpClientConnection
HttpClientConnection(HttpClientConnection &&)=delete
Aws::Crt::Http::OnIncomingBody
std::function< void(HttpStream &stream, const ByteCursor &data)> OnIncomingBody
Definition: HttpConnection.h:85
Aws::Crt::Http::HttpClientConnectionProxyOptions
Definition: HttpConnection.h:299
Aws::Crt::Io::SocketOptions
Definition: SocketOptions.h:48
Aws::Crt::Http::HttpRequestOptions::onStreamComplete
OnStreamComplete onStreamComplete
Definition: HttpConnection.h:121
Aws::Crt::Http::HttpClientConnectionOptions::TlsOptions
Optional< Io::TlsConnectionOptions > TlsOptions
Definition: HttpConnection.h:501
std
Definition: StringView.h:862
AWS_CRT_CPP_API
#define AWS_CRT_CPP_API
Definition: Exports.h:36
Aws
Definition: Allocator.h:11
SocketOptions.h
Bootstrap.h
Aws::Crt::Http::ProxyEnvVarOptions::~ProxyEnvVarOptions
~ProxyEnvVarOptions()=default
Aws::Crt::ByteCursor
aws_byte_cursor ByteCursor
Definition: Types.h:31
Aws::Crt::Http::HttpClientConnectionProxyOptions::operator=
HttpClientConnectionProxyOptions & operator=(HttpClientConnectionProxyOptions &&rhs)=default
Aws::Crt::Http::HttpClientConnectionProxyOptions::BasicAuthUsername
String BasicAuthUsername
Definition: HttpConnection.h:363
Aws::Crt::Allocator
aws_allocator Allocator
Definition: Allocator.h:14
Aws::Crt::Http::ProxyEnvVarOptions::operator=
ProxyEnvVarOptions & operator=(const ProxyEnvVarOptions &rhs)=default
Aws::Crt::Http::OnWriteDataComplete
std::function< void(std::shared_ptr< HttpStream > &stream, int errorCode)> OnWriteDataComplete
Definition: HttpConnection.h:216
Aws::Crt::Http::HttpStream
Definition: HttpConnection.h:150
Aws::Crt::Http::ProxyEnvVarType
ProxyEnvVarType
Definition: HttpConnection.h:376
Aws::Crt::Io::ClientBootstrap
Definition: Bootstrap.h:35
Aws::Crt::Http::HttpClientConnectionOptions::Bootstrap
Io::ClientBootstrap * Bootstrap
Definition: HttpConnection.h:458
Aws::Crt::Http::HttpClientConnectionProxyOptions::Port
uint32_t Port
Definition: HttpConnection.h:330
Aws::Crt::Http::ProxyEnvVarType::Disabled
@ Disabled
Aws::Crt::Http::HttpRequestOptions::onIncomingHeadersBlockDone
OnIncomingHeadersBlockDone onIncomingHeadersBlockDone
Definition: HttpConnection.h:111
Aws::Crt::Http::HttpClientStream
Definition: HttpConnection.h:222
Aws::Crt::Http::HttpClientConnectionOptions::OnConnectionShutdownCallback
OnConnectionShutdown OnConnectionShutdownCallback
Definition: HttpConnection.h:477
Aws::Crt::String
std::basic_string< char, std::char_traits< char >, StlAllocator< char > > String
Definition: Types.h:45
Aws::Crt::Http::HttpStream::HttpStream
HttpStream(const HttpStream &)=delete
Aws::Crt::Http::HttpClientStream::HttpClientStream
HttpClientStream(HttpClientStream &&)=delete
Aws::Crt::Http::HttpRequestOptions
Definition: HttpConnection.h:101
Aws::Crt::Http::HttpClientConnectionOptions::ProxyOptions
Optional< HttpClientConnectionProxyOptions > ProxyOptions
Definition: HttpConnection.h:507
Aws::Crt::Http::ProxyEnvVarOptions::ProxyEnvVarOptions
ProxyEnvVarOptions(const ProxyEnvVarOptions &rhs)=default
Stream.h