-
-
Notifications
You must be signed in to change notification settings - Fork 118
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
399 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1,70 @@ | ||
import HTTPTypes | ||
|
||
package extension HTTPFields { | ||
init(_ dictionary: [String: String]) { | ||
extension HTTPFields { | ||
package init(_ dictionary: [String: String]) { | ||
self.init(dictionary.map { .init(name: .init($0.key)!, value: $0.value) }) | ||
} | ||
var dictionary: [String: String] { | ||
|
||
package var dictionary: [String: String] { | ||
let keyValues = self.map { | ||
($0.name.rawName, $0.value) | ||
} | ||
|
||
return .init(keyValues, uniquingKeysWith: { $1 }) | ||
} | ||
mutating func merge(with other: Self) { | ||
|
||
package mutating func merge(with other: Self) { | ||
for field in other { | ||
self[field.name] = field.value | ||
} | ||
} | ||
func merging(with other: Self) -> Self { | ||
|
||
package func merging(with other: Self) -> Self { | ||
var copy = self | ||
|
||
for field in other { | ||
copy[field.name] = field.value | ||
} | ||
|
||
return copy | ||
} | ||
|
||
/// Append or update a value in header. | ||
/// | ||
/// Example: | ||
/// ```swift | ||
/// var headers: HTTPFields = [ | ||
/// "Prefer": "count=exact,return=representation" | ||
/// ] | ||
/// | ||
/// headers.appendOrUpdate(.prefer, value: "return=minimal") | ||
/// #expect(headers == ["Prefer": "count=exact,return=minimal"] | ||
/// ``` | ||
package mutating func appendOrUpdate( | ||
_ name: HTTPField.Name, | ||
value: String, | ||
separator: String = "," | ||
) { | ||
if let currentValue = self[name] { | ||
var components = currentValue.components(separatedBy: separator) | ||
|
||
if let key = value.split(separator: "=").first, | ||
let index = components.firstIndex(where: { $0.hasPrefix("\(key)=") }) | ||
{ | ||
components[index] = value | ||
} else { | ||
components.append(value) | ||
} | ||
|
||
self[name] = components.joined(separator: separator) | ||
} else { | ||
self[name] = value | ||
} | ||
} | ||
} | ||
|
||
package extension HTTPField.Name { | ||
static let xClientInfo = HTTPField.Name("X-Client-Info")! | ||
static let xRegion = HTTPField.Name("x-region")! | ||
static let xRelayError = HTTPField.Name("x-relay-error")! | ||
extension HTTPField.Name { | ||
package static let xClientInfo = HTTPField.Name("X-Client-Info")! | ||
package static let xRegion = HTTPField.Name("x-region")! | ||
package static let xRelayError = HTTPField.Name("x-relay-error")! | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.