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

[bug] Host matching not work without scheme? #530

Closed
pierreneter opened this issue Nov 4, 2019 · 6 comments
Closed

[bug] Host matching not work without scheme? #530

pierreneter opened this issue Nov 4, 2019 · 6 comments

Comments

@pierreneter
Copy link

Describe the bug

About: Matching Routes - Host

https://github.com/gorilla/mux#matching-routes

The docs:

r := mux.NewRouter()
// Only matches if domain is "www.example.com".
r.Host("www.example.com") // this
// Matches a dynamic subdomain.
r.Host("{subdomain:[a-z]+}.example.com")

It will not work if the host is passed in without a scheme.

Versions

Go version: go version go1.13.4 darwin/amd64
package version: github.com/gorilla/mux v1.7.3

Steps to Reproduce / Code Snippets

This not work:

func main() {
	r := mux.NewRouter()
	r.Host("localhost:8080")
	r.Handle("/", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		w.Write([]byte("ok"))
	}))
	http.ListenAndServe(":8080", r)
}

This work:

func main() {
	r := mux.NewRouter()
	r.Host("http://localhost:8080")
	r.Handle("/", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		w.Write([]byte("ok"))
	}))
	http.ListenAndServe(":8080", r)
}

Expected behavior

Work without scheme

@pierreneter pierreneter added the bug label Nov 4, 2019
@76creates
Copy link

also to add:
when you match host like r.Host("localhost:8080") it acts as a inverse, allowing all but that host
when you match like r.Host("http://localhost:8080") it allows all hosts regardless

I found it only working as expected when you chain .Host(...) to the r.Handle(...)

@elithrar
Copy link
Contributor

The examples in the docs may be a little confusing.

If you say r.Host without attaching a Route, you've created a matcher without a subsequent handler to call - which is why you see it "not allowing" that host.

You should either:

s := r.Host("sub.example.com").Subrouter()
s.HandleFunc("/", yourHandler)

... or use .PathPrefix to match all requests to that host. Calling r.Host on its own will effectively blackhole the matches.

@fharding1
Copy link
Contributor

I've seen a few issues from people confused by this behavior, and I think it's just the nature of the builder pattern mux uses. It makes sense, it's just not very intuitive to people new to mux. It could help to add a sentence to the Matching Routes section of the README that says something along the lines of: "Matchers without attached handlers will still match routes, however they will 404, even taking priority over routes with handlers (if added first)." with maybe an example similar to @pierreneter 's.

@elithrar
Copy link
Contributor

elithrar commented Nov 30, 2019 via email

@teraflik
Copy link

I think #474 should resolve this.

@stale
Copy link

stale bot commented Apr 14, 2020

This issue has been automatically marked as stale because it hasn't seen a recent update. It'll be automatically closed in a few days.

@stale stale bot added the stale label Apr 14, 2020
@stale stale bot closed this as completed Apr 28, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants