diff --git a/.gitignore b/.gitignore index a6b3e03..9534807 100644 --- a/.gitignore +++ b/.gitignore @@ -1,7 +1,7 @@ .DS_Store .swift-version -/.build +.build /Packages .xcode /*.xcodeproj diff --git a/Sources/HTTPTypes/HTTPFieldName.swift b/Sources/HTTPTypes/HTTPFieldName.swift index cb73532..9f0f1d8 100644 --- a/Sources/HTTPTypes/HTTPFieldName.swift +++ b/Sources/HTTPTypes/HTTPFieldName.swift @@ -54,6 +54,15 @@ extension HTTPField { /// Create an HTTP field name from a string produced by HPACK or QPACK decoders used in /// modern HTTP versions. /// + /// Returns nil if the name contains invalid characters defined in RFC 9110, or if it is not + /// already lowercased. Names arriving over HTTP/2 or HTTP/3 must be lowercased on the wire, + /// and a message carrying an uppercase field name has to be treated as malformed, so this + /// initializer rejects rather than canonicalises such a name. Use `init?(_:)` for names from + /// sources which are allowed to use mixed case. + /// + /// https://www.rfc-editor.org/rfc/rfc9113.html#name-field-validity + /// https://www.rfc-editor.org/rfc/rfc9114.html#name-field-formatting-and-compre + /// /// - Warning: Do not use directly with the `HTTPFields` struct which does not allow pseudo /// header fields. /// @@ -68,11 +77,16 @@ extension HTTPField { } else { token = Substring(name) } - guard HTTPField.isValidToken(token) else { + + switch HTTPField.tokenValidity(token) { + case .canonical: + self.rawName = name + self.canonicalName = name + + case .invalid, .valid: + // valid also allows uppercased characters return nil } - self.rawName = name - self.canonicalName = name } @inlinable diff --git a/Tests/HTTPTypesTests/HTTPFieldNameTests.swift b/Tests/HTTPTypesTests/HTTPFieldNameTests.swift new file mode 100644 index 0000000..67f88f0 --- /dev/null +++ b/Tests/HTTPTypesTests/HTTPFieldNameTests.swift @@ -0,0 +1,65 @@ +//===----------------------------------------------------------------------===// +// +// This source file is part of the Swift open source project +// +// Copyright (c) 2026 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 +// +//===----------------------------------------------------------------------===// + +import HTTPTypes +import Testing + +@Suite struct HTTPFieldNameTests { + @Test func initRejectsInvalidCharacters() { + #expect(HTTPField.Name("") == nil) + #expect(HTTPField.Name("content type") == nil) + #expect(HTTPField.Name("content:type") == nil) + #expect(HTTPField.Name("cöntent-type") == nil) + // `init(_:)` does not know about pseudo header fields, so the colon is just an illegal character. + #expect(HTTPField.Name(":method") == nil) + } + + @Test func initCanonicalisesMixedCase() { + let name = HTTPField.Name("Content-Type") + #expect(name?.rawName == "Content-Type") + #expect(name?.canonicalName == "content-type") + #expect(name == HTTPField.Name("content-type")) + } + + @Test func parsedAcceptsLowercasedNames() { + let name = HTTPField.Name(parsed: "content-type") + #expect(name?.rawName == "content-type") + #expect(name?.canonicalName == "content-type") + #expect(name == HTTPField.Name("content-type")) + + // The symbols RFC 9110 allows in a token, plus digits. + #expect(HTTPField.Name(parsed: "!#$%&'*+-.^_`|~09az") != nil) + } + + @Test func parsedAcceptsPseudoHeaderFields() { + let name = HTTPField.Name(parsed: ":method") + #expect(name?.rawName == ":method") + #expect(name?.canonicalName == ":method") + } + + @Test func parsedRejectsInvalidCharacters() { + #expect(HTTPField.Name(parsed: "") == nil) + #expect(HTTPField.Name(parsed: ":") == nil) + #expect(HTTPField.Name(parsed: "content type") == nil) + #expect(HTTPField.Name(parsed: "content:type") == nil) + #expect(HTTPField.Name(parsed: "cöntent-type") == nil) + } + + @Test func parsedRejectsUppercasedNames() { + #expect(HTTPField.Name(parsed: "A") == nil) + #expect(HTTPField.Name(parsed: "Content-Type") == nil) + #expect(HTTPField.Name(parsed: "content-typE") == nil) + #expect(HTTPField.Name(parsed: ":Method") == nil) + } +}