public async Task Invoke(HttpContext httpContext, UriForwardingTransformer uriTransformer, AuthProvider authProvider, ConnectionInfoFactory connectionFactory) { var requestLeft = httpContext.Request; var responseLeft = httpContext.Response; var clientConnection = await connectionFactory.GetConnection(); string currentHost = $"{requestLeft.Scheme}://{requestLeft.Host}"; Uri dest = uriTransformer.GetUriForActualHost(requestLeft.Path, requestLeft.QueryString.Value); var requestRight = GetRequest(requestLeft.Method, dest, requestLeft.Headers, clientConnection.Session); var canAuth = authProvider.CanAuthenticate(dest, clientConnection); if (canAuth) { requestRight.Headers.Authorization = authProvider.GetAuthenticationHeader(dest, clientConnection, false); } var responseRight = await _client.SendAsync(requestRight); if (responseRight.StatusCode == HttpStatusCode.Unauthorized && canAuth) { requestRight = GetRequest(requestLeft.Method, dest, requestLeft.Headers, clientConnection.Session); requestRight.Headers.Authorization = authProvider.GetAuthenticationHeader(dest, clientConnection, true); responseRight = await _client.SendAsync(requestRight); } foreach (var header in responseRight.Headers.Concat(responseRight.Content.Headers)) { Console.WriteLine($"Got header [{header.Key}] with [{header.Value}]"); if (header.Key == "Accept-Encoding") { continue; } responseLeft.Headers.Add(header.Key, new StringValues(RedirectValues(header.Value, uriTransformer, currentHost).ToArray())); } responseLeft.Headers["X-Prism-SessionId"] = clientConnection.Session; responseLeft.StatusCode = (int)responseRight.StatusCode; if (responseLeft.ContentType.Contains("json") || responseLeft.ContentType.Contains("xml")) { var contents = await responseRight.Content.ReadAsStringAsync(); contents = uriTransformer.RedirectUrisToCurrentHost(contents, currentHost); var bytes = System.Text.Encoding.UTF8.GetBytes(contents); responseLeft.ContentLength = bytes.Length; responseLeft.Headers.ContentLength = bytes.Length; responseLeft.Body.Write(bytes); } else { await responseRight.Content.CopyToAsync(responseLeft.Body); } }
public async Task Invoke(HttpContext httpContext, UriForwardingTransformer uriTransformer, TrackedRequestAccessor requestLog, ConnectionInfoFactory connectionFactory) { var connection = await connectionFactory.GetConnection(); var request = new TrackedRequest(httpContext.Request, uriTransformer, connection); _exchangeLog.Add(request); requestLog.SetRequest(request); await _next(httpContext); request.Complete(new TrackedResponse(httpContext.Response)); }