rocket_community/route/
route.rs

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