2016-06-06 11:51:56 +08:00
|
|
|
package rewrite
|
|
|
|
|
|
|
|
import (
|
httpserver/all: Clean up and standardize request URL handling (#1633)
* httpserver/all: Clean up and standardize request URL handling
The HTTP server now always creates a context value on the request which
is a copy of the request's URL struct. It should not be modified by
middlewares, but it is safe to get the value out of the request and make
changes to it locally-scoped. Thus, the value in the context always
stores the original request URL information as it was received. Any
rewrites that happen will be to the request's URL field directly.
The HTTP server no longer cleans /sanitizes the request URL. It made too
many strong assumptions and ended up making a lot of middleware more
complicated, including upstream proxying (and fastcgi). To alleviate
this complexity, we no longer change the request URL. Middlewares are
responsible to access the disk safely by using http.Dir or, if not
actually opening files, they can use httpserver.SafePath().
I'm hoping this will address issues with #1624, #1584, #1582, and others.
* staticfiles: Fix test on Windows
@abiosoft: I still can't figure out exactly what this is for. 😅
* Use (potentially) changed URL for browse redirects, as before
* Use filepath.ToSlash, clean up a couple proxy test cases
* Oops, fix variable name
2017-05-02 13:11:10 +08:00
|
|
|
"context"
|
2016-06-06 11:51:56 +08:00
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
"testing"
|
httpserver/all: Clean up and standardize request URL handling (#1633)
* httpserver/all: Clean up and standardize request URL handling
The HTTP server now always creates a context value on the request which
is a copy of the request's URL struct. It should not be modified by
middlewares, but it is safe to get the value out of the request and make
changes to it locally-scoped. Thus, the value in the context always
stores the original request URL information as it was received. Any
rewrites that happen will be to the request's URL field directly.
The HTTP server no longer cleans /sanitizes the request URL. It made too
many strong assumptions and ended up making a lot of middleware more
complicated, including upstream proxying (and fastcgi). To alleviate
this complexity, we no longer change the request URL. Middlewares are
responsible to access the disk safely by using http.Dir or, if not
actually opening files, they can use httpserver.SafePath().
I'm hoping this will address issues with #1624, #1584, #1582, and others.
* staticfiles: Fix test on Windows
@abiosoft: I still can't figure out exactly what this is for. 😅
* Use (potentially) changed URL for browse redirects, as before
* Use filepath.ToSlash, clean up a couple proxy test cases
* Oops, fix variable name
2017-05-02 13:11:10 +08:00
|
|
|
|
|
|
|
"github.com/mholt/caddy/caddyhttp/httpserver"
|
2016-06-06 11:51:56 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestTo(t *testing.T) {
|
|
|
|
fs := http.Dir("testdata")
|
|
|
|
tests := []struct {
|
|
|
|
url string
|
|
|
|
to string
|
|
|
|
expected string
|
|
|
|
}{
|
|
|
|
{"/", "/somefiles", "/somefiles"},
|
|
|
|
{"/somefiles", "/somefiles /index.php{uri}", "/index.php/somefiles"},
|
|
|
|
{"/somefiles", "/testfile /index.php{uri}", "/testfile"},
|
|
|
|
{"/somefiles", "/testfile/ /index.php{uri}", "/index.php/somefiles"},
|
|
|
|
{"/somefiles", "/somefiles /index.php{uri}", "/index.php/somefiles"},
|
|
|
|
{"/?a=b", "/somefiles /index.php?{query}", "/index.php?a=b"},
|
|
|
|
{"/?a=b", "/testfile /index.php?{query}", "/testfile?a=b"},
|
|
|
|
{"/?a=b", "/testdir /index.php?{query}", "/index.php?a=b"},
|
|
|
|
{"/?a=b", "/testdir/ /index.php?{query}", "/testdir/?a=b"},
|
2016-06-22 08:44:16 +08:00
|
|
|
{"/test?url=http://", " /p/{path}?{query}", "/p/test?url=http://"},
|
2017-01-24 13:15:27 +08:00
|
|
|
{"/test?url=http://", " /p/{rewrite_path}?{query}", "/p/test?url=http://"},
|
2016-06-22 08:44:16 +08:00
|
|
|
{"/test/?url=http://", " /{uri}", "/test/?url=http://"},
|
2016-06-06 11:51:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
uri := func(r *url.URL) string {
|
|
|
|
uri := r.Path
|
|
|
|
if r.RawQuery != "" {
|
|
|
|
uri += "?" + r.RawQuery
|
|
|
|
}
|
|
|
|
return uri
|
|
|
|
}
|
|
|
|
for i, test := range tests {
|
|
|
|
r, err := http.NewRequest("GET", test.url, nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
httpserver/all: Clean up and standardize request URL handling (#1633)
* httpserver/all: Clean up and standardize request URL handling
The HTTP server now always creates a context value on the request which
is a copy of the request's URL struct. It should not be modified by
middlewares, but it is safe to get the value out of the request and make
changes to it locally-scoped. Thus, the value in the context always
stores the original request URL information as it was received. Any
rewrites that happen will be to the request's URL field directly.
The HTTP server no longer cleans /sanitizes the request URL. It made too
many strong assumptions and ended up making a lot of middleware more
complicated, including upstream proxying (and fastcgi). To alleviate
this complexity, we no longer change the request URL. Middlewares are
responsible to access the disk safely by using http.Dir or, if not
actually opening files, they can use httpserver.SafePath().
I'm hoping this will address issues with #1624, #1584, #1582, and others.
* staticfiles: Fix test on Windows
@abiosoft: I still can't figure out exactly what this is for. 😅
* Use (potentially) changed URL for browse redirects, as before
* Use filepath.ToSlash, clean up a couple proxy test cases
* Oops, fix variable name
2017-05-02 13:11:10 +08:00
|
|
|
ctx := context.WithValue(r.Context(), httpserver.OriginalURLCtxKey, *r.URL)
|
|
|
|
r = r.WithContext(ctx)
|
2016-06-06 11:51:56 +08:00
|
|
|
To(fs, r, test.to, newReplacer(r))
|
|
|
|
if uri(r.URL) != test.expected {
|
|
|
|
t.Errorf("Test %v: expected %v found %v", i, test.expected, uri(r.URL))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|