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.
5 // This file implements the host side of CGI (being the webserver
8 // Package cgi implements CGI (Common Gateway Interface) as specified
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.
33 "golang.org/x/net/http/httpguts"
36 var trailingPort = regexp.MustCompile(`:([0-9]+)$`)
38 var osDefaultInheritEnv = func() []string {
41 return []string{"DYLD_LIBRARY_PATH"}
42 case "android", "linux", "freebsd", "netbsd", "openbsd":
43 return []string{"LD_LIBRARY_PATH"}
45 return []string{"LD_LIBRARY_PATH", "SHLIB_PATH"}
47 return []string{"LD_LIBRARY_PATH", "LD_LIBRARYN32_PATH", "LD_LIBRARY64_PATH"}
48 case "illumos", "solaris":
49 return []string{"LD_LIBRARY_PATH", "LD_LIBRARY_PATH_32", "LD_LIBRARY_PATH_64"}
51 return []string{"SystemRoot", "COMSPEC", "PATHEXT", "WINDIR"}
56 // Handler runs an executable in a subprocess with a CGI environment.
58 Path string // path to the CGI executable
59 Root string // root URI prefix of handler or empty for "/"
61 // Dir specifies the CGI executable's working directory.
62 // If Dir is empty, the base directory of Path is used.
63 // If Path has no base directory, the current working
67 Env []string // extra environment variables to set, if any, as "key=value"
68 InheritEnv []string // environment variables to inherit from host, as "key"
69 Logger *log.Logger // optional log for errors or nil to use log.Print
70 Args []string // optional arguments to pass to child process
71 Stderr io.Writer // optional stderr for the child process; nil means os.Stderr
73 // PathLocationHandler specifies the root http Handler that
74 // should handle internal redirects when the CGI process
75 // returns a Location header value starting with a "/", as
76 // specified in RFC 3875 ยง 6.3.2. This will likely be
77 // http.DefaultServeMux.
79 // If nil, a CGI response with a local URI path is instead sent
80 // back to the client and not redirected internally.
81 PathLocationHandler http.Handler
84 func (h *Handler) stderr() io.Writer {
91 // removeLeadingDuplicates remove leading duplicate in environments.
92 // It's possible to override environment like following.
96 // Env: []string{"SCRIPT_FILENAME=foo.php"},
98 func removeLeadingDuplicates(env []string) (ret []string) {
99 for i, e := range env {
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) {
117 func (h *Handler) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
118 if len(req.TransferEncoding) > 0 && req.TransferEncoding[0] == "chunked" {
119 rw.WriteHeader(http.StatusBadRequest)
120 rw.Write([]byte("Chunked request bodies are not supported by CGI."))
124 root := strings.TrimRight(h.Root, "/")
125 pathInfo := strings.TrimPrefix(req.URL.Path, root)
131 if matches := trailingPort.FindStringSubmatch(req.Host); len(matches) != 0 {
136 "SERVER_SOFTWARE=go",
137 "SERVER_PROTOCOL=HTTP/1.1",
138 "HTTP_HOST=" + req.Host,
139 "GATEWAY_INTERFACE=CGI/1.1",
140 "REQUEST_METHOD=" + req.Method,
141 "QUERY_STRING=" + req.URL.RawQuery,
142 "REQUEST_URI=" + req.URL.RequestURI(),
143 "PATH_INFO=" + pathInfo,
144 "SCRIPT_NAME=" + root,
145 "SCRIPT_FILENAME=" + h.Path,
146 "SERVER_PORT=" + port,
149 if remoteIP, remotePort, err := net.SplitHostPort(req.RemoteAddr); err == nil {
150 env = append(env, "REMOTE_ADDR="+remoteIP, "REMOTE_HOST="+remoteIP, "REMOTE_PORT="+remotePort)
152 // could not parse ip:port, let's use whole RemoteAddr and leave REMOTE_PORT undefined
153 env = append(env, "REMOTE_ADDR="+req.RemoteAddr, "REMOTE_HOST="+req.RemoteAddr)
156 if hostDomain, _, err := net.SplitHostPort(req.Host); err == nil {
157 env = append(env, "SERVER_NAME="+hostDomain)
159 env = append(env, "SERVER_NAME="+req.Host)
163 env = append(env, "HTTPS=on")
166 for k, v := range req.Header {
167 k = strings.Map(upperCaseAndUnderscore, k)
176 env = append(env, "HTTP_"+k+"="+strings.Join(v, joinStr))
179 if req.ContentLength > 0 {
180 env = append(env, fmt.Sprintf("CONTENT_LENGTH=%d", req.ContentLength))
182 if ctype := req.Header.Get("Content-Type"); ctype != "" {
183 env = append(env, "CONTENT_TYPE="+ctype)
186 envPath := os.Getenv("PATH")
188 envPath = "/bin:/usr/bin:/usr/ucb:/usr/bsd:/usr/local/bin"
190 env = append(env, "PATH="+envPath)
192 for _, e := range h.InheritEnv {
193 if v := os.Getenv(e); v != "" {
194 env = append(env, e+"="+v)
198 for _, e := range osDefaultInheritEnv {
199 if v := os.Getenv(e); v != "" {
200 env = append(env, e+"="+v)
205 env = append(env, h.Env...)
208 env = removeLeadingDuplicates(env)
215 cwd, path = filepath.Split(h.Path)
221 internalError := func(err error) {
222 rw.WriteHeader(http.StatusInternalServerError)
223 h.printf("CGI error: %v", err)
228 Args: append([]string{h.Path}, h.Args...),
233 if req.ContentLength != 0 {
236 stdoutRead, err := cmd.StdoutPipe()
247 if hook := testHookStartProcess; hook != nil {
251 defer stdoutRead.Close()
253 linebody := bufio.NewReaderSize(stdoutRead, 1024)
254 headers := make(http.Header)
257 sawBlankLine := false
259 line, isPrefix, err := linebody.ReadLine()
261 rw.WriteHeader(http.StatusInternalServerError)
262 h.printf("cgi: long header line from subprocess.")
269 rw.WriteHeader(http.StatusInternalServerError)
270 h.printf("cgi: error reading headers: %v", err)
278 header, val, ok := strings.Cut(string(line), ":")
280 h.printf("cgi: bogus header line: %s", string(line))
283 if !httpguts.ValidHeaderFieldName(header) {
284 h.printf("cgi: invalid header name: %q", header)
287 val = textproto.TrimString(val)
289 case header == "Status":
291 h.printf("cgi: bogus status (short): %q", val)
294 code, err := strconv.Atoi(val[0:3])
296 h.printf("cgi: bogus status: %q", val)
297 h.printf("cgi: line was %q", line)
302 headers.Add(header, val)
305 if headerLines == 0 || !sawBlankLine {
306 rw.WriteHeader(http.StatusInternalServerError)
307 h.printf("cgi: no headers")
311 if loc := headers.Get("Location"); loc != "" {
312 if strings.HasPrefix(loc, "/") && h.PathLocationHandler != nil {
313 h.handleInternalRedirect(rw, req, loc)
317 statusCode = http.StatusFound
321 if statusCode == 0 && headers.Get("Content-Type") == "" {
322 rw.WriteHeader(http.StatusInternalServerError)
323 h.printf("cgi: missing required Content-Type in headers")
328 statusCode = http.StatusOK
331 // Copy headers to rw's headers, after we've decided not to
332 // go into handleInternalRedirect, which won't want its rw
333 // headers to have been touched.
334 for k, vv := range headers {
335 for _, v := range vv {
336 rw.Header().Add(k, v)
340 rw.WriteHeader(statusCode)
342 _, err = io.Copy(rw, linebody)
344 h.printf("cgi: copy error: %v", err)
345 // And kill the child CGI process so we don't hang on
346 // the deferred cmd.Wait above if the error was just
347 // the client (rw) going away. If it was a read error
348 // (because the child died itself), then the extra
349 // kill of an already-dead process is harmless (the PID
350 // won't be reused until the Wait above).
355 func (h *Handler) printf(format string, v ...any) {
357 h.Logger.Printf(format, v...)
359 log.Printf(format, v...)
363 func (h *Handler) handleInternalRedirect(rw http.ResponseWriter, req *http.Request, path string) {
364 url, err := req.URL.Parse(path)
366 rw.WriteHeader(http.StatusInternalServerError)
367 h.printf("cgi: error resolving local URI path %q: %v", path, err)
370 // TODO: RFC 3875 isn't clear if only GET is supported, but it
371 // suggests so: "Note that any message-body attached to the
372 // request (such as for a POST request) may not be available
373 // to the resource that is the target of the redirect." We
374 // should do some tests against Apache to see how it handles
375 // POST, HEAD, etc. Does the internal redirect get the same
376 // method or just GET? What about incoming headers?
377 // (e.g. Cookies) Which headers, if any, are copied into the
379 newReq := &http.Request{
385 Header: make(http.Header),
387 RemoteAddr: req.RemoteAddr,
390 h.PathLocationHandler.ServeHTTP(rw, newReq)
393 func upperCaseAndUnderscore(r rune) rune {
395 case r >= 'a' && r <= 'z':
396 return r - ('a' - 'A')
400 // Maybe not part of the CGI 'spec' but would mess up
401 // the environment in any case, as Go represents the
402 // environment as a slice of "key=value" strings.
405 // TODO: other transformations in spec or practice?
409 var testHookStartProcess func(*os.Process) // nil except for some tests