Skip to content

Commit

Permalink
feat(server): Add Handler per-connection hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
mikedilger committed Sep 1, 2015
1 parent 8dbc38c commit 6b6182e
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 additions & 1 deletion src/server/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,8 @@ impl<H: Handler + 'static> Worker<H> {
fn handle_connection<S>(&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;
Expand All @@ -275,6 +277,9 @@ impl<H: Handler + 'static> Worker<H> {
let wrt = BufWriter::new(stream);

self.keep_alive_loop(rdr, wrt, addr);

self.handler.on_connection_end();

debug!("keep_alive loop ending for {}", addr);
}

Expand Down Expand Up @@ -335,7 +340,6 @@ impl<H: Handler + 'static> Worker<H> {

debug!("keep_alive = {:?} for {}", keep_alive, addr);
}

}

fn handle_expect<W: Write>(&self, req: &Request, wrt: &mut W) -> bool {
Expand Down Expand Up @@ -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<F> Handler for F where F: Fn(Request, Response<Fresh>), F: Sync + Send {
Expand Down

0 comments on commit 6b6182e

Please sign in to comment.