]> Cypherpunks repositories - gostls13.git/commitdiff
net/http/cgi: Correctly pass down the REMOTE_PORT value for CGI requests.
authorGeorge Shammas <george@shamm.as>
Thu, 18 Dec 2014 04:22:49 +0000 (23:22 -0500)
committerBrad Fitzpatrick <bradfitz@golang.org>
Tue, 23 Dec 2014 01:33:26 +0000 (01:33 +0000)
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>
src/net/http/cgi/child.go
src/net/http/cgi/child_test.go

index 45fc2e57cd7e7de212c1043fd277899a455cc9f8..ec1010882196c888c28c7ca557a954a95003e51e 100644 (file)
@@ -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
 }
index 075d8411bcf2e1a6a8e72500ca8405b90dbf82f6..14e0af475f5af90903a8f4668d44a173697eacea 100644 (file)
@@ -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)
+       }
+}