[CI] Make all CI checks green

# Motivation

We just adopted GH actions and want to make sure all the CI checks are green.

# Modification

This PR makes the necessary changes to pass all checks. Including:

- Adding a licenseignore file
- Moving `swift-format` and adopting the config that we use in all other swift on server repos.

# Result

Green CI.
This commit is contained in:
Franz Busch committed 2024-10-24 22:30:18 +02:00
1 parent 70cda1b770
commit f3d9ec3eef
29 files changed
+541 -207

No files matched your search

+25 -15
View File
@@ -112,7 +112,9 @@ public struct HTTPField: Sendable, Hashable {
///
/// - Parameter body: The closure to be invoked with the buffer.
/// - Returns: Result of the `body` closure.
public func withUnsafeBytesOfValue<Result>(_ body: (UnsafeBufferPointer<UInt8>) throws -> Result) rethrows -> Result {
public func withUnsafeBytesOfValue<Result>(
_ body: (UnsafeBufferPointer<UInt8>) throws -> Result
) rethrows -> Result {
try self.rawValue.withUnsafeBytes(body)
}
@@ -135,7 +137,7 @@ public struct HTTPField: Sendable, Hashable {
switch byte {
case 0x09, 0x20:
break
case 0x21 ... 0x7E, 0x80 ... 0xFF:
case 0x21...0x7E, 0x80...0xFF:
break
default:
return false
@@ -161,13 +163,15 @@ public struct HTTPField: Sendable, Hashable {
switch byte {
case 0x09, 0x20:
return byte
case 0x21 ... 0x7E, 0x80 ... 0xFF:
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 }
let trimmed = bytes.reversed().drop { $0 == 0x09 || $0 == 0x20 }.reversed().drop {
$0 == 0x09 || $0 == 0x20
}
return ISOLatin1String(unchecked: String(decoding: trimmed, as: UTF8.self))
}
}
@@ -242,11 +246,16 @@ extension HTTPField: Codable {
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.dataCorruptedError(forKey: .value, in: container, debugDescription: "HTTP field value \"\(value)\" contains invalid characters")
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),
let indexingStrategy = DynamicTableIndexingStrategy(rawValue: indexingStrategyValue) {
let indexingStrategy = DynamicTableIndexingStrategy(rawValue: indexingStrategyValue)
{
self.indexingStrategy = indexingStrategy
}
}
@@ -254,15 +263,16 @@ extension HTTPField: Codable {
extension HTTPField {
static func isValidToken(_ token: some StringProtocol) -> Bool {
!token.isEmpty && token.utf8.allSatisfy {
switch $0 {
case 0x21, 0x23, 0x24, 0x25, 0x26, 0x27, 0x2A, 0x2B, 0x2D, 0x2E, 0x5E, 0x5F, 0x60, 0x7C, 0x7E:
return true
case 0x30 ... 0x39, 0x41 ... 0x5A, 0x61 ... 0x7A: // DIGHT, ALPHA
return true
default:
return false
!token.isEmpty
&& token.utf8.allSatisfy {
switch $0 {
case 0x21, 0x23, 0x24, 0x25, 0x26, 0x27, 0x2A, 0x2B, 0x2D, 0x2E, 0x5E, 0x5F, 0x60, 0x7C, 0x7E:
return true
case 0x30...0x39, 0x41...0x5A, 0x61...0x7A: // DIGHT, ALPHA
return true
default:
return false
}
}
}
}
}
+103 -40
View File
@@ -66,16 +66,18 @@ extension HTTPField {
} else {
token = Substring(name)
}
guard token.utf8.allSatisfy({
switch $0 {
case 0x21, 0x23, 0x24, 0x25, 0x26, 0x27, 0x2A, 0x2B, 0x2D, 0x2E, 0x5E, 0x5F, 0x60, 0x7C, 0x7E:
return true
case 0x30 ... 0x39, 0x61 ... 0x7A: // DIGHT, ALPHA
return true
default:
return false
}
}) else {
guard
token.utf8.allSatisfy({
switch $0 {
case 0x21, 0x23, 0x24, 0x25, 0x26, 0x27, 0x2A, 0x2B, 0x2D, 0x2E, 0x5E, 0x5F, 0x60, 0x7C, 0x7E:
return true
case 0x30...0x39, 0x61...0x7A: // DIGHT, ALPHA
return true
default:
return false
}
})
else {
return nil
}
self.rawName = name
@@ -126,13 +128,20 @@ extension HTTPField.Name: Codable {
let nameString = try container.decode(String.self)
if nameString.hasPrefix(":") {
guard nameString.lowercased() == nameString,
HTTPField.isValidToken(nameString.dropFirst()) else {
throw DecodingError.dataCorruptedError(in: container, debugDescription: "HTTP pseudo field name \"\(nameString)\" contains invalid characters")
HTTPField.isValidToken(nameString.dropFirst())
else {
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.dataCorruptedError(in: container, debugDescription: "HTTP field name \"\(nameString)\" contains invalid characters")
throw DecodingError.dataCorruptedError(
in: container,
debugDescription: "HTTP field name \"\(nameString)\" contains invalid characters"
)
}
self = name
}
@@ -170,42 +179,58 @@ extension HTTPField.Name {
/// Access-Control-Allow-Credentials
///
/// https://fetch.spec.whatwg.org/
public static var accessControlAllowCredentials: Self { .init(rawName: "Access-Control-Allow-Credentials", canonicalName: "access-control-allow-credentials") }
public static var accessControlAllowCredentials: Self {
.init(rawName: "Access-Control-Allow-Credentials", canonicalName: "access-control-allow-credentials")
}
/// Access-Control-Allow-Headers
///
/// https://fetch.spec.whatwg.org/
public static var accessControlAllowHeaders: Self { .init(rawName: "Access-Control-Allow-Headers", canonicalName: "access-control-allow-headers") }
public static var accessControlAllowHeaders: Self {
.init(rawName: "Access-Control-Allow-Headers", canonicalName: "access-control-allow-headers")
}
/// Access-Control-Allow-Methods
///
/// https://fetch.spec.whatwg.org/
public static var accessControlAllowMethods: Self { .init(rawName: "Access-Control-Allow-Methods", canonicalName: "access-control-allow-methods") }
public static var accessControlAllowMethods: Self {
.init(rawName: "Access-Control-Allow-Methods", canonicalName: "access-control-allow-methods")
}
/// Access-Control-Allow-Origin
///
/// https://fetch.spec.whatwg.org/
public static var accessControlAllowOrigin: Self { .init(rawName: "Access-Control-Allow-Origin", canonicalName: "access-control-allow-origin") }
public static var accessControlAllowOrigin: Self {
.init(rawName: "Access-Control-Allow-Origin", canonicalName: "access-control-allow-origin")
}
/// Access-Control-Expose-Headers
///
/// https://fetch.spec.whatwg.org/
public static var accessControlExposeHeaders: Self { .init(rawName: "Access-Control-Expose-Headers", canonicalName: "access-control-expose-headers") }
public static var accessControlExposeHeaders: Self {
.init(rawName: "Access-Control-Expose-Headers", canonicalName: "access-control-expose-headers")
}
/// Access-Control-Max-Age
///
/// https://fetch.spec.whatwg.org/
public static var accessControlMaxAge: Self { .init(rawName: "Access-Control-Max-Age", canonicalName: "access-control-max-age") }
public static var accessControlMaxAge: Self {
.init(rawName: "Access-Control-Max-Age", canonicalName: "access-control-max-age")
}
/// Access-Control-Request-Headers
///
/// https://fetch.spec.whatwg.org/
public static var accessControlRequestHeaders: Self { .init(rawName: "Access-Control-Request-Headers", canonicalName: "access-control-request-headers") }
public static var accessControlRequestHeaders: Self {
.init(rawName: "Access-Control-Request-Headers", canonicalName: "access-control-request-headers")
}
/// Access-Control-Request-Method
///
/// https://fetch.spec.whatwg.org/
public static var accessControlRequestMethod: Self { .init(rawName: "Access-Control-Request-Method", canonicalName: "access-control-request-method") }
public static var accessControlRequestMethod: Self {
.init(rawName: "Access-Control-Request-Method", canonicalName: "access-control-request-method")
}
/// Age
///
@@ -220,7 +245,9 @@ extension HTTPField.Name {
/// Authentication-Info
///
/// https://www.rfc-editor.org/rfc/rfc9110.html
public static var authenticationInfo: Self { .init(rawName: "Authentication-Info", canonicalName: "authentication-info") }
public static var authenticationInfo: Self {
.init(rawName: "Authentication-Info", canonicalName: "authentication-info")
}
/// Authorization
///
@@ -240,7 +267,9 @@ extension HTTPField.Name {
/// Content-Disposition
///
/// https://www.rfc-editor.org/rfc/rfc6266.html
public static var contentDisposition: Self { .init(rawName: "Content-Disposition", canonicalName: "content-disposition") }
public static var contentDisposition: Self {
.init(rawName: "Content-Disposition", canonicalName: "content-disposition")
}
/// Content-Encoding
///
@@ -270,12 +299,16 @@ extension HTTPField.Name {
/// Content-Security-Policy
///
/// https://www.w3.org/TR/CSP/
public static var contentSecurityPolicy: Self { .init(rawName: "Content-Security-Policy", canonicalName: "content-security-policy") }
public static var contentSecurityPolicy: Self {
.init(rawName: "Content-Security-Policy", canonicalName: "content-security-policy")
}
/// Content-Security-Policy-Report-Only
///
/// https://www.w3.org/TR/CSP/
public static var contentSecurityPolicyReportOnly: Self { .init(rawName: "Content-Security-Policy-Report-Only", canonicalName: "content-security-policy-report-only") }
public static var contentSecurityPolicyReportOnly: Self {
.init(rawName: "Content-Security-Policy-Report-Only", canonicalName: "content-security-policy-report-only")
}
/// Content-Type
///
@@ -290,7 +323,9 @@ extension HTTPField.Name {
/// Cross-Origin-Resource-Policy
///
/// https://fetch.spec.whatwg.org/
public static var crossOriginResourcePolicy: Self { .init(rawName: "Cross-Origin-Resource-Policy", canonicalName: "cross-origin-resource-policy") }
public static var crossOriginResourcePolicy: Self {
.init(rawName: "Cross-Origin-Resource-Policy", canonicalName: "cross-origin-resource-policy")
}
/// Date
///
@@ -351,7 +386,9 @@ extension HTTPField.Name {
/// If-Unmodified-Since
///
/// https://www.rfc-editor.org/rfc/rfc9110.html
public static var ifUnmodifiedSince: Self { .init(rawName: "If-Unmodified-Since", canonicalName: "if-unmodified-since") }
public static var ifUnmodifiedSince: Self {
.init(rawName: "If-Unmodified-Since", canonicalName: "if-unmodified-since")
}
/// Last-Modified
///
@@ -381,17 +418,23 @@ extension HTTPField.Name {
/// Proxy-Authenticate
///
/// https://www.rfc-editor.org/rfc/rfc9110.html
public static var proxyAuthenticate: Self { .init(rawName: "Proxy-Authenticate", canonicalName: "proxy-authenticate") }
public static var proxyAuthenticate: Self {
.init(rawName: "Proxy-Authenticate", canonicalName: "proxy-authenticate")
}
/// Proxy-Authentication-Info
///
/// https://www.rfc-editor.org/rfc/rfc9110.html
public static var proxyAuthenticationInfo: Self { .init(rawName: "Proxy-Authentication-Info", canonicalName: "proxy-authentication-info") }
public static var proxyAuthenticationInfo: Self {
.init(rawName: "Proxy-Authentication-Info", canonicalName: "proxy-authentication-info")
}
/// Proxy-Authorization
///
/// https://www.rfc-editor.org/rfc/rfc9110.html
public static var proxyAuthorization: Self { .init(rawName: "Proxy-Authorization", canonicalName: "proxy-authorization") }
public static var proxyAuthorization: Self {
.init(rawName: "Proxy-Authorization", canonicalName: "proxy-authorization")
}
/// Proxy-Status
///
@@ -421,12 +464,16 @@ extension HTTPField.Name {
/// Sec-WebSocket-Accept
///
/// https://www.rfc-editor.org/rfc/rfc6455.html
public static var secWebSocketAccept: Self { .init(rawName: "Sec-WebSocket-Accept", canonicalName: "sec-websocket-accept") }
public static var secWebSocketAccept: Self {
.init(rawName: "Sec-WebSocket-Accept", canonicalName: "sec-websocket-accept")
}
/// Sec-WebSocket-Extensions
///
/// https://www.rfc-editor.org/rfc/rfc6455.html
public static var secWebSocketExtensions: Self { .init(rawName: "Sec-WebSocket-Extensions", canonicalName: "sec-websocket-extensions") }
public static var secWebSocketExtensions: Self {
.init(rawName: "Sec-WebSocket-Extensions", canonicalName: "sec-websocket-extensions")
}
/// Sec-WebSocket-Key
///
@@ -436,12 +483,16 @@ extension HTTPField.Name {
/// Sec-WebSocket-Protocol
///
/// https://www.rfc-editor.org/rfc/rfc6455.html
public static var secWebSocketProtocol: Self { .init(rawName: "Sec-WebSocket-Protocol", canonicalName: "sec-websocket-protocol") }
public static var secWebSocketProtocol: Self {
.init(rawName: "Sec-WebSocket-Protocol", canonicalName: "sec-websocket-protocol")
}
/// Sec-WebSocket-Version
///
/// https://www.rfc-editor.org/rfc/rfc6455.html
public static var secWebSocketVersion: Self { .init(rawName: "Sec-WebSocket-Version", canonicalName: "sec-websocket-version") }
public static var secWebSocketVersion: Self {
.init(rawName: "Sec-WebSocket-Version", canonicalName: "sec-websocket-version")
}
/// Server
///
@@ -456,7 +507,9 @@ extension HTTPField.Name {
/// Strict-Transport-Security
///
/// https://www.rfc-editor.org/rfc/rfc6797.html
public static var strictTransportSecurity: Self { .init(rawName: "Strict-Transport-Security", canonicalName: "strict-transport-security") }
public static var strictTransportSecurity: Self {
.init(rawName: "Strict-Transport-Security", canonicalName: "strict-transport-security")
}
/// TE
///
@@ -501,7 +554,9 @@ extension HTTPField.Name {
/// X-Content-Type-Options
///
/// https://fetch.spec.whatwg.org/
public static var xContentTypeOptions: Self { .init(rawName: "X-Content-Type-Options", canonicalName: "x-content-type-options") }
public static var xContentTypeOptions: Self {
.init(rawName: "X-Content-Type-Options", canonicalName: "x-content-type-options")
}
// Deprecated
/// P3P
@@ -522,15 +577,21 @@ extension HTTPField.Name {
/// Proxy-Connection
static var proxyConnection: Self { .init(rawName: "Proxy-Connection", canonicalName: "proxy-connection") }
/// Upgrade-Insecure-Requests
static var upgradeInsecureRequests: Self { .init(rawName: "Upgrade-Insecure-Requests", canonicalName: "upgrade-insecure-requests") }
static var upgradeInsecureRequests: Self {
.init(rawName: "Upgrade-Insecure-Requests", canonicalName: "upgrade-insecure-requests")
}
/// Datagram-Flow-Id
static var datagramFlowId: Self { .init(rawName: "Datagram-Flow-Id", canonicalName: "datagram-flow-id") }
/// Capsule-Protocol
static var capsuleProtocol: Self { .init(rawName: "Capsule-Protocol", canonicalName: "capsule-protocol") }
/// Server-Connection-Id
static var serverConnectionId: Self { .init(rawName: "Server-Connection-Id", canonicalName: "server-connection-id") }
static var serverConnectionId: Self {
.init(rawName: "Server-Connection-Id", canonicalName: "server-connection-id")
}
/// Client-Connection-Id
static var clientConnectionId: Self { .init(rawName: "Client-Connection-Id", canonicalName: "client-connection-id") }
static var clientConnectionId: Self {
.init(rawName: "Client-Connection-Id", canonicalName: "client-connection-id")
}
/// Sec-CH-Background
static var secCHBackground: Self { .init(rawName: "Sec-CH-Background", canonicalName: "sec-ch-background") }
/// Sec-CH-Geohash
@@ -538,7 +599,9 @@ extension HTTPField.Name {
/// Client-Geohash
static var clientGeohash: Self { .init(rawName: "Client-Geohash", canonicalName: "client-geohash") }
/// Proxy-QUIC-Forwarding
static var proxyQUICForwarding: Self { .init(rawName: "Proxy-QUIC-Forwarding", canonicalName: "proxy-quic-forwarding") }
static var proxyQUICForwarding: Self {
.init(rawName: "Proxy-QUIC-Forwarding", canonicalName: "proxy-quic-forwarding")
}
/// Proxy-Config-Epoch
static var proxyConfigEpoch: Self { .init(rawName: "Proxy-Config-Epoch", canonicalName: "proxy-config-epoch") }
/// Connect-UDP-Bind
+21 -10
View File
@@ -133,10 +133,14 @@ public struct HTTPFields: Sendable, Hashable {
set {
if let newValue {
if #available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *),
name == .cookie {
self.setFields(newValue.split(separator: "; ", omittingEmptySubsequences: false).lazy.map {
HTTPField(name: name, value: String($0))
}, for: name)
name == .cookie
{
self.setFields(
newValue.split(separator: "; ", omittingEmptySubsequences: false).lazy.map {
HTTPField(name: name, value: String($0))
},
for: name
)
} else {
self.setFields(CollectionOfOne(HTTPField(name: name, value: newValue)), for: name)
}
@@ -278,7 +282,8 @@ extension HTTPFields: RangeReplaceableCollection, RandomAccessCollection, Mutabl
}
}
public mutating func replaceSubrange<C>(_ subrange: Range<Int>, with newElements: C) where C: Collection, Element == C.Element {
public mutating func replaceSubrange<C>(_ subrange: Range<Int>, with newElements: C)
where C: Collection, Element == C.Element {
if !isKnownUniquelyReferenced(&self._storage) {
self._storage = self._storage.copy()
}
@@ -289,10 +294,13 @@ extension HTTPFields: RangeReplaceableCollection, RandomAccessCollection, Mutabl
}
} else {
self._storage.index = nil
self._storage.fields.replaceSubrange(subrange, with: newElements.lazy.map { field in
precondition(!field.name.isPseudo, "Pseudo header field \"\(field.name)\" disallowed")
return (field, 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")
}
}
@@ -326,7 +334,10 @@ extension HTTPFields: Codable {
while !container.isAtEnd {
let field = try container.decode(HTTPField.self)
guard !field.name.isPseudo else {
throw DecodingError.dataCorruptedError(in: container, debugDescription: "Pseudo header field \"\(field)\" disallowed")
throw DecodingError.dataCorruptedError(
in: container,
debugDescription: "Pseudo header field \"\(field)\" disallowed"
)
}
self.append(field)
}
+30 -6
View File
@@ -111,9 +111,18 @@ struct HTTPParsedFields {
throw ParsingError.requestWithResponsePseudo
}
try self.validateFields()
var request = HTTPRequest(method: requestMethod, scheme: self.scheme, authority: self.authority, path: self.path, headerFields: self.fields)
var request = HTTPRequest(
method: requestMethod,
scheme: self.scheme,
authority: self.authority,
path: self.path,
headerFields: self.fields
)
if let extendedConnectProtocol = self.extendedConnectProtocol {
request.pseudoHeaderFields.extendedConnectProtocol = HTTPField(name: .protocol, uncheckedValue: extendedConnectProtocol)
request.pseudoHeaderFields.extendedConnectProtocol = HTTPField(
name: .protocol,
uncheckedValue: extendedConnectProtocol
)
}
return request
}
@@ -124,7 +133,9 @@ struct HTTPParsedFields {
guard let statusString = self.status?._storage else {
throw ParsingError.responseWithoutStatus
}
if self.method != nil || self.scheme != nil || self.authority != nil || self.path != nil || self.extendedConnectProtocol != nil {
if self.method != nil || self.scheme != nil || self.authority != nil || self.path != nil
|| self.extendedConnectProtocol != nil
{
throw ParsingError.responseWithRequestPseudo
}
if !HTTPResponse.Status.isValidStatus(statusString) {
@@ -137,7 +148,9 @@ struct HTTPParsedFields {
var trailerFields: HTTPFields {
get throws {
if self.method != nil || self.scheme != nil || self.authority != nil || self.path != nil || self.extendedConnectProtocol != nil || self.status != nil {
if self.method != nil || self.scheme != nil || self.authority != nil || self.path != nil
|| self.extendedConnectProtocol != nil || self.status != nil
{
throw ParsingError.trailerFieldsWithPseudo
}
try self.validateFields()
@@ -147,12 +160,23 @@ struct HTTPParsedFields {
}
extension HTTPRequest {
fileprivate init(method: Method, scheme: ISOLatin1String?, authority: ISOLatin1String?, path: ISOLatin1String?, headerFields: HTTPFields) {
fileprivate init(
method: Method,
scheme: ISOLatin1String?,
authority: ISOLatin1String?,
path: ISOLatin1String?,
headerFields: HTTPFields
) {
let methodField = HTTPField(name: .method, uncheckedValue: ISOLatin1String(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) }
self.pseudoHeaderFields = .init(method: methodField, scheme: schemeField, authority: authorityField, path: pathField)
self.pseudoHeaderFields = .init(
method: methodField,
scheme: schemeField,
authority: authorityField,
path: pathField
)
self.headerFields = headerFields
}
}
+38 -9
View File
@@ -213,7 +213,12 @@ public struct HTTPRequest: Sendable, Hashable {
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) }
self.pseudoHeaderFields = .init(method: methodField, scheme: schemeField, authority: authorityField, path: pathField)
self.pseudoHeaderFields = .init(
method: methodField,
scheme: schemeField,
authority: authorityField,
path: pathField
)
self.headerFields = headerFields
}
}
@@ -254,40 +259,64 @@ extension HTTPRequest.PseudoHeaderFields: Codable {
switch field.name {
case .method:
guard method == nil else {
throw DecodingError.dataCorruptedError(in: container, debugDescription: "Multiple \":method\" pseudo header fields")
throw DecodingError.dataCorruptedError(
in: container,
debugDescription: "Multiple \":method\" pseudo header fields"
)
}
method = field
case .scheme:
guard scheme == nil else {
throw DecodingError.dataCorruptedError(in: container, debugDescription: "Multiple \":scheme\" pseudo header fields")
throw DecodingError.dataCorruptedError(
in: container,
debugDescription: "Multiple \":scheme\" pseudo header fields"
)
}
scheme = field
case .authority:
guard authority == nil else {
throw DecodingError.dataCorruptedError(in: container, debugDescription: "Multiple \":authority\" pseudo header fields")
throw DecodingError.dataCorruptedError(
in: container,
debugDescription: "Multiple \":authority\" pseudo header fields"
)
}
authority = field
case .path:
guard path == nil else {
throw DecodingError.dataCorruptedError(in: container, debugDescription: "Multiple \":path\" pseudo header fields")
throw DecodingError.dataCorruptedError(
in: container,
debugDescription: "Multiple \":path\" pseudo header fields"
)
}
path = field
case .protocol:
guard extendedConnectProtocol == nil else {
throw DecodingError.dataCorruptedError(in: container, debugDescription: "Multiple \":protocol\" pseudo header fields")
throw DecodingError.dataCorruptedError(
in: container,
debugDescription: "Multiple \":protocol\" pseudo header fields"
)
}
extendedConnectProtocol = field
default:
guard field.name.rawName.hasPrefix(":") else {
throw DecodingError.dataCorruptedError(in: container, debugDescription: "\"\(field)\" is not a pseudo header field")
throw DecodingError.dataCorruptedError(
in: container,
debugDescription: "\"\(field)\" is not a pseudo header field"
)
}
}
}
guard let method else {
throw DecodingError.dataCorruptedError(in: container, debugDescription: "\":method\" pseudo header field is missing")
throw DecodingError.dataCorruptedError(
in: container,
debugDescription: "\":method\" pseudo header field is missing"
)
}
guard HTTPField.isValidToken(method.rawValue._storage) else {
throw DecodingError.dataCorruptedError(in: container, debugDescription: "\"\(method.rawValue._storage)\" is not a valid method")
throw DecodingError.dataCorruptedError(
in: container,
debugDescription: "\"\(method.rawValue._storage)\" is not a valid method"
)
}
self.init(
method: method,
+47 -23
View File
@@ -33,7 +33,7 @@ public struct HTTPResponse: Sendable, Hashable {
/// characters not representable in ISO Latin 1 encoding, are converted
/// into space characters.
public init(code: Int, reasonPhrase: String = "") {
precondition((0 ... 999).contains(code), "Invalid status code")
precondition((0...999).contains(code), "Invalid status code")
self.code = code
self.reasonPhrase = Self.legalizingReasonPhrase(reasonPhrase)
}
@@ -46,7 +46,7 @@ public struct HTTPResponse: Sendable, Hashable {
/// Create a custom status from an integer literal.
/// - Parameter value: The status code.
public init(integerLiteral value: Int) {
precondition((0 ... 999).contains(value), "Invalid status code")
precondition((0...999).contains(value), "Invalid status code")
self.code = value
self.reasonPhrase = ""
}
@@ -70,15 +70,15 @@ public struct HTTPResponse: Sendable, Hashable {
/// The kind of the status code.
public var kind: Kind {
switch self.code {
case 100 ... 199:
case 100...199:
return .informational
case 200 ... 299:
case 200...299:
return .successful
case 300 ... 399:
case 300...399:
return .redirection
case 400 ... 499:
case 400...499:
return .clientError
case 500 ... 599:
case 500...599:
return .serverError
default:
return .invalid
@@ -115,7 +115,7 @@ public struct HTTPResponse: Sendable, Hashable {
}
static func isValidStatus(_ status: String) -> Bool {
status.count == 3 && status.utf8.allSatisfy { (0x30 ... 0x39).contains($0) }
status.count == 3 && status.utf8.allSatisfy { (0x30...0x39).contains($0) }
}
static func isValidReasonPhrase(_ reasonPhrase: String) -> Bool {
@@ -123,7 +123,7 @@ public struct HTTPResponse: Sendable, Hashable {
switch $0 {
case 0x09, 0x20:
return true
case 0x21 ... 0x7E, 0x80 ... 0xFF:
case 0x21...0x7E, 0x80...0xFF:
return true
default:
return false
@@ -139,7 +139,7 @@ public struct HTTPResponse: Sendable, Hashable {
switch scala.value {
case 0x09, 0x20:
return scala
case 0x21 ... 0x7E, 0x80 ... 0xFF:
case 0x21...0x7E, 0x80...0xFF:
return scala
default:
return " "
@@ -159,9 +159,9 @@ public struct HTTPResponse: Sendable, Hashable {
public var status: Status {
get {
var codeIterator = self.pseudoHeaderFields.status.rawValue._storage.utf8.makeIterator()
let code = Int(codeIterator.next()! - 48) * 100 +
Int(codeIterator.next()! - 48) * 10 +
Int(codeIterator.next()! - 48)
let code =
Int(codeIterator.next()! - 48) * 100 + Int(codeIterator.next()! - 48) * 10
+ Int(codeIterator.next()! - 48)
return Status(uncheckedCode: code, reasonPhrase: self.reasonPhrase)
}
set {
@@ -232,20 +232,32 @@ extension HTTPResponse.PseudoHeaderFields: Codable {
switch field.name {
case .status:
guard status == nil else {
throw DecodingError.dataCorruptedError(in: container, debugDescription: "Multiple \":status\" pseudo header fields")
throw DecodingError.dataCorruptedError(
in: container,
debugDescription: "Multiple \":status\" pseudo header fields"
)
}
status = field
default:
guard field.name.rawName.hasPrefix(":") else {
throw DecodingError.dataCorruptedError(in: container, debugDescription: "\"\(field)\" is not a pseudo header field")
throw DecodingError.dataCorruptedError(
in: container,
debugDescription: "\"\(field)\" is not a pseudo header field"
)
}
}
}
guard let status else {
throw DecodingError.dataCorruptedError(in: container, debugDescription: "\":status\" pseudo header field is missing")
throw DecodingError.dataCorruptedError(
in: container,
debugDescription: "\":status\" pseudo header field is missing"
)
}
guard HTTPResponse.Status.isValidStatus(status.rawValue._storage) else {
throw DecodingError.dataCorruptedError(in: container, debugDescription: "\"\(status.rawValue._storage)\" is not a valid status code")
throw DecodingError.dataCorruptedError(
in: container,
debugDescription: "\"\(status.rawValue._storage)\" is not a valid status code"
)
}
self.init(status: status)
}
@@ -319,7 +331,9 @@ extension HTTPResponse.Status {
/// 203 Non-Authoritative Information
///
/// https://www.rfc-editor.org/rfc/rfc9110.html
public static var nonAuthoritativeInformation: Self { .init(uncheckedCode: 203, reasonPhrase: "Non-Authoritative Information") }
public static var nonAuthoritativeInformation: Self {
.init(uncheckedCode: 203, reasonPhrase: "Non-Authoritative Information")
}
/// 204 No Content
///
@@ -408,7 +422,9 @@ extension HTTPResponse.Status {
/// 407 Proxy Authentication Required
///
/// https://www.rfc-editor.org/rfc/rfc9110.html
public static var proxyAuthenticationRequired: Self { .init(uncheckedCode: 407, reasonPhrase: "Proxy Authentication Required") }
public static var proxyAuthenticationRequired: Self {
.init(uncheckedCode: 407, reasonPhrase: "Proxy Authentication Required")
}
/// 408 Request Timeout
///
@@ -491,12 +507,16 @@ extension HTTPResponse.Status {
/// 431 Request Header Fields Too Large
///
/// https://www.rfc-editor.org/rfc/rfc6585.html
public static var requestHeaderFieldsTooLarge: Self { .init(uncheckedCode: 431, reasonPhrase: "Request Header Fields Too Large") }
public static var requestHeaderFieldsTooLarge: Self {
.init(uncheckedCode: 431, reasonPhrase: "Request Header Fields Too Large")
}
/// 451 Unavailable For Legal Reasons
///
/// https://www.rfc-editor.org/rfc/rfc7725.html
public static var unavailableForLegalReasons: Self { .init(uncheckedCode: 451, reasonPhrase: "Unavailable For Legal Reasons") }
public static var unavailableForLegalReasons: Self {
.init(uncheckedCode: 451, reasonPhrase: "Unavailable For Legal Reasons")
}
// MARK: 5xx
@@ -528,10 +548,14 @@ extension HTTPResponse.Status {
/// 505 HTTP Version Not Supported
///
/// https://www.rfc-editor.org/rfc/rfc9110.html
public static var httpVersionNotSupported: Self { .init(uncheckedCode: 505, reasonPhrase: "HTTP Version Not Supported") }
public static var httpVersionNotSupported: Self {
.init(uncheckedCode: 505, reasonPhrase: "HTTP Version Not Supported")
}
/// 511 Network Authentication Required
///
/// https://www.rfc-editor.org/rfc/rfc6585.html
public static var networkAuthenticationRequired: Self { .init(uncheckedCode: 511, reasonPhrase: "Network Authentication Required") }
public static var networkAuthenticationRequired: Self {
.init(uncheckedCode: 511, reasonPhrase: "Network Authentication Required")
}
}
+3 -1
View File
@@ -28,7 +28,9 @@ struct ISOLatin1String: Sendable, Hashable {
return string
}
private func withISOLatin1BytesSlowPath<Result>(_ body: (UnsafeBufferPointer<UInt8>) throws -> Result) rethrows -> Result {
private func withISOLatin1BytesSlowPath<Result>(
_ body: (UnsafeBufferPointer<UInt8>) throws -> Result
) rethrows -> Result {
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)