diff --git a/caddyhttp/fastcgi/fastcgi.go b/caddyhttp/fastcgi/fastcgi.go index c3e1cab9..8d0f282a 100644 --- a/caddyhttp/fastcgi/fastcgi.go +++ b/caddyhttp/fastcgi/fastcgi.go @@ -31,6 +31,11 @@ type Handler struct { ServerPort string } +// When a rewrite is performed, a header field of this name +// is added to the request +// It contains the original request URI before the rewrite. +const internalRewriteFieldName = "Caddy-Rewrite-Original-URI" + // ServeHTTP satisfies the httpserver.Handler interface. func (h Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error) { for _, rule := range h.Rules { @@ -201,11 +206,9 @@ func (h Handler) buildEnv(r *http.Request, rule Rule, fpath string) (map[string] // or it might have been rewritten internally by the rewrite middleware (see issue #256). // If it was rewritten, there will be a header indicating the original URL, // which is needed to get the correct RequestURI value for PHP apps. - const internalRewriteFieldName = "Caddy-Rewrite-Original-URI" reqURI := r.URL.RequestURI() if origURI := r.Header.Get(internalRewriteFieldName); origURI != "" { reqURI = origURI - r.Header.Del(internalRewriteFieldName) } // Some variables are unused but cleared explicitly to prevent @@ -258,13 +261,15 @@ func (h Handler) buildEnv(r *http.Request, rule Rule, fpath string) (map[string] env[envVar[0]] = replacer.Replace(envVar[1]) } - // Add all HTTP headers to env variables + // Add all HTTP headers (except Caddy-Rewrite-Original-URI ) to env variables for field, val := range r.Header { + if strings.ToLower(field) == strings.ToLower(internalRewriteFieldName) { + continue + } header := strings.ToUpper(field) header = headerNameReplacer.Replace(header) env["HTTP_"+header] = strings.Join(val, ", ") } - return env, nil } diff --git a/caddyhttp/fastcgi/fastcgi_test.go b/caddyhttp/fastcgi/fastcgi_test.go index 46f06c9b..4b22ea95 100644 --- a/caddyhttp/fastcgi/fastcgi_test.go +++ b/caddyhttp/fastcgi/fastcgi_test.go @@ -7,6 +7,7 @@ import ( "net/http/httptest" "net/url" "strconv" + "strings" "sync" "testing" ) @@ -230,6 +231,9 @@ func TestBuildEnv(t *testing.T) { Host: "localhost:2015", RemoteAddr: "[2b02:1810:4f2d:9400:70ab:f822:be8a:9093]:51688", RequestURI: "/fgci_test.php", + Header: map[string][]string{ + "Foo": {"Bar", "two"}, + }, } } @@ -293,4 +297,24 @@ func TestBuildEnv(t *testing.T) { envExpected["CUSTOM_URI"] = "custom_uri/fgci_test.php?test=blabla" envExpected["CUSTOM_QUERY"] = "custom=true&test=blabla" testBuildEnv(r, rule, fpath, envExpected) + + // 6. Test Caddy-Rewrite-Original-URI header is not removed + r = newReq() + rule.EnvVars = [][2]string{ + {"HTTP_HOST", "{host}"}, + {"CUSTOM_URI", "custom_uri{uri}"}, + {"CUSTOM_QUERY", "custom=true&{query}"}, + } + envExpected = newEnv() + envExpected["HTTP_HOST"] = "localhost:2015" + envExpected["CUSTOM_URI"] = "custom_uri/fgci_test.php?test=blabla" + envExpected["CUSTOM_QUERY"] = "custom=true&test=blabla" + httpFieldName := strings.ToUpper(internalRewriteFieldName) + envExpected["HTTP_"+httpFieldName] = "" + r.Header.Add(internalRewriteFieldName, "/apath/torewrite/index.php") + testBuildEnv(r, rule, fpath, envExpected) + if r.Header.Get(internalRewriteFieldName) == "" { + t.Errorf("Error: Header Expected %v", internalRewriteFieldName) + } + } diff --git a/caddyhttp/fastcgi/setup_test.go b/caddyhttp/fastcgi/setup_test.go index b9fd6b0d..e53bc605 100644 --- a/caddyhttp/fastcgi/setup_test.go +++ b/caddyhttp/fastcgi/setup_test.go @@ -125,6 +125,17 @@ func TestFastcgiParse(t *testing.T) { dialer: &persistentDialer{size: 5, network: network, address: address}, IndexFiles: []string{}, }}}, + {`fastcgi / ` + defaultAddress + ` { + split .php + }`, + false, []Rule{{ + Path: "/", + Address: defaultAddress, + Ext: "", + SplitPath: ".php", + dialer: basicDialer{network: network, address: address}, + IndexFiles: []string{}, + }}}, } for i, test := range tests { actualFastcgiConfigs, err := fastcgiParse(caddy.NewTestController("http", test.inputFastcgiConfig))