AWS IoT Device SDK C++ v2  1.34.0
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/TlsOptions.h>
14 
15 #include <functional>
16 #include <memory>
17 
18 namespace Aws
19 {
20  namespace Crt
21  {
22  namespace Io
23  {
24  class ClientBootstrap;
25  }
26 
27  namespace Http
28  {
29  class HttpClientConnection;
30  class HttpStream;
31  class HttpClientStream;
32  class HttpRequest;
33  class HttpProxyStrategy;
34  using HttpHeader = aws_http_header;
35 
43  std::function<void(const std::shared_ptr<HttpClientConnection> &connection, int errorCode)>;
44 
53  using OnConnectionShutdown = std::function<void(HttpClientConnection &connection, int errorCode)>;
54 
63  using OnIncomingHeaders = std::function<void(
64  HttpStream &stream,
65  enum aws_http_header_block headerBlock,
66  const HttpHeader *headersArray,
67  std::size_t headersCount)>;
68 
76  std::function<void(HttpStream &stream, enum aws_http_header_block block)>;
77 
84  using OnIncomingBody = std::function<void(HttpStream &stream, const ByteCursor &data)>;
85 
94  using OnStreamComplete = std::function<void(HttpStream &stream, int errorCode)>;
95 
100  {
105 
111 
116 
121  };
122 
127  class AWS_CRT_CPP_API HttpStream : public std::enable_shared_from_this<HttpStream>
128  {
129  public:
130  virtual ~HttpStream();
131  HttpStream(const HttpStream &) = delete;
132  HttpStream(HttpStream &&) = delete;
133  HttpStream &operator=(const HttpStream &) = delete;
135 
139  HttpClientConnection &GetConnection() const noexcept;
140 
144  virtual int GetResponseStatusCode() const noexcept = 0;
145 
155  void UpdateWindow(std::size_t incrementSize) noexcept;
156 
157  protected:
158  aws_http_stream *m_stream;
159  std::shared_ptr<HttpClientConnection> m_connection;
160  HttpStream(const std::shared_ptr<HttpClientConnection> &connection) noexcept;
161 
162  private:
163  OnIncomingHeaders m_onIncomingHeaders;
164  OnIncomingHeadersBlockDone m_onIncomingHeadersBlockDone;
165  OnIncomingBody m_onIncomingBody;
166  OnStreamComplete m_onStreamComplete;
167 
168  static int s_onIncomingHeaders(
169  struct aws_http_stream *stream,
170  enum aws_http_header_block headerBlock,
171  const struct aws_http_header *headerArray,
172  size_t numHeaders,
173  void *userData) noexcept;
174  static int s_onIncomingHeaderBlockDone(
175  struct aws_http_stream *stream,
176  enum aws_http_header_block headerBlock,
177  void *userData) noexcept;
178  static int s_onIncomingBody(
179  struct aws_http_stream *stream,
180  const struct aws_byte_cursor *data,
181  void *userData) noexcept;
182  static void s_onStreamComplete(struct aws_http_stream *stream, int errorCode, void *userData) noexcept;
183 
184  friend class HttpClientConnection;
185  };
186 
188  {
189  ClientStreamCallbackData() : allocator(nullptr), stream(nullptr) {}
191  std::shared_ptr<HttpStream> stream;
192  };
193 
198  {
199  public:
200  ~HttpClientStream();
205 
210  virtual int GetResponseStatusCode() const noexcept override;
211 
217  bool Activate() noexcept;
218 
219  private:
220  HttpClientStream(const std::shared_ptr<HttpClientConnection> &connection) noexcept;
221 
222  ClientStreamCallbackData m_callbackData;
223  friend class HttpClientConnection;
224  };
225 
233  {
234  None,
235  Basic,
236  };
237 
243  {
251  Legacy = AWS_HPCT_HTTP_LEGACY,
252 
257  Forwarding = AWS_HPCT_HTTP_FORWARD,
258 
263  Tunneling = AWS_HPCT_HTTP_TUNNEL,
264  };
265 
270  {
271  public:
275 
278 
280 
289  void InitializeRawProxyOptions(struct aws_http_proxy_options &raw_options) const;
290 
296 
301  uint32_t Port;
302 
308 
313 
318  std::shared_ptr<HttpProxyStrategy> ProxyStrategy;
319 
329 
335 
341  };
342 
347  {
348  public:
352 
354 
357 
364 
369 
376 
383 
389 
394  uint32_t Port;
395 
401 
407 
413 
422  };
423 
424  enum class HttpVersion
425  {
426  Unknown = AWS_HTTP_VERSION_UNKNOWN,
427  Http1_0 = AWS_HTTP_VERSION_1_0,
428  Http1_1 = AWS_HTTP_VERSION_1_1,
429  Http2 = AWS_HTTP_VERSION_2,
430  };
431 
435  class AWS_CRT_CPP_API HttpClientConnection : public std::enable_shared_from_this<HttpClientConnection>
436  {
437  public:
438  virtual ~HttpClientConnection() = default;
443 
456  std::shared_ptr<HttpClientStream> NewClientStream(const HttpRequestOptions &requestOptions) noexcept;
457 
461  bool IsOpen() const noexcept;
462 
470  void Close() noexcept;
471 
475  HttpVersion GetVersion() noexcept;
476 
480  int LastError() const noexcept { return m_lastError; }
481 
490  static bool CreateConnection(
491  const HttpClientConnectionOptions &connectionOptions,
492  Allocator *allocator) noexcept;
493 
494  protected:
495  HttpClientConnection(aws_http_connection *m_connection, Allocator *allocator) noexcept;
496  aws_http_connection *m_connection;
497 
498  private:
499  Allocator *m_allocator;
500  int m_lastError;
501 
502  static void s_onClientConnectionSetup(
503  struct aws_http_connection *connection,
504  int error_code,
505  void *user_data) noexcept;
506  static void s_onClientConnectionShutdown(
507  struct aws_http_connection *connection,
508  int error_code,
509  void *user_data) noexcept;
510  };
511 
512  } // namespace Http
513  } // namespace Crt
514 } // namespace Aws
Aws::Crt::Http::HttpVersion
HttpVersion
Definition: HttpConnection.h:425
Aws::Crt::Http::HttpClientConnectionOptions::~HttpClientConnectionOptions
~HttpClientConnectionOptions()=default
Aws::Crt::Http::HttpRequestOptions::onIncomingBody
OnIncomingBody onIncomingBody
Definition: HttpConnection.h:115
Aws::Crt::Http::AwsHttpProxyConnectionType
AwsHttpProxyConnectionType
Definition: HttpConnection.h:243
Aws::Crt::Http::ClientStreamCallbackData::ClientStreamCallbackData
ClientStreamCallbackData()
Definition: HttpConnection.h:189
Aws::Crt::Http::HttpClientConnectionOptions::InitialWindowSize
size_t InitialWindowSize
Definition: HttpConnection.h:368
Aws::Crt::Http::HttpStream::operator=
HttpStream & operator=(HttpStream &&)=delete
Aws::Crt::Http::HttpClientConnectionOptions::Port
uint32_t Port
Definition: HttpConnection.h:394
Aws::Crt::Http::HttpClientConnectionOptions::SocketOptions
Io::SocketOptions SocketOptions
Definition: HttpConnection.h:400
Aws::Crt::Http::HttpClientConnectionOptions::HostName
String HostName
Definition: HttpConnection.h:388
Aws::Crt::Http::AwsHttpProxyAuthenticationType
AwsHttpProxyAuthenticationType
Definition: HttpConnection.h:233
Aws::Crt::Http::HttpRequestOptions::request
HttpRequest * request
Definition: HttpConnection.h:104
Aws::Crt::Http::HttpStream::HttpStream
HttpStream(HttpStream &&)=delete
TlsOptions.h
Aws::Crt::Http::HttpHeader
aws_http_header HttpHeader
Definition: HttpConnection.h:34
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:67
Aws::Crt::Http::OnIncomingHeadersBlockDone
std::function< void(HttpStream &stream, enum aws_http_header_block block)> OnIncomingHeadersBlockDone
Definition: HttpConnection.h:76
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:295
Aws::Crt::Http::HttpClientConnectionOptions::operator=
HttpClientConnectionOptions & operator=(const HttpClientConnectionOptions &rhs)=default
Aws::Crt::LastError
AWS_CRT_CPP_API int LastError() noexcept
Definition: Api.cpp:422
Aws::Crt::Http::HttpClientConnection::~HttpClientConnection
virtual ~HttpClientConnection()=default
Aws::Crt::Http::OnStreamComplete
std::function< void(HttpStream &stream, int errorCode)> OnStreamComplete
Definition: HttpConnection.h:94
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:43
Aws::Crt::Http::HttpStream::operator=
HttpStream & operator=(const HttpStream &)=delete
Aws::Crt::Http::ClientStreamCallbackData::stream
std::shared_ptr< HttpStream > stream
Definition: HttpConnection.h:191
Aws::Crt::Http::HttpClientConnectionProxyOptions::ProxyConnectionType
AwsHttpProxyConnectionType ProxyConnectionType
Definition: HttpConnection.h:312
Aws::Crt::Http::HttpVersion::Unknown
@ Unknown
Aws::Crt::Http::HttpClientConnectionOptions::ManualWindowManagement
bool ManualWindowManagement
Definition: HttpConnection.h:421
Aws::Crt::Http::HttpRequestOptions::onIncomingHeaders
OnIncomingHeaders onIncomingHeaders
Definition: HttpConnection.h:109
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:307
Aws::Crt::Http::HttpClientConnectionProxyOptions::BasicAuthPassword
String BasicAuthPassword
Definition: HttpConnection.h:340
Aws::Crt::Http::HttpClientConnectionOptions::OnConnectionSetupCallback
OnConnectionSetup OnConnectionSetupCallback
Definition: HttpConnection.h:375
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:190
Aws::Crt::Http::HttpClientConnection::operator=
HttpClientConnection & operator=(const HttpClientConnection &)=delete
Aws::Crt::Http::ClientStreamCallbackData
Definition: HttpConnection.h:188
Aws::Crt::Http::AwsHttpProxyConnectionType::Legacy
@ Legacy
Aws::Crt::Optional
Definition: Optional.h:18
Aws::Crt::Http::HttpClientConnectionProxyOptions::ProxyStrategy
std::shared_ptr< HttpProxyStrategy > ProxyStrategy
Definition: HttpConnection.h:318
Aws::Crt::Http::HttpClientConnectionOptions
Definition: HttpConnection.h:347
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:436
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:496
Aws::Crt::Http::HttpClientConnectionProxyOptions::AuthType
AwsHttpProxyAuthenticationType AuthType
Definition: HttpConnection.h:328
Aws::Crt::Http::OnConnectionShutdown
std::function< void(HttpClientConnection &connection, int errorCode)> OnConnectionShutdown
Definition: HttpConnection.h:53
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:84
Aws::Crt::Http::HttpClientConnectionProxyOptions
Definition: HttpConnection.h:270
Aws::Crt::Io::SocketOptions
Definition: SocketOptions.h:48
Aws::Crt::Http::HttpRequestOptions::onStreamComplete
OnStreamComplete onStreamComplete
Definition: HttpConnection.h:120
Aws::Crt::Http::HttpClientConnectionOptions::TlsOptions
Optional< Io::TlsConnectionOptions > TlsOptions
Definition: HttpConnection.h:406
std
Definition: StringView.h:852
AWS_CRT_CPP_API
#define AWS_CRT_CPP_API
Definition: Exports.h:37
Aws
Definition: Allocator.h:11
SocketOptions.h
Bootstrap.h
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:334
Aws::Crt::Allocator
aws_allocator Allocator
Definition: Allocator.h:14
Aws::Crt::Http::HttpStream
Definition: HttpConnection.h:128
Aws::Crt::Io::ClientBootstrap
Definition: Bootstrap.h:35
Aws::Crt::Http::HttpClientConnectionOptions::Bootstrap
Io::ClientBootstrap * Bootstrap
Definition: HttpConnection.h:363
Aws::Crt::Http::HttpClientConnectionProxyOptions::Port
uint32_t Port
Definition: HttpConnection.h:301
Aws::Crt::Http::HttpRequestOptions::onIncomingHeadersBlockDone
OnIncomingHeadersBlockDone onIncomingHeadersBlockDone
Definition: HttpConnection.h:110
Aws::Crt::Http::HttpClientStream
Definition: HttpConnection.h:198
Aws::Crt::Http::HttpClientConnectionOptions::OnConnectionShutdownCallback
OnConnectionShutdown OnConnectionShutdownCallback
Definition: HttpConnection.h:382
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:100
Aws::Crt::Http::HttpClientConnectionOptions::ProxyOptions
Optional< HttpClientConnectionProxyOptions > ProxyOptions
Definition: HttpConnection.h:412