Skip to content
Merged
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: 4 additions & 0 deletions bitreq/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,8 @@ required-features = ["async"]
# Allow `format!("{}", x)`instead of enforcing `format!("{x}")`
uninlined_format_args = "allow"

[lints.rust.unexpected_cfgs]
level = "warn"
check-cfg = ["cfg(fuzzing)"]

# vim: ft=conf
49 changes: 49 additions & 0 deletions bitreq/fuzz/src/url_parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,55 @@ pub fn do_test(data: &[u8]) {
let ref_pairs: Vec<(String, String)> =
ref_url.query_pairs().map(|(k, v)| (k.into_owned(), v.into_owned())).collect();
assert_eq!(bitreq_pairs, ref_pairs, "Query pairs mismatch for input: {:?}", input);

// Test append_query_params - use parts of the input as key/value
// This exercises the expect in append_query_params
{
let mut url_clone = bitreq_url.clone();
// Use the input itself as both key and value to exercise encoding
url_clone.append_query_params([(input.to_string(), input.to_string())]);
// Verify the URL is still valid by accessing its fields
let _ = url_clone.query();
let _ = url_clone.as_str();

// Test with empty strings
let mut url_clone2 = bitreq_url.clone();
url_clone2.append_query_params([("".into(), "".into())]);
let _ = url_clone2.as_str();

// Test appending multiple params in one call
let mut url_clone3 = bitreq_url.clone();
url_clone3.append_query_params([
("key1".into(), "value1".into()),
("key2".into(), input.to_string()),
]);
let _ = url_clone3.as_str();
}

// Test preserve_fragment_from - exercises the expect in preserve_fragment_from
{
// Create a URL with a fragment to test preserving from
if let Ok(url_with_frag) = BitreqUrl::parse("http://example.com#testfrag") {
let mut url_clone = bitreq_url.clone();
url_clone.preserve_fragment_from(&url_with_frag);
let new_frag = url_clone.fragment().unwrap();
assert_eq!(new_frag, "testfrag");
let _ = url_clone.as_str();
}

// Test with the original URL as the source (may or may not have fragment)
let mut url_clone2 = bitreq_url.clone();
url_clone2.preserve_fragment_from(&bitreq_url);
let _ = url_clone2.as_str();

// Test preserve_fragment_from with a URL that has no fragment
if let Ok(url_no_frag) = BitreqUrl::parse("http://example.com/path") {
let mut url_clone3 = bitreq_url.clone();
url_clone3.preserve_fragment_from(&url_no_frag);
Comment thread
tnull marked this conversation as resolved.
let _ = url_clone3.as_str();
assert_eq!(url_clone2.fragment(), url_clone3.fragment());
}
}
}
(Ok(v), Err(e)) => {
panic!("bitreq parsed, URL did not. Input {input:?}. Err {e:?}");
Expand Down
12 changes: 10 additions & 2 deletions bitreq/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ use core::{fmt, str};
#[cfg(feature = "std")]
use std::{error, io};

use crate::UrlParseError;

/// Represents an error while sending, receiving, or parsing an HTTP response.
#[derive(Debug)]
#[non_exhaustive]
Expand All @@ -12,10 +14,11 @@ pub enum Error {
#[cfg(feature = "json-using-serde")]
/// Ran into a Serde error.
SerdeJsonError(serde_json::Error),
/// The given URL is invalid and we failed parsing.
InvalidUrl(UrlParseError),
/// The response body contains invalid UTF-8, so the `as_str()`
/// conversion failed.
InvalidUtf8InBody(str::Utf8Error),

#[cfg(feature = "rustls")]
/// Ran into a rustls error while creating the connection.
RustlsCreateConnection(rustls::Error),
Expand Down Expand Up @@ -97,8 +100,8 @@ impl fmt::Display for Error {
SerdeJsonError(err) => write!(f, "{}", err),
#[cfg(feature = "std")]
IoError(err) => write!(f, "{}", err),
InvalidUrl(err) => write!(f, "failed to parse given URL: {}", err),
InvalidUtf8InBody(err) => write!(f, "{}", err),

#[cfg(feature = "rustls")]
RustlsCreateConnection(err) => write!(f, "error creating rustls connection: {}", err),
#[cfg(feature = "native-tls")]
Expand Down Expand Up @@ -140,6 +143,7 @@ impl error::Error for Error {
SerdeJsonError(err) => Some(err),
#[cfg(feature = "std")]
IoError(err) => Some(err),
InvalidUrl(err) => Some(err),
InvalidUtf8InBody(err) => Some(err),
#[cfg(feature = "rustls")]
RustlsCreateConnection(err) => Some(err),
Expand All @@ -152,3 +156,7 @@ impl error::Error for Error {
impl From<io::Error> for Error {
fn from(other: io::Error) -> Error { Error::IoError(other) }
}

impl From<UrlParseError> for Error {
fn from(other: UrlParseError) -> Error { Error::InvalidUrl(other) }
}
14 changes: 4 additions & 10 deletions bitreq/src/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -363,13 +363,8 @@ pub(crate) struct ParsedRequest {
impl ParsedRequest {
#[allow(unused_mut)]
pub(crate) fn new(mut config: Request) -> Result<ParsedRequest, Error> {
let mut url = Url::parse(&config.url).map_err(|e| {
Error::IoError(std::io::Error::new(std::io::ErrorKind::InvalidInput, e.to_string()))
})?;

for (key, value) in &config.params {
url.append_query_param(key, value);
}
let mut url = Url::parse(&config.url)?;
url.append_query_params(config.params.drain(..));

#[cfg(all(feature = "proxy", feature = "std"))]
// Set default proxy from environment variables
Expand Down Expand Up @@ -511,9 +506,8 @@ impl ParsedRequest {
let mut absolute_url = String::new();
self.url.write_base_url_to(&mut absolute_url).unwrap();
absolute_url.push_str(url);
let mut new_url = Url::parse(&absolute_url).map_err(|e| {
Error::IoError(std::io::Error::new(std::io::ErrorKind::InvalidInput, e.to_string()))
})?;
let mut new_url = Url::parse(&absolute_url)?;

// Preserve fragment from original URL if new URL doesn't have one (RFC 7231 section 7.1.2)
new_url.preserve_fragment_from(&self.url);
std::mem::swap(&mut new_url, &mut self.url);
Expand Down
Loading
Loading