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

Allow header support in Router, MethodNotFoundHandler (405) and CORS middleware #2039

Merged
merged 2 commits into from
Jan 3, 2022

Conversation

aldas
Copy link
Contributor

@aldas aldas commented Dec 4, 2021

This PR adds support for Allow header to

  • http OPTIONS method responses
  • status 405 (method not found) responses
  • CORS middleware

Allow header lists all method types registered for given routed url path.

Related RFCs:

Implementation notes:

  • Although in case of OPTIONS method router now add special options method handler instead of MethodNotFound handler as found/matched handler, we can not rely on that for CORS middleware. In CORS middleware we can not remove IF conditions for OPTIONS as when browser sends OPTIONS request it does not (by default) include cookie / authentication headers and therefore if we would blindly use next(c) and hope to meet our router optionshandler we probably not succeed because of different kinds of authentication middlewares (ala JWT or BasicAuth or KeyAuth) which check for stuff like that.
  • The reason for adding Allow value to context echo.ContextKeyHeaderAllow is because default 405 handler needs to be able to access that value and possibly CORS middleware is interested in that value. As echo.MethodNotAllowedHandler is part of public API we can not change how that method is created due backwards compatibility.
  • optionsMethodHandler is kept private as it is router specific implementation detail. If you want your own then you can add them with e.OPTIONS() method for paths you choose.

Using context values in Echo core is so far unprecedented and potentially controversial decision. I did not want to introduce new field into echo.context struct as it is quite specific case and I know that Go standard library http.Server is using context.Context to add some info to each request context - so it is not so unheard of but probably should be avoided:

See:

Let's have a discussion

@aldas aldas requested a review from lammel December 4, 2021 21:12
@codecov
Copy link

codecov bot commented Dec 4, 2021

Codecov Report

Merging #2039 (73c9678) into master (b437ee3) will increase coverage by 0.24%.
The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #2039      +/-   ##
==========================================
+ Coverage   91.32%   91.57%   +0.24%     
==========================================
  Files          33       33              
  Lines        2871     2919      +48     
==========================================
+ Hits         2622     2673      +51     
+ Misses        159      157       -2     
+ Partials       90       89       -1     
Impacted Files Coverage Δ
context.go 86.93% <ø> (ø)
echo.go 94.20% <100.00%> (+0.05%) ⬆️
middleware/cors.go 92.13% <100.00%> (+2.97%) ⬆️
router.go 96.48% <100.00%> (+0.75%) ⬆️
middleware/request_id.go 85.71% <0.00%> (+1.50%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update b437ee3...73c9678. Read the comment docs.

@aldas
Copy link
Contributor Author

aldas commented Dec 5, 2021

@lammel when you have time. please review.

@lammel
Copy link
Contributor

lammel commented Dec 5, 2021 via email

Copy link
Contributor

@lammel lammel left a comment

Choose a reason for hiding this comment

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

Didn't have time to run or check on the tests yet, but code is mostly reviewed now an LGTM.

context.go Outdated Show resolved Hide resolved
echo.go Outdated Show resolved Hide resolved
echo.go Outdated Show resolved Hide resolved
echo.go Show resolved Hide resolved
middleware/cors.go Outdated Show resolved Hide resolved
middleware/cors.go Show resolved Hide resolved
middleware/cors_test.go Show resolved Hide resolved
@aldas aldas requested a review from lammel December 9, 2021 20:06
Copy link
Contributor

@lammel lammel left a comment

Choose a reason for hiding this comment

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

Fine piece of coding work. LGTM!

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.

2 participants