Fix up matchers tests and take care of TODO in rewrite

This commit is contained in:
Matthew Holt 2019-05-21 13:10:14 -06:00
parent 9d54f655aa
commit 67d32e6779
3 changed files with 9 additions and 10 deletions

View File

@ -240,10 +240,10 @@ func TestPathREMatcher(t *testing.T) {
} }
for key, expectVal := range tc.expectRepl { for key, expectVal := range tc.expectRepl {
placeholder := fmt.Sprintf("{matchers.path_regexp.%s}", key) placeholder := fmt.Sprintf("{http.matchers.path_regexp.%s}", key)
actualVal := repl.ReplaceAll(placeholder, "<empty>") actualVal := repl.ReplaceAll(placeholder, "<empty>")
if actualVal != expectVal { if actualVal != expectVal {
t.Errorf("Test %d [%v]: Expected placeholder {matchers.path_regexp.%s} to be '%s' but got '%s'", t.Errorf("Test %d [%v]: Expected placeholder {http.matchers.path_regexp.%s} to be '%s' but got '%s'",
i, tc.match.Pattern, key, expectVal, actualVal) i, tc.match.Pattern, key, expectVal, actualVal)
continue continue
} }
@ -358,10 +358,10 @@ func TestHeaderREMatcher(t *testing.T) {
} }
for key, expectVal := range tc.expectRepl { for key, expectVal := range tc.expectRepl {
placeholder := fmt.Sprintf("{matchers.header_regexp.%s}", key) placeholder := fmt.Sprintf("{http.matchers.header_regexp.%s}", key)
actualVal := repl.ReplaceAll(placeholder, "<empty>") actualVal := repl.ReplaceAll(placeholder, "<empty>")
if actualVal != expectVal { if actualVal != expectVal {
t.Errorf("Test %d [%v]: Expected placeholder {matchers.header_regexp.%s} to be '%s' but got '%s'", t.Errorf("Test %d [%v]: Expected placeholder {http.matchers.header_regexp.%s} to be '%s' but got '%s'",
i, tc.match, key, expectVal, actualVal) i, tc.match, key, expectVal, actualVal)
continue continue
} }

View File

@ -48,16 +48,17 @@ func addHTTPVarsToReplacer(repl caddy2.Replacer, req *http.Request, w http.Respo
dir, _ := path.Split(req.URL.Path) dir, _ := path.Split(req.URL.Path)
return dir return dir
}() }()
m["http.request.uri.query"] = req.URL.RawQuery
for param, vals := range req.URL.Query() {
m["http.request.uri.query."+param] = strings.Join(vals, ",")
}
for field, vals := range req.Header { for field, vals := range req.Header {
m["http.request.header."+strings.ToLower(field)] = strings.Join(vals, ",") m["http.request.header."+strings.ToLower(field)] = strings.Join(vals, ",")
} }
for _, cookie := range req.Cookies() { for _, cookie := range req.Cookies() {
m["http.request.cookie."+cookie.Name] = cookie.Value m["http.request.cookie."+cookie.Name] = cookie.Value
} }
for param, vals := range req.URL.Query() {
m["http.request.uri.query."+param] = strings.Join(vals, ",")
}
hostLabels := strings.Split(req.Host, ".") hostLabels := strings.Split(req.Host, ".")
for i, label := range hostLabels { for i, label := range hostLabels {

View File

@ -36,17 +36,15 @@ func (rewr Rewrite) ServeHTTP(w http.ResponseWriter, r *http.Request, next caddy
} }
if rewr.URI != "" { if rewr.URI != "" {
// TODO: clean this all up, I don't think it's right
oldURI := r.RequestURI oldURI := r.RequestURI
newURI := repl.ReplaceAll(rewr.URI, "") newURI := repl.ReplaceAll(rewr.URI, "")
u, err := url.Parse(newURI) u, err := url.Parse(newURI)
if err != nil { if err != nil {
return caddyhttp.Error(http.StatusInternalServerError, err) return caddyhttp.Error(http.StatusInternalServerError, err)
} }
r.RequestURI = newURI r.RequestURI = newURI
r.URL.Path = u.Path r.URL.Path = u.Path
if u.RawQuery != "" { if u.RawQuery != "" {
r.URL.RawQuery = u.RawQuery r.URL.RawQuery = u.RawQuery