From 10484cfad2cdd852218c508db0f978161b9268ff Mon Sep 17 00:00:00 2001 From: John Chadwick Date: Sat, 2 Sep 2017 00:15:53 -0400 Subject: [PATCH] fastcgi: Fix SCRIPT_NAME when path in address (#1852) * Add tests for SCRIPT_NAME * fastcgi: Include vhost path prefix in SCRIPT_NAME --- caddyhttp/fastcgi/fastcgi.go | 6 ++++++ caddyhttp/fastcgi/fastcgi_test.go | 16 +++++++++++++++- 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/caddyhttp/fastcgi/fastcgi.go b/caddyhttp/fastcgi/fastcgi.go index 550a795b..450dc6fd 100644 --- a/caddyhttp/fastcgi/fastcgi.go +++ b/caddyhttp/fastcgi/fastcgi.go @@ -18,6 +18,7 @@ import ( "sync/atomic" "time" + "github.com/mholt/caddy" "github.com/mholt/caddy/caddyhttp/httpserver" ) @@ -248,6 +249,11 @@ func (h Handler) buildEnv(r *http.Request, rule Rule, fpath string) (map[string] // Strip PATH_INFO from SCRIPT_NAME scriptName = strings.TrimSuffix(scriptName, pathInfo) + // Add vhost path prefix to scriptName. Otherwise, some PHP software will + // have difficulty discovering its URL. + pathPrefix, _ := r.Context().Value(caddy.CtxKey("path_prefix")).(string) + scriptName = path.Join(pathPrefix, scriptName) + // Get the request URI from context. The context stores the original URI in case // it was changed by a middleware such as rewrite. By default, we pass the // original URI in as the value of REQUEST_URI (the user can overwrite this diff --git a/caddyhttp/fastcgi/fastcgi_test.go b/caddyhttp/fastcgi/fastcgi_test.go index 6f7afe74..de2ecf56 100644 --- a/caddyhttp/fastcgi/fastcgi_test.go +++ b/caddyhttp/fastcgi/fastcgi_test.go @@ -12,6 +12,7 @@ import ( "testing" "time" + "github.com/mholt/caddy" "github.com/mholt/caddy/caddyhttp/httpserver" ) @@ -122,7 +123,11 @@ func TestBuildEnv(t *testing.T) { } } - rule := Rule{} + rule := Rule{ + Ext: ".php", + SplitPath: ".php", + IndexFiles: []string{"index.php"}, + } url, err := url.Parse("http://localhost:2015/fgci_test.php?test=foobar") if err != nil { t.Error("Unexpected error:", err.Error()) @@ -156,6 +161,7 @@ func TestBuildEnv(t *testing.T) { "QUERY_STRING": "test=foobar", "REQUEST_METHOD": "GET", "HTTP_HOST": "localhost:2015", + "SCRIPT_NAME": "/fgci_test.php", } } @@ -206,6 +212,14 @@ func TestBuildEnv(t *testing.T) { envExpected["CUSTOM_URI"] = "custom_uri/fgci_test.php?test=foobar" envExpected["CUSTOM_QUERY"] = "custom=true&test=foobar" testBuildEnv(r, rule, fpath, envExpected) + + // 6. Test SCRIPT_NAME includes path prefix + r = newReq() + ctx := context.WithValue(r.Context(), caddy.CtxKey("path_prefix"), "/test") + r = r.WithContext(ctx) + envExpected = newEnv() + envExpected["SCRIPT_NAME"] = "/test/fgci_test.php" + testBuildEnv(r, rule, fpath, envExpected) } func TestReadTimeout(t *testing.T) {