Skip to main content

Mount

Struct Mount 

Source
pub struct Mount { /* private fields */ }
Expand description

Any tower service under a path prefix, inside the app’s pipeline.

A module declares it with m.mount(..). It answers the prefix itself and every path below it, for every method unless methods names some, behind the pipeline a route has: request id, request context and client address, the app’s global layers (session and CSRF among them), authentication, then on every request the mount’s access check, guards, throttle, timeout and body limit. One of public, authenticated, roles, permissions or can is required, as for a route: a mount without an access declaration fails the boot, and so does one whose prefix is malformed, starts with /_ (the framework’s own paths), or overlaps a route or another mount.

The service sees the request as the app received it but for its URI: scheme and authority as received, and for the path / followed by the rest of the path below the prefix as written (/ for the prefix itself), then ? and the query as written. Nothing is decoded, so %2F, %3F and %23 reach the service as they were sent. Before that the router has refused a . or .. segment (raw, encoded, or once the rest of the path is decoded), a backslash, a NUL and bytes that are not UTF-8; the mount refuses an empty segment below the prefix (//, also once %2F is read as /) with 400. /khata/ and the prefix in another case are not found. A service that decodes its own path still treats it as untrusted input.

A request with an Upgrade header (a WebSocket or h2c handshake) or the method CONNECT never reaches the service: a mounted service answers requests, it does not take over the connection. One that reaches the mount is answered 501 after the access check. With methods, a method the list leaves out, CONNECT among them, is the router’s 405 before the access check, as for a route; HEAD runs the GET answer when GET is listed, and OPTIONS is a 204 with Allow.

The body is cut off at the mount’s limit also when it comes without a length. A panic in the service answers 500, as in a handler. An error of the service answers 500 with the error as its source, which the app’s error mappings (m.map_error) see and the log records; one that comes from the body’s limit or the server’s read timeout answers 413 or 408, as the body extractors do. A response below 500 the service writes goes out as written, a 4xx included: its status, headers and body are the service’s (a 422 with a plain-text body stays a 422 with that body). A 5xx with no body or a short plain-text one (of known length, up to 16 KiB) takes the app’s JSON error shape, its text logged, never sent; any other 5xx body (JSON, a longer or streamed text) goes out as written. The pipeline adds X-Request-Id, and the default security headers where the service set none.

The service is built in register; it reaches the app’s providers through the AppContext in each request’s extensions.

use std::time::Duration;

use navree_http::extract::Request;
use navree_http::{AppRouter, ModuleBuilderMountExt, Mount};
use navree_kernel::{Module, ModuleBuilder};

struct LedgerModule;

impl Module for LedgerModule {
    fn register(&self, m: &mut ModuleBuilder) {
        // The service of another crate; here, one that answers every path.
        let khata = AppRouter::from_handler(|request: Request| async move {
            format!("the khata at {}", request.uri().path())
        });
        m.mount(
            Mount::new("/khata", khata)
                .authenticated()
                .throttle(60, Duration::from_secs(60)),
        );
    }
}

Implementations§

Source§

impl Mount

Source

pub fn new<S, B>(prefix: &str, service: S) -> Mount
where S: Service<Request, Response = Response<B>> + Clone + Send + Sync + 'static, S::Error: Into<BoxError>, S::Future: Send + 'static, B: HttpBody<Data = Bytes> + Send + 'static, B::Error: Into<BoxError>,

service at prefix (/khata, absolute: not under http.api_prefix): it sees requests for prefix and prefix/... with the prefix taken off the path, raw (/khata/v1/items%3Fx arrives as /v1/items%3Fx). Its access is declared next.

Source

pub fn public(self) -> Mount

No authentication or authorization: every request reaches the service.

Source

pub fn authenticated(self) -> Mount

Any signed-in user may call the mount (no further authorization).

Source

pub fn roles(self, roles: &[&str]) -> Mount

Requires any of the roles (checked by the RouteAuthorizer on every request).

Source

pub fn permissions(self, permissions: &[&str]) -> Mount

Requires the permissions (checked by the RouteAuthorizer on every request).

Source

pub fn can(self, action: &str, subject: &str) -> Mount

Requires the ability action on the type subject (checked by the RouteAuthorizer on every request). A mount has no path parameter, so no record is named.

Source

pub fn guard(self, guard: Arc<dyn Guard>) -> Mount

Adds a guard, run on every request before authorization and the service.

Source

pub fn throttle(self, limit: u32, per: Duration) -> Mount

At most limit requests per per, per user (per client IP for guests), counted for the whole mount: every path and method under the prefix takes from one budget.

Source

pub fn timeout(self, d: Duration) -> Mount

Overrides the app-wide request timeout (shorter or longer).

Source

pub fn body_limit(self, bytes: usize) -> Mount

Overrides the app-wide request body limit: a request that declares a longer body is refused before the service is called, and a body sent without a length ends in an error once it passes the limit.

Source

pub fn name(self, name: &str) -> Mount

The mount’s name, for logs and the routes command.

Source

pub fn methods(self, methods: &[Method]) -> Mount

Only these methods (default: every method, extension methods such as PROPFIND included; an empty list fails the boot). A method named twice is kept once. With a list, HEAD runs the GET answer without its body and OPTIONS lists the methods, as for a route.

Trait Implementations§

Source§

impl Clone for Mount

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Mount

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl !RefUnwindSafe for Mount

§

impl !UnwindSafe for Mount

§

impl Forget for Mount

§

impl Freeze for Mount

§

impl Move for Mount

§

impl Send for Mount

§

impl Sync for Mount

§

impl Unpin for Mount

§

impl UnsafeUnpin for Mount

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self> ⓘ

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self> ⓘ

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> ⓘ
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self> ⓘ

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more