Patchwork libgo patch committed: Inherit environment in http/cgi

login
register
mail settings
Submitter Ian Taylor
Date April 22, 2011, 6:23 p.m.
Message ID <mcrwrimw3ex.fsf@google.com>
Download mbox | patch
Permalink /patch/92569/
State New
Headers show

Comments

Ian Taylor - April 22, 2011, 6:23 p.m.
This libgo patch brings over a patch to the master Go library to inherit
environment variables in http/cgi.  This should fix PR go/48503.
Bootstrapped and ran Go testsuite on x86_64-unknown-linux-gnu.
Committed to mainline.

Ian

Patch

diff -r 9457f9a2e900 libgo/go/http/cgi/host.go
--- a/libgo/go/http/cgi/host.go	Thu Apr 21 15:51:28 2011 -0700
+++ b/libgo/go/http/cgi/host.go	Fri Apr 22 11:12:38 2011 -0700
@@ -25,20 +25,30 @@ 
 	"os"
 	"path/filepath"
 	"regexp"
+	"runtime"
 	"strconv"
 	"strings"
 )
 
 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.
 type Handler struct {
 	Path string // path to the CGI executable
 	Root string // root URI prefix of handler or empty for "/"
 
-	Env    []string    // extra environment variables to set, if any
-	Logger *log.Logger // optional log for errors or nil to use log.Print
-	Args   []string    // optional arguments to pass to child process
+	Env        []string    // extra environment variables to set, if any, as "key=value"
+	InheritEnv []string    // environment variables to inherit from host, as "key"
+	Logger     *log.Logger // optional log for errors or nil to use log.Print
+	Args       []string    // optional arguments to pass to child process
 }
 
 func (h *Handler) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
@@ -110,6 +120,24 @@ 
 		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 {
+		if v := os.Getenv(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)
 	if cwd == "" {
 		cwd = "."