Skip to content
Myr

feat: add QUERY method routing (#3801)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Yunus · committed by GitHub · parent 98884fb · Browse files · 9 files+78 added−16 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

9 files+78 added−16 removed

  • other:+78 added−16 removed

9 files+78 added−16 removed

modified:axum/src/routing/method_filter.rs+17 added-9 removed
24 unmodified lines
25 ///26 /// [extended CONNECT]: https://www.rfc-editor.org/rfc/rfc8441.html#section-427 /// [HTTP Upgrade Token Registry]: https://www.iana.org/assignments/http-upgrade-tokens/http-upgrade-tokens.xhtml28 /// [`WebSocketUpgrade`]: crate::extract::WebSocketUpgrade29 pub const CONNECT: Self = Self::from_bits(0b0_0000_0001);29 pub const CONNECT: Self = Self::from_bits(0b00_0000_0001);30 /// Match `DELETE` requests.31 pub const DELETE: Self = Self::from_bits(0b0_0000_0010);31 pub const DELETE: Self = Self::from_bits(0b00_0000_0010);32 /// Match `GET` requests.33 pub const GET: Self = Self::from_bits(0b0_0000_0100);33 pub const GET: Self = Self::from_bits(0b00_0000_0100);34 /// Match `HEAD` requests.35 pub const HEAD: Self = Self::from_bits(0b0_0000_1000);35 pub const HEAD: Self = Self::from_bits(0b00_0000_1000);36 /// Match `OPTIONS` requests.37 pub const OPTIONS: Self = Self::from_bits(0b0_0001_0000);37 pub const OPTIONS: Self = Self::from_bits(0b00_0001_0000);38 /// Match `PATCH` requests.39 pub const PATCH: Self = Self::from_bits(0b0_0010_0000);39 pub const PATCH: Self = Self::from_bits(0b00_0010_0000);40 /// Match `POST` requests.41 pub const POST: Self = Self::from_bits(0b0_0100_0000);41 pub const POST: Self = Self::from_bits(0b00_0100_0000);42 /// Match `PUT` requests.43 pub const PUT: Self = Self::from_bits(0b0_1000_0000);43 pub const PUT: Self = Self::from_bits(0b00_1000_0000);44 /// Match `TRACE` requests.45 pub const TRACE: Self = Self::from_bits(0b1_0000_0000);45 pub const TRACE: Self = Self::from_bits(0b01_0000_0000);46 /// Match `QUERY` requests.47 pub const QUERY: Self = Self::from_bits(0b10_0000_0000);4849 const fn bits(self) -> u16 {50 let bits = self;51 bits.0
48 unmodified lines
100 Method::PATCH => Ok(Self::PATCH),101 Method::POST => Ok(Self::POST),102 Method::PUT => Ok(Self::PUT),103 Method::TRACE => Ok(Self::TRACE),104 Method::QUERY => Ok(Self::QUERY),105 other => Err(NoMatchingMethodFilter { method: other }),106 }107 }108}
48 unmodified lines
157 MethodFilter::try_from(Method::TRACE).unwrap(),158 MethodFilter::TRACE159 );160161 assert_eq!(162 MethodFilter::try_from(Method::QUERY).unwrap(),163 MethodFilter::QUERY164 );165166 assert!(167 MethodFilter::try_from(http::Method::from_bytes(b"CUSTOM").unwrap())168 .unwrap_err()169 .to_string()
4 unmodified lines
modified:axum/src/routing/method_routing.rs+28 added-1 removed
339 unmodified lines
340top_level_service_fn!(patch_service, PATCH);341top_level_service_fn!(post_service, POST);342top_level_service_fn!(put_service, PUT);343top_level_service_fn!(trace_service, TRACE);344top_level_service_fn!(query_service, QUERY);345346/// Route requests with the given method to the service.347///348/// # Example
96 unmodified lines
445top_level_handler_fn!(patch, PATCH);446top_level_handler_fn!(post, POST);447top_level_handler_fn!(put, PUT);448top_level_handler_fn!(trace, TRACE);449top_level_handler_fn!(query, QUERY);450451/// Route requests with the given method to the handler.452///453/// # Example
101 unmodified lines
555 post: MethodEndpoint<S, E>,556 put: MethodEndpoint<S, E>,557 trace: MethodEndpoint<S, E>,558 connect: MethodEndpoint<S, E>,559 query: MethodEndpoint<S, E>,560 fallback: Fallback<S, E>,561 allow_header: AllowHeader,562}563
42 unmodified lines
606 .field("post", &self.post)607 .field("put", &self.put)608 .field("trace", &self.trace)609 .field("connect", &self.connect)610 .field("query", &self.query)611 .field("fallback", &self.fallback)612 .field("allow_header", &self.allow_header)613 .finish()614 }
45 unmodified lines
660 chained_handler_fn!(patch, PATCH);661 chained_handler_fn!(post, POST);662 chained_handler_fn!(put, PUT);663 chained_handler_fn!(trace, TRACE);664 chained_handler_fn!(query, QUERY);665666 /// Add a fallback [`Handler`] to the router.667 pub fn fallback<H, T>(mut self, handler: H) -> Self668 where
25 unmodified lines
694 post,695 put,696 trace,697 connect,698 query,699 fallback,700 allow_header: _,701 } = self;702
10 unmodified lines
713 (post, MethodFilter::POST),714 (put, MethodFilter::PUT),715 (trace, MethodFilter::TRACE),716 (connect, MethodFilter::CONNECT),717 (query, MethodFilter::QUERY),718 ]719 .into_iter()720 .filter_map(|(ep, f)| ep.is_some().then_some(f))721 .reduce(MethodFilter::or)
104 unmodified lines
826 post: MethodEndpoint::None,827 put: MethodEndpoint::None,828 trace: MethodEndpoint::None,829 connect: MethodEndpoint::None,830 query: MethodEndpoint::None,831 allow_header: AllowHeader::None,832 fallback: Fallback::Default(fallback),833 }834 }
9 unmodified lines
844 post: self.post.with_state(&state),845 put: self.put.with_state(&state),846 trace: self.trace.with_state(&state),847 connect: self.connect.with_state(&state),848 query: self.query.with_state(&state),849 allow_header: self.allow_header,850 fallback: self.fallback.with_state(state),851 }852 }
150 unmodified lines
1003 &mut self.allow_header,1004 &["CONNECT"],1005 );10061007 set_endpoint(1008 "QUERY",1009 &mut self.query,1010 endpoint,1011 filter,1012 MethodFilter::QUERY,1013 &mut self.allow_header,1014 &["QUERY"],1015 );10161017 self1018 }10191020 chained_service_fn!(connect_service, CONNECT);
4 unmodified lines
1025 chained_service_fn!(patch_service, PATCH);1026 chained_service_fn!(post_service, POST);1027 chained_service_fn!(put_service, PUT);1028 chained_service_fn!(trace_service, TRACE);1029 chained_service_fn!(query_service, QUERY);10301031 #[doc = include_str!("../docs/method_routing/fallback.md")]1032 pub fn fallback_service<T>(mut self, svc: T) -> Self1033 where
28 unmodified lines
1062 post: self.post.map(layer_fn.clone()),1063 put: self.put.map(layer_fn.clone()),1064 trace: self.trace.map(layer_fn.clone()),1065 connect: self.connect.map(layer_fn.clone()),1066 query: self.query.map(layer_fn.clone()),1067 fallback: self.fallback.map(layer_fn),1068 allow_header: self.allow_header,1069 }1070 }
17 unmodified lines
1088 && self.post.is_none()1089 && self.put.is_none()1090 && self.trace.is_none()1091 && self.connect.is_none()1092 && self.query.is_none()1093 {1094 panic!(1095 "Adding a route_layer before any routes is a no-op. \1096 Add the routes you want the layer to apply to first."
9 unmodified lines
1106 self.patch = self.patch.map(layer_fn.clone());1107 self.post = self.post.map(layer_fn.clone());1108 self.put = self.put.map(layer_fn.clone());1109 self.trace = self.trace.map(layer_fn.clone());1088 self.connect = self.connect.map(layer_fn);1110 self.connect = self.connect.map(layer_fn.clone());1111 self.query = self.query.map(layer_fn);11121113 self1114 }1115
37 unmodified lines
1153 self.post = merge_inner(path, "POST", self.post, other.post)?;1154 self.put = merge_inner(path, "PUT", self.put, other.put)?;1155 self.trace = merge_inner(path, "TRACE", self.trace, other.trace)?;1156 self.connect = merge_inner(path, "CONNECT", self.connect, other.connect)?;1157 self.query = merge_inner(path, "QUERY", self.query, other.query)?;11581159 self.fallback = self1160 .fallback1161 .merge(other.fallback)
67 unmodified lines
1229 post,1230 put,1231 trace,1232 connect,1233 query,1234 fallback,1235 allow_header,1236 } = self;1237
6 unmodified lines
1244 call!(req, PUT, put);1245 call!(req, DELETE, delete);1246 call!(req, TRACE, trace);1247 call!(req, CONNECT, connect);1248 call!(req, QUERY, query);12491250 let future = fallback.clone().call_with_state(req, state);12511252 match allow_header {
35 unmodified lines
1288 post: self.post.clone(),1289 put: self.put.clone(),1290 trace: self.trace.clone(),1291 connect: self.connect.clone(),1292 query: self.query.clone(),1293 fallback: self.fallback.clone(),1294 allow_header: self.allow_header.clone(),1295 }1296 }
474 unmodified lines
modified:axum/src/routing/mod.rs+1 added-1 removed
43 unmodified lines
4445pub use self::method_routing::{46 any, any_service, connect, connect_service, delete, delete_service, get, get_service, head,47 head_service, on, on_service, options, options_service, patch, patch_service, post,48 post_service, put, put_service, trace, trace_service, MethodRouter,48 post_service, put, put_service, query, query_service, trace, trace_service, MethodRouter,49};5051macro_rules! panic_on_err {52 ($expr:expr) => {
788 unmodified lines
modified:axum/Cargo.toml+1 added-1 removed
110 unmodified lines
111axum-core = { path = "../axum-core", version = "0.5.6" }112bytes = "1.7"113futures-core = "0.3"114futures-util = { version = "0.3", default-features = false, features = ["alloc"] }115http = "1.0.0"115http = "1.5.0"116http-body = "1.0.0"117http-body-util = "0.1.0"118itoa = "1.0.5"119matchit = "=0.9.2"
134 unmodified lines
modified:axum/CHANGELOG.md+2 added
24 unmodified lines
25 type, not just `axum::body::Body` ([#3205])26- **changed:** `Redirect` constructors now accept any `impl Into<String>` ([#3635])27- **changed:** Updated `matchit` allowing for routes with captures and static prefixes and suffixes ([#3702])28- **fixed:** Responses to `HEAD` will not accidentally reply with `content-length: 0` anymore ([#3742])29- **added:** Add `MethodFilter::QUERY`, `routing::query[_service]` and `MethodRouter::query[_service]` ([#3801])30- **fixed:** `MethodRouter::merge` no longer lists a method twice in the `Allow`31 header after merging `get` and `head` ([#3836])3233[#3158]: https://github.com/tokio-rs/axum/pull/3158
8 unmodified lines
42[#3721]: https://github.com/tokio-rs/axum/pull/372143[#3742]: https://github.com/tokio-rs/axum/pull/374244[#3836]: https://github.com/tokio-rs/axum/pull/383645[#3757]: https://github.com/tokio-rs/axum/pull/375746[#3801]: https://github.com/tokio-rs/axum/pull/38014748# 0.8.94950- **added:** `WebSocketUpgrade::{requested_protocols, set_selected_protocol}` for more
2409 unmodified lines
modified:axum-extra/src/routing/mod.rs+23 added
232 unmodified lines
233 H: axum::handler::Handler<T, S>,234 T: SecondElementIs<P> + 'static,235 P: TypedPath;236237 /// Add a typed `QUERY` route to the router.238 ///239 /// The path will be inferred from the first argument to the handler function which must240 /// implement [`TypedPath`].241 ///242 /// See [`TypedPath`] for more details and examples.243 #[cfg(feature = "typed-routing")]244 fn typed_query<H, T, P>(self, handler: H) -> Self245 where246 H: axum::handler::Handler<T, S>,247 T: SecondElementIs<P> + 'static,248 P: TypedPath;249250 /// Add another route to the router with an additional "trailing slash redirect" route.251 ///252 /// If you add a route _without_ a trailing slash, such as `/foo`, this method will also add a253 /// route for `/foo/` that redirects to `/foo`.
126 unmodified lines
380 {381 self.route(P::PATH, axum::routing::connect(handler))382 }383384 #[cfg(feature = "typed-routing")]385 fn typed_query<H, T, P>(self, handler: H) -> Self386 where387 H: axum::handler::Handler<T, S>,388 T: SecondElementIs<P> + 'static,389 P: TypedPath,390 {391 self.route(P::PATH, axum::routing::query(handler))392 }393394 #[track_caller]395 fn route_with_tsr(mut self, path: &str, method_router: MethodRouter<S>) -> Self396 where397 Self: Sized,
197 unmodified lines
modified:axum-extra/CHANGELOG.md+2 added
20 unmodified lines
21 Router::new()22 .route_with_tsr("/path", get(/* handler */))23 .route_with_tsr("/path", post(/* handler */))24 ```25- **added:** Add `RouterExt::typed_query` ([#3801])26- **fixed:** Escape multipart `Content-Disposition` parameters and reject27 newlines in field names and filenames ([#3776])2829[#3599]: https://github.com/tokio-rs/axum/pull/359930[#3586]: https://github.com/tokio-rs/axum/pull/358631[#3801]: https://github.com/tokio-rs/axum/pull/380132[#3776]: https://github.com/tokio-rs/axum/pull/37763334# 0.12.635
550 unmodified lines
modified:examples/Cargo.lock+2 added-2 removed
2306 unmodified lines
2307]23082309[[package]]2310name = "http"2311version = "1.4.2"2311version = "1.5.0"2312source = "registry+https://github.com/rust-lang/crates.io-index"2313checksum = "6970f50e31d6fc17d3fa27329444bfa74e196cf62e95052a3f6fee181dba6425"2313checksum = "918d3568bebf352712bc2ef3d46a8bcf1a75b373be6539de198e9105cbbf9ce0"2314dependencies = [2315 "bytes",2316 "itoa",2317]
3146 unmodified lines
modified:Cargo.lock+2 added-2 removed
676 unmodified lines
677]678679[[package]]680name = "http"681version = "1.4.0"681version = "1.5.0"682source = "registry+https://github.com/rust-lang/crates.io-index"683checksum = "e3ba2a386d7f85a81f119ad7498ebe444d2e22c2af0b86b069416ace48b3311a"683checksum = "918d3568bebf352712bc2ef3d46a8bcf1a75b373be6539de198e9105cbbf9ce0"684dependencies = [685 "bytes",686 "itoa",687]
1696 unmodified lines