From: hopehook Date: Wed, 15 Jun 2022 19:02:38 +0000 (+0800) Subject: net/http/cgi: remove port from the CGI environment of variable SERVER_NAME X-Git-Tag: go1.20rc1~1715 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=7ba458d7d8461835255b6353c773731f47c16215;p=gostls13.git net/http/cgi: remove port from the CGI environment of variable SERVER_NAME 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 Reviewed-by: Damien Neil Run-TryBot: Ian Lance Taylor Reviewed-by: xie cui <523516579@qq.com> Auto-Submit: Dmitri Shuralyov Reviewed-by: Dmitri Shuralyov --- diff --git a/src/net/http/cgi/host.go b/src/net/http/cgi/host.go index 0d43e140d5..349dda15ac 100644 --- a/src/net/http/cgi/host.go +++ b/src/net/http/cgi/host.go @@ -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") } diff --git a/src/net/http/cgi/host_test.go b/src/net/http/cgi/host_test.go index f8abc88c89..55ca092dbc 100644 --- a/src/net/http/cgi/host_test.go +++ b/src/net/http/cgi/host_test.go @@ -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)