28 Commits
Author SHA1 Message Date
Fabian Fett bff4b6903c Regression fix: HTTPField.Name.init(parsed:) rejects uppercased names again (#155)
### Motivation

#144 introduced a regression in `HTTPField.Name(parsed:)`.

HTTP/2 and HTTP/3 require field names to be lowercased on the wire (RFC
9113 § 8.2.1, RFC 9114 § 4.1.2). Previously we enforced this by only
accepting lowercase ASCII letters.

In #144 we replaced a specific scan with `HTTPField.isValidToken`, which
is shared in multiple token validation places and returns `valid` for
uppercased names. We checked for `isValid`, which is true for `valid` as
well as `canonical` representations. Allowing `valid` and not just
`canonical` representations is the regression.

This lead to this unwanted behavior:
- `HTTPField.Name(parsed: "Content-Type")` now succeeds, so a decoder
can no longer detect the malformed message it has to reject.
- The returned name violates the documented invariant that
`canonicalName` is lowercased, so it compares and hashes unequal to
`HTTPField.Name("Content-Type")`, which does canonicalise.

This path had no test coverage, so neither regression was caught.

### Changes

Require `.canonical` rather than merely valid token validity in
`init(parsed:)`, restoring the pre-#144 behavior, and document why. Add
`HTTPFieldNameTests` covering both initializers: valid names, pseudo
header fields, invalid characters, the mixed-case canonicalisation that
`init(_:)` performs, and the uppercase rejection that `init(parsed:)`
performs.

### Result

`init(parsed:)` rejects uppercased names again and `canonicalName` is
lowercased for every name it returns.
2026-09-07 09:56:43 +02:00
Hashim Khan dfebed0b4e Make HTTPFields hashing consistent with equality (#143)
## Summary

HTTPFields equality does not depend on the order of differently named
fields. Hashing used storage order, so equal values could hash
differently.

This hashes each name group in relative order, then mixes those group
hashes without depending on name order. The existing hash test now
asserts equal hashes.

Fixes #139
2026-09-03 13:55:14 +00:00
Mathias QuinteroandFabian Fett f41c097554 Improve Performance of HTTPField.value (#146)
### Motivation

When instantiating new HTTPFields, the values need to be validated,
legalized and turned into the internal representation.
We use this representation to expose a string in `HTTPField.value` as
well as to iterate over raw bytes using
`HTTPField.withUnsafeBytesOfValue`.

Currently the internal representation is to use a ISO Latin 1 String to
store the bytes only once. This however means that we need to transcode
back and forth for non ascii values, which causes a lot of extra
computations and allocations.

### Modifications

1. Moved logic to a standalone structure for `HTTPField.Value` rather
than `ISOLatin1String`
2. Added Unit Tests and Benchmarks
3. Swapped internal storage of values to be an enum of either a string
or a value
4. Whenever the bytes stored are valid utf8, we will not always store
the value as a raw string. This means that we need the least amount of
allocations and transcoding when operating with the value through our
public API
5. We only store a raw array of bytes whenever the bytes are not valid
utf8. This speeds up `withUnsafeBytesOfValue` massively and
`HTTPField.value` a little bit
6. During legalization, store the modified bytes in a temporary buffer
instead of using lazy collections to avoid allocating an array and speed
up utf8 validation
7. Use spans whenever possible to speed up access to bytes without
boundary checks

### Result

These are the changes on the benchmarks added in this PR

```
HTTPField-asciiBytesValueRoundTrip
Instructions: 28K -> 6847 (-75%)
Malloc: 1 -> 1

HTTPField-asciiValueRoundTrip
Instructions: 7367 -> 3081 (-58%)
Malloc: 0 -> 0

HTTPField-invalidAsciiBytesValueRoundTrip
Instructions: 133K -> 7255 (-94%)
Malloc: 6 -> 1 (-83%)

HTTPField-invalidAsciiBytesValueRoundTripAsLenientValue
Instructions: 62K -> 6623 (-89%)
Malloc: 3 -> 1 (-66%)

HTTPField-invalidAsciiValueRoundTrip
Instructions: 93K -> 6959 (-92%)
Malloc: 5 -> 1 (-80%)

HTTPField-invalidNonASCIIValueRoundTrip
Instructions: 393K -> 8735 (-97%)
Malloc: 11 -> 1 (-91%)

HTTPField-nonASCIIValueRoundTrip
Instructions: 131K -> 3219 (-97%)
Malloc: 4 -> 0 (-100%)
```

---------

Co-authored-by: Fabian Fett <fabianfett@apple.com>
2026-08-31 11:30:56 +02:00
Eric RosenbergandFabian Fett 6226da4bb9 Add HTTP/2 and HTTP/3 error code types (#148)
### Motivation

HTTP/2 and HTTP/3 both carry a numeric error code to say why a stream or
a connection was terminated. HTTPTypes has no currency type for either,
so adopters passed bare integers around or defined their own.

### Modifications

Add HTTP2ErrorCode and HTTP3ErrorCode.

### Result

HTTP/2 and HTTP/3 error codes are first-class currency types.

---------

Co-authored-by: Fabian Fett <fabianfett@apple.com>
2026-08-29 00:04:31 +01:00
Mathias Quintero c12e12f7c5 Adding tests for HTTP Field Value Handling (#147)
### Motivation

In #146 we're going to be revamping the internal structure and
implementation of how we store values for http fields. To do it safely,
I want to add a few tests to make sure we do not regress it

### Modifications

Added tests for all the edge cases I could come up with for HTTP Field
Values

### Result

Tests Pass
2026-08-28 12:45:31 +00:00
Fabian Fett e186752bbc Improve equality check performance (#141)
### Motivation

#138 introduced code paths that scale quadratically when running an
equality check.

### Changes

- Introduce a new lock-step equality check that performs great for small
fields and fields that share roughly the same order
- Introduce a new lookup equality check that scales linear for vastly
different field orders

### Result

Faster equality checks
2026-08-18 14:46:30 +00:00
Fabian Fett 43cf84fb56 Reimplement HTTPFields on top of a simple Array (#138)
### Motivation

We like faster and simpler code. When optimizing performance we focus on
often used use-cases.

### Changes

- Reimplement HTTPFields on top of a simple Array

### Result

- HTTPFields building becomes significantly faster
- HTTPFields lookups are faster for small fields, and only slightly
slower for larger (nearly always offset by reduced build time)

Follow ups:

- Make the equality implementation smarter
2026-08-18 14:26:31 +02:00
Fabian Fett 4b1dd0cab3 Add hash tests, incl. a known failing one (#137)
### Motivation

The more we test the more we know about current behavior

### Changes

- Add tests for hashing, incl. a failing one

### Result

We know that our hash implementation isn't respecting different header
order
2026-08-14 12:56:29 +02:00
Fabian Fett 7d65507593 Move to swift-testing (#135)
### Motivation

We want to refactor some internals of this library. For this we need
tests. The existing tests are written with XCTest. Given that the total
number of test is relatively small, let's take the chance and move to
swift-testing before we add further tests.

### Changes

- Move all tests from XCTest to swift-testing
- This is a mechanical change

### Result

All tests use the test framework of the future.
2026-08-13 10:59:18 +02:00
Guoye Zhang f14fbc0e56 Add FoundationURL trait (#118)
Add a default enabled trait "FoundationURL" that can be disabled for
users who don't want Foundation dependencies. Also enable
`InternalImportsByDefault`.
2026-06-01 11:57:33 -07:00
Guoye Zhang 45eb022491 Stop using Foundation on platforms with FoundationEssentials (#108)
Resolves #107
2025-10-21 08:44:07 -07:00
Guoye Zhang 3acdb1c8e8 Move HTTPRequest.url to the main library (#79)
Resolves #76
2025-10-17 07:51:03 +01:00
Guoye ZhangandCory Benfield 9c9d5ea7e3 Convert WebSocket URLRequest to RFC8441 HTTPRequest (#92)
WebSocket uses the extended-CONNECT form in HTTP/2 and HTTP/3. When
creating an HTTPRequest from a URLRequest, we should translate it into
the new form.

`:method`: `GET` -> `CONNECT`
`:scheme`: `wss`/`ws` -> `https`/`http`
`:protocol`: `websocket`

HTTPTypes defaults to modern HTTP versions, and the expectation is that
the HTTP/1 serializer has to translate the extended-CONNECT request back
to the old request form.

---------

Co-authored-by: Cory Benfield <lukasa@apple.com>
2025-10-05 11:13:17 -07:00
Guoye Zhang e8011a7bc3 PseudoHeaderFields adopting CoW to reduce size (#88)
HTTPRequest size 288 -> 16
HTTPResponse size 80 -> 16

rdar://144520456
2025-02-21 17:30:57 -08:00
Guoye Zhang 40ca463b5f Request.url should return nil for situations where it is not applicable (#84)
Regular CONNECT requests and OPTIONS * requests do not have URLs
2025-01-29 07:10:41 +00:00
Guoye Zhang 74e4995e39 Support building without CoreFoundation on Windows 2024-10-28 17:16:02 -07:00
Franz Busch f3d9ec3eef [CI] Make all CI checks green
# Motivation

We just adopted GH actions and want to make sure all the CI checks are green.

# Modification

This PR makes the necessary changes to pass all checks. Including:

- Adding a licenseignore file
- Moving `swift-format` and adopting the config that we use in all other swift on server repos.

# Result

Green CI.
2024-10-24 22:30:18 +02:00
Guoye Zhang 291e929d94 Add conveniences for modern HTTP parsers 2024-06-18 20:22:37 -07:00
Guoye Zhang 20d5ed91ae Support lenient legalization of header field values 2024-04-23 17:31:54 -07:00
Guoye Zhang 43107b6de1 Update contributors 2023-09-19 12:24:27 -07:00
Guoye Zhang 4105126730 Rename raw subscript to values 2023-09-02 12:48:20 -07:00
Guoye Zhang bd06898009 Merge pull request #23 from guoye-zhang/codable
Support Codable
2023-08-17 15:37:47 -07:00
Guoye Zhang 792abeab1f Support Codable 2023-08-07 18:09:53 -07:00
Guoye Zhang 4c7d1331a5 Field order should be insignificant between fields of different names 2023-08-07 16:14:56 -07:00
Guoye Zhang 79ea5d0bc5 Update format and contributors 2023-07-14 16:03:19 -07:00
Tomohiro Kumagai 57140e3abc Make Request.Method and Response.Status conform to Sendable. 2023-07-14 22:53:32 +09:00
Guoye Zhang ae848811f9 Internal API review feedback 2023-06-30 10:06:35 -07:00
Guoye Zhang c164739694 Import code 2023-06-30 10:06:35 -07:00