rewritePassthroughRequest configures the outbound request for the upstream and applies proxy headers.
(pr *httputil.ProxyRequest, provBaseURL *url.URL)
| 79 | // rewritePassthroughRequest configures the outbound request for the upstream and |
| 80 | // applies proxy headers. |
| 81 | func rewritePassthroughRequest(pr *httputil.ProxyRequest, provBaseURL *url.URL) { |
| 82 | pr.SetURL(provBaseURL) |
| 83 | |
| 84 | // Rewrite sets "X-Forwarded-For" to just last hop (clients IP address). |
| 85 | // To preserve old Director behavior pr.In "X-Forwarded-For" header |
| 86 | // values need to be copied manually. |
| 87 | // https://pkg.go.dev/net/http/httputil#ProxyRequest.SetXForwarded |
| 88 | if prior, ok := pr.In.Header["X-Forwarded-For"]; ok { |
| 89 | pr.Out.Header["X-Forwarded-For"] = append([]string(nil), prior...) |
| 90 | } |
| 91 | pr.SetXForwarded() |
| 92 | |
| 93 | span := trace.SpanFromContext(pr.Out.Context()) |
| 94 | span.SetAttributes(attribute.String(tracing.PassthroughUpstreamURL, pr.Out.URL.String())) |
| 95 | |
| 96 | // Avoid default Go user-agent if none provided. |
| 97 | if _, ok := pr.Out.Header["User-Agent"]; !ok { |
| 98 | pr.Out.Header.Set("User-Agent", "aibridge") // TODO: use build tag. |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | // newInvalidBaseURLHandler returns a handler that always returns 502 |
| 103 | // when the provider's base URL is invalid. |