mirror of
https://github.com/apple/swift-http-types
synced 2026-11-28 05:14:20 +00:00
Support lenient legalization of header field values
This commit is contained in:
1 parent
fe805ee87f
commit
20d5ed91ae
2 files changed
+26
No files matched your search
@@ -68,6 +68,15 @@ public struct HTTPField: Sendable, Hashable {
|
||||
self.rawValue = Self.legalizeValue(ISOLatin1String(value))
|
||||
}
|
||||
|
||||
/// Create an HTTP field from a name and a value. Leniently legalize the value.
|
||||
/// - Parameters:
|
||||
/// - name: The HTTP field name.
|
||||
/// - value: The HTTP field value. Newlines and NULs are converted into space characters.
|
||||
public init(name: Name, lenientValue: some Collection<UInt8>) {
|
||||
self.name = name
|
||||
self.rawValue = Self.lenientLegalizeValue(ISOLatin1String(lenientValue))
|
||||
}
|
||||
|
||||
init(name: Name, uncheckedValue: ISOLatin1String) {
|
||||
self.name = name
|
||||
self.rawValue = uncheckedValue
|
||||
@@ -162,6 +171,22 @@ public struct HTTPField: Sendable, Hashable {
|
||||
}
|
||||
}
|
||||
|
||||
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))
|
||||
}
|
||||
}
|
||||
|
||||
/// Whether the string is valid for an HTTP field value based on RFC 9110.
|
||||
///
|
||||
/// https://www.rfc-editor.org/rfc/rfc9110.html#name-field-values
|
||||
|
||||
Reference in new issue
Block a user