-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Expose Route::matches()
to allow external implementation of Options
handling
#1561
Comments
In general, making the router 'queryable' would definitely enhance it's utility for all kinds of middleware. |
Can you rephrase this? What does "that all the method is checked for guards" mean?
This is an expensive operation, but it would be fairly straightforward to implement outside of Rocket if we exposed just a bit more. There is if this is not an options or if this was already handled {
return;
}
let original = request.method();
let allow = &[Get, Put, Post, Delete, ..].iter()
.filter(|method| {
request.set_method(method);
request.rocket()
.routes()
.any(|r| r.matches(request))
})
.map(|method| method.as_str())
.join(", ");
request.set_method(original);
/* set the response */ Ideas on whether we should expose these two API elements, @jebrosen? My only concern is wanting to change |
I don't mind I'm a bit less happy about the idea of exposing |
I don't believe I've purposefully avoided exposing In all, exposing |
Well I don't how it's implemented internal, but rocket checks that a given request can satisfy all the guards/data that a handler function needs before calling it. The only difference for options is checks that the function could be called, that all the preconditions match ( A routing match ), but simply doesn't call it. The simplest solution might be for the get!/put!/post!/ etc macros to take an optional |
This would be arbitrarily expensive as guards can perform arbitrary work. Rocket won't do this. |
Roue::matches() to allow external implmentation of
Options` handling
Roue::matches() to allow external implmentation of
Options` handlingRoue::matches()
to allow external implmentation of Options
handling
Roue::matches()
to allow external implmentation of Options
handlingRoute::matches()
to allow external implementation of Options
handling
Reading up on Options, it's just a CORS check, and apparently the options response makes no other determination where or not an actual PUT/GET/Etc request would succeed or fail. It's just "Hey, what are your CORs requirements" In that case, I think, shipping with a simple CORS fairing OOTB, and the other Issue dealing with Forward being able to pass a responder or error or something to use if matching fails would cover all of my cases. I was worried there might be some coupling between options and routes, but doesn't seem that way. I guess if we want to allow customization, having an options macro ( like get, etc ) would let people overrride the CORS fairing behavior, and not worry about expensive guards too. |
I know I've been proposing a lot of features and not contributing any, but I've been crunching away with rocket, and not had time. I should be able to contribute when things die down. |
Hmmm, is this all that's needed? diff --git a/core/lib/src/router/collider.rs b/core/lib/src/router/collider.rs
index 1b6d82b6..39abb918 100644
--- a/core/lib/src/router/collider.rs
+++ b/core/lib/src/router/collider.rs
@@ -96,7 +96,7 @@ impl Route {
/// * All static components in the route's query string are also in the
/// request query string, though in any position. If there is no query
/// in the route, requests with/without queries match.
- pub(crate) fn matches(&self, req: &Request<'_>) -> bool {
+ pub fn matches(&self, req: &Request<'_>) -> bool {
self.method == req.method()
&& paths_match(self, req)
&& queries_match(self, req) I guess the ergonomics can be improved by adding a macro for creating enums with a method to iterate over all variants (for |
Technically, yes, this is all that's needed. But much more consideration needs to be given before this is done. This is low hanging fruit with a high thought-to-actual-work ratio. It is my plan to address this once all of the other pieces for the release are ready. |
Would this belong in |
At the moment, there is no intention of providing this functionality in any official capacity. A fairing can intercept all OPTIONs requests and emit a response; a guard cannot do this. |
I am not ready to commit |
Currently rocket requires to redefine the same route twice if you want to add options support.
Two Options
or
The text was updated successfully, but these errors were encountered: