8f9e6622b0
Custom headers configured under openai-compatibility (and any other provider passing through applyCustomHeaders) were silently dropped for the Host key, because Go's net/http reads the wire Host from req.Host, not req.Header["Host"]. As a result, virtual-host routed upstreams (e.g. LiteLLM behind an ingress) saw the base-url's host instead of the user-configured override and returned 404. Detect the Host key with http.CanonicalHeaderKey and assign it to req.Host so it is actually written on the wire. Other headers continue to use Header.Set as before. Fixes #2833
61 lines
1.4 KiB
Go
61 lines
1.4 KiB
Go
package util
|
|
|
|
import (
|
|
"net/http"
|
|
"strings"
|
|
)
|
|
|
|
// ApplyCustomHeadersFromAttrs applies user-defined headers stored in the provided attributes map.
|
|
// Custom headers override built-in defaults when conflicts occur.
|
|
func ApplyCustomHeadersFromAttrs(r *http.Request, attrs map[string]string) {
|
|
if r == nil {
|
|
return
|
|
}
|
|
applyCustomHeaders(r, extractCustomHeaders(attrs))
|
|
}
|
|
|
|
func extractCustomHeaders(attrs map[string]string) map[string]string {
|
|
if len(attrs) == 0 {
|
|
return nil
|
|
}
|
|
headers := make(map[string]string)
|
|
for k, v := range attrs {
|
|
if !strings.HasPrefix(k, "header:") {
|
|
continue
|
|
}
|
|
name := strings.TrimSpace(strings.TrimPrefix(k, "header:"))
|
|
if name == "" {
|
|
continue
|
|
}
|
|
val := strings.TrimSpace(v)
|
|
if val == "" {
|
|
continue
|
|
}
|
|
headers[name] = val
|
|
}
|
|
if len(headers) == 0 {
|
|
return nil
|
|
}
|
|
return headers
|
|
}
|
|
|
|
func applyCustomHeaders(r *http.Request, headers map[string]string) {
|
|
if r == nil || len(headers) == 0 {
|
|
return
|
|
}
|
|
for k, v := range headers {
|
|
if k == "" || v == "" {
|
|
continue
|
|
}
|
|
// Host is read from req.Host (not req.Header) by net/http when
|
|
// writing the request; setting it via Header.Set is silently
|
|
// dropped on the wire. Handle it explicitly so user-configured
|
|
// virtual-host overrides actually take effect upstream.
|
|
if http.CanonicalHeaderKey(k) == "Host" {
|
|
r.Host = v
|
|
continue
|
|
}
|
|
r.Header.Set(k, v)
|
|
}
|
|
}
|