mirror of
https://github.com/apple/swift-http-types
synced 2026-11-27 05:04:21 +00:00
11 files changed
+63
-59
No files matched your search
@@ -16,5 +16,6 @@
|
||||
|
||||
# rules
|
||||
|
||||
--disable andOperator
|
||||
--disable blankLinesAroundMark
|
||||
--disable wrapMultilineStatementBraces
|
||||
@@ -12,6 +12,8 @@ needs to be listed here.
|
||||
### Contributors
|
||||
|
||||
- Guoye Zhang <guoye_zhang@apple.com>
|
||||
- Sergey Dmitriev <s.dmitriev@safe-tech.ru>
|
||||
- Tomohiro Kumagai <tomo@ez-net.jp>
|
||||
|
||||
**Updating this list**
|
||||
|
||||
|
||||
@@ -57,7 +57,7 @@ extension HTTPField {
|
||||
|
||||
extension HTTPField.Name: Hashable {
|
||||
public func hash(into hasher: inout Hasher) {
|
||||
hasher.combine(canonicalName)
|
||||
hasher.combine(self.canonicalName)
|
||||
}
|
||||
|
||||
public static func == (lhs: HTTPField.Name, rhs: HTTPField.Name) -> Bool {
|
||||
@@ -67,7 +67,7 @@ extension HTTPField.Name: Hashable {
|
||||
|
||||
extension HTTPField.Name: LosslessStringConvertible {
|
||||
public var description: String {
|
||||
rawName
|
||||
self.rawName
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -40,28 +40,28 @@ public struct HTTPFields: Sendable, Hashable {
|
||||
#if canImport(os.lock)
|
||||
self.lock.initialize(to: os_unfair_lock())
|
||||
#else
|
||||
let err = pthread_mutex_init(lock, nil)
|
||||
let err = pthread_mutex_init(self.lock, nil)
|
||||
precondition(err == 0, "pthread_mutex_init failed with error \(err)")
|
||||
#endif
|
||||
}
|
||||
|
||||
deinit {
|
||||
#if !canImport(os.lock)
|
||||
let err = pthread_mutex_destroy(lock)
|
||||
let err = pthread_mutex_destroy(self.lock)
|
||||
precondition(err == 0, "pthread_mutex_destroy failed with error \(err)")
|
||||
#endif
|
||||
lock.deallocate()
|
||||
self.lock.deallocate()
|
||||
}
|
||||
|
||||
var ensureIndex: [String: UInt16] {
|
||||
#if canImport(os.lock)
|
||||
os_unfair_lock_lock(self.lock)
|
||||
defer { os_unfair_lock_unlock(lock) }
|
||||
defer { os_unfair_lock_unlock(self.lock) }
|
||||
#else
|
||||
let err = pthread_mutex_lock(lock)
|
||||
let err = pthread_mutex_lock(self.lock)
|
||||
precondition(err == 0, "pthread_mutex_lock failed with error \(err)")
|
||||
defer {
|
||||
let err = pthread_mutex_unlock(lock)
|
||||
let err = pthread_mutex_unlock(self.lock)
|
||||
precondition(err == 0, "pthread_mutex_unlock failed with error \(err)")
|
||||
}
|
||||
#endif
|
||||
@@ -69,12 +69,12 @@ public struct HTTPFields: Sendable, Hashable {
|
||||
return index
|
||||
}
|
||||
var newIndex = [String: UInt16]()
|
||||
for i in self.fields.indices.reversed() {
|
||||
let name = self.fields[i].0.name.canonicalName
|
||||
self.fields[i].1 = newIndex[name] ?? .max
|
||||
newIndex[name] = UInt16(i)
|
||||
for index in self.fields.indices.reversed() {
|
||||
let name = self.fields[index].0.name.canonicalName
|
||||
self.fields[index].1 = newIndex[name] ?? .max
|
||||
newIndex[name] = UInt16(index)
|
||||
}
|
||||
index = newIndex
|
||||
self.index = newIndex
|
||||
return newIndex
|
||||
}
|
||||
|
||||
@@ -85,7 +85,7 @@ public struct HTTPFields: Sendable, Hashable {
|
||||
os_unfair_lock_lock(self.lock)
|
||||
#else
|
||||
do {
|
||||
let err = pthread_mutex_lock(lock)
|
||||
let err = pthread_mutex_lock(self.lock)
|
||||
precondition(err == 0, "pthread_mutex_lock failed with error \(err)")
|
||||
}
|
||||
#endif
|
||||
@@ -94,7 +94,7 @@ public struct HTTPFields: Sendable, Hashable {
|
||||
os_unfair_lock_unlock(self.lock)
|
||||
#else
|
||||
do {
|
||||
let err = pthread_mutex_unlock(lock)
|
||||
let err = pthread_mutex_unlock(self.lock)
|
||||
precondition(err == 0, "pthread_mutex_unlock failed with error \(err)")
|
||||
}
|
||||
#endif
|
||||
@@ -113,7 +113,7 @@ public struct HTTPFields: Sendable, Hashable {
|
||||
|
||||
func append(field: HTTPField) {
|
||||
let name = field.name.canonicalName
|
||||
if var index = index?[name] {
|
||||
if var index = self.index?[name] {
|
||||
while true {
|
||||
let next = self.fields[Int(index)].1
|
||||
if next == .max { break }
|
||||
@@ -121,7 +121,7 @@ public struct HTTPFields: Sendable, Hashable {
|
||||
}
|
||||
self.fields[Int(index)].1 = UInt16(self.fields.count)
|
||||
} else {
|
||||
index?[name] = UInt16(self.fields.count)
|
||||
self.index?[name] = UInt16(self.fields.count)
|
||||
}
|
||||
self.fields.append((field, .max))
|
||||
}
|
||||
@@ -291,12 +291,12 @@ extension HTTPFields: RangeReplaceableCollection, RandomAccessCollection, Mutabl
|
||||
}
|
||||
}
|
||||
|
||||
public mutating func reserveCapacity(_ n: Int) {
|
||||
public mutating func reserveCapacity(_ capacity: Int) {
|
||||
if !isKnownUniquelyReferenced(&self._storage) {
|
||||
self._storage = self._storage.copy()
|
||||
}
|
||||
self._storage.index?.reserveCapacity(n)
|
||||
self._storage.fields.reserveCapacity(n)
|
||||
self._storage.index?.reserveCapacity(capacity)
|
||||
self._storage.fields.reserveCapacity(capacity)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -312,14 +312,14 @@ extension Array {
|
||||
var offset = 0
|
||||
var iterator = removalIndices.makeIterator()
|
||||
var nextToRemoveOptional = iterator.next()
|
||||
for i in indices {
|
||||
while let nextToRemove = nextToRemoveOptional, index(i, offsetBy: offset) == nextToRemove {
|
||||
for index in self.indices {
|
||||
while let nextToRemove = nextToRemoveOptional, self.index(index, offsetBy: offset) == nextToRemove {
|
||||
offset += 1
|
||||
nextToRemoveOptional = iterator.next()
|
||||
}
|
||||
let toKeep = index(i, offsetBy: offset)
|
||||
if toKeep < endIndex {
|
||||
swapAt(i, toKeep)
|
||||
let toKeep = self.index(index, offsetBy: offset)
|
||||
if toKeep < self.endIndex {
|
||||
self.swapAt(index, toKeep)
|
||||
} else {
|
||||
break
|
||||
}
|
||||
|
||||
@@ -14,7 +14,7 @@
|
||||
|
||||
extension String {
|
||||
var isASCII: Bool {
|
||||
utf8.allSatisfy { $0 & 0x80 == 0 }
|
||||
self.utf8.allSatisfy { $0 & 0x80 == 0 }
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -21,9 +21,9 @@ extension HTTPRequest {
|
||||
/// fields.
|
||||
public var url: URL? {
|
||||
get {
|
||||
if let schemeField = pseudoHeaderFields.scheme,
|
||||
let authorityField = pseudoHeaderFields.authority,
|
||||
let pathField = pseudoHeaderFields.path {
|
||||
if let schemeField = self.pseudoHeaderFields.scheme,
|
||||
let authorityField = self.pseudoHeaderFields.authority,
|
||||
let pathField = self.pseudoHeaderFields.path {
|
||||
return schemeField.withUnsafeBytesOfValue { scheme in
|
||||
authorityField.withUnsafeBytesOfValue { authority in
|
||||
pathField.withUnsafeBytesOfValue { path in
|
||||
@@ -42,9 +42,9 @@ extension HTTPRequest {
|
||||
self.authority = authority.map { String(decoding: $0, as: UTF8.self) }
|
||||
self.path = String(decoding: path, as: UTF8.self)
|
||||
} else {
|
||||
pseudoHeaderFields.scheme = nil
|
||||
pseudoHeaderFields.authority = nil
|
||||
pseudoHeaderFields.path = nil
|
||||
self.pseudoHeaderFields.scheme = nil
|
||||
self.pseudoHeaderFields.authority = nil
|
||||
self.pseudoHeaderFields.path = nil
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -89,10 +89,10 @@ extension URL {
|
||||
return withUnsafeTemporaryAllocation(of: UInt8.self, capacity: length) { buffer in
|
||||
CFURLGetBytes(url, buffer.baseAddress, buffer.count)
|
||||
|
||||
func unionRange(_ a: CFRange, _ b: CFRange) -> CFRange {
|
||||
if a.location == kCFNotFound { return b }
|
||||
if b.location == kCFNotFound { return a }
|
||||
return CFRange(location: a.location, length: b.location + b.length - a.location)
|
||||
func unionRange(_ first: CFRange, _ second: CFRange) -> CFRange {
|
||||
if first.location == kCFNotFound { return second }
|
||||
if second.location == kCFNotFound { return first }
|
||||
return CFRange(location: first.location, length: second.location + second.length - first.location)
|
||||
}
|
||||
|
||||
func bufferSlice(_ range: CFRange) -> UnsafeMutableBufferPointer<UInt8> {
|
||||
|
||||
@@ -16,7 +16,7 @@ import HTTPTypes
|
||||
|
||||
extension String {
|
||||
var isASCII: Bool {
|
||||
utf8.allSatisfy { $0 & 0x80 == 0 }
|
||||
self.utf8.allSatisfy { $0 & 0x80 == 0 }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -39,10 +39,10 @@ extension HTTPField {
|
||||
}
|
||||
|
||||
var isoLatin1Value: String {
|
||||
if value.isASCII {
|
||||
return value
|
||||
if self.value.isASCII {
|
||||
return self.value
|
||||
} else {
|
||||
return withUnsafeBytesOfValue { buffer in
|
||||
return self.withUnsafeBytesOfValue { buffer in
|
||||
let scalars = buffer.lazy.map { UnicodeScalar(UInt32($0))! }
|
||||
var string = ""
|
||||
string.unicodeScalars.append(contentsOf: scalars)
|
||||
|
||||
@@ -46,13 +46,13 @@ extension URLRequest {
|
||||
|
||||
/// Convert the `URLRequest` into an `HTTPRequest`.
|
||||
public var httpRequest: HTTPRequest? {
|
||||
guard let method = HTTPRequest.Method(httpMethod ?? "GET"),
|
||||
guard let method = HTTPRequest.Method(self.httpMethod ?? "GET"),
|
||||
let url
|
||||
else {
|
||||
return nil
|
||||
}
|
||||
var request = HTTPRequest(method: method, url: url)
|
||||
if let allHTTPHeaderFields {
|
||||
if let allHTTPHeaderFields = self.allHTTPHeaderFields {
|
||||
request.headerFields.reserveCapacity(allHTTPHeaderFields.count)
|
||||
for (name, value) in allHTTPHeaderFields {
|
||||
if let name = HTTPField.Name(name) {
|
||||
|
||||
@@ -40,11 +40,11 @@ extension HTTPURLResponse {
|
||||
|
||||
/// Convert the `HTTPURLResponse` into an `HTTPResponse`.
|
||||
public var httpResponse: HTTPResponse? {
|
||||
guard (0 ... 999).contains(statusCode) else {
|
||||
guard (0 ... 999).contains(self.statusCode) else {
|
||||
return nil
|
||||
}
|
||||
var response = HTTPResponse(status: .init(code: statusCode))
|
||||
if let fields = allHeaderFields as? [String: String] {
|
||||
var response = HTTPResponse(status: .init(code: self.statusCode))
|
||||
if let fields = self.allHeaderFields as? [String: String] {
|
||||
response.headerFields.reserveCapacity(fields.count)
|
||||
for (name, value) in fields {
|
||||
if let name = HTTPField.Name(name) {
|
||||
|
||||
@@ -21,17 +21,17 @@ import FoundationNetworking
|
||||
extension URLSessionTask {
|
||||
/// The original HTTP request this task was created with.
|
||||
public var originalHTTPRequest: HTTPRequest? {
|
||||
originalRequest?.httpRequest
|
||||
self.originalRequest?.httpRequest
|
||||
}
|
||||
|
||||
/// The current HTTP request -- may differ from the `originalHTTPRequest` due to HTTP redirection.
|
||||
public var currentHTTPRequest: HTTPRequest? {
|
||||
currentRequest?.httpRequest
|
||||
self.currentRequest?.httpRequest
|
||||
}
|
||||
|
||||
/// The HTTP response received from the server.
|
||||
public var httpResponse: HTTPResponse? {
|
||||
(response as? HTTPURLResponse)?.httpResponse
|
||||
(self.response as? HTTPURLResponse)?.httpResponse
|
||||
}
|
||||
}
|
||||
|
||||
@@ -41,6 +41,7 @@ private enum HTTPTypeConversionError: Error {
|
||||
}
|
||||
|
||||
#if os(macOS) || os(iOS) || os(watchOS) || os(tvOS) || os(visionOS)
|
||||
|
||||
@available(macOS 12.0, iOS 15.0, watchOS 8.0, tvOS 15.0, *)
|
||||
extension URLSession {
|
||||
/// Convenience method to load data using an `HTTPRequest`; creates and resumes a `URLSessionDataTask` internally.
|
||||
@@ -52,7 +53,7 @@ extension URLSession {
|
||||
guard let urlRequest = URLRequest(httpRequest: request) else {
|
||||
throw HTTPTypeConversionError.failedToConvertHTTPRequestToURLRequest
|
||||
}
|
||||
let (data, urlResponse) = try await data(for: urlRequest, delegate: delegate)
|
||||
let (data, urlResponse) = try await self.data(for: urlRequest, delegate: delegate)
|
||||
guard let response = (urlResponse as? HTTPURLResponse)?.httpResponse else {
|
||||
throw HTTPTypeConversionError.failedToConvertURLResponseToHTTPResponse
|
||||
}
|
||||
@@ -69,7 +70,7 @@ extension URLSession {
|
||||
guard let urlRequest = URLRequest(httpRequest: request) else {
|
||||
throw HTTPTypeConversionError.failedToConvertHTTPRequestToURLRequest
|
||||
}
|
||||
let (data, urlResponse) = try await upload(for: urlRequest, fromFile: fileURL, delegate: delegate)
|
||||
let (data, urlResponse) = try await self.upload(for: urlRequest, fromFile: fileURL, delegate: delegate)
|
||||
guard let response = (urlResponse as? HTTPURLResponse)?.httpResponse else {
|
||||
throw HTTPTypeConversionError.failedToConvertURLResponseToHTTPResponse
|
||||
}
|
||||
@@ -86,7 +87,7 @@ extension URLSession {
|
||||
guard let urlRequest = URLRequest(httpRequest: request) else {
|
||||
throw HTTPTypeConversionError.failedToConvertHTTPRequestToURLRequest
|
||||
}
|
||||
let (data, urlResponse) = try await upload(for: urlRequest, from: bodyData, delegate: delegate)
|
||||
let (data, urlResponse) = try await self.upload(for: urlRequest, from: bodyData, delegate: delegate)
|
||||
guard let response = (urlResponse as? HTTPURLResponse)?.httpResponse else {
|
||||
throw HTTPTypeConversionError.failedToConvertURLResponseToHTTPResponse
|
||||
}
|
||||
@@ -102,7 +103,7 @@ extension URLSession {
|
||||
guard let urlRequest = URLRequest(httpRequest: request) else {
|
||||
throw HTTPTypeConversionError.failedToConvertHTTPRequestToURLRequest
|
||||
}
|
||||
let (location, urlResponse) = try await download(for: urlRequest, delegate: delegate)
|
||||
let (location, urlResponse) = try await self.download(for: urlRequest, delegate: delegate)
|
||||
guard let response = (urlResponse as? HTTPURLResponse)?.httpResponse else {
|
||||
throw HTTPTypeConversionError.failedToConvertURLResponseToHTTPResponse
|
||||
}
|
||||
@@ -118,7 +119,7 @@ extension URLSession {
|
||||
guard let urlRequest = URLRequest(httpRequest: request) else {
|
||||
throw HTTPTypeConversionError.failedToConvertHTTPRequestToURLRequest
|
||||
}
|
||||
let (data, urlResponse) = try await bytes(for: urlRequest, delegate: delegate)
|
||||
let (data, urlResponse) = try await self.bytes(for: urlRequest, delegate: delegate)
|
||||
guard let response = (urlResponse as? HTTPURLResponse)?.httpResponse else {
|
||||
throw HTTPTypeConversionError.failedToConvertURLResponseToHTTPResponse
|
||||
}
|
||||
@@ -136,7 +137,7 @@ extension URLSession {
|
||||
guard let urlRequest = URLRequest(httpRequest: request) else {
|
||||
throw HTTPTypeConversionError.failedToConvertHTTPRequestToURLRequest
|
||||
}
|
||||
let (data, urlResponse) = try await data(for: urlRequest)
|
||||
let (data, urlResponse) = try await self.data(for: urlRequest)
|
||||
guard let response = (urlResponse as? HTTPURLResponse)?.httpResponse else {
|
||||
throw HTTPTypeConversionError.failedToConvertURLResponseToHTTPResponse
|
||||
}
|
||||
@@ -152,7 +153,7 @@ extension URLSession {
|
||||
guard let urlRequest = URLRequest(httpRequest: request) else {
|
||||
throw HTTPTypeConversionError.failedToConvertHTTPRequestToURLRequest
|
||||
}
|
||||
let (data, urlResponse) = try await upload(for: urlRequest, fromFile: fileURL)
|
||||
let (data, urlResponse) = try await self.upload(for: urlRequest, fromFile: fileURL)
|
||||
guard let response = (urlResponse as? HTTPURLResponse)?.httpResponse else {
|
||||
throw HTTPTypeConversionError.failedToConvertURLResponseToHTTPResponse
|
||||
}
|
||||
@@ -168,7 +169,7 @@ extension URLSession {
|
||||
guard let urlRequest = URLRequest(httpRequest: request) else {
|
||||
throw HTTPTypeConversionError.failedToConvertHTTPRequestToURLRequest
|
||||
}
|
||||
let (data, urlResponse) = try await upload(for: urlRequest, from: bodyData)
|
||||
let (data, urlResponse) = try await self.upload(for: urlRequest, from: bodyData)
|
||||
guard let response = (urlResponse as? HTTPURLResponse)?.httpResponse else {
|
||||
throw HTTPTypeConversionError.failedToConvertURLResponseToHTTPResponse
|
||||
}
|
||||
|
||||
@@ -77,14 +77,14 @@ final class HTTPTypesTests: XCTestCase {
|
||||
func isSendable(_ value: some Sendable) -> Bool { true }
|
||||
func isSendable(_ value: Any) -> Bool { false }
|
||||
|
||||
let field: HTTPField = HTTPField(name: .userAgent, value: "")
|
||||
let field: HTTPField = .init(name: .userAgent, value: "")
|
||||
let indexingStrategy: HTTPField.DynamicTableIndexingStrategy = field.indexingStrategy
|
||||
let name: HTTPField.Name = field.name
|
||||
let fields: HTTPFields = [:]
|
||||
let request: HTTPRequest = HTTPRequest(method: .post, scheme: nil, authority: nil, path: nil)
|
||||
let request: HTTPRequest = .init(method: .post, scheme: nil, authority: nil, path: nil)
|
||||
let method: HTTPRequest.Method = request.method
|
||||
let requestPseudoHeaderFields: HTTPRequest.PseudoHeaderFields = request.pseudoHeaderFields
|
||||
let response: HTTPResponse = HTTPResponse(status: .ok)
|
||||
let response: HTTPResponse = .init(status: .ok)
|
||||
let status: HTTPResponse.Status = response.status
|
||||
let responsePseudoHeaderFields: HTTPResponse.PseudoHeaderFields = response.pseudoHeaderFields
|
||||
|
||||
|
||||
Reference in new issue
Block a user