mirror of
https://github.com/willnorris/imageproxy.git
synced 2024-12-16 21:56:43 -05:00
finish hadnling 'cleaned' remote URLs
This commit is contained in:
parent
a1af9aa8e2
commit
b61992ec51
2 changed files with 15 additions and 7 deletions
18
data.go
18
data.go
|
@ -222,10 +222,6 @@ func (r Request) String() string {
|
||||||
return u.String()
|
return u.String()
|
||||||
}
|
}
|
||||||
|
|
||||||
// reCleanedURL matches an absolute HTTP URL that has been munged by path.Clean
|
|
||||||
// or a webserver that collapses multiple slashes.
|
|
||||||
var reCleanedURL = regexp.MustCompile(`^(https?):/([^/])`)
|
|
||||||
|
|
||||||
// NewRequest parses an http.Request into an imageproxy Request. Options and
|
// NewRequest parses an http.Request into an imageproxy Request. Options and
|
||||||
// the remote image URL are specified in the request path, formatted as:
|
// the remote image URL are specified in the request path, formatted as:
|
||||||
// /{options}/{remote_url}. Options may be omitted, so a request path may
|
// /{options}/{remote_url}. Options may be omitted, so a request path may
|
||||||
|
@ -244,8 +240,7 @@ func NewRequest(r *http.Request, baseURL *url.URL) (*Request, error) {
|
||||||
req := &Request{Original: r}
|
req := &Request{Original: r}
|
||||||
|
|
||||||
path := r.URL.Path[1:] // strip leading slash
|
path := r.URL.Path[1:] // strip leading slash
|
||||||
path = reCleanedURL.ReplaceAllString(path, "$1://$2")
|
req.URL, err = parseURL(path)
|
||||||
req.URL, err = url.Parse(path)
|
|
||||||
if err != nil || !req.URL.IsAbs() {
|
if err != nil || !req.URL.IsAbs() {
|
||||||
// first segment should be options
|
// first segment should be options
|
||||||
parts := strings.SplitN(path, "/", 2)
|
parts := strings.SplitN(path, "/", 2)
|
||||||
|
@ -254,7 +249,7 @@ func NewRequest(r *http.Request, baseURL *url.URL) (*Request, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
var err error
|
var err error
|
||||||
req.URL, err = url.Parse(parts[1])
|
req.URL, err = parseURL(parts[1])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, URLError{fmt.Sprintf("unable to parse remote URL: %v", err), r.URL}
|
return nil, URLError{fmt.Sprintf("unable to parse remote URL: %v", err), r.URL}
|
||||||
}
|
}
|
||||||
|
@ -278,3 +273,12 @@ func NewRequest(r *http.Request, baseURL *url.URL) (*Request, error) {
|
||||||
req.URL.RawQuery = r.URL.RawQuery
|
req.URL.RawQuery = r.URL.RawQuery
|
||||||
return req, nil
|
return req, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var reCleanedURL = regexp.MustCompile(`^(https?):/+([^/])`)
|
||||||
|
|
||||||
|
// parseURL parses s as a URL, handling URLs that have been munged by
|
||||||
|
// path.Clean or a webserver that collapses multiple slashes.
|
||||||
|
func parseURL(s string) (*url.URL, error) {
|
||||||
|
s = reCleanedURL.ReplaceAllString(s, "$1://$2")
|
||||||
|
return url.Parse(s)
|
||||||
|
}
|
||||||
|
|
|
@ -147,6 +147,10 @@ func TestNewRequest(t *testing.T) {
|
||||||
"http://localhost/http:/example.com/foo",
|
"http://localhost/http:/example.com/foo",
|
||||||
"http://example.com/foo", emptyOptions, false,
|
"http://example.com/foo", emptyOptions, false,
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
"http://localhost/http:///example.com/foo",
|
||||||
|
"http://example.com/foo", emptyOptions, false,
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, tt := range tests {
|
for _, tt := range tests {
|
||||||
|
|
Loading…
Reference in a new issue