Skip to content

Commit

Permalink
Issue #219: Support absolute URLs
Browse files Browse the repository at this point in the history
This patch adds support for requests with absolute URLs.
According to https://www.w3.org/Protocols/rfc2616/rfc2616-sec5.html
servers must accept requests with absolute URLs.
  • Loading branch information
magiconair committed Jan 20, 2017
1 parent 632def7 commit cc3e3cd
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 19 deletions.
2 changes: 1 addition & 1 deletion route/issue57_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func TestIssue57(t *testing.T) {
route del svcb`,
}

req := &http.Request{RequestURI: "/foo"}
req := &http.Request{URL: mustParse("/foo")}
want := "http://foo.com:800"

for i, tt := range tests {
Expand Down
12 changes: 7 additions & 5 deletions route/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -269,10 +269,10 @@ func (t Table) route(host, path string) *Route {
func normalizeHost(req *http.Request) string {
host := strings.ToLower(req.Host)
if req.TLS == nil && strings.HasSuffix(host, ":80") {
return host[:len(host)-3]
return host[:len(host)-len(":80")]
}
if req.TLS != nil && strings.HasSuffix(host, ":443") {
return host[:len(host)-4]
return host[:len(host)-len(":443")]
}
return host
}
Expand All @@ -282,16 +282,18 @@ func normalizeHost(req *http.Request) string {
// and if none matches then it falls back to generic routes without
// a host. This is useful for a catch-all '/' rule.
func (t Table) Lookup(req *http.Request, trace string) *Target {
path := req.URL.Path

if trace != "" {
if len(trace) > 16 {
trace = trace[:15]
}
log.Printf("[TRACE] %s Tracing %s%s", trace, req.Host, req.RequestURI)
log.Printf("[TRACE] %s Tracing %s%s", trace, req.Host, path)
}

target := t.lookup(normalizeHost(req), req.RequestURI, trace)
target := t.lookup(normalizeHost(req), path, trace)
if target == nil {
target = t.lookup("", req.RequestURI, trace)
target = t.lookup("", path, trace)
}

if target != nil && trace != "" {
Expand Down
26 changes: 13 additions & 13 deletions route/table_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -482,25 +482,25 @@ func TestTableLookup(t *testing.T) {
dst string
}{
// match on host and path with and without trailing slash
{&http.Request{Host: "abc.com", RequestURI: "/"}, "http://foo.com:1000"},
{&http.Request{Host: "abc.com", RequestURI: "/bar"}, "http://foo.com:1000"},
{&http.Request{Host: "abc.com", RequestURI: "/foo"}, "http://foo.com:1500"},
{&http.Request{Host: "abc.com", RequestURI: "/foo/"}, "http://foo.com:2000"},
{&http.Request{Host: "abc.com", RequestURI: "/foo/bar"}, "http://foo.com:2500"},
{&http.Request{Host: "abc.com", RequestURI: "/foo/bar/"}, "http://foo.com:3000"},
{&http.Request{Host: "abc.com", URL: mustParse("/")}, "http://foo.com:1000"},
{&http.Request{Host: "abc.com", URL: mustParse("/bar")}, "http://foo.com:1000"},
{&http.Request{Host: "abc.com", URL: mustParse("/foo")}, "http://foo.com:1500"},
{&http.Request{Host: "abc.com", URL: mustParse("/foo/")}, "http://foo.com:2000"},
{&http.Request{Host: "abc.com", URL: mustParse("/foo/bar")}, "http://foo.com:2500"},
{&http.Request{Host: "abc.com", URL: mustParse("/foo/bar/")}, "http://foo.com:3000"},

// do not match on host but maybe on path
{&http.Request{Host: "def.com", RequestURI: "/"}, "http://foo.com:800"},
{&http.Request{Host: "def.com", RequestURI: "/bar"}, "http://foo.com:800"},
{&http.Request{Host: "def.com", RequestURI: "/foo"}, "http://foo.com:900"},
{&http.Request{Host: "def.com", URL: mustParse("/")}, "http://foo.com:800"},
{&http.Request{Host: "def.com", URL: mustParse("/bar")}, "http://foo.com:800"},
{&http.Request{Host: "def.com", URL: mustParse("/foo")}, "http://foo.com:900"},

// strip default port
{&http.Request{Host: "abc.com:80", RequestURI: "/"}, "http://foo.com:1000"},
{&http.Request{Host: "abc.com:443", RequestURI: "/", TLS: &tls.ConnectionState{}}, "http://foo.com:1000"},
{&http.Request{Host: "abc.com:80", URL: mustParse("/")}, "http://foo.com:1000"},
{&http.Request{Host: "abc.com:443", URL: mustParse("/"), TLS: &tls.ConnectionState{}}, "http://foo.com:1000"},

// not using default port
{&http.Request{Host: "abc.com:443", RequestURI: "/"}, "http://foo.com:800"},
{&http.Request{Host: "abc.com:80", RequestURI: "/", TLS: &tls.ConnectionState{}}, "http://foo.com:800"},
{&http.Request{Host: "abc.com:443", URL: mustParse("/")}, "http://foo.com:800"},
{&http.Request{Host: "abc.com:80", URL: mustParse("/"), TLS: &tls.ConnectionState{}}, "http://foo.com:800"},
}

for i, tt := range tests {
Expand Down

0 comments on commit cc3e3cd

Please sign in to comment.