| | | 1 | | // Copyright (c) Microsoft Corporation. All rights reserved. |
| | | 2 | | // Licensed under the MIT License. |
| | | 3 | | |
| | | 4 | | using System; |
| | | 5 | | using System.Collections.Generic; |
| | | 6 | | using System.Diagnostics; |
| | | 7 | | using System.Diagnostics.CodeAnalysis; |
| | | 8 | | using System.IO; |
| | | 9 | | using System.Net; |
| | | 10 | | using System.Net.Http; |
| | | 11 | | using System.Net.Http.Headers; |
| | | 12 | | using System.Threading; |
| | | 13 | | using System.Threading.Tasks; |
| | | 14 | | |
| | | 15 | | namespace Azure.Core.Pipeline |
| | | 16 | | { |
| | | 17 | | /// <summary> |
| | | 18 | | /// An <see cref="HttpPipelineTransport"/> implementation that uses <see cref="HttpClient"/> as the transport. |
| | | 19 | | /// </summary> |
| | | 20 | | public class HttpClientTransport : HttpPipelineTransport |
| | | 21 | | { |
| | | 22 | | private readonly HttpClient _client; |
| | | 23 | | |
| | | 24 | | /// <summary> |
| | | 25 | | /// Creates a new <see cref="HttpClientTransport"/> instance using default configuration. |
| | | 26 | | /// </summary> |
| | 672 | 27 | | public HttpClientTransport() : this(CreateDefaultClient()) |
| | | 28 | | { |
| | 672 | 29 | | } |
| | | 30 | | |
| | | 31 | | /// <summary> |
| | | 32 | | /// Creates a new instance of <see cref="HttpClientTransport"/> using the provided client instance. |
| | | 33 | | /// </summary> |
| | | 34 | | /// <param name="client">The instance of <see cref="HttpClient"/> to use.</param> |
| | 788 | 35 | | public HttpClientTransport(HttpClient client) |
| | | 36 | | { |
| | 788 | 37 | | _client = client ?? throw new ArgumentNullException(nameof(client)); |
| | 788 | 38 | | } |
| | | 39 | | |
| | | 40 | | /// <summary> |
| | | 41 | | /// A shared instance of <see cref="HttpClientTransport"/> with default parameters. |
| | | 42 | | /// </summary> |
| | 2 | 43 | | public static readonly HttpClientTransport Shared = new HttpClientTransport(); |
| | | 44 | | |
| | | 45 | | /// <inheritdoc /> |
| | | 46 | | public sealed override Request CreateRequest() |
| | 2382 | 47 | | => new PipelineRequest(); |
| | | 48 | | |
| | | 49 | | /// <inheritdoc /> |
| | | 50 | | public override void Process(HttpMessage message) |
| | | 51 | | { |
| | | 52 | | // Intentionally blocking here |
| | 1362 | 53 | | ProcessAsync(message).GetAwaiter().GetResult(); |
| | 1352 | 54 | | } |
| | | 55 | | |
| | | 56 | | /// <inheritdoc /> |
| | | 57 | | public sealed override async ValueTask ProcessAsync(HttpMessage message) |
| | | 58 | | { |
| | 2724 | 59 | | using (HttpRequestMessage httpRequest = BuildRequestMessage(message)) |
| | | 60 | | { |
| | | 61 | | HttpResponseMessage responseMessage; |
| | 2724 | 62 | | Stream? contentStream = null; |
| | | 63 | | try |
| | | 64 | | { |
| | 2724 | 65 | | responseMessage = await _client.SendAsync(httpRequest, HttpCompletionOption.ResponseHeadersRead, mes |
| | 2724 | 66 | | .ConfigureAwait(false); |
| | 2704 | 67 | | if (responseMessage.Content != null) |
| | | 68 | | { |
| | 2640 | 69 | | contentStream = await responseMessage.Content.ReadAsStreamAsync().ConfigureAwait(false); |
| | | 70 | | } |
| | 2704 | 71 | | } |
| | 8 | 72 | | catch (HttpRequestException e) |
| | | 73 | | { |
| | 8 | 74 | | throw new RequestFailedException(e.Message, e); |
| | | 75 | | } |
| | | 76 | | |
| | 2704 | 77 | | message.Response = new PipelineResponse(message.Request.ClientRequestId, responseMessage, contentStream) |
| | 2704 | 78 | | } |
| | 2704 | 79 | | } |
| | | 80 | | |
| | | 81 | | private static HttpClient CreateDefaultClient() |
| | | 82 | | { |
| | 672 | 83 | | var httpClientHandler = new HttpClientHandler(); |
| | 672 | 84 | | if (HttpEnvironmentProxy.TryCreate(out IWebProxy webProxy)) |
| | | 85 | | { |
| | 8 | 86 | | httpClientHandler.Proxy = webProxy; |
| | | 87 | | } |
| | | 88 | | |
| | 672 | 89 | | return new HttpClient(httpClientHandler); |
| | | 90 | | } |
| | | 91 | | |
| | | 92 | | private static HttpRequestMessage BuildRequestMessage(HttpMessage message) |
| | | 93 | | { |
| | 2724 | 94 | | if (!(message.Request is PipelineRequest pipelineRequest)) |
| | | 95 | | { |
| | 0 | 96 | | throw new InvalidOperationException("the request is not compatible with the transport"); |
| | | 97 | | } |
| | 2724 | 98 | | return pipelineRequest.BuildRequestMessage(message.CancellationToken); |
| | | 99 | | } |
| | | 100 | | |
| | | 101 | | internal static bool TryGetHeader(HttpHeaders headers, HttpContent? content, string name, [NotNullWhen(true)] ou |
| | | 102 | | { |
| | 7700 | 103 | | if (TryGetHeader(headers, content, name, out IEnumerable<string>? values)) |
| | | 104 | | { |
| | 664 | 105 | | value = JoinHeaderValues(values); |
| | 664 | 106 | | return true; |
| | | 107 | | } |
| | | 108 | | |
| | 7036 | 109 | | value = null; |
| | 7036 | 110 | | return false; |
| | | 111 | | } |
| | | 112 | | |
| | | 113 | | internal static bool TryGetHeader(HttpHeaders headers, HttpContent? content, string name, [NotNullWhen(true)] ou |
| | | 114 | | { |
| | 8008 | 115 | | return headers.TryGetValues(name, out values) || content?.Headers.TryGetValues(name, out values) == true; |
| | | 116 | | } |
| | | 117 | | |
| | | 118 | | internal static IEnumerable<HttpHeader> GetHeaders(HttpHeaders headers, HttpContent? content) |
| | | 119 | | { |
| | 33992 | 120 | | foreach (KeyValuePair<string, IEnumerable<string>> header in headers) |
| | | 121 | | { |
| | 12520 | 122 | | yield return new HttpHeader(header.Key, JoinHeaderValues(header.Value)); |
| | | 123 | | } |
| | | 124 | | |
| | 4456 | 125 | | if (content != null) |
| | | 126 | | { |
| | 5292 | 127 | | foreach (KeyValuePair<string, IEnumerable<string>> header in content.Headers) |
| | | 128 | | { |
| | 388 | 129 | | yield return new HttpHeader(header.Key, JoinHeaderValues(header.Value)); |
| | | 130 | | } |
| | | 131 | | } |
| | 4332 | 132 | | } |
| | | 133 | | |
| | | 134 | | internal static bool RemoveHeader(HttpHeaders headers, HttpContent? content, string name) |
| | | 135 | | { |
| | | 136 | | // .Remove throws on invalid header name so use TryGet here to check |
| | 3432 | 137 | | if (headers.TryGetValues(name, out _) && headers.Remove(name)) |
| | | 138 | | { |
| | 44 | 139 | | return true; |
| | | 140 | | } |
| | | 141 | | |
| | 3388 | 142 | | return content?.Headers.TryGetValues(name, out _) == true && content.Headers.Remove(name); |
| | | 143 | | } |
| | | 144 | | |
| | | 145 | | internal static bool ContainsHeader(HttpHeaders headers, HttpContent? content, string name) |
| | | 146 | | { |
| | | 147 | | // .Contains throws on invalid header name so use TryGet here |
| | 276 | 148 | | if (headers.TryGetValues(name, out _)) |
| | | 149 | | { |
| | 56 | 150 | | return true; |
| | | 151 | | } |
| | | 152 | | |
| | 220 | 153 | | return content?.Headers.TryGetValues(name, out _) == true; |
| | | 154 | | } |
| | | 155 | | |
| | | 156 | | internal static void CopyHeaders(HttpHeaders from, HttpHeaders to) |
| | | 157 | | { |
| | 3288 | 158 | | foreach (KeyValuePair<string, IEnumerable<string>> header in from) |
| | | 159 | | { |
| | 1228 | 160 | | if (!to.TryAddWithoutValidation(header.Key, header.Value)) |
| | | 161 | | { |
| | 0 | 162 | | throw new InvalidOperationException($"Unable to add header {header} to header collection."); |
| | | 163 | | } |
| | | 164 | | } |
| | 416 | 165 | | } |
| | | 166 | | |
| | | 167 | | private static string JoinHeaderValues(IEnumerable<string> values) |
| | | 168 | | { |
| | 13572 | 169 | | return string.Join(",", values); |
| | | 170 | | } |
| | | 171 | | |
| | | 172 | | private sealed class PipelineRequest : Request |
| | | 173 | | { |
| | | 174 | | private bool _wasSent = false; |
| | | 175 | | private readonly HttpRequestMessage _requestMessage; |
| | | 176 | | |
| | | 177 | | private PipelineContentAdapter? _requestContent; |
| | | 178 | | private string? _clientRequestId; |
| | | 179 | | |
| | 2382 | 180 | | public PipelineRequest() |
| | | 181 | | { |
| | 2382 | 182 | | _requestMessage = new HttpRequestMessage(); |
| | 2382 | 183 | | } |
| | | 184 | | |
| | | 185 | | public override RequestMethod Method |
| | | 186 | | { |
| | 2076 | 187 | | get => RequestMethod.Parse(_requestMessage.Method.Method); |
| | 654 | 188 | | set => _requestMessage.Method = ToHttpClientMethod(value); |
| | | 189 | | } |
| | | 190 | | |
| | 7572 | 191 | | public override RequestContent? Content { get; set; } |
| | | 192 | | |
| | | 193 | | public override string ClientRequestId |
| | | 194 | | { |
| | 8832 | 195 | | get => _clientRequestId ??= Guid.NewGuid().ToString(); |
| | | 196 | | set |
| | | 197 | | { |
| | 8 | 198 | | Argument.AssertNotNull(value, nameof(value)); |
| | 4 | 199 | | _clientRequestId = value; |
| | 4 | 200 | | } |
| | | 201 | | } |
| | | 202 | | |
| | | 203 | | protected internal override void AddHeader(string name, string value) |
| | | 204 | | { |
| | 5404 | 205 | | if (_requestMessage.Headers.TryAddWithoutValidation(name, value)) |
| | | 206 | | { |
| | 5054 | 207 | | return; |
| | | 208 | | } |
| | | 209 | | |
| | 350 | 210 | | PipelineContentAdapter requestContent = EnsureContentInitialized(); |
| | 350 | 211 | | if (!requestContent.Headers.TryAddWithoutValidation(name, value)) |
| | | 212 | | { |
| | 0 | 213 | | throw new InvalidOperationException("Unable to add header to request or content"); |
| | | 214 | | } |
| | 350 | 215 | | } |
| | | 216 | | |
| | 4324 | 217 | | protected internal override bool TryGetHeader(string name, [NotNullWhen(true)] out string? value) => HttpCli |
| | | 218 | | |
| | 136 | 219 | | protected internal override bool TryGetHeaderValues(string name, [NotNullWhen(true)] out IEnumerable<string> |
| | | 220 | | |
| | 112 | 221 | | protected internal override bool ContainsHeader(string name) => HttpClientTransport.ContainsHeader(_requestM |
| | | 222 | | |
| | 3432 | 223 | | protected internal override bool RemoveHeader(string name) => HttpClientTransport.RemoveHeader(_requestMessa |
| | | 224 | | |
| | 2316 | 225 | | protected internal override IEnumerable<HttpHeader> EnumerateHeaders() => GetHeaders(_requestMessage.Headers |
| | | 226 | | |
| | | 227 | | public HttpRequestMessage BuildRequestMessage(CancellationToken cancellation) |
| | | 228 | | { |
| | | 229 | | HttpRequestMessage currentRequest; |
| | 2724 | 230 | | if (_wasSent) |
| | | 231 | | { |
| | | 232 | | // A copy of a message needs to be made because HttpClient does not allow sending the same message t |
| | | 233 | | // and so the retry logic fails. |
| | 412 | 234 | | currentRequest = new HttpRequestMessage(_requestMessage.Method, Uri.ToUri()); |
| | 412 | 235 | | CopyHeaders(_requestMessage.Headers, currentRequest.Headers); |
| | | 236 | | } |
| | | 237 | | else |
| | | 238 | | { |
| | 2312 | 239 | | currentRequest = _requestMessage; |
| | | 240 | | } |
| | | 241 | | |
| | 2724 | 242 | | currentRequest.RequestUri = Uri.ToUri(); |
| | | 243 | | |
| | | 244 | | |
| | 2724 | 245 | | if (Content != null) |
| | | 246 | | { |
| | | 247 | | PipelineContentAdapter currentContent; |
| | 496 | 248 | | if (_wasSent) |
| | | 249 | | { |
| | 4 | 250 | | currentContent = new PipelineContentAdapter(); |
| | 4 | 251 | | CopyHeaders(_requestContent!.Headers, currentContent.Headers); |
| | | 252 | | } |
| | | 253 | | else |
| | | 254 | | { |
| | 492 | 255 | | currentContent = EnsureContentInitialized(); |
| | | 256 | | } |
| | | 257 | | |
| | 496 | 258 | | currentContent.CancellationToken = cancellation; |
| | 496 | 259 | | currentContent.PipelineContent = Content; |
| | 496 | 260 | | currentRequest.Content = currentContent; |
| | | 261 | | } |
| | | 262 | | |
| | 2724 | 263 | | _wasSent = true; |
| | 2724 | 264 | | return currentRequest; |
| | | 265 | | } |
| | | 266 | | |
| | | 267 | | public override void Dispose() |
| | | 268 | | { |
| | 1628 | 269 | | Content?.Dispose(); |
| | 1628 | 270 | | _requestMessage.Dispose(); |
| | 1628 | 271 | | } |
| | | 272 | | |
| | 0 | 273 | | public override string ToString() => _requestMessage.ToString(); |
| | | 274 | | |
| | 2 | 275 | | private static readonly HttpMethod s_patch = new HttpMethod("PATCH"); |
| | | 276 | | |
| | | 277 | | private static HttpMethod ToHttpClientMethod(RequestMethod requestMethod) |
| | | 278 | | { |
| | 654 | 279 | | var method = requestMethod.Method; |
| | | 280 | | // Fast-path common values |
| | 654 | 281 | | if (method.Length == 3) |
| | | 282 | | { |
| | 150 | 283 | | if (string.Equals(method, "GET", StringComparison.OrdinalIgnoreCase)) |
| | | 284 | | { |
| | 142 | 285 | | return HttpMethod.Get; |
| | | 286 | | } |
| | | 287 | | |
| | 8 | 288 | | if (string.Equals(method, "PUT", StringComparison.OrdinalIgnoreCase)) |
| | | 289 | | { |
| | 8 | 290 | | return HttpMethod.Put; |
| | | 291 | | } |
| | | 292 | | } |
| | 504 | 293 | | else if (method.Length == 4) |
| | | 294 | | { |
| | 492 | 295 | | if (string.Equals(method, "POST", StringComparison.OrdinalIgnoreCase)) |
| | | 296 | | { |
| | 488 | 297 | | return HttpMethod.Post; |
| | | 298 | | } |
| | 4 | 299 | | if (string.Equals(method, "HEAD", StringComparison.OrdinalIgnoreCase)) |
| | | 300 | | { |
| | 4 | 301 | | return HttpMethod.Head; |
| | | 302 | | } |
| | | 303 | | } |
| | | 304 | | else |
| | | 305 | | { |
| | 12 | 306 | | if (string.Equals(method, "PATCH", StringComparison.OrdinalIgnoreCase)) |
| | | 307 | | { |
| | 4 | 308 | | return s_patch; |
| | | 309 | | } |
| | 8 | 310 | | if (string.Equals(method, "DELETE", StringComparison.OrdinalIgnoreCase)) |
| | | 311 | | { |
| | 4 | 312 | | return HttpMethod.Delete; |
| | | 313 | | } |
| | | 314 | | } |
| | | 315 | | |
| | 4 | 316 | | return new HttpMethod(method); |
| | | 317 | | } |
| | | 318 | | |
| | | 319 | | private PipelineContentAdapter EnsureContentInitialized() |
| | | 320 | | { |
| | 842 | 321 | | if (_requestContent == null) |
| | | 322 | | { |
| | 538 | 323 | | _requestContent = new PipelineContentAdapter(); |
| | | 324 | | } |
| | | 325 | | |
| | 842 | 326 | | return _requestContent; |
| | | 327 | | } |
| | | 328 | | |
| | | 329 | | private sealed class PipelineContentAdapter : HttpContent |
| | | 330 | | { |
| | 1468 | 331 | | public RequestContent? PipelineContent { get; set; } |
| | | 332 | | |
| | 992 | 333 | | public CancellationToken CancellationToken { get; set; } |
| | | 334 | | |
| | | 335 | | protected override async Task SerializeToStreamAsync(Stream stream, TransportContext context) |
| | | 336 | | { |
| | | 337 | | Debug.Assert(PipelineContent != null); |
| | 496 | 338 | | await PipelineContent!.WriteToAsync(stream, CancellationToken).ConfigureAwait(false); |
| | 496 | 339 | | } |
| | | 340 | | |
| | | 341 | | protected override bool TryComputeLength(out long length) |
| | | 342 | | { |
| | | 343 | | Debug.Assert(PipelineContent != null); |
| | | 344 | | |
| | 476 | 345 | | return PipelineContent!.TryComputeLength(out length); |
| | | 346 | | } |
| | | 347 | | } |
| | | 348 | | } |
| | | 349 | | |
| | | 350 | | private sealed class PipelineResponse : Response |
| | | 351 | | { |
| | | 352 | | private readonly HttpResponseMessage _responseMessage; |
| | | 353 | | |
| | | 354 | | private readonly HttpContent _responseContent; |
| | | 355 | | |
| | | 356 | | private Stream? _contentStream; |
| | | 357 | | |
| | 2704 | 358 | | public PipelineResponse(string requestId, HttpResponseMessage responseMessage, Stream? contentStream) |
| | | 359 | | { |
| | 2704 | 360 | | ClientRequestId = requestId ?? throw new ArgumentNullException(nameof(requestId)); |
| | 2704 | 361 | | _responseMessage = responseMessage ?? throw new ArgumentNullException(nameof(responseMessage)); |
| | 2704 | 362 | | _contentStream = contentStream; |
| | 2704 | 363 | | _responseContent = _responseMessage.Content; |
| | 2704 | 364 | | } |
| | | 365 | | |
| | 6480 | 366 | | public override int Status => (int)_responseMessage.StatusCode; |
| | | 367 | | |
| | 2020 | 368 | | public override string ReasonPhrase => _responseMessage.ReasonPhrase; |
| | | 369 | | |
| | | 370 | | public override Stream? ContentStream |
| | | 371 | | { |
| | 12080 | 372 | | get => _contentStream; |
| | | 373 | | set |
| | | 374 | | { |
| | | 375 | | // Make sure we don't dispose the content if the stream was replaced |
| | 2872 | 376 | | _responseMessage.Content = null; |
| | | 377 | | |
| | 2872 | 378 | | _contentStream = value; |
| | 2872 | 379 | | } |
| | | 380 | | } |
| | | 381 | | |
| | 6744 | 382 | | public override string ClientRequestId { get; set; } |
| | | 383 | | |
| | 3376 | 384 | | protected internal override bool TryGetHeader(string name, [NotNullWhen(true)] out string? value) => HttpCli |
| | | 385 | | |
| | 172 | 386 | | protected internal override bool TryGetHeaderValues(string name, [NotNullWhen(true)] out IEnumerable<string> |
| | | 387 | | |
| | 164 | 388 | | protected internal override bool ContainsHeader(string name) => HttpClientTransport.ContainsHeader(_response |
| | | 389 | | |
| | 2180 | 390 | | protected internal override IEnumerable<HttpHeader> EnumerateHeaders() => GetHeaders(_responseMessage.Header |
| | | 391 | | |
| | | 392 | | public override void Dispose() |
| | | 393 | | { |
| | 1624 | 394 | | _responseMessage?.Dispose(); |
| | 1624 | 395 | | } |
| | | 396 | | |
| | 0 | 397 | | public override string ToString() => _responseMessage.ToString(); |
| | | 398 | | } |
| | | 399 | | } |
| | | 400 | | } |