14#include "azure/core/dll_import_export.hpp"
17#include "azure/core/internal/http/http_sanitizer.hpp"
27#include <shared_mutex>
41namespace Azure {
namespace Core {
namespace Http {
namespace Policies {
43 struct TransportOptions;
45 std::shared_ptr<HttpTransport> GetTransportAdapter(TransportOptions
const& transportOptions);
47 AZ_CORE_DLLEXPORT
extern std::set<std::string>
const g_defaultAllowedHttpQueryParameters;
48 AZ_CORE_DLLEXPORT
extern CaseInsensitiveSet
const g_defaultAllowedHttpHeaders;
52 class TelemetryPolicy;
82 friend class _internal::TelemetryPolicy;
83 long CppStandardVersion =
84#if defined(_azure_BUILDING_SDK)
86#elif defined(_azure_BUILDING_TESTS)
88#elif defined(_azure_BUILDING_SAMPLES)
92#if defined(_MSVC_LANG) && __cplusplus == 199711L
120 std::chrono::milliseconds
RetryDelay = std::chrono::milliseconds(800);
134 HttpStatusCode::RequestTimeout,
135 HttpStatusCode::InternalServerError,
136 HttpStatusCode::BadGateway,
137 HttpStatusCode::ServiceUnavailable,
138 HttpStatusCode::GatewayTimeout,
275 virtual std::unique_ptr<RawResponse>
Send(
278 Context const& context)
const = 0;
290 virtual std::unique_ptr<HttpPolicy>
Clone()
const = 0;
332 const size_t m_index;
333 const std::vector<std::unique_ptr<HttpPolicy>>& m_policies;
344 explicit NextHttpPolicy(
size_t index,
const std::vector<std::unique_ptr<HttpPolicy>>& policies)
345 : m_index(index), m_policies(policies)
361 namespace _internal {
367 class TransportPolicy final :
public HttpPolicy {
379 std::unique_ptr<HttpPolicy> Clone()
const override
381 return std::make_unique<TransportPolicy>(*
this);
384 std::unique_ptr<RawResponse> Send(
386 NextHttpPolicy nextPolicy,
387 Context const& context)
const override;
399 class RetryPolicyBase :
public HttpPolicy {
401 RetryOptions m_retryOptions;
410 explicit RetryPolicyBase(RetryOptions options) : m_retryOptions(std::move(options)) {}
412 std::unique_ptr<RawResponse> Send(
414 NextHttpPolicy nextPolicy,
415 Context
const& context)
const final;
428 static int32_t GetRetryCount(Context
const& context);
431 virtual bool ShouldRetryOnTransportFailure(
432 RetryOptions
const& retryOptions,
434 std::chrono::milliseconds& retryAfter,
435 double jitterFactor = -1)
const;
437 virtual bool ShouldRetryOnResponse(
438 RawResponse
const& response,
439 RetryOptions
const& retryOptions,
441 std::chrono::milliseconds& retryAfter,
442 double jitterFactor = -1)
const;
448 class RetryPolicy final :
public RetryPolicyBase {
455 explicit RetryPolicy(RetryOptions options) : RetryPolicyBase(std::move(options)) {}
457 std::unique_ptr<HttpPolicy>
Clone()
const override
459 return std::make_unique<RetryPolicy>(*
this);
469 class RequestIdPolicy final :
public HttpPolicy {
471 constexpr static const char* RequestIdHeader =
"x-ms-client-request-id";
478 explicit RequestIdPolicy() {}
480 std::unique_ptr<HttpPolicy>
Clone()
const override
482 return std::make_unique<RequestIdPolicy>(*
this);
485 std::unique_ptr<RawResponse>
Send(
487 NextHttpPolicy nextPolicy,
488 Context
const& context)
const override
490 if (!request.GetHeader(RequestIdHeader).HasValue())
493 request.SetHeader(RequestIdHeader, uuid);
496 return nextPolicy.Send(request, context);
508 class RequestActivityPolicy final :
public HttpPolicy {
510 Azure::Core::Http::_internal::HttpSanitizer m_httpSanitizer;
522 explicit RequestActivityPolicy(
523 Azure::Core::Http::_internal::HttpSanitizer
const& httpSanitizer)
524 : m_httpSanitizer(httpSanitizer)
528 std::unique_ptr<HttpPolicy>
Clone()
const override
530 return std::make_unique<RequestActivityPolicy>(*
this);
533 std::unique_ptr<RawResponse>
Send(
535 NextHttpPolicy nextPolicy,
536 Context
const& context)
const override;
552 class TelemetryPolicy final :
public HttpPolicy {
554 std::string
const m_telemetryId;
564 explicit TelemetryPolicy(
565 std::string
const& packageName,
566 std::string
const& packageVersion,
567 TelemetryOptions options = TelemetryOptions())
568 : m_telemetryId(
Azure::Core::Http::_internal::HttpShared::GenerateUserAgent(
571 options.ApplicationId,
572 options.CppStandardVersion))
576 std::unique_ptr<HttpPolicy>
Clone()
const override
578 return std::make_unique<TelemetryPolicy>(*
this);
581 std::unique_ptr<RawResponse>
Send(
583 NextHttpPolicy nextPolicy,
584 Context
const& context)
const override;
591 class BearerTokenAuthenticationPolicy :
public HttpPolicy {
593 std::shared_ptr<const Credentials::TokenCredential> m_credential;
594 Credentials::TokenRequestContext m_tokenRequestContext;
596 mutable Credentials::AccessToken m_accessToken;
597 mutable std::shared_timed_mutex m_accessTokenMutex;
598 mutable Credentials::TokenRequestContext m_accessTokenContext;
599 mutable std::atomic<bool> m_invalidateToken = {
false};
608 explicit BearerTokenAuthenticationPolicy(
609 std::shared_ptr<const Credentials::TokenCredential> credential,
610 Credentials::TokenRequestContext tokenRequestContext)
611 : m_credential(std::move(credential)),
612 m_tokenRequestContext(std::move(tokenRequestContext))
616 std::unique_ptr<HttpPolicy>
Clone()
const override
619 return std::unique_ptr<HttpPolicy>(
new BearerTokenAuthenticationPolicy(*
this));
622 std::unique_ptr<RawResponse>
Send(
624 NextHttpPolicy nextPolicy,
625 Context
const& context)
const override;
628 BearerTokenAuthenticationPolicy(BearerTokenAuthenticationPolicy
const& other)
629 : BearerTokenAuthenticationPolicy(other.m_credential, other.m_tokenRequestContext)
631 std::shared_lock<std::shared_timed_mutex> readLock(other.m_accessTokenMutex);
632 m_accessToken = other.m_accessToken;
633 m_accessTokenContext = other.m_accessTokenContext;
634 m_invalidateToken.store(other.m_invalidateToken.load());
637 void operator=(BearerTokenAuthenticationPolicy
const&) =
delete;
639 virtual std::unique_ptr<RawResponse> AuthorizeAndSendRequest(
641 NextHttpPolicy& nextPolicy,
642 Context
const& context)
const;
644 virtual bool AuthorizeRequestOnChallenge(
645 std::string
const& challenge,
647 Context
const& context)
const;
649 void AuthenticateAndAuthorizeRequest(
651 Credentials::TokenRequestContext
const& tokenRequestContext,
652 Context
const& context)
const;
661 class LogPolicy final :
public HttpPolicy {
662 LogOptions m_options;
663 Azure::Core::Http::_internal::HttpSanitizer m_httpSanitizer;
670 explicit LogPolicy(LogOptions options)
671 : m_options(std::move(options)),
672 m_httpSanitizer(m_options.AllowedHttpQueryParameters, m_options.AllowedHttpHeaders)
676 std::unique_ptr<HttpPolicy>
Clone()
const override
678 return std::make_unique<LogPolicy>(*
this);
681 std::unique_ptr<RawResponse>
Send(
683 NextHttpPolicy nextPolicy,
684 Context
const& context)
const override;
A map<string, string> with case-insensitive key comparison.
std::set< std::string, _internal::StringExtensions::CaseInsensitiveComparator > CaseInsensitiveSet
A type alias of std::set<std::string> with case-insensitive element comparison.
Definition case_insensitive_containers.hpp:31
A context is a node within a unidirectional tree that represents deadlines and key/value pairs.
Definition context.hpp:72
HTTP policy base class.
Definition policy.hpp:259
virtual ~HttpPolicy()
Destructs HttpPolicy.
Definition policy.hpp:284
HttpPolicy()=default
Constructs a default instance of HttpPolicy.
HttpPolicy & operator=(const HttpPolicy &other)=default
Assigns this HttpPolicy to copy the other.
virtual std::unique_ptr< HttpPolicy > Clone() const =0
Creates a clone of this HttpPolicy.
HttpPolicy(HttpPolicy &&other)=default
Constructs HttpPolicy by moving other HttpPolicy.
HttpPolicy(const HttpPolicy &other)=default
Constructs a copy of other HttpPolicy.
virtual std::unique_ptr< RawResponse > Send(Request &request, NextHttpPolicy nextPolicy, Context const &context) const =0
Applies this HTTP policy.
The next HTTP policy in the stack sequence of policies.
Definition policy.hpp:331
std::unique_ptr< RawResponse > Send(Request &request, Context const &context)
Applies this HTTP policy.
NextHttpPolicy(size_t index, const std::vector< std::unique_ptr< HttpPolicy > > &policies)
Constructs an abstraction representing a next line in the stack sequence of policies,...
Definition policy.hpp:344
A request message from a client to a server.
Definition http.hpp:183
std::string ToString() const
Gets Uuid as a string.
static Uuid CreateUuid()
Creates a new random UUID.
Manages an optional contained value, i.e. a value that may or may not be present.
Definition nullable.hpp:30
Context for canceling long running operations.
Credentials used for authentication with many (not all) Azure SDK client libraries.
HTTP request and response functionality.
Compute the hash value for the input binary data, using SHA256, SHA384 and SHA512.
Definition azure_assert.hpp:57
std::shared_ptr< Azure::Core::Http::HttpTransport > AzureSdkGetCustomHttpTransport()
Log options that parameterize the information being logged.
Definition policy.hpp:148
std::set< std::string > AllowedHttpQueryParameters
HTTP query parameter names that are allowed to be logged.
Definition policy.hpp:153
CaseInsensitiveSet AllowedHttpHeaders
HTTP header names that are allowed to be logged.
Definition policy.hpp:159
The set of options that can be specified to influence how retry attempts are made,...
Definition policy.hpp:108
std::chrono::milliseconds RetryDelay
The minimum permissible delay between retry attempts.
Definition policy.hpp:120
int32_t MaxRetries
The maximum number of retry attempts before giving up.
Definition policy.hpp:113
std::chrono::milliseconds MaxRetryDelay
The maximum permissible delay between retry attempts.
Definition policy.hpp:127
std::set< HttpStatusCode > StatusCodes
The HTTP status codes that indicate when an operation should be retried.
Definition policy.hpp:133
Telemetry options, used to configure telemetry parameters.
Definition policy.hpp:60
std::string ApplicationId
The Application ID is the last part of the user agent for telemetry.
Definition policy.hpp:68
std::shared_ptr< Azure::Core::Tracing::TracerProvider > TracingProvider
Specifies the default distributed tracing provider to use for this client. By default,...
Definition policy.hpp:74
HTTP transport options parameterize the HTTP transport adapter being used.
Definition policy.hpp:166
bool DisableTlsCertificateValidation
Disable SSL/TLS certificate verification. This option allows transport layer to perform insecure SSL/...
Definition policy.hpp:215
std::shared_ptr< HttpTransport > Transport
Azure::Core::Http::HttpTransport that the transport policy will use to send and receive requests and ...
Definition policy.hpp:248
Azure::Nullable< std::string > ProxyPassword
The password to use when authenticating with the proxy server.
Definition policy.hpp:192
bool EnableCertificateRevocationListCheck
Enable TLS Certificate validation against a certificate revocation list.
Definition policy.hpp:202
std::string ExpectedTlsRootCertificate
Base64 encoded DER representation of an X.509 certificate expected in the certificate chain used in T...
Definition policy.hpp:228
Azure::Nullable< std::string > HttpProxy
The URL for the proxy server to use for this connection.
Definition policy.hpp:176
Azure::Nullable< std::string > ProxyUserName
The username to use when authenticating with the proxy server.
Definition policy.hpp:184
Utilities to be used by HTTP transport implementations.
Universally unique identifier.