Skip to content

Commit 3ffc975

Browse files
georgyobradfitz
authored andcommitted
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 <[email protected]>
1 parent 209dd4c commit 3ffc975

File tree

2 files changed

+23
-4
lines changed

2 files changed

+23
-4
lines changed

src/net/http/cgi/child.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -132,9 +132,9 @@ func RequestFromMap(params map[string]string) (*http.Request, error) {
132132
}
133133

134134
// Request.RemoteAddr has its port set by Go's standard http
135-
// server, so we do here too. We don't have one, though, so we
136-
// use a dummy one.
137-
r.RemoteAddr = net.JoinHostPort(params["REMOTE_ADDR"], "0")
135+
// server, so we do here too.
136+
remotePort, _ := strconv.Atoi(params["REMOTE_PORT"]) // zero if unset or invalid
137+
r.RemoteAddr = net.JoinHostPort(params["REMOTE_ADDR"], strconv.Itoa(remotePort))
138138

139139
return r, nil
140140
}

src/net/http/cgi/child_test.go

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ func TestRequest(t *testing.T) {
2222
"CONTENT_LENGTH": "123",
2323
"CONTENT_TYPE": "text/xml",
2424
"REMOTE_ADDR": "5.6.7.8",
25+
"REMOTE_PORT": "54321",
2526
}
2627
req, err := RequestFromMap(env)
2728
if err != nil {
@@ -60,7 +61,7 @@ func TestRequest(t *testing.T) {
6061
if req.TLS != nil {
6162
t.Errorf("expected nil TLS")
6263
}
63-
if e, g := "5.6.7.8:0", req.RemoteAddr; e != g {
64+
if e, g := "5.6.7.8:54321", req.RemoteAddr; e != g {
6465
t.Errorf("RemoteAddr: got %q; want %q", g, e)
6566
}
6667
}
@@ -129,3 +130,21 @@ func TestRequestWithoutRequestURI(t *testing.T) {
129130
t.Errorf("URL = %q; want %q", g, e)
130131
}
131132
}
133+
134+
func TestRequestWithoutRemotePort(t *testing.T) {
135+
env := map[string]string{
136+
"SERVER_PROTOCOL": "HTTP/1.1",
137+
"HTTP_HOST": "example.com",
138+
"REQUEST_METHOD": "GET",
139+
"REQUEST_URI": "/path?a=b",
140+
"CONTENT_LENGTH": "123",
141+
"REMOTE_ADDR": "5.6.7.8",
142+
}
143+
req, err := RequestFromMap(env)
144+
if err != nil {
145+
t.Fatalf("RequestFromMap: %v", err)
146+
}
147+
if e, g := "5.6.7.8:0", req.RemoteAddr; e != g {
148+
t.Errorf("RemoteAddr: got %q; want %q", g, e)
149+
}
150+
}

0 commit comments

Comments
 (0)