14#include "azure/core/dll_import_export.hpp"
17#include "azure/core/internal/http/http_sanitizer.hpp"
28#include <shared_mutex>
42namespace Azure {
namespace Core {
namespace Http {
namespace Policies {
44 struct TransportOptions;
46 std::shared_ptr<HttpTransport> GetTransportAdapter(TransportOptions
const& transportOptions);
48 AZ_CORE_DLLEXPORT
extern std::set<std::string>
const g_defaultAllowedHttpQueryParameters;
49 AZ_CORE_DLLEXPORT
extern CaseInsensitiveSet
const g_defaultAllowedHttpHeaders;
53 class TelemetryPolicy;
83 friend class _internal::TelemetryPolicy;
84 long CppStandardVersion =
85#if defined(_azure_BUILDING_SDK)
87#elif defined(_azure_BUILDING_TESTS)
89#elif defined(_azure_BUILDING_SAMPLES)
116 std::chrono::milliseconds
RetryDelay = std::chrono::milliseconds(800);
130 HttpStatusCode::RequestTimeout,
131 HttpStatusCode::InternalServerError,
132 HttpStatusCode::BadGateway,
133 HttpStatusCode::ServiceUnavailable,
134 HttpStatusCode::GatewayTimeout,
271 virtual std::unique_ptr<RawResponse>
Send(
274 Context const& context)
const = 0;
286 virtual std::unique_ptr<HttpPolicy>
Clone()
const = 0;
328 const size_t m_index;
329 const std::vector<std::unique_ptr<HttpPolicy>>& m_policies;
340 explicit NextHttpPolicy(
size_t index,
const std::vector<std::unique_ptr<HttpPolicy>>& policies)
341 : m_index(index), m_policies(policies)
357 namespace _internal {
363 class TransportPolicy final :
public HttpPolicy {
365 TransportOptions m_options;
373 explicit TransportPolicy(TransportOptions
const& options = TransportOptions());
375 std::unique_ptr<HttpPolicy> Clone()
const override
377 return std::make_unique<TransportPolicy>(*
this);
380 std::unique_ptr<RawResponse> Send(
382 NextHttpPolicy nextPolicy,
383 Context const& context)
const override;
390#if !defined(_azure_TESTING_BUILD)
393 :
public HttpPolicy {
395 RetryOptions m_retryOptions;
403 explicit RetryPolicy(RetryOptions options) : m_retryOptions(std::move(options)) {}
405 std::unique_ptr<HttpPolicy> Clone()
const override
407 return std::make_unique<RetryPolicy>(*
this);
410 std::unique_ptr<RawResponse>
Send(
412 NextHttpPolicy nextPolicy,
413 Context
const& context)
const final;
426 static int32_t GetRetryCount(Context
const& context);
429 virtual bool ShouldRetryOnTransportFailure(
430 RetryOptions
const& retryOptions,
432 std::chrono::milliseconds& retryAfter,
433 double jitterFactor = -1)
const;
435 virtual bool ShouldRetryOnResponse(
436 RawResponse
const& response,
437 RetryOptions
const& retryOptions,
439 std::chrono::milliseconds& retryAfter,
440 double jitterFactor = -1)
const;
449 class RequestIdPolicy final :
public HttpPolicy {
451 constexpr static const char* RequestIdHeader =
"x-ms-client-request-id";
458 explicit RequestIdPolicy() {}
460 std::unique_ptr<HttpPolicy>
Clone()
const override
462 return std::make_unique<RequestIdPolicy>(*
this);
465 std::unique_ptr<RawResponse>
Send(
467 NextHttpPolicy nextPolicy,
468 Context
const& context)
const override
470 if (!request.GetHeader(RequestIdHeader).HasValue())
473 request.SetHeader(RequestIdHeader, uuid);
476 return nextPolicy.Send(request, context);
488 class RequestActivityPolicy final :
public HttpPolicy {
490 Azure::Core::Http::_internal::HttpSanitizer m_httpSanitizer;
502 explicit RequestActivityPolicy(
503 Azure::Core::Http::_internal::HttpSanitizer
const& httpSanitizer)
504 : m_httpSanitizer(httpSanitizer)
508 std::unique_ptr<HttpPolicy>
Clone()
const override
510 return std::make_unique<RequestActivityPolicy>(*
this);
513 std::unique_ptr<RawResponse>
Send(
515 NextHttpPolicy nextPolicy,
516 Context
const& context)
const override;
532 class TelemetryPolicy final :
public HttpPolicy {
534 std::string
const m_telemetryId;
544 explicit TelemetryPolicy(
545 std::string
const& packageName,
546 std::string
const& packageVersion,
547 TelemetryOptions options = TelemetryOptions())
548 : m_telemetryId(
Azure::Core::Http::_detail::UserAgentGenerator::GenerateUserAgent(
551 options.ApplicationId,
552 options.CppStandardVersion))
556 std::unique_ptr<HttpPolicy>
Clone()
const override
558 return std::make_unique<TelemetryPolicy>(*
this);
561 std::unique_ptr<RawResponse>
Send(
563 NextHttpPolicy nextPolicy,
564 Context
const& context)
const override;
571 class BearerTokenAuthenticationPolicy :
public HttpPolicy {
573 std::shared_ptr<Credentials::TokenCredential const>
const m_credential;
574 Credentials::TokenRequestContext m_tokenRequestContext;
576 mutable Credentials::AccessToken m_accessToken;
577 mutable std::shared_timed_mutex m_accessTokenMutex;
578 mutable Credentials::TokenRequestContext m_accessTokenContext;
587 explicit BearerTokenAuthenticationPolicy(
588 std::shared_ptr<Credentials::TokenCredential const> credential,
589 Credentials::TokenRequestContext tokenRequestContext)
590 : m_credential(std::move(credential)),
591 m_tokenRequestContext(std::move(tokenRequestContext))
595 std::unique_ptr<HttpPolicy>
Clone()
const override
598 return std::unique_ptr<HttpPolicy>(
new BearerTokenAuthenticationPolicy(*
this));
601 std::unique_ptr<RawResponse>
Send(
603 NextHttpPolicy nextPolicy,
604 Context
const& context)
const override;
607 BearerTokenAuthenticationPolicy(BearerTokenAuthenticationPolicy
const& other)
608 : BearerTokenAuthenticationPolicy(other.m_credential, other.m_tokenRequestContext)
610 std::shared_lock<std::shared_timed_mutex> readLock(other.m_accessTokenMutex);
611 m_accessToken = other.m_accessToken;
612 m_accessTokenContext = other.m_accessTokenContext;
615 void operator=(BearerTokenAuthenticationPolicy
const&) =
delete;
617 virtual std::unique_ptr<RawResponse> AuthorizeAndSendRequest(
619 NextHttpPolicy& nextPolicy,
620 Context
const& context)
const;
622 virtual bool AuthorizeRequestOnChallenge(
623 std::string
const& challenge,
625 Context
const& context)
const;
627 void AuthenticateAndAuthorizeRequest(
629 Credentials::TokenRequestContext
const& tokenRequestContext,
630 Context
const& context)
const;
639 class LogPolicy final :
public HttpPolicy {
640 LogOptions m_options;
641 Azure::Core::Http::_internal::HttpSanitizer m_httpSanitizer;
648 explicit LogPolicy(LogOptions options)
649 : m_options(std::move(options)),
650 m_httpSanitizer(m_options.AllowedHttpQueryParameters, m_options.AllowedHttpHeaders)
654 std::unique_ptr<HttpPolicy>
Clone()
const override
656 return std::make_unique<LogPolicy>(*
this);
659 std::unique_ptr<RawResponse>
Send(
661 NextHttpPolicy nextPolicy,
662 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:255
virtual ~HttpPolicy()
Destructs HttpPolicy.
Definition policy.hpp:280
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:327
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:340
std::unique_ptr< RawResponse > Send(Request &request, Context const &context)
Applies this HTTP policy.
Definition policy.cpp:14
A request message from a client to a server.
Definition http.hpp:182
std::string ToString()
Gets Uuid as a string.
Definition uuid.cpp:20
static Uuid CreateUuid()
Creates a new random UUID.
Definition uuid.cpp:50
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:144
std::set< std::string > AllowedHttpQueryParameters
HTTP query parameter names that are allowed to be logged.
Definition policy.hpp:149
CaseInsensitiveSet AllowedHttpHeaders
HTTP header names that are allowed to be logged.
Definition policy.hpp:155
The set of options that can be specified to influence how retry attempts are made,...
Definition policy.hpp:104
std::chrono::milliseconds RetryDelay
The minimum permissible delay between retry attempts.
Definition policy.hpp:116
int32_t MaxRetries
The maximum number of retry attempts before giving up.
Definition policy.hpp:109
std::chrono::milliseconds MaxRetryDelay
The maximum permissible delay between retry attempts.
Definition policy.hpp:123
std::set< HttpStatusCode > StatusCodes
The HTTP status codes that indicate when an operation should be retried.
Definition policy.hpp:129
Telemetry options, used to configure telemetry parameters.
Definition policy.hpp:61
std::string ApplicationId
The Application ID is the last part of the user agent for telemetry.
Definition policy.hpp:69
std::shared_ptr< Azure::Core::Tracing::TracerProvider > TracingProvider
Specifies the default distributed tracing provider to use for this client. By default,...
Definition policy.hpp:75
HTTP transport options parameterize the HTTP transport adapter being used.
Definition policy.hpp:162
bool DisableTlsCertificateValidation
Disable SSL/TLS certificate verification. This option allows transport layer to perform insecure SSL/...
Definition policy.hpp:211
std::shared_ptr< HttpTransport > Transport
Azure::Core::Http::HttpTransport that the transport policy will use to send and receive requests and ...
Definition policy.hpp:244
Azure::Nullable< std::string > ProxyPassword
The password to use when authenticating with the proxy server.
Definition policy.hpp:188
bool EnableCertificateRevocationListCheck
Enable TLS Certificate validation against a certificate revocation list.
Definition policy.hpp:198
std::string ExpectedTlsRootCertificate
Base64 encoded DER representation of an X.509 certificate expected in the certificate chain used in T...
Definition policy.hpp:224
Azure::Nullable< std::string > HttpProxy
The URL for the proxy server to use for this connection.
Definition policy.hpp:172
Azure::Nullable< std::string > ProxyUserName
The username to use when authenticating with the proxy server.
Definition policy.hpp:180
Utilities to be used by HTTP transport implementations.
Declaration of the UserAgentGenerator type.
Universally unique identifier.