Commit Graph

307 Commits

Author SHA1 Message Date
Clinton Nkwocha 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
Greg Cotten 31122eaf7c Add Request/Response History to all public Response types (#817)
Work to close
https://github.com/swift-server/async-http-client/issues/790

The fact that `HTTPClient.Request` is not Sendable make me think we're
going to need to store something else, such as a `URL` and
`HTTPRequestHead`, instead?
2025-03-03 15:48:27 +00:00
Greg Cotten da621ce4a8 Add didVisitURL delegate method (#816)
Trying to pave the way for closing
https://github.com/swift-server/async-http-client/issues/790 with some
direction from @Lukasa.

I have no idea where is best to insert this new delegate method. I'm
currently doing it first thing in `receiveResponseHead0`, and not using
an EventLoopFuture for back pressure management. The state machine seems
pretty fragile and I don't want to leave too much of an imprint. Trying
to be a part of an EventLoopFuture chain seems really complicated and
would really leave a mark on the codebase, so I'm wondering if it's
possible to just warn the user "do not block"?

Anyway, just a jumping-off point and happy to take direction!
2025-02-21 08:30:24 +00:00
Greg Cotten ad262cc3d2 Propagate HTTPClient.Task<Response> failures to subsequent redirect tasks (#814)
Discussed in
https://github.com/swift-server/async-http-client/issues/753
2025-02-20 10:22:40 +00:00
Greg Cotten d05bf23650 Add head property to FileDownloadDelegate's Progress/Response struct (#811)
I needed a way to use a `FileDownloadDelegate` task to fish out the
recommended file name.
```swift
let response = try await downloadTask.get()

// access content-disposition
response.head.headers.first(name: "Content-Disposition")
```

The `head` property is an explicitly unwrapped optional because there is
no "default value" to set it to, and it won't be accessed by the user
until it's already been set anyway. This is a little inelegant, so I
could change it to something like below where I fill in bogus init data,
but that seems worse for some reason.
```swift
public struct Progress: Sendable {
    public var totalBytes: Int?
    public var receivedBytes: Int
    public var head: HTTPResponseHead
}

private var progress = Progress(
    totalBytes: nil,
    receivedBytes: 0,
    head: .init(
        version: .init(major: 0, minor: 0),
        status: .badRequest
    )
)
```
2025-02-18 15:18:55 +00:00
Johannes Weiss b645ad4082 fix 5.10 compile on Ubuntu 24.04 (Noble) for Intel (x86_64) (#810)
Specifically Swift 5.10 _on Intel on Ubuntu Noble (24.04)_ has a crazy
bug which leads to compilation failures in a `#if compiler(>=6.0)`
block: https://github.com/swiftlang/swift/issues/79285 .

This workaround fixes the compilation by _changing the whitespace_.

Thanks @gwynne for finding this workaround!

---------

Co-authored-by: Johannes Weiss <johannes@jweiss.io>
2025-02-11 12:25:31 +00:00
Johannes Weiss 89dc8d0068 baby steps towards a Structured Concurrency API (#806)
At the moment, `HTTPClient`'s entire API surface violates Structured
Concurrency. Both the creation & shutdown of a HTTP client as well as
making requests (#807) doesn't follow Structured Concurrency. Some of
the problems are:

1. Upon return of methods, resources are still in active use in other
threads/tasks
2. Cancellation doesn't always work

This PR is baby steps towards a Structured Concurrency API, starting
with start/shutdown of the HTTP client.

Co-authored-by: Johannes Weiss <johannes@jweiss.io>
2025-02-06 17:11:37 +00:00
Allan Shortlidge 60fa3dcfc5 Add missing import of Network module (#804) 2025-01-29 18:42:58 +00:00
Rick Newton-Rogers f38c2fea86 Avoid precondition failure in write timeout (#803)
### Motivation:

In some cases we can crash because of a precondition failure when the
write timeout fires and we aren't in the running state. This can happen
for example if the connection is closed whilst the write timer is
active.

### Modifications:

* Remove the precondition and instead take no action if the timeout
fires outside of the running state. Instead we take a new `Action`,
`.noAction` when the timer fires.
* Clear write timeouts upon request completion. When a request completes
we have no use for the idle write timer, we clear the read timer and we
should clear the write one too.

### Result:

Fewer crashes.

The supplied tests fails without these changes and passes with either of them.
2025-01-28 17:50:38 +00:00
Marc Prud'hommeaux e69318d4cb Android support (#799)
This PR adds support for Android, mostly just by importing the Android
module when needed.
2025-01-14 16:39:43 +00:00
Cory Benfield 126518507b Unbreak CI (#800)
# Motivation

The NIO 2.78 release introduced a bunch of new warnings. These warnings
cause us a bunch of trouble, so we should fix them.

# Modifications

Mostly use a bunch of assumeIsolated() and syncOperations.

# Result 

CI passes again.

Note that Swift 6 has _many_ more warnings than this, but we expect more
to come and we aren't using warnings-as-errors on that mode at the
moment. We'll be cleaning that up soon.
2025-01-14 16:34:21 +00:00
Rick Newton-Rogers dbd5c864ad Enable MemberImportVisibility check on all targets (#794)
Enable MemberImportVisibility check on all targets. Use a standard
string header and footer to bracket the new block for ease of updating
in the future with scripts.
2024-12-13 15:07:57 +01:00
Johannes Weiss 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-Rogers 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
Agam Dua acaca2d50d Added: ability to set basic authentication on requests (#778)
Motivation:

As an HTTP library, async-http-client should have authentication
support.

Modifications:

This adds a `setBasicAuth()` method to both HTTPClientRequest and
`HTTPClient.Request` and their related unit tests.

Result:

Library users will be able to leverage this method to use basic
authentication on their requests without implementing this in their own
projects.

Note: I also ran the tests (`swift test`) with the
`docker.io/library/swift:6.0-focal` and
`docker.io/library/swift:5.10.1-focal` to ensure linux compatibility.

Signed-off-by: Agam Dua <agam_dua@apple.com>
2024-10-21 16:27:28 +01:00
Rick Newton-Rogers 0a9b72369b workaround Foundation.URL behavior changes (#777)
`Foundation.URL` has various behavior changes in Swift 6 to better match
RFC 3986 which impact AHC.

In particular it now no longer strips the square brackets in IPv6 hosts
which are not tolerated by `inet_pton` so these must be manually
stripped.

https://github.com/swiftlang/swift-foundation/issues/957
https://github.com/swiftlang/swift-foundation/issues/958
https://github.com/swiftlang/swift-foundation/issues/962
2024-10-03 03:02:00 -07:00
Alastair Houghton 64abc77edf Don't just import locale_h. (#775)
On modularised platforms, #771 broke things because it changed from
importing `Musl` or `Glibc` to importing just `locale_h`. The latter
understandably doesn't define `errno` or `EOVERFLOW`, so we get a build
failure.

Fixes #773.
2024-09-30 10:17:01 -07:00
aryan-25 38608db985 Reduce time spent logging EventLoop description in HTTP1ClientChannelHandler (#772)
### Motivation:

A performance test executing 100,000 sequential requests against a
simple
[`NIOHTTP1Server`](https://github.com/apple/swift-nio/blob/main/Sources/NIOHTTP1Server/README.md)
revealed that 7% of total run time is spent in the setter of the
`request` property in `HTTP1ClientChannelHandler` (GitHub Issue #754).

The poor performance comes from [processing the string interpolation
`"\(self.eventLoop)"`](https://github.com/swift-server/async-http-client/blob/6df8e1c17e68f0f93de2443b8c8cafca9ddcc89a/Sources/AsyncHTTPClient/ConnectionPool/HTTP1/HTTP1ClientChannelHandler.swift#L39C17-L39C75)
which under the hood calls a computed property.

This problem can entirely be avoided by storing `eventLoop.description`
when initializing `HTTP1ClientChannelHandler`, and using that stored
value in `request`'s setter, rather than computing the property each
time.

### Modifications:

- Created a new property `let eventLoopDescription:
Logger.MetadataValue` in `HTTP1ClientChannelHandler` that stores the
description of the `eventLoop` argument that is passed into the
initializer.
- Replaced the string interpolation `"\(self.eventLoop)"` in `request`'s
setter with `self.eventLoopDescription`.

### Result:

`HTTP1ClientChannelHandler.eventLoop`'s `description` property is cached
upon initialization rather than being computed each time in the
`request` property's setter.

---------

Co-authored-by: Cory Benfield <lukasa@apple.com>
2024-09-23 04:31:45 -07:00
Anthony Doeraene 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
Ian Anderson 6df8e1c17e Explicitly import locale modules (#771)
The Darwin module is slowly being split up, and as it gets further
along, it will stop importing some of the split-out modules like the one
for locale.h that provides newlocale() and other locale API. However,
there's a wrinkle that on platforms with xlocale, it's xlocale.h that
provides most of the POSIX locale.h functions and not locale.h, so
prefer the xlocale module when available.
2024-09-14 07:55:55 +01:00
Fabian Fett 11205411bb Fix crash when writablity becomes false and races against finishing the http request (#768)
### Motivation

If the channel's writability changed to false just before we finished a
request, we currently run into a precondition.

### Changes

- Remove the precondition and handle the case appropiatly

### Result

A crash less.
2024-09-03 13:54:44 +02:00
Fabian Fett 776a1c2304 Fix NIO deprecations after update to 2.71.0 (#769)
`NIOTooManyBytesError` now requires the `maxBytes` in its initializer.
2024-09-03 13:36:24 +02:00
Ayush Garg 1290119b31 Assume http2 connection by default, instead of http1 (#758)
Since most of the servers now conform to http2, the change here updates
the behaviour of assuming the connection to be http2 and not http1 by
default. It will migrate to http1 if the server only supports http1.
One can set the `httpVersion` in `ClientConfiguration` to `.http1Only`
which will start with http1 instead of http2.

Additional Changes:

- Fixed an off by one error in the maximum additional general purpose
connection check
- Updated tests

---------

Co-authored-by: Ayush Garg <ayushgarg@apple.com>
Co-authored-by: David Nadoba <d_nadoba@apple.com>
Co-authored-by: Fabian Fett <fabianfett@apple.com>
2024-08-15 14:26:52 +02:00
aryan-25 4b7a68e997 Fix OOM issue when setting concurrentHTTP1ConnectionsPerHostSoftLimit to Int.max (#763)
### Motivation:

When a user wishes to make the connection pool create as many concurrent
connections as possible, a natural way to achieve this would be to set
`.max` to the `concurrentHTTP1ConnectionsPerHostSoftLimit` property.

```swift
HTTPClient.Configuration().connectionPool = .init(
    idleTimeout: .hours(1),
    concurrentHTTP1ConnectionsPerHostSoftLimit: .max
)
```

The `concurrentHTTP1ConnectionsPerHostSoftLimit` property is of type
`Int`. Setting it to `Int.max` leads to `Int.max` being passed as an
argument to `Array`s `.reserveCapacity(_:)` method, causing an OOM
issue.

Addresses Github Issue #751 

### Modifications:

Capped the argument to `self.connections.reserveCapacity(_:)` to 1024 in
`HTTPConnectionPool.HTTP1Connections`

### Result:

Users can now set the `concurrentHTTP1ConnectionsPerHostSoftLimit`
property to `.max` without causing an OOM issue.
2024-08-14 11:56:26 +02:00
Johannes Weiss 0bd9111ca7 mark HTTPClient.Response Sendable (#759)
`HTTPClient.Response` is trivially `Sendable`, let's mark it `Sendable`.
2024-08-07 08:28:39 -07:00
aryan-25 54d1006dc9 Add leading slash in relative URL requests if necessary (#747) 2024-07-09 12:47:07 +02:00
aryan-25 07536f6a4e Resolve ambiguity issue for the stream function, remove @deprecated marking from the original implementation, and make argument labels consistent. (#749) 2024-07-03 16:14:47 +01:00
aryan-25 4316ecae09 Add support for request body to be larger than 2GB on 32-bit devices (#746)
### Motivation:

- The properties that store the request body length and the cumulative number of bytes sent as part of a request are of type `Int`. 
- On 32-bit devices, when sending requests larger than `Int32.max`, these properties overflow and cause a crash.
- To solve this problem, the properties should use the explicit `Int64` type.

### Modifications:

- Changed the type of the `known` field of the `RequestBodyLength` enum to `Int64`.
- Changed the type of `expectedBodyLength` and `sentBodyBytes` in `HTTPRequestStateMachine` to `Int64?` and `Int64` respectively.
- Deprecated the `public var length: Int?` property of `HTTPClient.Body` and backed it with a new property: `contentLength: Int64?`
  - Added a new initializer and "overloaded" the `stream` function in `HTTPClient.Body` to work with the new `contentLength` property.
  - **Note:** The newly added `stream` function has different parameter names (`length` -> `contentLength` and `stream` -> `bodyStream`) to avoid ambiguity problems.
- Added a test case that streams a 3GB request -- verified this fails with the types of the properties set explicitly to `Int32`. 

### Result:

- 32-bit devices can send requests larger than 2GB without integer overflow issues.
2024-06-28 11:33:04 +02:00
Andreas Ley 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
Mahdi Bahrami 0ae99db85b Increase decompression limit ratio 10 -> 25 (#740)
Co-authored-by: Franz Busch <f.busch@apple.com>
Co-authored-by: Cory Benfield <lukasa@apple.com>
2024-05-09 05:23:45 -07:00
Gustavo Cairo a22083713e Disable SETTINGS_ENABLE_PUSH HTTP/2 setting (#741) 2024-05-01 12:49:09 +01:00
Johannes Weiss 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 Cairo 83f015bf94 Fix write timeout not being initialised (#730) 2024-03-21 16:18:22 +00:00
Alastair Houghton 09b7eb751e Add support for Musl. (#726)
Motivation:

We would like to make this work for Musl so that we can build fully
statically linked binaries that use AsyncHTTPClient.

Modifications:

Define `_GNU_SOURCE` as a compiler argument; doing it in a source file
doesn't work properly with modular headers.

Add imports of `Musl` in appropriate places.

`Musl` doesn't have `strptime_l`, so avoid using that.

Result:

async-http-client will build for Musl.
2024-01-19 16:09:35 -08:00
Gustavo Cairo 5ccda442f1 Use the given connection pool idle timeout in the HTTPClient.Configuration inits (#723) 2023-12-21 09:51:54 -03:00
Gustavo Cairo ffe36fcf54 Fix potential race conditions when cancelling read/write idle timers (#720) 2023-12-18 10:16:27 -03:00
Si Beaumont a4904fcc6b Add missing availability guards in tests (#719)
## Motivation

Some of the test code was missing availability guards for Apple platforms, resulting in build failures for these platforms, e.g.

```
error: 'AsyncSequence' is only available in iOS 13.0 or newer
```

## Modifications

Add missing availability guards. I've tried to keep them as scoped as possible.

## Result

Tests can now build for run on iOS and other Apple platforms.
2023-12-18 04:52:50 -08:00
Gustavo Cairo d2d35663a2 Add an idle write timeout (#718) 2023-12-18 09:06:06 -03:00
Mahdi Bahrami 4824907382 Fix wrong/outdated connect timeout documentation (#714) 2023-10-25 08:19:44 -07:00
Johannes Weiss d766674c7e HTTPClientRequest: allow custom TLS config (#709) 2023-10-16 07:00:58 -07:00
Rick Newton-Rogers 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 Nadoba 4e74fefd1c Support custom backgroundActivityLogger with using the default ELG. (#711) 2023-09-06 13:49:25 +01:00
David Nadoba 75d7f63abf Automatically chunk large request bodies (#710)
* ChunkedCollection

* Use swift-algorithms

* SwiftFormat

* test chunking

* add documentation

* SwiftFormat

* fix old swift versions

* fix older swift versions

second attempt

* fix old swift versions

third attempt

* fix old swift versions

fourth attempt

* update documentation
2023-09-06 11:08:09 +01:00
Johannes Weiss 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
brenno 668c193451 Removed duplicated code (#694)
Co-authored-by: Franz Busch <f.busch@apple.com>
2023-06-26 12:13:50 +02:00
brenno c7fb77557c Replace os() with canImport(Darwin) (#693)
* Replace os() with canImport(Darwin)

* Remove compilation conditional since it'll always be true
2023-06-26 00:22:30 -07:00
David Nadoba 78db67e5bf Tolerate new request after connection error happened (#688)
* Tolerate new request after connection error happened

* fix tests
2023-05-17 09:37:35 +01:00
David Nadoba d62c475401 Drop Swift 5.5 (#686) 2023-04-14 17:05:09 +01:00
Corey 333e60cc90 Fix building transaction extension for Apple Platforms (#685) 2023-04-14 06:03:14 -07:00
David Nadoba b9029ef67c Add support for custom cancellation error (#683) 2023-04-13 17:00:03 +01:00