gitee.com/zhaochuninhefei/gmgo@v0.0.31-0.20240209061119-069254a02979/gmhttp/cgi/host.go (about)

     1  // Copyright 2011 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  // This file implements the host side of CGI (being the webserver
     6  // parent process).
     7  
     8  // Package cgi implements CGI (Common Gateway Interface) as specified
     9  // in RFC 3875.
    10  //
    11  // Note that using CGI means starting a new process to handle each
    12  // request, which is typically less efficient than using a
    13  // long-running server. This package is intended primarily for
    14  // compatibility with existing systems.
    15  package cgi
    16  
    17  import (
    18  	"bufio"
    19  	"fmt"
    20  	"io"
    21  	"log"
    22  	"net"
    23  	"net/textproto"
    24  	"os"
    25  	"os/exec"
    26  	"path/filepath"
    27  	"regexp"
    28  	"runtime"
    29  	"strconv"
    30  	"strings"
    31  
    32  	http "gitee.com/zhaochuninhefei/gmgo/gmhttp"
    33  
    34  	"golang.org/x/net/http/httpguts"
    35  )
    36  
    37  var trailingPort = regexp.MustCompile(`:([0-9]+)$`)
    38  
    39  var osDefaultInheritEnv = func() []string {
    40  	switch runtime.GOOS {
    41  	case "darwin", "ios":
    42  		return []string{"DYLD_LIBRARY_PATH"}
    43  	case "linux", "freebsd", "netbsd", "openbsd":
    44  		return []string{"LD_LIBRARY_PATH"}
    45  	case "hpux":
    46  		return []string{"LD_LIBRARY_PATH", "SHLIB_PATH"}
    47  	case "irix":
    48  		return []string{"LD_LIBRARY_PATH", "LD_LIBRARYN32_PATH", "LD_LIBRARY64_PATH"}
    49  	case "illumos", "solaris":
    50  		return []string{"LD_LIBRARY_PATH", "LD_LIBRARY_PATH_32", "LD_LIBRARY_PATH_64"}
    51  	case "windows":
    52  		return []string{"SystemRoot", "COMSPEC", "PATHEXT", "WINDIR"}
    53  	}
    54  	return nil
    55  }()
    56  
    57  // Handler runs an executable in a subprocess with a CGI environment.
    58  type Handler struct {
    59  	Path string // path to the CGI executable
    60  	Root string // root URI prefix of handler or empty for "/"
    61  
    62  	// Dir specifies the CGI executable's working directory.
    63  	// If Dir is empty, the base directory of Path is used.
    64  	// If Path has no base directory, the current working
    65  	// directory is used.
    66  	Dir string
    67  
    68  	Env        []string    // extra environment variables to set, if any, as "key=value"
    69  	InheritEnv []string    // environment variables to inherit from host, as "key"
    70  	Logger     *log.Logger // optional log for errors or nil to use log.Print
    71  	Args       []string    // optional arguments to pass to child process
    72  	Stderr     io.Writer   // optional stderr for the child process; nil means os.Stderr
    73  
    74  	// PathLocationHandler specifies the root http Handler that
    75  	// should handle internal redirects when the CGI process
    76  	// returns a Location header value starting with a "/", as
    77  	// specified in RFC 3875 ยง 6.3.2. This will likely be
    78  	// http.DefaultServeMux.
    79  	//
    80  	// If nil, a CGI response with a local URI path is instead sent
    81  	// back to the client and not redirected internally.
    82  	PathLocationHandler http.Handler
    83  }
    84  
    85  func (h *Handler) stderr() io.Writer {
    86  	if h.Stderr != nil {
    87  		return h.Stderr
    88  	}
    89  	return os.Stderr
    90  }
    91  
    92  // removeLeadingDuplicates remove leading duplicate in environments.
    93  // It's possible to override environment like following.
    94  //    cgi.Handler{
    95  //      ...
    96  //      Env: []string{"SCRIPT_FILENAME=foo.php"},
    97  //    }
    98  func removeLeadingDuplicates(env []string) (ret []string) {
    99  	for i, e := range env {
   100  		found := false
   101  		if eq := strings.IndexByte(e, '='); eq != -1 {
   102  			keq := e[:eq+1] // "key="
   103  			for _, e2 := range env[i+1:] {
   104  				if strings.HasPrefix(e2, keq) {
   105  					found = true
   106  					break
   107  				}
   108  			}
   109  		}
   110  		if !found {
   111  			ret = append(ret, e)
   112  		}
   113  	}
   114  	return
   115  }
   116  
   117  func (h *Handler) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
   118  	root := h.Root
   119  	if root == "" {
   120  		root = "/"
   121  	}
   122  
   123  	if len(req.TransferEncoding) > 0 && req.TransferEncoding[0] == "chunked" {
   124  		rw.WriteHeader(http.StatusBadRequest)
   125  		_, _ = rw.Write([]byte("Chunked request bodies are not supported by CGI."))
   126  		return
   127  	}
   128  
   129  	pathInfo := req.URL.Path
   130  	if root != "/" && strings.HasPrefix(pathInfo, root) {
   131  		pathInfo = pathInfo[len(root):]
   132  	}
   133  
   134  	port := "80"
   135  	if matches := trailingPort.FindStringSubmatch(req.Host); len(matches) != 0 {
   136  		port = matches[1]
   137  	}
   138  
   139  	env := []string{
   140  		"SERVER_SOFTWARE=go",
   141  		"SERVER_NAME=" + req.Host,
   142  		"SERVER_PROTOCOL=HTTP/1.1",
   143  		"HTTP_HOST=" + req.Host,
   144  		"GATEWAY_INTERFACE=CGI/1.1",
   145  		"REQUEST_METHOD=" + req.Method,
   146  		"QUERY_STRING=" + req.URL.RawQuery,
   147  		"REQUEST_URI=" + req.URL.RequestURI(),
   148  		"PATH_INFO=" + pathInfo,
   149  		"SCRIPT_NAME=" + root,
   150  		"SCRIPT_FILENAME=" + h.Path,
   151  		"SERVER_PORT=" + port,
   152  	}
   153  
   154  	if remoteIP, remotePort, err := net.SplitHostPort(req.RemoteAddr); err == nil {
   155  		env = append(env, "REMOTE_ADDR="+remoteIP, "REMOTE_HOST="+remoteIP, "REMOTE_PORT="+remotePort)
   156  	} else {
   157  		// could not parse ip:port, let's use whole RemoteAddr and leave REMOTE_PORT undefined
   158  		env = append(env, "REMOTE_ADDR="+req.RemoteAddr, "REMOTE_HOST="+req.RemoteAddr)
   159  	}
   160  
   161  	if req.TLS != nil {
   162  		env = append(env, "HTTPS=on")
   163  	}
   164  
   165  	for k, v := range req.Header {
   166  		k = strings.Map(upperCaseAndUnderscore, k)
   167  		if k == "PROXY" {
   168  			// See Issue 16405
   169  			continue
   170  		}
   171  		joinStr := ", "
   172  		if k == "COOKIE" {
   173  			joinStr = "; "
   174  		}
   175  		env = append(env, "HTTP_"+k+"="+strings.Join(v, joinStr))
   176  	}
   177  
   178  	if req.ContentLength > 0 {
   179  		env = append(env, fmt.Sprintf("CONTENT_LENGTH=%d", req.ContentLength))
   180  	}
   181  	if ctype := req.Header.Get("Content-Type"); ctype != "" {
   182  		env = append(env, "CONTENT_TYPE="+ctype)
   183  	}
   184  
   185  	envPath := os.Getenv("PATH")
   186  	if envPath == "" {
   187  		envPath = "/bin:/usr/bin:/usr/ucb:/usr/bsd:/usr/local/bin"
   188  	}
   189  	env = append(env, "PATH="+envPath)
   190  
   191  	for _, e := range h.InheritEnv {
   192  		if v := os.Getenv(e); v != "" {
   193  			env = append(env, e+"="+v)
   194  		}
   195  	}
   196  
   197  	for _, e := range osDefaultInheritEnv {
   198  		if v := os.Getenv(e); v != "" {
   199  			env = append(env, e+"="+v)
   200  		}
   201  	}
   202  
   203  	if h.Env != nil {
   204  		env = append(env, h.Env...)
   205  	}
   206  
   207  	env = removeLeadingDuplicates(env)
   208  
   209  	var cwd, path string
   210  	if h.Dir != "" {
   211  		path = h.Path
   212  		cwd = h.Dir
   213  	} else {
   214  		cwd, path = filepath.Split(h.Path)
   215  	}
   216  	if cwd == "" {
   217  		cwd = "."
   218  	}
   219  
   220  	internalError := func(err error) {
   221  		rw.WriteHeader(http.StatusInternalServerError)
   222  		h.printf("CGI error: %v", err)
   223  	}
   224  
   225  	cmd := &exec.Cmd{
   226  		Path:   path,
   227  		Args:   append([]string{h.Path}, h.Args...),
   228  		Dir:    cwd,
   229  		Env:    env,
   230  		Stderr: h.stderr(),
   231  	}
   232  	if req.ContentLength != 0 {
   233  		cmd.Stdin = req.Body
   234  	}
   235  	stdoutRead, err := cmd.StdoutPipe()
   236  	if err != nil {
   237  		internalError(err)
   238  		return
   239  	}
   240  
   241  	err = cmd.Start()
   242  	if err != nil {
   243  		internalError(err)
   244  		return
   245  	}
   246  	if hook := testHookStartProcess; hook != nil {
   247  		hook(cmd.Process)
   248  	}
   249  	defer func(cmd *exec.Cmd) {
   250  		_ = cmd.Wait()
   251  	}(cmd)
   252  	defer func(stdoutRead io.ReadCloser) {
   253  		_ = stdoutRead.Close()
   254  	}(stdoutRead)
   255  
   256  	linebody := bufio.NewReaderSize(stdoutRead, 1024)
   257  	headers := make(http.Header)
   258  	statusCode := 0
   259  	headerLines := 0
   260  	sawBlankLine := false
   261  	for {
   262  		line, isPrefix, err := linebody.ReadLine()
   263  		if isPrefix {
   264  			rw.WriteHeader(http.StatusInternalServerError)
   265  			h.printf("cgi: long header line from subprocess.")
   266  			return
   267  		}
   268  		if err == io.EOF {
   269  			break
   270  		}
   271  		if err != nil {
   272  			rw.WriteHeader(http.StatusInternalServerError)
   273  			h.printf("cgi: error reading headers: %v", err)
   274  			return
   275  		}
   276  		if len(line) == 0 {
   277  			sawBlankLine = true
   278  			break
   279  		}
   280  		headerLines++
   281  		parts := strings.SplitN(string(line), ":", 2)
   282  		if len(parts) < 2 {
   283  			h.printf("cgi: bogus header line: %s", string(line))
   284  			continue
   285  		}
   286  		header, val := parts[0], parts[1]
   287  		if !httpguts.ValidHeaderFieldName(header) {
   288  			h.printf("cgi: invalid header name: %q", header)
   289  			continue
   290  		}
   291  		val = textproto.TrimString(val)
   292  		switch {
   293  		case header == "Status":
   294  			if len(val) < 3 {
   295  				h.printf("cgi: bogus status (short): %q", val)
   296  				return
   297  			}
   298  			code, err := strconv.Atoi(val[0:3])
   299  			if err != nil {
   300  				h.printf("cgi: bogus status: %q", val)
   301  				h.printf("cgi: line was %q", line)
   302  				return
   303  			}
   304  			statusCode = code
   305  		default:
   306  			headers.Add(header, val)
   307  		}
   308  	}
   309  	if headerLines == 0 || !sawBlankLine {
   310  		rw.WriteHeader(http.StatusInternalServerError)
   311  		h.printf("cgi: no headers")
   312  		return
   313  	}
   314  
   315  	if loc := headers.Get("Location"); loc != "" {
   316  		if strings.HasPrefix(loc, "/") && h.PathLocationHandler != nil {
   317  			h.handleInternalRedirect(rw, req, loc)
   318  			return
   319  		}
   320  		if statusCode == 0 {
   321  			statusCode = http.StatusFound
   322  		}
   323  	}
   324  
   325  	if statusCode == 0 && headers.Get("Content-Type") == "" {
   326  		rw.WriteHeader(http.StatusInternalServerError)
   327  		h.printf("cgi: missing required Content-Type in headers")
   328  		return
   329  	}
   330  
   331  	if statusCode == 0 {
   332  		statusCode = http.StatusOK
   333  	}
   334  
   335  	// Copy headers to rw's headers, after we've decided not to
   336  	// go into handleInternalRedirect, which won't want its rw
   337  	// headers to have been touched.
   338  	for k, vv := range headers {
   339  		for _, v := range vv {
   340  			rw.Header().Add(k, v)
   341  		}
   342  	}
   343  
   344  	rw.WriteHeader(statusCode)
   345  
   346  	_, err = io.Copy(rw, linebody)
   347  	if err != nil {
   348  		h.printf("cgi: copy error: %v", err)
   349  		// And kill the child CGI process so we don't hang on
   350  		// the deferred cmd.Wait above if the error was just
   351  		// the client (rw) going away. If it was a read error
   352  		// (because the child died itself), then the extra
   353  		// kill of an already-dead process is harmless (the PID
   354  		// won't be reused until the Wait above).
   355  		_ = cmd.Process.Kill()
   356  	}
   357  }
   358  
   359  func (h *Handler) printf(format string, v ...interface{}) {
   360  	if h.Logger != nil {
   361  		h.Logger.Printf(format, v...)
   362  	} else {
   363  		log.Printf(format, v...)
   364  	}
   365  }
   366  
   367  func (h *Handler) handleInternalRedirect(rw http.ResponseWriter, req *http.Request, path string) {
   368  	url, err := req.URL.Parse(path)
   369  	if err != nil {
   370  		rw.WriteHeader(http.StatusInternalServerError)
   371  		h.printf("cgi: error resolving local URI path %q: %v", path, err)
   372  		return
   373  	}
   374  	// TODO: RFC 3875 isn't clear if only GET is supported, but it
   375  	// suggests so: "Note that any message-body attached to the
   376  	// request (such as for a POST request) may not be available
   377  	// to the resource that is the target of the redirect."  We
   378  	// should do some tests against Apache to see how it handles
   379  	// POST, HEAD, etc. Does the internal redirect get the same
   380  	// method or just GET? What about incoming headers?
   381  	// (e.g. Cookies) Which headers, if any, are copied into the
   382  	// second request?
   383  	newReq := &http.Request{
   384  		Method:     "GET",
   385  		URL:        url,
   386  		Proto:      "HTTP/1.1",
   387  		ProtoMajor: 1,
   388  		ProtoMinor: 1,
   389  		Header:     make(http.Header),
   390  		Host:       url.Host,
   391  		RemoteAddr: req.RemoteAddr,
   392  		TLS:        req.TLS,
   393  	}
   394  	h.PathLocationHandler.ServeHTTP(rw, newReq)
   395  }
   396  
   397  func upperCaseAndUnderscore(r rune) rune {
   398  	switch {
   399  	case r >= 'a' && r <= 'z':
   400  		return r - ('a' - 'A')
   401  	case r == '-':
   402  		return '_'
   403  	case r == '=':
   404  		// Maybe not part of the CGI 'spec' but would mess up
   405  		// the environment in any case, as Go represents the
   406  		// environment as a slice of "key=value" strings.
   407  		return '_'
   408  	}
   409  	// TODO: other transformations in spec or practice?
   410  	return r
   411  }
   412  
   413  var testHookStartProcess func(*os.Process) // nil except for some tests