pub struct HtmlRewriter { /* private fields */ }Expand description
A trillium [Handler] that rewrites HTML response bodies with
lol-html, using lol-async.
It wraps the response produced by other handlers: in before_send it
inspects the outgoing Content-Type and, if the mime subtype is html (e.g. text/html),
replaces the response body with a streaming rewrite driven by the Settings returned from the
closure passed to HtmlRewriter::new. Responses with any other content type (or none) are
passed through unchanged.
Implementations§
Source§impl HtmlRewriter
impl HtmlRewriter
Sourcepub fn new(
f: impl Fn() -> Settings<'static, 'static> + Send + Sync + 'static,
) -> Self
pub fn new( f: impl Fn() -> Settings<'static, 'static> + Send + Sync + 'static, ) -> Self
Construct a new html rewriter from a closure that builds Settings.
The closure — rather than a Settings value — is required because lol-html’s content
handlers are single-use; it is invoked once per rewritten response to produce a fresh set of
handlers. Build the settings with [Settings::new_send()] as the base (its handlers are
Send, as required here) and populate element_content_handlers /
document_content_handlers. See [lol_async::html::Settings] and the
lol-html docs for the full rewriting API.
Trait Implementations§
Source§impl Debug for HtmlRewriter
impl Debug for HtmlRewriter
Source§impl Handler for HtmlRewriter
impl Handler for HtmlRewriter
Source§async fn before_send(&self, conn: Conn) -> Conn
async fn before_send(&self, conn: Conn) -> Conn
§fn run(&self, conn: Conn) -> impl Future<Output = Conn> + Send
fn run(&self, conn: Conn) -> impl Future<Output = Conn> + Send
§fn init(&mut self, info: &mut Info) -> impl Future<Output = ()> + Send
fn init(&mut self, info: &mut Info) -> impl Future<Output = ()> + Send
§fn has_upgrade(&self, upgrade: &Upgrade) -> bool
fn has_upgrade(&self, upgrade: &Upgrade) -> bool
Handler::upgrade]. The first handler that responds true to this will receive
ownership of the [trillium::Upgrade][crate::Upgrade] in a subsequent call to
[Handler::upgrade]§fn upgrade(&self, upgrade: Upgrade) -> impl Future<Output = ()> + Send
fn upgrade(&self, upgrade: Upgrade) -> impl Future<Output = ()> + Send
Handler::has_upgrade] and will
only be called once for this upgrade. There is no return value, and this function takes
exclusive ownership of the underlying transport once this is called. You can downcast
the transport to whatever the source transport type is and perform any non-http protocol
communication that has been negotiated. You probably don’t want this unless you’re
implementing something like websockets. Please note that for many transports such as
TcpStreams, dropping the transport (and therefore the Upgrade) will hang up /
disconnect.