From 5ae6702e18ace03f174944780a710ab059114381 Mon Sep 17 00:00:00 2001 From: Martin Algesten Date: Sun, 5 Jan 2025 13:48:45 +0100 Subject: [PATCH 1/4] Code format --- src/proxy.rs | 103 +++++++++++++++++++++++++-------------------------- 1 file changed, 51 insertions(+), 52 deletions(-) diff --git a/src/proxy.rs b/src/proxy.rs index 54aec181..a5ef1f56 100644 --- a/src/proxy.rs +++ b/src/proxy.rs @@ -201,72 +201,71 @@ impl Connector for ConnectProxyConnector { }; let is_connect_proxy = details.config.connect_proxy_uri().is_some(); + if !is_connect_proxy { + return Ok(Some(transport)); + } - if is_connect_proxy { - // unwrap is ok because connect_proxy_uri() above checks it. - let proxy = details.config.proxy().unwrap(); - - let mut w = TransportAdapter::new(transport); + // unwrap is ok because connect_proxy_uri() above checks it. + let proxy = details.config.proxy().unwrap(); - let uri = &details.uri; - uri.ensure_valid_url()?; + let mut w = TransportAdapter::new(transport); - // All these unwrap() are ok because ensure_valid_uri() above checks them. - let host = uri.host().unwrap(); - let port = uri - .port_u16() - .unwrap_or(uri.scheme().unwrap().default_port().unwrap()); + let uri = &details.uri; + uri.ensure_valid_url()?; - write!(w, "CONNECT {}:{} HTTP/1.1\r\n", host, port)?; - write!(w, "Host: {}:{}\r\n", host, port)?; - if let Some(v) = details.config.user_agent().as_str(DEFAULT_USER_AGENT) { - write!(w, "User-Agent: {}\r\n", v)?; - } - write!(w, "Proxy-Connection: Keep-Alive\r\n")?; + // All these unwrap() are ok because ensure_valid_uri() above checks them. + let host = uri.host().unwrap(); + let port = uri + .port_u16() + .unwrap_or(uri.scheme().unwrap().default_port().unwrap()); - let use_creds = proxy.username().is_some() || proxy.password().is_some(); + write!(w, "CONNECT {}:{} HTTP/1.1\r\n", host, port)?; + write!(w, "Host: {}:{}\r\n", host, port)?; + if let Some(v) = details.config.user_agent().as_str(DEFAULT_USER_AGENT) { + write!(w, "User-Agent: {}\r\n", v)?; + } + write!(w, "Proxy-Connection: Keep-Alive\r\n")?; - if use_creds { - let user = proxy.username().unwrap_or_default(); - let pass = proxy.password().unwrap_or_default(); - let creds = BASE64_STANDARD.encode(format!("{}:{}", user, pass)); - write!(w, "Proxy-Authorization: basic {}\r\n", creds)?; - } + let use_creds = proxy.username().is_some() || proxy.password().is_some(); - write!(w, "\r\n")?; - w.flush()?; + if use_creds { + let user = proxy.username().unwrap_or_default(); + let pass = proxy.password().unwrap_or_default(); + let creds = BASE64_STANDARD.encode(format!("{}:{}", user, pass)); + write!(w, "Proxy-Authorization: basic {}\r\n", creds)?; + } - let mut transport = w.into_inner(); + write!(w, "\r\n")?; + w.flush()?; - let response = loop { - let made_progress = transport.await_input(details.timeout)?; - let buffers = transport.buffers(); - let input = buffers.input(); - let Some((used_input, response)) = try_parse_response::<20>(input)? else { - if !made_progress { - let reason = "proxy server did not respond".to_string(); - return Err(Error::ConnectProxyFailed(reason)); - } - continue; - }; - buffers.input_consume(used_input); - break response; - }; + let mut transport = w.into_inner(); - match response.status() { - StatusCode::OK => { - trace!("CONNECT proxy connected"); - } - x => { - let reason = format!("proxy server responded {}/{}", x.as_u16(), x.as_str()); + let response = loop { + let made_progress = transport.await_input(details.timeout)?; + let buffers = transport.buffers(); + let input = buffers.input(); + let Some((used_input, response)) = try_parse_response::<20>(input)? else { + if !made_progress { + let reason = "proxy server did not respond".to_string(); return Err(Error::ConnectProxyFailed(reason)); } - } + continue; + }; + buffers.input_consume(used_input); + break response; + }; - Ok(Some(transport)) - } else { - Ok(Some(transport)) + match response.status() { + StatusCode::OK => { + trace!("CONNECT proxy connected"); + } + x => { + let reason = format!("proxy server responded {}/{}", x.as_u16(), x.as_str()); + return Err(Error::ConnectProxyFailed(reason)); + } } + + Ok(Some(transport)) } } From 76d07b7a94f2e1f11c916ce4470663fe6f1f45f0 Mon Sep 17 00:00:00 2001 From: Martin Algesten Date: Sun, 5 Jan 2025 13:49:25 +0100 Subject: [PATCH 2/4] Connect proxy host/port for Host header --- src/proxy.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/proxy.rs b/src/proxy.rs index a5ef1f56..4e7f6ae8 100644 --- a/src/proxy.rs +++ b/src/proxy.rs @@ -220,7 +220,7 @@ impl Connector for ConnectProxyConnector { .unwrap_or(uri.scheme().unwrap().default_port().unwrap()); write!(w, "CONNECT {}:{} HTTP/1.1\r\n", host, port)?; - write!(w, "Host: {}:{}\r\n", host, port)?; + write!(w, "Host: {}:{}\r\n", proxy.host(), proxy.port())?; if let Some(v) = details.config.user_agent().as_str(DEFAULT_USER_AGENT) { write!(w, "User-Agent: {}\r\n", v)?; } From d45bc4f944fd7165ef52dacf416ebb3d5ee3b96a Mon Sep 17 00:00:00 2001 From: Martin Algesten Date: Sun, 5 Jan 2025 14:37:23 +0100 Subject: [PATCH 3/4] Reinstate ConnectProxyConnector --- src/proxy.rs | 3 ++- src/unversioned/transport/mod.rs | 2 ++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/src/proxy.rs b/src/proxy.rs index 4e7f6ae8..b6149626 100644 --- a/src/proxy.rs +++ b/src/proxy.rs @@ -186,7 +186,8 @@ impl Proxy { /// /// This operates on the previous chained transport typically a TcpConnector optionally /// wrapped in TLS. -pub struct ConnectProxyConnector; +#[derive(Default)] +pub struct ConnectProxyConnector(()); impl Connector for ConnectProxyConnector { type Out = In; diff --git a/src/unversioned/transport/mod.rs b/src/unversioned/transport/mod.rs index d7f81a35..597b5b70 100644 --- a/src/unversioned/transport/mod.rs +++ b/src/unversioned/transport/mod.rs @@ -349,6 +349,8 @@ impl Default for DefaultConnector { crate::tls::TlsProvider::NativeTls, )); + let inner = inner.chain(ConnectProxyConnector::default()); + DefaultConnector { inner: boxed_connector(inner), } From a9ef1567f448533cc3d6dce725bb4a752f59b307 Mon Sep 17 00:00:00 2001 From: Martin Algesten Date: Sun, 5 Jan 2025 20:46:30 +0100 Subject: [PATCH 4/4] Update changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4d3ee39a..3193f8c9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,6 @@ # Unreleased + * Re-enable CONNECT proxy support (#932) * Body::content_length (#927) * Handle Authorization: Basic from URI (#923) * Remove many uses of Box::new() from Connector chain (#919)