mirror of
https://github.com/apple/swift-http-types
synced 2026-11-24 04:37:51 +00:00
Remove custom DecodingError and harden fields
This commit is contained in:
1 parent
792abeab1f
commit
e885fa315c
5 files changed
+33
-49
No files matched your search
@@ -211,16 +211,12 @@ extension HTTPField: Codable {
|
||||
}
|
||||
}
|
||||
|
||||
private enum DecodingError: Error {
|
||||
case invalidValue(String)
|
||||
}
|
||||
|
||||
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 Self.isValidValue(value) else {
|
||||
throw DecodingError.invalidValue(value)
|
||||
throw DecodingError.dataCorruptedError(forKey: .value, in: container, debugDescription: "HTTP field value \"\(value)\" contains invalid characters")
|
||||
}
|
||||
self.init(name: name, uncheckedValue: ISOLatin1String(unchecked: value))
|
||||
if let indexingStrategyValue = try container.decodeIfPresent(UInt8.self, forKey: .indexingStrategy),
|
||||
|
||||
@@ -52,6 +52,10 @@ extension HTTPField {
|
||||
self.rawName = rawName
|
||||
self.canonicalName = canonicalName
|
||||
}
|
||||
|
||||
var isPseudo: Bool {
|
||||
self.rawName.hasPrefix(":")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -83,22 +87,18 @@ extension HTTPField.Name: Codable {
|
||||
try container.encode(self.rawName)
|
||||
}
|
||||
|
||||
private enum DecodingError: Error {
|
||||
case invalidString(String)
|
||||
}
|
||||
|
||||
public init(from decoder: Decoder) throws {
|
||||
let container = try decoder.singleValueContainer()
|
||||
let nameString = try container.decode(String.self)
|
||||
if nameString.hasPrefix(":") {
|
||||
guard nameString.lowercased() == nameString,
|
||||
HTTPField.isValidToken(nameString.dropFirst()) else {
|
||||
throw DecodingError.invalidString(nameString)
|
||||
throw DecodingError.dataCorruptedError(in: container, debugDescription: "HTTP pseudo field name \"\(nameString)\" contains invalid characters")
|
||||
}
|
||||
self.init(rawName: nameString, canonicalName: nameString)
|
||||
} else {
|
||||
guard let name = Self(nameString) else {
|
||||
throw DecodingError.invalidString(nameString)
|
||||
throw DecodingError.dataCorruptedError(in: container, debugDescription: "HTTP field name \"\(nameString)\" contains invalid characters")
|
||||
}
|
||||
self = name
|
||||
}
|
||||
|
||||
@@ -112,6 +112,7 @@ public struct HTTPFields: Sendable, Hashable {
|
||||
}
|
||||
|
||||
func append(field: HTTPField) {
|
||||
precondition(!field.name.isPseudo, "Pseudo header field \"\(field.name)\" disallowed")
|
||||
let name = field.name.canonicalName
|
||||
if var index = self.index?[name] {
|
||||
while true {
|
||||
@@ -124,6 +125,7 @@ public struct HTTPFields: Sendable, Hashable {
|
||||
self.index?[name] = UInt16(self.fields.count)
|
||||
}
|
||||
self.fields.append((field, .max))
|
||||
precondition(self.fields.count < UInt16.max, "Too many fields")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -232,8 +234,10 @@ public struct HTTPFields: Sendable, Hashable {
|
||||
extension HTTPFields: ExpressibleByDictionaryLiteral {
|
||||
public init(dictionaryLiteral elements: (HTTPField.Name, String)...) {
|
||||
for (name, value) in elements {
|
||||
precondition(!name.isPseudo, "Pseudo header field \"\(name)\" disallowed")
|
||||
self._storage.append(field: HTTPField(name: name, value: value))
|
||||
}
|
||||
precondition(self.count < UInt16.max, "Too many fields")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -271,6 +275,7 @@ extension HTTPFields: RangeReplaceableCollection, RandomAccessCollection, Mutabl
|
||||
self._storage = self._storage.copy()
|
||||
}
|
||||
if newValue.name != self._storage.fields[position].0.name {
|
||||
precondition(!newValue.name.isPseudo, "Pseudo header field \"\(newValue.name)\" disallowed")
|
||||
self._storage.index = nil
|
||||
}
|
||||
self._storage.fields[position].0 = newValue
|
||||
@@ -281,13 +286,18 @@ extension HTTPFields: RangeReplaceableCollection, RandomAccessCollection, Mutabl
|
||||
if !isKnownUniquelyReferenced(&self._storage) {
|
||||
self._storage = self._storage.copy()
|
||||
}
|
||||
if subrange.startIndex == count {
|
||||
if subrange.startIndex == self.count {
|
||||
for field in newElements {
|
||||
precondition(!field.name.isPseudo, "Pseudo header field \"\(field.name)\" disallowed")
|
||||
self._storage.append(field: field)
|
||||
}
|
||||
} else {
|
||||
self._storage.index = nil
|
||||
self._storage.fields.replaceSubrange(subrange, with: newElements.lazy.map { ($0, 0) })
|
||||
self._storage.fields.replaceSubrange(subrange, with: newElements.lazy.map { field in
|
||||
precondition(!field.name.isPseudo, "Pseudo header field \"\(field.name)\" disallowed")
|
||||
return (field, 0)
|
||||
})
|
||||
precondition(self.count < UInt16.max, "Too many fields")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -312,10 +322,6 @@ extension HTTPFields: Codable {
|
||||
try container.encode(contentsOf: self)
|
||||
}
|
||||
|
||||
private enum DecodingError: Error {
|
||||
case isPseudo(String)
|
||||
}
|
||||
|
||||
public init(from decoder: Decoder) throws {
|
||||
var container = try decoder.unkeyedContainer()
|
||||
if let count = container.count {
|
||||
@@ -323,8 +329,8 @@ extension HTTPFields: Codable {
|
||||
}
|
||||
while !container.isAtEnd {
|
||||
let field = try container.decode(HTTPField.self)
|
||||
guard !field.name.rawName.hasPrefix(":") else {
|
||||
throw DecodingError.isPseudo(field.name.rawName)
|
||||
guard !field.name.isPseudo else {
|
||||
throw DecodingError.dataCorruptedError(in: container, debugDescription: "Pseudo header field \"\(field)\" disallowed")
|
||||
}
|
||||
self.append(field)
|
||||
}
|
||||
|
||||
@@ -242,17 +242,6 @@ extension HTTPRequest.PseudoHeaderFields: Codable {
|
||||
}
|
||||
}
|
||||
|
||||
private enum DecodingError: Error {
|
||||
case missingMethod
|
||||
case multipleMethod
|
||||
case multipleScheme
|
||||
case multipleAuthority
|
||||
case multiplePath
|
||||
case multipleExtendedConnectProtocol
|
||||
case invalidMethod(String)
|
||||
case notPseudo(String)
|
||||
}
|
||||
|
||||
public init(from decoder: Decoder) throws {
|
||||
var container = try decoder.unkeyedContainer()
|
||||
var method: HTTPField?
|
||||
@@ -265,40 +254,40 @@ extension HTTPRequest.PseudoHeaderFields: Codable {
|
||||
switch field.name {
|
||||
case .method:
|
||||
guard method == nil else {
|
||||
throw DecodingError.multipleMethod
|
||||
throw DecodingError.dataCorruptedError(in: container, debugDescription: "Multiple \":method\" pseudo header fields")
|
||||
}
|
||||
method = field
|
||||
case .scheme:
|
||||
guard scheme == nil else {
|
||||
throw DecodingError.multipleScheme
|
||||
throw DecodingError.dataCorruptedError(in: container, debugDescription: "Multiple \":scheme\" pseudo header fields")
|
||||
}
|
||||
scheme = field
|
||||
case .authority:
|
||||
guard authority == nil else {
|
||||
throw DecodingError.multipleAuthority
|
||||
throw DecodingError.dataCorruptedError(in: container, debugDescription: "Multiple \":authority\" pseudo header fields")
|
||||
}
|
||||
authority = field
|
||||
case .path:
|
||||
guard path == nil else {
|
||||
throw DecodingError.multiplePath
|
||||
throw DecodingError.dataCorruptedError(in: container, debugDescription: "Multiple \":path\" pseudo header fields")
|
||||
}
|
||||
path = field
|
||||
case .protocol:
|
||||
guard extendedConnectProtocol == nil else {
|
||||
throw DecodingError.multipleExtendedConnectProtocol
|
||||
throw DecodingError.dataCorruptedError(in: container, debugDescription: "Multiple \":protocol\" pseudo header fields")
|
||||
}
|
||||
extendedConnectProtocol = field
|
||||
default:
|
||||
guard field.name.rawName.hasPrefix(":") else {
|
||||
throw DecodingError.notPseudo(field.name.rawName)
|
||||
throw DecodingError.dataCorruptedError(in: container, debugDescription: "\"\(field)\" is not a pseudo header field")
|
||||
}
|
||||
}
|
||||
}
|
||||
guard let method else {
|
||||
throw DecodingError.missingMethod
|
||||
throw DecodingError.dataCorruptedError(in: container, debugDescription: "\":method\" pseudo header field is missing")
|
||||
}
|
||||
guard HTTPField.isValidToken(method.rawValue._storage) else {
|
||||
throw DecodingError.invalidMethod(method.rawValue._storage)
|
||||
throw DecodingError.dataCorruptedError(in: container, debugDescription: "\"\(method.rawValue._storage)\" is not a valid method")
|
||||
}
|
||||
self.init(method: method, scheme: scheme, authority: authority, path: path,
|
||||
extendedConnectProtocol: extendedConnectProtocol)
|
||||
|
||||
@@ -211,13 +211,6 @@ extension HTTPResponse.PseudoHeaderFields: Codable {
|
||||
try container.encode(self.status)
|
||||
}
|
||||
|
||||
private enum DecodingError: Error {
|
||||
case missingStatus
|
||||
case multipleStatus
|
||||
case invalidStatus(String)
|
||||
case notPseudo(String)
|
||||
}
|
||||
|
||||
public init(from decoder: Decoder) throws {
|
||||
var container = try decoder.unkeyedContainer()
|
||||
var status: HTTPField?
|
||||
@@ -226,20 +219,20 @@ extension HTTPResponse.PseudoHeaderFields: Codable {
|
||||
switch field.name {
|
||||
case .status:
|
||||
guard status == nil else {
|
||||
throw DecodingError.missingStatus
|
||||
throw DecodingError.dataCorruptedError(in: container, debugDescription: "Multiple \":status\" pseudo header fields")
|
||||
}
|
||||
status = field
|
||||
default:
|
||||
guard field.name.rawName.hasPrefix(":") else {
|
||||
throw DecodingError.notPseudo(field.name.rawName)
|
||||
throw DecodingError.dataCorruptedError(in: container, debugDescription: "\"\(field)\" is not a pseudo header field")
|
||||
}
|
||||
}
|
||||
}
|
||||
guard let status else {
|
||||
throw DecodingError.missingStatus
|
||||
throw DecodingError.dataCorruptedError(in: container, debugDescription: "\":status\" pseudo header field is missing")
|
||||
}
|
||||
guard HTTPResponse.Status.isValidStatus(status.rawValue._storage) else {
|
||||
throw DecodingError.invalidStatus(status.rawValue._storage)
|
||||
throw DecodingError.dataCorruptedError(in: container, debugDescription: "\"\(status.rawValue._storage)\" is not a valid status code")
|
||||
}
|
||||
self.init(status: status)
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user