Skip to content

Commit

Permalink
Add Response::remote_addr()
Browse files Browse the repository at this point in the history
  • Loading branch information
mvforell committed Mar 5, 2022
1 parent f549184 commit 74f6bb5
Showing 1 changed file with 11 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/response.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::io::{self, Read};
use std::net::SocketAddr;
use std::str::FromStr;
use std::{fmt, io::BufRead};

Expand Down Expand Up @@ -80,6 +81,8 @@ pub struct Response {
length: Option<usize>,
/// The compression type of the response body.
compression: Option<Compression>,
/// The remote address that sent this response.
remote_addr: Option<SocketAddr>,
}

/// index into status_line where we split: HTTP/1.1 200 OK
Expand Down Expand Up @@ -226,6 +229,11 @@ impl Response {
charset_from_content_type(self.header("content-type"))
}

/// The remote address that sent this response.
pub fn remote_addr(&self) -> Option<SocketAddr> {
self.remote_addr
}

/// Turn this response into a `impl Read` of the body.
///
/// 1. If `Transfer-Encoding: chunked`, the returned reader will unchunk it
Expand Down Expand Up @@ -468,6 +476,8 @@ impl Response {
///
/// assert_eq!(resp.status(), 401);
pub(crate) fn do_from_stream(stream: Stream, unit: Option<Unit>) -> Result<Response, Error> {
let remote_addr = stream.socket().and_then(|s| s.peer_addr().ok());

//
// HTTP/1.1 200 OK\r\n
let mut stream =
Expand Down Expand Up @@ -517,6 +527,7 @@ impl Response {
history: vec![],
length,
compression,
remote_addr,
})
}

Expand Down

0 comments on commit 74f6bb5

Please sign in to comment.