Skip to content

Commit a9dcbbc

Browse files
committed
Fix host header for connect proxy
1 parent 423aa8f commit a9dcbbc

File tree

3 files changed

+30
-15
lines changed

3 files changed

+30
-15
lines changed

src/proxy.rs

+8-7
Original file line numberDiff line numberDiff line change
@@ -211,16 +211,17 @@ impl<In: Transport> Connector<In> for ConnectProxyConnector {
211211

212212
let mut w = TransportAdapter::new(transport);
213213

214-
let uri = &details.uri;
215-
uri.ensure_valid_url()?;
214+
// unwrap is ok because run.rs will construct the ConnectionDetails
215+
// such that CONNECT proxy _must_ have the `proxied` field set.
216+
let proxied = details.proxied.unwrap();
217+
proxied.ensure_valid_url()?;
216218

217-
// All these unwrap() are ok because ensure_valid_uri() above checks them.
218-
let host = uri.host().unwrap();
219-
let port = uri
219+
let proxied_host = proxied.host().unwrap();
220+
let proxied_port = proxied
220221
.port_u16()
221-
.unwrap_or(uri.scheme().unwrap().default_port().unwrap());
222+
.unwrap_or(proxied.scheme().unwrap().default_port().unwrap());
222223

223-
write!(w, "CONNECT {}:{} HTTP/1.1\r\n", host, port)?;
224+
write!(w, "CONNECT {}:{} HTTP/1.1\r\n", proxied_host, proxied_port)?;
224225
write!(w, "Host: {}:{}\r\n", proxy.host(), proxy.port())?;
225226
if let Some(v) = details.config.user_agent().as_str(DEFAULT_USER_AGENT) {
226227
write!(w, "User-Agent: {}\r\n", v)?;

src/run.rs

+11-8
Original file line numberDiff line numberDiff line change
@@ -315,23 +315,25 @@ fn add_headers(
315315
fn connect(
316316
agent: &Agent,
317317
config: &Config,
318-
uri: &Uri,
318+
wanted_uri: &Uri,
319319
timings: &mut CallTimings,
320320
) -> Result<Connection, Error> {
321321
// If we're using a CONNECT proxy, we need to resolve that hostname.
322322
let maybe_connect_uri = config.connect_proxy_uri();
323323

324-
let effective_uri = maybe_connect_uri.unwrap_or(uri);
324+
let (uri, proxied) = if let Some(connect_uri) = maybe_connect_uri {
325+
(connect_uri, Some(wanted_uri))
326+
} else {
327+
(wanted_uri, None)
328+
};
325329

326330
// Before resolving the URI we need to ensure it is a full URI. We
327331
// cannot make requests with partial uri like "/path".
328-
effective_uri.ensure_valid_url()?;
332+
uri.ensure_valid_url()?;
329333

330-
let addrs = agent.resolver.resolve(
331-
effective_uri,
332-
config,
333-
timings.next_timeout(Timeout::Resolve),
334-
)?;
334+
let addrs = agent
335+
.resolver
336+
.resolve(uri, config, timings.next_timeout(Timeout::Resolve))?;
335337

336338
timings.record_time(Timeout::Resolve);
337339

@@ -342,6 +344,7 @@ fn connect(
342344
config,
343345
now: timings.now(),
344346
timeout: timings.next_timeout(Timeout::Connect),
347+
proxied,
345348
};
346349

347350
let connection = agent.pool.connect(&details, config.max_idle_age().into())?;

src/unversioned/transport/mod.rs

+11
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,9 @@ where
173173
/// The parameters needed to create a [`Transport`].
174174
pub struct ConnectionDetails<'a> {
175175
/// Full uri that is being requested.
176+
///
177+
/// In the case of CONNECT (HTTP) proxy, this is the URI of the
178+
/// proxy, and the actual URI is in the `proxied` field.
176179
pub uri: &'a Uri,
177180

178181
/// The resolved IP address + port for the uri being requested. See [`Resolver`].
@@ -196,6 +199,14 @@ pub struct ConnectionDetails<'a> {
196199
/// The next timeout for making the connection.
197200
// TODO(martin): Make mechanism to lower duration for each step in the connector chain.
198201
pub timeout: NextTimeout,
202+
203+
/// In case of CONNECT (HTTP) proxy, this is the actual requested
204+
/// uri that will go through the proxy.
205+
///
206+
/// This ends up in the connect line like `CONNECT host:port HTTP/1.1`.
207+
///
208+
/// For socks proxy it is `None`.
209+
pub proxied: Option<&'a Uri>,
199210
}
200211

201212
impl<'a> ConnectionDetails<'a> {

0 commit comments

Comments
 (0)