Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

net/http ServeMux Wildcard No Compilation Error When Using * and No Documentation on * #68676

Closed
CarsonGH opened this issue Jul 31, 2024 · 2 comments
Labels
Documentation Issues describing a change to documentation.

Comments

@CarsonGH
Copy link

CarsonGH commented Jul 31, 2024

Go version

go version go1.22.5 darwin/arm64

Output of go env in your module/workspace:

GO111MODULE=''
GOARCH='arm64'
GOBIN=''
GOCACHE='/Users/carson/Library/Caches/go-build'
GOENV='/Users/carson/Library/Application Support/go/env'
GOEXE=''
GOEXPERIMENT=''
GOFLAGS=''
GOHOSTARCH='arm64'
GOHOSTOS='darwin'
GOINSECURE=''
GOMODCACHE='/Users/carson/go/pkg/mod'
GONOPROXY=''
GONOSUMDB=''
GOOS='darwin'
GOPATH='/Users/carson/go'
GOPRIVATE=''
GOPROXY='https://proxy.golang.org,direct'
GOROOT='/usr/local/go'
GOSUMDB='sum.golang.org'
GOTMPDIR=''
GOTOOLCHAIN='auto'
GOTOOLDIR='/usr/local/go/pkg/tool/darwin_arm64'
GOVCS=''
GOVERSION='go1.22.5'
GCCGO='gccgo'
AR='ar'
CC='clang'
CXX='clang++'
CGO_ENABLED='1'
GOMOD='/Users/carson/Desktop/MAIN/PROJECTS/languai/backend/go.mod'
GOWORK=''
CGO_CFLAGS='-O2 -g'
CGO_CPPFLAGS=''
CGO_CXXFLAGS='-O2 -g'
CGO_FFLAGS='-O2 -g'
CGO_LDFLAGS='-O2 -g'
PKG_CONFIG='pkg-config'
GOGCCFLAGS='-fPIC -arch arm64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -ffile-prefix-map=/var/folders/wj/q115_7kn1rgcgsgy3zqqcg100000gn/T/go-build2426427935=/tmp/go-build -gno-record-gcc-switches -fno-common'

What did you do?

func main(){

type application struct{}

func (app *application) routes() *http.ServeMux {
	router.HandleFunc("/api/*", func(w http.ResponseWriter, r *http.Request) {
		w.WriteHeader(http.StatusOK)
		w.Write([]byte("200 OK - This is the entrypoint for all api requests"))
	})

	router.HandleFunc("GET /*", func(w http.ResponseWriter, r *http.Request) {
		w.WriteHeader(http.StatusOK)
		w.Write([]byte("200 OK - This is the react homepage entrypoint/catchall"))
	})
	
//VERSUS THIS FORMAT!!!!!!! WHICH THROWS ERROR

		router.HandleFunc("/api/", func(w http.ResponseWriter, r *http.Request) {
		w.WriteHeader(http.StatusOK)
		w.Write([]byte("200 OK - This is the entrypoint for all api requests"))
	})

	router.HandleFunc("GET /", func(w http.ResponseWriter, r *http.Request) {
		w.WriteHeader(http.StatusOK)
		w.Write([]byte("200 OK - This is the react homepage entrypoint/catchall"))
	})
}

myApp = &application{}

srv := http.Server{
  Addr:     cfg.addr,
  ErrorLog: app.errorLog,
  Handler:  app.routes(),
  }
srv.ListenAndServe()
defer srv.Close()
}

Sending GET requests to localhost:4055/api/testing

What did you see happen?

When I use the wilcard (*) in the path I don't get a compile error that the specificity of the Method is more specific than the route pattern.

When I exclude the wildcard * and just use "/api/" and "/" OR "/api/{test...}" and "/{test..}" or "/api/{test..}" and "/" I get the error message properly.

I read through the serveMux documentation and it doesn't say anything about not using * in the route pathing or talk about it's usage as a wildcard

Also when using the wildcard routes /api/* and GET /*

The request sent to /api/testing gets routed to the "GET /" handler instead of the "/api/"

I would have thought it would go to the "/api/*" but after reading the documentation technically neither routes are more specific than each other.

What did you expect to see?

I expected for the * wildcard to behave like the {test...} or trailing slash and throw an error due to non specificity.

There is no mention of the * character in the http/net ServeMux Patterns documentation at all whether it should or should not be used.

Either adding a warning to the documentation to not use the * char, treating * as a normal string character, Treating * as a {test...} wildcard, or throwing an error about * properly.

@gopherbot gopherbot added the Documentation Issues describing a change to documentation. label Jul 31, 2024
@gabyhelp
Copy link

Related Issues and Documentation

(Emoji vote if this was helpful or unhelpful; more detailed feedback welcome in this discussion.)

@seankhliao
Copy link
Member

Nowhere in Go documentation suggests that * is a valid wildcard character for HTTP paths, I don't think it makes sense to try and mention arbitrary syntaxes that we don't support.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Documentation Issues describing a change to documentation.
Projects
None yet
Development

No branches or pull requests

4 participants