Avoid tuple to describe response status line

This commit is contained in:
Martin Algesten
2019-10-20 12:25:54 +02:00
parent d8f8220c3c
commit 27be8117b8

View File

@@ -40,13 +40,20 @@ pub struct Response {
url: Option<String>, url: Option<String>,
error: Option<Error>, error: Option<Error>,
status_line: String, status_line: String,
index: (usize, usize), // index into status_line where we split: HTTP/1.1 200 OK index: ResponseStatusIndex,
status: u16, status: u16,
headers: Vec<Header>, headers: Vec<Header>,
unit: Option<Unit>, unit: Option<Unit>,
stream: Option<Stream>, stream: Option<Stream>,
} }
/// index into status_line where we split: HTTP/1.1 200 OK
#[derive(Debug, Clone, Copy, Eq, PartialEq)]
struct ResponseStatusIndex {
http_version: usize,
response_code: usize,
}
impl ::std::fmt::Debug for Response { impl ::std::fmt::Debug for Response {
fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::result::Result<(), ::std::fmt::Error> { fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::result::Result<(), ::std::fmt::Error> {
write!( write!(
@@ -90,7 +97,7 @@ impl Response {
/// The http version: `HTTP/1.1` /// The http version: `HTTP/1.1`
pub fn http_version(&self) -> &str { pub fn http_version(&self) -> &str {
&self.status_line.as_str()[0..self.index.0] &self.status_line.as_str()[0..self.index.http_version]
} }
/// The status as a u16: `200` /// The status as a u16: `200`
@@ -100,7 +107,7 @@ impl Response {
/// The status text: `OK` /// The status text: `OK`
pub fn status_text(&self) -> &str { pub fn status_text(&self) -> &str {
&self.status_line.as_str()[self.index.1 + 1..].trim() &self.status_line.as_str()[self.index.response_code + 1..].trim()
} }
/// The header corresponding header value for the give name, if any. /// The header corresponding header value for the give name, if any.
@@ -445,7 +452,7 @@ impl Response {
} }
/// parse a line like: HTTP/1.1 200 OK\r\n /// parse a line like: HTTP/1.1 200 OK\r\n
fn parse_status_line(line: &str) -> Result<((usize, usize), u16), Error> { fn parse_status_line(line: &str) -> Result<(ResponseStatusIndex, u16), Error> {
// //
let mut split = line.splitn(3, ' '); let mut split = line.splitn(3, ' ');
@@ -464,7 +471,13 @@ fn parse_status_line(line: &str) -> Result<((usize, usize), u16), Error> {
let status = status.parse::<u16>().map_err(|_| Error::BadStatus)?; let status = status.parse::<u16>().map_err(|_| Error::BadStatus)?;
Ok(((index1, index2), status)) Ok((
ResponseStatusIndex {
http_version: index1,
response_code: index2,
},
status,
))
} }
impl FromStr for Response { impl FromStr for Response {