|
| 1 | +// |
| 2 | +// HTTPHeaders.swift |
| 3 | +// FlyingFox |
| 4 | +// |
| 5 | +// Created by Simon Whitty on 13/09/2025. |
| 6 | +// Copyright © 2025 Simon Whitty. All rights reserved. |
| 7 | +// |
| 8 | +// Distributed under the permissive MIT license |
| 9 | +// Get the latest version from here: |
| 10 | +// |
| 11 | +// https://github.com/swhitty/FlyingFox |
| 12 | +// |
| 13 | +// Permission is hereby granted, free of charge, to any person obtaining a copy |
| 14 | +// of this software and associated documentation files (the "Software"), to deal |
| 15 | +// in the Software without restriction, including without limitation the rights |
| 16 | +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 17 | +// copies of the Software, and to permit persons to whom the Software is |
| 18 | +// furnished to do so, subject to the following conditions: |
| 19 | +// |
| 20 | +// The above copyright notice and this permission notice shall be included in all |
| 21 | +// copies or substantial portions of the Software. |
| 22 | +// |
| 23 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 24 | +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 25 | +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 26 | +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 27 | +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 28 | +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 29 | +// SOFTWARE. |
| 30 | +// |
| 31 | + |
| 32 | +public struct HTTPHeaders: Hashable, Sendable, Sequence, ExpressibleByDictionaryLiteral { |
| 33 | + var storage: [HTTPHeader: [String]] = [:] |
| 34 | + |
| 35 | + public init() { } |
| 36 | + |
| 37 | + public init(_ headers: [HTTPHeader: String]) { |
| 38 | + self.storage = headers.mapValues { [$0] } |
| 39 | + } |
| 40 | + |
| 41 | + public init(dictionaryLiteral elements: (HTTPHeader, String)...) { |
| 42 | + for (header, value) in elements { |
| 43 | + if HTTPHeaders.canCombineValues(for: header) { |
| 44 | + let values = value |
| 45 | + .split(separator: ",", omittingEmptySubsequences: true) |
| 46 | + .map { String($0.trimmingCharacters(in: .whitespaces)) } |
| 47 | + storage[header, default: []].append(contentsOf: values) |
| 48 | + } else { |
| 49 | + storage[header, default: []].append(value) |
| 50 | + } |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + public subscript(header: HTTPHeader) -> String? { |
| 55 | + get { |
| 56 | + guard let values = storage[header] else { return nil } |
| 57 | + if HTTPHeaders.canCombineValues(for: header) { |
| 58 | + return values.joined(separator: ", ") |
| 59 | + } else { |
| 60 | + return values.first |
| 61 | + } |
| 62 | + } |
| 63 | + set { |
| 64 | + if let newValue { |
| 65 | + if storage[header] != nil { |
| 66 | + storage[header]?[0] = newValue |
| 67 | + } else { |
| 68 | + storage[header] = [newValue] |
| 69 | + } |
| 70 | + } else { |
| 71 | + storage.removeValue(forKey: header) |
| 72 | + } |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + public var keys: some Collection<HTTPHeader> { |
| 77 | + storage.keys |
| 78 | + } |
| 79 | + |
| 80 | + public var values: some Collection<[String]> { |
| 81 | + storage.values |
| 82 | + } |
| 83 | + |
| 84 | + public func values(for header: HTTPHeader) -> [String] { |
| 85 | + storage[header] ?? [] |
| 86 | + } |
| 87 | + |
| 88 | + public mutating func addValue(_ value: String, for header: HTTPHeader) { |
| 89 | + storage[header, default: []].append(value) |
| 90 | + } |
| 91 | + |
| 92 | + public mutating func setValues(_ values: [String], for header: HTTPHeader) { |
| 93 | + storage[header] = values |
| 94 | + } |
| 95 | + |
| 96 | + public mutating func removeValue(_ header: HTTPHeader) { |
| 97 | + storage.removeValue(forKey: header) |
| 98 | + } |
| 99 | + |
| 100 | + public func makeIterator() -> some IteratorProtocol<(key: HTTPHeader, value: String)> { |
| 101 | + storage.lazy |
| 102 | + .flatMap { (key, values) in values.lazy.map { (key, $0) } } |
| 103 | + .makeIterator() |
| 104 | + } |
| 105 | +} |
| 106 | + |
| 107 | +package extension HTTPHeaders { |
| 108 | + |
| 109 | + private static let singleValueHeaders: Set<HTTPHeader> = [ |
| 110 | + .cookie, .setCookie, .date, .eTag, .contentLength, .contentType, .authorization, .host |
| 111 | + ] |
| 112 | + |
| 113 | + static func canCombineValues(for header: HTTPHeader) -> Bool { |
| 114 | + !singleValueHeaders.contains(header) |
| 115 | + } |
| 116 | +} |
0 commit comments