Merge branch 'routes' into user-management
This commit is contained in:
commit
e678e45b78
|
@ -16,6 +16,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|||
- `server_dir` default feature for serve_dir utils [@Alch-Emi](https://github.com/Alch-Emi)
|
||||
### Improved
|
||||
- build time and size by [@Alch-Emi](https://github.com/Alch-Emi)
|
||||
### Changed
|
||||
- Added route API [@Alch-Emi](https://github.com/Alch-Emi)
|
||||
|
||||
## [0.3.0] - 2020-11-14
|
||||
### Added
|
||||
|
|
|
@ -19,7 +19,8 @@ async fn main() -> Result<()> {
|
|||
let users = Arc::<RwLock::<HashMap<CertBytes, String>>>::default();
|
||||
|
||||
Server::bind(("0.0.0.0", GEMINI_PORT))
|
||||
.serve(move|req| handle_request(users.clone(), req))
|
||||
.add_route("/", move|req| handle_request(users.clone(), req))
|
||||
.serve()
|
||||
.await
|
||||
}
|
||||
|
||||
|
|
|
@ -12,7 +12,8 @@ async fn main() -> Result<()> {
|
|||
.init();
|
||||
|
||||
Server::bind(("localhost", GEMINI_PORT))
|
||||
.serve(handle_request)
|
||||
.add_route("/",handle_request)
|
||||
.serve()
|
||||
.await
|
||||
}
|
||||
|
||||
|
|
56
examples/routing.rs
Normal file
56
examples/routing.rs
Normal file
|
@ -0,0 +1,56 @@
|
|||
use anyhow::*;
|
||||
use futures_core::future::BoxFuture;
|
||||
use futures_util::FutureExt;
|
||||
use log::LevelFilter;
|
||||
use northstar::{Document, document::HeadingLevel, Request, Response, GEMINI_PORT};
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() -> Result<()> {
|
||||
env_logger::builder()
|
||||
.filter_module("northstar", LevelFilter::Debug)
|
||||
.init();
|
||||
|
||||
northstar::Server::bind(("localhost", GEMINI_PORT))
|
||||
.add_route("/", handle_base)
|
||||
.add_route("/route", handle_short)
|
||||
.add_route("/route/long", handle_long)
|
||||
.serve()
|
||||
.await
|
||||
}
|
||||
|
||||
fn handle_base(req: Request) -> BoxFuture<'static, Result<Response>> {
|
||||
let doc = generate_doc("base", &req);
|
||||
async move {
|
||||
Ok(Response::document(doc))
|
||||
}.boxed()
|
||||
}
|
||||
|
||||
fn handle_short(req: Request) -> BoxFuture<'static, Result<Response>> {
|
||||
let doc = generate_doc("short", &req);
|
||||
async move {
|
||||
Ok(Response::document(doc))
|
||||
}.boxed()
|
||||
}
|
||||
|
||||
fn handle_long(req: Request) -> BoxFuture<'static, Result<Response>> {
|
||||
let doc = generate_doc("long", &req);
|
||||
async move {
|
||||
Ok(Response::document(doc))
|
||||
}.boxed()
|
||||
}
|
||||
|
||||
fn generate_doc(route_name: &str, req: &Request) -> Document {
|
||||
let trailing = req.trailing_segments().join("/");
|
||||
let mut doc = Document::new();
|
||||
doc.add_heading(HeadingLevel::H1, "Routing Demo")
|
||||
.add_text(&format!("You're currently on the {} route", route_name))
|
||||
.add_text(&format!("Trailing segments: /{}", trailing))
|
||||
.add_blank_line()
|
||||
.add_text("Here's some links to try:")
|
||||
.add_link_without_label("/")
|
||||
.add_link_without_label("/route")
|
||||
.add_link_without_label("/route/long")
|
||||
.add_link_without_label("/route/not_real")
|
||||
.add_link_without_label("/rowte");
|
||||
doc
|
||||
}
|
|
@ -11,7 +11,8 @@ async fn main() -> Result<()> {
|
|||
.init();
|
||||
|
||||
Server::bind(("localhost", GEMINI_PORT))
|
||||
.serve(handle_request)
|
||||
.add_route("/", handle_request)
|
||||
.serve()
|
||||
.await
|
||||
}
|
||||
|
||||
|
|
|
@ -18,7 +18,8 @@ async fn main() -> Result<()> {
|
|||
.init();
|
||||
|
||||
Server::bind(("0.0.0.0", GEMINI_PORT))
|
||||
.serve(handle_request)
|
||||
.add_route("/", handle_request)
|
||||
.serve()
|
||||
.await
|
||||
}
|
||||
|
||||
|
|
54
src/lib.rs
54
src/lib.rs
|
@ -22,9 +22,11 @@ use rustls::*;
|
|||
use anyhow::*;
|
||||
use lazy_static::lazy_static;
|
||||
use crate::util::opt_timeout;
|
||||
use routing::RoutingNode;
|
||||
|
||||
pub mod types;
|
||||
pub mod util;
|
||||
pub mod routing;
|
||||
#[cfg(feature = "user_management")]
|
||||
pub mod user_management;
|
||||
|
||||
|
@ -45,7 +47,7 @@ pub (crate) type HandlerResponse = BoxFuture<'static, Result<Response>>;
|
|||
pub struct Server {
|
||||
tls_acceptor: TlsAcceptor,
|
||||
listener: Arc<TcpListener>,
|
||||
handler: Handler,
|
||||
routes: Arc<RoutingNode<Handler>>,
|
||||
timeout: Duration,
|
||||
complex_timeout: Option<Duration>,
|
||||
#[cfg(feature="user_management")]
|
||||
|
@ -104,19 +106,26 @@ impl Server {
|
|||
|
||||
request.set_cert(client_cert);
|
||||
|
||||
let handler = (self.handler)(request);
|
||||
let handler = AssertUnwindSafe(handler);
|
||||
let response = if let Some((trailing, handler)) = self.routes.match_request(&request) {
|
||||
|
||||
let response = util::HandlerCatchUnwind::new(handler).await
|
||||
.unwrap_or_else(|_| Response::server_error(""))
|
||||
.or_else(|err| {
|
||||
error!("Handler failed: {:?}", err);
|
||||
Response::server_error("")
|
||||
})
|
||||
.context("Request handler failed")?;
|
||||
request.set_trailing(trailing);
|
||||
|
||||
self.send_response(response, &mut stream).await
|
||||
.context("Failed to send response")?;
|
||||
let handler = (handler)(request);
|
||||
let handler = AssertUnwindSafe(handler);
|
||||
|
||||
util::HandlerCatchUnwind::new(handler).await
|
||||
.unwrap_or_else(|_| Response::server_error(""))
|
||||
.or_else(|err| {
|
||||
error!("Handler failed: {:?}", err);
|
||||
Response::server_error("")
|
||||
})
|
||||
.context("Request handler failed")?
|
||||
} else {
|
||||
Response::not_found()
|
||||
};
|
||||
|
||||
self.send_response(response, &mut stream).await
|
||||
.context("Failed to send response")?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
@ -209,6 +218,7 @@ pub struct Builder<A> {
|
|||
key_path: PathBuf,
|
||||
timeout: Duration,
|
||||
complex_body_timeout_override: Option<Duration>,
|
||||
routes: RoutingNode<Handler>,
|
||||
#[cfg(feature="user_management")]
|
||||
data_dir: PathBuf,
|
||||
}
|
||||
|
@ -221,6 +231,7 @@ impl<A: ToSocketAddrs> Builder<A> {
|
|||
complex_body_timeout_override: Some(Duration::from_secs(30)),
|
||||
cert_path: PathBuf::from("cert/cert.pem"),
|
||||
key_path: PathBuf::from("cert/key.pem"),
|
||||
routes: RoutingNode::default(),
|
||||
#[cfg(feature="user_management")]
|
||||
data_dir: "data".into(),
|
||||
}
|
||||
|
@ -334,20 +345,33 @@ impl<A: ToSocketAddrs> Builder<A> {
|
|||
self
|
||||
}
|
||||
|
||||
pub async fn serve<F>(self, handler: F) -> Result<()>
|
||||
/// Add a handler for a route
|
||||
///
|
||||
/// A route must be an absolute path, for example "/endpoint" or "/", but not
|
||||
/// "endpoint". Entering a relative or malformed path will result in a panic.
|
||||
///
|
||||
/// For more information about routing mechanics, see the docs for [`RoutingNode`].
|
||||
pub fn add_route<H>(mut self, path: &'static str, handler: H) -> Self
|
||||
where
|
||||
F: Fn(Request) -> HandlerResponse + Send + Sync + 'static,
|
||||
H: Fn(Request) -> HandlerResponse + Send + Sync + 'static,
|
||||
{
|
||||
self.routes.add_route(path, Arc::new(handler));
|
||||
self
|
||||
}
|
||||
|
||||
pub async fn serve(mut self) -> Result<()> {
|
||||
let config = tls_config(&self.cert_path, &self.key_path)
|
||||
.context("Failed to create TLS config")?;
|
||||
|
||||
let listener = TcpListener::bind(self.addr).await
|
||||
.context("Failed to create socket")?;
|
||||
|
||||
self.routes.shrink();
|
||||
|
||||
let server = Server {
|
||||
tls_acceptor: TlsAcceptor::from(config),
|
||||
listener: Arc::new(listener),
|
||||
handler: Arc::new(handler),
|
||||
routes: Arc::new(self.routes),
|
||||
timeout: self.timeout,
|
||||
complex_timeout: self.complex_body_timeout_override,
|
||||
#[cfg(feature="user_management")]
|
||||
|
|
157
src/routing.rs
Normal file
157
src/routing.rs
Normal file
|
@ -0,0 +1,157 @@
|
|||
//! Utilities for routing requests
|
||||
//!
|
||||
//! See [`RoutingNode`] for details on how routes are matched.
|
||||
|
||||
use uriparse::path::{Path, Segment};
|
||||
|
||||
use std::collections::HashMap;
|
||||
use std::convert::TryInto;
|
||||
|
||||
use crate::types::Request;
|
||||
|
||||
/// A node for linking values to routes
|
||||
///
|
||||
/// Routing is processed by a tree, with each child being a single path segment. For
|
||||
/// example, if an entry existed at "/trans/rights", then the root-level node would have
|
||||
/// a child "trans", which would have a child "rights". "rights" would have no children,
|
||||
/// but would have an attached entry.
|
||||
///
|
||||
/// If one route is shorter than another, say "/trans/rights" and
|
||||
/// "/trans/rights/r/human", then the longer route always matches first, so a request for
|
||||
/// "/trans/rights/r/human/rights" would be routed to "/trans/rights/r/human", and
|
||||
/// "/trans/rights/now" would route to "/trans/rights"
|
||||
///
|
||||
/// Routing is only performed on normalized paths, so "/endpoint" and "/endpoint/" are
|
||||
/// considered to be the same route.
|
||||
pub struct RoutingNode<T>(Option<T>, HashMap<String, Self>);
|
||||
|
||||
impl<T> RoutingNode<T> {
|
||||
/// Attempt to find and entry based on path segments
|
||||
///
|
||||
/// This searches the network of routing nodes attempting to match a specific request,
|
||||
/// represented as a sequence of path segments. For example, "/dir/image.png?text"
|
||||
/// should be represented as `&["dir", "image.png"]`.
|
||||
///
|
||||
/// If a match is found, it is returned, along with the segments of the path trailing
|
||||
/// the subpath matcing the route. For example, a route `/foo` recieving a request to
|
||||
/// `/foo/bar` would produce `vec!["bar"]`
|
||||
///
|
||||
/// See [`RoutingNode`] for details on how routes are matched.
|
||||
pub fn match_path<I,S>(&self, path: I) -> Option<(Vec<S>, &T)>
|
||||
where
|
||||
I: IntoIterator<Item=S>,
|
||||
S: AsRef<str>,
|
||||
{
|
||||
let mut node = self;
|
||||
let mut path = path.into_iter().filter(|seg| !seg.as_ref().is_empty());
|
||||
let mut last_seen_handler = None;
|
||||
let mut since_last_handler = Vec::new();
|
||||
loop {
|
||||
let Self(maybe_handler, map) = node;
|
||||
|
||||
if maybe_handler.is_some() {
|
||||
last_seen_handler = maybe_handler.as_ref();
|
||||
since_last_handler.clear();
|
||||
}
|
||||
|
||||
if let Some(segment) = path.next() {
|
||||
let maybe_route = map.get(segment.as_ref());
|
||||
since_last_handler.push(segment);
|
||||
|
||||
if let Some(route) = maybe_route {
|
||||
node = route;
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
} else {
|
||||
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`]
|
||||
///
|
||||
/// See [`RoutingNode::match_path()`] for more information
|
||||
pub fn match_request(&self, req: &Request) -> Option<(Vec<String>, &T)> {
|
||||
let mut path = req.path().to_borrowed();
|
||||
path.normalize(false);
|
||||
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
|
||||
///
|
||||
/// This method wraps [`add_route_by_path()`](Self::add_route_by_path()) while
|
||||
/// unwrapping any errors that might occur. For this reason, this method only takes
|
||||
/// static strings. If you would like to add a string dynamically, please use
|
||||
/// [`RoutingNode::add_route_by_path()`] in order to appropriately deal with any
|
||||
/// errors that might arise.
|
||||
pub fn add_route(&mut self, path: &'static str, data: T) {
|
||||
let path: Path = path.try_into().expect("Malformed path route received");
|
||||
self.add_route_by_path(path, data).unwrap();
|
||||
}
|
||||
|
||||
/// Add a route to the network
|
||||
///
|
||||
/// The path provided MUST be absolute. Callers should verify this before calling
|
||||
/// this method.
|
||||
///
|
||||
/// For information about how routes work, see [`RoutingNode::match_path()`]
|
||||
pub fn add_route_by_path(&mut self, mut path: Path, data: T) -> Result<(), ConflictingRouteError>{
|
||||
debug_assert!(path.is_absolute());
|
||||
path.normalize(false);
|
||||
|
||||
let mut node = self;
|
||||
for segment in path.segments() {
|
||||
if segment != "" {
|
||||
node = node.1.entry(segment.to_string()).or_default();
|
||||
}
|
||||
}
|
||||
|
||||
if node.0.is_some() {
|
||||
Err(ConflictingRouteError())
|
||||
} else {
|
||||
node.0 = Some(data);
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
/// Recursively shrink maps to fit
|
||||
pub fn shrink(&mut self) {
|
||||
let mut to_shrink = vec![&mut self.1];
|
||||
while let Some(shrink) = to_shrink.pop() {
|
||||
shrink.shrink_to_fit();
|
||||
to_shrink.extend(shrink.values_mut().map(|n| &mut n.1));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> Default for RoutingNode<T> {
|
||||
fn default() -> Self {
|
||||
Self(None, HashMap::default())
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
pub struct ConflictingRouteError();
|
||||
|
||||
impl std::error::Error for ConflictingRouteError { }
|
||||
|
||||
impl std::fmt::Display for ConflictingRouteError {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
|
||||
write!(f, "Attempted to create a route with the same matcher as an existing route")
|
||||
}
|
||||
}
|
|
@ -11,7 +11,7 @@ pub enum Body {
|
|||
|
||||
impl From<Document> for Body {
|
||||
fn from(document: Document) -> Self {
|
||||
Body::from(document.to_string())
|
||||
Self::from(document.to_string())
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -183,7 +183,7 @@ impl Document {
|
|||
.map(URIReference::into_owned)
|
||||
.or_else(|_| ".".try_into()).expect("Northstar BUG");
|
||||
let label = LinkLabel::from_lossy(label);
|
||||
let link = Link { uri, label: Some(label) };
|
||||
let link = Link { uri: Box::new(uri), label: Some(label) };
|
||||
let link = Item::Link(link);
|
||||
|
||||
self.add_item(link);
|
||||
|
@ -213,7 +213,7 @@ impl Document {
|
|||
.map(URIReference::into_owned)
|
||||
.or_else(|_| ".".try_into()).expect("Northstar BUG");
|
||||
let link = Link {
|
||||
uri,
|
||||
uri: Box::new(uri),
|
||||
label: None,
|
||||
};
|
||||
let link = Item::Link(link);
|
||||
|
@ -391,6 +391,7 @@ impl fmt::Display for Document {
|
|||
}
|
||||
}
|
||||
|
||||
#[allow(clippy::enum_variant_names)]
|
||||
enum Item {
|
||||
Text(Text),
|
||||
Link(Link),
|
||||
|
@ -414,7 +415,7 @@ impl Text {
|
|||
}
|
||||
|
||||
struct Link {
|
||||
uri: URIReference<'static>,
|
||||
uri: Box<URIReference<'static>>,
|
||||
label: Option<LinkLabel>,
|
||||
}
|
||||
|
||||
|
@ -424,7 +425,7 @@ impl LinkLabel {
|
|||
fn from_lossy(line: impl Cowy<str>) -> Self {
|
||||
let line = strip_newlines(line);
|
||||
|
||||
LinkLabel(line)
|
||||
Self(line)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -12,7 +12,7 @@ impl Meta {
|
|||
/// Creates a new "Meta" string.
|
||||
/// Fails if `meta` contains `\n`.
|
||||
pub fn new(meta: impl Cowy<str>) -> Result<Self> {
|
||||
ensure!(!meta.as_ref().contains("\n"), "Meta must not contain newlines");
|
||||
ensure!(!meta.as_ref().contains('\n'), "Meta must not contain newlines");
|
||||
ensure!(meta.as_ref().len() <= Self::MAX_LEN, "Meta must not exceed {} bytes", Self::MAX_LEN);
|
||||
|
||||
Ok(Self(meta.into()))
|
||||
|
|
|
@ -13,6 +13,7 @@ pub struct Request {
|
|||
uri: URIReference<'static>,
|
||||
input: Option<String>,
|
||||
certificate: Option<Certificate>,
|
||||
trailing_segments: Option<Vec<String>>,
|
||||
#[cfg(feature="user_management")]
|
||||
manager: UserManager,
|
||||
}
|
||||
|
@ -54,15 +55,41 @@ impl Request {
|
|||
uri,
|
||||
input,
|
||||
certificate,
|
||||
trailing_segments: None,
|
||||
#[cfg(feature="user_management")]
|
||||
manager,
|
||||
})
|
||||
}
|
||||
|
||||
pub fn uri(&self) -> &URIReference {
|
||||
pub const fn uri(&self) -> &URIReference {
|
||||
&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> {
|
||||
self.uri()
|
||||
.path()
|
||||
|
@ -80,7 +107,11 @@ impl Request {
|
|||
self.certificate = cert;
|
||||
}
|
||||
|
||||
pub fn certificate(&self) -> Option<&Certificate> {
|
||||
pub fn set_trailing(&mut self, segments: Vec<String>) {
|
||||
self.trailing_segments = Some(segments);
|
||||
}
|
||||
|
||||
pub const fn certificate(&self) -> Option<&Certificate> {
|
||||
self.certificate.as_ref()
|
||||
}
|
||||
|
||||
|
|
|
@ -12,7 +12,7 @@ pub struct Response {
|
|||
}
|
||||
|
||||
impl Response {
|
||||
pub fn new(header: ResponseHeader) -> Self {
|
||||
pub const fn new(header: ResponseHeader) -> Self {
|
||||
Self {
|
||||
header,
|
||||
body: None,
|
||||
|
@ -34,7 +34,7 @@ impl Response {
|
|||
}
|
||||
|
||||
pub fn success(mime: &Mime) -> Self {
|
||||
let header = ResponseHeader::success(&mime);
|
||||
let header = ResponseHeader::success(mime);
|
||||
Self::new(header)
|
||||
}
|
||||
|
||||
|
@ -86,7 +86,7 @@ impl Response {
|
|||
self
|
||||
}
|
||||
|
||||
pub fn header(&self) -> &ResponseHeader {
|
||||
pub const fn header(&self) -> &ResponseHeader {
|
||||
&self.header
|
||||
}
|
||||
|
||||
|
|
|
@ -88,11 +88,11 @@ impl ResponseHeader {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn status(&self) -> &Status {
|
||||
pub const fn status(&self) -> &Status {
|
||||
&self.status
|
||||
}
|
||||
|
||||
pub fn meta(&self) -> &Meta {
|
||||
pub const fn meta(&self) -> &Meta {
|
||||
&self.meta
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,4 +1,3 @@
|
|||
|
||||
#[derive(Debug,Copy,Clone,PartialEq,Eq)]
|
||||
pub struct Status(u8);
|
||||
|
||||
|
@ -22,7 +21,7 @@ impl Status {
|
|||
pub const CERTIFICATE_NOT_AUTHORIZED: Self = Self(61);
|
||||
pub const CERTIFICATE_NOT_VALID: Self = Self(62);
|
||||
|
||||
pub fn code(&self) -> u8 {
|
||||
pub const fn code(&self) -> u8 {
|
||||
self.0
|
||||
}
|
||||
|
||||
|
@ -30,7 +29,7 @@ impl Status {
|
|||
self.category().is_success()
|
||||
}
|
||||
|
||||
pub fn category(&self) -> StatusCategory {
|
||||
pub const fn category(&self) -> StatusCategory {
|
||||
let class = self.0 / 10;
|
||||
|
||||
match class {
|
||||
|
|
Loading…
Reference in a new issue