Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add error handling to Request methods #308

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/hyperium_http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,13 +90,13 @@ fn from_url_to_uri(url: &Url) -> http::Uri {
}

impl TryFrom<http::Request<Body>> for Request {
type Error = crate::url::ParseError;
type Error = crate::Error;

fn try_from(req: http::Request<Body>) -> Result<Self, Self::Error> {
let (parts, body) = req.into_parts();
let method = parts.method.into();
let url = from_uri_to_url(parts.uri)?;
let mut req = Request::new(method, url);
let mut req = Request::new(method, url)?;
req.set_body(body);
req.set_version(Some(parts.version.into()));
hyperium_headers_to_headers(parts.headers, req.as_mut());
Expand Down
6 changes: 3 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@
//! # Example
//!
//! ```
//! # fn main() -> Result<(), http_types::url::ParseError> {
//! # fn main() -> Result<(), http_types::Error> {
//! #
//! use http_types::{Url, Method, Request, Response, StatusCode};
//! use http_types::{Method, Request, Response, StatusCode};
//!
//! let mut req = Request::new(Method::Get, Url::parse("https://example.com")?);
//! let mut req = Request::new(Method::Get, "https://example.com")?;
//! req.set_body("Hello, Nori!");
//!
//! let mut res = Response::new(StatusCode::Ok);
Expand Down
23 changes: 11 additions & 12 deletions src/proxies/forwarded.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ impl<'a> Forwarded<'a> {
/// ```rust
/// # use http_types::{proxies::Forwarded, Method::Get, Request, Url, Result};
/// # fn main() -> Result<()> {
/// let mut request = Request::new(Get, Url::parse("http://_/")?);
/// let mut request = Request::new(Get, "http://_/")?;
/// request.insert_header(
/// "Forwarded",
/// r#"for=192.0.2.43, for="[2001:db8:cafe::17]", for=unknown;proto=https"#
Expand All @@ -58,7 +58,7 @@ impl<'a> Forwarded<'a> {
/// ```rust
/// # use http_types::{proxies::Forwarded, Method::Get, Request, Url, Result};
/// # fn main() -> Result<()> {
/// let mut request = Request::new(Get, Url::parse("http://_/")?);
/// let mut request = Request::new(Get, "http://_/")?;
/// request.insert_header("X-Forwarded-For", "192.0.2.43, 2001:db8:cafe::17, unknown");
/// request.insert_header("X-Forwarded-Proto", "https");
/// let forwarded = Forwarded::from_headers(&request)?.unwrap();
Expand All @@ -85,7 +85,7 @@ impl<'a> Forwarded<'a> {
/// ```rust
/// # use http_types::{proxies::Forwarded, Method::Get, Request, Url, Result};
/// # fn main() -> Result<()> {
/// let mut request = Request::new(Get, Url::parse("http://_/")?);
/// let mut request = Request::new(Get, "http://_/")?;
/// request.insert_header(
/// "Forwarded",
/// r#"for=192.0.2.43, for="[2001:db8:cafe::17]", for=unknown;proto=https"#
Expand All @@ -98,7 +98,7 @@ impl<'a> Forwarded<'a> {
/// ```rust
/// # use http_types::{proxies::Forwarded, Method::Get, Request, Url, Result};
/// # fn main() -> Result<()> {
/// let mut request = Request::new(Get, Url::parse("http://_/")?);
/// let mut request = Request::new(Get, "http://_/")?;
/// request.insert_header("X-Forwarded-For", "192.0.2.43, 2001:db8:cafe::17");
/// assert!(Forwarded::from_forwarded_header(&request)?.is_none());
/// # Ok(()) }
Expand All @@ -121,7 +121,7 @@ impl<'a> Forwarded<'a> {
/// ```rust
/// # use http_types::{proxies::Forwarded, Method::Get, Request, Url, Result};
/// # fn main() -> Result<()> {
/// let mut request = Request::new(Get, Url::parse("http://_/")?);
/// let mut request = Request::new(Get, "http://_/")?;
/// request.insert_header("X-Forwarded-For", "192.0.2.43, 2001:db8:cafe::17");
/// let forwarded = Forwarded::from_headers(&request)?.unwrap();
/// assert_eq!(forwarded.forwarded_for(), vec!["192.0.2.43", "[2001:db8:cafe::17]"]);
Expand All @@ -130,7 +130,7 @@ impl<'a> Forwarded<'a> {
/// ```rust
/// # use http_types::{proxies::Forwarded, Method::Get, Request, Url, Result};
/// # fn main() -> Result<()> {
/// let mut request = Request::new(Get, Url::parse("http://_/")?);
/// let mut request = Request::new(Get, "http://_/")?;
/// request.insert_header(
/// "Forwarded",
/// r#"for=192.0.2.43, for="[2001:db8:cafe::17]", for=unknown;proto=https"#
Expand Down Expand Up @@ -492,7 +492,6 @@ impl<'a> TryFrom<&'a str> for Forwarded<'a> {
mod tests {
use super::*;
use crate::{Method::Get, Request, Response, Result};
use url::Url;

#[test]
fn parsing_for() -> Result<()> {
Expand Down Expand Up @@ -545,7 +544,7 @@ mod tests {
assert_eq!(forwarded.forwarded_for(), vec!["client.com"]);
assert_eq!(forwarded.host(), Some("host.com"));
assert_eq!(forwarded.proto(), Some("https"));
assert!(matches!(forwarded, Forwarded{..}));
assert!(matches!(forwarded, Forwarded { .. }));
Ok(())
}

Expand Down Expand Up @@ -599,7 +598,7 @@ mod tests {

#[test]
fn from_x_headers() -> Result<()> {
let mut request = Request::new(Get, Url::parse("http://_/")?);
let mut request = Request::new(Get, "http://_/")?;
request.append_header(X_FORWARDED_FOR, "192.0.2.43, 2001:db8:cafe::17");
request.append_header(X_FORWARDED_PROTO, "gopher");
let forwarded = Forwarded::from_headers(&request)?.unwrap();
Expand Down Expand Up @@ -644,13 +643,13 @@ mod tests {
assert_eq!(forwarded.forwarded_for(), vec!["client"]);
assert_eq!(forwarded.host(), Some("example.com"));
assert_eq!(forwarded.proto(), Some("https"));
assert!(matches!(forwarded, Forwarded{..}));
assert!(matches!(forwarded, Forwarded { .. }));
Ok(())
}

#[test]
fn from_request() -> Result<()> {
let mut request = Request::new(Get, Url::parse("http://_/")?);
let mut request = Request::new(Get, "http://_/")?;
request.append_header("Forwarded", "for=for");

let forwarded = Forwarded::from_headers(&request)?.unwrap();
Expand All @@ -662,7 +661,7 @@ mod tests {
#[test]
fn owned_can_outlive_request() -> Result<()> {
let forwarded = {
let mut request = Request::new(Get, Url::parse("http://_/")?);
let mut request = Request::new(Get, "http://_/")?;
request.append_header("Forwarded", "for=for;by=by;host=host;proto=proto");
Forwarded::from_headers(&request)?.unwrap().into_owned()
};
Expand Down
Loading