Commit b9244cdf authored by Matthew Holt's avatar Matthew Holt

templates: Another context fix when host header is missing port

parent a2ba00bd
...@@ -110,6 +110,10 @@ func (c Context) Host() (string, error) { ...@@ -110,6 +110,10 @@ func (c Context) Host() (string, error) {
func (c Context) Port() (string, error) { func (c Context) Port() (string, error) {
_, port, err := net.SplitHostPort(c.Req.Host) _, port, err := net.SplitHostPort(c.Req.Host)
if err != nil { if err != nil {
if !strings.Contains(c.Req.Host, ":") {
// common with sites served on the default port 80
return "80", nil
}
return "", err return "", err
} }
return port, nil return port, nil
......
...@@ -260,14 +260,19 @@ func TestPort(t *testing.T) { ...@@ -260,14 +260,19 @@ func TestPort(t *testing.T) {
}, },
{ {
input: "localhost", input: "localhost",
expectedPort: "", expectedPort: "80", // assuming 80 is the default port
shouldErr: true, // missing port in address shouldErr: false,
}, },
{ {
input: ":8080", input: ":8080",
expectedPort: "8080", expectedPort: "8080",
shouldErr: false, shouldErr: false,
}, },
{
input: "[::]",
expectedPort: "",
shouldErr: true,
},
} }
for _, test := range tests { for _, test := range tests {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment