public override async ValueTask TransformRequestAsync(HttpContext httpContext, HttpRequestMessage proxyRequest, string destinationPrefix) { if (ShouldCopyRequestHeaders.GetValueOrDefault(true)) { await base.TransformRequestAsync(httpContext, proxyRequest, destinationPrefix); } if (RequestTransforms.Count == 0) { return; } var transformContext = new RequestTransformContext() { DestinationPrefix = destinationPrefix, HttpContext = httpContext, ProxyRequest = proxyRequest, Path = httpContext.Request.Path, Query = new QueryTransformContext(httpContext.Request), HeadersCopied = ShouldCopyRequestHeaders.GetValueOrDefault(true), }; foreach (var requestTransform in RequestTransforms) { await requestTransform.ApplyAsync(transformContext); } // Allow a transform to directly set a custom RequestUri. proxyRequest.RequestUri ??= RequestUtilities.MakeDestinationAddress( transformContext.DestinationPrefix, transformContext.Path, transformContext.Query.QueryString); }
private void CopyRequestHeaders(HttpContext context, HttpRequestMessage proxyRequest) { // Transforms that were run in the first pass. HashSet <string> transformsRun = null; if (ShouldCopyRequestHeaders.GetValueOrDefault(true)) { foreach (var header in context.Request.Headers) { var headerName = header.Key; var headerValue = header.Value; if (StringValues.IsNullOrEmpty(headerValue)) { continue; } // Filter out HTTP/2 pseudo headers like ":method" and ":path", those go into other fields. if (headerName.Length > 0 && headerName[0] == ':') { continue; } if (RequestHeaderTransforms.TryGetValue(headerName, out var transform)) { (transformsRun ??= new HashSet <string>(StringComparer.OrdinalIgnoreCase)).Add(headerName); headerValue = transform.Apply(context, proxyRequest, headerValue); if (StringValues.IsNullOrEmpty(headerValue)) { continue; } } RequestUtilities.AddHeader(proxyRequest, headerName, headerValue); } } transformsRun ??= EmptyHash; // Run any transforms that weren't run yet. foreach (var(headerName, transform) in RequestHeaderTransforms) { if (!transformsRun.Contains(headerName)) { var headerValue = transform.Apply(context, proxyRequest, StringValues.Empty); if (!StringValues.IsNullOrEmpty(headerValue)) { RequestUtilities.AddHeader(proxyRequest, headerName, headerValue); } } } }