Fix issue #1346 {path} logging {uri} and add {rewrite_uri} placeholder (#1481)

* Fixed issue with {path} actually {uri}

* Test added for path rewrite

* add in uri_escaped

* added rewrite_uri and test

* fix broken test.  Just checks for existance of rewrite header

* gitignore

* Use context to store uri value

* ignore .vscode

* tidy up, removal of comments and invalidated tests

* Remove commented out code.

* added comment as requested by lint

* fixed spelling mistake

* clarified code with variable name

* added context for uri and test

* added TODO comment to move consts
This commit is contained in:
Toby Allen
2017-03-11 21:59:47 +00:00
committed by Matt Holt
parent 6aa0e30af3
commit cfe52084aa
8 changed files with 78 additions and 48 deletions

View File

@ -240,15 +240,23 @@ func (r *replacer) getSubstitution(key string) string {
case "{path}":
// if a rewrite has happened, the original URI should be used as the path
// rather than the rewritten URI
path := r.request.Header.Get("Caddy-Rewrite-Original-URI")
if path == "" {
var path string
origpath, _ := r.request.Context().Value(caddy.URIxRewriteCtxKey).(string)
if origpath == "" {
path = r.request.URL.Path
} else {
parsedURL, _ := url.Parse(origpath)
path = parsedURL.Path
}
return path
case "{path_escaped}":
path := r.request.Header.Get("Caddy-Rewrite-Original-URI")
if path == "" {
var path string
origpath, _ := r.request.Context().Value(caddy.URIxRewriteCtxKey).(string)
if origpath == "" {
path = r.request.URL.Path
} else {
parsedURL, _ := url.Parse(origpath)
path = parsedURL.Path
}
return url.QueryEscape(path)
case "{rewrite_path}":
@ -276,8 +284,20 @@ func (r *replacer) getSubstitution(key string) string {
}
return port
case "{uri}":
return r.request.URL.RequestURI()
uri, _ := r.request.Context().Value(caddy.URIxRewriteCtxKey).(string)
if uri == "" {
uri = r.request.URL.RequestURI()
}
return uri
case "{uri_escaped}":
uri, _ := r.request.Context().Value(caddy.URIxRewriteCtxKey).(string)
if uri == "" {
uri = r.request.URL.RequestURI()
}
return url.QueryEscape(uri)
case "{rewrite_uri}":
return r.request.URL.RequestURI()
case "{rewrite_uri_escaped}":
return url.QueryEscape(r.request.URL.RequestURI())
case "{when}":
return now().Format(timeFormat)

View File

@ -1,12 +1,15 @@
package httpserver
import (
"context"
"net/http"
"net/http/httptest"
"os"
"strings"
"testing"
"time"
"github.com/mholt/caddy"
)
func TestNewReplacer(t *testing.T) {
@ -149,6 +152,40 @@ func TestSet(t *testing.T) {
}
}
// Test function to test that various placeholders hold correct values after a rewrite
// has been performed. The NewRequest actually contains the rewritten value.
func TestPathRewrite(t *testing.T) {
w := httptest.NewRecorder()
recordRequest := NewResponseRecorder(w)
reader := strings.NewReader(`{"username": "dennis"}`)
request, err := http.NewRequest("POST", "http://getcaddy.com/index.php?key=value", reader)
if err != nil {
t.Fatalf("Request Formation Failed: %s\n", err.Error())
}
ctx := context.WithValue(request.Context(), caddy.URIxRewriteCtxKey, "a/custom/path.php?key=value")
request = request.WithContext(ctx)
repl := NewReplacer(request, recordRequest, "")
if repl.Replace("This path is '{path}'") != "This path is 'a/custom/path.php'" {
t.Error("Expected host {path} replacement failed (" + repl.Replace("This path is '{path}'") + ")")
}
if repl.Replace("This path is {rewrite_path}") != "This path is /index.php" {
t.Error("Expected host {rewrite_path} replacement failed (" + repl.Replace("This path is {rewrite_path}") + ")")
}
if repl.Replace("This path is '{uri}'") != "This path is 'a/custom/path.php?key=value'" {
t.Error("Expected host {uri} replacement failed (" + repl.Replace("This path is '{uri}'") + ")")
}
if repl.Replace("This path is {rewrite_uri}") != "This path is /index.php?key=value" {
t.Error("Expected host {rewrite_uri} replacement failed (" + repl.Replace("This path is {rewrite_uri}") + ")")
}
}
func TestRound(t *testing.T) {
var tests = map[time.Duration]time.Duration{
// 599.935µs -> 560µs