Skip to content

Commit b61992e

Browse files
committed
finish hadnling 'cleaned' remote URLs
1 parent a1af9aa commit b61992e

2 files changed

Lines changed: 15 additions & 7 deletions

File tree

data.go

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -222,10 +222,6 @@ func (r Request) String() string {
222222
return u.String()
223223
}
224224

225-
// reCleanedURL matches an absolute HTTP URL that has been munged by path.Clean
226-
// or a webserver that collapses multiple slashes.
227-
var reCleanedURL = regexp.MustCompile(`^(https?):/([^/])`)
228-
229225
// NewRequest parses an http.Request into an imageproxy Request. Options and
230226
// the remote image URL are specified in the request path, formatted as:
231227
// /{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) {
244240
req := &Request{Original: r}
245241

246242
path := r.URL.Path[1:] // strip leading slash
247-
path = reCleanedURL.ReplaceAllString(path, "$1://$2")
248-
req.URL, err = url.Parse(path)
243+
req.URL, err = parseURL(path)
249244
if err != nil || !req.URL.IsAbs() {
250245
// first segment should be options
251246
parts := strings.SplitN(path, "/", 2)
@@ -254,7 +249,7 @@ func NewRequest(r *http.Request, baseURL *url.URL) (*Request, error) {
254249
}
255250

256251
var err error
257-
req.URL, err = url.Parse(parts[1])
252+
req.URL, err = parseURL(parts[1])
258253
if err != nil {
259254
return nil, URLError{fmt.Sprintf("unable to parse remote URL: %v", err), r.URL}
260255
}
@@ -278,3 +273,12 @@ func NewRequest(r *http.Request, baseURL *url.URL) (*Request, error) {
278273
req.URL.RawQuery = r.URL.RawQuery
279274
return req, nil
280275
}
276+
277+
var reCleanedURL = regexp.MustCompile(`^(https?):/+([^/])`)
278+
279+
// parseURL parses s as a URL, handling URLs that have been munged by
280+
// path.Clean or a webserver that collapses multiple slashes.
281+
func parseURL(s string) (*url.URL, error) {
282+
s = reCleanedURL.ReplaceAllString(s, "$1://$2")
283+
return url.Parse(s)
284+
}

data_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,10 @@ func TestNewRequest(t *testing.T) {
147147
"http://localhost/http:/example.com/foo",
148148
"http://example.com/foo", emptyOptions, false,
149149
},
150+
{
151+
"http://localhost/http:///example.com/foo",
152+
"http://example.com/foo", emptyOptions, false,
153+
},
150154
}
151155

152156
for _, tt := range tests {

0 commit comments

Comments
 (0)