From b9244cdf2ee719d9c921b50bed0ebd0d793373e9 Mon Sep 17 00:00:00 2001 From: Matthew Holt Date: Tue, 17 Nov 2015 14:35:18 -0700 Subject: [PATCH] templates: Another context fix when host header is missing port --- middleware/context.go | 4 ++++ middleware/context_test.go | 9 +++++++-- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/middleware/context.go b/middleware/context.go index b00d163e..58764202 100644 --- a/middleware/context.go +++ b/middleware/context.go @@ -110,6 +110,10 @@ func (c Context) Host() (string, error) { func (c Context) Port() (string, error) { _, port, 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 "80", nil + } return "", err } return port, nil diff --git a/middleware/context_test.go b/middleware/context_test.go index 11d2a439..e60bd7f1 100644 --- a/middleware/context_test.go +++ b/middleware/context_test.go @@ -260,14 +260,19 @@ func TestPort(t *testing.T) { }, { input: "localhost", - expectedPort: "", - shouldErr: true, // missing port in address + expectedPort: "80", // assuming 80 is the default port + shouldErr: false, }, { input: ":8080", expectedPort: "8080", shouldErr: false, }, + { + input: "[::]", + expectedPort: "", + shouldErr: true, + }, } for _, test := range tests {