Commit Graph

122 Commits

Author SHA1 Message Date
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
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
Jacob Hoffman-Andrews
e3138b0ace Add proxy on agent. (#178) 2020-10-06 00:12:26 -07:00
Jacob Hoffman-Andrews
e8c3403f7b Remove DEFAULT_HOST (#153)
In a few places we relied on "localhost" as a default if a URL's host
was not set, but I think it's better to error out in these cases.

In general, there are a few places in Unit that assumed there is a
host as part of the URL. I've made that explicit by doing a check
at the beginning of `connect()`. I've also tried to plumb through
the semantics of "host is always present" by changing the parameter
types of some of the functions that use the hostname.

I considered a more thorough way to express this with types - for
instance implementing an `HttpUrl` struct that embeds a `Url`, and
exports most of the same methods, but guarantees that host is always
present. However, that was more invasive than this so I did a smaller
change to start.
2020-09-27 10:07:13 -07:00
Deluvi
7de192d3f1 Add BodySize enum 2020-09-27 10:50:24 +02:00
Deluvi
e224a6d126 Set chunked Transfer-Encoding when using send
Previously, using `.send()` on `Request` would require to set either the
Transfer-Encoding or the Content-Length header.

In an effort to provide better ergonomics for this library and to avoid
making users fall in a not-so obvious pitfall, the library should build a
valid Request without asking the user to mess around with the headers.

This commit attempts to fix this issue: if the user use `.send()` to
provide an unknown sized reader, the chunked Transfer-Encoding will be
used. Of course, there are prior checks to ensure we do not override the
user wish, like if the user already set a Content-Length or a
Transfer-Encoding.

This commit fix an edge case where the Content-Length would not be
automatically set if the Transfer-Encoding is set but not chunked.
2020-09-27 10:50:24 +02:00
Ulrik Mikaelsson
f599828c6d Turn Option<AgentState> into AgentState (#149)
`Some(AgentState)` seem to be assumed pretty much everywhere. I could not
find any test or piece of code hinting at how `None` should be interpreted,
or even see how a state of `None` could even be constructed.

Co-authored-by: Ulrik <ulrikm@spotify.com>
2020-09-18 13:47:11 -07:00
Jacob Hoffman-Andrews
6a88c2c8bf Clean up unused code and long imports. (#137)
This removes some commented out methods, and also changes instances of
::std::foo to use a more idiomatic import path.
2020-09-12 18:42:15 -07:00
Martin Algesten
50c19c5484 Read buffer to avoid byte-by-byte syscalls (#141)
Fixes #140
2020-09-12 18:27:15 -07:00
Martin Algesten
4bc6f79847 cargo fmt 2020-08-07 21:51:35 +02:00
André Cruz
75d5e52a45 Added method to set the TLS connection builder (#116)
This allows configuring the accepted certs, client authentication,
etc, when using the native TLS crate.
2020-07-28 21:44:35 +02:00
Jacob Hoffman-Andrews
7fce93d1c8 Fix tests. 2020-07-02 23:07:53 -07:00
Jacob Hoffman-Andrews
b53de7a7ed Remove default URL_BASE of localhost. 2020-07-02 23:01:51 -07:00
Jacob Hoffman-Andrews
ea7ed88399 Request: rename path field to URL. (#104)
* Request: rename path field to URL.

This more accurately reflects its use.
2020-07-01 09:53:10 +02:00
Jacob Hoffman-Andrews
e4ad3a5e5f Remove unnecessary lifetime annotations. (#103) 2020-07-01 09:40:47 +02:00
Jacob Hoffman-Andrews
00461fb5bd Only retry idempotent requests. (#80)
This also reverts a change to send_body that was originally added to
return the number of bytes written. It's no longer needed now that we
check the size of the reader in advance.

Fixes #76.
2020-06-22 09:40:55 -07:00
Jacob Hoffman-Andrews
15be014e05 Un-deprecate read/write timeouts. (#78)
In https://github.com/algesten/ureq/pull/67#issuecomment-647112883,
Shnatsel points out that there are uses for read_timeout vs timeout. For
instance, when downloading a large file it's useful to have a read
timouet in case the download stalls, but it would be hard to pick a good
overall timeout without knowing the size of the file and the bandwidth
of the connection.
2020-06-21 14:13:22 -07:00
Jacob Hoffman-Andrews
fdc1f37662 Update documentation. (#73)
* Update documentation.

Synchronize goals section between README and rustdoc, and add two goals
(blocking API; no unsafe) that were mentioned elsewhere in the README.

Add error handling to examples for the module and for the Response
object.

And a section on synthetic errors to the top-level module documentation.

* Add back missing close brace.

* Add main function that returns Result.

* Add links to send_bytes and send_form.

* Document chunked encoding for send.

* Use a larger vec of bytes for send.
2020-06-21 09:55:50 +02:00
Jacob Hoffman-Andrews
57be414d97 Add overall timeout for requests. (#67)
This deprecates timeout_read() and timeout_write() in favor of
timeout(). The new timeout method on Request takes a Duration instead
of a number of milliseconds, and is measured against overall request
time, not per-read time.

Once a request is started, the timeout is turned into a deadline
specific to that call. The deadline is used in conjunction with the
new DeadlineStream class, which sets a timeout on each read according
to the remaining time for the request. Once the request is done,
the DeadlineStream is unwrapped via .into::<Stream>() to become
an undecorated Stream again for return to the pool. Timeouts on the
stream are unset at this point.

Still to be done:

Add a setting on Agent for default timeout.
Change header-writing code to apply overall deadline rather than
per-write timeout.
Fixes #28.
2020-06-21 09:47:35 +02:00
Martin Algesten
7a73fa1871 cargo fmt 2020-06-15 09:33:08 +02:00
Martin Algesten
4196eeff40 remove curlies warnings 2020-06-12 21:03:49 +02:00
Martin Algesten
c5bc27260b code format 2020-05-23 09:29:57 +02:00
Rob Young
2e3a75166d Allow TLS client config to be overridden
See: https://docs.rs/rustls/latest/rustls/struct.ClientConfig.html
2020-05-20 20:54:04 +02:00
bbx0
8c2d035809 Clean up dependencies 2020-04-18 11:47:49 +02:00
bbx0
55f42a614b Use &str instead of AsRef<str> to be consistent with the API 2020-04-18 11:47:49 +02:00
bbx0
3bedf9ab69 Add support for application/x-www-form-urlencoded 2020-04-18 11:47:49 +02:00
rustysec
3b0df412ef initial proxy impl 2020-03-14 09:54:54 +01:00
Martin Algesten
2956683870 bump deps, fix clippy warnings 2020-03-14 09:54:54 +01:00
Rob Young
28bdb89175 Set a default content type for JSON requests
When sending a JSON request a Content-Type of application/json is
usually wanted. This is often set as a default for JSON methods by HTTP
clients so can be confusing when it is not set. However, we do not want
to prevent the user from setting their own Content-Type.
2020-02-06 07:57:00 +01:00
Martin Algesten
4b9cfe2b67 fix clippy lints 2020-01-07 07:59:29 +01:00
Martin Algesten
fef05fd5c9 send_bytes 2019-06-21 15:53:45 +02:00
Chris West (Faux)
ceb7c3ac14 use pub(crate) instead of include!() 2019-05-27 17:44:14 +01:00
Martin Algesten
141668acba list all header names in request and response 2019-02-03 11:52:05 +05:30
Martin Algesten
1519dcb0b1 fix redirect logic 2018-12-20 10:54:47 +01:00
Martin Algesten
131476bd1a remove extern crate 2018-12-18 13:45:13 +01:00
Martin Algesten
4151aab3b4 redirects 0 2018-12-18 13:34:05 +01:00
Martin Algesten
a7f54b3647 fix redirect query parameters/hostname 2018-12-04 18:05:19 +01:00
Martin Algesten
b16b14c6aa simpler api 2018-09-04 10:59:10 +02:00
Martin Algesten
c0384c364b Url deconstruction function 2018-09-03 11:22:20 +02:00
Martin Algesten
1e536af904 get_url 2018-07-02 12:33:47 +02:00
Martin Algesten
1d541763c6 get_method 2018-07-02 12:32:01 +02:00
Martin Algesten
9cf33365e5 avoid panic for headers 2018-07-02 09:10:31 +02:00
Martin Algesten
fc1d908d9c less restrictive send() signature 2018-07-01 19:01:59 +02:00
Martin Algesten
51744804da avoid parsing when setting headers 2018-07-01 18:33:50 +02:00
Martin Algesten
2d2daf58ba doc 2018-07-01 11:13:21 +02:00
Martin Algesten
4a5944443f connection pooling 2018-06-30 16:52:54 +02:00
Martin Algesten
c5fb12a1fe code format 2018-06-30 14:57:22 +02:00
Martin Algesten
317f75f8bf set unit on response 2018-06-30 14:37:48 +02:00