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>
This commit is contained in:
Mathias QuinteroandFabian Fett authored and GitHub committed 2026-08-31 11:30:56 +02:00
1 parent 6226da4bb9
commit f41c097554
43 files changed
+730 -211

No files matched your search

@@ -107,9 +107,14 @@ let sampleURL = URL(string: "https://www.example.com/api/v1/items?page=2&limit=5
let sampleRequest = HTTPRequest(method: .get, url: sampleURL, headerFields: sampleRequestFields)
let asciiFieldValue = "attachment; filename=\"quarterly-report-2026.pdf\""
/// A field value that is not representable as ASCII, exercising the ISO-Latin-1 slow paths.
let latin1FieldValue = "attachment; filename=\"Übergrößenträger-Prüfbericht-2026.pdf\""
let invalidAsciiFieldValue = "\0 \t\(asciiFieldValue)\n\(asciiFieldValue)\t\r"
let invalidLatin1FieldValue = "\0 \t\(latin1FieldValue)\n\(latin1FieldValue)\t\r"
extension HTTPField.Name {
/// `isPseudo` is internal to HTTPTypes, so recompute it here.
fileprivate var isPseudoName: Bool {
@@ -144,7 +149,23 @@ let benchmarks: @Sendable () -> Void = {
}
}
// MARK: ISO-Latin-1 values
// MARK: HTTPField.Value handling
Benchmark("HTTPField-asciiValueRoundTrip", configuration: makeDefaultConfiguration()) { benchmark in
for _ in benchmark.scaledIterations {
let field = HTTPField(name: .contentDisposition, value: asciiFieldValue)
blackHole(field.value)
field.withUnsafeBytesOfValue { blackHole($0.count) }
}
}
Benchmark("HTTPField-asciiBytesValueRoundTrip", configuration: makeDefaultConfiguration()) { benchmark in
for _ in benchmark.scaledIterations {
let field = HTTPField(name: .contentDisposition, value: asciiFieldValue.utf8)
blackHole(field.value)
field.withUnsafeBytesOfValue { blackHole($0.count) }
}
}
Benchmark("HTTPField-nonASCIIValueRoundTrip", configuration: makeDefaultConfiguration()) { benchmark in
for _ in benchmark.scaledIterations {
@@ -154,6 +175,50 @@ let benchmarks: @Sendable () -> Void = {
}
}
Benchmark(
"HTTPField-invalidAsciiValueRoundTrip",
configuration: makeDefaultConfiguration()
) { benchmark in
for _ in benchmark.scaledIterations {
let field = HTTPField(name: .contentDisposition, value: invalidAsciiFieldValue)
blackHole(field.value)
field.withUnsafeBytesOfValue { blackHole($0.count) }
}
}
Benchmark(
"HTTPField-invalidAsciiBytesValueRoundTrip",
configuration: makeDefaultConfiguration()
) { benchmark in
for _ in benchmark.scaledIterations {
let field = HTTPField(name: .contentDisposition, value: invalidAsciiFieldValue.utf8)
blackHole(field.value)
field.withUnsafeBytesOfValue { blackHole($0.count) }
}
}
Benchmark(
"HTTPField-invalidAsciiBytesValueRoundTripAsLenientValue",
configuration: makeDefaultConfiguration()
) { benchmark in
for _ in benchmark.scaledIterations {
let field = HTTPField(name: .contentDisposition, lenientValue: invalidAsciiFieldValue.utf8)
blackHole(field.value)
field.withUnsafeBytesOfValue { blackHole($0.count) }
}
}
Benchmark(
"HTTPField-invalidNonASCIIValueRoundTrip",
configuration: makeDefaultConfiguration()
) { benchmark in
for _ in benchmark.scaledIterations {
let field = HTTPField(name: .contentDisposition, value: invalidLatin1FieldValue.utf8)
blackHole(field.value)
field.withUnsafeBytesOfValue { blackHole($0.count) }
}
}
// MARK: URL conversion
Benchmark("HTTPRequest(method,url)-requestFromURL", configuration: makeDefaultConfiguration()) { benchmark in
@@ -0,0 +1,3 @@
{
"mallocCountTotal" : 17
}
@@ -0,0 +1,3 @@
{
"mallocCountTotal" : 16
}
@@ -0,0 +1,3 @@
{
"mallocCountTotal" : 17
}
@@ -0,0 +1,3 @@
{
"mallocCountTotal" : 17
}
@@ -0,0 +1,3 @@
{
"mallocCountTotal" : 17
}
@@ -0,0 +1,3 @@
{
"mallocCountTotal" : 17
}
@@ -1,3 +1,3 @@
{
"mallocCountTotal" : 20
"mallocCountTotal" : 16
}
@@ -0,0 +1,3 @@
{
"mallocCountTotal" : 17
}
@@ -0,0 +1,3 @@
{
"mallocCountTotal" : 16
}
@@ -0,0 +1,3 @@
{
"mallocCountTotal" : 17
}
@@ -0,0 +1,3 @@
{
"mallocCountTotal" : 17
}
@@ -0,0 +1,3 @@
{
"mallocCountTotal" : 17
}
@@ -0,0 +1,3 @@
{
"mallocCountTotal" : 17
}
@@ -1,3 +1,3 @@
{
"mallocCountTotal" : 20
"mallocCountTotal" : 16
}
@@ -0,0 +1,3 @@
{
"mallocCountTotal" : 19
}
@@ -0,0 +1,3 @@
{
"mallocCountTotal" : 18
}
@@ -0,0 +1,3 @@
{
"mallocCountTotal" : 19
}
@@ -0,0 +1,3 @@
{
"mallocCountTotal" : 19
}
@@ -0,0 +1,3 @@
{
"mallocCountTotal" : 19
}
@@ -0,0 +1,3 @@
{
"mallocCountTotal" : 19
}
@@ -1,3 +1,3 @@
{
"mallocCountTotal" : 23
"mallocCountTotal" : 18
}
@@ -0,0 +1,3 @@
{
"mallocCountTotal" : 19
}
@@ -0,0 +1,3 @@
{
"mallocCountTotal" : 18
}
@@ -0,0 +1,3 @@
{
"mallocCountTotal" : 19
}
@@ -0,0 +1,3 @@
{
"mallocCountTotal" : 19
}
@@ -0,0 +1,3 @@
{
"mallocCountTotal" : 19
}
@@ -1,3 +1,3 @@
{
"mallocCountTotal" : 23
"mallocCountTotal" : 18
}
@@ -0,0 +1,3 @@
{
"mallocCountTotal" : 19
}
@@ -0,0 +1,3 @@
{
"mallocCountTotal" : 18
}
@@ -0,0 +1,3 @@
{
"mallocCountTotal" : 19
}
@@ -0,0 +1,3 @@
{
"mallocCountTotal" : 19
}
@@ -0,0 +1,3 @@
{
"mallocCountTotal" : 19
}
@@ -1,3 +1,3 @@
{
"mallocCountTotal" : 23
"mallocCountTotal" : 18
}
+15 -87
View File
@@ -57,7 +57,7 @@ public struct HTTPField: Sendable, Hashable {
/// Invalid bytes are converted into space characters.
public init(name: Name, value: String) {
self.name = name
self.rawValue = Self.legalizeValue(ISOLatin1String(value))
self.rawValue = HTTPField.Value(legalize: value)
}
/// Create an HTTP field from a name and a value.
@@ -66,7 +66,7 @@ public struct HTTPField: Sendable, Hashable {
/// - value: The HTTP field value. Invalid bytes are converted into space characters.
public init(name: Name, value: some Collection<UInt8>) {
self.name = name
self.rawValue = Self.legalizeValue(ISOLatin1String(value))
self.rawValue = HTTPField.Value(legalize: value)
}
/// Create an HTTP field from a name and a value. Leniently legalize the value.
@@ -77,10 +77,10 @@ public struct HTTPField: Sendable, Hashable {
@available(HTTPTypes 1.1, *)
public init(name: Name, lenientValue: some Collection<UInt8>) {
self.name = name
self.rawValue = Self.lenientLegalizeValue(ISOLatin1String(lenientValue))
self.rawValue = HTTPField.Value(lenient: lenientValue)
}
init(name: Name, uncheckedValue: ISOLatin1String) {
init(name: Name, uncheckedValue: HTTPField.Value) {
self.name = name
self.rawValue = uncheckedValue
}
@@ -101,7 +101,7 @@ public struct HTTPField: Sendable, Hashable {
self.rawValue.string
}
set {
self.rawValue = Self.legalizeValue(ISOLatin1String(newValue))
self.rawValue = HTTPField.Value(legalize: newValue)
}
}
@@ -123,76 +123,7 @@ public struct HTTPField: Sendable, Hashable {
/// The strategy for whether the field is indexed in the HPACK or QPACK dynamic table.
public var indexingStrategy: DynamicTableIndexingStrategy = .automatic
var rawValue: ISOLatin1String
private static func _isValidValue(_ bytes: some Sequence<UInt8>) -> Bool {
var iterator = bytes.makeIterator()
guard var byte = iterator.next() else {
// Empty string is allowed.
return true
}
if byte == 0x09 || byte == 0x20 {
// First character cannot be a space or a tab.
return false
}
while true {
switch byte {
case 0x09, 0x20:
break
case 0x21...0x7E, 0x80...0xFF:
break
default:
return false
}
if let next = iterator.next() {
byte = next
} else {
break
}
}
if byte == 0x09 || byte == 0x20 {
// Last character cannot be a space or a tab.
return false
}
return true
}
static func legalizeValue(_ value: ISOLatin1String) -> ISOLatin1String {
if self._isValidValue(value._storage.utf8) {
return value
} else {
let bytes = value._storage.utf8.lazy.map { byte -> UInt8 in
switch byte {
case 0x09, 0x20:
return byte
case 0x21...0x7E, 0x80...0xFF:
return byte
default:
return 0x20
}
}
let trimmed = bytes.reversed().drop { $0 == 0x09 || $0 == 0x20 }.reversed().drop {
$0 == 0x09 || $0 == 0x20
}
return ISOLatin1String(unchecked: String(decoding: trimmed, as: UTF8.self))
}
}
static func lenientLegalizeValue(_ value: ISOLatin1String) -> ISOLatin1String {
if value._storage.utf8.allSatisfy({ $0 != 0x00 && $0 != 0x0A && $0 != 0x0D }) {
return value
} else {
let bytes = value._storage.utf8.lazy.map { byte -> UInt8 in
switch byte {
case 0x00, 0x0A, 0x0D:
return 0x20
default:
return byte
}
}
return ISOLatin1String(unchecked: String(decoding: bytes, as: UTF8.self))
}
}
var rawValue: HTTPField.Value
/// Whether the string is valid for an HTTP field value based on RFC 9110.
///
@@ -201,7 +132,7 @@ public struct HTTPField: Sendable, Hashable {
/// - Parameter value: The string to validate.
/// - Returns: Whether the string is valid.
public static func isValidValue(_ value: String) -> Bool {
self._isValidValue(value.utf8)
Self.Value.isValid(value)
}
/// Whether the byte collection is valid for an HTTP field value based on RFC 9110.
@@ -211,7 +142,7 @@ public struct HTTPField: Sendable, Hashable {
/// - Parameter value: The byte collection to validate.
/// - Returns: Whether the byte collection is valid.
public static func isValidValue(_ value: some Collection<UInt8>) -> Bool {
self._isValidValue(value)
Self.Value.isValid(value)
}
}
@@ -242,7 +173,7 @@ extension HTTPField: Codable {
public func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encode(self.name, forKey: .name)
try container.encode(self.rawValue._storage, forKey: .value)
try container.encode(self.rawValue, forKey: .value)
if self.indexingStrategy != .automatic {
try container.encode(self.indexingStrategy.rawValue, forKey: .indexingStrategy)
}
@@ -251,15 +182,8 @@ extension HTTPField: Codable {
public init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
let name = try container.decode(Name.self, forKey: .name)
let value = try container.decode(String.self, forKey: .value)
guard value.unicodeScalars.allSatisfy({ $0.value <= UInt8.max }) && Self.isValidValue(value) else {
throw DecodingError.dataCorruptedError(
forKey: .value,
in: container,
debugDescription: "HTTP field value \"\(value)\" contains invalid characters"
)
}
self.init(name: name, uncheckedValue: ISOLatin1String(unchecked: value))
let value = try container.decode(HTTPField.Value.self, forKey: .value)
self.init(name: name, uncheckedValue: value)
if let indexingStrategyValue = try container.decodeIfPresent(UInt8.self, forKey: .indexingStrategy),
let indexingStrategy = DynamicTableIndexingStrategy(rawValue: indexingStrategyValue)
{
@@ -318,6 +242,10 @@ extension HTTPField {
Self.tokenValidity(token).isValid
}
static func isValidToken(_ bytes: some Collection<UInt8>) -> Bool {
Self.tokenValidity(bytes).isValid
}
#if compiler(>=6.3) && !(os(watchOS) && _pointerBitWidth(_32))
private static func tokenValidity(_ token: String) -> TokenValidity {
#if canImport(Darwin)
+490
View File
@@ -0,0 +1,490 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift open source project
//
// Copyright (c) 2023 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See CONTRIBUTORS.txt for the list of Swift project authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//
extension HTTPField {
struct Value: Sendable {
fileprivate enum Storage: Equatable {
case string(String)
case bytes([UInt8])
}
private let _storage: Storage
init(unchecked: String) {
self._storage = .string(unchecked)
}
var string: String {
switch self._storage {
case .string(let string):
return string
case .bytes(let bytes):
return String(decoding: bytes, as: UTF8.self)
}
}
func withUnsafeBytes<Return, Failure: Error>(
_ body: (UnsafeBufferPointer<UInt8>) throws(Failure) -> Return
) throws(Failure) -> Return {
switch self._storage {
case .string(var string):
return try string.withUTF8 { buffer in
Result { () throws(Failure) in
try body(buffer)
}
}.get()
case .bytes(let array):
return try array.withUnsafeBufferPointer { buffer throws(Failure) in
try Result { () throws(Failure) in
try body(buffer)
}.get()
}
}
}
}
}
extension HTTPField.Value: Hashable {
static func == (lhs: Self, rhs: Self) -> Bool {
switch (lhs._storage, rhs._storage) {
case (.string(let lhs), .string(let rhs)):
return lhs == rhs
case (.bytes(let lhs), .bytes(let rhs)):
return lhs == rhs
default:
return lhs.withUnsafeBytes { lhsBytes in
rhs.withUnsafeBytes { rhsBytes in
lhsBytes.elementsEqual(rhsBytes)
}
}
}
}
func hash(into hasher: inout Hasher) {
self.withUnsafeBytes { buffer in
for byte in buffer {
hasher.combine(byte)
}
}
}
}
extension HTTPField.Value {
init(legalize value: String) {
if Self.isValid(value) {
self._storage = .string(value)
} else {
#if compiler(>=6.2) && !(os(watchOS) && _pointerBitWidth(_32))
#if canImport(Darwin)
if #available(macOS 26.0, iOS 26.0, watchOS 26.0, tvOS 26.0, visionOS 26.0, *) {
self._storage = Self.legalize(from: value.utf8.span)
return
}
self._storage = Self.legalize(from: value.utf8)
#else
self._storage = Self.legalize(from: value.utf8.span)
#endif
#else
self._storage = Self.legalize(from: value.utf8)
#endif
}
}
init(legalize bytes: some Collection<UInt8>) {
if Self.isValid(bytes) {
self._storage = .init(from: bytes)
} else {
self._storage = Self.legalize(from: bytes)
}
}
init(lenient bytes: some Collection<UInt8>) {
if Self.isLenient(bytes) {
self._storage = .init(from: bytes)
} else {
self._storage = Self.cleanUpAsLenient(from: bytes)
}
}
}
extension HTTPField.Value {
#if compiler(>=6.2) && !(os(watchOS) && _pointerBitWidth(_32))
static func isValid(_ string: String) -> Bool {
#if canImport(Darwin)
if #available(macOS 26.0, iOS 26.0, watchOS 26.0, tvOS 26.0, visionOS 26.0, *) {
return isLegal(string.utf8.span)
}
return isLegal(string.utf8)
#else
return isLegal(string.utf8.span)
#endif
}
#else
static func isValid(_ string: String) -> Bool {
isLegal(string.utf8)
}
#endif
static func isValid(_ bytes: some Collection<UInt8>) -> Bool {
isLegal(bytes)
}
var isValidToken: Bool {
switch self._storage {
case .string(let string):
return HTTPField.isValidToken(string)
case .bytes(let bytes):
return HTTPField.isValidToken(bytes)
}
}
}
#if !hasFeature(Embedded)
extension HTTPField.Value: Codable {
public func encode(to encoder: Encoder) throws {
var container = encoder.singleValueContainer()
try container.encode(isoLatin1)
}
public init(from decoder: Decoder) throws {
let container = try decoder.singleValueContainer()
let isoLatin1 = try container.decode(String.self)
guard isoLatin1.unicodeScalars.allSatisfy({ $0.value <= UInt8.max }) && Self.isValid(isoLatin1) else {
throw DecodingError.dataCorruptedError(
in: container,
debugDescription: "HTTP field value \"\(isoLatin1)\" contains invalid characters"
)
}
self.init(fromISOLatin1: isoLatin1)
}
}
#endif
extension HTTPField.Value {
fileprivate static func isLegal(_ bytes: some Sequence<UInt8>) -> Bool {
#if compiler(>=6.2)
#if canImport(Darwin)
if #available(macOS 26.0, iOS 26.0, watchOS 26.0, tvOS 26.0, visionOS 26.0, *) {
let optimized = bytes.withContiguousStorageIfAvailable { isLegal($0.span) }
if let optimized {
return optimized
}
}
#else
let optimized = bytes.withContiguousStorageIfAvailable { isLegal($0.span) }
if let optimized {
return optimized
}
#endif
#endif
var iterator = bytes.makeIterator()
guard var byte = iterator.next() else {
// Empty string is allowed.
return true
}
if byte == 0x09 || byte == 0x20 {
// First character cannot be a space or a tab.
return false
}
while true {
switch byte {
case 0x09, 0x20:
break
case 0x21...0x7E, 0x80...0xFF:
break
default:
return false
}
if let next = iterator.next() {
byte = next
} else {
break
}
}
if byte == 0x09 || byte == 0x20 {
// Last character cannot be a space or a tab.
return false
}
return true
}
#if compiler(>=6.2)
@available(macOS 26.0, iOS 26.0, watchOS 26.0, tvOS 26.0, visionOS 26.0, *)
fileprivate static func isLegal(_ bytes: borrowing Span<UInt8>) -> Bool {
for index in bytes.indices {
switch bytes[index] {
case 0x09, 0x20:
if index == 0 || index == bytes.count - 1 {
return false
}
break
case 0x21...0x7E, 0x80...0xFF:
break
default:
return false
}
}
return true
}
#endif
fileprivate static func isLenient(_ bytes: some Sequence<UInt8>) -> Bool {
#if compiler(>=6.2)
#if canImport(Darwin)
if #available(macOS 26.0, iOS 26.0, watchOS 26.0, tvOS 26.0, visionOS 26.0, *) {
let optimized = bytes.withContiguousStorageIfAvailable { isLenient($0.span) }
if let optimized {
return optimized
}
}
#else
let optimized = bytes.withContiguousStorageIfAvailable { isLenient($0.span) }
if let optimized {
return optimized
}
#endif
#endif
return bytes.allSatisfy { $0 != 0x00 && $0 != 0x0A && $0 != 0x0D }
}
#if compiler(>=6.2)
@available(macOS 26.0, iOS 26.0, watchOS 26.0, tvOS 26.0, visionOS 26.0, *)
fileprivate static func isLenient(_ bytes: Span<UInt8>) -> Bool {
for index in bytes.indices {
switch bytes[index] {
case 0x00, 0x0A, 0x0D:
return false
default:
continue
}
}
return true
}
#endif
}
extension HTTPField.Value {
private static func legalize(from bytes: some Collection<UInt8>) -> Storage {
#if compiler(>=6.2)
#if canImport(Darwin)
if #available(macOS 26.0, iOS 26.0, watchOS 26.0, tvOS 26.0, visionOS 26.0, *) {
let optimized = bytes.withContiguousStorageIfAvailable { legalize(from: $0.span) }
if let optimized {
return optimized
}
}
#else
let optimized = bytes.withContiguousStorageIfAvailable { legalize(from: $0.span) }
if let optimized {
return optimized
}
#endif
#endif
return withUnsafeTemporaryAllocation(of: UInt8.self, capacity: bytes.count) { buffer in
var index = 0
var lastValidIndex = 0
for byte in bytes {
switch byte {
case 0x21...0x7E, 0x80...0xFF:
buffer[index] = byte
lastValidIndex = index
index += 1
case 0x09, 0x20:
if index > 0 {
buffer[index] = byte
index += 1
}
default:
if index > 0 {
buffer[index] = 0x20
index += 1
}
}
}
if index == 0 {
return .string("")
}
return .init(from: buffer[...lastValidIndex])
}
}
#if compiler(>=6.2)
@available(macOS 26.0, iOS 26.0, watchOS 26.0, tvOS 26.0, visionOS 26.0, *)
private static func legalize(from bytes: Span<UInt8>) -> Storage {
withUnsafeTemporaryAllocation(of: UInt8.self, capacity: bytes.count) { buffer in
var index = 0
var lastValidIndex = 0
for spanIndex in bytes.indices {
let byte = bytes[spanIndex]
switch byte {
case 0x21...0x7E, 0x80...0xFF:
buffer[index] = byte
lastValidIndex = index
index += 1
case 0x09, 0x20:
if index > 0 {
buffer[index] = byte
index += 1
}
default:
if index > 0 {
buffer[index] = 0x20
index += 1
}
}
}
if index == 0 {
return .string("")
}
return .init(from: buffer[...lastValidIndex])
}
}
#endif
private static func cleanUpAsLenient(from bytes: some Collection<UInt8>) -> Storage {
#if compiler(>=6.2)
#if canImport(Darwin)
if #available(macOS 26.0, iOS 26.0, watchOS 26.0, tvOS 26.0, visionOS 26.0, *) {
let optimized = bytes.withContiguousStorageIfAvailable { cleanUpAsLenient(from: $0.span) }
if let optimized {
return optimized
}
}
#else
let optimized = bytes.withContiguousStorageIfAvailable { cleanUpAsLenient(from: $0.span) }
if let optimized {
return optimized
}
#endif
#endif
return withUnsafeTemporaryAllocation(of: UInt8.self, capacity: bytes.count) { buffer in
var index = 0
for byte in bytes {
switch byte {
case 0x00, 0x0A, 0x0D:
buffer[index] = 0x20
default:
buffer[index] = byte
}
index += 1
}
return .init(from: buffer)
}
}
#if compiler(>=6.2)
@available(macOS 26.0, iOS 26.0, watchOS 26.0, tvOS 26.0, visionOS 26.0, *)
private static func cleanUpAsLenient(from bytes: Span<UInt8>) -> Storage {
withUnsafeTemporaryAllocation(of: UInt8.self, capacity: bytes.count) { buffer in
for index in bytes.indices {
let byte = bytes[index]
switch byte {
case 0x00, 0x0A, 0x0D:
buffer[index] = 0x20
default:
buffer[index] = byte
}
}
return .init(from: buffer)
}
}
#endif
}
extension HTTPField.Value.Storage {
init(from bytes: some Sequence<UInt8>) {
#if canImport(Darwin)
if #available(macOS 15.0, iOS 18.0, watchOS 11.0, tvOS 18.0, visionOS 2.0, *) {
if let string = String(validating: bytes, as: UTF8.self) {
self = .string(string)
return
}
}
#else
if let string = String(validating: bytes, as: UTF8.self) {
self = .string(string)
return
}
#endif
self = .bytes(Array(bytes))
}
}
extension Sequence where Element == UInt8 {
fileprivate var isASCII: Bool {
allSatisfy { $0 & 0x80 == 0 }
}
}
extension HTTPField.Value {
var isoLatin1: String {
switch self._storage {
case .string(let string):
if string.utf8.isASCII {
return string
}
return Self.transcodeToISOLatin1SlowPath(from: string.utf8)
case .bytes(let bytes):
if bytes.isASCII {
return self.string
}
return Self.transcodeToISOLatin1SlowPath(from: bytes)
}
}
init(fromISOLatin1 string: String) {
if string.utf8.isASCII {
self._storage = .string(string)
return
}
let bytes = string.unicodeScalars.lazy.map { scalar in
assert(scalar.value <= UInt8.max)
return UInt8(truncatingIfNeeded: scalar.value)
}
self._storage = .init(from: bytes)
}
private static func transcodeToISOLatin1SlowPath(from bytes: some Collection<UInt8>) -> String {
let scalars = bytes.lazy.map { UnicodeScalar(UInt32($0))! }
var string = ""
string.unicodeScalars.append(contentsOf: scalars)
return string
}
}
+15 -15
View File
@@ -15,16 +15,16 @@
#if !hasFeature(Embedded) || compiler(>=6.4)
struct HTTPParsedFields {
private var method: ISOLatin1String?
private var scheme: ISOLatin1String?
private var authority: ISOLatin1String?
private var path: ISOLatin1String?
private var extendedConnectProtocol: ISOLatin1String?
private var status: ISOLatin1String?
private var method: HTTPField.Value?
private var scheme: HTTPField.Value?
private var authority: HTTPField.Value?
private var path: HTTPField.Value?
private var extendedConnectProtocol: HTTPField.Value?
private var status: HTTPField.Value?
private var contentLength: ISOLatin1String?
private var contentDisposition: ISOLatin1String?
private var location: ISOLatin1String?
private var contentLength: HTTPField.Value?
private var contentDisposition: HTTPField.Value?
private var location: HTTPField.Value?
private var fields: HTTPFields
@@ -130,7 +130,7 @@ struct HTTPParsedFields {
guard let method = self.method else {
throw ParsingError.requestWithoutMethod
}
guard let requestMethod = HTTPRequest.Method(method._storage) else {
guard let requestMethod = HTTPRequest.Method(method.string) else {
throw ParsingError.invalidMethod
}
if self.status != nil {
@@ -155,7 +155,7 @@ struct HTTPParsedFields {
var response: HTTPResponse {
get throws {
guard let statusString = self.status?._storage else {
guard let statusString = self.status?.string else {
throw ParsingError.responseWithoutStatus
}
if self.method != nil || self.scheme != nil || self.authority != nil || self.path != nil
@@ -185,12 +185,12 @@ struct HTTPParsedFields {
extension HTTPRequest {
fileprivate init(
method: Method,
scheme: ISOLatin1String?,
authority: ISOLatin1String?,
path: ISOLatin1String?,
scheme: HTTPField.Value?,
authority: HTTPField.Value?,
path: HTTPField.Value?,
headerFields: HTTPFields
) {
let methodField = HTTPField(name: .method, uncheckedValue: ISOLatin1String(unchecked: method.rawValue))
let methodField = HTTPField(name: .method, uncheckedValue: HTTPField.Value(unchecked: method.rawValue))
let schemeField = scheme.map { HTTPField(name: .scheme, uncheckedValue: $0) }
let authorityField = authority.map { HTTPField(name: .authority, uncheckedValue: $0) }
let pathField = path.map { HTTPField(name: .path, uncheckedValue: $0) }
+7 -7
View File
@@ -58,10 +58,10 @@ public struct HTTPRequest: Sendable, Hashable {
/// A convenient way to access the value of the ":method" pseudo header field.
public var method: Method {
get {
Method(unchecked: self.pseudoHeaderFields.method.rawValue._storage)
Method(unchecked: self.pseudoHeaderFields.method.value)
}
set {
self.pseudoHeaderFields.method.rawValue = ISOLatin1String(unchecked: newValue.rawValue)
self.pseudoHeaderFields.method.rawValue = HTTPField.Value(unchecked: newValue.rawValue)
}
}
@@ -205,7 +205,7 @@ public struct HTTPRequest: Sendable, Hashable {
}
set {
precondition(newValue.name == .method, "Cannot change pseudo-header field name")
precondition(HTTPField.isValidToken(newValue.rawValue._storage), "Invalid character in method field")
precondition(newValue.rawValue.isValidToken, "Invalid character in method field")
if !isKnownUniquelyReferenced(&self._storage) {
self._storage = self._storage.copy()
@@ -313,7 +313,7 @@ public struct HTTPRequest: Sendable, Hashable {
/// - path: The value of the ":path" pseudo header field.
/// - headerFields: The request header fields.
public init(method: Method, scheme: String?, authority: String?, path: String?, headerFields: HTTPFields = [:]) {
let methodField = HTTPField(name: .method, uncheckedValue: ISOLatin1String(unchecked: method.rawValue))
let methodField = HTTPField(name: .method, uncheckedValue: HTTPField.Value(unchecked: method.rawValue))
let schemeField = scheme.map { HTTPField(name: .scheme, value: $0) }
let authorityField = authority.map { HTTPField(name: .authority, value: $0) }
let pathField = path.map { HTTPField(name: .path, value: $0) }
@@ -330,7 +330,7 @@ public struct HTTPRequest: Sendable, Hashable {
@available(HTTPTypes 1.0, *)
extension HTTPRequest: CustomDebugStringConvertible {
public var debugDescription: String {
"(\(self.pseudoHeaderFields.method.rawValue._storage)) \((self.pseudoHeaderFields.scheme?.value).map { "\($0)://" } ?? "")\(self.pseudoHeaderFields.authority?.value ?? "")\(self.pseudoHeaderFields.path?.value ?? "")"
"(\(self.pseudoHeaderFields.method.value)) \((self.pseudoHeaderFields.scheme?.value).map { "\($0)://" } ?? "")\(self.pseudoHeaderFields.authority?.value ?? "")\(self.pseudoHeaderFields.path?.value ?? "")"
}
}
@@ -420,10 +420,10 @@ extension HTTPRequest.PseudoHeaderFields: Codable {
debugDescription: "\":method\" pseudo header field is missing"
)
}
guard HTTPField.isValidToken(method.rawValue._storage) else {
guard method.rawValue.isValidToken else {
throw DecodingError.dataCorruptedError(
in: container,
debugDescription: "\"\(method.rawValue._storage)\" is not a valid method"
debugDescription: "\"\(method.value)\" is not a valid method"
)
}
self.init(
+6 -6
View File
@@ -159,14 +159,14 @@ public struct HTTPResponse: Sendable, Hashable {
/// phrase.
public var status: Status {
get {
var codeIterator = self.pseudoHeaderFields.status.rawValue._storage.utf8.makeIterator()
var codeIterator = self.pseudoHeaderFields.status.value.utf8.makeIterator()
let code =
Int(codeIterator.next()! - 48) * 100 + Int(codeIterator.next()! - 48) * 10
+ Int(codeIterator.next()! - 48)
return Status(uncheckedCode: code, reasonPhrase: self.pseudoHeaderFields.reasonPhrase)
}
set {
self.pseudoHeaderFields.status.rawValue = ISOLatin1String(unchecked: newValue.fieldValue)
self.pseudoHeaderFields.status.rawValue = HTTPField.Value(unchecked: newValue.fieldValue)
self.pseudoHeaderFields.reasonPhrase = newValue.reasonPhrase
}
}
@@ -209,7 +209,7 @@ public struct HTTPResponse: Sendable, Hashable {
}
set {
precondition(newValue.name == .status, "Cannot change pseudo-header field name")
precondition(Status.isValidStatus(newValue.rawValue._storage), "Invalid status code")
precondition(Status.isValidStatus(newValue.value), "Invalid status code")
if !isKnownUniquelyReferenced(&self._storage) {
self._storage = self._storage.copy()
@@ -236,7 +236,7 @@ public struct HTTPResponse: Sendable, Hashable {
init(status: Status) {
self._storage = .init(
status: HTTPField(name: .status, uncheckedValue: ISOLatin1String(unchecked: status.fieldValue)),
status: HTTPField(name: .status, uncheckedValue: HTTPField.Value(unchecked: status.fieldValue)),
reasonPhrase: status.reasonPhrase
)
}
@@ -303,10 +303,10 @@ extension HTTPResponse.PseudoHeaderFields: Codable {
debugDescription: "\":status\" pseudo header field is missing"
)
}
guard HTTPResponse.Status.isValidStatus(status.rawValue._storage) else {
guard HTTPResponse.Status.isValidStatus(status.value) else {
throw DecodingError.dataCorruptedError(
in: container,
debugDescription: "\"\(status.rawValue._storage)\" is not a valid status code"
debugDescription: "\"\(status.value)\" is not a valid status code"
)
}
self.init(status: status)
-90
View File
@@ -1,90 +0,0 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift open source project
//
// Copyright (c) 2023 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See CONTRIBUTORS.txt for the list of Swift project authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//
extension String {
var isASCII: Bool {
self.utf8.allSatisfy { $0 & 0x80 == 0 }
}
}
struct ISOLatin1String: Sendable, Hashable {
let _storage: String
private static func transcodeSlowPath(from bytes: some Collection<UInt8>) -> String {
let scalars = bytes.lazy.map { UnicodeScalar(UInt32($0))! }
var string = ""
string.unicodeScalars.append(contentsOf: scalars)
return string
}
private func withISOLatin1BytesSlowPath<Return, Failure: Error>(
_ body: (UnsafeBufferPointer<UInt8>) throws(Failure) -> Return
) throws(Failure) -> Return {
try withUnsafeTemporaryAllocation(of: UInt8.self, capacity: self._storage.unicodeScalars.count) { buffer in
for (index, scalar) in self._storage.unicodeScalars.enumerated() {
assert(scalar.value <= UInt8.max)
buffer[index] = UInt8(truncatingIfNeeded: scalar.value)
}
return Result { () throws(Failure) in
try body(UnsafeBufferPointer(buffer))
}
}.get()
}
init(_ string: String) {
if string.isASCII {
self._storage = string
} else {
self._storage = Self.transcodeSlowPath(from: string.utf8)
}
}
init(_ bytes: some Collection<UInt8>) {
let ascii = bytes.allSatisfy { $0 & 0x80 == 0 }
if ascii {
self._storage = String(decoding: bytes, as: UTF8.self)
} else {
self._storage = Self.transcodeSlowPath(from: bytes)
}
}
init(unchecked: String) {
self._storage = unchecked
}
var string: String {
if self._storage.isASCII {
return self._storage
} else {
return self.withISOLatin1BytesSlowPath {
String(decoding: $0, as: UTF8.self)
}
}
}
func withUnsafeBytes<Return, Failure: Error>(
_ body: (UnsafeBufferPointer<UInt8>) throws(Failure) -> Return
) throws(Failure) -> Return {
if self._storage.isASCII {
var string = self._storage
return try string.withUTF8 { buffer in
Result { () throws(Failure) in
try body(buffer)
}
}.get()
} else {
return try self.withISOLatin1BytesSlowPath(body)
}
}
}
@@ -150,4 +150,40 @@ extension HTTPField.Name {
#expect(buffer[3] == 0x20)
}
}
@Test func equalityOfString() {
let lhs = HTTPField(name: .foo, value: "bar")
let rhs = HTTPField(name: .foo, value: " bar ")
#expect(lhs == rhs)
}
@Test func inequalityOfStrings() {
let lhs = HTTPField(name: .foo, value: "bar")
let rhs = HTTPField(name: .foo, value: "baz")
#expect(lhs != rhs)
}
@Test func equalityOfBytes() {
let lhs = HTTPField(name: .foo, value: [0x00, 0x62, 0x61, 0x00, 0x72, 0xC0, 0x00])
let rhs = HTTPField(name: .foo, value: [0x00, 0x00, 0x62, 0x61, 0x00, 0x72, 0xC0, 0x00])
#expect(lhs == rhs)
}
@Test func inequalityOfBytes() {
let lhs = HTTPField(name: .foo, value: [0x00, 0x62, 0x61, 0x00, 0x72, 0xC0, 0x00])
let rhs = HTTPField(name: .foo, value: [0x00, 0x62, 0x61, 0x00, 0x7A, 0xC0, 0x00])
#expect(lhs != rhs)
}
@Test func inequalityOfStringAndBytes() {
let lhs = HTTPField(name: .foo, value: "bar")
// picked 0xC0 since it's guaranteed to not be utf8
let rhs = HTTPField(name: .foo, value: [0xC0, 0x62, 0x61, 0x00, 0x72, 0xC0])
#expect(lhs != rhs)
}
}