-
Notifications
You must be signed in to change notification settings - Fork 17.7k
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.Handler does not populate named path wildcards #69623
Comments
I drafted a possible fix in https://go.dev/cl/615735. |
Change https://go.dev/cl/615735 mentions this issue: |
It doesn't seem unreasonable for Unless I'm missing something, the example middleware can be written more simply using
|
Thanks for your thoughts. If one thinks of ServeMux.Handler as a pure lookup function, then i agree that populating the request is surprising. In my mind the bug is that the returned handler can no longer be used as a regular HTTP handler. That means it's no longer safe to call ServeHTTP on it, because the request doesn't carry the information it needs (at least when using patterns with wildcards). The example middleware in the initial description was a bit too short to demonstrate this, so here's a slightly extended version that shows how to subtly hold it wrong:
At a first glance the code looks fine, but assuming ensureAuthenticationHandler.ServeHTTP eventually calls h.ServeHTTP it would fail on patterns with wildcards. The fix is, as you suggest, to re-use the mux (e.g. I think it would be better to populate the request and avoid subtle bugs like the above. |
i still think Handler should be a pure lookup function, and it's up to the middleware to use the second return parameter to fill in the request, as is expected of any other third party router. |
unclear if this "needs investigation" or WAI. |
Definitely "needs investigation." It's not clear to me what the implications are of populating the request in the presence of further mux nesting and http.StripPrefix. Also, to echo what @seankhliao said, there may be other uses of Handler that are just pure lookup, perhaps with variations on the request. The fundamental problem is that you're modifying a shared resource. Furthermore, so far in all examples, there is a workaround that is not too far from the problematic code (though perhaps a bit subtle). |
Maybe the handler returned by mux.Handler should modify the request passed to it?
|
Wrapping the registered handler into another handler (which then modifies the request to populate the fields on demand) would break code that expects the returned handler to match the registered one (e.g. uses type assertion). The options that i'm seeing right now:
I don't find any of these particularly appealing. Maybe there are more options that i'm not seeing though. |
Change https://go.dev/cl/618096 mentions this issue: |
Go version
go version devel go1.24-b17a55d095 Tue Sep 24 23:20:50 2024 +0000 linux/amd64
Output of
go env
in your module/workspace:What did you do?
The http.ServeMux.Handler method currently discards patterns and matches that findHandler returns.
This behavior makes it impossible to use named path wildcards in http.Handler implementations that want to use an http.ServeMux as the underlying request multiplexer.
The following program shows a common pattern that demonstrates this:
What did you see happen?
Executing above program yields
The wildcards were not populated.
What did you expect to see?
I'd expect to see the wildcards populated.
The text was updated successfully, but these errors were encountered: