| | | 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.Diagnostics; |
| | | 5 | | using System.Diagnostics.CodeAnalysis; |
| | | 6 | | using System.IO; |
| | | 7 | | using System.Net.Http.Headers; |
| | | 8 | | using System.Runtime.Versioning; |
| | | 9 | | using System.Threading; |
| | | 10 | | using System.Threading.Tasks; |
| | | 11 | | |
| | | 12 | | namespace System.Net.Http |
| | | 13 | | { |
| | | 14 | | public partial class HttpClient : HttpMessageInvoker |
| | | 15 | | { |
| | | 16 | | #region Fields |
| | | 17 | | |
| | | 18 | | private static IWebProxy? s_defaultProxy; |
| | 0 | 19 | | private static readonly TimeSpan s_defaultTimeout = TimeSpan.FromSeconds(100); |
| | 0 | 20 | | private static readonly TimeSpan s_maxTimeout = TimeSpan.FromMilliseconds(int.MaxValue); |
| | 0 | 21 | | private static readonly TimeSpan s_infiniteTimeout = Threading.Timeout.InfiniteTimeSpan; |
| | | 22 | | private const HttpCompletionOption DefaultCompletionOption = HttpCompletionOption.ResponseContentRead; |
| | | 23 | | |
| | | 24 | | private volatile bool _operationStarted; |
| | | 25 | | private volatile bool _disposed; |
| | | 26 | | |
| | | 27 | | private CancellationTokenSource _pendingRequestsCts; |
| | | 28 | | private HttpRequestHeaders? _defaultRequestHeaders; |
| | 0 | 29 | | private Version _defaultRequestVersion = HttpRequestMessage.DefaultRequestVersion; |
| | 0 | 30 | | private HttpVersionPolicy _defaultVersionPolicy = HttpRequestMessage.DefaultVersionPolicy; |
| | | 31 | | |
| | | 32 | | private Uri? _baseAddress; |
| | | 33 | | private TimeSpan _timeout; |
| | | 34 | | private int _maxResponseContentBufferSize; |
| | | 35 | | |
| | | 36 | | #endregion Fields |
| | | 37 | | |
| | | 38 | | #region Properties |
| | | 39 | | public static IWebProxy DefaultProxy |
| | | 40 | | { |
| | 0 | 41 | | get => LazyInitializer.EnsureInitialized(ref s_defaultProxy, () => SystemProxyInfo.Proxy); |
| | | 42 | | set |
| | 0 | 43 | | { |
| | 0 | 44 | | ArgumentNullException.ThrowIfNull(value); |
| | 0 | 45 | | s_defaultProxy = value; |
| | 0 | 46 | | } |
| | | 47 | | } |
| | | 48 | | |
| | | 49 | | public HttpRequestHeaders DefaultRequestHeaders => |
| | 0 | 50 | | _defaultRequestHeaders ??= new HttpRequestHeaders(); |
| | | 51 | | |
| | | 52 | | public Version DefaultRequestVersion |
| | | 53 | | { |
| | 0 | 54 | | get => _defaultRequestVersion; |
| | | 55 | | set |
| | 0 | 56 | | { |
| | 0 | 57 | | CheckDisposedOrStarted(); |
| | 0 | 58 | | ArgumentNullException.ThrowIfNull(value); |
| | 0 | 59 | | _defaultRequestVersion = value; |
| | 0 | 60 | | } |
| | | 61 | | } |
| | | 62 | | |
| | | 63 | | /// <summary> |
| | | 64 | | /// Gets or sets the default value of <see cref="HttpRequestMessage.VersionPolicy" /> for implicitly created req |
| | | 65 | | /// e.g.: <see cref="GetAsync(string?)" />, <see cref="PostAsync(string?, HttpContent)" />. |
| | | 66 | | /// </summary> |
| | | 67 | | /// <remarks> |
| | | 68 | | /// Note that this property has no effect on any of the <see cref="Send(HttpRequestMessage)" /> and <see cref="S |
| | | 69 | | /// since they accept fully initialized <see cref="HttpRequestMessage" />. |
| | | 70 | | /// </remarks> |
| | | 71 | | public HttpVersionPolicy DefaultVersionPolicy |
| | | 72 | | { |
| | 0 | 73 | | get => _defaultVersionPolicy; |
| | | 74 | | set |
| | 0 | 75 | | { |
| | 0 | 76 | | CheckDisposedOrStarted(); |
| | 0 | 77 | | _defaultVersionPolicy = value; |
| | 0 | 78 | | } |
| | | 79 | | } |
| | | 80 | | |
| | | 81 | | public Uri? BaseAddress |
| | | 82 | | { |
| | 0 | 83 | | get => _baseAddress; |
| | | 84 | | set |
| | 0 | 85 | | { |
| | | 86 | | // It's OK to not have a base address specified, but if one is, it needs to be absolute. |
| | 0 | 87 | | if (value is not null && !value.IsAbsoluteUri) |
| | 0 | 88 | | { |
| | 0 | 89 | | throw new ArgumentException(SR.net_http_client_absolute_baseaddress_required, nameof(value)); |
| | | 90 | | } |
| | | 91 | | |
| | 0 | 92 | | CheckDisposedOrStarted(); |
| | | 93 | | |
| | 0 | 94 | | if (NetEventSource.Log.IsEnabled()) NetEventSource.UriBaseAddress(this, value); |
| | | 95 | | |
| | 0 | 96 | | _baseAddress = value; |
| | 0 | 97 | | } |
| | | 98 | | } |
| | | 99 | | |
| | | 100 | | public TimeSpan Timeout |
| | | 101 | | { |
| | 0 | 102 | | get => _timeout; |
| | | 103 | | set |
| | 0 | 104 | | { |
| | 0 | 105 | | if (value != s_infiniteTimeout) |
| | 0 | 106 | | { |
| | 0 | 107 | | ArgumentOutOfRangeException.ThrowIfLessThanOrEqual(value, TimeSpan.Zero); |
| | 0 | 108 | | ArgumentOutOfRangeException.ThrowIfGreaterThan(value, s_maxTimeout); |
| | 0 | 109 | | } |
| | 0 | 110 | | CheckDisposedOrStarted(); |
| | 0 | 111 | | _timeout = value; |
| | 0 | 112 | | } |
| | | 113 | | } |
| | | 114 | | |
| | | 115 | | public long MaxResponseContentBufferSize |
| | | 116 | | { |
| | 0 | 117 | | get => _maxResponseContentBufferSize; |
| | | 118 | | set |
| | 0 | 119 | | { |
| | 0 | 120 | | ArgumentOutOfRangeException.ThrowIfNegativeOrZero(value); |
| | 0 | 121 | | if (value > HttpContent.MaxBufferSize) |
| | 0 | 122 | | { |
| | 0 | 123 | | throw new ArgumentOutOfRangeException(nameof(value), value, |
| | 0 | 124 | | SR.Format(System.Globalization.CultureInfo.InvariantCulture, |
| | 0 | 125 | | SR.net_http_content_buffersize_limit, HttpContent.MaxBufferSize)); |
| | | 126 | | } |
| | 0 | 127 | | CheckDisposedOrStarted(); |
| | | 128 | | |
| | 0 | 129 | | Debug.Assert(HttpContent.MaxBufferSize <= int.MaxValue); |
| | 0 | 130 | | _maxResponseContentBufferSize = (int)value; |
| | 0 | 131 | | } |
| | | 132 | | } |
| | | 133 | | |
| | | 134 | | #endregion Properties |
| | | 135 | | |
| | | 136 | | #region Constructors |
| | | 137 | | |
| | 0 | 138 | | public HttpClient() : this(new HttpClientHandler()) |
| | 0 | 139 | | { |
| | 0 | 140 | | } |
| | | 141 | | |
| | 0 | 142 | | public HttpClient(HttpMessageHandler handler) : this(handler, true) |
| | 0 | 143 | | { |
| | 0 | 144 | | } |
| | | 145 | | |
| | 0 | 146 | | public HttpClient(HttpMessageHandler handler, bool disposeHandler) : base(handler, disposeHandler) |
| | 0 | 147 | | { |
| | 0 | 148 | | _timeout = s_defaultTimeout; |
| | 0 | 149 | | _maxResponseContentBufferSize = HttpContent.MaxBufferSize; |
| | 0 | 150 | | _pendingRequestsCts = new CancellationTokenSource(); |
| | 0 | 151 | | } |
| | | 152 | | |
| | | 153 | | #endregion Constructors |
| | | 154 | | |
| | | 155 | | #region Public Send |
| | | 156 | | |
| | | 157 | | #region Simple Get Overloads |
| | | 158 | | |
| | | 159 | | public Task<string> GetStringAsync([StringSyntax(StringSyntaxAttribute.Uri)] string? requestUri) => |
| | 0 | 160 | | GetStringAsync(CreateUri(requestUri)); |
| | | 161 | | |
| | | 162 | | public Task<string> GetStringAsync(Uri? requestUri) => |
| | 0 | 163 | | GetStringAsync(requestUri, CancellationToken.None); |
| | | 164 | | |
| | | 165 | | public Task<string> GetStringAsync([StringSyntax(StringSyntaxAttribute.Uri)] string? requestUri, CancellationTok |
| | 0 | 166 | | GetStringAsync(CreateUri(requestUri), cancellationToken); |
| | | 167 | | |
| | | 168 | | public Task<string> GetStringAsync(Uri? requestUri, CancellationToken cancellationToken) |
| | 0 | 169 | | { |
| | 0 | 170 | | HttpRequestMessage request = CreateRequestMessage(HttpMethod.Get, requestUri); |
| | | 171 | | |
| | | 172 | | // Called outside of async state machine to propagate certain exception even without awaiting the returned t |
| | 0 | 173 | | CheckRequestBeforeSend(request); |
| | | 174 | | |
| | 0 | 175 | | return GetStringAsyncCore(request, cancellationToken); |
| | 0 | 176 | | } |
| | | 177 | | |
| | | 178 | | private async Task<string> GetStringAsyncCore(HttpRequestMessage request, CancellationToken cancellationToken) |
| | 0 | 179 | | { |
| | 0 | 180 | | bool telemetryStarted = StartSend(request); |
| | 0 | 181 | | bool responseContentTelemetryStarted = false; |
| | | 182 | | |
| | 0 | 183 | | (CancellationTokenSource cts, bool disposeCts, CancellationTokenSource pendingRequestsCts) = PrepareCancella |
| | 0 | 184 | | HttpResponseMessage? response = null; |
| | | 185 | | try |
| | 0 | 186 | | { |
| | | 187 | | // Wait for the response message and make sure it completed successfully. |
| | 0 | 188 | | response = await base.SendAsync(request, cts.Token).ConfigureAwait(false); |
| | 0 | 189 | | ThrowForNullResponse(response); |
| | 0 | 190 | | response.EnsureSuccessStatusCode(); |
| | | 191 | | |
| | | 192 | | // Get the response content. |
| | 0 | 193 | | HttpContent c = response.Content; |
| | 0 | 194 | | if (HttpTelemetry.Log.IsEnabled() && telemetryStarted) |
| | 0 | 195 | | { |
| | 0 | 196 | | HttpTelemetry.Log.ResponseContentStart(); |
| | 0 | 197 | | responseContentTelemetryStarted = true; |
| | 0 | 198 | | } |
| | | 199 | | |
| | | 200 | | // Since the underlying byte[] will never be exposed, we use an ArrayPool-backed |
| | | 201 | | // stream to which we copy all of the data from the response. |
| | 0 | 202 | | using var buffer = new HttpContent.LimitArrayPoolWriteStream( |
| | 0 | 203 | | _maxResponseContentBufferSize, |
| | 0 | 204 | | c.Headers.ContentLength.GetValueOrDefault(), |
| | 0 | 205 | | getFinalSizeFromPool: true); |
| | | 206 | | |
| | 0 | 207 | | using Stream responseStream = c.TryReadAsStream() ?? await c.ReadAsStreamAsync(cts.Token).ConfigureAwait |
| | | 208 | | try |
| | 0 | 209 | | { |
| | 0 | 210 | | await responseStream.CopyToAsync(buffer, cts.Token).ConfigureAwait(false); |
| | 0 | 211 | | } |
| | 0 | 212 | | catch (Exception e) when (HttpContent.StreamCopyExceptionNeedsWrapping(e)) |
| | 0 | 213 | | { |
| | 0 | 214 | | throw HttpContent.WrapStreamCopyException(e); |
| | | 215 | | } |
| | | 216 | | |
| | | 217 | | // Decode and return the data from the buffer. |
| | 0 | 218 | | return HttpContent.ReadBufferAsString(buffer, c.Headers); |
| | | 219 | | } |
| | 0 | 220 | | catch (Exception e) |
| | 0 | 221 | | { |
| | 0 | 222 | | HandleFailure(e, telemetryStarted, response, cts, cancellationToken, pendingRequestsCts); |
| | 0 | 223 | | throw; |
| | | 224 | | } |
| | | 225 | | finally |
| | 0 | 226 | | { |
| | 0 | 227 | | FinishSend(response, cts, disposeCts, telemetryStarted, responseContentTelemetryStarted); |
| | 0 | 228 | | } |
| | 0 | 229 | | } |
| | | 230 | | |
| | | 231 | | public Task<byte[]> GetByteArrayAsync([StringSyntax(StringSyntaxAttribute.Uri)] string? requestUri) => |
| | 0 | 232 | | GetByteArrayAsync(CreateUri(requestUri)); |
| | | 233 | | |
| | | 234 | | public Task<byte[]> GetByteArrayAsync(Uri? requestUri) => |
| | 0 | 235 | | GetByteArrayAsync(requestUri, CancellationToken.None); |
| | | 236 | | |
| | | 237 | | public Task<byte[]> GetByteArrayAsync([StringSyntax(StringSyntaxAttribute.Uri)] string? requestUri, Cancellation |
| | 0 | 238 | | GetByteArrayAsync(CreateUri(requestUri), cancellationToken); |
| | | 239 | | |
| | | 240 | | public Task<byte[]> GetByteArrayAsync(Uri? requestUri, CancellationToken cancellationToken) |
| | 0 | 241 | | { |
| | 0 | 242 | | HttpRequestMessage request = CreateRequestMessage(HttpMethod.Get, requestUri); |
| | | 243 | | |
| | | 244 | | // Called outside of async state machine to propagate certain exception even without awaiting the returned t |
| | 0 | 245 | | CheckRequestBeforeSend(request); |
| | | 246 | | |
| | 0 | 247 | | return GetByteArrayAsyncCore(request, cancellationToken); |
| | 0 | 248 | | } |
| | | 249 | | |
| | | 250 | | private async Task<byte[]> GetByteArrayAsyncCore(HttpRequestMessage request, CancellationToken cancellationToken |
| | 0 | 251 | | { |
| | 0 | 252 | | bool telemetryStarted = StartSend(request); |
| | 0 | 253 | | bool responseContentTelemetryStarted = false; |
| | | 254 | | |
| | 0 | 255 | | (CancellationTokenSource cts, bool disposeCts, CancellationTokenSource pendingRequestsCts) = PrepareCancella |
| | 0 | 256 | | HttpResponseMessage? response = null; |
| | | 257 | | try |
| | 0 | 258 | | { |
| | | 259 | | // Wait for the response message and make sure it completed successfully. |
| | 0 | 260 | | response = await base.SendAsync(request, cts.Token).ConfigureAwait(false); |
| | 0 | 261 | | ThrowForNullResponse(response); |
| | 0 | 262 | | response.EnsureSuccessStatusCode(); |
| | | 263 | | |
| | | 264 | | // Get the response content. |
| | 0 | 265 | | HttpContent c = response.Content; |
| | 0 | 266 | | if (HttpTelemetry.Log.IsEnabled() && telemetryStarted) |
| | 0 | 267 | | { |
| | 0 | 268 | | HttpTelemetry.Log.ResponseContentStart(); |
| | 0 | 269 | | responseContentTelemetryStarted = true; |
| | 0 | 270 | | } |
| | | 271 | | |
| | | 272 | | // If we got a content length, then we assume that it's correct. If that's the case, |
| | | 273 | | // we can opportunistically allocate the exact-sized buffer while buffering the content. |
| | | 274 | | // If we didn't get a content length, then we assume we're going to have to grow |
| | | 275 | | // the buffer potentially several times and that it's unlikely the underlying buffer |
| | | 276 | | // at the end will be the exact size needed, in which case it's more beneficial to use |
| | | 277 | | // ArrayPool buffers and copy out to a new array at the end. |
| | 0 | 278 | | using var buffer = new HttpContent.LimitArrayPoolWriteStream( |
| | 0 | 279 | | _maxResponseContentBufferSize, |
| | 0 | 280 | | c.Headers.ContentLength.GetValueOrDefault(), |
| | 0 | 281 | | getFinalSizeFromPool: false); |
| | | 282 | | |
| | 0 | 283 | | using Stream responseStream = c.TryReadAsStream() ?? await c.ReadAsStreamAsync(cts.Token).ConfigureAwait |
| | | 284 | | try |
| | 0 | 285 | | { |
| | 0 | 286 | | await responseStream.CopyToAsync(buffer, cts.Token).ConfigureAwait(false); |
| | 0 | 287 | | } |
| | 0 | 288 | | catch (Exception e) when (HttpContent.StreamCopyExceptionNeedsWrapping(e)) |
| | 0 | 289 | | { |
| | 0 | 290 | | throw HttpContent.WrapStreamCopyException(e); |
| | | 291 | | } |
| | | 292 | | |
| | 0 | 293 | | return buffer.ToArray(); |
| | | 294 | | } |
| | 0 | 295 | | catch (Exception e) |
| | 0 | 296 | | { |
| | 0 | 297 | | HandleFailure(e, telemetryStarted, response, cts, cancellationToken, pendingRequestsCts); |
| | 0 | 298 | | throw; |
| | | 299 | | } |
| | | 300 | | finally |
| | 0 | 301 | | { |
| | 0 | 302 | | FinishSend(response, cts, disposeCts, telemetryStarted, responseContentTelemetryStarted); |
| | 0 | 303 | | } |
| | 0 | 304 | | } |
| | | 305 | | |
| | | 306 | | public Task<Stream> GetStreamAsync([StringSyntax(StringSyntaxAttribute.Uri)] string? requestUri) => |
| | 0 | 307 | | GetStreamAsync(CreateUri(requestUri)); |
| | | 308 | | |
| | | 309 | | public Task<Stream> GetStreamAsync([StringSyntax(StringSyntaxAttribute.Uri)] string? requestUri, CancellationTok |
| | 0 | 310 | | GetStreamAsync(CreateUri(requestUri), cancellationToken); |
| | | 311 | | |
| | | 312 | | public Task<Stream> GetStreamAsync(Uri? requestUri) => |
| | 0 | 313 | | GetStreamAsync(requestUri, CancellationToken.None); |
| | | 314 | | |
| | | 315 | | public Task<Stream> GetStreamAsync(Uri? requestUri, CancellationToken cancellationToken) |
| | 0 | 316 | | { |
| | 0 | 317 | | HttpRequestMessage request = CreateRequestMessage(HttpMethod.Get, requestUri); |
| | | 318 | | |
| | | 319 | | // Called outside of async state machine to propagate certain exception even without awaiting the returned t |
| | 0 | 320 | | CheckRequestBeforeSend(request); |
| | | 321 | | |
| | 0 | 322 | | return GetStreamAsyncCore(request, cancellationToken); |
| | 0 | 323 | | } |
| | | 324 | | |
| | | 325 | | private async Task<Stream> GetStreamAsyncCore(HttpRequestMessage request, CancellationToken cancellationToken) |
| | 0 | 326 | | { |
| | 0 | 327 | | bool telemetryStarted = StartSend(request); |
| | | 328 | | |
| | 0 | 329 | | (CancellationTokenSource cts, bool disposeCts, CancellationTokenSource pendingRequestsCts) = PrepareCancella |
| | 0 | 330 | | HttpResponseMessage? response = null; |
| | | 331 | | try |
| | 0 | 332 | | { |
| | | 333 | | // Wait for the response message and make sure it completed successfully. |
| | 0 | 334 | | response = await base.SendAsync(request, cts.Token).ConfigureAwait(false); |
| | 0 | 335 | | ThrowForNullResponse(response); |
| | 0 | 336 | | response.EnsureSuccessStatusCode(); |
| | | 337 | | |
| | 0 | 338 | | HttpContent c = response.Content; |
| | 0 | 339 | | return c.TryReadAsStream() ?? await c.ReadAsStreamAsync(cancellationToken).ConfigureAwait(false); |
| | | 340 | | } |
| | 0 | 341 | | catch (Exception e) |
| | 0 | 342 | | { |
| | 0 | 343 | | HandleFailure(e, telemetryStarted, response, cts, cancellationToken, pendingRequestsCts); |
| | 0 | 344 | | throw; |
| | | 345 | | } |
| | | 346 | | finally |
| | 0 | 347 | | { |
| | 0 | 348 | | FinishSend(response, cts, disposeCts, telemetryStarted, responseContentTelemetryStarted: false); |
| | 0 | 349 | | } |
| | 0 | 350 | | } |
| | | 351 | | |
| | | 352 | | #endregion Simple Get Overloads |
| | | 353 | | |
| | | 354 | | #region REST Send Overloads |
| | | 355 | | |
| | | 356 | | public Task<HttpResponseMessage> GetAsync([StringSyntax(StringSyntaxAttribute.Uri)] string? requestUri) => |
| | 0 | 357 | | GetAsync(CreateUri(requestUri)); |
| | | 358 | | |
| | | 359 | | public Task<HttpResponseMessage> GetAsync(Uri? requestUri) => |
| | 0 | 360 | | GetAsync(requestUri, DefaultCompletionOption); |
| | | 361 | | |
| | | 362 | | public Task<HttpResponseMessage> GetAsync([StringSyntax(StringSyntaxAttribute.Uri)] string? requestUri, HttpComp |
| | 0 | 363 | | GetAsync(CreateUri(requestUri), completionOption); |
| | | 364 | | |
| | | 365 | | public Task<HttpResponseMessage> GetAsync(Uri? requestUri, HttpCompletionOption completionOption) => |
| | 0 | 366 | | GetAsync(requestUri, completionOption, CancellationToken.None); |
| | | 367 | | |
| | | 368 | | public Task<HttpResponseMessage> GetAsync([StringSyntax(StringSyntaxAttribute.Uri)] string? requestUri, Cancella |
| | 0 | 369 | | GetAsync(CreateUri(requestUri), cancellationToken); |
| | | 370 | | |
| | | 371 | | public Task<HttpResponseMessage> GetAsync(Uri? requestUri, CancellationToken cancellationToken) => |
| | 0 | 372 | | GetAsync(requestUri, DefaultCompletionOption, cancellationToken); |
| | | 373 | | |
| | | 374 | | public Task<HttpResponseMessage> GetAsync([StringSyntax(StringSyntaxAttribute.Uri)] string? requestUri, HttpComp |
| | 0 | 375 | | GetAsync(CreateUri(requestUri), completionOption, cancellationToken); |
| | | 376 | | |
| | | 377 | | public Task<HttpResponseMessage> GetAsync(Uri? requestUri, HttpCompletionOption completionOption, CancellationTo |
| | 0 | 378 | | SendAsync(CreateRequestMessage(HttpMethod.Get, requestUri), completionOption, cancellationToken); |
| | | 379 | | |
| | | 380 | | public Task<HttpResponseMessage> PostAsync([StringSyntax(StringSyntaxAttribute.Uri)] string? requestUri, HttpCon |
| | 0 | 381 | | PostAsync(CreateUri(requestUri), content); |
| | | 382 | | |
| | | 383 | | public Task<HttpResponseMessage> PostAsync(Uri? requestUri, HttpContent? content) => |
| | 0 | 384 | | PostAsync(requestUri, content, CancellationToken.None); |
| | | 385 | | |
| | | 386 | | public Task<HttpResponseMessage> PostAsync([StringSyntax(StringSyntaxAttribute.Uri)] string? requestUri, HttpCon |
| | 0 | 387 | | PostAsync(CreateUri(requestUri), content, cancellationToken); |
| | | 388 | | |
| | | 389 | | public Task<HttpResponseMessage> PostAsync(Uri? requestUri, HttpContent? content, CancellationToken cancellation |
| | 0 | 390 | | { |
| | 0 | 391 | | HttpRequestMessage request = CreateRequestMessage(HttpMethod.Post, requestUri); |
| | 0 | 392 | | request.Content = content; |
| | 0 | 393 | | return SendAsync(request, cancellationToken); |
| | 0 | 394 | | } |
| | | 395 | | |
| | | 396 | | public Task<HttpResponseMessage> PutAsync([StringSyntax(StringSyntaxAttribute.Uri)] string? requestUri, HttpCont |
| | 0 | 397 | | PutAsync(CreateUri(requestUri), content); |
| | | 398 | | |
| | | 399 | | public Task<HttpResponseMessage> PutAsync(Uri? requestUri, HttpContent? content) => |
| | 0 | 400 | | PutAsync(requestUri, content, CancellationToken.None); |
| | | 401 | | |
| | | 402 | | public Task<HttpResponseMessage> PutAsync([StringSyntax(StringSyntaxAttribute.Uri)] string? requestUri, HttpCont |
| | 0 | 403 | | PutAsync(CreateUri(requestUri), content, cancellationToken); |
| | | 404 | | |
| | | 405 | | public Task<HttpResponseMessage> PutAsync(Uri? requestUri, HttpContent? content, CancellationToken cancellationT |
| | 0 | 406 | | { |
| | 0 | 407 | | HttpRequestMessage request = CreateRequestMessage(HttpMethod.Put, requestUri); |
| | 0 | 408 | | request.Content = content; |
| | 0 | 409 | | return SendAsync(request, cancellationToken); |
| | 0 | 410 | | } |
| | | 411 | | |
| | | 412 | | public Task<HttpResponseMessage> PatchAsync([StringSyntax(StringSyntaxAttribute.Uri)] string? requestUri, HttpCo |
| | 0 | 413 | | PatchAsync(CreateUri(requestUri), content); |
| | | 414 | | |
| | | 415 | | public Task<HttpResponseMessage> PatchAsync(Uri? requestUri, HttpContent? content) => |
| | 0 | 416 | | PatchAsync(requestUri, content, CancellationToken.None); |
| | | 417 | | |
| | | 418 | | public Task<HttpResponseMessage> PatchAsync([StringSyntax(StringSyntaxAttribute.Uri)] string? requestUri, HttpCo |
| | 0 | 419 | | PatchAsync(CreateUri(requestUri), content, cancellationToken); |
| | | 420 | | |
| | | 421 | | public Task<HttpResponseMessage> PatchAsync(Uri? requestUri, HttpContent? content, CancellationToken cancellatio |
| | 0 | 422 | | { |
| | 0 | 423 | | HttpRequestMessage request = CreateRequestMessage(HttpMethod.Patch, requestUri); |
| | 0 | 424 | | request.Content = content; |
| | 0 | 425 | | return SendAsync(request, cancellationToken); |
| | 0 | 426 | | } |
| | | 427 | | |
| | | 428 | | public Task<HttpResponseMessage> DeleteAsync([StringSyntax(StringSyntaxAttribute.Uri)] string? requestUri) => |
| | 0 | 429 | | DeleteAsync(CreateUri(requestUri)); |
| | | 430 | | |
| | | 431 | | public Task<HttpResponseMessage> DeleteAsync(Uri? requestUri) => |
| | 0 | 432 | | DeleteAsync(requestUri, CancellationToken.None); |
| | | 433 | | |
| | | 434 | | public Task<HttpResponseMessage> DeleteAsync([StringSyntax(StringSyntaxAttribute.Uri)] string? requestUri, Cance |
| | 0 | 435 | | DeleteAsync(CreateUri(requestUri), cancellationToken); |
| | | 436 | | |
| | | 437 | | public Task<HttpResponseMessage> DeleteAsync(Uri? requestUri, CancellationToken cancellationToken) => |
| | 0 | 438 | | SendAsync(CreateRequestMessage(HttpMethod.Delete, requestUri), cancellationToken); |
| | | 439 | | |
| | | 440 | | #endregion REST Send Overloads |
| | | 441 | | |
| | | 442 | | #region Advanced Send Overloads |
| | | 443 | | |
| | | 444 | | [UnsupportedOSPlatform("android")] |
| | | 445 | | [UnsupportedOSPlatform("browser")] |
| | | 446 | | [UnsupportedOSPlatform("ios")] |
| | | 447 | | [UnsupportedOSPlatform("tvos")] |
| | | 448 | | public HttpResponseMessage Send(HttpRequestMessage request) => |
| | 0 | 449 | | Send(request, DefaultCompletionOption, cancellationToken: default); |
| | | 450 | | |
| | | 451 | | [UnsupportedOSPlatform("android")] |
| | | 452 | | [UnsupportedOSPlatform("browser")] |
| | | 453 | | [UnsupportedOSPlatform("ios")] |
| | | 454 | | [UnsupportedOSPlatform("tvos")] |
| | | 455 | | public HttpResponseMessage Send(HttpRequestMessage request, HttpCompletionOption completionOption) => |
| | 0 | 456 | | Send(request, completionOption, cancellationToken: default); |
| | | 457 | | |
| | | 458 | | [UnsupportedOSPlatform("android")] |
| | | 459 | | [UnsupportedOSPlatform("browser")] |
| | | 460 | | [UnsupportedOSPlatform("ios")] |
| | | 461 | | [UnsupportedOSPlatform("tvos")] |
| | | 462 | | public override HttpResponseMessage Send(HttpRequestMessage request, CancellationToken cancellationToken) => |
| | 0 | 463 | | Send(request, DefaultCompletionOption, cancellationToken); |
| | | 464 | | |
| | | 465 | | [UnsupportedOSPlatform("android")] |
| | | 466 | | [UnsupportedOSPlatform("browser")] |
| | | 467 | | [UnsupportedOSPlatform("ios")] |
| | | 468 | | [UnsupportedOSPlatform("tvos")] |
| | | 469 | | public HttpResponseMessage Send(HttpRequestMessage request, HttpCompletionOption completionOption, CancellationT |
| | 0 | 470 | | { |
| | 0 | 471 | | CheckRequestBeforeSend(request); |
| | 0 | 472 | | (CancellationTokenSource cts, bool disposeCts, CancellationTokenSource pendingRequestsCts) = PrepareCancella |
| | | 473 | | |
| | 0 | 474 | | bool telemetryStarted = StartSend(request); |
| | 0 | 475 | | bool responseContentTelemetryStarted = false; |
| | 0 | 476 | | HttpResponseMessage? response = null; |
| | | 477 | | try |
| | 0 | 478 | | { |
| | | 479 | | // Wait for the send request to complete, getting back the response. |
| | 0 | 480 | | response = base.Send(request, cts.Token); |
| | 0 | 481 | | ThrowForNullResponse(response); |
| | | 482 | | |
| | | 483 | | // Buffer the response content if we've been asked to. |
| | 0 | 484 | | if (ShouldBufferResponse(completionOption, request)) |
| | 0 | 485 | | { |
| | 0 | 486 | | if (HttpTelemetry.Log.IsEnabled() && telemetryStarted) |
| | 0 | 487 | | { |
| | 0 | 488 | | HttpTelemetry.Log.ResponseContentStart(); |
| | 0 | 489 | | responseContentTelemetryStarted = true; |
| | 0 | 490 | | } |
| | | 491 | | |
| | 0 | 492 | | response.Content.LoadIntoBuffer(_maxResponseContentBufferSize, cts.Token); |
| | 0 | 493 | | } |
| | | 494 | | |
| | 0 | 495 | | return response; |
| | | 496 | | } |
| | 0 | 497 | | catch (Exception e) |
| | 0 | 498 | | { |
| | 0 | 499 | | HandleFailure(e, telemetryStarted, response, cts, cancellationToken, pendingRequestsCts); |
| | 0 | 500 | | throw; |
| | | 501 | | } |
| | | 502 | | finally |
| | 0 | 503 | | { |
| | 0 | 504 | | FinishSend(response, cts, disposeCts, telemetryStarted, responseContentTelemetryStarted); |
| | 0 | 505 | | } |
| | 0 | 506 | | } |
| | | 507 | | |
| | | 508 | | public Task<HttpResponseMessage> SendAsync(HttpRequestMessage request) => |
| | 0 | 509 | | SendAsync(request, DefaultCompletionOption, CancellationToken.None); |
| | | 510 | | |
| | | 511 | | public override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationTo |
| | 0 | 512 | | SendAsync(request, DefaultCompletionOption, cancellationToken); |
| | | 513 | | |
| | | 514 | | public Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, HttpCompletionOption completionOption) => |
| | 0 | 515 | | SendAsync(request, completionOption, CancellationToken.None); |
| | | 516 | | |
| | | 517 | | public Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, HttpCompletionOption completionOption, Ca |
| | 0 | 518 | | { |
| | | 519 | | // Called outside of async state machine to propagate certain exception even without awaiting the returned t |
| | 0 | 520 | | CheckRequestBeforeSend(request); |
| | 0 | 521 | | (CancellationTokenSource cts, bool disposeCts, CancellationTokenSource pendingRequestsCts) = PrepareCancella |
| | | 522 | | |
| | 0 | 523 | | return Core(request, completionOption, cts, disposeCts, pendingRequestsCts, cancellationToken); |
| | | 524 | | |
| | | 525 | | async Task<HttpResponseMessage> Core( |
| | | 526 | | HttpRequestMessage request, HttpCompletionOption completionOption, |
| | | 527 | | CancellationTokenSource cts, bool disposeCts, CancellationTokenSource pendingRequestsCts, CancellationTo |
| | 0 | 528 | | { |
| | 0 | 529 | | bool telemetryStarted = StartSend(request); |
| | 0 | 530 | | bool responseContentTelemetryStarted = false; |
| | 0 | 531 | | HttpResponseMessage? response = null; |
| | | 532 | | try |
| | 0 | 533 | | { |
| | | 534 | | // Wait for the send request to complete, getting back the response. |
| | 0 | 535 | | response = await base.SendAsync(request, cts.Token).ConfigureAwait(false); |
| | 0 | 536 | | ThrowForNullResponse(response); |
| | | 537 | | |
| | | 538 | | // Buffer the response content if we've been asked to. |
| | 0 | 539 | | if (ShouldBufferResponse(completionOption, request)) |
| | 0 | 540 | | { |
| | 0 | 541 | | if (HttpTelemetry.Log.IsEnabled() && telemetryStarted) |
| | 0 | 542 | | { |
| | 0 | 543 | | HttpTelemetry.Log.ResponseContentStart(); |
| | 0 | 544 | | responseContentTelemetryStarted = true; |
| | 0 | 545 | | } |
| | | 546 | | |
| | 0 | 547 | | await response.Content.LoadIntoBufferAsync(_maxResponseContentBufferSize, cts.Token).ConfigureAw |
| | 0 | 548 | | } |
| | | 549 | | |
| | 0 | 550 | | return response; |
| | | 551 | | } |
| | 0 | 552 | | catch (Exception e) |
| | 0 | 553 | | { |
| | 0 | 554 | | HandleFailure(e, telemetryStarted, response, cts, originalCancellationToken, pendingRequestsCts); |
| | 0 | 555 | | throw; |
| | | 556 | | } |
| | | 557 | | finally |
| | 0 | 558 | | { |
| | 0 | 559 | | FinishSend(response, cts, disposeCts, telemetryStarted, responseContentTelemetryStarted); |
| | 0 | 560 | | } |
| | 0 | 561 | | } |
| | 0 | 562 | | } |
| | | 563 | | |
| | | 564 | | private void CheckRequestBeforeSend(HttpRequestMessage request) |
| | 0 | 565 | | { |
| | 0 | 566 | | ArgumentNullException.ThrowIfNull(request); |
| | | 567 | | |
| | 0 | 568 | | ObjectDisposedException.ThrowIf(_disposed, this); |
| | 0 | 569 | | CheckRequestMessage(request); |
| | | 570 | | |
| | 0 | 571 | | SetOperationStarted(); |
| | | 572 | | |
| | | 573 | | // PrepareRequestMessage will resolve the request address against the base address. |
| | 0 | 574 | | PrepareRequestMessage(request); |
| | 0 | 575 | | } |
| | | 576 | | |
| | | 577 | | private static void ThrowForNullResponse([NotNull] HttpResponseMessage? response) |
| | 0 | 578 | | { |
| | 0 | 579 | | if (response is null) |
| | 0 | 580 | | { |
| | 0 | 581 | | throw new InvalidOperationException(SR.net_http_handler_noresponse); |
| | | 582 | | } |
| | 0 | 583 | | } |
| | | 584 | | |
| | | 585 | | private static bool ShouldBufferResponse(HttpCompletionOption completionOption, HttpRequestMessage request) => |
| | 0 | 586 | | completionOption == HttpCompletionOption.ResponseContentRead && |
| | 0 | 587 | | !string.Equals(request.Method.Method, "HEAD", StringComparison.OrdinalIgnoreCase); |
| | | 588 | | |
| | | 589 | | private void HandleFailure(Exception e, bool telemetryStarted, HttpResponseMessage? response, CancellationTokenS |
| | 0 | 590 | | { |
| | 0 | 591 | | response?.Dispose(); |
| | | 592 | | |
| | 0 | 593 | | Exception? toThrow = null; |
| | | 594 | | |
| | 0 | 595 | | if (e is OperationCanceledException oce) |
| | 0 | 596 | | { |
| | 0 | 597 | | if (cancellationToken.IsCancellationRequested) |
| | 0 | 598 | | { |
| | 0 | 599 | | if (oce.CancellationToken != cancellationToken) |
| | 0 | 600 | | { |
| | | 601 | | // We got a cancellation exception, and the caller requested cancellation, but the exception doe |
| | | 602 | | // Massage things so that the cancellation exception we propagate appropriately contains the cal |
| | | 603 | | // multiple things caused cancellation, in which case we can attribute it to the caller's token, |
| | | 604 | | // exception contains the linked token source, in which case that token isn't meaningful to the |
| | 0 | 605 | | e = toThrow = new TaskCanceledException(oce.Message, oce, cancellationToken); |
| | 0 | 606 | | } |
| | 0 | 607 | | } |
| | 0 | 608 | | else if (cts.IsCancellationRequested && !pendingRequestsCts.IsCancellationRequested) |
| | 0 | 609 | | { |
| | | 610 | | // If the linked cancellation token source was canceled, but cancellation wasn't requested, either b |
| | | 611 | | // the only other cause could be a timeout. Treat it as such. |
| | | 612 | | |
| | | 613 | | // cancellationToken could have been triggered right after we checked it, but before we checked the |
| | | 614 | | // We must check it again to avoid reporting a timeout when one did not occur. |
| | 0 | 615 | | if (!cancellationToken.IsCancellationRequested) |
| | 0 | 616 | | { |
| | 0 | 617 | | Debug.Assert(_timeout.TotalSeconds > 0); |
| | 0 | 618 | | e = toThrow = new TaskCanceledException(SR.Format(SR.net_http_request_timedout, _timeout.TotalSe |
| | 0 | 619 | | } |
| | 0 | 620 | | } |
| | 0 | 621 | | } |
| | 0 | 622 | | else if (e is HttpRequestException && cts.IsCancellationRequested) // if cancellationToken is canceled, cts |
| | 0 | 623 | | { |
| | | 624 | | // If the cancellation token source was canceled, race conditions abound, and we consider the failure to |
| | | 625 | | // caused by the cancellation (e.g. WebException when reading from canceled response stream). |
| | 0 | 626 | | e = toThrow = CancellationHelper.CreateOperationCanceledException(e, cancellationToken.IsCancellationReq |
| | 0 | 627 | | } |
| | | 628 | | |
| | 0 | 629 | | LogRequestFailed(e, telemetryStarted); |
| | | 630 | | |
| | 0 | 631 | | if (NetEventSource.Log.IsEnabled()) NetEventSource.Error(this, e); |
| | | 632 | | |
| | 0 | 633 | | if (toThrow != null) |
| | 0 | 634 | | { |
| | 0 | 635 | | throw toThrow; |
| | | 636 | | } |
| | 0 | 637 | | } |
| | | 638 | | |
| | | 639 | | private static bool StartSend(HttpRequestMessage request) |
| | 0 | 640 | | { |
| | 0 | 641 | | if (HttpTelemetry.Log.IsEnabled()) |
| | 0 | 642 | | { |
| | 0 | 643 | | HttpTelemetry.Log.RequestStart(request); |
| | 0 | 644 | | return true; |
| | | 645 | | } |
| | | 646 | | |
| | 0 | 647 | | return false; |
| | 0 | 648 | | } |
| | | 649 | | |
| | | 650 | | private static void FinishSend(HttpResponseMessage? response, CancellationTokenSource cts, bool disposeCts, bool |
| | 0 | 651 | | { |
| | | 652 | | // Log completion. |
| | 0 | 653 | | if (HttpTelemetry.Log.IsEnabled() && telemetryStarted) |
| | 0 | 654 | | { |
| | 0 | 655 | | if (responseContentTelemetryStarted) |
| | 0 | 656 | | { |
| | 0 | 657 | | HttpTelemetry.Log.ResponseContentStop(); |
| | 0 | 658 | | } |
| | | 659 | | |
| | 0 | 660 | | HttpTelemetry.Log.RequestStop(response); |
| | 0 | 661 | | } |
| | | 662 | | |
| | | 663 | | // Dispose of the CancellationTokenSource if it was created specially for this request |
| | | 664 | | // rather than being used across multiple requests. |
| | 0 | 665 | | if (disposeCts) |
| | 0 | 666 | | { |
| | 0 | 667 | | cts.Dispose(); |
| | 0 | 668 | | } |
| | | 669 | | |
| | | 670 | | // This method used to also dispose of the request content, e.g.: |
| | | 671 | | // request.Content?.Dispose(); |
| | | 672 | | // This has multiple problems: |
| | | 673 | | // 1. It prevents code from reusing request content objects for subsequent requests, |
| | | 674 | | // as disposing of the object likely invalidates it for further use. |
| | | 675 | | // 2. It prevents the possibility of partial or full duplex communication, even if supported |
| | | 676 | | // by the handler, as the request content may still be in use even if the response |
| | | 677 | | // (or response headers) has been received. |
| | | 678 | | // By changing this to not dispose of the request content, disposal may end up being |
| | | 679 | | // left for the finalizer to handle, or the developer can explicitly dispose of the |
| | | 680 | | // content when they're done with it. But it allows request content to be reused, |
| | | 681 | | // and more importantly it enables handlers that allow receiving of the response before |
| | | 682 | | // fully sending the request. Prior to this change, a handler that supported duplex communication |
| | | 683 | | // would fail trying to access certain sites, if the site sent its response before it had |
| | | 684 | | // completely received the request: CurlHandler might then find that the request content |
| | | 685 | | // was disposed of while it still needed to read from it. |
| | 0 | 686 | | } |
| | | 687 | | |
| | | 688 | | public void CancelPendingRequests() |
| | 0 | 689 | | { |
| | 0 | 690 | | ObjectDisposedException.ThrowIf(_disposed, this); |
| | | 691 | | |
| | | 692 | | // With every request we link this cancellation token source. |
| | 0 | 693 | | CancellationTokenSource currentCts = Interlocked.Exchange(ref _pendingRequestsCts, new CancellationTokenSour |
| | | 694 | | |
| | 0 | 695 | | currentCts.Cancel(); |
| | 0 | 696 | | currentCts.Dispose(); |
| | 0 | 697 | | } |
| | | 698 | | |
| | | 699 | | #endregion Advanced Send Overloads |
| | | 700 | | |
| | | 701 | | #endregion Public Send |
| | | 702 | | |
| | | 703 | | #region IDisposable Members |
| | | 704 | | |
| | | 705 | | protected override void Dispose(bool disposing) |
| | 0 | 706 | | { |
| | 0 | 707 | | if (disposing && !_disposed) |
| | 0 | 708 | | { |
| | 0 | 709 | | _disposed = true; |
| | | 710 | | |
| | | 711 | | // Cancel all pending requests (if any). Note that we don't call CancelPendingRequests() but cancel |
| | | 712 | | // the CTS directly. The reason is that CancelPendingRequests() would cancel the current CTS and create |
| | | 713 | | // a new CTS. We don't want a new CTS in this case. |
| | 0 | 714 | | _pendingRequestsCts.Cancel(); |
| | 0 | 715 | | _pendingRequestsCts.Dispose(); |
| | 0 | 716 | | } |
| | | 717 | | |
| | 0 | 718 | | base.Dispose(disposing); |
| | 0 | 719 | | } |
| | | 720 | | |
| | | 721 | | #endregion |
| | | 722 | | |
| | | 723 | | #region Private Helpers |
| | | 724 | | |
| | | 725 | | private void SetOperationStarted() |
| | 0 | 726 | | { |
| | | 727 | | // This method flags the HttpClient instances as "active". I.e. we executed at least one request (or are |
| | | 728 | | // in the process of doing so). This information is used to lock-down all property setters. Once a |
| | | 729 | | // Send/SendAsync operation started, no property can be changed. |
| | 0 | 730 | | if (!_operationStarted) |
| | 0 | 731 | | { |
| | 0 | 732 | | _operationStarted = true; |
| | 0 | 733 | | } |
| | 0 | 734 | | } |
| | | 735 | | |
| | | 736 | | private void CheckDisposedOrStarted() |
| | 0 | 737 | | { |
| | 0 | 738 | | ObjectDisposedException.ThrowIf(_disposed, this); |
| | 0 | 739 | | if (_operationStarted) |
| | 0 | 740 | | { |
| | 0 | 741 | | throw new InvalidOperationException(SR.net_http_operation_started); |
| | | 742 | | } |
| | 0 | 743 | | } |
| | | 744 | | |
| | | 745 | | private static void CheckRequestMessage(HttpRequestMessage request) |
| | 0 | 746 | | { |
| | 0 | 747 | | if (!request.MarkAsSent()) |
| | 0 | 748 | | { |
| | 0 | 749 | | throw new InvalidOperationException(SR.net_http_client_request_already_sent); |
| | | 750 | | } |
| | 0 | 751 | | } |
| | | 752 | | |
| | | 753 | | private void PrepareRequestMessage(HttpRequestMessage request) |
| | 0 | 754 | | { |
| | 0 | 755 | | Uri? requestUri = null; |
| | 0 | 756 | | if ((request.RequestUri == null) && (_baseAddress == null)) |
| | 0 | 757 | | { |
| | 0 | 758 | | throw new InvalidOperationException(SR.net_http_client_invalid_requesturi); |
| | | 759 | | } |
| | 0 | 760 | | if (request.RequestUri == null) |
| | 0 | 761 | | { |
| | 0 | 762 | | requestUri = _baseAddress; |
| | 0 | 763 | | } |
| | | 764 | | else |
| | 0 | 765 | | { |
| | | 766 | | // If the request Uri is an absolute Uri, just use it. Otherwise try to combine it with the base Uri. |
| | 0 | 767 | | if (!request.RequestUri.IsAbsoluteUri) |
| | 0 | 768 | | { |
| | 0 | 769 | | if (_baseAddress == null) |
| | 0 | 770 | | { |
| | 0 | 771 | | throw new InvalidOperationException(SR.net_http_client_invalid_requesturi); |
| | | 772 | | } |
| | | 773 | | else |
| | 0 | 774 | | { |
| | 0 | 775 | | requestUri = new Uri(_baseAddress, request.RequestUri); |
| | 0 | 776 | | } |
| | 0 | 777 | | } |
| | 0 | 778 | | } |
| | | 779 | | |
| | | 780 | | // We modified the original request Uri. Assign the new Uri to the request message. |
| | 0 | 781 | | if (requestUri != null) |
| | 0 | 782 | | { |
| | 0 | 783 | | request.RequestUri = requestUri; |
| | 0 | 784 | | } |
| | | 785 | | |
| | | 786 | | // Add default headers |
| | 0 | 787 | | if (_defaultRequestHeaders != null) |
| | 0 | 788 | | { |
| | 0 | 789 | | request.Headers.AddHeaders(_defaultRequestHeaders); |
| | 0 | 790 | | } |
| | 0 | 791 | | } |
| | | 792 | | |
| | | 793 | | private (CancellationTokenSource TokenSource, bool DisposeTokenSource, CancellationTokenSource PendingRequestsCt |
| | 0 | 794 | | { |
| | | 795 | | // We need a CancellationTokenSource to use with the request. We always have the global |
| | | 796 | | // _pendingRequestsCts to use, plus we may have a token provided by the caller, and we may |
| | | 797 | | // have a timeout. If we have a timeout or a caller-provided token, we need to create a new |
| | | 798 | | // CTS (we can't, for example, timeout the pending requests CTS, as that could cancel other |
| | | 799 | | // unrelated operations). Otherwise, we can use the pending requests CTS directly. |
| | | 800 | | |
| | | 801 | | // Snapshot the current pending requests cancellation source. It can change concurrently due to cancellation |
| | | 802 | | // and it being replaced, and we need a stable view of it: if cancellation occurs and the caller's token has |
| | | 803 | | // it's either due to this source or due to the timeout, and checking whether this source is the culprit is |
| | | 804 | | // it's more approximate checking elapsed time. |
| | 0 | 805 | | CancellationTokenSource pendingRequestsCts = _pendingRequestsCts; |
| | | 806 | | |
| | 0 | 807 | | bool hasTimeout = _timeout != s_infiniteTimeout; |
| | 0 | 808 | | if (hasTimeout || cancellationToken.CanBeCanceled) |
| | 0 | 809 | | { |
| | 0 | 810 | | CancellationTokenSource cts = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken, pending |
| | 0 | 811 | | if (hasTimeout) |
| | 0 | 812 | | { |
| | 0 | 813 | | cts.CancelAfter(_timeout); |
| | 0 | 814 | | } |
| | | 815 | | |
| | 0 | 816 | | return (cts, DisposeTokenSource: true, pendingRequestsCts); |
| | | 817 | | } |
| | | 818 | | |
| | 0 | 819 | | return (pendingRequestsCts, DisposeTokenSource: false, pendingRequestsCts); |
| | 0 | 820 | | } |
| | | 821 | | |
| | | 822 | | private static Uri? CreateUri(string? uri) => |
| | 0 | 823 | | string.IsNullOrEmpty(uri) ? null : new Uri(uri, UriKind.RelativeOrAbsolute); |
| | | 824 | | |
| | | 825 | | private HttpRequestMessage CreateRequestMessage(HttpMethod method, Uri? uri) => |
| | 0 | 826 | | new HttpRequestMessage(method, uri) { Version = _defaultRequestVersion, VersionPolicy = _defaultVersionPolic |
| | | 827 | | #endregion Private Helpers |
| | | 828 | | } |
| | | 829 | | } |