API changes for 2.0

* Remove Request::build
* All mutations on Request follow builder pattern

The previous `build()` on request was necessary because mutating
functions did not follow a proper builder pattern (taking `&mut self`
instead of `mut self`). With a proper builder pattern, the need for
`.build()` goes away.

* All Request body and call methods consume self

Anything which "executes" the request will now consume the `Request`
to produce a `Result<Response>`.

* Move all config from request to agent builder

Timeouts, redirect config, proxy settings and TLS config are now on
`AgentBuilder`.

* Rename max_pool_connections -> max_idle_connections
* Rename max_pool_connections_per_host ->  max_idle_connections_per_host

Consistent internal and external naming.

* Introduce new AgentConfig for static config created by builder.

`Agent` can be seen as having two parts. Static config and a mutable
shared state between all states. The static config goes into
`AgentConfig` and the mutable shared state into `AgentState`.

* Replace all use of `Default` for `new`.

Deriving or implementing `Default` makes for a secondary instantiation
API.  It is useful in some cases, but gets very confusing when there
is both `new` _and_ a `Default`. It's especially devious for derived
values where a reasonable default is not `0`, `false` or `None`.

* Remove feature native_tls, we want only native rustls.

This feature made for very clunky handling throughout the code. From a
security point of view, it's better to stick with one single TLS API.
Rustls recently got an official audit (very positive).

https://github.com/ctz/rustls/tree/master/audit

Rustls deliberately omits support for older, insecure TLS such as TLS
1.1 or RC4. This might be a problem for a user of ureq, but on balance
not considered important enough to keep native_tls.

* Remove auth and support for basic auth.

The API just wasn't enough. A future reintroduction should at least
also provide a `Bearer` mechanism and possibly more.

* Rename jar -> cookie_store
* Rename jar -> cookie_tin

Just make some field names sync up with the type.

* Drop "cookies" as default feature

The need for handling cookies is probably rare, let's not enable it by
default.

* Change all feature checks for "cookie" to "cookies"

The outward facing feature is "cookies" and I think it's better form
that the code uses the official feature name instead of the optional
library "cookies".

* Keep `set` on Agent level as well as AgentBuilder.

The idea is that an auth exchange might result in a header that need
to be set _after_ the agent has been built.
This commit is contained in:
Martin Algesten
2020-10-25 11:08:50 +01:00
parent 703ca41960
commit 1369c32351
24 changed files with 398 additions and 647 deletions

View File

@@ -5,14 +5,14 @@ use log::{debug, info};
use qstring::QString;
use url::Url;
#[cfg(feature = "cookie")]
#[cfg(feature = "cookies")]
use cookie::Cookie;
use crate::body::{self, BodySize, Payload, SizedReader};
use crate::header;
use crate::resolve::ArcResolver;
use crate::stream::{self, connect_test, Stream};
#[cfg(feature = "cookie")]
#[cfg(feature = "cookies")]
use crate::Agent;
use crate::{Error, Header, Request, Response};
@@ -81,7 +81,7 @@ impl Unit {
extra.push(Header::new("Authorization", &format!("Basic {}", encoded)));
}
#[cfg(feature = "cookie")]
#[cfg(feature = "cookies")]
extra.extend(extract_cookies(&req.agent, &url).into_iter());
extra
@@ -94,7 +94,7 @@ impl Unit {
.cloned()
.collect();
let deadline = match req.timeout {
let deadline = match req.agent.config.timeout {
None => None,
Some(timeout) => {
let now = time::Instant::now();
@@ -203,12 +203,12 @@ pub(crate) fn connect(
};
// squirrel away cookies
#[cfg(feature = "cookie")]
#[cfg(feature = "cookies")]
save_cookies(&unit, &resp);
// handle redirects
if resp.redirect() && req.redirects > 0 {
if redirect_count == req.redirects {
if resp.redirect() && req.agent.config.redirects > 0 {
if redirect_count == req.agent.config.redirects {
return Err(Error::TooManyRedirects);
}
@@ -255,11 +255,11 @@ pub(crate) fn connect(
Ok(resp)
}
#[cfg(feature = "cookie")]
#[cfg(feature = "cookies")]
fn extract_cookies(agent: &Agent, url: &Url) -> Option<Header> {
let header_value = agent
.state
.jar
.cookie_tin
.get_request_cookies(url)
.iter()
.map(|c| c.encoded().to_string())
@@ -295,7 +295,7 @@ fn connect_socket(unit: &Unit, hostname: &str, use_pooled: bool) -> Result<(Stre
while let Some(stream) = agent
.state
.pool
.try_get_connection(&unit.url, &unit.req.proxy)
.try_get_connection(&unit.url, unit.req.agent.config.proxy.clone())
{
let server_closed = stream.server_closed()?;
if !server_closed {
@@ -379,7 +379,7 @@ fn send_prelude(unit: &Unit, stream: &mut Stream, redir: bool) -> io::Result<()>
}
/// Investigate a response for "Set-Cookie" headers.
#[cfg(feature = "cookie")]
#[cfg(feature = "cookies")]
fn save_cookies(unit: &Unit, resp: &Response) {
//
@@ -397,7 +397,7 @@ fn save_cookies(unit: &Unit, resp: &Response) {
unit.req
.agent
.state
.jar
.cookie_tin
.store_response_cookies(cookies, &unit.url.clone());
}
@@ -411,13 +411,13 @@ mod tests {
#[test]
fn match_cookies_returns_one_header() {
let agent = Agent::default();
let agent = Agent::new();
let url: Url = "https://crates.io/".parse().unwrap();
let cookie1: Cookie = "cookie1=value1; Domain=crates.io; Path=/".parse().unwrap();
let cookie2: Cookie = "cookie2=value2; Domain=crates.io; Path=/".parse().unwrap();
agent
.state
.jar
.cookie_tin
.store_response_cookies(vec![cookie1, cookie2].into_iter(), &url);
// There's no guarantee to the order in which cookies are defined.