Skip to content

Commit

Permalink
improve parsing relative uri
Browse files Browse the repository at this point in the history
  • Loading branch information
jayjamesjay committed Aug 15, 2024
1 parent f46dd1c commit ce69208
Show file tree
Hide file tree
Showing 7 changed files with 199 additions and 84 deletions.
63 changes: 37 additions & 26 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion examples/advanced_request_get.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ fn main() {
.parse();

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

// Makes a request to server. Sends the prepared message.
Expand Down
2 changes: 1 addition & 1 deletion src/chunked.rs
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ fn is_ascii_space(b: u8) -> bool {
}
}

fn parse_hex_uint(data: Vec<u8>) -> Result<usize, &'static str> {
fn parse_hex_uint<'a>(data: Vec<u8>) -> Result<usize, &'a str> {
let mut n = 0usize;
for (i, v) in data.iter().enumerate() {
if i == 16 {
Expand Down
61 changes: 34 additions & 27 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,13 @@ impl fmt::Display for ParseErr {
use self::ParseErr::*;

let err = match self {
Utf8(_) => "invalid character",
Int(_) => "cannot parse number",
Invalid => "invalid value",
Empty => "nothing to parse",
StatusErr => "status line contains invalid values",
HeadersErr => "headers contain invalid values",
UriErr => "uri contains invalid characters",
Utf8(_) => "Invalid character",
Int(_) => "Cannot parse number",
Invalid => "Invalid value",
Empty => "Nothing to parse",
StatusErr => "Status line contains invalid values",
HeadersErr => "Headers contain invalid values",
UriErr => "URI contains invalid characters",
};
write!(f, "ParseErr: {}", err)
}
Expand All @@ -57,8 +57,9 @@ impl From<str::Utf8Error> for ParseErr {
pub enum Error {
IO(io::Error),
Parse(ParseErr),
Timeout(mpsc::RecvTimeoutError),
Timeout,
Tls,
Thread,
}

impl error::Error for Error {
Expand All @@ -68,8 +69,7 @@ impl error::Error for Error {
match self {
IO(e) => Some(e),
Parse(e) => Some(e),
Timeout(e) => Some(e),
Tls => None,
Timeout | Tls | Thread => None,
}
}
}
Expand All @@ -81,27 +81,14 @@ impl fmt::Display for Error {
let err = match self {
IO(_) => "IO error",
Parse(err) => return err.fmt(f),
Timeout(_) => "Timeout error",
Timeout => "Timeout error",
Tls => "TLS error",
Thread => "Thread communication error",
};
write!(f, "Error: {}", err)
}
}

#[cfg(feature = "native-tls")]
impl From<native_tls::Error> for Error {
fn from(_e: native_tls::Error) -> Self {
Error::Tls
}
}

#[cfg(feature = "native-tls")]
impl<T> From<native_tls::HandshakeError<T>> for Error {
fn from(_e: native_tls::HandshakeError<T>) -> Self {
Error::Tls
}
}

impl From<io::Error> for Error {
fn from(e: io::Error) -> Self {
Error::IO(e)
Expand All @@ -121,8 +108,8 @@ impl From<str::Utf8Error> for Error {
}

impl From<mpsc::RecvTimeoutError> for Error {
fn from(e: mpsc::RecvTimeoutError) -> Self {
Error::Timeout(e)
fn from(_e: mpsc::RecvTimeoutError) -> Self {
Error::Timeout
}
}

Expand All @@ -132,3 +119,23 @@ impl From<rustls::Error> for Error {
Error::Tls
}
}

#[cfg(feature = "native-tls")]
impl From<native_tls::Error> for Error {
fn from(_e: native_tls::Error) -> Self {
Error::Tls
}
}

#[cfg(feature = "native-tls")]
impl<T> From<native_tls::HandshakeError<T>> for Error {
fn from(_e: native_tls::HandshakeError<T>) -> Self {
Error::Tls
}
}

impl<T> From<mpsc::SendError<T>> for Error {
fn from(_e: mpsc::SendError<T>) -> Self {
Error::Thread
}
}
22 changes: 13 additions & 9 deletions src/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ use std::{

const CR_LF: &str = "\r\n";
const DEFAULT_REQ_TIMEOUT: u64 = 60 * 60;
const DEFAULT_CALL_TIMEOUT: u64 = 60;

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

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

if let Some(b) = &self.body {
request_msg.extend(*b);
if let Some(b) = self.body {
request_msg.extend(b);
}

request_msg
Expand Down Expand Up @@ -363,9 +364,9 @@ impl<'a> Request<'a> {
Request {
messsage: message,
redirect_policy: RedirectPolicy::default(),
connect_timeout: Some(Duration::from_secs(60)),
read_timeout: Some(Duration::from_secs(60)),
write_timeout: Some(Duration::from_secs(60)),
connect_timeout: Some(Duration::from_secs(DEFAULT_CALL_TIMEOUT)),
read_timeout: Some(Duration::from_secs(DEFAULT_CALL_TIMEOUT)),
write_timeout: Some(Duration::from_secs(DEFAULT_CALL_TIMEOUT)),
timeout: Duration::from_secs(DEFAULT_REQ_TIMEOUT),
root_cert_file_pem: None,
}
Expand Down Expand Up @@ -613,8 +614,11 @@ impl<'a> Request<'a> {
/// let request = Request::new(&uri)
/// .redirect_policy(RedirectPolicy::Limit(5));
/// ```
pub fn redirect_policy(&mut self, policy: RedirectPolicy<fn() -> bool>) -> &mut Self {
self.redirect_policy = policy;
pub fn redirect_policy<T>(&mut self, policy: T) -> &mut Self
where
RedirectPolicy<fn() -> bool>: From<T>,
{
self.redirect_policy = RedirectPolicy::from(policy);
self
}

Expand All @@ -638,7 +642,7 @@ impl<'a> Request<'a> {
T: Write,
{
// Set up a stream.
let mut stream = Stream::new(self.messsage.uri, self.connect_timeout)?;
let mut stream = Stream::connect(self.messsage.uri, self.connect_timeout)?;
stream.set_read_timeout(self.read_timeout)?;
stream.set_write_timeout(self.write_timeout)?;
stream = Stream::try_to_https(stream, self.messsage.uri, self.root_cert_file_pem)?;
Expand Down Expand Up @@ -689,7 +693,7 @@ impl<'a> Request<'a> {
}

let params = response.basic_info(&self.messsage.method).to_vec();
sender_supp.send(params).unwrap();
sender_supp.send(params)?;

// Receive and process `body` of the response.
let content_len = response.content_len().unwrap_or(1);
Expand Down
Loading

0 comments on commit ce69208

Please sign in to comment.