1
0
mirror of https://github.com/golang/go synced 2024-11-22 05:14:40 -07:00

http/cgi: pass some default environment variables

This isn't really part of RFC 3875 but matches
the behavior of Apache, et al.

R=iant, iant2
CC=golang-dev
https://golang.org/cl/4435065
This commit is contained in:
Brad Fitzpatrick 2011-04-22 11:02:33 -07:00
parent 4335bee42e
commit c94db30ec9
2 changed files with 21 additions and 8 deletions

View File

@ -25,12 +25,21 @@ import (
"os" "os"
"path/filepath" "path/filepath"
"regexp" "regexp"
"runtime"
"strconv" "strconv"
"strings" "strings"
) )
var trailingPort = regexp.MustCompile(`:([0-9]+)$`) var trailingPort = regexp.MustCompile(`:([0-9]+)$`)
var osDefaultInheritEnv = map[string][]string{
"darwin": []string{"DYLD_LIBRARY_PATH"},
"freebsd": []string{"LD_LIBRARY_PATH"},
"hpux": []string{"LD_LIBRARY_PATH", "SHLIB_PATH"},
"linux": []string{"LD_LIBRARY_PATH"},
"windows": []string{"SystemRoot", "COMSPEC", "PATHEXT", "WINDIR"},
}
// Handler runs an executable in a subprocess with a CGI environment. // Handler runs an executable in a subprocess with a CGI environment.
type Handler struct { type Handler struct {
Path string // path to the CGI executable Path string // path to the CGI executable
@ -111,12 +120,24 @@ func (h *Handler) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
env = append(env, h.Env...) env = append(env, h.Env...)
} }
path := os.Getenv("PATH")
if path == "" {
path = "/bin:/usr/bin:/usr/ucb:/usr/bsd:/usr/local/bin"
}
env = append(env, "PATH="+path)
for _, e := range h.InheritEnv { for _, e := range h.InheritEnv {
if v := os.Getenv(e); v != "" { if v := os.Getenv(e); v != "" {
env = append(env, e+"="+v) env = append(env, e+"="+v)
} }
} }
for _, e := range osDefaultInheritEnv[runtime.GOOS] {
if v := os.Getenv(e); v != "" {
env = append(env, e+"="+v)
}
}
cwd, pathBase := filepath.Split(h.Path) cwd, pathBase := filepath.Split(h.Path)
if cwd == "" { if cwd == "" {
cwd = "." cwd = "."

View File

@ -22,14 +22,6 @@ func TestHostingOurselves(t *testing.T) {
Path: os.Args[0], Path: os.Args[0],
Root: "/test.go", Root: "/test.go",
Args: []string{"-test.run=TestBeChildCGIProcess"}, Args: []string{"-test.run=TestBeChildCGIProcess"},
// When using a shared library with gccgo, make sure
// we can still find the library when we exec
// ourselves.
InheritEnv: []string{
"LD_LIBRARY_PATH",
"SHLIB_PATH",
"DYLD_LIBRARY_PATH",
},
} }
expectedMap := map[string]string{ expectedMap := map[string]string{
"test": "Hello CGI-in-CGI", "test": "Hello CGI-in-CGI",