feat(server): add Transport to on_request
This commit is contained in:
@@ -28,9 +28,9 @@ impl<H: Handler<T>, T: Transport> Message<H, T> {
|
||||
impl<H: Handler<T>, T: Transport> http::MessageHandler<T> for Message<H, T> {
|
||||
type Message = http::ServerMessage;
|
||||
|
||||
fn on_incoming(&mut self, head: http::RequestHead) -> Next {
|
||||
fn on_incoming(&mut self, head: http::RequestHead, transport: &T) -> Next {
|
||||
trace!("on_incoming {:?}", head);
|
||||
let req = request::new(head);
|
||||
let req = request::new(head, transport);
|
||||
self.handler.on_request(req)
|
||||
}
|
||||
|
||||
|
||||
@@ -324,7 +324,7 @@ impl Listening {
|
||||
/// Each event handler returns it's desired `Next` action.
|
||||
pub trait Handler<T: Transport> {
|
||||
/// This event occurs first, triggering when a `Request` has been parsed.
|
||||
fn on_request(&mut self, request: Request) -> Next;
|
||||
fn on_request(&mut self, request: Request<T>) -> Next;
|
||||
/// This event occurs each time the `Request` is ready to be read from.
|
||||
fn on_request_readable(&mut self, request: &mut http::Decoder<T>) -> Next;
|
||||
/// This event occurs after the first time this handled signals `Next::write()`.
|
||||
|
||||
@@ -3,13 +3,15 @@
|
||||
//! These are requests that a `hyper::Server` receives, and include its method,
|
||||
//! target URI, headers, and message body.
|
||||
|
||||
use std::fmt;
|
||||
|
||||
use version::HttpVersion;
|
||||
use method::Method;
|
||||
use header::Headers;
|
||||
use http::{RequestHead, MessageHead, RequestLine};
|
||||
use uri::RequestUri;
|
||||
|
||||
pub fn new(incoming: RequestHead) -> Request {
|
||||
pub fn new<'a, T>(incoming: RequestHead, transport: &'a T) -> Request<'a, T> {
|
||||
let MessageHead { version, subject: RequestLine(method, uri), headers } = incoming;
|
||||
debug!("Request Line: {:?} {:?} {:?}", method, uri, version);
|
||||
debug!("{:#?}", headers);
|
||||
@@ -19,22 +21,31 @@ pub fn new(incoming: RequestHead) -> Request {
|
||||
uri: uri,
|
||||
headers: headers,
|
||||
version: version,
|
||||
transport: transport,
|
||||
}
|
||||
}
|
||||
|
||||
/// A request bundles several parts of an incoming `NetworkStream`, given to a `Handler`.
|
||||
#[derive(Debug)]
|
||||
pub struct Request {
|
||||
// The IP address of the remote connection.
|
||||
//remote_addr: SocketAddr,
|
||||
pub struct Request<'a, T: 'a> {
|
||||
method: Method,
|
||||
headers: Headers,
|
||||
uri: RequestUri,
|
||||
version: HttpVersion,
|
||||
headers: Headers,
|
||||
transport: &'a T,
|
||||
}
|
||||
|
||||
impl<'a, T> fmt::Debug for Request<'a, T> {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
f.debug_struct("Request")
|
||||
.field("method", &self.method)
|
||||
.field("uri", &self.uri)
|
||||
.field("version", &self.version)
|
||||
.field("headers", &self.headers)
|
||||
.finish()
|
||||
}
|
||||
}
|
||||
|
||||
impl Request {
|
||||
impl<'a, T> Request<'a, T> {
|
||||
/// The `Method`, such as `Get`, `Post`, etc.
|
||||
#[inline]
|
||||
pub fn method(&self) -> &Method { &self.method }
|
||||
@@ -43,6 +54,10 @@ impl Request {
|
||||
#[inline]
|
||||
pub fn headers(&self) -> &Headers { &self.headers }
|
||||
|
||||
/// The underlying `Transport` of this request.
|
||||
#[inline]
|
||||
pub fn transport(&self) -> &'a T { self.transport }
|
||||
|
||||
/// The target request-uri for this request.
|
||||
#[inline]
|
||||
pub fn uri(&self) -> &RequestUri { &self.uri }
|
||||
|
||||
Reference in New Issue
Block a user