| | | 1 | | // Licensed to the .NET Foundation under one or more agreements. |
| | | 2 | | // The .NET Foundation licenses this file to you under the MIT license. |
| | | 3 | | |
| | | 4 | | using System; |
| | | 5 | | using System.Collections.Generic; |
| | | 6 | | using System.Diagnostics.CodeAnalysis; |
| | | 7 | | using System.Text; |
| | | 8 | | using System.Threading; |
| | | 9 | | using System.Threading.Tasks; |
| | | 10 | | |
| | | 11 | | namespace System.Net.Http |
| | | 12 | | { |
| | | 13 | | public abstract class DelegatingHandler : HttpMessageHandler |
| | | 14 | | { |
| | | 15 | | private HttpMessageHandler? _innerHandler; |
| | | 16 | | private volatile bool _operationStarted; |
| | | 17 | | private volatile bool _disposed; |
| | | 18 | | |
| | | 19 | | [DisallowNull] |
| | | 20 | | public HttpMessageHandler? InnerHandler |
| | | 21 | | { |
| | | 22 | | get |
| | 0 | 23 | | { |
| | 0 | 24 | | return _innerHandler; |
| | 0 | 25 | | } |
| | | 26 | | set |
| | 0 | 27 | | { |
| | 0 | 28 | | ArgumentNullException.ThrowIfNull(value); |
| | 0 | 29 | | CheckDisposedOrStarted(); |
| | | 30 | | |
| | 0 | 31 | | if (NetEventSource.Log.IsEnabled()) NetEventSource.Associate(this, value); |
| | 0 | 32 | | _innerHandler = value; |
| | 0 | 33 | | } |
| | | 34 | | } |
| | | 35 | | |
| | 0 | 36 | | protected DelegatingHandler() |
| | 0 | 37 | | { |
| | 0 | 38 | | } |
| | | 39 | | |
| | 0 | 40 | | protected DelegatingHandler(HttpMessageHandler innerHandler) |
| | 0 | 41 | | { |
| | 0 | 42 | | InnerHandler = innerHandler; |
| | 0 | 43 | | } |
| | | 44 | | |
| | | 45 | | protected internal override HttpResponseMessage Send(HttpRequestMessage request, CancellationToken cancellationT |
| | 0 | 46 | | { |
| | 0 | 47 | | ArgumentNullException.ThrowIfNull(request); |
| | | 48 | | |
| | 0 | 49 | | SetOperationStarted(); |
| | 0 | 50 | | return _innerHandler!.Send(request, cancellationToken); |
| | 0 | 51 | | } |
| | | 52 | | |
| | | 53 | | protected internal override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken ca |
| | 0 | 54 | | { |
| | 0 | 55 | | ArgumentNullException.ThrowIfNull(request); |
| | | 56 | | |
| | 0 | 57 | | SetOperationStarted(); |
| | 0 | 58 | | return _innerHandler!.SendAsync(request, cancellationToken); |
| | 0 | 59 | | } |
| | | 60 | | |
| | | 61 | | protected override void Dispose(bool disposing) |
| | 0 | 62 | | { |
| | 0 | 63 | | if (disposing && !_disposed) |
| | 0 | 64 | | { |
| | 0 | 65 | | _disposed = true; |
| | 0 | 66 | | _innerHandler?.Dispose(); |
| | 0 | 67 | | } |
| | | 68 | | |
| | 0 | 69 | | base.Dispose(disposing); |
| | 0 | 70 | | } |
| | | 71 | | |
| | | 72 | | private void CheckDisposedOrStarted() |
| | 0 | 73 | | { |
| | 0 | 74 | | ObjectDisposedException.ThrowIf(_disposed, this); |
| | | 75 | | |
| | 0 | 76 | | if (_operationStarted) |
| | 0 | 77 | | { |
| | 0 | 78 | | throw new InvalidOperationException(SR.net_http_operation_started); |
| | | 79 | | } |
| | 0 | 80 | | } |
| | | 81 | | |
| | | 82 | | private void SetOperationStarted() |
| | 0 | 83 | | { |
| | 0 | 84 | | ObjectDisposedException.ThrowIf(_disposed, this); |
| | | 85 | | |
| | 0 | 86 | | if (_innerHandler == null) |
| | 0 | 87 | | { |
| | 0 | 88 | | throw new InvalidOperationException(SR.net_http_handler_not_assigned); |
| | | 89 | | } |
| | | 90 | | // This method flags the handler instances as "active". I.e. we executed at least one request (or are |
| | | 91 | | // in the process of doing so). This information is used to lock-down all property setters. Once a |
| | | 92 | | // Send/SendAsync operation started, no property can be changed. |
| | 0 | 93 | | if (!_operationStarted) |
| | 0 | 94 | | { |
| | 0 | 95 | | _operationStarted = true; |
| | 0 | 96 | | } |
| | 0 | 97 | | } |
| | | 98 | | } |
| | | 99 | | } |