Commit Graph

52 Commits

Author SHA1 Message Date
Fabian Fett 4d726bad8d Add ConnectionPool HTTP1StateMachine (#416) 2021-09-09 15:34:03 +02:00
Fabian Fett eab2a84b1c Use explicit NIO imports (#407)
* Use explicit NIO imports for `NIOCore`, `NIOPosix` and `NIOEmbedded`
* Updated dependencies
2021-08-19 21:11:49 +02:00
Fabian Fett a9aee2675f Add HTTPScheduledRequest and HTTPExecutingRequest (#384)
- Adding four new protocols:
  - `HTTPExecutingRequest` a protocol representing an HTTP task that is executed on a ChannelHandler.
  - `HTTPScheduledRequest` a protocol representing an HTTP task that is scheduled for execution, but in a waiting state (example: Waiting for an idle connection in the connection pool).
  - `HTTPRequestExecutor` a protocol that can be used from the `HTTPExecutingRequest` abstracting away functionality that will normally be implemented by a `ChannelHandler`
  - `HTTPRequestScheduler` a protocol that can be used from the `HTTPScheduledRequest` abstracting away functionality that will normally be implemented by a `ConnectionPool`
- An implementation of the `HTTPExecutingTask` and `HTTPScheduledRequest` called `RequestBag`. It implements  our current API using the new protocols

Co-authored-by: George Barnett <gbarnett@apple.com>
Co-authored-by: Cory Benfield <lukasa@apple.com>
2021-07-07 13:44:48 +02:00
Fabian Fett 8e4d51908d Refactor Channel creation (#377)
- The connection creation logic has been refactored into a number of smaller methods that can be combined
- Connection creation now has a logical home. It is moved from `Utils.swift` into a `ConnectionFactory`
- There are explicit `ChannelHandlers` that are used for connection creation:
  - `TLSEventsHandler` got its own file and unit tests
  - `HTTP1ProxyConnectHandler` got its own file and unit tests
  - `SOCKSEventsHandler` got its own file and unit tests
- Some small things are already part of this pr that will get their context later. For example: 
  - `HTTPConnectionPool` is added as a namespace to not cause major renames in follow up PRs
  - `HTTPConnectionPool.Connection.ID` and its generator were added now. (This will be used later to identify a connection during its lifetime)
  - the file `HTTPConnectionPool+Manager` was added to give `HTTPConnectionPool.Connection.ID.Generator` already its final destination.
2021-06-28 13:17:33 +02:00
David Evans 102b7e4bce Update NIO family dependencies to 5.2+ versions and fix deprecations (#381)
Updated:

NIO
NIOSSL
NIO Extras
NIOTS
Also fix TLSConfiguration.forClient() warnings by converting to TLSConfiguration.makeClientConfiguration(). Also the same for forServer().
2021-06-23 14:58:08 +01:00
David Evans 3fd0658dd9 Implement SOCKS proxy functionality (#375)
Add a new Proxy type to enable a HTTPClient to send requests via a SOCKSv5 Proxy server.
2021-06-18 13:02:58 +01:00
Johannes Weiss e2d03ffb32 cache NIOSSLContext (saves 27k allocs per conn) (#362)
Motivation:

At the moment, AHC assumes that creating a `NIOSSLContext` is both cheap
and doesn't block.

Neither of these two assumptions are true.

To create a `NIOSSLContext`, BoringSSL will have to read a lot of
certificates in the trust store (on disk) which require a lot of ASN1
parsing and much much more.

On my Ubuntu test machine, creating one `NIOSSLContext` is about 27,000
allocations!!! To make it worse, AHC allocates a fresh `NIOSSLContext`
for _every single connection_, whether HTTP or HTTPS. Yes, correct.

Modification:

- Cache NIOSSLContexts per TLSConfiguration in a LRU cache
- Don't get an NIOSSLContext for HTTP (plain text) connections

Result:

New connections should be _much_ faster in general assuming that you're
not using a different TLSConfiguration for every connection.
2021-05-13 12:16:52 +01:00
Cory Benfield b075d19007 Unconditionally insert TLSEventsHandler (#349)
Motivation:

AsyncHTTPClient attempts to avoid the problem of Happy Eyeballs making
it hard to know which Channel will be returned by only inserting the
TLSEventsHandler upon completion of the connect promise. Unfortunately,
as this may involve event loop hops, there are some awkward timing
windows in play where the connect may complete before this handler gets
added.

We should remove that timing window by ensuring that all channels always
have this handler in place, and instead of trying to wait until we know
which Channel will win, we can find the TLSEventsHandler that belongs to
the winning channel after the fact.

Modifications:

- TLSEventsHandler no longer removes itself from the pipeline or throws
  away its promise.
- makeHTTP1Channel now searches for the TLSEventsHandler from the
  pipeline that was created and is also responsible for removing it.
- Better sanity checking that the proxy TLS case does not overlap with
  the connection-level TLS case.

Results:

Further shrinking windows for pipeline management issues.
2021-03-18 12:21:23 +00:00
Cory Benfield ae5f185907 Use synchronous pipeline hops to remove windows. (#346)
Motivation:

There is an awkward timing window in the TLSEventsHandler flow where it
is possible for the NIOSSLClientHandler to fail the handshake on
handlerAdded. If this happens, the TLSEventsHandler will not be in the
pipeline, and so the handshake failure error will be lost and we'll get
a generic one instead.

This window can be resolved without performance penalty if we use the
new synchronous pipeline operations view to add the two handlers
backwards. If this is done then we can ensure that the TLSEventsHandler
is always in the pipeline before the NIOSSLClientHandler, and so there
is no risk of event loss.

While I'm here, AHC does a lot of pipeline modification. This has led to
lengthy future chains with lots of event loop hops for no particularly
good reason. I've therefore replaced all pipeline operations with their
synchronous counterparts. All but one sequence was happening on the
correct event loop, and for the one that may not I've added a fast-path
dispatch that should tolerate being on the wrong one. The result is
cleaner, more linear code that also reduces the allocations and event
loop hops.

Modifications:

- Use synchronous pipeline operations everywhere
- Change the order of adding TLSEventsHandler and NIOSSLClientHandler

Result:

Faster, safer, fewer timing windows.
2021-03-16 09:41:55 +00:00
Artem Redkin 0a8dddbe15 Fixes default timeout documentation comment (#317)
Motivation:
Right now documentation states that timrout defaults to no timeout, this
is no actually true, if timeout is not set NIO bootstrap defaults to 10
seconds connect timeout.

Modifications:
Updates documentation comment.

Result:
Closes #118
2020-11-11 16:02:21 +00:00
Artem Redkin 236b1de5ad Fixes propagation of errors during TLS handshakre (#316)
Motivation:
Right now we only handle one type of SSL error: `.handshakeFailed`,
but in reality a multitude of errors can happen, for example, remote
party might just close connection that will in turn raise
`.uncleanShutdown` error, that will be dropped on the floor and
users will only get non-descriptive `NoResult` error.

Modifications:
Handle all types of SSL errors during handshake instead of just one.
Adds a test.

Result:
Closes #313

Co-authored-by: Cory Benfield <lukasa@apple.com>
2020-11-11 15:50:13 +00:00
Artem Redkin c9a9bf061d rename connection pool configuration (#288) 2020-07-31 10:06:53 +01:00
Artem Redkin f69b68ffa8 fail if user tries writing bytes after request is sent (#270) 2020-07-30 11:22:55 +01:00
Artem Redkin 2e6a64abb3 add a separate configuration struct for pool (#284)
* add a separate configuration struct for pool

* review fixes

* review fix
2020-07-17 16:06:11 +01:00
Adam Fowler 96a803e98a Use SwiftLogNoOpLogHandler from swift-log (#282)
Co-authored-by: Cory Benfield <lukasa@apple.com>
Co-authored-by: Artem Redkin <artem@redkin.me>
2020-07-17 09:47:23 +01:00
Adam Fowler 643a0dc249 Added default value for queue in HTTPClient.shutdown() (#279)
Default it to .global()
2020-06-29 13:26:06 +01:00
Max Desiatov dcf7e5c702 Fix invalid code in HTTPClient.swift doc comment (#271)
`HTTPClient(eventLoopGroupProvider = .createNew)` is not a valid Swift expression.
2020-06-24 08:13:30 +01:00
Artem Redkin 61a80a2d34 assume chunked on a stream with no length (#247)
Motivation:
Streams length parameter is optional to allow cases were stream length is not known in advance, but we do not support this in request validation. This PR aims to address that.

Modifications:
Modifies request validation to default to chunked encoding if body length is zero or to passed in content-length header
Adds a test

Result:
Closes #218
2020-06-23 16:42:16 +01:00
Artem Redkin aac4357b65 notify delegate about connect errors (#245) 2020-06-23 15:00:19 +01:00
Max Desiatov c4f5155384 Fix doc comment for redirectConfiguration (#266)
`redirectConfiguration` can't default to `false` as it's not a boolean value, and the default value is `RedirectConfiguration()`.

Co-authored-by: Johannes Weiss <johannesweiss@apple.com>
Co-authored-by: Artem Redkin <artem@redkin.me>
2020-06-22 17:39:58 +01:00
Dimitri Bouniol ec48f4f114 Convenience methods for socket paths (#235)
* Added additional tests for socketPath-based requests

Motivation:

While going through the existing tests, I identified a few more instances where we could add some testing.

Modifications:

Added one test that verifies Requests are being decoded correctly, and improved three others to check for path parsing, error throwing, and schema casing respectively.

Result:

Tests that continue to pass, but that will also catch any incompatible changes in the future.

* Added some convenience initializers to URL and methods to Request for making requests to socket paths

Motivation:

Creating URLs for connecting to servers bound to socket paths currently requires some additional code to get exactly right. It would be nice to have convenience methods on both URL and Request to assist here.

Modifications:

- Refactored the get/post/patch/put/delete methods so they all call into a one line execute() method.
- Added variations on the above methods so they can be called with socket paths (both over HTTP and HTTPS).
- Added public convenience initializers to URL to support the above, and so socket path URLs can be easily created in other situations.
- Added unit tests for creating socket path URLs, and testing the new suite of convenience execute methods (that, er, test `HTTPMETHOD`s). (patch, put, and delete are now also tested as a result of these tests)
- Updated the read me with basic usage instructions.

Result:

New methods that allow for easily creating requests to socket paths, and passing tests to go with them.

* Removed some of the new public methods added for creating a socket-path based request

Motivation:

I previously added too much new public API that will most likely not be necessary, and can be better accessed using a generic execute method.

Modifications:

Removed the get/post/patch/put/delete methods that were specific to socket paths.

Result:

Less new public API.

* Renamed execute(url:) methods such that the HTTP method is the first argument in the parameter list

Motivation:

If these are intended to be general methods for building simple requests, then it makes sense to have the method be the first parameter in the list.

Modifications:

Moved the `method: HTTPMethod` parameter to the front of the list for all `execute([...] url: [...])` methods, and made it default to .GET. I also changed the url parameter to be `urlPath` for the two socketPath based execute methods.

Result:

A cleaner public interface for users of the API.

* Fixed some minor issues introduces with logging

Motivation:

Some of the convenience request methods weren't properly adapted for logging.

Modifications:

- Removed a doc comment from patch() that incorrectly referenced a logger.
- Fixed an issue where patch() would call into post().
- Added a doc comment to delete() that references the logger.
- Tests for the above come in the next commit...

Result:

Correct documentation and functionality for the patch() and delete() methods.

* Updated logging tests to also check the new execute methods

Motivation:

The logging tests previously didn't check for socket path-based requests.

Modifications:

Updated the `testAllMethodsLog()` and `testAllMethodsLog()` tests to include checks for each of the new `execute()` methods.

Result:

Two more tests that pass.
2020-06-19 10:51:03 +01:00
Artem Redkin 606ab0eaea check body length (#255) 2020-06-16 09:17:09 +01:00
Johannes Weiss 86db162a11 logging support (#227)
Motivation:

AsyncHTTPClient is not a simple piece of software and nowadays also
quite stateful. To debug issues, the user may want logging.

Modification:

Support passing a logger to the request methods.

Result:

Debugging simplified.
2020-06-09 15:55:23 +01:00
Dimitri Bouniol 364d1069a4 Better support for UNIX Domain sockets (#228)
* Added tests for http+unix and https+unix url schemes

Motivation:

Using a base URL as the socket path only works when the URL object is maintained as long as possible through the stack. Additionally, it doesn't currently provide a way to use TLS over UNIX sockets.

Modifications:

Added two tests to test out the to-be supported URL schemes, http+unix, and https+unix, which encode the socket path as a %-escaped hostname, as some existing services already do.

Result:

Better UNIX domain socket support.
2020-06-02 15:22:20 +01:00
Artem Redkin 8adfdb9bc5 refactor pool (#192) 2020-05-18 17:47:21 +01:00
Adam Fowler 5298f20331 Support NIO Transport Services - part 2 (#184)
This is the continuation of the good work of @Yasumoto and @weissi in #135

The following code adds support for NIO Transport services. When the ConnectionPool asks for a connection bootstrap it is returned a NIOClientTCPBootstrap which wraps either a NIOTSConnectionBootstrap or a ClientBootstrap depending on whether the EventLoop we are running on is NIOTSEventLoop.

If you initialize an HTTPClient with eventLoopGroupProvider set to .createNew then if you are running on iOS, macOS 10.14 or later it will provide a NIOTSEventLoopGroup instead of a EventLoopGroup.

Currently a number of tests are failing. 4 of these are related to the NIOSSLUncleanShutdown error the others all seem related to various race conditions which are being dealt with on other PRs. I have tested this code with aws-sdk-swift and it is working on both macOS and iOS.

Things look into:

The aws-sdk-swift NIOTS HTTP client had issues with on Mojave. We should check if this is the case for async-http-client as well.

Co-authored-by: Joe Smith <yasumoto7@gmail.com>
Co-authored-by: Johannes Weiss <johannesweiss@apple.com>
2020-04-18 14:17:46 +01:00
Fabian Fett 2d88de3eb6 Verify header field names (#191)
* HTTPRequest without body: Content-Length shall not be send
* Verify field names comply with RFC7230
2020-04-01 16:48:01 +01:00
Artem Redkin a6d1ebe302 Implement asynchronous shutdown (#183) 2020-03-30 13:23:34 +01:00
Trevör 38bef7c546 Fix leaked TLS handshake promise (#180) (#180)
motivation:
TLS handshake promise was leaked in some cases of failure (see #179)

changes:
- Avoid leaking promise
- Clearer completion flow for related futures
- Add testAvoidLeakingTLSHandshakeCompletionPromise test
2020-03-12 18:37:19 +00:00
Trevör 9cdd1dc389 Close idle pool connections (#170)
* Close idle pool connections

Motivation: Pooled connections should close at some point (see #168)

Changes:
- Add new poolingTimeout property to HTTPClient.Configuration, it's
default value is .seconds(60), it can be set to nil if one wishes to
disable this timeout.
- Add relevant unit test

Co-authored-by: Johannes Weiss <johannesweiss@apple.com>
2020-03-03 21:51:50 +00:00
Trevör 19e2ea727e Add an HTTP/1.1 connection pool (#105)
motivation: Better performance thanks to connection reuse

changes:
- Added a connection pool for HTTP/1.1
- All requests automatically use the connection pool
- Up to 8 parallel connections per (scheme, host, port)
- Multiple additional unit tests
2020-02-25 15:43:16 +00:00
Johannes Weiss de7421906c fix UDS without a baseURL (#165)
Previously, UNIX Domain Sockets would only work if the URL also had a
"base URL". If it didn't have a base URL, it would try to connect to the
empty string which would fail :).

Now, we support both cases:
- URLs with a baseURL (the path to the UDS) and a path (actual path)
- URLs that just have an actual path (path to the UDS) where we'll just
  use "/" as the URL's path
2020-02-24 17:52:36 +00:00
Marcin Krzyzanowski e90f5fd03d Support UNIX Domain Sockets (#151)
Adds support for UNIX Domain Socket requests.

Usage:

```
let httpClient = HTTPClient(eventLoopGroupProvider: .createNew)
let socketURL = URL(string: "unix:///var/run/docker.sock")!
let req = try HTTPClient.Request(url: URL(string: "/users/list", relativeTo: socketURL)!, method: .GET)
let response = try httpClient.execute(request: req).wait()
```
2020-01-27 15:55:12 +00:00
Andreas Kostuch 8f2f7b1691 Bugfix HTTPS SNI and IP Address (#139)
* Bugfix HTTPS SNI and IP Address

Motivation:

Solving the SNI Bug

Modifications:

Added an internal extension on String for checking if the hostname is an IP Address -- see the private extension on SNI. Additionally using the IPv4Address and IPv6Address Function from Network above 10.14 as protecting with #availabe.
Adding the test for HTTPS and IP in as hostname

Result:

We get results with an IP as Hostname
2019-12-16 22:21:25 +00:00
Johannes Weiss 48e284d1ea fix NIO deprecations & update to secure versions (#141) 2019-12-10 18:08:28 +00:00
tomer doron 018e139eef update copyrights note (#125)
motivation: the Swift Server Workgroup is not a legal entity and cannot hold copyrights. with this change, code authors continue and retain their copyrights under the apache license and previous copyrights note, but Apple steps up instead of the workgroup which has no legal status

changes:
* update header files to say "Apple Inc. and the AsyncHTTPClient project authors" instead of "Swift Server Workgroup and the AsyncHTTPClient project authors"
* update validation scripts to check for the correct header
* add CONTRIBUTING.md file to explain how to make contributions and include a legal notice about licensing the contribution to Apple and the project
* regenerate CONTRIBUTORS.md to reflect most recent contributions
2019-11-06 11:20:30 -08:00
Artem Redkin 51dc885a30 add support for redirect limits (#113) 2019-10-23 19:29:25 +03:00
Johannes Weiss c1c3da3990 remove all deprecations (#116) 2019-10-22 17:52:11 -07:00
Artem Redkin 191c4ba506 add response decompression support (#86)
fixes #44
2019-10-22 17:45:12 -07:00
Johannes Weiss 513be15050 EventLoop preference overhaul (#102) 2019-09-21 21:33:15 +02:00
Johannes Weiss 1b31c5a4e3 give generic parameters nice names (#99) 2019-09-09 17:54:21 +01:00
vkill 47de4bb5ec Add authorization to proxy (#94) 2019-09-08 14:30:24 +01:00
Johannes Weiss 26afbc14bb make sure HTTPClient is shutdown (#98)
Motivation:

Right now, HTTPClient only asserts that it's shut down if it was started with its own EventLoopGroup.
That however is weird because it's lifecycle model depends on the parameters you pass to `init`.

Modifications:

Always validate the lifecycle (in debug mode).

Result:

API makes more sense.
2019-09-05 15:33:13 +01:00
Artem Redkin bbf7966992 nest timeout configuration type inside configuration (#93)
* nest timeout configuration type inside configuration

* move timeout to an extension
2019-08-31 09:49:56 +01:00
tomer doron b1eb92eb3d refactor proxy configuration (#90)
* refactor proxy configuration

motivation: make proxy configuration follow same convention as other configuration

changes:
* nest Proxy under HTTPClient.Configuration instad of top level HTTPClient
* make host and port public and mutable, following convention of other configuration objects in this library
* add some API docs

* fixup
2019-08-24 08:58:51 +01:00
Ludovic Dewailly 8814439abe redirects ignore EventLoop preference - issue#88 (#89) 2019-08-21 11:26:19 +01:00
Artem Redkin 64851a1a0a add NIO event loop as an argument for execute (#79)
* add NIO event loop as an argument for execute

* review fix: add to np-delegate method as well

* Resolve confict

* add missing linux test

* fix formatting

* missing self

* review fix: add event loop preference argument instead of eventloop

* formatting

* review fix: spelling

* fix compilation error

* review fixes: make preference argument not explicit and add precondition that EL must be part of ELG
2019-08-20 17:50:10 +01:00
vkill e0eeb0481c Ignore uncleanShutdown error when state is .head or .body (#77)
* Ignore uncleanShutdown error when state is head or body

* Add ignoreNIOSSLUncleanShutdownError to Configuration

* Revert old HTTPClient.init founctions

* Run generate_linux_tests.rb

* Rename ignoreNIOSSLUncleanShutdownError to ignoreUncleanSSLShutdown

* Make tests compatible with swift 5.0
2019-08-15 11:55:33 +01:00
Artem Redkin 0c617a4d7f document public API (#62)
Co-Authored-By: Joe Smith <yasumoto7@gmail.com>
2019-07-17 10:56:54 +01:00
Artem Redkin 9b1e9d3640 add deadlines (#57) 2019-07-03 14:40:26 +03:00