mirror of
https://github.com/apple/swift-http-types
synced 2026-11-22 04:30:40 +00:00
PseudoHeaderFields adopting CoW to reduce size (#88)
HTTPRequest size 288 -> 16 HTTPResponse size 80 -> 16 rdar://144520456
This commit is contained in:
1 parent
4bd2f8104f
commit
e8011a7bc3
3 files changed
+178
-24
No files matched your search
@@ -146,53 +146,156 @@ public struct HTTPRequest: Sendable, Hashable {
|
||||
|
||||
/// The pseudo header fields of a request.
|
||||
public struct PseudoHeaderFields: Sendable, Hashable {
|
||||
private final class _Storage: @unchecked Sendable, Hashable {
|
||||
var method: HTTPField
|
||||
var scheme: HTTPField?
|
||||
var authority: HTTPField?
|
||||
var path: HTTPField?
|
||||
var extendedConnectProtocol: HTTPField?
|
||||
|
||||
init(
|
||||
method: HTTPField,
|
||||
scheme: HTTPField?,
|
||||
authority: HTTPField?,
|
||||
path: HTTPField?,
|
||||
extendedConnectProtocol: HTTPField?
|
||||
) {
|
||||
self.method = method
|
||||
self.scheme = scheme
|
||||
self.authority = authority
|
||||
self.path = path
|
||||
self.extendedConnectProtocol = extendedConnectProtocol
|
||||
}
|
||||
|
||||
func copy() -> Self {
|
||||
.init(
|
||||
method: self.method,
|
||||
scheme: self.scheme,
|
||||
authority: self.authority,
|
||||
path: self.path,
|
||||
extendedConnectProtocol: self.extendedConnectProtocol
|
||||
)
|
||||
}
|
||||
|
||||
static func == (lhs: _Storage, rhs: _Storage) -> Bool {
|
||||
lhs.method == rhs.method && lhs.scheme == rhs.scheme && lhs.authority == rhs.authority
|
||||
&& lhs.path == rhs.path && lhs.extendedConnectProtocol == rhs.extendedConnectProtocol
|
||||
}
|
||||
|
||||
func hash(into hasher: inout Hasher) {
|
||||
hasher.combine(self.method)
|
||||
hasher.combine(self.scheme)
|
||||
hasher.combine(self.authority)
|
||||
hasher.combine(self.path)
|
||||
hasher.combine(self.extendedConnectProtocol)
|
||||
}
|
||||
}
|
||||
|
||||
private var _storage: _Storage
|
||||
|
||||
/// The underlying ":method" pseudo header field.
|
||||
///
|
||||
/// The value of this field must be a valid method.
|
||||
///
|
||||
/// https://www.rfc-editor.org/rfc/rfc9110.html#name-methods
|
||||
public var method: HTTPField {
|
||||
willSet {
|
||||
get {
|
||||
self._storage.method
|
||||
}
|
||||
set {
|
||||
precondition(newValue.name == .method, "Cannot change pseudo-header field name")
|
||||
precondition(HTTPField.isValidToken(newValue.rawValue._storage), "Invalid character in method field")
|
||||
|
||||
if !isKnownUniquelyReferenced(&self._storage) {
|
||||
self._storage = self._storage.copy()
|
||||
}
|
||||
self._storage.method = newValue
|
||||
}
|
||||
}
|
||||
|
||||
/// The underlying ":scheme" pseudo header field.
|
||||
public var scheme: HTTPField? {
|
||||
willSet {
|
||||
get {
|
||||
self._storage.scheme
|
||||
}
|
||||
set {
|
||||
if let name = newValue?.name {
|
||||
precondition(name == .scheme, "Cannot change pseudo-header field name")
|
||||
}
|
||||
|
||||
if !isKnownUniquelyReferenced(&self._storage) {
|
||||
self._storage = self._storage.copy()
|
||||
}
|
||||
self._storage.scheme = newValue
|
||||
}
|
||||
}
|
||||
|
||||
/// The underlying ":authority" pseudo header field.
|
||||
public var authority: HTTPField? {
|
||||
willSet {
|
||||
get {
|
||||
self._storage.authority
|
||||
}
|
||||
set {
|
||||
if let name = newValue?.name {
|
||||
precondition(name == .authority, "Cannot change pseudo-header field name")
|
||||
}
|
||||
|
||||
if !isKnownUniquelyReferenced(&self._storage) {
|
||||
self._storage = self._storage.copy()
|
||||
}
|
||||
self._storage.authority = newValue
|
||||
}
|
||||
}
|
||||
|
||||
/// The underlying ":path" pseudo header field.
|
||||
public var path: HTTPField? {
|
||||
willSet {
|
||||
get {
|
||||
self._storage.path
|
||||
}
|
||||
set {
|
||||
if let name = newValue?.name {
|
||||
precondition(name == .path, "Cannot change pseudo-header field name")
|
||||
}
|
||||
|
||||
if !isKnownUniquelyReferenced(&self._storage) {
|
||||
self._storage = self._storage.copy()
|
||||
}
|
||||
self._storage.path = newValue
|
||||
}
|
||||
}
|
||||
|
||||
/// The underlying ":protocol" pseudo header field.
|
||||
public var extendedConnectProtocol: HTTPField? {
|
||||
willSet {
|
||||
get {
|
||||
self._storage.extendedConnectProtocol
|
||||
}
|
||||
set {
|
||||
if let name = newValue?.name {
|
||||
precondition(name == .protocol, "Cannot change pseudo-header field name")
|
||||
}
|
||||
|
||||
if !isKnownUniquelyReferenced(&self._storage) {
|
||||
self._storage = self._storage.copy()
|
||||
}
|
||||
self._storage.extendedConnectProtocol = newValue
|
||||
}
|
||||
}
|
||||
|
||||
init(
|
||||
method: HTTPField,
|
||||
scheme: HTTPField?,
|
||||
authority: HTTPField?,
|
||||
path: HTTPField?,
|
||||
extendedConnectProtocol: HTTPField? = nil
|
||||
) {
|
||||
self._storage = .init(
|
||||
method: method,
|
||||
scheme: scheme,
|
||||
authority: authority,
|
||||
path: path,
|
||||
extendedConnectProtocol: extendedConnectProtocol
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
/// The pseudo header fields.
|
||||
|
||||
@@ -162,32 +162,88 @@ public struct HTTPResponse: Sendable, Hashable {
|
||||
let code =
|
||||
Int(codeIterator.next()! - 48) * 100 + Int(codeIterator.next()! - 48) * 10
|
||||
+ Int(codeIterator.next()! - 48)
|
||||
return Status(uncheckedCode: code, reasonPhrase: self.reasonPhrase)
|
||||
return Status(uncheckedCode: code, reasonPhrase: self.pseudoHeaderFields.reasonPhrase)
|
||||
}
|
||||
set {
|
||||
self.pseudoHeaderFields.status.rawValue = ISOLatin1String(unchecked: newValue.fieldValue)
|
||||
self.reasonPhrase = newValue.reasonPhrase
|
||||
self.pseudoHeaderFields.reasonPhrase = newValue.reasonPhrase
|
||||
}
|
||||
}
|
||||
|
||||
/// The pseudo header fields of a response.
|
||||
public struct PseudoHeaderFields: Sendable, Hashable {
|
||||
private final class _Storage: @unchecked Sendable, Hashable {
|
||||
var status: HTTPField
|
||||
var reasonPhrase: String
|
||||
|
||||
init(status: HTTPField, reasonPhrase: String) {
|
||||
self.status = status
|
||||
self.reasonPhrase = reasonPhrase
|
||||
}
|
||||
|
||||
func copy() -> Self {
|
||||
.init(
|
||||
status: self.status,
|
||||
reasonPhrase: self.reasonPhrase
|
||||
)
|
||||
}
|
||||
|
||||
static func == (lhs: _Storage, rhs: _Storage) -> Bool {
|
||||
lhs.status == rhs.status
|
||||
}
|
||||
|
||||
func hash(into hasher: inout Hasher) {
|
||||
hasher.combine(self.status)
|
||||
}
|
||||
}
|
||||
|
||||
private var _storage: _Storage
|
||||
|
||||
/// The underlying ":status" pseudo header field.
|
||||
///
|
||||
/// The value of this field must be 3 ASCII decimal digits.
|
||||
public var status: HTTPField {
|
||||
willSet {
|
||||
get {
|
||||
self._storage.status
|
||||
}
|
||||
set {
|
||||
precondition(newValue.name == .status, "Cannot change pseudo-header field name")
|
||||
precondition(Status.isValidStatus(newValue.rawValue._storage), "Invalid status code")
|
||||
|
||||
if !isKnownUniquelyReferenced(&self._storage) {
|
||||
self._storage = self._storage.copy()
|
||||
}
|
||||
self._storage.status = newValue
|
||||
}
|
||||
}
|
||||
|
||||
var reasonPhrase: String {
|
||||
get {
|
||||
self._storage.reasonPhrase
|
||||
}
|
||||
set {
|
||||
if !isKnownUniquelyReferenced(&self._storage) {
|
||||
self._storage = self._storage.copy()
|
||||
}
|
||||
self._storage.reasonPhrase = newValue
|
||||
}
|
||||
}
|
||||
|
||||
private init(status: HTTPField) {
|
||||
self._storage = .init(status: status, reasonPhrase: "")
|
||||
}
|
||||
|
||||
init(status: Status) {
|
||||
self._storage = .init(
|
||||
status: HTTPField(name: .status, uncheckedValue: ISOLatin1String(unchecked: status.fieldValue)),
|
||||
reasonPhrase: status.reasonPhrase
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
/// The pseudo header fields.
|
||||
public var pseudoHeaderFields: PseudoHeaderFields
|
||||
|
||||
private var reasonPhrase: String
|
||||
|
||||
/// The response header fields.
|
||||
public var headerFields: HTTPFields
|
||||
|
||||
@@ -196,20 +252,9 @@ public struct HTTPResponse: Sendable, Hashable {
|
||||
/// - status: The status code and an optional reason phrase.
|
||||
/// - headerFields: The response header fields.
|
||||
public init(status: Status, headerFields: HTTPFields = [:]) {
|
||||
let statusField = HTTPField(name: .status, uncheckedValue: ISOLatin1String(unchecked: status.fieldValue))
|
||||
self.pseudoHeaderFields = .init(status: statusField)
|
||||
self.reasonPhrase = status.reasonPhrase
|
||||
self.pseudoHeaderFields = .init(status: status)
|
||||
self.headerFields = headerFields
|
||||
}
|
||||
|
||||
public func hash(into hasher: inout Hasher) {
|
||||
hasher.combine(self.pseudoHeaderFields)
|
||||
hasher.combine(self.headerFields)
|
||||
}
|
||||
|
||||
public static func == (lhs: HTTPResponse, rhs: HTTPResponse) -> Bool {
|
||||
lhs.pseudoHeaderFields == rhs.pseudoHeaderFields && lhs.headerFields == rhs.headerFields
|
||||
}
|
||||
}
|
||||
|
||||
extension HTTPResponse: CustomDebugStringConvertible {
|
||||
@@ -273,7 +318,7 @@ extension HTTPResponse: Codable {
|
||||
public func encode(to encoder: Encoder) throws {
|
||||
var container = encoder.container(keyedBy: CodingKeys.self)
|
||||
try container.encode(self.pseudoHeaderFields, forKey: .pseudoHeaderFields)
|
||||
try container.encode(self.reasonPhrase, forKey: .reasonPhrase)
|
||||
try container.encode(self.pseudoHeaderFields.reasonPhrase, forKey: .reasonPhrase)
|
||||
try container.encode(self.headerFields, forKey: .headerFields)
|
||||
}
|
||||
|
||||
@@ -288,7 +333,7 @@ extension HTTPResponse: Codable {
|
||||
guard Status.isValidReasonPhrase(reasonPhrase) else {
|
||||
throw DecodingError.invalidReasonPhrase(reasonPhrase)
|
||||
}
|
||||
self.reasonPhrase = reasonPhrase
|
||||
self.pseudoHeaderFields.reasonPhrase = reasonPhrase
|
||||
self.headerFields = try container.decode(HTTPFields.self, forKey: .headerFields)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -248,4 +248,10 @@ final class HTTPTypesTests: XCTestCase {
|
||||
XCTAssertEqual(trailerFields[HTTPField.Name("trailer1")!], "value1")
|
||||
XCTAssertEqual(trailerFields[HTTPField.Name("trailer2")!], "value2")
|
||||
}
|
||||
|
||||
func testTypeLayoutSize() {
|
||||
XCTAssertEqual(MemoryLayout<HTTPRequest>.size, MemoryLayout<AnyObject>.size * 2)
|
||||
XCTAssertEqual(MemoryLayout<HTTPResponse>.size, MemoryLayout<AnyObject>.size * 2)
|
||||
XCTAssertEqual(MemoryLayout<HTTPFields>.size, MemoryLayout<AnyObject>.size)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user