From d8be787f398239f3c25b888e2b2e45d15a558011 Mon Sep 17 00:00:00 2001 From: MathiasB Date: Thu, 28 Jan 2016 15:26:33 +0100 Subject: [PATCH 1/3] FastCGI: IPv6 when parsing r.RemoteAddr --- middleware/fastcgi/fastcgi.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/middleware/fastcgi/fastcgi.go b/middleware/fastcgi/fastcgi.go index 517505b6d..153cae7f6 100755 --- a/middleware/fastcgi/fastcgi.go +++ b/middleware/fastcgi/fastcgi.go @@ -182,7 +182,7 @@ func (h Handler) buildEnv(r *http.Request, rule Rule, fpath string) (map[string] // Separate remote IP and port; more lenient than net.SplitHostPort var ip, port string - if idx := strings.Index(r.RemoteAddr, ":"); idx > -1 { + if idx := strings.LastIndex(r.RemoteAddr, ":"); idx > -1 { ip = r.RemoteAddr[:idx] port = r.RemoteAddr[idx+1:] } else { From ac197f1694cdacd8cfe8f5aeaddf49326d5cc21e Mon Sep 17 00:00:00 2001 From: MathiasB Date: Fri, 29 Jan 2016 11:46:06 +0100 Subject: [PATCH 2/3] FastCGI: some simple tests for buildEnv More tests are needed for the other environmental variables. These tests were specifically made for testing of IP addresses. --- middleware/fastcgi/fastcgi_test.go | 64 ++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) diff --git a/middleware/fastcgi/fastcgi_test.go b/middleware/fastcgi/fastcgi_test.go index 69ee02f33..0d391a237 100644 --- a/middleware/fastcgi/fastcgi_test.go +++ b/middleware/fastcgi/fastcgi_test.go @@ -1,6 +1,8 @@ package fastcgi import ( + "net/http" + "net/url" "testing" ) @@ -29,3 +31,65 @@ func TestRuleParseAddress(t *testing.T) { } } + +func BuildEnvSingle(r *http.Request, rule Rule, fpath string, envExpected map[string]string, t *testing.T) { + + h := Handler{} + + env, err := h.buildEnv(r, rule, fpath) + if err != nil { + t.Error("Unexpected error:", err.Error()) + } + + for k, v := range envExpected { + if env[k] != v { + t.Errorf("Unexpected %v. Got %v, expected %v", k, env[k], v) + } + } + +} + +func TestBuildEnv(t *testing.T) { + + rule := Rule{} + url, err := url.Parse("http://localhost:2015/fgci_test.php?test=blabla") + if err != nil { + t.Error("Unexpected error:", err.Error()) + } + + r := http.Request{ + Method: "GET", + URL: url, + Proto: "HTTP/1.1", + ProtoMajor: 1, + ProtoMinor: 1, + Host: "localhost:2015", + RemoteAddr: "[2b02:1810:4f2d:9400:70ab:f822:be8a:9093]:51688", + RequestURI: "/fgci_test.php", + } + + fpath := "/fgci_test.php" + + var envExpected = map[string]string{ + "REMOTE_ADDR": "[2b02:1810:4f2d:9400:70ab:f822:be8a:9093]", + "REMOTE_PORT": "51688", + "SERVER_PROTOCOL": "HTTP/1.1", + "QUERY_STRING": "test=blabla", + "REQUEST_METHOD": "GET", + "HTTP_HOST": "localhost:2015", + } + + // 1. Test for full canonical IPv6 address + BuildEnvSingle(&r, rule, fpath, envExpected, t) + + // 2. Test for shorthand notation of IPv6 address + r.RemoteAddr = "[::1]:51688" + envExpected["REMOTE_ADDR"] = "[::1]" + BuildEnvSingle(&r, rule, fpath, envExpected, t) + + // 3. Test for IPv4 address + r.RemoteAddr = "192.168.0.10:51688" + envExpected["REMOTE_ADDR"] = "192.168.0.10" + BuildEnvSingle(&r, rule, fpath, envExpected, t) + +} From c59fd1c76ed35b8f0f767c34285767ab1026be2c Mon Sep 17 00:00:00 2001 From: MathiasB Date: Mon, 1 Feb 2016 09:39:13 +0100 Subject: [PATCH 3/3] Defined test function in TestBuildEnv --- middleware/fastcgi/fastcgi_test.go | 40 +++++++++++++++--------------- 1 file changed, 20 insertions(+), 20 deletions(-) diff --git a/middleware/fastcgi/fastcgi_test.go b/middleware/fastcgi/fastcgi_test.go index 0d391a237..1fc7446d0 100644 --- a/middleware/fastcgi/fastcgi_test.go +++ b/middleware/fastcgi/fastcgi_test.go @@ -32,25 +32,25 @@ func TestRuleParseAddress(t *testing.T) { } -func BuildEnvSingle(r *http.Request, rule Rule, fpath string, envExpected map[string]string, t *testing.T) { - - h := Handler{} - - env, err := h.buildEnv(r, rule, fpath) - if err != nil { - t.Error("Unexpected error:", err.Error()) - } - - for k, v := range envExpected { - if env[k] != v { - t.Errorf("Unexpected %v. Got %v, expected %v", k, env[k], v) - } - } - -} - func TestBuildEnv(t *testing.T) { + buildEnvSingle := func(r *http.Request, rule Rule, fpath string, envExpected map[string]string, t *testing.T) { + + h := Handler{} + + env, err := h.buildEnv(r, rule, fpath) + if err != nil { + t.Error("Unexpected error:", err.Error()) + } + + for k, v := range envExpected { + if env[k] != v { + t.Errorf("Unexpected %v. Got %v, expected %v", k, env[k], v) + } + } + + } + rule := Rule{} url, err := url.Parse("http://localhost:2015/fgci_test.php?test=blabla") if err != nil { @@ -80,16 +80,16 @@ func TestBuildEnv(t *testing.T) { } // 1. Test for full canonical IPv6 address - BuildEnvSingle(&r, rule, fpath, envExpected, t) + buildEnvSingle(&r, rule, fpath, envExpected, t) // 2. Test for shorthand notation of IPv6 address r.RemoteAddr = "[::1]:51688" envExpected["REMOTE_ADDR"] = "[::1]" - BuildEnvSingle(&r, rule, fpath, envExpected, t) + buildEnvSingle(&r, rule, fpath, envExpected, t) // 3. Test for IPv4 address r.RemoteAddr = "192.168.0.10:51688" envExpected["REMOTE_ADDR"] = "192.168.0.10" - BuildEnvSingle(&r, rule, fpath, envExpected, t) + buildEnvSingle(&r, rule, fpath, envExpected, t) }