Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions Sources/AsyncHTTPClient/RequestValidation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@ extension HTTPHeaders {

private func validateFieldNames() throws {
let invalidFieldNames = self.compactMap { name, _ -> String? in
// [RFC 9110](https://www.rfc-editor.org/rfc/rfc9110.html#name-field-names) defines
// `field-name = token`, and `token = 1*tchar`, so a field name must contain at least
// one character. `allSatisfy` alone is true for the empty name.
guard !name.isEmpty else { return name }

let satisfy = name.utf8.allSatisfy { char -> Bool in
switch char {
case UInt8(ascii: "a")...UInt8(ascii: "z"),
Expand Down
13 changes: 13 additions & 0 deletions Tests/AsyncHTTPClientTests/RequestValidationTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,19 @@ class RequestValidationTests: XCTestCase {
}
}

func testEmptyHeaderFieldNameIsRejected() {
// RFC 9110 defines `field-name = token` and `token = 1*tchar`, so the empty
// name is not a valid field name.
var headers = HTTPHeaders([
("", "Haha")
])

XCTAssertThrowsError(try headers.validateAndSetTransportFraming(method: .GET, bodyLength: .known(0))) {
error in
XCTAssertEqual(error as? HTTPClientError, HTTPClientError.invalidHeaderFieldNames([""]))
}
}

func testValidHeaderFieldNames() {
var headers = HTTPHeaders([
("abcdefghijklmnopqrstuvwxyz", "Haha"),
Expand Down