]> 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 a3fba4b41313456df8ec581b4d14e45431379b19..ef222ab73a75c0ce2c6d897bd635470f503ca071 100644 (file)
@@ -35,10 +35,7 @@ import (
 
 var trailingPort = regexp.MustCompile(`:([0-9]+)$`)
 
-var osDefaultInheritEnv = getOSDefaultInheritEnv()
-
-// TODO(mdempsky): Revert CL 522935 after #62277 is fixed.
-func getOSDefaultInheritEnv() []string {
+var osDefaultInheritEnv = func() []string {
        switch runtime.GOOS {
        case "darwin", "ios":
                return []string{"DYLD_LIBRARY_PATH"}
@@ -54,7 +51,7 @@ func getOSDefaultInheritEnv() []string {
                return []string{"SystemRoot", "COMSPEC", "PATHEXT", "WINDIR"}
        }
        return nil
-}
+}()
 
 // Handler runs an executable in a subprocess with a CGI environment.
 type Handler struct {
@@ -118,23 +115,19 @@ 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]
        }