Skip to main content

ProxyConfig

Struct ProxyConfig 

Source
pub struct ProxyConfig { /* private fields */ }
Expand description

Proxy configuration for routing requests through an HTTP proxy.

The proxy URL itself must be http://..., including when tunneling HTTPS via CONNECT.

Implementations§

Source§

impl ProxyConfig

Source

pub fn new<U: ToUrl>(url: U) -> Result<Self, NanoGetError>

Creates a new proxy configuration.

The URL must use http://; otherwise NanoGetError::UnsupportedProxyScheme is returned.

Examples found in repository?
examples/proxy-and-proxy-auth/main.rs (line 29)
19fn main() -> Result<(), Box<dyn Error>> {
20    let Some(proxy_url) = env::var("NANO_GET_PROXY_URL").ok() else {
21        print_setup();
22        return Ok(());
23    };
24
25    let target_url = env_or_default("NANO_GET_PROXY_TARGET_URL", "http://example.com");
26    let proxy_user = env_or_default("NANO_GET_PROXY_AUTH_USER", "proxy-user");
27    let proxy_pass = env_or_default("NANO_GET_PROXY_AUTH_PASS", "proxy-pass");
28
29    let mut proxy = ProxyConfig::new(proxy_url)?;
30    proxy.add_header("X-Example-Proxy", "nano-get-demo")?;
31
32    let challenge_driven = Client::builder()
33        .proxy(proxy.clone())
34        .basic_proxy_auth(proxy_user.clone(), proxy_pass.clone())
35        .build()
36        .execute(Request::get(&target_url)?)?;
37
38    let preemptive = Client::builder()
39        .proxy(proxy.clone())
40        .preemptive_basic_proxy_auth(proxy_user.clone(), proxy_pass.clone())
41        .build()
42        .execute(Request::get(&target_url)?)?;
43
44    let mut manual = Request::get(&target_url)?;
45    manual.proxy_basic_auth(proxy_user, proxy_pass)?;
46    let manual_response = Client::builder().proxy(proxy).build().execute(manual)?;
47
48    println!("proxy-and-proxy-auth example");
49    println!("challenge-driven status: {}", challenge_driven.status_code);
50    println!("preemptive status: {}", preemptive.status_code);
51    println!(
52        "request-level override status: {}",
53        manual_response.status_code
54    );
55
56    Ok(())
57}
Source

pub fn url(&self) -> &Url

Returns the parsed proxy URL.

Source

pub fn headers(&self) -> &[Header]

Returns additional headers that will be attached to proxy requests.

Source

pub fn add_header( &mut self, name: impl Into<String>, value: impl Into<String>, ) -> Result<&mut Self, NanoGetError>

Adds a custom header to outgoing proxy requests.

Protocol-managed and hop-by-hop headers are rejected.

Examples found in repository?
examples/proxy-and-proxy-auth/main.rs (line 30)
19fn main() -> Result<(), Box<dyn Error>> {
20    let Some(proxy_url) = env::var("NANO_GET_PROXY_URL").ok() else {
21        print_setup();
22        return Ok(());
23    };
24
25    let target_url = env_or_default("NANO_GET_PROXY_TARGET_URL", "http://example.com");
26    let proxy_user = env_or_default("NANO_GET_PROXY_AUTH_USER", "proxy-user");
27    let proxy_pass = env_or_default("NANO_GET_PROXY_AUTH_PASS", "proxy-pass");
28
29    let mut proxy = ProxyConfig::new(proxy_url)?;
30    proxy.add_header("X-Example-Proxy", "nano-get-demo")?;
31
32    let challenge_driven = Client::builder()
33        .proxy(proxy.clone())
34        .basic_proxy_auth(proxy_user.clone(), proxy_pass.clone())
35        .build()
36        .execute(Request::get(&target_url)?)?;
37
38    let preemptive = Client::builder()
39        .proxy(proxy.clone())
40        .preemptive_basic_proxy_auth(proxy_user.clone(), proxy_pass.clone())
41        .build()
42        .execute(Request::get(&target_url)?)?;
43
44    let mut manual = Request::get(&target_url)?;
45    manual.proxy_basic_auth(proxy_user, proxy_pass)?;
46    let manual_response = Client::builder().proxy(proxy).build().execute(manual)?;
47
48    println!("proxy-and-proxy-auth example");
49    println!("challenge-driven status: {}", challenge_driven.status_code);
50    println!("preemptive status: {}", preemptive.status_code);
51    println!(
52        "request-level override status: {}",
53        manual_response.status_code
54    );
55
56    Ok(())
57}

Trait Implementations§

Source§

impl Clone for ProxyConfig

Source§

fn clone(&self) -> ProxyConfig

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for ProxyConfig

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.