Skip to content

Commit 82ed909

Browse files
committed
feat(client): add url property Response
This will always be the last URL that was used by the Request, which is useful for determining what the final URL was after redirection. BREAKING CHANGE: Technically a break, since `Response::new()` takes an additional argument. In practice, the only place that should have been creating Responses directly is inside the Client, so it shouldn't break anyone. If you were creating Responses manually, you'll need to pass a Url argument.
1 parent 5978f7c commit 82ed909

File tree

2 files changed

+20
-9
lines changed

2 files changed

+20
-9
lines changed

src/client/request.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ impl Request<Streaming> {
110110
///
111111
/// Consumes the Request.
112112
pub fn send(self) -> ::Result<Response> {
113-
Response::with_message(self.message)
113+
Response::with_message(self.url, self.message)
114114
}
115115
}
116116

src/client/response.rs

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
//! Client Responses
22
use std::io::{self, Read};
33

4+
use url::Url;
5+
46
use header;
57
use net::NetworkStream;
68
use http::{self, RawStatus, ResponseHead, HttpMessage};
9+
use http::h1::Http11Message;
710
use status;
811
use version;
9-
use http::h1::Http11Message;
1012

1113
/// A response for a client request to a remote server.
1214
#[derive(Debug)]
@@ -17,6 +19,8 @@ pub struct Response {
1719
pub headers: header::Headers,
1820
/// The HTTP version of this response from the server.
1921
pub version: version::HttpVersion,
22+
/// The final URL of this response.
23+
pub url: Url,
2024
status_raw: RawStatus,
2125
message: Box<HttpMessage>,
2226
is_drained: bool,
@@ -25,13 +29,13 @@ pub struct Response {
2529
impl Response {
2630

2731
/// Creates a new response from a server.
28-
pub fn new(stream: Box<NetworkStream + Send>) -> ::Result<Response> {
32+
pub fn new(url: Url, stream: Box<NetworkStream + Send>) -> ::Result<Response> {
2933
trace!("Response::new");
30-
Response::with_message(Box::new(Http11Message::with_stream(stream)))
34+
Response::with_message(url, Box::new(Http11Message::with_stream(stream)))
3135
}
3236

3337
/// Creates a new response received from the server on the given `HttpMessage`.
34-
pub fn with_message(mut message: Box<HttpMessage>) -> ::Result<Response> {
38+
pub fn with_message(url: Url, mut message: Box<HttpMessage>) -> ::Result<Response> {
3539
trace!("Response::with_message");
3640
let ResponseHead { headers, raw_status, version } = try!(message.get_incoming());
3741
let status = status::StatusCode::from_u16(raw_status.0);
@@ -42,6 +46,7 @@ impl Response {
4246
status: status,
4347
version: version,
4448
headers: headers,
49+
url: url,
4550
message: message,
4651
status_raw: raw_status,
4752
is_drained: false,
@@ -89,6 +94,8 @@ impl Drop for Response {
8994
mod tests {
9095
use std::io::{self, Read};
9196

97+
use url::Url;
98+
9299
use header::TransferEncoding;
93100
use header::Encoding;
94101
use http::HttpMessage;
@@ -131,7 +138,8 @@ mod tests {
131138
\r\n"
132139
);
133140

134-
let res = Response::new(Box::new(stream)).unwrap();
141+
let url = Url::parse("http://hyper.rs").unwrap();
142+
let res = Response::new(url, Box::new(stream)).unwrap();
135143

136144
// The status line is correct?
137145
assert_eq!(res.status, status::StatusCode::Ok);
@@ -162,7 +170,8 @@ mod tests {
162170
\r\n"
163171
);
164172

165-
let res = Response::new(Box::new(stream)).unwrap();
173+
let url = Url::parse("http://hyper.rs").unwrap();
174+
let res = Response::new(url, Box::new(stream)).unwrap();
166175

167176
assert!(read_to_string(res).is_err());
168177
}
@@ -181,7 +190,8 @@ mod tests {
181190
\r\n"
182191
);
183192

184-
let res = Response::new(Box::new(stream)).unwrap();
193+
let url = Url::parse("http://hyper.rs").unwrap();
194+
let res = Response::new(url, Box::new(stream)).unwrap();
185195

186196
assert!(read_to_string(res).is_err());
187197
}
@@ -200,7 +210,8 @@ mod tests {
200210
\r\n"
201211
);
202212

203-
let res = Response::new(Box::new(stream)).unwrap();
213+
let url = Url::parse("http://hyper.rs").unwrap();
214+
let res = Response::new(url, Box::new(stream)).unwrap();
204215

205216
assert_eq!(read_to_string(res).unwrap(), "1".to_owned());
206217
}

0 commit comments

Comments
 (0)