]> Cypherpunks.ru repositories - gostls13.git/blobdiff - src/net/http/cgi/host.go
net/http/cgi: the PATH_INFO should be empty or start with a slash
[gostls13.git] / src / net / http / cgi / host.go
index cd42f4d921866499aa61492422e5623f67c21dd8..ef222ab73a75c0ce2c6d897bd635470f503ca071 100644 (file)
@@ -39,13 +39,13 @@ var osDefaultInheritEnv = func() []string {
        switch runtime.GOOS {
        case "darwin", "ios":
                return []string{"DYLD_LIBRARY_PATH"}
-       case "linux", "freebsd", "netbsd", "openbsd":
+       case "android", "linux", "freebsd", "netbsd", "openbsd":
                return []string{"LD_LIBRARY_PATH"}
        case "hpux":
                return []string{"LD_LIBRARY_PATH", "SHLIB_PATH"}
        case "irix":
                return []string{"LD_LIBRARY_PATH", "LD_LIBRARYN32_PATH", "LD_LIBRARY64_PATH"}
-       case "solaris":
+       case "illumos", "solaris":
                return []string{"LD_LIBRARY_PATH", "LD_LIBRARY_PATH_32", "LD_LIBRARY_PATH_64"}
        case "windows":
                return []string{"SystemRoot", "COMSPEC", "PATHEXT", "WINDIR"}
@@ -90,10 +90,11 @@ func (h *Handler) stderr() io.Writer {
 
 // removeLeadingDuplicates remove leading duplicate in environments.
 // It's possible to override environment like following.
-//    cgi.Handler{
-//      ...
-//      Env: []string{"SCRIPT_FILENAME=foo.php"},
-//    }
+//
+//     cgi.Handler{
+//       ...
+//       Env: []string{"SCRIPT_FILENAME=foo.php"},
+//     }
 func removeLeadingDuplicates(env []string) (ret []string) {
        for i, e := range env {
                found := false
@@ -114,30 +115,25 @@ func removeLeadingDuplicates(env []string) (ret []string) {
 }
 
 func (h *Handler) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
-       root := h.Root
-       if root == "" {
-               root = "/"
-       }
-
        if len(req.TransferEncoding) > 0 && req.TransferEncoding[0] == "chunked" {
                rw.WriteHeader(http.StatusBadRequest)
                rw.Write([]byte("Chunked request bodies are not supported by CGI."))
                return
        }
 
-       pathInfo := req.URL.Path
-       if root != "/" && strings.HasPrefix(pathInfo, root) {
-               pathInfo = pathInfo[len(root):]
-       }
+       root := strings.TrimRight(h.Root, "/")
+       pathInfo := strings.TrimPrefix(req.URL.Path, root)
 
        port := "80"
+       if req.TLS != nil {
+               port = "443"
+       }
        if matches := trailingPort.FindStringSubmatch(req.Host); len(matches) != 0 {
                port = matches[1]
        }
 
        env := []string{
                "SERVER_SOFTWARE=go",
-               "SERVER_NAME=" + req.Host,
                "SERVER_PROTOCOL=HTTP/1.1",
                "HTTP_HOST=" + req.Host,
                "GATEWAY_INTERFACE=CGI/1.1",
@@ -157,6 +153,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")
        }
@@ -273,12 +275,11 @@ func (h *Handler) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
                        break
                }
                headerLines++
-               parts := strings.SplitN(string(line), ":", 2)
-               if len(parts) < 2 {
+               header, val, ok := strings.Cut(string(line), ":")
+               if !ok {
                        h.printf("cgi: bogus header line: %s", string(line))
                        continue
                }
-               header, val := parts[0], parts[1]
                if !httpguts.ValidHeaderFieldName(header) {
                        h.printf("cgi: invalid header name: %q", header)
                        continue
@@ -351,7 +352,7 @@ func (h *Handler) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
        }
 }
 
-func (h *Handler) printf(format string, v ...interface{}) {
+func (h *Handler) printf(format string, v ...any) {
        if h.Logger != nil {
                h.Logger.Printf(format, v...)
        } else {