Skip to main content

WinHttpPacResolver

Struct WinHttpPacResolver 

Source
pub struct WinHttpPacResolver { /* private fields */ }
Available on Windows and crate feature pac-windows-native and crate feature pac only.
Expand description

PAC/WPAD via WinHttpGetProxyForUrlEx. Reuse one session; the cache it does not bound is described on resolve.

Pac / WpadAutoDetect: native fetch; WPAD only after with_wpad(true). PacInline: a JS engine, PacResolver routes it there and the rest here. Direct/Manual: resolve_config. resolve_config answers a hostless URL Direct, except PacInline: that one is Error::PacNotSupported whether or not the URL has a host. resolve does not short-circuit it; see its doc. PacPolicy does not apply: real DNS/local IP.

Implementations§

Source§

impl WinHttpPacResolver

Source

pub fn new() -> Result<Self, Error>

Open a session with the default DEFAULT_WINHTTP_PAC_TIMEOUT budget.

§Errors

Error::Io when WinHttpOpen, WinHttpSetTimeouts or WinHttpSetStatusCallback fails.

Source

pub fn with_timeout(timeout: Duration) -> Result<Self, Error>

Open a session; zero timeout → Error::PacTimeout (not “unlimited”).

§Errors

Error::Io (WinHttpOpen / SetTimeouts / SetStatusCallback) or Error::PacTimeout when timeout is zero.

Source

pub fn timeout(&self) -> Duration

The per-resolution budget.

Source

pub fn with_wpad(self, wpad: bool) -> Self

Allow WPAD discovery. Off by default: discovery asks the local network where the PAC script is, and whoever answers for wpad or DHCP option 252 there chooses the proxy. Off, WpadAutoDetect with a host and the AutoDetect / AutoDetectThenUrl sources are Error::PacNotSupported; a hostless URL is still Direct.

Source

pub fn wpad(&self) -> bool

Whether WPAD discovery is allowed.

Source

pub fn reset_auto_proxy(&self) -> Result<AutoProxyReset, Error>

Flush what the WinHTTP autoproxy service remembers (discovered and downloaded scripts, and the WPAD misses) so that the next resolution discovers and downloads afresh. Call it after the network changes.

This is WinHttpResetAutoProxy with WINHTTP_RESET_ALL | WINHTTP_RESET_OUT_OF_PROC. The cache it clears belongs to the service, not to this resolver, so the reset is machine-wide: every process that resolves through the service fetches again. Opening a new resolver does not do this; the service outlives the session.

Microsoft documents that a reset within 30 seconds of the previous one returns success and does nothing. A call inside that window is therefore not made, and AutoProxyReset::TooSoon says how long to wait. The window counts resets made through this crate in this process; one made by another process is invisible here, and a reset requested right after it can still be dropped by the service.

Windows 8 / Server 2012 or later.

§Errors

Error::Io when WinHttpResetAutoProxy fails.

Source

pub fn resolve( &self, url: &Url, source: &WinHttpPacSource, ) -> Result<Vec<ProxyStep>, Error>

Resolve url via source. Full FindProxyForURL chain → ProxySteps (HTTP/HTTPS/SOCKS4; fProxy == FALSE → Direct; FTP skipped; repeats collapse, as in parse_find_proxy_result, so the chain can be shorter than the entry count WinHTTP reported).

ERROR_WINHTTP_AUTODETECTION_FAILED → [Direct], but only from a source that asked for auto-detect. Download / script failure (and that same code from a URL-only source, where there was nothing to discover) stays an error.

A hostless URL (mailto:, data:) is passed to FindProxyForURL with an empty host, as in pac::evaluate. resolve_config returns Direct for hostless URLs without evaluation because it resolves a whole configuration; this method evaluates the source selected by the caller.

Asking twice can be answered once. What WinHTTP caches is the autoproxy URL and the script; a repeat resolution re-runs that script rather than fetching it again, and a WPAD miss is remembered for as long as the session lives. The call that would bypass the cache is not made: it hands the user’s credentials to whatever WPAD found. Dropping the resolver does not flush it: WinHttpGetProxyForUrlEx “always executes out-of-process”, and with that service active Microsoft’s AutoProxy Cache topic puts the cached URL and script “available to the whole computer”: typically until the machine’s IP address changes. reset_auto_proxy flushes it, machine-wide. Unlike ProxyConfig the result carries no capture time to say which fetch it came from.

A PacTimeout cancels the resolution without finishing it: the abandoned operation goes on draining behind this session, and a retry issued at once can spend its own budget waiting behind that rather than on the script. Widen the timeout or wait before retrying.

§Errors

Error::PacNotSupported for an auto-detect source without with_wpad(true), Error::PacTimeout, Error::PacEvaluation (ERROR_WINHTTP_BAD_AUTO_PROXY_SCRIPT), Error::PacInvalidResult, Error::Io (incl. undownloadable PAC URL).

Source

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

Like resolve_with_pac but WinHTTP fetches the script. Direct/Manual → resolve; hostless → Direct except PacInline → Error::PacNotSupported.

WpadAutoDetect may re-read the live registry for PAC URL / static fallback: not a pure function of config alone.

§Errors

Error::PacNotSupported for PacInline, and for WpadAutoDetect with a host unless with_wpad(true), plus resolve’s errors and, for the Direct/Manual arm this hands over, resolve’s, which is where Error::ProxyEntryUnusable comes from.

Trait Implementations§

Source§

impl Debug for WinHttpPacResolver

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