private static void UpdateRedirectRequest( HttpResponseMessage response, HttpRequestMessage redirect, HttpContent?originalContent) { Debug.Assert(response.RequestMessage is not null); var location = response.Headers.Location; if (location != null) { if (!location.IsAbsoluteUri && response.RequestMessage.RequestUri is Uri requestUri) { location = new Uri(requestUri, location); } redirect.RequestUri = location; } if (!ShouldKeepVerb(response)) { redirect.Method = HttpMethod.Get; } else { redirect.Method = response.RequestMessage.Method; redirect.Content = originalContent; } foreach (var property in response.RequestMessage.Options) { var key = new HttpRequestOptionsKey <object?>(property.Key); redirect.Options.Set(key, property.Value); } }
public override Task ProcessHttpRequestAsync(HttpRequestMessage request, CancellationToken cancellationToken) { if (string.IsNullOrEmpty(_sasToken)) { Action <string> action = (value) => { #if NETSTANDARD2_1_OR_GREATER || NET5_0_OR_GREATER _sasToken = value.Replace(SASHeaderName + " ", "", StringComparison.Ordinal); #else _sasToken = value.Replace(SASHeaderName + " ", ""); #endif SetAuthorizationHeader(request, _sasToken); }; #if NET5_0_OR_GREATER HttpRequestOptions requestOptions = request.Options; var requestOptionsKey = new HttpRequestOptionsKey <Action <string> >(TpmDelegatingHandler.ProvisioningHeaderName); requestOptions.Set(requestOptionsKey, action); #else request.Properties.Add(TpmDelegatingHandler.ProvisioningHeaderName, action); #endif } else { SetAuthorizationHeader(request, _sasToken); } return(base.ProcessHttpRequestAsync(request, cancellationToken)); }
private static void UpdateRedirectRequest( HttpResponseMessage response, HttpRequestMessage redirect, HttpContent originalContent) { var location = response.Headers.Location; if (!location.IsAbsoluteUri) { location = new Uri( new Uri(response.RequestMessage.RequestUri.GetLeftPart(UriPartial.Authority)), location); } redirect.RequestUri = location; if (!ShouldKeepVerb(response)) { redirect.Method = HttpMethod.Get; } else { redirect.Method = response.RequestMessage.Method; redirect.Content = originalContent; } foreach (var property in response.RequestMessage.Options) { var key = new HttpRequestOptionsKey <object>(property.Key); redirect.Options.Set(key, property.Value); } }
public async Task BrowserHttpHandler_Streaming() { var WebAssemblyEnableStreamingResponseKey = new HttpRequestOptionsKey <bool>("WebAssemblyEnableStreamingResponse"); var size = 1500 * 1024 * 1024; var req = new HttpRequestMessage(HttpMethod.Get, Configuration.Http.RemoteSecureHttp11Server.BaseUri + "large.ashx?size=" + size); req.Options.Set(WebAssemblyEnableStreamingResponseKey, true); using (HttpClient client = CreateHttpClientForRemoteServer(Configuration.Http.RemoteSecureHttp11Server)) // we need to switch off Response buffering of default ResponseContentRead option using (HttpResponseMessage response = await client.SendAsync(req, HttpCompletionOption.ResponseHeadersRead)) { Assert.Equal(typeof(StreamContent), response.Content.GetType()); Assert.Equal("application/octet-stream", response.Content.Headers.ContentType.MediaType); Assert.True(size == response.Content.Headers.ContentLength, "ContentLength"); var stream = await response.Content.ReadAsStreamAsync(); Assert.Equal("ReadOnlyStream", stream.GetType().Name); var buffer = new byte[1024 * 1024]; int totalCount = 0; int fetchedCount = 0; do { // with WebAssemblyEnableStreamingResponse option set, we will be using https://developer.mozilla.org/en-US/docs/Web/API/ReadableStreamDefaultReader/read fetchedCount = await stream.ReadAsync(buffer, 0, buffer.Length); totalCount += fetchedCount; } while (fetchedCount != 0); Assert.Equal(size, totalCount); } }
public async Task <TimeSpan> GetResponseTimeAsync(string uri) { var client = _httpClientFactory.CreateClient(AppConstants.Measuring); var request = new HttpRequestMessage(HttpMethod.Get, uri); await client.SendAsync(request); HttpRequestOptionsKey <Stopwatch> key = new HttpRequestOptionsKey <Stopwatch>(AppConstants.StopWatch); request.Options.TryGetValue(key, out var watcher); return(watcher.Elapsed); }
protected override async Task <HttpResponseMessage> SendAsync( HttpRequestMessage request, CancellationToken cancellationToken) { var watcher = new Stopwatch(); HttpRequestOptionsKey <Stopwatch> key = new HttpRequestOptionsKey <Stopwatch>(AppConstants.StopWatch); request.Options.Set(key, watcher); watcher.Start(); var response = await base.SendAsync(request, cancellationToken); watcher.Stop(); return(response); }
public async Task GetAsync_CancelDuringResponseBodyReceived_Unbuffered_TaskCanceledQuickly(bool chunkedTransfer, bool connectionClose, bool readOrCopyToAsync) { if (LoopbackServerFactory.Version >= HttpVersion20.Value && (chunkedTransfer || connectionClose)) { // There is no chunked encoding or connection header in HTTP/2 and later return; } if (IsWinHttpHandler && UseVersion >= HttpVersion20.Value) { return; } using (HttpClient client = CreateHttpClient()) { client.Timeout = Timeout.InfiniteTimeSpan; var cts = new CancellationTokenSource(); await LoopbackServerFactory.CreateServerAsync(async (server, url) => { var clientFinished = new TaskCompletionSource <bool>(); Task serverTask = server.AcceptConnectionAsync(async connection => { var headers = new List <HttpHeaderData>(); headers.Add(chunkedTransfer ? new HttpHeaderData("Transfer-Encoding", "chunked") : new HttpHeaderData("Content-Length", "20")); if (connectionClose) { headers.Add(new HttpHeaderData("Connection", "close")); } await connection.ReadRequestDataAsync(); await connection.SendResponseAsync(HttpStatusCode.OK, headers: headers, isFinal: false); await clientFinished.Task; #if TARGET_BROWSER // make sure that the browser closed the connection await connection.WaitForCloseAsync(CancellationToken.None); #endif }); var req = new HttpRequestMessage(HttpMethod.Get, url) { Version = UseVersion }; req.Headers.ConnectionClose = connectionClose; #if TARGET_BROWSER var WebAssemblyEnableStreamingResponseKey = new HttpRequestOptionsKey <bool>("WebAssemblyEnableStreamingResponse"); req.Options.Set(WebAssemblyEnableStreamingResponseKey, true); #endif Task <HttpResponseMessage> getResponse = client.SendAsync(TestAsync, req, HttpCompletionOption.ResponseHeadersRead, cts.Token); await ValidateClientCancellationAsync(async() => { // This 'using' shouldn't be necessary in general. However, HTTP3 does not remove the request stream from the // active stream table until the user disposes the response (or it gets finalized). // This means the connection will fail to shut down promptly. // See https://github.com/dotnet/runtime/issues/58072 using HttpResponseMessage resp = await getResponse; Stream respStream = await resp.Content.ReadAsStreamAsync(TestAsync); Task readTask = readOrCopyToAsync ? respStream.ReadAsync(new byte[1], 0, 1, cts.Token) : respStream.CopyToAsync(Stream.Null, 10, cts.Token); cts.Cancel(); await readTask; }); try { clientFinished.SetResult(true); await serverTask; } catch (Exception ex) { _output.WriteLine($"Ignored exception:{Environment.NewLine}{ex}"); } }); } }