From 25dea2903e613534e00a2a4e305e0c8f2daeea8d Mon Sep 17 00:00:00 2001 From: Matthew Holt Date: Sat, 11 Jan 2020 13:47:42 -0700 Subject: http: A little more polish on rewrite handler and try_files directive --- modules/caddyhttp/fileserver/caddyfile.go | 28 ++++++++++++++++------------ 1 file changed, 16 insertions(+), 12 deletions(-) (limited to 'modules/caddyhttp/fileserver/caddyfile.go') diff --git a/modules/caddyhttp/fileserver/caddyfile.go b/modules/caddyhttp/fileserver/caddyfile.go index aa11ba0..d26b435 100644 --- a/modules/caddyhttp/fileserver/caddyfile.go +++ b/modules/caddyhttp/fileserver/caddyfile.go @@ -107,11 +107,17 @@ func parseCaddyfile(h httpcaddyfile.Helper) (caddyhttp.MiddlewareHandler, error) // try_files // } // } -// rewrite @try_files {http.matchers.file.relative}{http.request.uri.query_string} +// rewrite @try_files {http.matchers.file.relative} // -// If any of the files in the list have a query string, the query string will -// be ignored when checking for file existence, but will be augmented into -// the request's URI when rewriting the request. +// This directive rewrites request paths only, preserving any other part +// of the URI, unless the part is explicitly given in the file list. For +// example, if any of the files in the list have a query string: +// +// try_files {path} index.php?{query}&p={path} +// +// then the query string will not be treated as part of the file name; and +// if that file matches, the given query string will replace any query string +// that already exists on the request URI. func parseTryFiles(h httpcaddyfile.Helper) ([]httpcaddyfile.ConfigValue, error) { if !h.Next() { return nil, h.ArgErr() @@ -123,16 +129,14 @@ func parseTryFiles(h httpcaddyfile.Helper) ([]httpcaddyfile.ConfigValue, error) } // makeRoute returns a route that tries the files listed in try - // and then rewrites to the matched file, and appends writeURIAppend - // to the end of the query string. - makeRoute := func(try []string, writeURIAppend string) []httpcaddyfile.ConfigValue { + // and then rewrites to the matched file; userQueryString is + // appended to the rewrite rule. + makeRoute := func(try []string, userQueryString string) []httpcaddyfile.ConfigValue { handler := rewrite.Rewrite{ - URI: "{http.matchers.file.relative}{http.request.uri.query_string}" + writeURIAppend, + URI: "{http.matchers.file.relative}" + userQueryString, } matcherSet := caddy.ModuleMap{ - "file": h.JSON(MatchFile{ - TryFiles: try, - }), + "file": h.JSON(MatchFile{TryFiles: try}), } return h.NewRoute(matcherSet, handler) } @@ -149,7 +153,7 @@ func parseTryFiles(h httpcaddyfile.Helper) ([]httpcaddyfile.ConfigValue, error) result = append(result, makeRoute(try, "")...) try = []string{} } - result = append(result, makeRoute([]string{item[:idx]}, "&"+item[idx+1:])...) + result = append(result, makeRoute([]string{item[:idx]}, item[idx:])...) continue } // accumulate consecutive non-query-string parameters -- cgit v1.2.3