azure-core
win_http_transport.hpp
Go to the documentation of this file.
1 // Copyright (c) Microsoft Corporation. All rights reserved.
2 // SPDX-License-Identifier: MIT
3 
9 #pragma once
10 
11 #include "azure/core/context.hpp"
12 #include "azure/core/http/http.hpp"
15 #include "azure/core/internal/unique_handle.hpp"
16 #include "azure/core/platform.hpp"
17 
18 #if defined(AZ_PLATFORM_WINDOWS)
19 #if !defined(WIN32_LEAN_AND_MEAN)
20 #define WIN32_LEAN_AND_MEAN
21 #endif
22 #if !defined(NOMINMAX)
23 #define NOMINMAX
24 #endif
25 #include <windows.h>
26 #endif
27 
28 #include <memory>
29 #include <type_traits>
30 #include <vector>
31 #include <wincrypt.h>
32 #include <winhttp.h>
33 
34 namespace Azure { namespace Core {
35  namespace _internal {
43  template <> struct UniqueHandleHelper<HINTERNET>
44  {
45  static void FreeHandle(HINTERNET obj) { WinHttpCloseHandle(obj); }
46 
47  using type = BasicUniqueHandle<void, FreeHandle>;
48  };
49  } // namespace _internal
50 
51  namespace Http {
52 
53  namespace _detail {
54 
55  constexpr static size_t DefaultUploadChunkSize = 1024 * 64;
56  constexpr static size_t MaximumUploadChunkSize = 1024 * 1024;
57 
58  // Forward declaration for WinHttpRequest.
59  class WinHttpRequest;
60  } // namespace _detail
61 
67  {
72 
87 
92 
102  std::string ProxyInformation;
103 
108 
113 
119  std::vector<std::string> ExpectedTlsRootCertificates;
120  };
121 
127  private:
128  WinHttpTransportOptions m_options;
129  // m_sessionhandle is const to ensure immutability.
130  const Azure::Core::_internal::UniqueHandle<HINTERNET> m_sessionHandle;
131 
132  Azure::Core::_internal::UniqueHandle<HINTERNET> CreateSessionHandle();
133  Azure::Core::_internal::UniqueHandle<HINTERNET> CreateConnectionHandle(
134  Azure::Core::Url const& url,
135  Azure::Core::Context const& context);
136  std::unique_ptr<_detail::WinHttpRequest> CreateRequestHandle(
137  Azure::Core::_internal::UniqueHandle<HINTERNET> const& connectionHandle,
138  Azure::Core::Url const& url,
139  Azure::Core::Http::HttpMethod const& method);
140 
141  // Callback to allow a derived transport to extract the request handle. Used for WebSocket
142  // transports.
143  virtual void OnUpgradedConnection(std::unique_ptr<_detail::WinHttpRequest> const&){};
144 
151  void GetErrorAndThrow(const std::string& exceptionMessage, DWORD error = GetLastError());
152 
153  public:
160 
172 
181  virtual std::unique_ptr<RawResponse> Send(Request& request, Context const& context) override;
182 
183  // See also:
184  // [Core Guidelines C.35: "A base class destructor should be either public
185  // and virtual or protected and
186  // non-virtual"](http://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#c35-a-base-class-destructor-should-be-either-public-and-virtual-or-protected-and-non-virtual)
187  virtual ~WinHttpTransport();
188  };
189 
190  } // namespace Http
191 }} // namespace Azure::Core
A context is a node within a tree that represents deadlines and key/value pairs.
Definition: context.hpp:45
The method to be performed on the resource identified by the Request.
Definition: http.hpp:94
Base class for all HTTP transport implementations.
Definition: transport.hpp:19
A request message from a client to a server.
Definition: http.hpp:176
Concrete implementation of an HTTP transport that uses WinHTTP when sending and receiving requests an...
Definition: win_http_transport.hpp:126
virtual std::unique_ptr< RawResponse > Send(Request &request, Context const &context) override
Implements the HTTP transport interface to send an HTTP Request and produce an HTTP RawResponse.
Definition: win_http_transport.cpp:1301
WinHttpTransport(WinHttpTransportOptions const &options=WinHttpTransportOptions())
Constructs WinHttpTransport.
Definition: win_http_transport.cpp:772
Represents the location where a request will be performed.
Definition: url.hpp:46
Context for canceling long running operations.
HTTP request and response functionality.
Azure SDK abstractions.
Definition: azure_assert.hpp:55
Platform-specific macros.
HTTP transport policies, and their options.
HTTP transport options parameterize the HTTP transport adapter being used.
Definition: policy.hpp:138
Sets the WinHTTP session and connection options used to customize the behavior of the transport.
Definition: win_http_transport.hpp:67
bool EnableCertificateRevocationListCheck
If True, enables checks for certificate revocation.
Definition: win_http_transport.hpp:91
std::vector< std::string > ExpectedTlsRootCertificates
Array of Base64 encoded DER encoded X.509 certificate. These certificates should form a chain of cert...
Definition: win_http_transport.hpp:119
bool IgnoreUnknownCertificateAuthority
When true, allows an invalid certificate authority.
Definition: win_http_transport.hpp:71
bool EnableSystemDefaultProxy
If True, enables the use of the system default proxy.
Definition: win_http_transport.hpp:86
Azure::Nullable< std::string > ProxyUserName
User name for proxy authentication.
Definition: win_http_transport.hpp:107
std::string ProxyInformation
Proxy information.
Definition: win_http_transport.hpp:102
Azure::Nullable< std::string > ProxyPassword
Password for proxy authentication.
Definition: win_http_transport.hpp:112
Utilities to be used by HTTP transport implementations.