Skip to content

Commit ce69208

Browse files
committed
improve parsing relative uri
1 parent f46dd1c commit ce69208

File tree

7 files changed

+199
-84
lines changed

7 files changed

+199
-84
lines changed

Cargo.lock

Lines changed: 37 additions & 26 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/advanced_request_get.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ fn main() {
2424
.parse();
2525

2626
// Connects to a server. Uses information from `addr`.
27-
let mut stream = Stream::new(&addr, Some(Duration::from_secs(60))).unwrap();
27+
let mut stream = Stream::connect(&addr, Some(Duration::from_secs(60))).unwrap();
2828
stream = Stream::try_to_https(stream, &addr, None).unwrap();
2929

3030
// Makes a request to server. Sends the prepared message.

src/chunked.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ fn is_ascii_space(b: u8) -> bool {
175175
}
176176
}
177177

178-
fn parse_hex_uint(data: Vec<u8>) -> Result<usize, &'static str> {
178+
fn parse_hex_uint<'a>(data: Vec<u8>) -> Result<usize, &'a str> {
179179
let mut n = 0usize;
180180
for (i, v) in data.iter().enumerate() {
181181
if i == 16 {

src/error.rs

Lines changed: 34 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,13 @@ impl fmt::Display for ParseErr {
2929
use self::ParseErr::*;
3030

3131
let err = match self {
32-
Utf8(_) => "invalid character",
33-
Int(_) => "cannot parse number",
34-
Invalid => "invalid value",
35-
Empty => "nothing to parse",
36-
StatusErr => "status line contains invalid values",
37-
HeadersErr => "headers contain invalid values",
38-
UriErr => "uri contains invalid characters",
32+
Utf8(_) => "Invalid character",
33+
Int(_) => "Cannot parse number",
34+
Invalid => "Invalid value",
35+
Empty => "Nothing to parse",
36+
StatusErr => "Status line contains invalid values",
37+
HeadersErr => "Headers contain invalid values",
38+
UriErr => "URI contains invalid characters",
3939
};
4040
write!(f, "ParseErr: {}", err)
4141
}
@@ -57,8 +57,9 @@ impl From<str::Utf8Error> for ParseErr {
5757
pub enum Error {
5858
IO(io::Error),
5959
Parse(ParseErr),
60-
Timeout(mpsc::RecvTimeoutError),
60+
Timeout,
6161
Tls,
62+
Thread,
6263
}
6364

6465
impl error::Error for Error {
@@ -68,8 +69,7 @@ impl error::Error for Error {
6869
match self {
6970
IO(e) => Some(e),
7071
Parse(e) => Some(e),
71-
Timeout(e) => Some(e),
72-
Tls => None,
72+
Timeout | Tls | Thread => None,
7373
}
7474
}
7575
}
@@ -81,27 +81,14 @@ impl fmt::Display for Error {
8181
let err = match self {
8282
IO(_) => "IO error",
8383
Parse(err) => return err.fmt(f),
84-
Timeout(_) => "Timeout error",
84+
Timeout => "Timeout error",
8585
Tls => "TLS error",
86+
Thread => "Thread communication error",
8687
};
8788
write!(f, "Error: {}", err)
8889
}
8990
}
9091

91-
#[cfg(feature = "native-tls")]
92-
impl From<native_tls::Error> for Error {
93-
fn from(_e: native_tls::Error) -> Self {
94-
Error::Tls
95-
}
96-
}
97-
98-
#[cfg(feature = "native-tls")]
99-
impl<T> From<native_tls::HandshakeError<T>> for Error {
100-
fn from(_e: native_tls::HandshakeError<T>) -> Self {
101-
Error::Tls
102-
}
103-
}
104-
10592
impl From<io::Error> for Error {
10693
fn from(e: io::Error) -> Self {
10794
Error::IO(e)
@@ -121,8 +108,8 @@ impl From<str::Utf8Error> for Error {
121108
}
122109

123110
impl From<mpsc::RecvTimeoutError> for Error {
124-
fn from(e: mpsc::RecvTimeoutError) -> Self {
125-
Error::Timeout(e)
111+
fn from(_e: mpsc::RecvTimeoutError) -> Self {
112+
Error::Timeout
126113
}
127114
}
128115

@@ -132,3 +119,23 @@ impl From<rustls::Error> for Error {
132119
Error::Tls
133120
}
134121
}
122+
123+
#[cfg(feature = "native-tls")]
124+
impl From<native_tls::Error> for Error {
125+
fn from(_e: native_tls::Error) -> Self {
126+
Error::Tls
127+
}
128+
}
129+
130+
#[cfg(feature = "native-tls")]
131+
impl<T> From<native_tls::HandshakeError<T>> for Error {
132+
fn from(_e: native_tls::HandshakeError<T>) -> Self {
133+
Error::Tls
134+
}
135+
}
136+
137+
impl<T> From<mpsc::SendError<T>> for Error {
138+
fn from(_e: mpsc::SendError<T>) -> Self {
139+
Error::Thread
140+
}
141+
}

src/request.rs

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ use std::{
1818

1919
const CR_LF: &str = "\r\n";
2020
const DEFAULT_REQ_TIMEOUT: u64 = 60 * 60;
21+
const DEFAULT_CALL_TIMEOUT: u64 = 60;
2122

2223
/// HTTP request methods
2324
#[derive(Debug, PartialEq, Clone, Copy)]
@@ -308,8 +309,8 @@ impl<'a> RequestMessage<'a> {
308309

309310
let mut request_msg = (request_line + &headers + CR_LF).as_bytes().to_vec();
310311

311-
if let Some(b) = &self.body {
312-
request_msg.extend(*b);
312+
if let Some(b) = self.body {
313+
request_msg.extend(b);
313314
}
314315

315316
request_msg
@@ -363,9 +364,9 @@ impl<'a> Request<'a> {
363364
Request {
364365
messsage: message,
365366
redirect_policy: RedirectPolicy::default(),
366-
connect_timeout: Some(Duration::from_secs(60)),
367-
read_timeout: Some(Duration::from_secs(60)),
368-
write_timeout: Some(Duration::from_secs(60)),
367+
connect_timeout: Some(Duration::from_secs(DEFAULT_CALL_TIMEOUT)),
368+
read_timeout: Some(Duration::from_secs(DEFAULT_CALL_TIMEOUT)),
369+
write_timeout: Some(Duration::from_secs(DEFAULT_CALL_TIMEOUT)),
369370
timeout: Duration::from_secs(DEFAULT_REQ_TIMEOUT),
370371
root_cert_file_pem: None,
371372
}
@@ -613,8 +614,11 @@ impl<'a> Request<'a> {
613614
/// let request = Request::new(&uri)
614615
/// .redirect_policy(RedirectPolicy::Limit(5));
615616
/// ```
616-
pub fn redirect_policy(&mut self, policy: RedirectPolicy<fn() -> bool>) -> &mut Self {
617-
self.redirect_policy = policy;
617+
pub fn redirect_policy<T>(&mut self, policy: T) -> &mut Self
618+
where
619+
RedirectPolicy<fn() -> bool>: From<T>,
620+
{
621+
self.redirect_policy = RedirectPolicy::from(policy);
618622
self
619623
}
620624

@@ -638,7 +642,7 @@ impl<'a> Request<'a> {
638642
T: Write,
639643
{
640644
// Set up a stream.
641-
let mut stream = Stream::new(self.messsage.uri, self.connect_timeout)?;
645+
let mut stream = Stream::connect(self.messsage.uri, self.connect_timeout)?;
642646
stream.set_read_timeout(self.read_timeout)?;
643647
stream.set_write_timeout(self.write_timeout)?;
644648
stream = Stream::try_to_https(stream, self.messsage.uri, self.root_cert_file_pem)?;
@@ -689,7 +693,7 @@ impl<'a> Request<'a> {
689693
}
690694

691695
let params = response.basic_info(&self.messsage.method).to_vec();
692-
sender_supp.send(params).unwrap();
696+
sender_supp.send(params)?;
693697

694698
// Receive and process `body` of the response.
695699
let content_len = response.content_len().unwrap_or(1);

0 commit comments

Comments
 (0)