diff options
| author | George Shammas <george@shamm.as> | 2014-12-17 23:22:49 -0500 |
|---|---|---|
| committer | Brad Fitzpatrick <bradfitz@golang.org> | 2014-12-23 01:33:26 +0000 |
| commit | 3ffc9756d1963e834fbce63ff969de8959216c2b (patch) | |
| tree | a2a4a206c20cde0f0dd988193b0d12b7b8802172 /src/net/http | |
| parent | 209dd4cdc1e9180d819b1217f065a63b94e82ff9 (diff) | |
| download | go-3ffc9756d1963e834fbce63ff969de8959216c2b.tar.xz | |
net/http/cgi: Correctly pass down the REMOTE_PORT value for CGI requests.
Currently when we get a CGI or FCGI request, the remote port of the client
is hard coded to zero, despite nearly every webserver passing down the
REMOTE_PORT variable.
This was likely originally excluded because the CGI RFC (rfc3875) does not
mention anything about the remote port of the client. However every webserver
tested does pass REMOTE_PORT down. This includes Apache 2.2, Apache 2.4,
nginx and lighttpd.
Fixes #8351
Change-Id: I4c6366cb39f0ccc05e038bd31d85f93b76e8d0c8
Reviewed-on: https://go-review.googlesource.com/1750
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
Diffstat (limited to 'src/net/http')
| -rw-r--r-- | src/net/http/cgi/child.go | 6 | ||||
| -rw-r--r-- | src/net/http/cgi/child_test.go | 21 |
2 files changed, 23 insertions, 4 deletions
diff --git a/src/net/http/cgi/child.go b/src/net/http/cgi/child.go index 45fc2e57cd..ec10108821 100644 --- a/src/net/http/cgi/child.go +++ b/src/net/http/cgi/child.go @@ -132,9 +132,9 @@ func RequestFromMap(params map[string]string) (*http.Request, error) { } // Request.RemoteAddr has its port set by Go's standard http - // server, so we do here too. We don't have one, though, so we - // use a dummy one. - r.RemoteAddr = net.JoinHostPort(params["REMOTE_ADDR"], "0") + // server, so we do here too. + remotePort, _ := strconv.Atoi(params["REMOTE_PORT"]) // zero if unset or invalid + r.RemoteAddr = net.JoinHostPort(params["REMOTE_ADDR"], strconv.Itoa(remotePort)) return r, nil } diff --git a/src/net/http/cgi/child_test.go b/src/net/http/cgi/child_test.go index 075d8411bc..14e0af475f 100644 --- a/src/net/http/cgi/child_test.go +++ b/src/net/http/cgi/child_test.go @@ -22,6 +22,7 @@ func TestRequest(t *testing.T) { "CONTENT_LENGTH": "123", "CONTENT_TYPE": "text/xml", "REMOTE_ADDR": "5.6.7.8", + "REMOTE_PORT": "54321", } req, err := RequestFromMap(env) if err != nil { @@ -60,7 +61,7 @@ func TestRequest(t *testing.T) { if req.TLS != nil { t.Errorf("expected nil TLS") } - if e, g := "5.6.7.8:0", req.RemoteAddr; e != g { + if e, g := "5.6.7.8:54321", req.RemoteAddr; e != g { t.Errorf("RemoteAddr: got %q; want %q", g, e) } } @@ -129,3 +130,21 @@ func TestRequestWithoutRequestURI(t *testing.T) { t.Errorf("URL = %q; want %q", g, e) } } + +func TestRequestWithoutRemotePort(t *testing.T) { + env := map[string]string{ + "SERVER_PROTOCOL": "HTTP/1.1", + "HTTP_HOST": "example.com", + "REQUEST_METHOD": "GET", + "REQUEST_URI": "/path?a=b", + "CONTENT_LENGTH": "123", + "REMOTE_ADDR": "5.6.7.8", + } + req, err := RequestFromMap(env) + if err != nil { + t.Fatalf("RequestFromMap: %v", err) + } + if e, g := "5.6.7.8:0", req.RemoteAddr; e != g { + t.Errorf("RemoteAddr: got %q; want %q", g, e) + } +} |
