Skip to main content

Jar

Struct Jar 

Source
pub struct Jar(/* private fields */);
Available on crate feature cookies only.
Expand description

A good default CookieStore implementation.

This is the implementation used when simply calling cookie_store(true). This type is exposed to allow creating one and filling it with some existing cookies more easily, before creating a crate::Client.

Implementations§

Source§

impl Jar

Source

pub fn get<U: IntoUri>(&self, name: &str, uri: U) -> Option<Cookie<'static>>

Returns an unexpired cookie by name for an exact URI scope.

The URI host is canonicalized and its path is used as the exact stored cookie path. Use matches to select every cookie that would apply to a request URI through RFC domain and path matching. When both storage scopes contain the name, the older cookie is returned.

§Example
use wreq::cookie::Jar;
let jar = Jar::default();
jar.add("foo=bar; Path=/foo; Domain=example.com", "http://example.com/foo");
let cookie = jar.get("foo", "http://example.com/foo").unwrap();
assert_eq!(cookie.value(), "bar");
Source

pub fn contains<U: IntoUri>(&self, name: &str, uri: U) -> bool

Returns whether an unexpired cookie exists for an exact URI scope.

This performs the same scope lookup as get without cloning the cookie.

Source

pub fn get_all(&self) -> impl Iterator<Item = Cookie<'static>>

Returns all unexpired cookies in the jar.

The returned cookies are owned snapshots with their effective stored Path. Host-only cookies keep their Domain attribute absent, so importing a snapshot into another jar does not broaden its domain scope. Snapshots are returned in creation order.

§Example
use wreq::cookie::Jar;
let jar = Jar::default();
jar.add("foo=bar; Domain=example.com", "http://example.com");
for cookie in jar.get_all() {
    println!("{}={}", cookie.name(), cookie.value());
}
Source

pub fn len(&self) -> usize

Returns the number of unexpired cookies in the jar.

Expired cookies are excluded even if their internal entries have not yet been overwritten or cleared.

Source

pub fn is_empty(&self) -> bool

Returns true when the jar has no unexpired cookies.

Source

pub fn matches<U: IntoUri>( &self, uri: U, ) -> impl Iterator<Item = Cookie<'static>>

Returns the unexpired cookies that apply to an HTTP or HTTPS request URI.

Selection applies domain matching, path matching, host-only scope, the Secure attribute, and expiration rules from the RFC 6265 retrieval model. Returned cookies are owned snapshots that preserve host-only scope. Unsupported URI schemes do not match any cookies.

§Example
use wreq::cookie::Jar;

let jar = Jar::default();
jar.add("root=1; Domain=example.com; Path=/", "https://example.com/");
jar.add("api=2; Domain=example.com; Path=/api", "https://example.com/api");

let cookies = jar
    .matches("https://www.example.com/api/users")
    .collect::<Vec<_>>();
assert_eq!(cookies.len(), 2);
assert!(cookies.iter().any(|cookie| cookie.name() == "root"));
assert!(cookies.iter().any(|cookie| cookie.name() == "api"));
Source

pub fn add<C, U>(&self, cookie: C, uri: U)
where C: IntoCookie, U: IntoUri,

Stores a cookie received from a URI.

The URI supplies the host-only domain and default path when those attributes are absent. Cookies with an invalid URI or an invalid or mismatched Domain are ignored. A non-positive Max-Age removes an existing cookie in the same scope, as required by the RFC 6265 storage model. A positive Max-Age is stored as an absolute deadline. Insecure origins cannot set Secure cookies or overlay an existing Secure cookie, following the RFC 6265bis storage model.

§Example
use wreq::cookie::Jar;
use cookie::CookieBuilder;
let jar = Jar::default();
let cookie = CookieBuilder::new("foo", "bar")
    .domain("example.com")
    .path("/")
    .build();
jar.add(cookie, "http://example.com");
Source

pub fn remove<C, U>(&self, cookie: C, uri: U)
where C: Into<RawCookie<'static>>, U: IntoUri,

Removes a cookie from an exact URI scope.

The URI host and path identify the stored scope. Both host-only and Domain cookies with the same name in that scope are removed; other domains and paths are left unchanged.

§Example
use wreq::cookie::Jar;
let jar = Jar::default();
jar.add("foo=bar; Path=/foo; Domain=example.com", "http://example.com/foo");
assert!(jar.get("foo", "http://example.com/foo").is_some());
jar.remove("foo", "http://example.com/foo");
assert!(jar.get("foo", "http://example.com/foo").is_none());
Source

pub fn clear(&self)

Removes every cookie from the jar, leaving it empty.

§Example
use wreq::cookie::Jar;
let jar = Jar::default();
jar.add("foo=bar; Domain=example.com", "http://example.com");
assert_eq!(jar.get_all().count(), 1);
jar.clear();
assert_eq!(jar.get_all().count(), 0);

Trait Implementations§

Source§

impl CookieStore for Jar

Source§

fn set_cookies( &self, cookie_headers: &mut dyn Iterator<Item = &HeaderValue>, uri: &Uri, )

Stores Set-Cookie field values received from uri.
Source§

fn cookies(&self, uri: &Uri, version: Version) -> Cookies

Serializes the cookies that apply to uri for the requested HTTP version. Read more
Source§

impl Debug for Jar

Source§

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

Formats the value using the given formatter. Read more
Source§

impl Default for Jar

Source§

fn default() -> Jar

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl !Freeze for Jar

§

impl !RefUnwindSafe for Jar

§

impl Send for Jar

§

impl Sync for Jar

§

impl Unpin for Jar

§

impl UnsafeUnpin for Jar

§

impl UnwindSafe for Jar

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<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

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<S> IntoCookieStore for S
where S: CookieStore + 'static,

Source§

fn into_shared(self) -> Arc<dyn CookieStore>

Available on crate feature cookies only.
Converts this value into a shared cookie store.
Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
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.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

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