Skip to content

net/http: document and test behavior of ServeMux with ports #23351

Closed
@kellegous

Description

@kellegous

Please answer these questions before submitting your issue. Thanks!

What version of Go are you using (go version)?

1.9.2 and 1.10beta1

Does this issue reproduce with the latest release?

Yes

What operating system and processor architecture are you using (go env)?

GOARCH="amd64"
GOBIN=""
GOCACHE="/Users/knorton/Library/Caches/go-build"
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOOS="darwin"
GOPATH="/Users/knorton/Documents/2018/01/underpants"
GORACE=""
GOROOT="/Users/knorton/Documents/2018/01/underpants/go1.10"
GOTMPDIR=""
GOTOOLDIR="/Users/knorton/Documents/2018/01/underpants/go1.10/pkg/tool/darwin_amd64"
GCCGO="gccgo"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/9k/yc77pqm56jj79pp163xszgn00000gn/T/go-build934488457=/tmp/go-build -gno-record-gcc-switches -fno-common"

What did you do?

Consider this Go program:

package main

import (
	"fmt"
	"log"
	"net/http"
)

func main() {
	m := http.NewServeMux()

	m.HandleFunc("localhost:8080/",
		func(w http.ResponseWriter, r *http.Request) {
			fmt.Fprintln(w, "locahost:8080")
		})

	log.Panic(http.ListenAndServe(":8080", m))
}

If you use Go 1.8.5:

  1. go run example.go
  2. Visit http://localhost:8080/
  3. You will see the text "localhost:8080" display in the browser.

If you use Go 1.10beta1 and Go 1.9.2:

  1. go run example.go
  2. Visit http://localhost:8080/
  3. You will get a 404 NotFound.

What did you expect to see?

I expect that compiling the example in 1.9.2 should behave as it did in 1.8.5.

More specifically, I would expect Go 1.9.2 to call the handler that I explicitly added for the host localhost:8080.

What did you see instead?

Instead, this program called with Go 1.9.2 returns a 404 instead of calling the handler that I added for the host localhost:8080.

More Details

This change seems to have been reviewed at: https://go-review.googlesource.com/c/go/+/38194

This change strips the port from the host only when it is trying to locate a handler. It does not strip the port when the user adds a handler for a host that includes a port number. As a result, trying to add handlers for hosts that include the port no longer works.

To make matters worse, these change makes it hard to write http code for hosts with ports that works on both Go 1.9 and Go <1.9.

In Go 1.9, the route has to be setup with m.HandleFunc("localhost", ...)
And in Go < 1.9, the route has to be setup with m.HandleFunc("localhost:8080", ...).

To make the code work on all versions of Go, the handler function needs to be registered for both "localhost" and "localhost:8080"

Metadata

Metadata

Assignees

No one assigned

    Labels

    DocumentationIssues describing a change to documentation.FrozenDueToAgeNeedsFixThe path to resolution is known, but the work has not been done.TestingAn issue that has been verified to require only test changes, not just a test failure.help wanted

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions