diff --git a/src/server/mod.rs b/src/server/mod.rs index eb7b3af872..79e5943543 100644 --- a/src/server/mod.rs +++ b/src/server/mod.rs @@ -265,20 +265,32 @@ impl Future for __ProtoBindTransport { } } -struct HttpService { - inner: T, - remote_addr: SocketAddr, +impl From> for Request { + fn from(message: Message<__ProtoRequest, http::TokioBody>) -> Request { + let (head, body) = match message { + Message::WithoutBody(head) => (head.0, http::Body::empty()), + Message::WithBody(head, body) => (head.0, body.into()), + }; + request::new(None, head, body) + } } -fn map_response_to_message(res: Response) -> Message<__ProtoResponse, B> { - let (head, body) = response::split(res); - if let Some(body) = body { - Message::WithBody(__ProtoResponse(head), body.into()) - } else { - Message::WithoutBody(__ProtoResponse(head)) +impl Into> for Response { + fn into(self) -> Message<__ProtoResponse, B> { + let (head, body) = response::split(self); + if let Some(body) = body { + Message::WithBody(__ProtoResponse(head), body.into()) + } else { + Message::WithoutBody(__ProtoResponse(head)) + } } } +struct HttpService { + inner: T, + remote_addr: SocketAddr, +} + type ResponseHead = http::MessageHead<::StatusCode>; impl Service for HttpService @@ -296,8 +308,8 @@ impl Service for HttpService Message::WithoutBody(head) => (head.0, http::Body::empty()), Message::WithBody(head, body) => (head.0, body.into()), }; - let req = request::new(self.remote_addr, head, body); - self.inner.call(req).map(map_response_to_message) + let req = request::new(Some(self.remote_addr), head, body); + self.inner.call(req).map(Into::into) } } diff --git a/src/server/request.rs b/src/server/request.rs index 76840478bc..c998097078 100644 --- a/src/server/request.rs +++ b/src/server/request.rs @@ -18,7 +18,7 @@ pub struct Request { uri: Uri, version: HttpVersion, headers: Headers, - remote_addr: SocketAddr, + remote_addr: Option, body: Body, } @@ -40,8 +40,11 @@ impl Request { pub fn version(&self) -> &HttpVersion { &self.version } /// The remote socket address of this request + /// + /// This is an `Option`, because some underlying transports may not have + /// a socket address, such as Unix Sockets. #[inline] - pub fn remote_addr(&self) -> &SocketAddr { &self.remote_addr } + pub fn remote_addr(&self) -> Option<&SocketAddr> { self.remote_addr.as_ref() } /// The target path of this Request. #[inline] @@ -82,9 +85,9 @@ impl fmt::Debug for Request { } } -pub fn new(addr: SocketAddr, incoming: RequestHead, body: Body) -> Request { +pub fn new(addr: Option, incoming: RequestHead, body: Body) -> Request { let MessageHead { version, subject: RequestLine(method, uri), headers } = incoming; - debug!("Request::new: addr={}, req=\"{} {} {}\"", addr, method, uri, version); + debug!("Request::new: addr={}, req=\"{} {} {}\"", MaybeAddr(&addr), method, uri, version); debug!("Request::new: headers={:?}", headers); Request { @@ -96,3 +99,15 @@ pub fn new(addr: SocketAddr, incoming: RequestHead, body: Body) -> Request { body: body, } } + +struct MaybeAddr<'a>(&'a Option); + +impl<'a> fmt::Display for MaybeAddr<'a> { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + match *self.0 { + Some(ref addr) => fmt::Display::fmt(addr, f), + None => f.write_str("None"), + } + } +} +