net/http/httputil: preserve query params in reverse proxy

Fixes #2853

R=golang-dev, r
CC=golang-dev
https://golang.org/cl/5642056
This commit is contained in:
Brad Fitzpatrick 2012-02-07 18:00:30 -08:00
parent 92f55949f9
commit 518ee115b7
2 changed files with 44 additions and 1 deletions

View file

@ -55,11 +55,16 @@ func singleJoiningSlash(a, b string) string {
// target's path is "/base" and the incoming request was for "/dir",
// the target request will be for /base/dir.
func NewSingleHostReverseProxy(target *url.URL) *ReverseProxy {
targetQuery := target.RawQuery
director := func(req *http.Request) {
req.URL.Scheme = target.Scheme
req.URL.Host = target.Host
req.URL.Path = singleJoiningSlash(target.Path, req.URL.Path)
req.URL.RawQuery = target.RawQuery
if targetQuery == "" || req.URL.RawQuery == "" {
req.URL.RawQuery = targetQuery + req.URL.RawQuery
} else {
req.URL.RawQuery = targetQuery + "&" + req.URL.RawQuery
}
}
return &ReverseProxy{Director: director}
}