From cc229aefaef01751995978a7950cf3d803c8735a Mon Sep 17 00:00:00 2001 From: Matthew Holt Date: Tue, 27 Oct 2015 23:20:05 -0600 Subject: [PATCH] templates: Parse host successfully when port is implicit (fixes #292) --- middleware/context.go | 4 ++++ middleware/context_test.go | 12 +++++++++++- 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/middleware/context.go b/middleware/context.go index 6c45d0337..b00d163eb 100644 --- a/middleware/context.go +++ b/middleware/context.go @@ -97,6 +97,10 @@ func (c Context) URI() string { func (c Context) Host() (string, error) { host, _, err := net.SplitHostPort(c.Req.Host) if err != nil { + if !strings.Contains(c.Req.Host, ":") { + // common with sites served on the default port 80 + return c.Req.Host, nil + } return "", err } return host, nil diff --git a/middleware/context_test.go b/middleware/context_test.go index 8933134d8..11d2a4390 100644 --- a/middleware/context_test.go +++ b/middleware/context_test.go @@ -232,8 +232,13 @@ func TestHost(t *testing.T) { }, { input: "localhost", + expectedHost: "localhost", + shouldErr: false, + }, + { + input: "[::]", expectedHost: "", - shouldErr: true, // missing port in address + shouldErr: true, }, } @@ -258,6 +263,11 @@ func TestPort(t *testing.T) { expectedPort: "", shouldErr: true, // missing port in address }, + { + input: ":8080", + expectedPort: "8080", + shouldErr: false, + }, } for _, test := range tests {