Commit Graph
100 Commits
Author SHA1 Message Date
Jacob Hoffman-Andrews 8a32cae507 Remove sync_wrapper dep in favor of Mutex (#514)
We unwrap the stream exactly once per response, and we know that case
will be uncontended for the same reason `SyncWrapper` works:
`into_reader()` takes `self`, so it must have exclusive ownership.
Uncontended mutexes are extremely cheap. This saves us a dependency
at a trivial performance cost.
2022-05-09 10:32:23 -07:00
Jacob Hoffman-Andrews 4bb6d3a4db DeadlineStream: read from Stream's buffer first (#508)
This fixes an issue where requests with a deadline would do a syscall
on every read, rather than pulling from the buffer. Fixes a problem
reported in #506 where `resp.into_json()` was unnecessarily slow.
2022-05-09 10:31:59 -07:00
Jacob Hoffman-Andrews 101467f13f Return stream to pool on exact read (#509)
If the user reads exactly the number of bytes in the response, then
drops the Read object, streams will never get returned to the pool since
the user never triggered a read past the end of the LimitedRead.

This fixes that by making PoolReturnRead aware of the level below it, so
it can ask if a stream is "done" without actually doing a read.

Also, a refactorign:

Previously, Response contained an Option<Box<Unit>> because the testing
method `from_str()` would construct a Response with no associated Unit.
However, this increased code complexity with no corresponding test
benefit. Instead, construct a fake Unit in from_str().

Also, instead of taking an `Option<Box<Unit>>`, PoolReturnRead now takes
a URL (to figure out host and port for the PoolKey) and an &Agent where
it will return the stream. This cuts interconnectedness somewhat:
PoolReturnRead doesn't need to know about Unit anymore.
2022-04-30 17:22:27 -07:00
Jacob Hoffman-Andrews 4d77d365a0 Improve unittest 2022-04-30 14:55:07 +02:00
Jacob Hoffman-Andrews ec8095be49 Fix Debug impl for AgentConfig
Wrap Arc<dyn TlsConnector> in a newtype with a stub Debug impl so we can
go back to `#[derive(Debug)]` on AgentConfig.
2022-04-30 14:55:07 +02:00
Jacob Hoffman-Andrews c55ac31087 Fix quoting in test runner 2021-12-20 09:20:52 +01:00
Jacob Hoffman-Andrews 352bf19f40 Fix doctests 2021-12-20 09:19:25 +01:00
Jacob Hoffman-Andrews 2df70168c4 Use ? instead of unwrap in examples (#458)
This is recommended by the Rust API Guidelines:

https://rust-lang.github.io/api-guidelines/documentation.html#examples-use--not-try-not-unwrap-c-question-mark

One exception: When we need to unwrap an Option, the examples still use
.unwrap(). The alternative would be something like
`.ok_or(SomeErrorHere)?`, which feels like an awkward way to deal with
an Option. This might get better with NoneError:

https://docs.rs/rustc-std-workspace-std/1.0.1/std/option/struct.NoneError.html

I also rearranged some examples that used turbofish to use type
annotations. I think type annotations are more familiar to early Rust
users (and they use fewer characters and less punctuation, which is
always nice).
2021-12-19 20:04:30 -08:00
Jacob Hoffman-Andrews 2563df4f62 Bump rustls to >=0.20.1
Without this we get a build error because we depend on an impl of Error
on InvalidDnsNameError.
2021-12-20 02:39:38 +01:00
Jacob Hoffman-Andrews 56276c3742 Add support for alternate TLs implementations. 2021-12-17 17:47:30 +01:00
Jacob Hoffman-Andrews 94a8040706 Prepare for release 2.3.1 (#437) 2021-11-14 09:55:55 -08:00
Jacob Hoffman-Andrews 9587a6cc8e Update CHANGELOG and Cargo.toml for 2.3.0 (#431) 2021-10-21 11:39:43 -07:00
Jacob Hoffman-Andrews f22e67be4c Fix agent test. 2021-10-21 07:38:59 +02:00
Jacob Hoffman-Andrews 5ae55ad5f2 Match versions of rustls 2021-10-21 07:38:59 +02:00
Jacob Hoffman-Andrews 848eb7be38 Match versions of rustls 2021-10-21 07:38:59 +02:00
Jacob Hoffman-Andrews 3bc7d8336b Fix lint 2021-10-21 07:38:59 +02:00
Jacob Hoffman-Andrews 5fa912c4d3 Update to rustls 0.20, webpki 0.22 2021-10-21 07:38:59 +02:00
Jacob Hoffman-AndrewsandMichael Richardson 535bc4b4cb Remove two unused fields. (#425)
Co-authored-by: Michael Richardson <mcr@sandelman.ca>
2021-10-02 01:04:36 -07:00
Jacob Hoffman-Andrews c0d7157ae7 Update CHANGELOG and Cargo.toml for 2.1.1 (#376) 2021-04-26 11:00:53 -07:00
Jacob Hoffman-Andrews 50d9ccff8c Don't reuse conns with bytes pending from server (#372)
This makes us less likely to try and reuse a closed connection, which
produces problems in particular for requests that can't be retried.

Fixes #361
Fixes #124
2021-04-18 10:46:20 -07:00
Jacob Hoffman-Andrews 5e4b37a393 Add more parameters to cureq example (#371) 2021-04-17 12:04:44 -07:00
Jacob Hoffman-Andrews 3044ae7efd Add size limits on responses (#351)
This prevents a malicious server from running the client out of memory
by returning a very large body.

Limit into_string() to 1 megabyte.
Limit individual header fields to 100 kilobytes.
Limit number of header fields to 100.

Part of #267
2021-03-24 13:28:23 -07:00
Jacob Hoffman-Andrews 9ec4e7192a Add -k option to cureq example (#342)
By analogy with curl, this turns off certificate verification. Requires
enabling the "dangerous_configuration" feature in the rustls dependency.
2021-03-23 17:00:32 -07:00
Jacob Hoffman-Andrews a34d450657 Remove specialized error message for complete_io. (#349)
This was triggering for errors like InvalidCertificate and giving the
message "Sometimes this means the host doesn't support any of the same
ciphersuites as rustls, or doesn't support TLS 1.2 and above," which is
confusing in that situation.

For now, offer no specialized error. I'd like to come back when I find
more time and restore this, but only for the error cases where it's
useful.
2021-03-23 17:00:09 -07:00
Jacob Hoffman-Andrews b80f12e384 Offer separate error during handshakes. (#331)
It's useful to know that an error was specific to the TLS handshake,
versus the TCP connect, or a later stage of a request.
2021-02-21 14:26:25 -08:00
Jacob Hoffman-Andrews 6f86ee7f93 Add example "cureq". (#330)
Contrary to smoke-test, this takes full URLs on the commandline and
prints their contents to stdout. This makes it easier to test behavior
with specific URLs. I hope to later add flags for various behaviors like
printing headers, following redirects, enabling / disabling cookies,
and verbose output.

Also add a useful debug line when receiving a cookie header.
2021-02-21 14:26:12 -08:00
Jacob Hoffman-Andrews 413e8bf4aa Fix imports 2021-02-21 13:32:42 -08:00
Jacob Hoffman-Andrews 671f24ab49 Offer separate error during handshakes.
It's useful to know that an error was specific to the TLS handshake,
versus the TCP connect, or a later stage of a request.
2021-02-21 13:20:38 -08:00
Jacob Hoffman-Andrews e411a629bd Changelog for 2.0.2 2021-02-21 09:00:33 +01:00
Jacob Hoffman-Andrews b246f0a9d2 Apply deadline across redirects. (#313)
Previously, each redirect could take timeout time, so a series of slow
redirects could run for longer than expected, or indefinitely.
2021-02-07 12:29:35 -08:00
Jacob Hoffman-Andrews 5999421d84 Check cargo fmt in CI. 2021-01-17 21:56:28 +01:00
Jacob Hoffman-Andrews 0b5db55378 Bump to 2.0.1. (#289)
Update CHANGELOG.md and Cargo.toml
2021-01-10 13:34:45 -08:00
Jacob Hoffman-Andrews 1584690c95 Update CHANGELOG. 2021-01-10 13:28:07 -08:00
Jacob Hoffman-Andrews fff4c8355e Merge branch 'main' of github.com:algesten/ureq into changelog-2.0.1 2021-01-10 13:26:54 -08:00
Jacob Hoffman-Andrews 932c180d1d Response: Use ErrorKind::UnexpectedEof for "premature close" (#293)
In a client application, we're explicitly trying to differentiate "invalid data"
scenarios from "broken transfer".
2021-01-10 13:25:42 -08:00
Jacob Hoffman-Andrews 96985956aa Bump to 2.0.1.
Brings in a fix to 308 redirects.
2021-01-03 17:52:38 -08:00
Jacob Hoffman-Andrews 6126a2fe50 Merge pull request #288 from jyn514/308
Fix 307/308 redirects (again, for 2.0).

Also add some improvements to registration of test handlers, and add env_logger support to tests.
2021-01-03 17:45:05 -08:00
Jacob Hoffman-Andrews 6390558415 Update CHANGELOG for 2.0.0. 2021-01-03 21:07:09 +01:00
Jacob Hoffman-Andrews 234eb5572f Update CHANGELOG and Cargo.toml for 2.0.0-rc4 (#279) 2020-12-19 12:11:38 -08:00
Jacob Hoffman-Andrews de1805190e Do more validation on status line. (#266)
Status code must be exactly three digits.
HTTP version must be "HTTP/", digit, dot, digit.

https://tools.ietf.org/html/rfc7230#section-3.1.2
status-line = HTTP-version SP status-code SP reason-phrase CRLF
2020-12-18 22:18:25 -08:00
Jacob Hoffman-Andrews 243b987110 Remove error_on_non_2xx. (#272)
After the recent changes in #257, it's probably not necessary. It's now
quite easy to use a match statement to extract responses for certain
status codes, or all status codes.

Add documentation on how to turn a status code error back into a
Response.
2020-12-18 22:10:55 -08:00
Jacob Hoffman-Andrews e25a6876f9 Document error handling. 2020-12-18 22:04:00 -08:00
Jacob Hoffman-Andrews 8cb4f401e3 Add history to response objects (#275)
This allows Error to report both the URL that caused an error, and the
original URL that was requested.

Change unit::connect to use the Response history for tracking number of
redirects, instead of passing the count as a separate parameter.

Incidentally, move handling of the `stream` fully inside `Response`.
Instead of `do_from_read` + `set_stream`, we now have `do_from_stream`,
which takes ownership of the stream and keeps it. We also have
`do_from_request`, which does all of `do_from_stream`, but also sets the
`previous` field.
2020-12-13 11:59:11 -08:00
Jacob Hoffman-Andrews 10baf7c051 Clarify example from README. (#277)
We should include the `fn main() -> Result` part, because people may not
know they need the `-> Result` in order for `?` to work.

Also, I replaced `Accept: text/html` with `Example-Header: header
value`. My thinking is that if someone doesn't know they need to remove
that Accept for non-HTML URLs, they might get unexpected results.
2020-12-12 10:33:30 -08:00
Jacob Hoffman-Andrews 1448969dd7 Increment version to 2.0.0-rc3 2020-12-12 09:27:50 -08:00
Jacob Hoffman-Andrews f8e0e15376 Changelog for 2.0.0-rc3 (#273) 2020-12-12 09:26:53 -08:00
Jacob Hoffman-Andrews 60d4967a16 Remove changelog entry about error_on_non_2xx 2020-12-12 09:25:38 -08:00
Jacob Hoffman-Andrews b9e3d3e76c Fix test 2020-12-06 14:55:55 -08:00
Jacob Hoffman-Andrews ac93fa7e18 Remove references to error_on_non_2xx 2020-12-05 20:32:30 -08:00
Jacob Hoffman-Andrews 06760d5961 Changelog for 2.0.0-rc3 2020-12-05 14:10:21 -08:00
Jacob Hoffman-Andrews 9e270c77e8 Remove error_on_non_2xx.
After the recent changes in #257, it's probably not necessary. It's now
quite easy to use a match statement to extract responses for certain
status codes, or all status codes.
2020-12-05 14:05:15 -08:00
Jacob Hoffman-Andrews ec04eef7bc Fix up error docs. (#270) 2020-12-05 13:15:33 -08:00
Jacob Hoffman-Andrews 6c9378ce37 De-redundantize Error kinds. (#259)
Change "Bad" to "Invalid" in error names, mimicking io::Error::ErrorKind.

Change InvalidProxyCreds to ProxyUnauthorized.

Change DnsFailed to just Dns (the fact that there was a failure is implicit
in the fact that this was an error).
2020-12-05 12:05:29 -08:00
Jacob Hoffman-Andrews 57f251d766 Remove Response::status_line. (#263)
The necessary functionality is available via status_text, and removing
this method means we have more flexibility in our internal storage.
2020-12-05 12:01:56 -08:00
Jacob Hoffman-Andrews c3a6f50dbe Remove status methods on Response. (#258)
Now that Responses with non-2xx statuses get turned into `Error`,
there is less need for these. Also, surveying the set of public crates
that depend on ureq, none of them use these methods. It seems that
users tend to prefer checking the status code directly.

Here is my thinking on each of these individually:

.ok() -- With the new Result API, any Request you get back will be
.ok(). Also, I think the name .ok() is a little confusing with
Result::ok().

.error() - with the new Result API, this is an exact overlap with
anything that would return Error. People will just check for whether a
Result is Err(...) rather than call .error().

.client_error() - most of the time, if someone wants to specially handle
a 4xx error, they want to handle specific ones, because the response to
them is different. For instance a specialized response to a 404 would be
"delete this from the list of URLs to check in the future," where a
specialized response to a 401 would be "try and load updated
credentials." For instance:

https://github.com/msfjarvis/healthchecks-rs/blob/4200edb9ed73bed56a05d81cdd3b0a65badc7ede/healthchecks/src/manage.rs#L70-L84

https://github.com/SirWindfield/cargo-free/blob/75d4b363b63fa2b6f3ac12a196a2cb80f0afc848/src/lib.rs#L59-L63

https://github.com/lukehsiao/netlify-ddns-rs/blob/1d7daea38bf78d9f1bbfba9d38ea1863ed70ed1d/src/netlify.rs#L101-L112

.server_error() - I don't have as much objection to this one, since it's
reasonable to want to treat all server errors (500, 502, 503) more or
less the same. Although even at that, 501 Not Implemented seems like
people would want to handle it differently. I guess that doesn't come up
much in practice - I've never seen a 501 in the wild.

.redirect() - Usually redirects are handled under the hood, unless
someone disables automatic redirect handling. I'm not terribly opposed
to this one, but given that no-one's using it and it's just as easy to
do 300..399.contains(resp.status()), I'm mildly inclined towards
deletion.
2020-12-05 11:32:25 -08:00
Jacob Hoffman-Andrews be6be7e600 Fix test 2020-12-05 15:29:11 +01:00
Jacob Hoffman-Andrews 18a9b08973 Revert deletions of client_error and friends. 2020-12-05 15:29:11 +01:00
Jacob Hoffman-Andrews 64ebd47979 Add nicer doctest; revert smoke-test. 2020-12-05 15:29:11 +01:00
Jacob Hoffman-Andrews 5a55f94101 Improve smoke-test.rs 2020-12-05 15:29:11 +01:00
Jacob Hoffman-Andrews 814b10ceaf Update smoke-test. 2020-12-05 15:29:11 +01:00
Jacob Hoffman-Andrews 219185f73f Make Error an enum again. 2020-12-05 15:29:11 +01:00
Jacob Hoffman-Andrews b20c4fc3be Revert diff 2020-12-05 15:29:11 +01:00
Jacob Hoffman-Andrews 3cd160279f Revert diff 2020-12-05 15:29:11 +01:00
Jacob Hoffman-Andrews f9378ab089 Replace Error::status and Error::into_response with http. 2020-12-05 15:29:11 +01:00
Jacob Hoffman-Andrews d267dffde1 Snapshow of match variant. 2020-12-05 15:29:11 +01:00
Jacob Hoffman-Andrews 33066fb074 Improve doctest comments. 2020-12-05 15:29:11 +01:00
Jacob Hoffman-Andrews 2167cf2481 Interlink docs 2020-12-05 15:29:11 +01:00
Jacob Hoffman-Andrews 71a7e3298d Add documentation for handling statuses. 2020-12-05 15:29:11 +01:00
Jacob Hoffman-Andrews 4c3b93d86d Add Error::{kind, status, into_response}.
Also, remove Response::{ok, error, client_error, server_error,
redirect}. The idea is that you would access these through the
Error object instead.

I fetched all the reverse dependencies of ureq on crates.io and looked
for uses of the methods being removed. I found none.

I'm also considering removing the error_on_non_2xx method entirely. If
it's easy to get the underlying response for errors, it would be nice to
make that the single way to do things rather than support two separate
ways of handling HTTP errors.
2020-12-05 15:29:11 +01:00
Jacob Hoffman-Andrews 35c03521b9 Add debug logs for stream pooling. 2020-12-05 15:05:20 +01:00
Jacob Hoffman-Andrews 7afc4b04f3 Use BufRead in more places (#261)
This moves Stream's enum into an `Inner` enum, and wraps a single
BufReader around the whole thing. This makes it easier to consistently
treat the contents of Stream as being wrapped in a BufReader.

Also, implement BufRead for DeadlineStream. This means when a timeout
is set, we don't have to set that timeout on the socket with every
small read, just when we fill up the buffer. This reduces the number
of syscalls.

Remove the `Cursor` variant from Stream. It was strictly less powerful
than that `Test` variant, so I've replaced the handful of Cursor uses
with `Test`. Because some of those cases weren't test, I removed the
`#[cfg(test)]` param on the `Test` variant.

Now that all inputs to `do_from_read` are `impl BufRead`, add that
as a type constraint. Change `read_next_line` to take advantage of
`BufRead::read_line`, which may be somewhat faster (though I haven't
benchmarked).
2020-11-30 09:23:19 -08:00
Jacob Hoffman-Andrews 36b307423c Fix non-tls case correctly. 2020-11-29 00:02:37 -08:00
Jacob Hoffman-Andrews 6b6a59f215 Fix non-tls case. 2020-11-28 23:57:02 -08:00
Jacob Hoffman-Andrews a286a7a22d Make DeadlineStream Read use the BufRead. 2020-11-28 23:33:28 -08:00
Jacob Hoffman-Andrews 6a22c54ba2 Small cleanups. 2020-11-28 22:34:32 -08:00
Jacob Hoffman-Andrews 50cb5cecd1 Fix buffered DeadlineStream 2020-11-28 17:47:17 -08:00
Jacob Hoffman-Andrews 131a0264d1 Move BufReader up the stack in Stream.
Stream now has an `Inner` enum, and wraps an instance of that enum in a
BufReader. This allows Stream itself to implement BufRead trivially, and
simplify some of the match dispatching. Having Stream implement BufRead
means we can make use of `read_line` instead of our own `read_next_line`
(not done in this PR yet).

Also, removes the `Cursor` variant of the Inner enum in favor of using
the `Test` variant everywhere, since it's strictly more powerful.
2020-11-28 12:04:28 -08:00
Jacob Hoffman-Andrews a0b88926fa Remove a keyword from Cargo.toml. (#250)
Crates has a max of 5 keywords.
2020-11-23 00:02:01 -08:00
Jacob Hoffman-Andrews f944764ee0 Merge pull request #249 from jsha/merge-master-into-release-2.0a
Merge master into release 2.0a
2020-11-22 23:47:02 -08:00
Jacob Hoffman-Andrews 0e5cb2d018 Merge branch 'master' into merge-master-into-release-2.0a 2020-11-22 23:38:53 -08:00
Jacob Hoffman-Andrews 3d0cb33596 Add URL to Error in non-2xx case. (#243) 2020-11-22 23:36:45 -08:00
Jacob Hoffman-Andrews de4aab7472 Tweak CHANGELOG a little bit. (#242)
Mention cloning Agent, default timeout, and removal of Request::build.
2020-11-22 23:36:25 -08:00
Jacob Hoffman-Andrews 61a6d1c5b1 Remove commented-out timeout_during_headers test. (#244)
We wound up with a second, working copy of this test at some point after
merging master into release-2.0.
2020-11-22 23:36:05 -08:00
Jacob Hoffman-Andrews 1ec8674e88 Merge pull request #241 from jsha/changelog-2.0
Update CHANGELOG, Cargo.toml, and docs for 2.0.
2020-11-22 13:27:01 -08:00
Jacob Hoffman-Andrews feec10b0a7 Merge pull request #240 from jsha/merge-master-into-release-2.0
Merge master into release 2.0
2020-11-22 13:26:29 -08:00
Jacob Hoffman-Andrews c915b236ae Fix is_connection_closed. (#238)
Fixes #237.
2020-11-22 12:56:58 -08:00
Jacob Hoffman-Andrews cc7ce757bd Update CHANGELOG, Cargo.toml, and docs for 2.0. 2020-11-22 11:09:32 -08:00
Jacob Hoffman-Andrews 1c41da87f0 Merge branch 'master' into merge-master-into-release-2.0 2020-11-22 10:25:13 -08:00
Jacob Hoffman-Andrews 96f6ed15d7 Remove "what" section of test matrix. (#239)
We were generating two whole sets of jobs, for --tests and --docs
separately. But the default invocation of cargo runs both tests and
doctests. We can save some resources and speed up our test runs by
allowing cargo to do both in one invocation.
2020-11-22 00:23:59 -08:00
Jacob Hoffman-Andrews 6a7b064f2a Remove Headers from the public API. (#224)
It turns out Headers is actually an internal-only API. None of the
user-facing types use it.

Unfortunately, making it unexported also required deleting the doctests,
since doctests can only run against a public interface.
2020-11-22 00:15:13 -08:00
Jacob Hoffman-Andrews 213d64086c Merge into_json_deserialize into into_json. (#235)
Followup to #56. At the time, doing this would have been an API
break; but we can do this as part of 2.0. This simplifies the API
nicely and creates better symmetry between sending bodies and
receiving them.
2020-11-21 22:19:13 -08:00
Jacob Hoffman-Andrews 2136f00bef Make Error Send + Sync (#236) 2020-11-21 22:11:35 -08:00
Jacob Hoffman-Andrews e92bf0b4bb Add ureq::request_url and Agent::request_url. (#226)
These let a user pass an already-parsed Url.
2020-11-21 22:11:15 -08:00
Jacob Hoffman-Andrews 0321ea043d Add some short docstrings for types. (#225)
These types were missing a short docstring, leaving the comment space to
their right blank on the crate page.

Also, this takes advantage of intra-rustdoc links to link terms:
https://github.com/rust-lang/rfcs/blob/master/text/1946-intra-rustdoc-links.md
2020-11-21 22:10:37 -08:00
Jacob Hoffman-Andrews 420c3e2483 Merge branch 'release-2.0' into json_deserialize 2020-11-21 16:25:14 -08:00
Jacob Hoffman-Andrews fade03b54e Rewrite the Error type. (#234)
This adds a source field to keep track of upstream errors and allow
backtraces, plus a URL field to indicate what URL an error was
associated with.

The enum variants we used to use for Error are now part of a new
ErrorKind type. For convenience within ureq, ErrorKinds can be turned
into an Error with `.new()` or `.msg("some additional information")`.

Error acts as a builder, so additional information can be added after
initial construction. For instance, we return a DnsFailed error when
name resolution fails. When that error bubbles up to Request's
`do_call`, Request adds the URL.

Fixes #232.
2020-11-21 16:14:44 -08:00
Jacob Hoffman-Andrews 271e650662 Merge into_json_deserialize into into_json. 2020-11-21 15:47:49 -08:00
Jacob Hoffman-Andrews dac517e30e Update README and docs. (#233)
This makes src/lib.rs the primary source for crate-level documentation.
I've generated README.md with `cargo readme > README.md`. Since links to
specific documentation items should be relative when possible, but must
be absolute in README.md, I've used the new syntax for intra-rustdoc
links
(https://github.com/rust-lang/rfcs/blob/master/text/1946-intra-rustdoc-links.md),
along with a README.tpl that sets up those links to point at the
absolute versions. `cargo readme` uses the README.tpl by default.

I've also rewritten the crate level docs, removing some TODO information
at the bottom, and moving the license information to CONTRIBUTING.md.
2020-11-21 09:40:04 -08:00
Jacob Hoffman-Andrews 4fe37260a4 Update README 2020-11-19 21:38:45 -08:00
Jacob Hoffman-Andrews c891aa31e5 Tweak sentence. 2020-11-19 21:36:23 -08:00