Commit Graph
109 Commits
Author SHA1 Message Date
Konrad `ktoso` MalawskiandGitHub c2a3a2cfb7 [Tracing] Default tracer to global bootstrapped tracer (#861) 2025-10-09 17:01:05 +09:00
8430dd49d4 Introduce built-in swift-distributed-tracing support (#857)
Co-authored-by: Moritz Lang <16192401+slashmo@users.noreply.github.com>
Co-authored-by: George Barnett <gbarnett@apple.com>
2025-10-07 16:30:31 +09:00
Cory BenfieldandGitHub 7dc119c7ed Add support for HTTP/1 connection pre-warming (#856)
Motivation

This patch adds support for HTTP/1 connection pre-warming. This allows
the user to request that the HTTP/1 connection pool create and maintain
extra connections, above-and-beyond those strictly needed to run the
pool. This pool can be used to absorb small spikes in request traffic
without increasing latency to account for connection creation.

Modifications

- Added new configuration properties for pre-warmed connections.
- Amended the HTTP/1 state machine to create new connections where
necessary.
- Added state machine tests.

Results

Pre-warmed connections are available.
2025-09-09 09:55:45 +01:00
Johannes WeissandGitHub abb11d5b90 make HTTPClient Sendable without @unchecked (#852)
Previously, `HTTPClient` used `@unchecked Sendable`, now it's compiler
checked.
2025-08-12 10:04:16 +01:00
George BarnettandGitHub 3b265e6a00 Enable warnings as errors in CI (#842)
Motivation:

Now that strict concurrency has been adopted the AHC should avoid
regressing by treating all warnings as errors in CI.

Modifications:

- Treat warnings as errors in CI

Result:

Stricter CI
2025-05-08 10:51:33 +01:00
Cory BenfieldandGitHub beb2637432 Clean up Task error handling. (#839)
Motivation

We have some Task error handling functions that are generic for no
apparent reason. They're also typically called from contexts where they
also report the error to the delegate, but one of the call sites doesn't
do that. So add a test for that as well.

Modifications

- Rewrite Task.fail(with:delegate:) to be non-generic.
- Add a call to the delegate error handler on the path that is missing
it.
- Add a test for that call

Results

Cleaner, easier to follow code
2025-04-30 13:57:00 +00:00
George BarnettandGitHub 0e715a2793 Fix a few simple sendability issues (#832)
Motivation:

We're about to go on a sendability journey. Let's pick some low hanging
fruit to get started.

Modifications:

- Add a few assume-isolated calls
- Stop using static var
- Use a dispatch group instead of a work item to wait for work to be
done.

Result:

Fewer warnings
2025-04-28 14:17:36 +01:00
a3d00a65b9 Add "debug initializer" hook for channels (#801)
Motivation:

As requested in #596, it can be handy to have a lower-level access to
channels (HTTP/1 connection, HTTP/2 connection, or HTTP/2 stream) to
enable a more fine-grained interaction for, say, observability, testing,
etc.

Modifications:

- Add 3 new properties (`http1_1ConnectionDebugInitializer`,
`http2ConnectionDebugInitializer` and
`http2StreamChannelDebugInitializer`) to `HTTPClient.Configuration` with
access to the respective channels. These properties are of `Optional`
type `@Sendable (Channel) -> EventLoopFuture<Void>` and are called when
creating a connection/stream.

Result:

Provides APIs for a lower-level access to channels.

---------

Co-authored-by: Cory Benfield <lukasa@apple.com>
Co-authored-by: David Nadoba <d_nadoba@apple.com>
Co-authored-by: George Barnett <gbarnett@apple.com>
2025-04-25 12:11:02 +00:00
Johannes WeissandGitHub 2119f0d9cc fix 784: dont crash on huge in-memory bodies (#785)
fixes #784 

`writeChunks` had 3 bugs:
1. An actually wrong `UnsafeMutableTransferBox` -> removed that type
which should never be created
2. A loooong future chain (instead of one final promise) -> implemented
3. Potentially infinite recursion which lead to the crash in #784) ->
fixed too
2024-11-26 14:52:39 +00:00
Rick Newton-RogersandGitHub c621142327 Adopt GitHub actions (#780)
Migrate CI to use GitHub Actions.

### Motivation:

To migrate to GitHub actions and centralised infrastructure.

### Modifications:

Changes of note:
* Adopt swift-format using rules from SwiftNIO.
* Remove scripts and docker files which are no longer needed.
* Disabled warnings-as-errors on Swift 6.0 CI pipelines for now.

### Result:

Feature parity with old CI.
2024-10-29 15:01:46 +00:00
15dbe6dcee Add an option to enable Multipath TCP on clients (#766)
Multipath TCP (MPTCP) is a TCP extension allowing to enhance the
reliability of the network by using multiple interfaces. This extension
provides a seamless handover between interfaces in case of deterioration
of the connection on the original one. In the context of iOS and Mac OS
X, it could be really interesting to leverage the capabilities of MPTCP
as they could benefit from their multiple interfaces (ethernet + Wi-fi
for Mac OS X, Wi-fi + cellular for iOS).

This contribution introduces patches to HTTPClient.Configuration and
establishment of the Bootstraps. A supplementary field "enableMultipath"
was added to the configuration, allowing to request the use of MPTCP.
This flag is then used when creating the channels to configure the
client.

Note that in the future, it might also be potentially interesting to
offer more precise configuration options for MPTCP on MacOS, as the
Network framework allows also to select a type of service, instead of
just offering the option to create MPTCP connections. Currently, when
enabling MPTCP, only the Handover mode is used.

---------

Co-authored-by: Cory Benfield <lukasa@apple.com>
2024-09-20 12:22:17 +01:00
e27aef494d Make ConnectionPool's retryConnectionEstablishment public (#744)
* Make ConnectionPool's `retryConnectionEstablishment` public

* Unified tests to consistently use `enableFastFailureModeForTesting()`

* Add `retryConnectionEstablishment` as optional parameter to the initializer of `HTTPClient.Configuration.ConnectionPool`

* Reverted change to initializer to prevent API stability breakage

* Add parameterless initializer for `HTTPClient.Configuration.ConnectionPool`

* Moved default values for `HTTPClient.Configuration.ConnectionPool` to the property declarations, so they only have to be specified at one point

* Removed superfluous spaces

Co-authored-by: Cory Benfield <lukasa@apple.com>

* Re-added missing line break

---------

Co-authored-by: Cory Benfield <lukasa@apple.com>
2024-06-18 14:04:29 +01:00
e0977cf290 HTTPClient.shared a globally shared singleton & .browserLike configuration (#705)
Co-authored-by: Johannes Weiss <johannes@jweiss.io>
2024-04-03 05:54:00 -07:00
Gustavo CairoandGitHub 83f015bf94 Fix write timeout not being initialised (#730) 2024-03-21 16:18:22 +00:00
Gustavo CairoandGitHub 5ccda442f1 Use the given connection pool idle timeout in the HTTPClient.Configuration inits (#723) 2023-12-21 09:51:54 -03:00
Gustavo CairoandGitHub d2d35663a2 Add an idle write timeout (#718) 2023-12-18 09:06:06 -03:00
Mahdi BahramiandGitHub 4824907382 Fix wrong/outdated connect timeout documentation (#714) 2023-10-25 08:19:44 -07:00
Rick Newton-RogersandGitHub de7c84a607 Bump minimum Swift version to 5.7 (#712)
Motivation:

Now that Swift 5.9 is GM we should update the supported versions and
remove 5.6

Modifications:

* Update `Package.swift`
* Remove `#if swift(>=5.7)` guards
* Delete the 5.6 docker compose file and make a 5.10 one
* Update docs

Result:

Remove support for Swift 5.6, add 5.10
2023-10-04 17:43:21 +01:00
David NadobaandGitHub 4e74fefd1c Support custom backgroundActivityLogger with using the default ELG. (#711) 2023-09-06 13:49:25 +01:00
8c90405f0c use NIOSingletons EventLoops/NIOThreadPool instead of spawning new (#697)
Co-authored-by: Johannes Weiss <johannes@jweiss.io>
2023-08-14 14:59:33 +01:00
David NadobaandGitHub d62c475401 Drop Swift 5.5 (#686) 2023-04-14 17:05:09 +01:00
George BarnettandGitHub 6c5058ee2c Add a control to limit connection reuses (#678)
Motivation:

Sometimes it can be helpful to limit the number of times a connection
can be used before discarding it. AHC has no such support for this at
the moment.

Modifications:

- Add a `maximumUsesPerConnection` configuration option which defaults
  to `nil` (i.e. no limit).
- For HTTP1 we count down uses in the state machine and close the
  connection if it hits zero.
- For HTTP2, each use maps to a stream so we count down remaining uses
  in the state machine which we combine with max concurrent streams to
  limit how many streams are available per connection. We also count
  remaining uses in the HTTP2 idle handler: we treat no remaining uses
  as receiving a GOAWAY frame and notify the pool which then drains the
  streams and replaces the connection.

Result:

Users can control how many times each connection can be used.
2023-04-11 14:49:44 +01:00
Cory BenfieldandGitHub 343cdf4639 Fix documentation and add support for CI-ing it (#679)
* Fix documentation and add support for CI-ing it

* Fixup license header
2023-04-11 13:35:46 +01:00
91b26407ad Update collect to use content-length to make early checks
Motivation:
not accumulate too many bytes

Modifications:
Implementing collect function to use NIOCore version to prevent overflowing

Co-authored-by: Cory Benfield <lukasa@apple.com>
2023-04-04 16:56:58 +01:00
David NadobaandGitHub 98b45ed1cd Allow DNS override (#675)
Sometimes it can be useful to connect to one host e.g. `x.example.com` but request and validate the certificate chain as if we would connect to `y.example.com`. This is what this PR adds support for by adding a `dnsOverride` configuration to `HTTPClient.Configuration`. This is similar to curls `—resolve-to` option but only allows overriding host and not ports for now.
2023-03-30 08:21:41 +01:00
Cory BenfieldandGitHub 9401037091 Make syncShutdown unavailable from async (#667)
* Make syncShutdown unavailable from async

Motivation

syncShutdown can cause unbounded thread blocking, we shouldn't allow it
in concurrent code.

Modification

Mark syncShutdown unavailable from async.

Result

Users are warned if they try to syncShutdown in an async context

* Only noasync on 5.7
2023-02-13 18:44:22 +01:00
Cory BenfieldandGitHub 7f05a8da46 Merge pull request from GHSA-v3r5-pjpm-mwgq
Motivation

Allowing arbitrary data in outbound header field values allows for the
possibility that users of AHC will accidentally pass untrusted data into
those values. That untrusted data can substantially alter the parsing
and content of the HTTP requests, which is extremely dangerous. The
result of this is vulnerability to CRLF injection.

Modifications

Add validation of outbound header field values.

Result

No longer vulnerable to CRLF injection
2023-01-17 11:06:08 +00:00
David NadobaandGitHub 0bdc425a84 Remove #if compiler(>=5.5) (#641)
* Remove `#if compiler(>=5.5)`

* Run SwiftFormat
2022-10-12 16:18:47 +01:00
David NadobaandGitHub 0b5bec741b Replace NIOSendable with Sendable (#640) 2022-10-12 08:50:28 +01:00
David NadobaandGitHub f17a47e916 Allow immediate request failure on connection error (#625) 2022-10-10 13:34:42 +01:00
Fabian FettandGitHub 4d69c84617 Add Hashable conformance to HTTPClient.Configuration.HTTPVersion (#636) 2022-10-07 08:15:14 -07:00
Fabian FettandGitHub 897d49aa1b Replace Lock with NIOLock (#628)
SwiftNIO 2.42.0 has deprecated Lock and replaced it with a new NIOLock. This commit removes all uses of Lock and replaces them with NIOLock. Further, now, we must require SwiftNIO 2.42.0
2022-09-27 15:42:47 +02:00
7f998f5118 add a future-returning shutdown method (#626)
Co-authored-by: Johannes Weiss <johannes@jweiss.io>
2022-09-07 06:12:05 -07:00
David NadobaandGitHub c3c90aab58 Adopt Sendable (#621) 2022-08-25 11:45:13 +02:00
KarlandGitHub 18109d842d Use NIOCore.System.coreCount for the fileIO thread pool (#618) 2022-08-20 12:22:41 +01:00
fc510a39cf Fix thread leak in FileDownloadDelegate (#614)
* Fix thread leak in `FileDownloadDelegate`

* `SwiftFormat`

* Add a shared file IO thread pool per HTTPClient

* User bigger thread pool and initlize lazily during first file write

* thread pool is actually not used in tests

* Update documentation

* fix review comments

* make `fileIOThreadPool` internal

* Add test to verify that we actually share the same thread pool across all delegates for a given HTTPClient

Co-authored-by: Cory Benfield <lukasa@apple.com>
2022-08-18 11:55:55 +02:00
Cory BenfieldandGitHub e294c8f28f Accurately apply the connect timeout in async code (#616)
Motivation

We should apply the connect timeout to the complete set of connection
attempts, rather than the request deadline. This allows users
fine-grained control over how long we attempt to connect for. This is
also the behaviour of our old-school interface.

Modifications

- Changed the connect deadline calculation for async/await to match that
  of the future-based code.
- Added a connect timeout test.

Result

Connect timeouts are properly handled
2022-08-16 13:17:45 +01:00
Cory BenfieldandGitHub 5e3e58dafd Use Docc for documentation (#613)
Motivation

Documentation is nice, and we can help support users by providing useful
clear docs.

Modifications

Add Docc to 5.6 and later builds
Make sure symbol references work
Add overview docs

Result

Nice rendering docs
2022-08-09 16:23:14 +01:00
Cory BenfieldandGitHub 0527bbb466 Remove the last remaining NIOAtomic (#607)
Motivation

Warnings aren't great, and NIOAtomic is deprecated.

Modifications

Replace the last use of NIOAtomic with ManagedAtomic.

Result

Fewer warnings
Fixes #606
2022-08-03 10:02:20 +01:00
David NadobaandGitHub 14fa6d944d Report last connection error if request deadline is exceeded (#601) 2022-07-01 10:39:38 +02:00
3fcd67061f Improve errors and testing using NIOTS (#588)
Motivation

Currently error reporting with NIO Transport Services is often sub-par.
This occurs because the Network.framework connections may enter the
waiting state until the network connectivity state changes. We were not
watching for the user event that contains the error in that state, so if
we timed out in that state we'd just give a generic timeout error,
instead of telling the user anything more detailed.

Additionally, several of our tests assume that failure will be fast, but
in NIO Transport Services we will enter that .waiting state. This is
reasonable, as changed network connections may make a connection that
was not succeeding suddenly viable. However, it's inconvenient for
testing, where we're mostly interested in confirming that the error path
works as expected.

Modifications

- Add an observer of the WaitingForConnectivity event that records it
  into our state machine for later reporting.
- Add support for disabling waiting for connectivity for testing
  purposes.
- Add annotations to several tests to stop them waiting for
  connectivity.

Results

Faster tests, better coverage, better errors for our users.

Co-authored-by: David Nadoba <dnadoba@gmail.com>
2022-06-01 14:13:47 +01:00
Christian PriebeandGitHub 89b0da2bf8 Add HTTPClientError shortDescription property (#583)
This adds a shortDescription property to HTTPClientError which provides a short
description of the error without associated values.
2022-04-22 07:59:58 +01:00
David NadobaandGitHub b1c0c3c77b replace EventLoopGroup.next() with .any() (#526) 2021-12-14 21:59:54 +01:00
David NadobaandGitHub bf5668c37b Use the current EventLoop as the requests preferred EventLoop (#515)
* Use the current `EventLoop` as the requests prefered `EventLoop` if possible

* use released version of swift-nio
2021-12-14 20:10:31 +01:00
David NadobaandGitHub c4feafd813 Refactor redirect logic to be reusable for async/await (#522)
* refactor RedirectHandler
- `redirectState` is no longer a property of `HTTPClient.Request`. RedirectHandler now stores this state directly and therefore no longer optional.
- we no longer count the number of allowed redirects down. Instead the number of redirects is dervied from `self.visited.count` and we compare it to the maxRedirect to check if we git the limit.

* `HTTPClient.Configuration.RedirectConfiguration.Configuration` is now called `HTTPClient.Configuration.RedirectConfiguration.Mode`
only two `Configuration`s left in the type name

* add redirect logger test
2021-12-08 19:16:06 +01:00
David NadobaandGitHub 9eaecbbbce SwiftFormat --ifdef no-indent (#494)
* SwiftFormat --ifdef no-indent

* update `generate_linux_tests.rb` to use new indention rule
2021-11-25 17:09:19 +01:00
David NadobaandGitHub 0ed00b8b36 Always overwrite Transport-Encoding and Content-Length headers (#479) 2021-11-23 10:22:57 +01:00
Fabian FettandGitHub ec2e080d70 Only crash in debug mode, if HTTPClient was not shutdown (#478)
### Motivation

Generally we want to inform users that they need to shutdown their HTTPClient. Until `1.6.0` we did this with an assert in HTTPClient's deinit. With `1.6.0` this behavior was raised to a precondition. Because of this adopters might suddenly crash in production where they didn't before.

### Changes

- This pr reverts the current behavior back to something pre `1.6.0`

### Result

- HTTPClient doesn't crash in production anymore.
2021-11-17 14:22:55 +01:00
David NadobaandGitHub 3e44e5154e [HTTP2] Make HTTPVersion public and set to .automatic by default (#473) 2021-11-11 14:41:11 +01:00
7617c35db3 Handle NIOSSLError.uncleanShutdown correctly (#472)
### Motivation

Fixes #238 and #231.

### Changes

- Extracted the unclean shutdown test from `HTTPClientTests` into their own file `HTTPClientUncleanSSLConnectionShutdownTests`
- Copy and pasted @weissi great explanation from #238 into the test file
- Removed property `ignoreUncleanSSLShutdown` everywhere

### Result

`ignoreUncleanSSLShutdown` on `HTTPClient.Configuration` is deprecated and ignored.

Co-authored-by: Johannes Weiss <johannesweiss@apple.com>
2021-11-11 11:02:54 +01:00