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;
93 std::chrono::milliseconds
RetryDelay = std::chrono::milliseconds(800);
107 HttpStatusCode::RequestTimeout,
108 HttpStatusCode::InternalServerError,
109 HttpStatusCode::BadGateway,
110 HttpStatusCode::ServiceUnavailable,
111 HttpStatusCode::GatewayTimeout,
248 virtual std::unique_ptr<RawResponse>
Send(
251 Context const& context)
const = 0;
263 virtual std::unique_ptr<HttpPolicy>
Clone()
const = 0;
305 const size_t m_index;
306 const std::vector<std::unique_ptr<HttpPolicy>>& m_policies;
317 explicit NextHttpPolicy(
size_t index,
const std::vector<std::unique_ptr<HttpPolicy>>& policies)
318 : m_index(index), m_policies(policies)
334 namespace _internal {
340 class TransportPolicy final :
public HttpPolicy {
342 TransportOptions m_options;
350 explicit TransportPolicy(TransportOptions
const& options = TransportOptions());
352 std::unique_ptr<HttpPolicy> Clone()
const override
354 return std::make_unique<TransportPolicy>(*
this);
357 std::unique_ptr<RawResponse> Send(
359 NextHttpPolicy nextPolicy,
360 Context const& context)
const override;
367#if !defined(_azure_TESTING_BUILD)
370 :
public HttpPolicy {
372 RetryOptions m_retryOptions;
380 explicit RetryPolicy(RetryOptions options) : m_retryOptions(std::move(options)) {}
382 std::unique_ptr<HttpPolicy> Clone()
const override
384 return std::make_unique<RetryPolicy>(*
this);
387 std::unique_ptr<RawResponse>
Send(
389 NextHttpPolicy nextPolicy,
390 Context
const& context)
const final;
403 static int32_t GetRetryCount(Context
const& context);
406 virtual bool ShouldRetryOnTransportFailure(
407 RetryOptions
const& retryOptions,
409 std::chrono::milliseconds& retryAfter,
410 double jitterFactor = -1)
const;
412 virtual bool ShouldRetryOnResponse(
413 RawResponse
const& response,
414 RetryOptions
const& retryOptions,
416 std::chrono::milliseconds& retryAfter,
417 double jitterFactor = -1)
const;
426 class RequestIdPolicy final :
public HttpPolicy {
428 constexpr static const char* RequestIdHeader =
"x-ms-client-request-id";
435 explicit RequestIdPolicy() {}
437 std::unique_ptr<HttpPolicy>
Clone()
const override
439 return std::make_unique<RequestIdPolicy>(*
this);
442 std::unique_ptr<RawResponse>
Send(
444 NextHttpPolicy nextPolicy,
445 Context
const& context)
const override
447 if (!request.GetHeader(RequestIdHeader).HasValue())
450 request.SetHeader(RequestIdHeader, uuid);
453 return nextPolicy.Send(request, context);
465 class RequestActivityPolicy final :
public HttpPolicy {
467 Azure::Core::Http::_internal::HttpSanitizer m_httpSanitizer;
479 explicit RequestActivityPolicy(
480 Azure::Core::Http::_internal::HttpSanitizer
const& httpSanitizer)
481 : m_httpSanitizer(httpSanitizer)
485 std::unique_ptr<HttpPolicy>
Clone()
const override
487 return std::make_unique<RequestActivityPolicy>(*
this);
490 std::unique_ptr<RawResponse>
Send(
492 NextHttpPolicy nextPolicy,
493 Context
const& context)
const override;
509 class TelemetryPolicy final :
public HttpPolicy {
511 std::string
const m_telemetryId;
521 explicit TelemetryPolicy(
522 std::string
const& packageName,
523 std::string
const& packageVersion,
524 TelemetryOptions options = TelemetryOptions())
525 : m_telemetryId(
Azure::Core::Http::_detail::UserAgentGenerator::GenerateUserAgent(
528 options.ApplicationId))
532 std::unique_ptr<HttpPolicy>
Clone()
const override
534 return std::make_unique<TelemetryPolicy>(*
this);
537 std::unique_ptr<RawResponse>
Send(
539 NextHttpPolicy nextPolicy,
540 Context
const& context)
const override;
547 class BearerTokenAuthenticationPolicy :
public HttpPolicy {
549 std::shared_ptr<Credentials::TokenCredential const>
const m_credential;
550 Credentials::TokenRequestContext m_tokenRequestContext;
552 mutable Credentials::AccessToken m_accessToken;
553 mutable std::shared_timed_mutex m_accessTokenMutex;
554 mutable Credentials::TokenRequestContext m_accessTokenContext;
563 explicit BearerTokenAuthenticationPolicy(
564 std::shared_ptr<Credentials::TokenCredential const> credential,
565 Credentials::TokenRequestContext tokenRequestContext)
566 : m_credential(std::move(credential)),
567 m_tokenRequestContext(std::move(tokenRequestContext))
571 std::unique_ptr<HttpPolicy>
Clone()
const override
574 return std::unique_ptr<HttpPolicy>(
new BearerTokenAuthenticationPolicy(*
this));
577 std::unique_ptr<RawResponse>
Send(
579 NextHttpPolicy nextPolicy,
580 Context
const& context)
const override;
583 BearerTokenAuthenticationPolicy(BearerTokenAuthenticationPolicy
const& other)
584 : BearerTokenAuthenticationPolicy(other.m_credential, other.m_tokenRequestContext)
586 std::shared_lock<std::shared_timed_mutex> readLock(other.m_accessTokenMutex);
587 m_accessToken = other.m_accessToken;
588 m_accessTokenContext = other.m_accessTokenContext;
591 void operator=(BearerTokenAuthenticationPolicy
const&) =
delete;
593 virtual std::unique_ptr<RawResponse> AuthorizeAndSendRequest(
595 NextHttpPolicy& nextPolicy,
596 Context
const& context)
const;
598 virtual bool AuthorizeRequestOnChallenge(
599 std::string
const& challenge,
601 Context
const& context)
const;
603 void AuthenticateAndAuthorizeRequest(
605 Credentials::TokenRequestContext
const& tokenRequestContext,
606 Context
const& context)
const;
615 class LogPolicy final :
public HttpPolicy {
616 LogOptions m_options;
617 Azure::Core::Http::_internal::HttpSanitizer m_httpSanitizer;
624 explicit LogPolicy(LogOptions options)
625 : m_options(std::move(options)),
626 m_httpSanitizer(m_options.AllowedHttpQueryParameters, m_options.AllowedHttpHeaders)
630 std::unique_ptr<HttpPolicy>
Clone()
const override
632 return std::make_unique<LogPolicy>(*
this);
635 std::unique_ptr<RawResponse>
Send(
637 NextHttpPolicy nextPolicy,
638 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 tree that represents deadlines and key/value pairs.
Definition context.hpp:46
HTTP policy base class.
Definition policy.hpp:232
virtual ~HttpPolicy()
Destructs HttpPolicy.
Definition policy.hpp:257
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:304
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:317
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:121
std::set< std::string > AllowedHttpQueryParameters
HTTP query parameter names that are allowed to be logged.
Definition policy.hpp:126
CaseInsensitiveSet AllowedHttpHeaders
HTTP header names that are allowed to be logged.
Definition policy.hpp:132
The set of options that can be specified to influence how retry attempts are made,...
Definition policy.hpp:81
std::chrono::milliseconds RetryDelay
The minimum permissible delay between retry attempts.
Definition policy.hpp:93
int32_t MaxRetries
The maximum number of retry attempts before giving up.
Definition policy.hpp:86
std::chrono::milliseconds MaxRetryDelay
The maximum permissible delay between retry attempts.
Definition policy.hpp:100
std::set< HttpStatusCode > StatusCodes
The HTTP status codes that indicate when an operation should be retried.
Definition policy.hpp:106
Telemetry options, used to configure telemetry parameters.
Definition policy.hpp:57
std::string ApplicationId
The Application ID is the last part of the user agent for telemetry.
Definition policy.hpp:65
std::shared_ptr< Azure::Core::Tracing::TracerProvider > TracingProvider
Specifies the default distributed tracing provider to use for this client. By default,...
Definition policy.hpp:71
HTTP transport options parameterize the HTTP transport adapter being used.
Definition policy.hpp:139
bool DisableTlsCertificateValidation
Disable SSL/TLS certificate verification. This option allows transport layer to perform insecure SSL/...
Definition policy.hpp:188
std::shared_ptr< HttpTransport > Transport
Azure::Core::Http::HttpTransport that the transport policy will use to send and receive requests and ...
Definition policy.hpp:221
Azure::Nullable< std::string > ProxyPassword
The password to use when authenticating with the proxy server.
Definition policy.hpp:165
bool EnableCertificateRevocationListCheck
Enable TLS Certificate validation against a certificate revocation list.
Definition policy.hpp:175
std::string ExpectedTlsRootCertificate
Base64 encoded DER representation of an X.509 certificate expected in the certificate chain used in T...
Definition policy.hpp:201
Azure::Nullable< std::string > HttpProxy
The URL for the proxy server to use for this connection.
Definition policy.hpp:149
Azure::Nullable< std::string > ProxyUserName
The username to use when authenticating with the proxy server.
Definition policy.hpp:157
Utilities to be used by HTTP transport implementations.
Declaration of the UserAgentGenerator type.
Universally unique identifier.