|
15 | 15 | // Extensions which provide better ergonomics when using Foundation types, |
16 | 16 | // or by using Foundation APIs. |
17 | 17 |
|
| 18 | +#if canImport(FoundationEssentials) |
| 19 | +import FoundationEssentials |
| 20 | +#else |
18 | 21 | import Foundation |
| 22 | +#endif |
19 | 23 |
|
20 | 24 | extension HTTPClient.Cookie { |
21 | 25 | /// The cookie's expiration date. |
@@ -73,3 +77,66 @@ extension HTTPClient.Body { |
73 | 77 | self.bytes(data) |
74 | 78 | } |
75 | 79 | } |
| 80 | + |
| 81 | +extension StringProtocol { |
| 82 | + func addingPercentEncodingAllowingURLHost() -> String { |
| 83 | + guard !self.isEmpty else { return String(self) } |
| 84 | + |
| 85 | + let percent = UInt8(ascii: "%") |
| 86 | + let utf8Buffer = self.utf8 |
| 87 | + let maxLength = utf8Buffer.count * 3 |
| 88 | + return withUnsafeTemporaryAllocation(of: UInt8.self, capacity: maxLength) { outputBuffer in |
| 89 | + var i = 0 |
| 90 | + for byte in utf8Buffer { |
| 91 | + if byte.isURLHostAllowed { |
| 92 | + outputBuffer[i] = byte |
| 93 | + i += 1 |
| 94 | + } else { |
| 95 | + outputBuffer[i] = percent |
| 96 | + outputBuffer[i + 1] = hexToAscii(byte >> 4) |
| 97 | + outputBuffer[i + 2] = hexToAscii(byte & 0xF) |
| 98 | + i += 3 |
| 99 | + } |
| 100 | + } |
| 101 | + return String(decoding: outputBuffer[..<i], as: UTF8.self) |
| 102 | + } |
| 103 | + } |
| 104 | +} |
| 105 | + |
| 106 | +private func hexToAscii(_ hex: UInt8) -> UInt8 { |
| 107 | + switch hex { |
| 108 | + case 0x0: return UInt8(ascii: "0") |
| 109 | + case 0x1: return UInt8(ascii: "1") |
| 110 | + case 0x2: return UInt8(ascii: "2") |
| 111 | + case 0x3: return UInt8(ascii: "3") |
| 112 | + case 0x4: return UInt8(ascii: "4") |
| 113 | + case 0x5: return UInt8(ascii: "5") |
| 114 | + case 0x6: return UInt8(ascii: "6") |
| 115 | + case 0x7: return UInt8(ascii: "7") |
| 116 | + case 0x8: return UInt8(ascii: "8") |
| 117 | + case 0x9: return UInt8(ascii: "9") |
| 118 | + case 0xA: return UInt8(ascii: "A") |
| 119 | + case 0xB: return UInt8(ascii: "B") |
| 120 | + case 0xC: return UInt8(ascii: "C") |
| 121 | + case 0xD: return UInt8(ascii: "D") |
| 122 | + case 0xE: return UInt8(ascii: "E") |
| 123 | + case 0xF: return UInt8(ascii: "F") |
| 124 | + default: fatalError("Invalid hex digit: \(hex)") |
| 125 | + } |
| 126 | +} |
| 127 | + |
| 128 | +extension UInt8 { |
| 129 | + fileprivate var isURLHostAllowed: Bool { |
| 130 | + switch self { |
| 131 | + case UInt8(ascii: "0")...UInt8(ascii: "9"), |
| 132 | + UInt8(ascii: "A")...UInt8(ascii: "Z"), |
| 133 | + UInt8(ascii: "a")...UInt8(ascii: "z"), |
| 134 | + UInt8(ascii: "!"), UInt8(ascii: "$"), UInt8(ascii: "&"), UInt8(ascii: "'"), |
| 135 | + UInt8(ascii: "("), UInt8(ascii: ")"), UInt8(ascii: "*"), UInt8(ascii: "+"), |
| 136 | + UInt8(ascii: ","), UInt8(ascii: "-"), UInt8(ascii: "."), UInt8(ascii: ";"), |
| 137 | + UInt8(ascii: "="), UInt8(ascii: "_"), UInt8(ascii: "~"): |
| 138 | + return true |
| 139 | + default: return false |
| 140 | + } |
| 141 | + } |
| 142 | +} |
0 commit comments