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"
|
||||
|
||||
[features]
|
||||
default = ["tls"]
|
||||
default = ["tls", "cookies"]
|
||||
json = ["serde_json"]
|
||||
charset = ["encoding"]
|
||||
tls = ["rustls", "webpki", "webpki-roots"]
|
||||
cookies = ["cookie"]
|
||||
|
||||
[dependencies]
|
||||
base64 = "0.10"
|
||||
chunked_transfer = "1"
|
||||
cookie = { version = "0.12", features = ["percent-encode"] }
|
||||
cookie = { version = "0.12", features = ["percent-encode"], optional = true}
|
||||
lazy_static = "1"
|
||||
qstring = "0.7"
|
||||
url = "2"
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
use std::sync::Arc;
|
||||
use std::sync::Mutex;
|
||||
|
||||
#[cfg(feature = "cookie")]
|
||||
use cookie::{Cookie, CookieJar};
|
||||
|
||||
use crate::header::{self, Header};
|
||||
@@ -51,6 +51,7 @@ pub(crate) struct AgentState {
|
||||
/// Reused connections between requests.
|
||||
pub(crate) pool: ConnectionPool,
|
||||
/// Cookies saved between requests.
|
||||
#[cfg(feature = "cookie")]
|
||||
pub(crate) jar: CookieJar,
|
||||
}
|
||||
|
||||
@@ -58,6 +59,7 @@ impl AgentState {
|
||||
fn new() -> Self {
|
||||
AgentState {
|
||||
pool: ConnectionPool::new(),
|
||||
#[cfg(feature = "cookie")]
|
||||
jar: CookieJar::new(),
|
||||
}
|
||||
}
|
||||
@@ -175,6 +177,7 @@ impl Agent {
|
||||
///
|
||||
/// assert!(agent.cookie("NID").is_some());
|
||||
/// ```
|
||||
#[cfg(feature = "cookie")]
|
||||
pub fn cookie(&self, name: &str) -> Option<Cookie<'static>> {
|
||||
let state = self.state.lock().unwrap();
|
||||
state
|
||||
@@ -191,6 +194,7 @@ impl Agent {
|
||||
/// let cookie = ureq::Cookie::new("name", "value");
|
||||
/// agent.set_cookie(cookie);
|
||||
/// ```
|
||||
#[cfg(feature = "cookie")]
|
||||
pub fn set_cookie(&self, cookie: Cookie<'static>) {
|
||||
let mut state = self.state.lock().unwrap();
|
||||
match state.as_mut() {
|
||||
|
||||
@@ -115,6 +115,7 @@ pub use crate::request::Request;
|
||||
pub use crate::response::Response;
|
||||
|
||||
// re-export
|
||||
#[cfg(feature = "cookie")]
|
||||
pub use cookie::Cookie;
|
||||
#[cfg(feature = "json")]
|
||||
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 base64;
|
||||
#[cfg(feature = "cookie")]
|
||||
use cookie::{Cookie, CookieJar};
|
||||
use qstring::QString;
|
||||
use url::Url;
|
||||
@@ -149,7 +150,9 @@ pub(crate) fn connect(
|
||||
let mut resp = Response::from_read(&mut stream);
|
||||
|
||||
// squirrel away cookies
|
||||
save_cookies(&unit, &resp);
|
||||
if cfg!(feature = "cookies") {
|
||||
save_cookies(&unit, &resp);
|
||||
}
|
||||
|
||||
// handle redirects
|
||||
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
|
||||
#[cfg(feature = "cookie")]
|
||||
fn match_cookies<'a>(jar: &'a CookieJar, domain: &str, path: &str, is_secure: bool) -> Vec<Header> {
|
||||
jar.iter()
|
||||
.filter(|c| {
|
||||
@@ -299,6 +303,7 @@ fn send_prelude(unit: &Unit, stream: &mut Stream, redir: bool) -> IoResult<()> {
|
||||
}
|
||||
|
||||
/// Investigate a response for "Set-Cookie" headers.
|
||||
#[cfg(feature = "cookie")]
|
||||
fn save_cookies(unit: &Unit, resp: &Response) {
|
||||
//
|
||||
|
||||
|
||||
Reference in New Issue
Block a user