New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

net/http/httputil: ReverseProxy doesn't handle empty path #25710

Open
shellfly opened this Issue Jun 3, 2018 · 6 comments

Comments

Projects
None yet
4 participants
@shellfly

shellfly commented Jun 3, 2018

Please answer these questions before submitting your issue. Thanks!

What version of Go are you using (go version)?

go version go1.10.1 darwin/amd64

Does this issue reproduce with the latest release?

yes

What operating system and processor architecture are you using (go env)?

GOARCH="amd64"
GOBIN=""
GOCACHE="/Users/hao/Library/Caches/go-build"
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOOS="darwin"
GOPATH="/Users/hao/go"
GORACE=""
GOROOT="/usr/local/Cellar/go/1.10.1/libexec"
GOTMPDIR=""
GOTOOLDIR="/usr/local/Cellar/go/1.10.1/libexec/pkg/tool/darwin_amd64"
GCCGO="gccgo"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/wc/gl6jp38s3lqdprky1txx23mr0000gn/T/go-build181870961=/tmp/go-build -gno-record-gcc-switches -fno-common"

What did you do?

package main

import (
	"net/http"
	"net/http/httputil"
	"net/url"
)

func main() {
	url, _ := url.Parse("http://localhost:9000/b")
	http.Handle("/api/a", http.StripPrefix("/api/a", httputil.NewSingleHostReverseProxy(url)))
	http.ListenAndServe(":9001", nil)
}

What did you expect to see?

proxy to http://localhost:8000/b

What did you see instead?

proxy to http://localhost:8000/b/, adding an extra slash.

this line add a slash even if url path is empty. is this expected behavior or a bug?

@meirf

This comment has been minimized.

Member

meirf commented Jun 4, 2018

this line add a slash even if url path is empty.

Actually, url path is not "". url path is "/". So it's joining: "/b" + "/" == "/b/". But I agree that this is an unexpected result. Though if we fixed this, it might break existing code.

Here's a more complete, minimal reproduction:

origin:

func main() {
	http.HandleFunc("/b", func(rw http.ResponseWriter, r *http.Request) {
		rw.Write([]byte("handle /b"))
	})
	http.HandleFunc("/b/", func(rw http.ResponseWriter, r *http.Request) {
		rw.Write([]byte("handle /b/"))
	})
	http.ListenAndServe(":9000", nil)
}

proxy:

func main() {
	url, _ := url.Parse("http://localhost:9000/b")
	http.Handle("/", httputil.NewSingleHostReverseProxy(url))
	http.ListenAndServe(":9001", nil)
}

request:
curl http://localhost:9001

@shellfly

This comment has been minimized.

shellfly commented Jun 4, 2018

@meirf if you use StripPrefix it will generate an empty url Path ""

@meirf

This comment has been minimized.

Member

meirf commented Jun 4, 2018

@shellfly Sorry I conflated your example with my repro. But I think my repro is similar.

Your case: "/b" + "". Expect "/b", actual "/b/". See code.
My case "/b" + "/". Expect "/b", actual "/b/". See code.

@agnivade agnivade added this to the Go1.12 milestone Jun 4, 2018

@agnivade

This comment has been minimized.

Member

agnivade commented Jun 4, 2018

/cc @bradfitz

@joekyo

This comment has been minimized.

Contributor

joekyo commented Jun 4, 2018

The empty url Path "" generated by StripPrefix is OK. If "/" is generated instead, the request to /b will become /b/ when it reach the backend server.
To make your code works as you expected, I guess the function singleJoiningSlash(a, b string) should check b first, and return a directly if b is an empty string.

@shellfly

This comment has been minimized.

shellfly commented Jun 5, 2018

@joekyo yes, this is what I thought, or we can do this check before singleJoiningSlash, since the function itself doing the correct thing of joining two paths.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment