Commit Graph

652 Commits

Author SHA1 Message Date
Martin Algesten
f652a9e449 rename error_for_status -> error_on_non_2xx 2020-11-15 21:13:45 +01:00
Martin Algesten
d4af920b53 Fix body send doc tests 2020-11-15 21:10:19 +01:00
Jacob Hoffman-Andrews
26145810bf Add some more updates. 2020-11-15 09:14:19 +01:00
Jacob Hoffman-Andrews
daa63d3bc6 Update some more doctests.
I missed these in my previous doctest PR.

The doctests all now run without accessing the network. Tested by
turning off networking and running them.

Request.call's doctest wouldn't run because it relied on making a custom
AgentBuilder and building it, which bypasses the test_agent. I concluded
that this doctest was mostly illustrating behavior of AgentBuilder, not
call(), and simplified it to be more like the other calling methods on
request.
2020-11-15 09:14:19 +01:00
Jacob Hoffman-Andrews
203573d27c Implement more realistic doctests. (#222)
Add is_test and fn main headers to various doctests, and use real
URLs along with the ? operator.
2020-11-14 09:53:15 -08:00
Jacob Hoffman-Andrews
ec8dace1af Turn Unit into a built Request (#223)
This involved removing the Request reference from Unit, and adding an
Agent, a method, and headers.

Also, move is_retryable to Unit.
2020-11-14 01:12:01 -08:00
Jacob Hoffman-Andrews
acc36ac370 Add support for using testserver in doctests. (#218)
Doctests run against a normally-built copy of the crate, i.e. one
without #[cfg(test)] set, so we can't use the conditional compilation
feature.

Instead, define a static var that indicates whether the library is
running in test mode or not. For each doctest, insert a hidden call that
sets this var to true. Then, when ureq::agent() is called, it returns a
test_agent instead.

This required moving testserver out of the test mod and into src/, so
that it can be included unconditionally (i.e. when cfg(test) is false).

This PR converts one doctest as an example. If we land this PR, I'll
send a followup to convert the rest.
2020-11-13 10:40:16 -08:00
Jacob Hoffman-Andrews
a0b901f35b Remove qstring dependency. (#221)
Instead, rely on Url's built-in query parameter handling. A Request now
accumulates a list of query param pairs, and joins them with a parsed
URL at the time do_call is called.

In the process, remove some getters that rely on parsing the URL.
Adapting these getters was going to be awkward, and they mostly
duplicate things people can readily get by parsing the URL.
2020-11-13 00:02:52 -08:00
Jacob Hoffman-Andrews
920eccf37a Remove proxy method on request (#220) 2020-11-12 23:25:05 -08:00
Jacob Hoffman-Andrews
c2d4e527a9 1.5.1 2020-11-08 20:58:11 -08:00
Jacob Hoffman-Andrews
310c14d601 Update CHANGELOG for 1.5.2. (#216) 2020-11-08 20:40:09 -08:00
Jacob Hoffman-Andrews
d6f31ebb52 Remove some dev-dependencies. (#200)
We don't really need rayon for the parallelism in smoke-test.

Also, now that we use log, smoke-test can just use that.
2020-11-08 11:54:25 -08:00
Joshua Nelson
acaf449a4c Allow getting the error from a response (#214)
This is an alternative to https://github.com/algesten/ureq/pull/132
which does not require breaking changes.

Closes https://github.com/algesten/ureq/issues/126.
2020-11-08 11:13:03 -08:00
Martin Algesten
b66e618198 Gate entire mod cookies on "cookies" feature 2020-11-08 11:31:49 +01:00
Martin Algesten
d0789ae8e9 Rename AgentBuilder set_tls_config -> tls_config
For consistency. We don't use set_xxx on the builder.
2020-11-08 11:31:49 +01:00
Martin Algesten
a8ee8ab75e Provide AgentBuilder::cookie_store setter 2020-11-08 11:31:49 +01:00
Martin Algesten
c2bbd57acc Agent::cookie_store to persist cookies 2020-11-08 11:31:49 +01:00
Martin Algesten
18201a08c5 Remove Agent::set_cookie
Preloading an agent with cookies can be done by providing a prepared
cookie store using `AgentBuilder`. At this point, we don't want direct
state mutation on the `Agent`, so this fn goes away.

Context:
https://github.com/algesten/ureq/issues/203#issuecomment-716385310
2020-11-08 11:31:49 +01:00
Martin Algesten
e37acc85b2 Remove AgentBuilder::set headers
The headers are not scoped by host, which means using them for
something like `Authorization` would effectively "leak" to all
requests using the agent.

Context:
https://github.com/algesten/ureq/issues/203#issuecomment-716385310
2020-11-08 11:31:49 +01:00
Jacob Hoffman-Andrews
d83de53bcd Restore HEAD and DELETE
Also, remove OPTIONS, TRACE, and PATCH from lib.rs.
2020-10-30 05:49:01 +01:00
Jacob Hoffman-Andrews
aae05c5614 Agent tweaks for 2.0
Replace `this` idiom with `mut self`.

Move idle connections constants from pool.rs to agent.rs.

Remove Agent.set and some convenience request methods
(leaving get, post, and put).

Move max_idle_connections setting from AgentConfig to AgentBuilder
(since the builder passes these to ConnectionPool and the Agent
doesn't subsequently need them).

Eliminate duplicate copy of proxy in AgentState; use the one in
AgentConfig.
2020-10-30 05:49:01 +01:00
Jacob Hoffman-Andrews
e3bf7c73f3 Replace serde_macros.rs with pub use. (#212) 2020-10-29 09:43:02 -07:00
Jacob Hoffman-Andrews
9484d86584 Merge branch 'release-2.0' into rm-rayon 2020-10-29 00:18:43 -07:00
Jacob Hoffman-Andrews
73af61132e Merge branch 'master' into release-2.0 2020-10-28 23:00:01 -07:00
Jacob Hoffman-Andrews
4a304fe937 Merge pull request #206 from jsha/merge-master-into-release-2.0
Merge master into release 2.0
2020-10-25 16:46:32 -07:00
Jacob Hoffman-Andrews
6781122b46 Merge pull request #205 from steffahn/generalize_send_reader_lifetime
Make Request::send more general.
2020-10-25 16:46:13 -07:00
Jacob Hoffman-Andrews
632825314c Merge pull request #207 from jsha/import-paths-tweaks
Tweak import paths
2020-10-25 16:45:36 -07:00
Jacob Hoffman-Andrews
17ab5110a3 Use lifetimes for more elements of Payload
Text and Bytes can both have their lifetimes parameterized.
2020-10-25 15:52:02 -07:00
Jacob Hoffman-Andrews
72e7e06334 Tweak import paths
Group together all cookie imports in agent.rs; consistently use `Duration`.
2020-10-25 15:10:03 -07:00
Jacob Hoffman-Andrews
a52c6021cf Merge branch 'master' into release-2.0 2020-10-25 14:07:32 -07:00
Frank Steffahn
c8cd130770 Make Request::send more general.
Removes `+ 'static` constraint from the `impl Read` parameter.
For this, lifetime parameters are added to `Payload` and `SizedReader`.
2020-10-25 21:51:04 +01:00
Jacob Hoffman-Andrews
0cfa06c80c API changes for 2.0 (#201)
* 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.

* Change ordering on TestServer's atomic bool to SeqCst
2020-10-25 11:40:10 -07:00
Martin Algesten
1369c32351 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.
2020-10-25 11:47:38 +01:00
Jacob Hoffman-Andrews
304981377b Restore serde dev_dependency. 2020-10-24 10:04:55 -07:00
Jacob Hoffman-Andrews
22e3839340 Review feedback. 2020-10-24 12:12:07 +02:00
Jacob Hoffman-Andrews
2bf9362eff Reinstate read timeouts on body.
This feature was broken in #67, which reset timeouts on the
stream before passing it to set_stream.

As part of this change, refactor the internal storage of
timeouts on the Request object to use Option<Duration>.

Remove the deadline field on Response. It wasn't used. The
deadline field on unit was used instead.

Add a unittest.
2020-10-24 12:12:07 +02:00
Jacob Hoffman-Andrews
32f9ebc04a Separate overall_timeout checks. 2020-10-24 12:08:27 +02:00
Jacob Hoffman-Andrews
1f5f65877a Update overall_timeout_during_headers test. 2020-10-24 12:08:27 +02:00
Jacob Hoffman-Andrews
14475cb5c7 Add test for read_timeout during headers. 2020-10-24 12:08:27 +02:00
Jacob Hoffman-Andrews
67c28d28a3 Check for synthetic_error early in unit::connect 2020-10-24 12:08:27 +02:00
Jacob Hoffman-Andrews
1c5dcc0096 Remove some dev-dependencies.
We don't really need rayon for the parallelism in smoke-test.

Also, now that we use log, smoke-test can just use that.
2020-10-22 22:55:16 -07:00
Jacob Hoffman-Andrews
703ca41960 Push mutexes down into pool and cookie store. (#193)
Previously, Agent stored most of its state in one big
Arc<Mutex<AgentState>>. This separates the Arc from the Mutexes.
Now, Agent is a thin wrapper around an Arc<AgentState>. The individual
components that need locking, ConnectionPool and CookieStore, now are
responsible for their own locking.

There were a couple of reasons for this. Internal components that needed
an Agent were often instead carrying around an Arc<Mutex<AgentState>>.
This felt like the components were too intertwined: those other
components shouldn't have to care quite so much about how Agent is
implemented. Also, this led to compromises of convenience: the Proxy on
Agent wound up stored inside the `Arc<Mutex<AgentState>>` even though it
didn't need locking. It was more convenient that way because that was
what Request and Unit had access too.

The other reason to push things down like this is that it can reduce
lock contention. Mutations to the cookie store don't need to lock the
connection pool, and vice versa. This was a secondary concern, since I
haven't actually profiled these things and found them to be a problem,
but it's a happy result of the refactoring.

Now all the components outside of Agent take an Agent instead of
AgentState.

In the process I removed `Agent.cookie()`. Its API was hard to use
correctly, since it didn't distinguish between cookies on different
hosts. And it would have required updates as part of this refactoring.
I'm open to reinstating some similar functionality with a refreshed API.

I kept `Agent.set_cookie`, but updated its method signature to take a
URL as well as a cookie.

Many of ConnectionPool's methods went from `&mut self` to `&self`,
because ConnectionPool is now using interior mutability.
2020-10-20 00:03:45 -07:00
Jacob Hoffman-Andrews
75bc803cf1 Use a testserver in tests. (#194)
This is a step towards allowing our tests to run without network access,
which will make them more resilient and faster.

Replace the URL in one instance of an HTTPS test that didn't need HTTPS.
2020-10-19 00:27:40 -07:00
dependabot-preview[bot]
bd2761e280 Update env_logger requirement from 0.7.1 to 0.8.1
Updates the requirements on [env_logger](https://github.com/env-logger-rs/env_logger) to permit the latest version.
- [Release notes](https://github.com/env-logger-rs/env_logger/releases)
- [Changelog](https://github.com/env-logger-rs/env_logger/blob/master/CHANGELOG.md)
- [Commits](https://github.com/env-logger-rs/env_logger/compare/v0.7.1...v0.8.1)

Signed-off-by: dependabot-preview[bot] <support@dependabot.com>
2020-10-19 09:11:35 +02:00
Martin Algesten
6e997909bf Document release process 2020-10-19 01:18:56 +02:00
Jacob Hoffman-Andrews
30162bf3bb Move Agent construction to new AgentBuilder.
In the process, rename set_foo methods to just foo, since methods on the
builder will always be setters.

Adds a new() method on ConnectionPool so it can be constructed directly
with the desired limits. Removes the setter methods on ConnectionPool
for those limits. This means that connection limits can only be set when
an Agent is built.

There were two tests that verify Send and Sync implementations, one for
Agent and one for Request. This PR moves the Request test to request.rs,
and changes both tests to more directly verify the traits. There may be
another way to do this, I'm not sure.
2020-10-18 12:12:07 +02:00
Jacob Hoffman-Andrews
0f083a4436 Add jsha to Authors list.
Per suggestion by Martin. :-)
2020-10-18 11:30:38 +02:00
Jacob Hoffman-Andrews
044f25b02a Add more header validation (#188)
This adds validation of header values on receive, and of both header
names and header values on send. This doesn't change the return
type of set to be a Result, it just validates when the request is
sent. Also removes the section in the README describing handling
of invalid headers, and updates a test that verified acceptance of
non-ASCII headers so that it verifies rejection of them instead.
2020-10-17 17:59:29 -07:00
Jacob Hoffman-Andrews
e36c1c2aa1 Switch to Result-based API. (#132)
Gets rid of synthetic_error, and makes the various send_* methods return `Result<Response, Error>`.
Introduces a new error type "HTTP", which represents an error due to status codes 4xx or 5xx.
The HTTP error type contains a boxed Response, so users can read the actual response if they want.
Adds an `error_for_status` setting to disable the functionality of treating 4xx and 5xx as errors.
Adds .unwrap() to a lot of tests.

Fixes #128.
2020-10-17 00:40:48 -07:00
Jacob Hoffman-Andrews
257d4e54dd Switch timeout APIs to use Duration. 2020-10-17 09:23:01 +02:00