Skip to content

Commit

Permalink
Set default proxy scheme in proxy uri
Browse files Browse the repository at this point in the history
  • Loading branch information
algesten committed Jan 29, 2025
1 parent 7533c1a commit c6e795e
Showing 1 changed file with 32 additions and 3 deletions.
35 changes: 32 additions & 3 deletions src/proxy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use std::convert::{TryFrom, TryInto};
use std::fmt;
use std::io::Write;
use std::sync::Arc;
use ureq_proto::http::uri::{PathAndQuery, Scheme};
use ureq_proto::parser::try_parse_response;

use http::{StatusCode, Uri};
Expand Down Expand Up @@ -83,14 +84,21 @@ impl Proxy {
}

fn new_with_flag(proxy: &str, from_env: bool) -> Result<Self, Error> {
let uri = proxy.parse::<Uri>().unwrap();
let mut uri = proxy.parse::<Uri>().unwrap();

// The uri must have an authority part (with the host), or
// it is invalid.
let _ = uri.authority().ok_or(Error::InvalidProxyUrl)?;

// The default protocol is Proto::HTTP
let scheme = uri.scheme_str().unwrap_or("http");
let scheme = match uri.scheme_str() {
Some(v) => v,
None => {
// The default protocol is Proto::HTTP, and it is missing in
// the uri. Let's put it in place.
uri = insert_default_scheme(uri);
"http"
}
};
let proto = scheme.try_into()?;

let inner = ProxyInner {
Expand Down Expand Up @@ -182,6 +190,20 @@ impl Proxy {
}
}

fn insert_default_scheme(uri: Uri) -> Uri {
let mut parts = uri.into_parts();

parts.scheme = Some(Scheme::HTTP);

// For some reason uri.into_parts can produce None for
// the path, but Uri::from_parts does not accept that.
parts.path_and_query = parts
.path_and_query
.or_else(|| Some(PathAndQuery::from_static("/")));

Uri::from_parts(parts).unwrap()
}

/// Connector for CONNECT proxy settings.
///
/// This operates on the previous chained transport typically a TcpConnector optionally
Expand Down Expand Up @@ -425,4 +447,11 @@ mod test {
let c = Proxy::new("socks://1.2.3.4").unwrap();
assert_no_alloc(|| c.clone());
}

#[test]
fn proxy_new_default_scheme() {
let c = Proxy::new("localhost:1234").unwrap();
assert_eq!(c.proto(), Proto::Http);
assert_eq!(c.uri(), "http://localhost:1234");
}
}

0 comments on commit c6e795e

Please sign in to comment.