mz_http_proxy/
reqwest.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 [`reqwest`].
17
18use reqwest::ClientBuilder;
19
20use crate::proxy::PROXY_CONFIG;
21
22/// Creates a `reqwest` client builder that obeys the system proxy
23/// configuration.
24///
25/// For details about the system proxy configuration, see the
26/// [crate documentation](crate).
27pub fn client_builder() -> ClientBuilder {
28    let proxy = reqwest::Proxy::custom(move |url| {
29        if PROXY_CONFIG.exclude(Some(url.scheme()), url.host_str(), url.port()) {
30            return None;
31        }
32        if let Some(http_proxy) = PROXY_CONFIG.http_proxy() {
33            if url.scheme() == "http" {
34                return Some(http_proxy.to_string());
35            }
36        }
37        if let Some(https_proxy) = PROXY_CONFIG.https_proxy() {
38            if url.scheme() == "https" {
39                return Some(https_proxy.to_string());
40            }
41        }
42        if let Some(all_proxy) = PROXY_CONFIG.all_proxy() {
43            return Some(all_proxy.to_string());
44        }
45        None
46    });
47    reqwest::ClientBuilder::new().proxy(proxy)
48}
49
50/// Creates a `reqwest` client that obeys the system proxy configuration.
51///
52/// For details about the system proxy configuration, see the
53/// [crate documentation](crate).
54pub fn client() -> reqwest::Client {
55    client_builder()
56        .build()
57        .expect("reqwest::Client known to be valid")
58}