diff --git a/middleware/context.go b/middleware/context.go index 6c45d033..b00d163e 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 8933134d..11d2a439 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 {