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/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 
42  using OnConnectionSetup =
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;
134  HttpStream &operator=(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();
201  HttpClientStream(const HttpClientStream &) = delete;
202  HttpClientStream(HttpClientStream &&) = delete;
203  HttpClientStream &operator=(const HttpClientStream &) = delete;
204  HttpClientStream &operator=(HttpClientStream &&) = delete;
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 
353  ~HttpClientConnectionOptions() = default;
354 
355  HttpClientConnectionOptions &operator=(const HttpClientConnectionOptions &rhs) = default;
356  HttpClientConnectionOptions &operator=(HttpClientConnectionOptions &&rhs) = default;
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;
439  HttpClientConnection(const HttpClientConnection &) = delete;
441  HttpClientConnection &operator=(const HttpClientConnection &) = delete;
442  HttpClientConnection &operator=(HttpClientConnection &&) = delete;
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
String HostName
Definition: HttpConnection.h:388
String HostName
Definition: HttpConnection.h:295
OnIncomingHeadersBlockDone onIncomingHeadersBlockDone
Definition: HttpConnection.h:110
aws_http_stream * m_stream
Definition: HttpConnection.h:158
#define AWS_CRT_CPP_API
Definition: Exports.h:36
HttpRequest * request
Definition: HttpConnection.h:104
OnIncomingHeaders onIncomingHeaders
Definition: HttpConnection.h:109
std::shared_ptr< HttpStream > stream
Definition: HttpConnection.h:191
Definition: HttpConnection.h:99
OnConnectionShutdown OnConnectionShutdownCallback
Definition: HttpConnection.h:382
AwsHttpProxyConnectionType ProxyConnectionType
Definition: HttpConnection.h:312
Definition: HttpConnection.h:197
String BasicAuthUsername
Definition: HttpConnection.h:334
std::function< void(HttpClientConnection &connection, int errorCode)> OnConnectionShutdown
Definition: HttpConnection.h:53
String BasicAuthPassword
Definition: HttpConnection.h:340
std::basic_string< char, std::char_traits< char >, StlAllocator< char >> String
Definition: Types.h:45
std::shared_ptr< HttpProxyStrategy > ProxyStrategy
Definition: HttpConnection.h:318
bool ManualWindowManagement
Definition: HttpConnection.h:421
aws_allocator Allocator
Definition: Allocator.h:14
uint32_t Port
Definition: HttpConnection.h:394
Definition: HttpRequestResponse.h:109
Definition: HttpConnection.h:187
Definition: HttpConnection.h:269
Allocator * allocator
Definition: HttpConnection.h:190
Definition: HttpConnection.h:435
Optional< HttpClientConnectionProxyOptions > ProxyOptions
Definition: HttpConnection.h:412
AwsHttpProxyAuthenticationType AuthType
Definition: HttpConnection.h:328
Optional< Io::TlsConnectionOptions > TlsOptions
Definition: HttpConnection.h:307
size_t InitialWindowSize
Definition: HttpConnection.h:368
Definition: HttpConnection.h:346
AwsHttpProxyAuthenticationType
Definition: HttpConnection.h:232
std::function< void(HttpStream &stream, const ByteCursor &data)> OnIncomingBody
Definition: HttpConnection.h:84
std::function< void(HttpStream &stream, int errorCode)> OnStreamComplete
Definition: HttpConnection.h:94
ClientStreamCallbackData()
Definition: HttpConnection.h:189
Definition: Bootstrap.h:34
Io::ClientBootstrap * Bootstrap
Definition: HttpConnection.h:363
Definition: HttpConnection.h:127
std::function< void(const std::shared_ptr< HttpClientConnection > &connection, int errorCode)> OnConnectionSetup
Definition: HttpConnection.h:43
aws_http_header HttpHeader
Definition: HttpConnection.h:34
Io::SocketOptions SocketOptions
Definition: HttpConnection.h:400
std::function< void(HttpStream &stream, enum aws_http_header_block headerBlock, const HttpHeader *headersArray, std::size_t headersCount)> OnIncomingHeaders
Definition: HttpConnection.h:67
std::shared_ptr< HttpClientConnection > m_connection
Definition: HttpConnection.h:159
std::function< void(HttpStream &stream, enum aws_http_header_block block)> OnIncomingHeadersBlockDone
Definition: HttpConnection.h:76
OnStreamComplete onStreamComplete
Definition: HttpConnection.h:120
aws_http_connection * m_connection
Definition: HttpConnection.h:496
HttpVersion
Definition: HttpConnection.h:424
int LastError() const noexcept
Definition: HttpConnection.h:480
OnIncomingBody onIncomingBody
Definition: HttpConnection.h:115
AwsHttpProxyConnectionType
Definition: HttpConnection.h:242
uint32_t Port
Definition: HttpConnection.h:301
OnConnectionSetup OnConnectionSetupCallback
Definition: HttpConnection.h:375
Definition: SocketOptions.h:47
Definition: Allocator.h:10
Optional< Io::TlsConnectionOptions > TlsOptions
Definition: HttpConnection.h:406