From a1af9aa8e2ae19881cf59e83134150b3a20df738 Mon Sep 17 00:00:00 2001 From: Will Norris Date: Thu, 26 May 2016 13:22:17 -0700 Subject: [PATCH] handle 'cleaned' remote URLs If imageproxy runs behind an http.ServeMux or certain web servers, the double slash in the remote URL will get collapsed down to a single slash. (e.g. http://example.com/ becomes http:/example.com/). This is now handled by imageproxy directly. Ref #65 --- data.go | 6 ++++++ data_test.go | 4 ++++ imageproxy.go | 4 ---- 3 files changed, 10 insertions(+), 4 deletions(-) diff --git a/data.go b/data.go index c5c5e2f..d3295fe 100644 --- a/data.go +++ b/data.go @@ -19,6 +19,7 @@ import ( "fmt" "net/http" "net/url" + "regexp" "strconv" "strings" ) @@ -221,6 +222,10 @@ func (r Request) String() 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 // the remote image URL are specified in the request path, formatted as: // /{options}/{remote_url}. Options may be omitted, so a request path may @@ -239,6 +244,7 @@ func NewRequest(r *http.Request, baseURL *url.URL) (*Request, error) { req := &Request{Original: r} path := r.URL.Path[1:] // strip leading slash + path = reCleanedURL.ReplaceAllString(path, "$1://$2") req.URL, err = url.Parse(path) if err != nil || !req.URL.IsAbs() { // first segment should be options diff --git a/data_test.go b/data_test.go index 735448f..fc18d9b 100644 --- a/data_test.go +++ b/data_test.go @@ -143,6 +143,10 @@ func TestNewRequest(t *testing.T) { "http://localhost//http://example.com/foo?bar", "http://example.com/foo?bar", emptyOptions, false, }, + { + "http://localhost/http:/example.com/foo", + "http://example.com/foo", emptyOptions, false, + }, } for _, tt := range tests { diff --git a/imageproxy.go b/imageproxy.go index aafcd59..d050283 100644 --- a/imageproxy.go +++ b/imageproxy.go @@ -35,10 +35,6 @@ import ( ) // Proxy serves image requests. -// -// Note that a Proxy should not be run behind a http.ServeMux, since the -// ServeMux aggressively cleans URLs and removes the double slash in the -// embedded request URL. type Proxy struct { Client *http.Client // client used to fetch remote URLs Cache Cache // cache used to cache responses