MCPcopy
hub / github.com/cortesi/devd / NewSingleHostReverseProxy

Function NewSingleHostReverseProxy

reverseproxy/reverseproxy.go:71–96  ·  view source on GitHub ↗

NewSingleHostReverseProxy returns a new ReverseProxy that rewrites URLs to the scheme, host, and base path provided in target. If the target's path is "/base" and the incoming request was for "/dir", the target request will be for /base/dir.

(target *url.URL, ci inject.CopyInject)

Source from the content-addressed store, hash-verified

69// target's path is "/base" and the incoming request was for "/dir",
70// the target request will be for /base/dir.
71func NewSingleHostReverseProxy(target *url.URL, ci inject.CopyInject) *ReverseProxy {
72 targetQuery := target.RawQuery
73 director := func(req *http.Request) {
74 req.URL.Host = target.Host
75 req.URL.Path = singleJoiningSlash(target.Path, req.URL.Path)
76 if req.Header.Get("X-Forwarded-Host") == "" {
77 req.Header.Set("X-Forwarded-Host", req.Host)
78 }
79 if req.Header.Get("X-Forwarded-Proto") == "" {
80 req.Header.Set("X-Forwarded-Proto", req.URL.Scheme)
81 }
82 req.URL.Scheme = target.Scheme
83
84 // Set "identity"-only content encoding, in order for injector to
85 // work on text response
86 req.Header.Set("Accept-Encoding", "identity")
87
88 req.Host = req.URL.Host
89 if targetQuery == "" || req.URL.RawQuery == "" {
90 req.URL.RawQuery = targetQuery + req.URL.RawQuery
91 } else {
92 req.URL.RawQuery = targetQuery + "&" + req.URL.RawQuery
93 }
94 }
95 return &ReverseProxy{Director: director, Inject: ci}
96}
97
98func copyHeader(dst, src http.Header) {
99 for k, vv := range src {

Callers 4

TestReverseProxyFunction · 0.85
TestXForwardedForFunction · 0.85
TestReverseProxyQueryFunction · 0.85

Calls 1

singleJoiningSlashFunction · 0.85

Tested by 4

TestReverseProxyFunction · 0.68
TestXForwardedForFunction · 0.68
TestReverseProxyQueryFunction · 0.68