mirror of
https://github.com/willnorris/imageproxy.git
synced 2024-12-30 22:34:18 -05:00
allow options path segment to be omitted
This commit is contained in:
parent
45d07f2378
commit
fde104a7a1
2 changed files with 43 additions and 36 deletions
|
@ -29,19 +29,26 @@ func (e URLError) Error() string {
|
||||||
|
|
||||||
// NewRequest parses an http.Request into an image request.
|
// NewRequest parses an http.Request into an image request.
|
||||||
func NewRequest(r *http.Request) (*data.Request, error) {
|
func NewRequest(r *http.Request) (*data.Request, error) {
|
||||||
path := strings.SplitN(r.URL.Path, "/", 3)
|
|
||||||
if len(path) != 3 {
|
|
||||||
return nil, URLError{"too few path segments", r.URL}
|
|
||||||
}
|
|
||||||
|
|
||||||
var err error
|
var err error
|
||||||
req := new(data.Request)
|
req := new(data.Request)
|
||||||
|
|
||||||
req.URL, err = url.Parse(path[2])
|
path := r.URL.Path[1:] // strip leading slash
|
||||||
if err != nil {
|
req.URL, err = url.Parse(path)
|
||||||
return nil, URLError{
|
if err != nil || !req.URL.IsAbs() {
|
||||||
fmt.Sprintf("unable to parse remote URL: %v", err),
|
// first segment is likely options
|
||||||
r.URL,
|
parts := strings.SplitN(path, "/", 2)
|
||||||
|
if len(parts) != 2 {
|
||||||
|
return nil, URLError{"too few path segments", r.URL}
|
||||||
|
}
|
||||||
|
|
||||||
|
req.URL, err = url.Parse(parts[1])
|
||||||
|
if err != nil {
|
||||||
|
return nil, URLError{fmt.Sprintf("unable to parse remote URL: %v", err), r.URL}
|
||||||
|
}
|
||||||
|
|
||||||
|
req.Options, err = data.ParseOptions(parts[0])
|
||||||
|
if err != nil {
|
||||||
|
return nil, URLError{err.Error(), r.URL}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -55,11 +62,6 @@ func NewRequest(r *http.Request) (*data.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 = r.URL.RawQuery
|
||||||
req.Options, err = data.ParseOptions(path[1])
|
|
||||||
if err != nil {
|
|
||||||
return nil, URLError{err.Error(), r.URL}
|
|
||||||
}
|
|
||||||
|
|
||||||
return req, nil
|
return req, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -2,71 +2,79 @@ package proxy
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"reflect"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/willnorris/go-imageproxy/data"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var emptyOptions = new(data.Options)
|
||||||
|
|
||||||
func TestNewRequest(t *testing.T) {
|
func TestNewRequest(t *testing.T) {
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
URL string
|
URL string
|
||||||
RemoteURL string
|
RemoteURL string
|
||||||
Width int
|
Options *data.Options
|
||||||
Height int
|
|
||||||
ExpectError bool
|
ExpectError bool
|
||||||
}{
|
}{
|
||||||
// invalid URLs
|
// invalid URLs
|
||||||
{
|
{
|
||||||
"http://localhost/", "", 0, 0, true,
|
"http://localhost/", "", nil, true,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"http://localhost/1/", "", 0, 0, true,
|
"http://localhost/1/", "", nil, true,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"http://localhost//example.com/foo", "", 0, 0, true,
|
"http://localhost//example.com/foo", "", nil, true,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"http://localhost//ftp://example.com/foo", "", 0, 0, true,
|
"http://localhost//ftp://example.com/foo", "", nil, true,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"http://localhost/s/http://example.com/", "", 0, 0, true,
|
"http://localhost/s/http://example.com/", "", nil, true,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"http://localhost/1xs/http://example.com/", "", 0, 0, true,
|
"http://localhost/1xs/http://example.com/", "", nil, true,
|
||||||
},
|
},
|
||||||
|
|
||||||
// valid URLs
|
// valid URLs
|
||||||
|
{
|
||||||
|
"http://localhost/http://example.com/foo",
|
||||||
|
"http://example.com/foo", nil, false,
|
||||||
|
},
|
||||||
{
|
{
|
||||||
"http://localhost//http://example.com/foo",
|
"http://localhost//http://example.com/foo",
|
||||||
"http://example.com/foo", 0, 0, false,
|
"http://example.com/foo", emptyOptions, false,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"http://localhost//https://example.com/foo",
|
"http://localhost//https://example.com/foo",
|
||||||
"https://example.com/foo", 0, 0, false,
|
"https://example.com/foo", emptyOptions, false,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"http://localhost//http://example.com/foo?bar",
|
"http://localhost//http://example.com/foo?bar",
|
||||||
"http://example.com/foo?bar", 0, 0, false,
|
"http://example.com/foo?bar", emptyOptions, false,
|
||||||
},
|
},
|
||||||
|
|
||||||
// size variations
|
// size variations
|
||||||
{
|
{
|
||||||
"http://localhost/x/http://example.com/",
|
"http://localhost/x/http://example.com/",
|
||||||
"http://example.com/", 0, 0, false,
|
"http://example.com/", emptyOptions, false,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"http://localhost/0/http://example.com/",
|
"http://localhost/0/http://example.com/",
|
||||||
"http://example.com/", 0, 0, false,
|
"http://example.com/", emptyOptions, false,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"http://localhost/1x/http://example.com/",
|
"http://localhost/1x/http://example.com/",
|
||||||
"http://example.com/", 1, 0, false,
|
"http://example.com/", &data.Options{1, 0}, false,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"http://localhost/x1/http://example.com/",
|
"http://localhost/x1/http://example.com/",
|
||||||
"http://example.com/", 0, 1, false,
|
"http://example.com/", &data.Options{0, 1}, false,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"http://localhost/1x2/http://example.com/",
|
"http://localhost/1x2/http://example.com/",
|
||||||
"http://example.com/", 1, 2, false,
|
"http://example.com/", &data.Options{1, 2}, false,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -91,11 +99,8 @@ func TestNewRequest(t *testing.T) {
|
||||||
if got := r.URL.String(); tt.RemoteURL != got {
|
if got := r.URL.String(); tt.RemoteURL != got {
|
||||||
t.Errorf("%d. Request URL = %v, want %v", i, got, tt.RemoteURL)
|
t.Errorf("%d. Request URL = %v, want %v", i, got, tt.RemoteURL)
|
||||||
}
|
}
|
||||||
if tt.Height != r.Height {
|
if !reflect.DeepEqual(tt.Options, r.Options) {
|
||||||
t.Errorf("%d. Request Height = %v, want %v", i, r.Height, tt.Height)
|
t.Errorf("%d. Request Options = %v, want %v", i, r.Options, tt.Options)
|
||||||
}
|
|
||||||
if tt.Width != r.Width {
|
|
||||||
t.Errorf("%d. Request Width = %v, want %v", i, r.Width, tt.Width)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue