Add trailing segments to request
This commit is contained in:
parent
54816e1f67
commit
59e3222ce8
|
@ -18,31 +18,33 @@ async fn main() -> Result<()> {
|
||||||
.await
|
.await
|
||||||
}
|
}
|
||||||
|
|
||||||
fn handle_base(_: Request) -> BoxFuture<'static, Result<Response>> {
|
fn handle_base(req: Request) -> BoxFuture<'static, Result<Response>> {
|
||||||
let doc = generate_doc("base");
|
let doc = generate_doc("base", &req);
|
||||||
async move {
|
async move {
|
||||||
Ok(Response::document(doc))
|
Ok(Response::document(doc))
|
||||||
}.boxed()
|
}.boxed()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn handle_short(_: Request) -> BoxFuture<'static, Result<Response>> {
|
fn handle_short(req: Request) -> BoxFuture<'static, Result<Response>> {
|
||||||
let doc = generate_doc("short");
|
let doc = generate_doc("short", &req);
|
||||||
async move {
|
async move {
|
||||||
Ok(Response::document(doc))
|
Ok(Response::document(doc))
|
||||||
}.boxed()
|
}.boxed()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn handle_long(_: Request) -> BoxFuture<'static, Result<Response>> {
|
fn handle_long(req: Request) -> BoxFuture<'static, Result<Response>> {
|
||||||
let doc = generate_doc("long");
|
let doc = generate_doc("long", &req);
|
||||||
async move {
|
async move {
|
||||||
Ok(Response::document(doc))
|
Ok(Response::document(doc))
|
||||||
}.boxed()
|
}.boxed()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn generate_doc(route_name: &str) -> Document {
|
fn generate_doc(route_name: &str, req: &Request) -> Document {
|
||||||
|
let trailing = req.trailing_segments().join("/");
|
||||||
let mut doc = Document::new();
|
let mut doc = Document::new();
|
||||||
doc.add_heading(HeadingLevel::H1, "Routing Demo")
|
doc.add_heading(HeadingLevel::H1, "Routing Demo")
|
||||||
.add_text(&format!("You're currently on the {} route", route_name))
|
.add_text(&format!("You're currently on the {} route", route_name))
|
||||||
|
.add_text(&format!("Trailing segments: /{}", trailing))
|
||||||
.add_blank_line()
|
.add_blank_line()
|
||||||
.add_text("Here's some links to try:")
|
.add_text("Here's some links to try:")
|
||||||
.add_link_without_label("/")
|
.add_link_without_label("/")
|
||||||
|
|
|
@ -95,7 +95,9 @@ impl Server {
|
||||||
|
|
||||||
request.set_cert(client_cert);
|
request.set_cert(client_cert);
|
||||||
|
|
||||||
let response = if let Some(handler) = self.routes.match_request(&request) {
|
let response = if let Some((trailing, handler)) = self.routes.match_request(&request) {
|
||||||
|
|
||||||
|
request.set_trailing(trailing);
|
||||||
|
|
||||||
let handler = (handler)(request);
|
let handler = (handler)(request);
|
||||||
let handler = AssertUnwindSafe(handler);
|
let handler = AssertUnwindSafe(handler);
|
||||||
|
|
|
@ -2,7 +2,7 @@
|
||||||
//!
|
//!
|
||||||
//! See [`RoutingNode`] for details on how routes are matched.
|
//! See [`RoutingNode`] for details on how routes are matched.
|
||||||
|
|
||||||
use uriparse::path::Path;
|
use uriparse::path::{Path, Segment};
|
||||||
|
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
use std::convert::TryInto;
|
use std::convert::TryInto;
|
||||||
|
@ -34,39 +34,64 @@ impl RoutingNode {
|
||||||
/// represented as a sequence of path segments. For example, "/dir/image.png?text"
|
/// represented as a sequence of path segments. For example, "/dir/image.png?text"
|
||||||
/// should be represented as `&["dir", "image.png"]`.
|
/// should be represented as `&["dir", "image.png"]`.
|
||||||
///
|
///
|
||||||
|
/// If a match is found, it is returned, along with the segments of the path trailing
|
||||||
|
/// the handler. For example, a route `/foo` recieving a request to `/foo/bar` would
|
||||||
|
/// receive `vec!["bar"]`
|
||||||
|
///
|
||||||
/// See [`RoutingNode`] for details on how routes are matched.
|
/// See [`RoutingNode`] for details on how routes are matched.
|
||||||
pub fn match_path<I,S>(&self, path: I) -> Option<&Handler>
|
pub fn match_path<I,S>(&self, path: I) -> Option<(Vec<S>, &Handler)>
|
||||||
where
|
where
|
||||||
I: IntoIterator<Item=S>,
|
I: IntoIterator<Item=S>,
|
||||||
S: AsRef<str>,
|
S: AsRef<str>,
|
||||||
{
|
{
|
||||||
let mut node = self;
|
let mut node = self;
|
||||||
let mut path = path.into_iter();
|
let mut path = path.into_iter().filter(|seg| !seg.as_ref().is_empty());
|
||||||
let mut last_seen_handler = None;
|
let mut last_seen_handler = None;
|
||||||
|
let mut since_last_handler = Vec::new();
|
||||||
loop {
|
loop {
|
||||||
let Self(maybe_handler, map) = node;
|
let Self(maybe_handler, map) = node;
|
||||||
|
|
||||||
last_seen_handler = maybe_handler.as_ref().or(last_seen_handler);
|
if maybe_handler.is_some() {
|
||||||
|
last_seen_handler = maybe_handler.as_ref();
|
||||||
|
since_last_handler.clear();
|
||||||
|
}
|
||||||
|
|
||||||
if let Some(segment) = path.next() {
|
if let Some(segment) = path.next() {
|
||||||
if let Some(route) = map.get(segment.as_ref()) {
|
let maybe_route = map.get(segment.as_ref());
|
||||||
|
since_last_handler.push(segment);
|
||||||
|
|
||||||
|
if let Some(route) = maybe_route {
|
||||||
node = route;
|
node = route;
|
||||||
} else {
|
} else {
|
||||||
return last_seen_handler;
|
break;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
return last_seen_handler;
|
break;
|
||||||
}
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
if let Some(handler) = last_seen_handler {
|
||||||
|
since_last_handler.extend(path);
|
||||||
|
Some((since_last_handler, handler))
|
||||||
|
} else {
|
||||||
|
None
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Attempt to identify a route for a given [`Request`]
|
/// Attempt to identify a route for a given [`Request`]
|
||||||
///
|
///
|
||||||
/// See [`RoutingNode`] for details on how routes are matched.
|
/// See [`RoutingNode::match_path()`] for more information
|
||||||
pub fn match_request(&self, req: &Request) -> Option<&Handler> {
|
pub fn match_request(&self, req: &Request) -> Option<(Vec<String>, &Handler)> {
|
||||||
let mut path = req.path().to_owned();
|
let mut path = req.path().to_borrowed();
|
||||||
path.normalize(false);
|
path.normalize(false);
|
||||||
self.match_path(path.segments())
|
self.match_path(path.segments())
|
||||||
|
.map(|(segs, h)| (
|
||||||
|
segs.into_iter()
|
||||||
|
.map(Segment::as_str)
|
||||||
|
.map(str::to_owned)
|
||||||
|
.collect(),
|
||||||
|
h,
|
||||||
|
))
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Add a route to the network
|
/// Add a route to the network
|
||||||
|
|
|
@ -8,6 +8,7 @@ pub struct Request {
|
||||||
uri: URIReference<'static>,
|
uri: URIReference<'static>,
|
||||||
input: Option<String>,
|
input: Option<String>,
|
||||||
certificate: Option<Certificate>,
|
certificate: Option<Certificate>,
|
||||||
|
trailing_segments: Option<Vec<String>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Request {
|
impl Request {
|
||||||
|
@ -36,6 +37,7 @@ impl Request {
|
||||||
uri,
|
uri,
|
||||||
input,
|
input,
|
||||||
certificate,
|
certificate,
|
||||||
|
trailing_segments: None,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -43,6 +45,31 @@ impl Request {
|
||||||
&self.uri
|
&self.uri
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[allow(clippy::missing_const_for_fn)]
|
||||||
|
/// All of the path segments following the route to which this request was bound.
|
||||||
|
///
|
||||||
|
/// For example, if this handler was bound to the `/api` route, and a request was
|
||||||
|
/// received to `/api/v1/endpoint`, then this value would be `["v1", "endpoint"]`.
|
||||||
|
/// This should not be confused with [`path_segments()`](Self::path_segments()), which
|
||||||
|
/// contains *all* of the segments, not just those trailing the route.
|
||||||
|
///
|
||||||
|
/// If the trailing segments have not been set, this method will panic, but this
|
||||||
|
/// should only be possible if you are constructing the Request yourself. Requests
|
||||||
|
/// to handlers registered through [`add_route`](northstar::Builder::add_route()) will
|
||||||
|
/// always have trailing segments set.
|
||||||
|
pub fn trailing_segments(&self) -> &Vec<String> {
|
||||||
|
self.trailing_segments.as_ref().unwrap()
|
||||||
|
}
|
||||||
|
|
||||||
|
/// All of the segments in this path, percent decoded
|
||||||
|
///
|
||||||
|
/// For example, for a request to `/api/v1/endpoint`, this would return `["api", "v1",
|
||||||
|
/// "endpoint"]`, no matter what route the handler that recieved this request was
|
||||||
|
/// bound to. This is not to be confused with
|
||||||
|
/// [`trailing_segments()`](Self::trailing_segments), which contains only the segments
|
||||||
|
/// following the bound route.
|
||||||
|
///
|
||||||
|
/// Additionally, unlike `trailing_segments()`, this method percent decodes the path.
|
||||||
pub fn path_segments(&self) -> Vec<String> {
|
pub fn path_segments(&self) -> Vec<String> {
|
||||||
self.uri()
|
self.uri()
|
||||||
.path()
|
.path()
|
||||||
|
@ -60,6 +87,10 @@ impl Request {
|
||||||
self.certificate = cert;
|
self.certificate = cert;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn set_trailing(&mut self, segments: Vec<String>) {
|
||||||
|
self.trailing_segments = Some(segments);
|
||||||
|
}
|
||||||
|
|
||||||
pub const fn certificate(&self) -> Option<&Certificate> {
|
pub const fn certificate(&self) -> Option<&Certificate> {
|
||||||
self.certificate.as_ref()
|
self.certificate.as_ref()
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue