Commit Graph

441 Commits

Author SHA1 Message Date
Martin Algesten d627ef9704 OrAnyStatus::or_any_status ergonomic helper
Some users might prefer to handle all HTTP responses as Response
regardless of status code.
2021-01-30 09:40:45 +01:00
Michael Diamond 0c467fee13 Add a user_agent() method to AgentBuilder to configure the default User-Agent header. (#311) 2021-01-29 17:23:30 -08:00
Martin Algesten 06abdff4bf cargo fmt (#303) 2021-01-17 11:07:52 -08:00
Jarobi 4aadda8ad3 Fix typo in doc of into_json 2021-01-17 13:51:19 +01: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
Martin Algesten 948f736125 Document proxy support (#298)
ureq supports proxying using HTTP CONNECT and SOCKS5, the features are however
missing documentation. This brings it to the lib.rs doc.
2021-01-09 09:58:07 -08:00
Martin Algesten c24b250c80 Implement std::error::Error for error::Transport (#299)
Close #294
2021-01-09 09:56:20 -08:00
Joshua Nelson d0bd2d5ea9 Use iteration instead of recursion for connect (#291)
This allows handling larger redirect chains.

Fixes #290
2021-01-05 13:55:26 -08:00
Ulrik 0ee024f8c2 Response: Use ErrorKind::UnexpectedEof for "premature close" 2021-01-05 12:07:11 +01:00
Joshua Nelson f0245aad23 Fix some clippy lints (#292)
This commit can be replicated with `cargo +nightly clippy --fix -Z unstable-options`,
plus an edit to fix another `return` missed by clippy.
2021-01-03 20:10:43 -08:00
Joshua Nelson aeeff40c95 Fix 307/308 redirects (again)
- Add unit test
- Use the original method instead of hard-coding GET
2021-01-03 20:31:00 -05:00
Joshua Nelson 498b7137c2 Make tests much easier to debug
- Don't panic on the mutex in all tests if a single test fails
- Give a more helpful message if a test handler wasn't registered
- Enable env_logger for tests
2021-01-03 20:26:17 -05:00
Martin Algesten 8f9f3e5827 Fix main lib.rs and README.md example (#284)
Small fixes to just ensure lib.rs and README.md are run via cargo readme.
2021-01-03 10:38:10 -08:00
Martin Algesten 1f0d84279a Document simplest error example 2020-12-27 23:47:43 +01: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 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 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
Martin Algesten 5aff777f1f Refactor Display for Transport 2020-12-05 20:00:47 +01:00
Martin Algesten b91d196fbc error::Transport part of public API 2020-12-05 20:00:47 +01: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 219185f73f Make Error an enum again. 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
Joshua Nelson 37f991fa50 Make sure the redirects doctest actually tests something for 301 redirects 2020-12-05 15:03:47 +01:00
Joshua Nelson 4a3d3c49a1 Note that 307 redirects aren't followed in the docs 2020-12-05 15:03:47 +01:00
Joshua Nelson 6bab430d29 Only follow 307/308 redirects for methods without a body 2020-12-05 15:03:47 +01:00
Joshua Nelson 8d21052c7e Follow 307/308 redirects 2020-12-05 15:03:47 +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
messense df1f4bd0e1 Replace unmaintained encoding crate with encoding_rs 2020-11-29 19:38:39 +01: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