rocket/route/route.rs
1use std::fmt;
2use std::convert::From;
3use std::borrow::Cow;
4
5use yansi::Paint;
6
7use crate::http::{uri, Method, MediaType};
8use crate::route::{Handler, RouteUri, BoxFuture};
9use crate::sentinel::Sentry;
10
11/// A request handling route.
12///
13/// A route consists of exactly the information in its fields. While a `Route`
14/// can be instantiated directly, doing so should be a rare or nonexistent
15/// event. Instead, a Rocket application should use Rocket's
16/// [`#[route]`](macro@crate::route) series of attributes to generate a `Route`.
17///
18/// ```rust
19/// # #[macro_use] extern crate rocket;
20/// # use std::path::PathBuf;
21/// #[get("/route/<path..>?query", rank = 2, format = "json")]
22/// fn route_name(path: PathBuf) { /* handler procedure */ }
23///
24/// use rocket::http::{Method, MediaType};
25///
26/// let route = routes![route_name].remove(0);
27/// assert_eq!(route.name.unwrap(), "route_name");
28/// assert_eq!(route.method, Method::Get);
29/// assert_eq!(route.uri, "/route/<path..>?query");
30/// assert_eq!(route.rank, 2);
31/// assert_eq!(route.format.unwrap(), MediaType::JSON);
32/// ```
33///
34/// Note that the `rank` and `format` attribute parameters are optional. See
35/// [`#[route]`](macro@crate::route) for details on macro usage. Note also that
36/// a route's mounted _base_ becomes part of its URI; see [`RouteUri`] for
37/// details.
38///
39/// # Routing
40///
41/// A request _matches_ a route _iff_:
42///
43/// * The route's method matches that of the incoming request.
44/// * The route's format (if any) matches that of the incoming request.
45/// - If route specifies a format, it only matches requests for that format.
46/// - If route doesn't specify a format, it matches requests for any format.
47/// - A route's `format` matches against the `Accept` header in the request
48/// when the route's method [`supports_payload()`] and `Content-Type`
49/// header otherwise.
50/// - Non-specific `Accept` header components (`*`) match anything.
51/// * All static components in the route's path match the corresponding
52/// components in the same position in the incoming request.
53/// * All static components in the route's query string are also in the
54/// request query string, though in any position. If there is no query
55/// in the route, requests with and without queries match.
56///
57/// Rocket routes requests to matching routes.
58///
59/// [`supports_payload()`]: Method::supports_payload()
60///
61/// # Collisions
62///
63/// Two routes are said to _collide_ if there exists a request that matches both
64/// routes. Colliding routes present a routing ambiguity and are thus disallowed
65/// by Rocket. Because routes can be constructed dynamically, collision checking
66/// is done at [`ignite`](crate::Rocket::ignite()) time, after it becomes
67/// statically impossible to add any more routes to an instance of `Rocket`.
68///
69/// Note that because query parsing is always lenient -- extra and missing query
70/// parameters are allowed -- queries do not directly impact whether two routes
71/// collide.
72///
73/// ## Resolving Collisions
74///
75/// Collisions are resolved through _ranking_. Routes with lower ranks have
76/// higher precedence during routing than routes with higher ranks. Thus, routes
77/// are attempted in ascending rank order. If a higher precedence route returns
78/// an `Outcome` of `Forward`, the next highest precedence route is attempted,
79/// and so on, until a route returns `Success` or `Error`, or there are no
80/// more routes to try. When all routes have been attempted, Rocket issues a
81/// `404` error, handled by the appropriate [`Catcher`](crate::Catcher).
82///
83/// ## Default Ranking
84///
85/// Most collisions are automatically resolved by Rocket's _default rank_. The
86/// default rank prefers static components over dynamic components in both paths
87/// and queries: the _more_ static a route's path and query are, the lower its
88/// rank and thus the higher its precedence.
89///
90/// There are three "colors" to paths and queries:
91/// 1. `static` - all components are static
92/// 2. `partial` - at least one, but not all, components are dynamic
93/// 3. `wild` - all components are dynamic
94///
95/// Static paths carry more weight than static queries. The same is true for
96/// partial and wild paths. This results in the following default ranking
97/// table:
98///
99/// | path | query | rank |
100/// |---------|---------|------|
101/// | static | static | -12 |
102/// | static | partial | -11 |
103/// | static | wild | -10 |
104/// | static | none | -9 |
105/// | partial | static | -8 |
106/// | partial | partial | -7 |
107/// | partial | wild | -6 |
108/// | partial | none | -5 |
109/// | wild | static | -4 |
110/// | wild | partial | -3 |
111/// | wild | wild | -2 |
112/// | wild | none | -1 |
113///
114/// Recall that _lower_ ranks have _higher_ precedence.
115///
116/// ### Example
117///
118/// ```rust
119/// use rocket::Route;
120/// use rocket::http::Method;
121///
122/// macro_rules! assert_rank {
123/// ($($uri:expr => $rank:expr,)*) => {$(
124/// let route = Route::new(Method::Get, $uri, rocket::route::dummy_handler);
125/// assert_eq!(route.rank, $rank);
126/// )*}
127/// }
128///
129/// assert_rank! {
130/// "/?foo" => -12, // static path, static query
131/// "/foo/bar?a=b&bob" => -12, // static path, static query
132/// "/?a=b&bob" => -12, // static path, static query
133///
134/// "/?a&<zoo..>" => -11, // static path, partial query
135/// "/foo?a&<zoo..>" => -11, // static path, partial query
136/// "/?a&<zoo>" => -11, // static path, partial query
137///
138/// "/?<zoo..>" => -10, // static path, wild query
139/// "/foo?<zoo..>" => -10, // static path, wild query
140/// "/foo?<a>&<b>" => -10, // static path, wild query
141///
142/// "/" => -9, // static path, no query
143/// "/foo/bar" => -9, // static path, no query
144///
145/// "/a/<b>?foo" => -8, // partial path, static query
146/// "/a/<b..>?foo" => -8, // partial path, static query
147/// "/<a>/b?foo" => -8, // partial path, static query
148///
149/// "/a/<b>?<b>&c" => -7, // partial path, partial query
150/// "/a/<b..>?a&<c..>" => -7, // partial path, partial query
151///
152/// "/a/<b>?<c..>" => -6, // partial path, wild query
153/// "/a/<b..>?<c>&<d>" => -6, // partial path, wild query
154/// "/a/<b..>?<c>" => -6, // partial path, wild query
155///
156/// "/a/<b>" => -5, // partial path, no query
157/// "/<a>/b" => -5, // partial path, no query
158/// "/a/<b..>" => -5, // partial path, no query
159///
160/// "/<b>/<c>?foo&bar" => -4, // wild path, static query
161/// "/<a>/<b..>?foo" => -4, // wild path, static query
162/// "/<b..>?cat" => -4, // wild path, static query
163///
164/// "/<b>/<c>?<foo>&bar" => -3, // wild path, partial query
165/// "/<a>/<b..>?a&<b..>" => -3, // wild path, partial query
166/// "/<b..>?cat&<dog>" => -3, // wild path, partial query
167///
168/// "/<b>/<c>?<foo>" => -2, // wild path, wild query
169/// "/<a>/<b..>?<b..>" => -2, // wild path, wild query
170/// "/<b..>?<c>&<dog>" => -2, // wild path, wild query
171///
172/// "/<b>/<c>" => -1, // wild path, no query
173/// "/<a>/<b..>" => -1, // wild path, no query
174/// "/<b..>" => -1, // wild path, no query
175/// }
176/// ```
177#[derive(Clone)]
178pub struct Route {
179 /// The name of this route, if one was given.
180 pub name: Option<Cow<'static, str>>,
181 /// The method this route matches against.
182 pub method: Method,
183 /// The function that should be called when the route matches.
184 pub handler: Box<dyn Handler>,
185 /// The route URI.
186 pub uri: RouteUri<'static>,
187 /// The rank of this route. Lower ranks have higher priorities.
188 pub rank: isize,
189 /// The media type this route matches against, if any.
190 pub format: Option<MediaType>,
191 /// The discovered sentinels.
192 pub(crate) sentinels: Vec<Sentry>,
193}
194
195impl Route {
196 /// Creates a new route with the given method, path, and handler with a base
197 /// of `/` and a computed [default rank](#default-ranking).
198 ///
199 /// # Panics
200 ///
201 /// Panics if `path` is not a valid Rocket route URI.
202 ///
203 /// # Example
204 ///
205 /// ```rust
206 /// use rocket::Route;
207 /// use rocket::http::Method;
208 /// # use rocket::route::dummy_handler as handler;
209 ///
210 /// // this is a rank 1 route matching requests to `GET /`
211 /// let index = Route::new(Method::Get, "/", handler);
212 /// assert_eq!(index.rank, -9);
213 /// assert_eq!(index.method, Method::Get);
214 /// assert_eq!(index.uri, "/");
215 /// ```
216 #[track_caller]
217 pub fn new<H: Handler>(method: Method, uri: &str, handler: H) -> Route {
218 Route::ranked(None, method, uri, handler)
219 }
220
221 /// Creates a new route with the given rank, method, path, and handler with
222 /// a base of `/`. If `rank` is `None`, the computed [default
223 /// rank](#default-ranking) is used.
224 ///
225 /// # Panics
226 ///
227 /// Panics if `path` is not a valid Rocket route URI.
228 ///
229 /// # Example
230 ///
231 /// ```rust
232 /// use rocket::Route;
233 /// use rocket::http::Method;
234 /// # use rocket::route::dummy_handler as handler;
235 ///
236 /// let foo = Route::ranked(1, Method::Post, "/foo?bar", handler);
237 /// assert_eq!(foo.rank, 1);
238 /// assert_eq!(foo.method, Method::Post);
239 /// assert_eq!(foo.uri, "/foo?bar");
240 ///
241 /// let foo = Route::ranked(None, Method::Post, "/foo?bar", handler);
242 /// assert_eq!(foo.rank, -12);
243 /// assert_eq!(foo.method, Method::Post);
244 /// assert_eq!(foo.uri, "/foo?bar");
245 /// ```
246 #[track_caller]
247 pub fn ranked<H, R>(rank: R, method: Method, uri: &str, handler: H) -> Route
248 where H: Handler + 'static, R: Into<Option<isize>>,
249 {
250 let uri = RouteUri::new("/", uri);
251 let rank = rank.into().unwrap_or_else(|| uri.default_rank());
252 Route {
253 name: None,
254 format: None,
255 sentinels: Vec::new(),
256 handler: Box::new(handler),
257 rank, uri, method,
258 }
259 }
260
261 /// Maps the `base` of this route using `mapper`, returning a new `Route`
262 /// with the returned base.
263 ///
264 /// `mapper` is called with the current base. The returned `String` is used
265 /// as the new base if it is a valid URI. If the returned base URI contains
266 /// a query, it is ignored. Returns an error if the base produced by
267 /// `mapper` is not a valid origin URI.
268 ///
269 /// # Example
270 ///
271 /// ```rust
272 /// use rocket::Route;
273 /// use rocket::http::{Method, uri::Origin};
274 /// # use rocket::route::dummy_handler as handler;
275 ///
276 /// let index = Route::new(Method::Get, "/foo/bar", handler);
277 /// assert_eq!(index.uri.base(), "/");
278 /// assert_eq!(index.uri.unmounted_origin.path(), "/foo/bar");
279 /// assert_eq!(index.uri.path(), "/foo/bar");
280 ///
281 /// let index = index.map_base(|base| format!("{}{}", "/boo", base)).unwrap();
282 /// assert_eq!(index.uri.base(), "/boo");
283 /// assert_eq!(index.uri.unmounted_origin.path(), "/foo/bar");
284 /// assert_eq!(index.uri.path(), "/boo/foo/bar");
285 /// ```
286 pub fn map_base<'a, F>(mut self, mapper: F) -> Result<Self, uri::Error<'static>>
287 where F: FnOnce(uri::Origin<'a>) -> String
288 {
289 let base = mapper(self.uri.base);
290 self.uri = RouteUri::try_new(&base, &self.uri.unmounted_origin.to_string())?;
291 Ok(self)
292 }
293}
294
295impl fmt::Display for Route {
296 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
297 if let Some(ref n) = self.name {
298 write!(f, "{}{}{} ", "(".cyan(), n.primary(), ")".cyan())?;
299 }
300
301 write!(f, "{} ", self.method.green())?;
302 if self.uri.base() != "/" {
303 write!(f, "{}", self.uri.base().blue().underline())?;
304 }
305
306 write!(f, "{}", self.uri.unmounted_origin.blue())?;
307
308 if self.rank > 1 {
309 write!(f, " [{}]", self.rank.primary().bold())?;
310 }
311
312 if let Some(ref format) = self.format {
313 write!(f, " {}", format.yellow())?;
314 }
315
316 Ok(())
317 }
318}
319
320impl fmt::Debug for Route {
321 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
322 f.debug_struct("Route")
323 .field("name", &self.name)
324 .field("method", &self.method)
325 .field("uri", &self.uri)
326 .field("rank", &self.rank)
327 .field("format", &self.format)
328 .finish()
329 }
330}
331
332/// Information generated by the `route` attribute during codegen.
333#[doc(hidden)]
334pub struct StaticInfo {
335 /// The route's name, i.e, the name of the function.
336 pub name: &'static str,
337 /// The route's method.
338 pub method: Method,
339 /// The route's URi, without the base mount point.
340 pub uri: &'static str,
341 /// The route's format, if any.
342 pub format: Option<MediaType>,
343 /// The route's handler, i.e, the annotated function.
344 pub handler: for<'r> fn(&'r crate::Request<'_>, crate::Data<'r>) -> BoxFuture<'r>,
345 /// The route's rank, if any.
346 pub rank: Option<isize>,
347 /// Route-derived sentinels, if any.
348 /// This isn't `&'static [SentryInfo]` because `type_name()` isn't `const`.
349 pub sentinels: Vec<Sentry>,
350}
351
352#[doc(hidden)]
353impl From<StaticInfo> for Route {
354 fn from(info: StaticInfo) -> Route {
355 // This should never panic since `info.path` is statically checked.
356 let uri = RouteUri::new("/", info.uri);
357
358 Route {
359 name: Some(info.name.into()),
360 method: info.method,
361 handler: Box::new(info.handler),
362 rank: info.rank.unwrap_or_else(|| uri.default_rank()),
363 format: info.format,
364 sentinels: info.sentinels.into_iter().collect(),
365 uri,
366 }
367 }
368}