Skip to main content

PacResolver

Struct PacResolver 

Source
pub struct PacResolver { /* private fields */ }
Available on crate feature pac only.
Expand description

Routes a whole ProxyConfig to whichever PAC engine answers its mode.

The order, first match wins:

  1. Direct / Manual: resolve, bypass list included. A script is ignored.
  2. A URL with no host (mailto:, data:): Direct, and no engine runs.
  3. A script the caller passed: the JS engine, whichever PAC mode is effective. A caller who fetched the body has already answered the question the mode asks.
  4. PacInline: the JS engine, with the body the mode carries.

“The JS engine” is the evaluator attached with with_evaluator when there is one, else evaluate under this resolver’s PacPolicy. 5. Pac / WpadAutoDetect with a native resolver attached (with_native): WinHttpPacResolver::resolve_config (Windows + pac-windows-native) or CfNetworkPacResolver::resolve_config (macOS + pac-macos-native, iOS + pac-ios-native), which downloads, and discovers once the resolver was built with_wpad(true); AndroidPacResolver::resolve_config (Android + pac-android-native), which asks the system PAC service through ProxySelector. 6. Otherwise what resolve_with_pac says: Pac → Error::PacFetchRequired, WpadAutoDetect → Error::PacNotSupported.

A failure is reported, never retried on the other engine: a native error does not fall back to JS, and a JS error does not fall back to the native one. The two engines disagree about what a script is allowed to see: PacPolicy governs the JS engine only, and the native one uses real DNS and the real local address, so a silent retry would change which policy produced the answer.

The two budgets are independent: PacPolicy::timeout bounds a JS evaluation, and the WinHTTP and CFNetwork resolvers’ timeout bounds a native resolution, download and discovery included. The Android resolver has no timeout of its own.

With no evaluator attached and no QuickJS engine compiled in (pac-quickjs off, or on Android or iOS, where the feature builds without one), the JS arms are Error::PacEngineUnavailable.

use proxy_watch::pac::{PacPolicy, PacResolver};
use proxy_watch::{ProxyConfig, ProxyConfigSource, ProxyMode, ProxyStep, Url};

let mode = ProxyMode::pac_inline(
    "function FindProxyForURL(u, h) { return 'DIRECT'; }".to_owned(),
);
let config = ProxyConfig::from_source(ProxyConfigSource::Registry, mode);
let url = Url::parse("http://example.net/").unwrap();

let resolver = PacResolver::new(PacPolicy::new());
assert_eq!(resolver.resolve_config(&config, &url, None)?, vec![ProxyStep::Direct]);

Implementations§

Source§

impl PacResolver

Source

pub fn new(policy: PacPolicy) -> Self

A resolver that runs PAC scripts under policy and has no native engine attached.

Source

pub fn with_native(self, native: Native) -> Self

Available on pac_native only.

Attach the native resolver (WinHttpPacResolver on Windows, CfNetworkPacResolver on macOS and iOS, AndroidPacResolver on Android) for Pac and WpadAutoDetect with no caller-supplied script.

Takes a built resolver rather than building one, so that a failure to open a session is the caller’s to see, not a silent fall back to “fetch it yourself”, and so that its with_wpad choice is the caller’s.

Source

pub fn with_system_native(self) -> Result<Self, Error>

Attach this target’s native resolver, built with its defaults (WPAD off), when a pac-*-native feature enables one; otherwise return self unchanged. Available without cfg guards in cross-platform applications.

§Errors

What building the native resolver returns: on Windows, a WinHTTP session that fails to open.

Source

pub fn native(&self) -> Option<&Native>

Available on pac_native only.

The attached native resolver, if any.

Source

pub fn with_evaluator( self, evaluator: impl PacEvaluator + Send + Sync + 'static, ) -> Self

Run the JS arms on evaluator instead of evaluate, for example a SubprocessEvaluator (feature pac-subprocess), so that a script from the configuration runs in a worker process.

The evaluator runs under the policy it was built with; this resolver’s policy then governs nothing. The native arm is unchanged: a native resolver still answers URL and WPAD modes when no script is passed.

Source

pub fn policy(&self) -> &PacPolicy

The policy evaluate runs under when no evaluator is attached.

Source

pub fn resolve_config( &self, config: &ProxyConfig, url: &Url, script: Option<&PacScript>, ) -> Result<Vec<ProxyStep>, Error>

Resolve url under config, routing as the type documentation lists.

§Errors

Whatever the engine the mode routed to returns: resolve_with_pac’s errors for the JS arms and the fall-through (an attached evaluator’s own errors in place of evaluate’s), and the native resolver’s resolve_config’s for the native arm.

Trait Implementations§

Source§

impl Debug for PacResolver

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> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self> ⓘ

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self> ⓘ

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

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

fn try_from(value: U) -> Result<T, !>

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.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> ⓘ
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self> ⓘ

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more