diff --git a/src/net/http/cgi/host.go b/src/net/http/cgi/host.go index 0d43e140d56..349dda15acf 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 f8abc88c897..55ca092dbca 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)