diff options
Diffstat (limited to 'libgo/go/net/http/cgi')
-rw-r--r-- | libgo/go/net/http/cgi/host.go | 24 | ||||
-rw-r--r-- | libgo/go/net/http/cgi/host_test.go | 27 |
2 files changed, 38 insertions, 13 deletions
diff --git a/libgo/go/net/http/cgi/host.go b/libgo/go/net/http/cgi/host.go index 4efbe7a..9b4d875 100644 --- a/libgo/go/net/http/cgi/host.go +++ b/libgo/go/net/http/cgi/host.go @@ -77,15 +77,15 @@ type Handler struct { // Env: []string{"SCRIPT_FILENAME=foo.php"}, // } func removeLeadingDuplicates(env []string) (ret []string) { - n := len(env) - for i := 0; i < n; i++ { - e := env[i] - s := strings.SplitN(e, "=", 2)[0] + for i, e := range env { found := false - for j := i + 1; j < n; j++ { - if s == strings.SplitN(env[j], "=", 2)[0] { - found = true - break + if eq := strings.IndexByte(e, '='); eq != -1 { + keq := e[:eq+1] // "key=" + for _, e2 := range env[i+1:] { + if strings.HasPrefix(e2, keq) { + found = true + break + } } } if !found { @@ -159,10 +159,6 @@ func (h *Handler) ServeHTTP(rw http.ResponseWriter, req *http.Request) { env = append(env, "CONTENT_TYPE="+ctype) } - if h.Env != nil { - env = append(env, h.Env...) - } - envPath := os.Getenv("PATH") if envPath == "" { envPath = "/bin:/usr/bin:/usr/ucb:/usr/bsd:/usr/local/bin" @@ -181,6 +177,10 @@ func (h *Handler) ServeHTTP(rw http.ResponseWriter, req *http.Request) { } } + if h.Env != nil { + env = append(env, h.Env...) + } + env = removeLeadingDuplicates(env) var cwd, path string diff --git a/libgo/go/net/http/cgi/host_test.go b/libgo/go/net/http/cgi/host_test.go index f341110..fb7d66a 100644 --- a/libgo/go/net/http/cgi/host_test.go +++ b/libgo/go/net/http/cgi/host_test.go @@ -16,6 +16,7 @@ import ( "os" "os/exec" "path/filepath" + "reflect" "runtime" "strconv" "strings" @@ -488,12 +489,36 @@ func TestEnvOverride(t *testing.T) { Args: []string{cgifile}, Env: []string{ "SCRIPT_FILENAME=" + cgifile, - "REQUEST_URI=/foo/bar"}, + "REQUEST_URI=/foo/bar", + "PATH=/wibble"}, } expectedMap := map[string]string{ "cwd": cwd, "env-SCRIPT_FILENAME": cgifile, "env-REQUEST_URI": "/foo/bar", + "env-PATH": "/wibble", } runCgiTest(t, h, "GET /test.cgi HTTP/1.0\nHost: example.com\n\n", expectedMap) } + +func TestRemoveLeadingDuplicates(t *testing.T) { + tests := []struct { + env []string + want []string + }{ + { + env: []string{"a=b", "b=c", "a=b2"}, + want: []string{"b=c", "a=b2"}, + }, + { + env: []string{"a=b", "b=c", "d", "e=f"}, + want: []string{"a=b", "b=c", "d", "e=f"}, + }, + } + for _, tt := range tests { + got := removeLeadingDuplicates(tt.env) + if !reflect.DeepEqual(got, tt.want) { + t.Errorf("removeLeadingDuplicates(%q) = %q; want %q", tt.env, got, tt.want) + } + } +} |