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

Adds the Allow header on 405 response #776

Merged
merged 2 commits into from
Jul 13, 2023
Merged

Conversation

EwenQuim
Copy link
Contributor

@EwenQuim EwenQuim commented Dec 24, 2022

By adding the Allow header with the lists of available methods on 405 Method Not Found error, this PR makes chi compliant with rfc9110, because this behaviour is not currently supported in Chi.

Note that it also make chi look better on this article (that will need to be updated). This article is the first to appear when searching on google "golang choose router".

Closes #446.

Don't hesitate to ask for changes. Thanks for this amazing package :)

EDIT : Now this PR does not inflicts any performance issues on the happy path (all routes except in case of 405). It simply does not touch any code that is not a 405. See this comment to get performance details.

@EwenQuim
Copy link
Contributor Author

@pkieltyka Any news about this ?

@thexpand
Copy link

Why is this not merged? Is it still pending on a PR review or is it something else?

@ghost
Copy link

ghost commented May 21, 2023

Any updates regarding this matter?

Copy link
Contributor

@VojtechVitek VojtechVitek left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi,

I think the chi router itself should be able to set the Allow header along with the HTTP 405 responses. If we do this only on HTTP 405s, we wouldn't introduce any performance impact in the "happy path" unlike this middleware.

However, I think you've done a great job on this middleware. Could you share it as a standalone package with the community?

Best,
Vojtech

Makes chi compliant with rfc9110
(cf https://httpwg.org/specs/rfc9110.html#field.allow)
Currently this behaviour is not supported, and it adds a small
but non-negligable performance offset.
That's why it is proposed as a middleware
@EwenQuim
Copy link
Contributor Author

EwenQuim commented May 22, 2023

I really believe that it is possible to add this important feature without performance impact, and I am actively working on it. I have a working proposal that has no (really 0ms/0byte) performance impact on the happy path for the default methodNotAllowed handler, with an impact in case of 405 similar to what I got for the middleware.

I am refactoring it to try to keep the "chi" spirit (simple, light, idiomatic and maintainable) so that it is easy to review and rework later.

Thank you for your review and comment.


All routes are unaffected, except the route trigerring a 405 (the /hi-post route)

old.txt new.txt
sec/op CI sec/op CI vs base P
Mux/route:/-8 1.3245e-07 4% 1.264e-07 3% -4.57% p=0.011 n=6
Mux/route:/hi-8 1.385e-07 12% 1.3375e-07 2% -3.43% p=0.004 n=6
Mux/route:/hi-post-8 1.518500e-07 2% 2.071500e-07 3% +36.42% p=0.002 n=6
Mux/route:/sup/123/and/this-8 2.092e-07 5% 2.0425e-07 4% ~ p=0.290 n=6
Mux/route:/sup/123/foo/this-8 2.491e-07 2% 2.527e-07 3% ~ p=0.310 n=6
Mux/route:/sharing/z/aBc-8 2.4895e-07 2% 2.6175e-07 3% +5.14% p=0.004 n=6
Mux/route:/sharing/z/aBc/twitter-8 3.135e-07 5% 3.102e-07 9% ~ p=0.732 n=6
Mux/route:/sharing/z/aBc/direct-8 3.755e-07 3% 3.637e-07 3% -3.14% p=0.015 n=6
Mux/route:/sharing/z/aBc/direct/download-8 4.3805e-07 2% 4.242e-07 1% -3.16% p=0.002 n=6
TreeGet-8 8.997e-08 1% 8.704e-08 1% -3.26% p=0.002 n=6
geomean 2.1010834226e-07 2.1353321752e-07 +1.63%
old.txt new.txt
allocs/op CI allocs/op CI vs base P
Mux/route:/-8 2 0% 2 0% ~ p=1.000 n=6
Mux/route:/hi-8 2 0% 2 0% ~ p=1.000 n=6
Mux/route:/hi-post-8 2 0% 4 0% +100.00% p=0.002 n=6
Mux/route:/sup/123/and/this-8 2 0% 2 0% ~ p=1.000 n=6
Mux/route:/sup/123/foo/this-8 2 0% 2 0% ~ p=1.000 n=6
Mux/route:/sharing/z/aBc-8 2 0% 2 0% ~ p=1.000 n=6
Mux/route:/sharing/z/aBc/twitter-8 3 0% 3 0% ~ p=1.000 n=6
Mux/route:/sharing/z/aBc/direct-8 3 0% 3 0% ~ p=1.000 n=6
Mux/route:/sharing/z/aBc/direct/download-8 4 0% 4 0% ~ p=1.000 n=6
TreeGet-8 0 0% 0 0% ~ p=1.000 n=6
geomean +7.18%

@EwenQuim EwenQuim changed the title middleware: SetAllowHeader adds the Allow header on 405 response Adds the Allow header on 405 response May 22, 2023
Comment on lines -381 to +385
func (mx *Mux) MethodNotAllowedHandler() http.HandlerFunc {
func (mx *Mux) MethodNotAllowedHandler(methodsAllowed ...methodTyp) http.HandlerFunc {
if mx.methodNotAllowedHandler != nil {
return mx.methodNotAllowedHandler
}
return methodNotAllowedHandler
return methodNotAllowedHandler(methodsAllowed...)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately I cannot pass the Allowed Methods to the (Mux).methodNotAllowedHandler given by the user directly in the Mux with the MethodNotAllowed() method, as it would break the current API.

It only set the Allow header when using default handler. But I think it is enough anyway, at least for this PR.

We might want to give the possibility for users to implement more easily this feature in their own handler by making the (Context).methodsAllowed field public. Is it worth it? It's up to you.

@zamicol
Copy link

zamicol commented Jun 1, 2023

Adding RFC 9110 compliance sounds like a good idea. 👍

Copy link
Contributor

@VojtechVitek VojtechVitek left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks so much better compared to the previous middleware solution. Good job!

@pkieltyka This PR sets Allow header on HTTP 405 responses (which makes chi compliant with RFC 9110) and doesn't affect the performance of the "happy path". Looks pretty good to me. Mind having a look?

@niss36
Copy link

niss36 commented Jun 9, 2023

Seems really useful as this is required by the RFC 😄

@Remy-Luciani
Copy link

Intent looks good, code looks good, perf look good. 😁 I hope it will be merged soon!

@EwenQuim
Copy link
Contributor Author

Hey, @VojtechVitek! I was wondering if you had any updates on @pkieltyka?

In case he's tied up or not available, I was thinking we could explore alternative ways to merge this feature. Seems like it could be good for a lot of people!

Copy link
Contributor

@VojtechVitek VojtechVitek left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM from me

@thexpand
Copy link

Do we have docs on how to use this?

@pkieltyka pkieltyka merged commit 4b14b83 into go-chi:master Jul 13, 2023
12 checks passed
@pkieltyka
Copy link
Member

thanks for the contribution @EwenQuim and to the reviewers (@VojtechVitek + others)

merged + released https://github.com/go-chi/chi/releases/tag/v5.0.10

@EwenQuim
Copy link
Contributor Author

Do we have docs on how to use this?

@thexpand it works "out of the box", it automatically sets the Allow header in case of a 405, so no docs are required :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Include Allow header in 405 Method Not Allowed response
7 participants