mirror of
https://github.com/willnorris/imageproxy.git
synced 2026-05-13 14:03:21 +02:00
Added tests and fixed code get it passing
This commit is contained in:
parent
dcfb286874
commit
ec264026c9
2 changed files with 81 additions and 11 deletions
53
data.go
53
data.go
|
|
@ -337,11 +337,13 @@ func (r Request) String() string {
|
||||||
// http://localhost/http://example.com/image.jpg
|
// http://localhost/http://example.com/image.jpg
|
||||||
func NewRequest(r *http.Request, baseURL *url.URL) (*Request, error) {
|
func NewRequest(r *http.Request, baseURL *url.URL) (*Request, error) {
|
||||||
var err error
|
var err error
|
||||||
|
var additionalQuery string
|
||||||
req := &Request{Original: r}
|
req := &Request{Original: r}
|
||||||
|
|
||||||
path := r.URL.EscapedPath()[1:] // strip leading slash
|
path := r.URL.EscapedPath()[1:] // the image url is stored in the path, so get image url from path but strip leading slash so it's not like /https://example.com
|
||||||
path, err = decodeUrl(path)
|
path, err, additionalQuery = decodeURL(path) // Conditionally decode url path param if it is url encoded this enables us to process image urls with query params built in
|
||||||
req.URL, err = parseURL(path)
|
req.URL, err = parseURL(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)
|
||||||
|
|
@ -371,7 +373,7 @@ func NewRequest(r *http.Request, baseURL *url.URL) (*Request, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// query string is always part of the remote URL
|
// query string is always part of the remote URL
|
||||||
req.URL.RawQuery = r.URL.RawQuery
|
req.URL.RawQuery = combineQueries(r.URL.RawQuery, additionalQuery)
|
||||||
return req, nil
|
return req, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -386,11 +388,42 @@ func parseURL(s string) (*url.URL, error) {
|
||||||
|
|
||||||
var reIsEncodedUrl = regexp.MustCompile(`^https?%`)
|
var reIsEncodedUrl = regexp.MustCompile(`^https?%`)
|
||||||
|
|
||||||
func decodeUrl(s string) (string, error) {
|
func decodeURL(s string) (string, error, string) {
|
||||||
var isUrlEncoded = reIsEncodedUrl.MatchString(s)
|
var startsWithHttp = strings.HasPrefix(s, "http")
|
||||||
if isUrlEncoded {
|
var prefix = ""
|
||||||
return url.QueryUnescape(s)
|
var u = s
|
||||||
} else {
|
|
||||||
return s, nil
|
if !startsWithHttp && strings.Contains(s, "http") {
|
||||||
}
|
var parts = strings.SplitN(s, "http", 2)
|
||||||
|
u = "http" + parts[1]
|
||||||
|
prefix = parts[0]
|
||||||
|
}
|
||||||
|
|
||||||
|
var isUrlEncoded = reIsEncodedUrl.MatchString(u)
|
||||||
|
print(u, ": ", isUrlEncoded, "\n")
|
||||||
|
if isUrlEncoded {
|
||||||
|
u, err := url.QueryUnescape(u)
|
||||||
|
if err != nil {
|
||||||
|
return u, err, ""
|
||||||
|
}
|
||||||
|
|
||||||
|
var parsed, err2 = url.Parse(u)
|
||||||
|
if err2 != nil {
|
||||||
|
return u, err2, ""
|
||||||
|
}
|
||||||
|
|
||||||
|
return prefix + u, err2, parsed.RawQuery
|
||||||
|
} else {
|
||||||
|
return prefix + u, nil, ""
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func combineQueries(a string, b string) string {
|
||||||
|
if a == "" {
|
||||||
|
return b
|
||||||
|
} else if b == "" {
|
||||||
|
return a
|
||||||
|
} else {
|
||||||
|
return a + "&" + b
|
||||||
|
}
|
||||||
}
|
}
|
||||||
37
data_test.go
37
data_test.go
|
|
@ -156,6 +156,43 @@ func TestNewRequest(t *testing.T) {
|
||||||
"http://localhost/http://example.com/%2C",
|
"http://localhost/http://example.com/%2C",
|
||||||
"http://example.com/%2C", emptyOptions, false,
|
"http://example.com/%2C", emptyOptions, false,
|
||||||
},
|
},
|
||||||
|
// URI Encoded cases
|
||||||
|
{
|
||||||
|
"http://localhost/1x2/http%3A%2F%2Fexample.com%2Ffoo",
|
||||||
|
"http://example.com/foo", Options{Width: 1, Height: 2}, false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"http://localhost/1x2/http%3A%2F%2Fexample.com%2Fhttp%2Fstuff",
|
||||||
|
"http://example.com/http/stuff", Options{Width: 1, Height: 2}, false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"http://localhost/http%3A%2F%2Fexample.com%2Ffoo",
|
||||||
|
"http://example.com/foo", emptyOptions, false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"http://localhost/http%3A%2F%2Fexample.com%2Ffoo",
|
||||||
|
"http://example.com/foo", emptyOptions, false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"http://localhost/http%3A%2Fexample.com%2Ffoo",
|
||||||
|
"http://example.com/foo", emptyOptions, false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"http://localhost/http%3A%2F%2F%2Fexample.com%2Ffoo",
|
||||||
|
"http://example.com/foo", emptyOptions, false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"http://localhost//http%3A%2F%2Fexample.com%2Ffoo",
|
||||||
|
"http://example.com/foo", emptyOptions, false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"http://localhost/http%3A%2F%2Fexample.com%2Ffoo%3Ftest%3D1%26test%3D2",
|
||||||
|
"http://example.com/foo?test=1&test=2", emptyOptions, false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"http://localhost/1x2/http%3A%2F%2Fexample.com%2Ffoo%3Ftest%3D1%26test%3D2",
|
||||||
|
"http://example.com/foo?test=1&test=2", Options{Width: 1, Height: 2}, false,
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, tt := range tests {
|
for _, tt := range tests {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue