Esempio n. 1
0
 private IEnumerable <string> RedirectValues(IEnumerable <string> values, UriForwardingTransformer uriTransformer, string currentHost)
 {
     foreach (var value in values)
     {
         yield return((value.StartsWith("http"))
             ? uriTransformer.RedirectUrisToCurrentHost(value, currentHost)
             : value);
     }
 }
Esempio n. 2
0
        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);
            }
        }
Esempio n. 3
0
        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));
        }