Exemplo n.º 1
0
        public HttpPipeline(HttpPipelineTransport transport, HttpPipelinePolicy[] policies = null, IServiceProvider services = null)
        {
            _transport = transport ?? throw new ArgumentNullException(nameof(transport));

            policies = policies ?? Array.Empty <HttpPipelinePolicy>();

            var all = new HttpPipelinePolicy[policies.Length + 1];

            all[policies.Length] = new HttpPipelineTransportPolicy(_transport);
            policies.CopyTo(all, 0);

            _pipeline = all;
            _services = services ?? HttpClientOptions.EmptyServiceProvider.Singleton;
        }
Exemplo n.º 2
0
        /// <summary>
        /// Creates a new instance of <see cref="HttpPipeline"/> with the provided transport, policies and response classifier.
        /// </summary>
        /// <param name="transport">The <see cref="HttpPipelineTransport"/> to use for sending the requests.</param>
        /// <param name="policies">Policies to be invoked as part of the pipeline in order.</param>
        /// <param name="responseClassifier">The response classifier to be used in invocations.</param>
        public HttpPipeline(HttpPipelineTransport transport, HttpPipelinePolicy[]?policies = null, ResponseClassifier?responseClassifier = null)
        {
            _transport         = transport ?? throw new ArgumentNullException(nameof(transport));
            ResponseClassifier = responseClassifier ?? new ResponseClassifier();

            policies = policies ?? Array.Empty <HttpPipelinePolicy>();

            var all = new HttpPipelinePolicy[policies.Length + 1];

            all[policies.Length] = new HttpPipelineTransportPolicy(_transport);
            policies.CopyTo(all, 0);

            _pipeline = all;
        }
Exemplo n.º 3
0
        /// <summary>
        /// Creates a new instance of <see cref="HttpPipeline"/> with the provided transport, policies and response classifier.
        /// </summary>
        /// <param name="transport">The <see cref="HttpPipelineTransport"/> to use for sending the requests.</param>
        /// <param name="policies">Policies to be invoked as part of the pipeline in order.</param>
        /// <param name="responseClassifier">The response classifier to be used in invocations.</param>
        public HttpPipeline(HttpPipelineTransport transport, HttpPipelinePolicy[]?policies = null, ResponseClassifier?responseClassifier = null)
        {
            _transport         = transport ?? throw new ArgumentNullException(nameof(transport));
            ResponseClassifier = responseClassifier ?? ResponseClassifier.Shared;

            policies ??= Array.Empty <HttpPipelinePolicy>();

            var all = new HttpPipelinePolicy[policies.Length + 1];

            all[policies.Length] = new HttpPipelineTransportPolicy(_transport,
                                                                   ClientDiagnostics.CreateMessageSanitizer(new DiagnosticsOptions()));
            policies.CopyTo(all, 0);

            _pipeline = all;
        }
Exemplo n.º 4
0
        internal HttpPipeline(
            HttpPipelineTransport transport,
            int perCallIndex,
            int perRetryIndex,
            HttpPipelinePolicy[] pipeline,
            ResponseClassifier responseClassifier)
        {
            ResponseClassifier = responseClassifier ?? throw new ArgumentNullException(nameof(responseClassifier));

            _transport = transport ?? throw new ArgumentNullException(nameof(transport));
            _pipeline  = pipeline ?? throw new ArgumentNullException(nameof(pipeline));

            Debug.Assert(pipeline[pipeline.Length - 1] is HttpPipelineTransportPolicy);

            _perCallIndex          = perCallIndex;
            _perRetryIndex         = perRetryIndex;
            _internallyConstructed = true;
        }
 public HttpPipelineTransportPolicy(HttpPipelineTransport transport)
 {
     _transport = transport;
 }
        internal static (ResponseClassifier Classifier, HttpPipelineTransport Transport, int PerCallIndex, int PerRetryIndex, HttpPipelinePolicy[] Policies, bool IsTransportOwned) BuildInternal(
            ClientOptions options,
            HttpPipelinePolicy[] perCallPolicies,
            HttpPipelinePolicy[] perRetryPolicies,
            HttpPipelineTransportOptions?defaultTransportOptions,
            ResponseClassifier?responseClassifier)
        {
            if (perCallPolicies == null)
            {
                throw new ArgumentNullException(nameof(perCallPolicies));
            }

            if (perRetryPolicies == null)
            {
                throw new ArgumentNullException(nameof(perRetryPolicies));
            }

            var policies = new List <HttpPipelinePolicy>(8 +
                                                         (options.Policies?.Count ?? 0) +
                                                         perCallPolicies.Length +
                                                         perRetryPolicies.Length);

            void AddCustomerPolicies(HttpPipelinePosition position)
            {
                if (options.Policies != null)
                {
                    foreach (var policy in options.Policies)
                    {
                        // skip null policies to ensure that calculations for perCallIndex and perRetryIndex are accurate
                        if (policy.Position == position && policy.Policy != null)
                        {
                            policies.Add(policy.Policy);
                        }
                    }
                }
            }

            // A helper to ensure that we only add non-null policies to the policies list
            // This ensures that calculations for perCallIndex and perRetryIndex are accurate
            void AddNonNullPolicies(HttpPipelinePolicy[] policiesToAdd)
            {
                for (int i = 0; i < policiesToAdd.Length; i++)
                {
                    var policy = policiesToAdd[i];
                    if (policy != null)
                    {
                        policies.Add(policy);
                    }
                }
            }

            DiagnosticsOptions diagnostics = options.Diagnostics;

            var sanitizer = new HttpMessageSanitizer(diagnostics.LoggedQueryParameters.ToArray(), diagnostics.LoggedHeaderNames.ToArray());

            bool isDistributedTracingEnabled = options.Diagnostics.IsDistributedTracingEnabled;

            policies.Add(ReadClientRequestIdPolicy.Shared);

            AddNonNullPolicies(perCallPolicies);

            AddCustomerPolicies(HttpPipelinePosition.PerCall);

            var perCallIndex = policies.Count;

            policies.Add(ClientRequestIdPolicy.Shared);

            if (diagnostics.IsTelemetryEnabled)
            {
                policies.Add(CreateTelemetryPolicy(options));
            }

            RetryOptions retryOptions = options.Retry;

            policies.Add(new RetryPolicy(retryOptions.Mode, retryOptions.Delay, retryOptions.MaxDelay, retryOptions.MaxRetries));

            policies.Add(RedirectPolicy.Shared);

            AddNonNullPolicies(perRetryPolicies);

            AddCustomerPolicies(HttpPipelinePosition.PerRetry);

            var perRetryIndex = policies.Count;

            if (diagnostics.IsLoggingEnabled)
            {
                string assemblyName = options.GetType().Assembly !.GetName().Name !;

                policies.Add(new LoggingPolicy(diagnostics.IsLoggingContentEnabled, diagnostics.LoggedContentSizeLimit, sanitizer, assemblyName));
            }

            policies.Add(new ResponseBodyPolicy(options.Retry.NetworkTimeout));

            policies.Add(new RequestActivityPolicy(isDistributedTracingEnabled, ClientDiagnostics.GetResourceProviderNamespace(options.GetType().Assembly), sanitizer));

            AddCustomerPolicies(HttpPipelinePosition.BeforeTransport);

            // Override the provided Transport with the provided transport options if the transport has not been set after default construction and options are not null.
            HttpPipelineTransport transport   = options.Transport;
            bool isTransportInternallyCreated = false;

            if (defaultTransportOptions != null)
            {
                if (options.IsCustomTransportSet)
                {
                    if (AzureCoreEventSource.Singleton.IsEnabled())
                    {
                        // Log that we were unable to override the custom transport
                        AzureCoreEventSource.Singleton.PipelineTransportOptionsNotApplied(options?.GetType().FullName ?? String.Empty);
                    }
                }
                else
                {
                    transport = HttpPipelineTransport.Create(defaultTransportOptions);
                    isTransportInternallyCreated = true;
                }
            }

            policies.Add(new HttpPipelineTransportPolicy(transport, sanitizer));

            responseClassifier ??= ResponseClassifier.Shared;

            return(responseClassifier, transport, perCallIndex, perRetryIndex, policies.ToArray(), isTransportInternallyCreated);
        }
 /// <summary>
 /// Creates a new instance of <see cref="HttpPipeline"/> with the provided transport, policies and response classifier.
 /// </summary>
 /// <param name="transport">The <see cref="HttpPipelineTransport"/> to use for sending the requests.</param>
 /// <param name="perCallIndex"></param>
 /// <param name="perRetryIndex"></param>
 /// <param name="policies">Policies to be invoked as part of the pipeline in order.</param>
 /// <param name="responseClassifier">The response classifier to be used in invocations.</param>
 internal DisposableHttpPipeline(HttpPipelineTransport transport, int perCallIndex, int perRetryIndex, HttpPipelinePolicy[] policies, ResponseClassifier responseClassifier)
     : base(transport, perCallIndex, perRetryIndex, policies, responseClassifier)
 {
 }
Exemplo n.º 8
0
 public HttpPipelineTransportPolicy(HttpPipelineTransport transport, HttpMessageSanitizer sanitizer)
 {
     _transport = transport;
     _sanitizer = sanitizer;
 }
 /// <summary>
 /// Creates a new instance of <see cref="HttpPipeline"/> with the provided transport, policies and response classifier.
 /// </summary>
 /// <param name="transport">The <see cref="HttpPipelineTransport"/> to use for sending the requests.</param>
 /// <param name="perCallIndex"></param>
 /// <param name="perRetryIndex"></param>
 /// <param name="policies">Policies to be invoked as part of the pipeline in order.</param>
 /// <param name="responseClassifier">The response classifier to be used in invocations.</param>
 /// <param name="isTransportOwnedInternally"> </param>
 internal DisposableHttpPipeline(HttpPipelineTransport transport, int perCallIndex, int perRetryIndex, HttpPipelinePolicy[] policies, ResponseClassifier responseClassifier, bool isTransportOwnedInternally)
     : base(transport, perCallIndex, perRetryIndex, policies, responseClassifier)
 {
     this.isTransportOwnedInternally = isTransportOwnedInternally;
 }