From 6b6182e8c4c81f634becebe7b45dc21bff59a286 Mon Sep 17 00:00:00 2001 From: Mike Dilger Date: Tue, 1 Sep 2015 15:53:15 +1200 Subject: [PATCH] feat(server): Add Handler per-connection hooks --- src/server/mod.rs | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/src/server/mod.rs b/src/server/mod.rs index 2061dcc235..14fa36dac2 100644 --- a/src/server/mod.rs +++ b/src/server/mod.rs @@ -256,6 +256,8 @@ impl Worker { fn handle_connection(&self, mut stream: &mut S) where S: NetworkStream + Clone { debug!("Incoming stream"); + self.handler.on_connection_start(); + if let Err(e) = self.set_timeouts(stream) { error!("set_timeouts error: {:?}", e); return; @@ -275,6 +277,9 @@ impl Worker { let wrt = BufWriter::new(stream); self.keep_alive_loop(rdr, wrt, addr); + + self.handler.on_connection_end(); + debug!("keep_alive loop ending for {}", addr); } @@ -335,7 +340,6 @@ impl Worker { debug!("keep_alive = {:?} for {}", keep_alive, addr); } - } fn handle_expect(&self, req: &Request, wrt: &mut W) -> bool { @@ -401,6 +405,16 @@ pub trait Handler: Sync + Send { fn check_continue(&self, _: (&Method, &RequestUri, &Headers)) -> StatusCode { StatusCode::Continue } + + /// This is run after a connection is received, on a per-connection basis (not a + /// per-request basis, as a connection with keep-alive may handle multiple + /// requests) + fn on_connection_start(&self) { } + + /// This is run before a connection is closed, on a per-connection basis (not a + /// per-request basis, as a connection with keep-alive may handle multiple + /// requests) + fn on_connection_end(&self) { } } impl Handler for F where F: Fn(Request, Response), F: Sync + Send {