Make cookies conditional
This commit is contained in:
committed by
Martin Algesten
parent
9afbb834af
commit
da42f2ed8f
@@ -11,15 +11,16 @@ categories = ["web-programming::http-client"]
|
|||||||
edition = "2018"
|
edition = "2018"
|
||||||
|
|
||||||
[features]
|
[features]
|
||||||
default = ["tls"]
|
default = ["tls", "cookies"]
|
||||||
json = ["serde_json"]
|
json = ["serde_json"]
|
||||||
charset = ["encoding"]
|
charset = ["encoding"]
|
||||||
tls = ["rustls", "webpki", "webpki-roots"]
|
tls = ["rustls", "webpki", "webpki-roots"]
|
||||||
|
cookies = ["cookie"]
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
base64 = "0.10"
|
base64 = "0.10"
|
||||||
chunked_transfer = "1"
|
chunked_transfer = "1"
|
||||||
cookie = { version = "0.12", features = ["percent-encode"] }
|
cookie = { version = "0.12", features = ["percent-encode"], optional = true}
|
||||||
lazy_static = "1"
|
lazy_static = "1"
|
||||||
qstring = "0.7"
|
qstring = "0.7"
|
||||||
url = "2"
|
url = "2"
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
use std::sync::Mutex;
|
use std::sync::Mutex;
|
||||||
|
#[cfg(feature = "cookie")]
|
||||||
use cookie::{Cookie, CookieJar};
|
use cookie::{Cookie, CookieJar};
|
||||||
|
|
||||||
use crate::header::{self, Header};
|
use crate::header::{self, Header};
|
||||||
@@ -51,6 +51,7 @@ pub(crate) struct AgentState {
|
|||||||
/// Reused connections between requests.
|
/// Reused connections between requests.
|
||||||
pub(crate) pool: ConnectionPool,
|
pub(crate) pool: ConnectionPool,
|
||||||
/// Cookies saved between requests.
|
/// Cookies saved between requests.
|
||||||
|
#[cfg(feature = "cookie")]
|
||||||
pub(crate) jar: CookieJar,
|
pub(crate) jar: CookieJar,
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -58,6 +59,7 @@ impl AgentState {
|
|||||||
fn new() -> Self {
|
fn new() -> Self {
|
||||||
AgentState {
|
AgentState {
|
||||||
pool: ConnectionPool::new(),
|
pool: ConnectionPool::new(),
|
||||||
|
#[cfg(feature = "cookie")]
|
||||||
jar: CookieJar::new(),
|
jar: CookieJar::new(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -175,6 +177,7 @@ impl Agent {
|
|||||||
///
|
///
|
||||||
/// assert!(agent.cookie("NID").is_some());
|
/// assert!(agent.cookie("NID").is_some());
|
||||||
/// ```
|
/// ```
|
||||||
|
#[cfg(feature = "cookie")]
|
||||||
pub fn cookie(&self, name: &str) -> Option<Cookie<'static>> {
|
pub fn cookie(&self, name: &str) -> Option<Cookie<'static>> {
|
||||||
let state = self.state.lock().unwrap();
|
let state = self.state.lock().unwrap();
|
||||||
state
|
state
|
||||||
@@ -191,6 +194,7 @@ impl Agent {
|
|||||||
/// let cookie = ureq::Cookie::new("name", "value");
|
/// let cookie = ureq::Cookie::new("name", "value");
|
||||||
/// agent.set_cookie(cookie);
|
/// agent.set_cookie(cookie);
|
||||||
/// ```
|
/// ```
|
||||||
|
#[cfg(feature = "cookie")]
|
||||||
pub fn set_cookie(&self, cookie: Cookie<'static>) {
|
pub fn set_cookie(&self, cookie: Cookie<'static>) {
|
||||||
let mut state = self.state.lock().unwrap();
|
let mut state = self.state.lock().unwrap();
|
||||||
match state.as_mut() {
|
match state.as_mut() {
|
||||||
|
|||||||
@@ -115,6 +115,7 @@ pub use crate::request::Request;
|
|||||||
pub use crate::response::Response;
|
pub use crate::response::Response;
|
||||||
|
|
||||||
// re-export
|
// re-export
|
||||||
|
#[cfg(feature = "cookie")]
|
||||||
pub use cookie::Cookie;
|
pub use cookie::Cookie;
|
||||||
#[cfg(feature = "json")]
|
#[cfg(feature = "json")]
|
||||||
pub use serde_json::{to_value as serde_to_value, Map as SerdeMap, Value as SerdeValue};
|
pub use serde_json::{to_value as serde_to_value, Map as SerdeMap, Value as SerdeValue};
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ use std::io::{Result as IoResult, Write};
|
|||||||
use std::sync::{Arc, Mutex};
|
use std::sync::{Arc, Mutex};
|
||||||
|
|
||||||
use base64;
|
use base64;
|
||||||
|
#[cfg(feature = "cookie")]
|
||||||
use cookie::{Cookie, CookieJar};
|
use cookie::{Cookie, CookieJar};
|
||||||
use qstring::QString;
|
use qstring::QString;
|
||||||
use url::Url;
|
use url::Url;
|
||||||
@@ -149,7 +150,9 @@ pub(crate) fn connect(
|
|||||||
let mut resp = Response::from_read(&mut stream);
|
let mut resp = Response::from_read(&mut stream);
|
||||||
|
|
||||||
// squirrel away cookies
|
// squirrel away cookies
|
||||||
save_cookies(&unit, &resp);
|
if cfg!(feature = "cookies") {
|
||||||
|
save_cookies(&unit, &resp);
|
||||||
|
}
|
||||||
|
|
||||||
// handle redirects
|
// handle redirects
|
||||||
if resp.redirect() && req.redirects > 0 {
|
if resp.redirect() && req.redirects > 0 {
|
||||||
@@ -196,6 +199,7 @@ pub(crate) fn connect(
|
|||||||
}
|
}
|
||||||
|
|
||||||
// TODO check so cookies can't be set for tld:s
|
// TODO check so cookies can't be set for tld:s
|
||||||
|
#[cfg(feature = "cookie")]
|
||||||
fn match_cookies<'a>(jar: &'a CookieJar, domain: &str, path: &str, is_secure: bool) -> Vec<Header> {
|
fn match_cookies<'a>(jar: &'a CookieJar, domain: &str, path: &str, is_secure: bool) -> Vec<Header> {
|
||||||
jar.iter()
|
jar.iter()
|
||||||
.filter(|c| {
|
.filter(|c| {
|
||||||
@@ -299,6 +303,7 @@ fn send_prelude(unit: &Unit, stream: &mut Stream, redir: bool) -> IoResult<()> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Investigate a response for "Set-Cookie" headers.
|
/// Investigate a response for "Set-Cookie" headers.
|
||||||
|
#[cfg(feature = "cookie")]
|
||||||
fn save_cookies(unit: &Unit, resp: &Response) {
|
fn save_cookies(unit: &Unit, resp: &Response) {
|
||||||
//
|
//
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user