mz_http_proxy/
hyper.rs

1// Copyright Materialize, Inc. and contributors. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License in the LICENSE file at the
6// root of this repository, or online at
7//
8//     http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15
16//! Proxy adapters for [`hyper`](hyper_dep).
17
18use std::error::Error;
19
20use hyper_dep::client::HttpConnector;
21use hyper_proxy::{Proxy, ProxyConnector};
22use hyper_tls::HttpsConnector;
23
24use crate::proxy::PROXY_CONFIG;
25
26/// A proxying HTTPS connector for hyper.
27pub type Connector = ProxyConnector<HttpsConnector<HttpConnector>>;
28
29/// Create a `hyper` connector that obeys the system proxy configuration.
30///
31/// For details about the system proxy configuration, see the
32/// [crate documentation](crate).
33pub fn connector() -> Result<Connector, Box<dyn Error + Send + Sync>> {
34    let mut connector = ProxyConnector::new(HttpsConnector::new())?;
35
36    if let Some(http_proxy) = PROXY_CONFIG.http_proxy() {
37        let matches = move |scheme: Option<&str>, host: Option<&str>, port| {
38            scheme == Some("http") && !PROXY_CONFIG.exclude(scheme, host, port)
39        };
40        connector.add_proxy(Proxy::new(matches, http_proxy.clone()));
41    }
42
43    if let Some(https_proxy) = PROXY_CONFIG.https_proxy() {
44        let matches = move |scheme: Option<&str>, host: Option<&str>, port| {
45            scheme == Some("https") && !PROXY_CONFIG.exclude(scheme, host, port)
46        };
47        connector.add_proxy(Proxy::new(matches, https_proxy.clone()));
48    }
49
50    if let Some(all_proxy) = PROXY_CONFIG.all_proxy() {
51        let matches = move |scheme: Option<&str>, host: Option<&str>, port| {
52            !PROXY_CONFIG.exclude(scheme, host, port)
53        };
54        connector.add_proxy(Proxy::new(matches, all_proxy.clone()));
55    }
56
57    Ok(connector)
58}