Commit Graph

134 Commits

Author SHA1 Message Date
Jacob Hoffman-Andrews
3745b028c6 Switch to MultiGzDecoder
This is more correct, since all gzip streams can consist of multiple members. It
has the happy side effect that it causes gzipped responses to reliably return
their streams to the pool.
2022-11-26 01:44:08 -08:00
Martin Algesten
5d9e89917c Test case showing gzip chunk not pooling 2022-11-26 01:44:08 -08:00
Max von Forell
855f20e662 Add Response::remote_addr() (#489)
Fixes #488.
2022-10-03 13:29:21 -07:00
Martin Algesten
b0796c18f3 Make build work (#546)
The mbedtls example has caused problem in the main build a number of
times. By making it a standalone `cargo new --bin`, we can keep it in
the source tree as a good example but avoid having it break the main
build.

Also, fix some clippy lints.
2022-09-29 08:29:32 -07:00
Michael Schubart
a367a82317 Fix mistake in API docs 2022-08-28 12:09:17 +02:00
Martin Algesten
134d82ecf4 debug! log method of body response 2022-07-10 11:55:57 +02:00
Jacob Hoffman-Andrews
f14fc45179 Buffer short responses (2) (#531)
* Response: build reader at construction time

* Remove unwrapping/rewrapping DeadlineStream

* Let into_reader() provide Sync + 'static

* Prebuffer Vec use known capacity

Co-authored-by: Martin Algesten <martin@algesten.se>
2022-07-10 11:12:50 +02:00
Jacob Hoffman-Andrews
9908c446d6 Simplify ReadWrite interface (#530)
Previously, ReadWrite had methods `is_poolable` and `written_bytes`, which
were solely for the use of unittests.

This replaces `written_bytes` and `TestStream` with a `struct Recorder`
that implements `ReadWrite` and allows unittests to access its recorded
bytes via an `Arc<Mutex<Vec<u8>>>`. It eliminates `is_poolable`; it's fine
to pool a Stream of any kind.

The new `Recorder` also has some convenience methods that abstract away
boilerplate code from many of our unittests.

I got rid of `Stream::from_vec` and `Stream::from_vec_poolable` because
they depended on `TestStream`. They've been replaced by `NoopStream` for
the pool.rs tests, and `ReadOnlyStream` for constructing `Response`s from
`&str` and some test cases.
2022-07-09 10:13:44 -07:00
Jacob Hoffman-Andrews
0cf1f8dbb9 Add Sync traits to ReadWrite trait (#528)
This allows us to get rid of a Mutex (and not take a dependency on
sync_wrapper).
2022-07-05 01:52:25 -07:00
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
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
Ed Morley
a8ed085a93 Fix broken TLS anchor link in docs overview
Now correctly links to:
https://docs.rs/ureq/latest/ureq/#https--tls--ssl

Introduced in 56276c3742.

Also fixes a rustdoc typo spotted by my IDE's spell checker whilst the
project was open.
2022-04-22 19:25:44 +02:00
Martin Algesten
2c29cc230c Remove Sync requirement of ReadWrite trait
3rd party TLS connections are currently required to provide a Sync
guarantee. Since ureq will only ever use the connection on a single
thread, this is not necessary.

The reason we end up with this bound is because we want Response and
Error to be Sync, in which case Rust's automatic inferral of Sync
fails.

This change "masks" the Stream in a wrapper making it Sync.

Close #474
2022-01-30 21:50:11 +01:00
Martin Algesten
219b5edf9e Rename test function as_write_vec -> into_written_bytes
Tests use `Response::as_write_vec` to inspect the outgoing HTTP/1.1
request line and headers. The current version has two problems:

1. Called `as_write_vec` when it actually returns a `&[u8]`.
2. Inspects/uses the `Response::stream` without consuming `Response`.

The first problem is trivial, but the second is subtle. Currently all
calls on `Response` that works with the internal `Response::stream`
consumes `self` (`into_string`, `into_reader`).

`Response` is by itself `Send + Sync`, and must be so because the
nested Stream is `Read + Write + Send + Sync`. However for
implementors of `TLSStream`, it would be nice to relax the `Sync`
requirement.

Assumption: If all fields in Response are `Sync` except
`Response::stream`, but any access to `stream` consumes `Response`, we
can consider the entire `Response` `Sync`.

This assumption can help us relax the `TlsStream` `Sync` requirement
in a later PR.
2022-01-29 17:32:18 +01:00
Martin Algesten
7b2f28bbc2 Tidy up Response::url initialization 2021-12-22 07:58:45 +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
Martin Algesten
f3857eed00 Ensure we provide a Transport::message() when we can 2021-12-19 21:17:26 +01:00
Martin Algesten
158a6c6f03 Clarify serde doc on Response::into_json (#451)
Close #428
2021-12-19 11:41:44 -08:00
Malloc Voidstar
598ebf4393 Remove Content-Encoding and length when decompressing
This should lower the chance of breakage. Probably also more proper for a client library.
2021-12-19 14:01:56 +01:00
Malloc Voidstar
23e993689a Rename Decompressor to BrotliDecoder to align with gzip 2021-12-19 14:01:56 +01:00
Malloc Voidstar
873e6066f3 Add support for gzip and brotli
Automatically sends the Accept-Encoding header on requests.

Not runtime-configurable, only with Cargo features.
2021-12-19 14:01:56 +01:00
Martin Algesten
c59632bd97 Use Url (instead of String) in internal history var 2021-12-19 11:00:39 +01:00
Jacob Hoffman-Andrews
56276c3742 Add support for alternate TLs implementations. 2021-12-17 17:47:30 +01:00
Jon Gjengset
1ec60e1c15 Remove trailing newline from testing response
The newline becomes part of the response body, even though it was not passed in
as `body`, which makes `Response::new` difficult to use with anything that
checksums data contents for example. Arguably there should also be a mechanism
for passing in `[u8]` bodies, but that's for a separate feature request.
2021-10-02 18:51:59 +02:00
Niketh Murali
4665b0aa5a Fix clippy warnings
Fix linter warning from clippy about unnecessary borrows - "This expression borrows a reference ... that is immediately dereferenced by the compiler"
2021-08-13 09:26:04 +02:00
12932
db5203d1a0 fix typo "memroy" to "memory" (#379) 2021-04-26 11:41:26 -07:00
Martin Algesten
3cfa9e6b35 Refactor into_string body reading 2021-03-25 21:49:39 +01:00
Martin Algesten
7c6ed53df3 Move Response::unit and stream to the heap 2021-03-25 21:04:12 +01:00
Martin Algesten
cfaca317c6 Provide url in Response Debug impl 2021-03-25 07:45:34 +01: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
Martin Algesten
026cf75690 Handle non-utf8 status and headers
Non-utf8 headers are ignored and reading the value for them will
yield `None`.
2021-03-14 23:14:43 +01:00
Martin Algesten
0dc609fc30 Ensure Error and Response implement Send+Sync
This is to ensure we don't accidentally introduce breaking changes
with respects to Send and Sync.
2021-03-14 19:00:04 +01:00
Martin Algesten
91cb0ce5fc Move unit tests inside conditionally compiled mod tests { } blocks
Idiomatic rust organizes unit tests into `mod tests { }` blocks
using conditional compilation `[cfg(test)]` to decide whether to
compile the code in that block.

This commit moves "bare" test functions into such blocks, and puts
the block at the bottom of respective file.
2021-03-14 18:56:09 +01:00
Martin Algesten
566295bebb Provide context for errors when reading status line and headers
The HTTP spec allows for non-ascii values both in the status line and
in headers. ureq does not handle that, we can however provide better
context for when it happens.
2021-03-13 23:03:25 +01:00
Martin Algesten
7222931b24 Update src/response.rs
Co-authored-by: Jacob Hoffman-Andrews <github@hoffman-andrews.com>
2021-02-21 23:20:58 +01:00
Martin Algesten
ea83edc609 Allow status lines with missing reason phrase
The spec says the reason phrase at least must be a space. However in
the wild, there are sites that just ends after the status code.
To be more compatible, this commit relaxes ureq's parsing.

Close #316
2021-02-21 23:20:58 +01:00
Martin Algesten
dc0069670d Update src/response.rs
Co-authored-by: Jacob Hoffman-Andrews <github@hoffman-andrews.com>
2021-02-21 23:20:20 +01:00
Martin Algesten
281a8462b5 Clarify doc
Close #326
2021-02-21 23:20:20 +01:00
Nicolas
1857061859 Add a test case for LF line ending response headers 2021-02-21 09:21:18 +01:00
Nicolas
a73ff2e465 Fix #321: LF header line ending 2021-02-21 09:21:10 +01: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
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
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
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
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
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:

4200edb9ed/healthchecks/src/manage.rs (L70-L84)

75d4b363b6/src/lib.rs (L59-L63)

1d7daea38b/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