pub struct Sites<G: Gate = Open> { /* private fields */ }Expand description
A table of applications, chosen per request by host and base path: an application that lists the request’s host (or an alias of it) wins over one that answers any host, and among those, the longest base path that holds the request’s path. A request no application claims goes to the first one added, which answers it by its own rules (421 for a host it does not serve, 404 outside its base).
use web_net::{App, Sites, get};
let blog = App::builder()
.host("example.com")
.base("/blog")
.route(get("/", |cx| { cx.text(200, "blog"); Ok(()) }).public())
.build()?;
let shop = App::builder()
.host("shop.example.com")
.route(get("/", |cx| { cx.text(200, "shop"); Ok(()) }).public())
.build()?;
let main = App::builder()
.host("example.com")
.route(get("/", |cx| { cx.text(200, "home"); Ok(()) }).public())
.build()?;
let sites = Sites::new().site(main).site(blog).site(shop);
use serve_net::testing::respond;
assert_eq!(respond(&sites, b"GET /blog/ HTTP/1.1\r\nHost: example.com\r\n\r\n").text(), "blog");
assert_eq!(respond(&sites, b"GET / HTTP/1.1\r\nHost: example.com\r\n\r\n").text(), "home");
assert_eq!(respond(&sites, b"GET / HTTP/1.1\r\nHost: shop.example.com\r\n\r\n").text(), "shop");
assert_eq!(respond(&sites, b"GET / HTTP/1.1\r\nHost: other.example\r\n\r\n").status, 421);Implementations§
Source§impl<G: Gate> Sites<G>
impl<G: Gate> Sites<G>
Sourcepub fn site(self, app: App<G>) -> Self
pub fn site(self, app: App<G>) -> Self
Adds an application. The first one added answers what no other claims.
Sourcepub fn pick(&self, req: &HttpRequest<'_>) -> Option<&App<G>>
pub fn pick(&self, req: &HttpRequest<'_>) -> Option<&App<G>>
The application for a request, if the table has any.
Sourcepub fn serve(
&self,
req: &HttpRequest<'_>,
env: &Envelope,
who: Option<&G::Identity>,
reply: &mut HttpReply,
) -> Served
pub fn serve( &self, req: &HttpRequest<'_>, env: &Envelope, who: Option<&G::Identity>, reply: &mut HttpReply, ) -> Served
As App::serve on the chosen application.
Trait Implementations§
Source§impl<G: Gate> HttpService for Sites<G>
impl<G: Gate> HttpService for Sites<G>
Source§fn call(
&self,
request: &HttpRequest<'_>,
env: &Envelope,
reply: &mut HttpReply,
) -> Outcome
fn call( &self, request: &HttpRequest<'_>, env: &Envelope, reply: &mut HttpReply, ) -> Outcome
The request is complete.
Source§fn head(&self, head: &HttpHead<'_>, env: &mut Envelope) -> HeadAction
fn head(&self, head: &HttpHead<'_>, env: &mut Envelope) -> HeadAction
The head is complete; before admission and before any body byte.
Source§fn upgraded(&self, env: &Envelope, conn: UpgradedConn)
fn upgraded(&self, env: &Envelope, conn: UpgradedConn)
After the 101 for an upgrade this service asked for in
head left,
and after done for that request. The conversation itself belongs to
the upgrade target (a WebSocketService, feature ws).Auto Trait Implementations§
impl<G = Open> !RefUnwindSafe for Sites<G>
impl<G = Open> !UnwindSafe for Sites<G>
impl<G> Forget for Sites<G>
impl<G> Freeze for Sites<G>
impl<G> Send for Sites<G>
impl<G> Sync for Sites<G>
impl<G> Unpin for Sites<G>
impl<G> UnsafeUnpin for Sites<G>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<S> Service<HttpFamily> for Swhere
S: HttpService,
impl<S> Service<HttpFamily> for Swhere
S: HttpService,
Source§fn head(&self, head: &HttpHead<'_>, env: &mut Envelope) -> HeadAction
fn head(&self, head: &HttpHead<'_>, env: &mut Envelope) -> HeadAction
The head is complete; before admission and any body byte.
Source§fn call(
&self,
request: &HttpRequest<'_>,
env: &Envelope,
reply: &mut Reply<HttpFamily>,
) -> Outcome
fn call( &self, request: &HttpRequest<'_>, env: &Envelope, reply: &mut Reply<HttpFamily>, ) -> Outcome
The request is complete.
Source§fn upgraded(&self, env: &Envelope, conn: UpgradedConn)
fn upgraded(&self, env: &Envelope, conn: UpgradedConn)
After a switch response the service asked for left.