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
Provenance
- Commit
a2e64a8- Tree
1e5faaa- Author
Yunus <112053367+yunz-dev@users.noreply.github.com>- Committer
GitHub <noreply@github.com>- Parent
98884fb
9 files changed+78 added−16 removed
modified:axum/src/routing/method_filter.rs+17 added-9 removed
24 unmodified lines25 ///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);48 49 const fn bits(self) -> u16 {50 let bits = self;51 bits.048 unmodified lines100 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 lines157 MethodFilter::try_from(Method::TRACE).unwrap(),158 MethodFilter::TRACE159 );160 161+ assert_eq!(162+ MethodFilter::try_from(Method::QUERY).unwrap(),163+ MethodFilter::QUERY164+ );165+166 assert!(167 MethodFilter::try_from(http::Method::from_bytes(b"CUSTOM").unwrap())168 .unwrap_err()169 .to_string()4 unmodified linesmodified:axum/src/routing/method_routing.rs+28 added-1 removed
339 unmodified lines340 top_level_service_fn!(patch_service, PATCH);341 top_level_service_fn!(post_service, POST);342 top_level_service_fn!(put_service, PUT);343 top_level_service_fn!(trace_service, TRACE);344+top_level_service_fn!(query_service, QUERY);345 346 /// Route requests with the given method to the service.347 ///348 /// # Example96 unmodified lines445 top_level_handler_fn!(patch, PATCH);446 top_level_handler_fn!(post, POST);447 top_level_handler_fn!(put, PUT);448 top_level_handler_fn!(trace, TRACE);449+top_level_handler_fn!(query, QUERY);450 451 /// Route requests with the given method to the handler.452 ///453 /// # Example101 unmodified lines555 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 lines606 .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 lines660 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);665 666 /// Add a fallback [`Handler`] to the router.667 pub fn fallback<H, T>(mut self, handler: H) -> Self668 where25 unmodified lines694 post,695 put,696 trace,697 connect,698+ query,699 fallback,700 allow_header: _,701 } = self;702 10 unmodified lines713 (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 lines826 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 lines844 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 lines1003 &mut self.allow_header,1004 &["CONNECT"],1005 );1006 1007+ set_endpoint(1008+ "QUERY",1009+ &mut self.query,1010+ endpoint,1011+ filter,1012+ MethodFilter::QUERY,1013+ &mut self.allow_header,1014+ &["QUERY"],1015+ );1016+1017 self1018 }1019 1020 chained_service_fn!(connect_service, CONNECT);4 unmodified lines1025 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);1030 1031 #[doc = include_str!("../docs/method_routing/fallback.md")]1032 pub fn fallback_service<T>(mut self, svc: T) -> Self1033 where28 unmodified lines1062 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 lines1088 && 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 lines1106 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);1112 1113 self1114 }1115 37 unmodified lines1153 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)?;1158 1159 self.fallback = self1160 .fallback1161 .merge(other.fallback)67 unmodified lines1229 post,1230 put,1231 trace,1232 connect,1233+ query,1234 fallback,1235 allow_header,1236 } = self;1237 6 unmodified lines1244 call!(req, PUT, put);1245 call!(req, DELETE, delete);1246 call!(req, TRACE, trace);1247 call!(req, CONNECT, connect);1248+ call!(req, QUERY, query);1249 1250 let future = fallback.clone().call_with_state(req, state);1251 1252 match allow_header {35 unmodified lines1288 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 linesmodified:axum/src/routing/mod.rs+1 added-1 removed
43 unmodified lines44 45 pub 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 };50 51 macro_rules! panic_on_err {52 ($expr:expr) => {788 unmodified linesmodified:axum/Cargo.toml+1 added-1 removed
110 unmodified lines111 axum-core = { path = "../axum-core", version = "0.5.6" }112 bytes = "1.7"113 futures-core = "0.3"114 futures-util = { version = "0.3", default-features = false, features = ["alloc"] }115-http = "1.0.0"115+http = "1.5.0"116 http-body = "1.0.0"117 http-body-util = "0.1.0"118 itoa = "1.0.5"119 matchit = "=0.9.2"134 unmodified linesmodified:axum/CHANGELOG.md+2 added
24 unmodified lines25 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])32 33 [#3158]: https://github.com/tokio-rs/axum/pull/31588 unmodified lines42 [#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/380147 48 # 0.8.949 50 - **added:** `WebSocketUpgrade::{requested_protocols, set_selected_protocol}` for more2409 unmodified linesmodified:axum-extra/src/routing/mod.rs+23 added
232 unmodified lines233 H: axum::handler::Handler<T, S>,234 T: SecondElementIs<P> + 'static,235 P: TypedPath;236 237+ /// 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;249+250 /// 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 lines380 {381 self.route(P::PATH, axum::routing::connect(handler))382 }383 384+ #[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+ }393+394 #[track_caller]395 fn route_with_tsr(mut self, path: &str, method_router: MethodRouter<S>) -> Self396 where397 Self: Sized,197 unmodified linesmodified:axum-extra/CHANGELOG.md+2 added
20 unmodified lines21 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])28 29 [#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/377633 34 # 0.12.635 550 unmodified linesmodified:examples/Cargo.lock+2 added-2 removed
2306 unmodified lines2307 ]2308 2309 [[package]]2310 name = "http"2311-version = "1.4.2"2311+version = "1.5.0"2312 source = "registry+https://github.com/rust-lang/crates.io-index"2313-checksum = "6970f50e31d6fc17d3fa27329444bfa74e196cf62e95052a3f6fee181dba6425"2313+checksum = "918d3568bebf352712bc2ef3d46a8bcf1a75b373be6539de198e9105cbbf9ce0"2314 dependencies = [2315 "bytes",2316 "itoa",2317 ]3146 unmodified linesmodified:Cargo.lock+2 added-2 removed
676 unmodified lines677 ]678 679 [[package]]680 name = "http"681-version = "1.4.0"681+version = "1.5.0"682 source = "registry+https://github.com/rust-lang/crates.io-index"683-checksum = "e3ba2a386d7f85a81f119ad7498ebe444d2e22c2af0b86b069416ace48b3311a"683+checksum = "918d3568bebf352712bc2ef3d46a8bcf1a75b373be6539de198e9105cbbf9ce0"684 dependencies = [685 "bytes",686 "itoa",687 ]1696 unmodified lines