]> Cypherpunks.ru repositories - gostls13.git/commitdiff
net/http/cgi: remove port from the CGI environment of variable SERVER_NAME
authorhopehook <hopehook.com@gmail.com>
Wed, 15 Jun 2022 19:02:38 +0000 (03:02 +0800)
committerGopher Robot <gobot@golang.org>
Wed, 10 Aug 2022 15:20:24 +0000 (15:20 +0000)
The SERVER_NAME variable in the CGI environment should not
contain the port, according to the section 4.1.14 of the
RFC 3875.

Fixes #53368.

Change-Id: Ifeea70206878cf83bc0bda35cb514d0226bbcd8c
Reviewed-on: https://go-review.googlesource.com/c/go/+/412434
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Damien Neil <dneil@google.com>
Run-TryBot: Ian Lance Taylor <iant@golang.org>
Reviewed-by: xie cui <523516579@qq.com>
Auto-Submit: Dmitri Shuralyov <dmitshur@golang.org>
Reviewed-by: Dmitri Shuralyov <dmitshur@google.com>
src/net/http/cgi/host.go
src/net/http/cgi/host_test.go

index 0d43e140d56898cd7491ff8a24f70632f032c135..349dda15acfb30c70079e3146879afd547b5b9ee 100644 (file)
@@ -138,7 +138,6 @@ func (h *Handler) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
 
        env := []string{
                "SERVER_SOFTWARE=go",
-               "SERVER_NAME=" + req.Host,
                "SERVER_PROTOCOL=HTTP/1.1",
                "HTTP_HOST=" + req.Host,
                "GATEWAY_INTERFACE=CGI/1.1",
@@ -158,6 +157,12 @@ func (h *Handler) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
                env = append(env, "REMOTE_ADDR="+req.RemoteAddr, "REMOTE_HOST="+req.RemoteAddr)
        }
 
+       if hostDomain, _, err := net.SplitHostPort(req.Host); err == nil {
+               env = append(env, "SERVER_NAME="+hostDomain)
+       } else {
+               env = append(env, "SERVER_NAME="+req.Host)
+       }
+
        if req.TLS != nil {
                env = append(env, "HTTPS=on")
        }
index f8abc88c8973775397063bb28468b9c1c57763c8..55ca092dbca569a110289c8925b173d92f8d697c 100644 (file)
@@ -114,7 +114,7 @@ func TestCGIBasicGet(t *testing.T) {
                "param-a":               "b",
                "param-foo":             "bar",
                "env-GATEWAY_INTERFACE": "CGI/1.1",
-               "env-HTTP_HOST":         "example.com",
+               "env-HTTP_HOST":         "example.com:80",
                "env-PATH_INFO":         "",
                "env-QUERY_STRING":      "foo=bar&a=b",
                "env-REMOTE_ADDR":       "1.2.3.4",
@@ -128,7 +128,7 @@ func TestCGIBasicGet(t *testing.T) {
                "env-SERVER_PORT":       "80",
                "env-SERVER_SOFTWARE":   "go",
        }
-       replay := runCgiTest(t, h, "GET /test.cgi?foo=bar&a=b HTTP/1.0\nHost: example.com\n\n", expectedMap)
+       replay := runCgiTest(t, h, "GET /test.cgi?foo=bar&a=b HTTP/1.0\nHost: example.com:80\n\n", expectedMap)
 
        if expected, got := "text/html", replay.Header().Get("Content-Type"); got != expected {
                t.Errorf("got a Content-Type of %q; expected %q", got, expected)