Commit d3ae2d27 authored by Brad Fitzpatrick's avatar Brad Fitzpatrick

net/http/cgi: optimize internal function removeLeadingDuplicates a bit

Change-Id: I0255f24f5c5925ea4daa28a28d23606df35d4373
Reviewed-on: https://go-review.googlesource.com/15824Reviewed-by: default avatarAndrew Gerrand <adg@golang.org>
parent 167a7123
...@@ -77,15 +77,15 @@ type Handler struct { ...@@ -77,15 +77,15 @@ type Handler struct {
// Env: []string{"SCRIPT_FILENAME=foo.php"}, // Env: []string{"SCRIPT_FILENAME=foo.php"},
// } // }
func removeLeadingDuplicates(env []string) (ret []string) { func removeLeadingDuplicates(env []string) (ret []string) {
n := len(env) for i, e := range env {
for i := 0; i < n; i++ {
e := env[i]
s := strings.SplitN(e, "=", 2)[0]
found := false found := false
for j := i + 1; j < n; j++ { if eq := strings.IndexByte(e, '='); eq != -1 {
if s == strings.SplitN(env[j], "=", 2)[0] { keq := e[:eq+1] // "key="
found = true for _, e2 := range env[i+1:] {
break if strings.HasPrefix(e2, keq) {
found = true
break
}
} }
} }
if !found { if !found {
......
...@@ -16,6 +16,7 @@ import ( ...@@ -16,6 +16,7 @@ import (
"os" "os"
"os/exec" "os/exec"
"path/filepath" "path/filepath"
"reflect"
"runtime" "runtime"
"strconv" "strconv"
"strings" "strings"
...@@ -498,3 +499,25 @@ func TestEnvOverride(t *testing.T) { ...@@ -498,3 +499,25 @@ func TestEnvOverride(t *testing.T) {
} }
runCgiTest(t, h, "GET /test.cgi HTTP/1.0\nHost: example.com\n\n", expectedMap) 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)
}
}
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment