| OLD | NEW | 
|---|
| 1 // | 1 // | 
| 2 // FavIcon | 2 // FavIcon | 
| 3 // Copyright © 2016 Leon Breedt | 3 // Copyright © 2016 Leon Breedt | 
| 4 // | 4 // | 
| 5 // Licensed under the Apache License, Version 2.0 (the "License"); | 5 // Licensed under the Apache License, Version 2.0 (the "License"); | 
| 6 // you may not use this file except in compliance with the License. | 6 // you may not use this file except in compliance with the License. | 
| 7 // You may obtain a copy of the License at | 7 // You may obtain a copy of the License at | 
| 8 // | 8 // | 
| 9 // http://www.apache.org/licenses/LICENSE-2.0 | 9 // http://www.apache.org/licenses/LICENSE-2.0 | 
| 10 // | 10 // | 
| 11 // Unless required by applicable law or agreed to in writing, software | 11 // Unless required by applicable law or agreed to in writing, software | 
| 12 // distributed under the License is distributed on an "AS IS" BASIS, | 12 // distributed under the License is distributed on an "AS IS" BASIS, | 
| 13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
| 14 // See the License for the specific language governing permissions and | 14 // See the License for the specific language governing permissions and | 
| 15 // limitations under the License. | 15 // limitations under the License. | 
| 16 // | 16 // | 
| 17 | 17 | 
| 18 extension String { | 18 extension String { | 
| 19     /// Parses this string as an HTTP Content-Type header. | 19     /// Parses this string as an HTTP Content-Type header. | 
| 20     /// - returns: A tuple containing the mime type and string, if this could be
      determined. | 20     /// - returns: A tuple containing the mime type and string, if this could be
      determined. | 
| 21     func parseAsHTTPContentTypeHeader() -> (mimeType: String, encoding: String.E
     ncoding) { | 21     func parseAsHTTPContentTypeHeader() -> (mimeType: String, encoding: String.E
     ncoding) { | 
| 22         let headerComponents = | 22         let headerComponents = | 
| 23             components(separatedBy: ";") | 23             components(separatedBy: ";") | 
| 24                 .map { $0.trimmingCharacters(in: .whitespaces) } | 24             .map { $0.trimmingCharacters(in: .whitespaces) } | 
| 25 | 25 | 
| 26         if headerComponents.count > 1 { | 26         if headerComponents.count > 1 { | 
| 27             let parameters = | 27             let parameters = | 
| 28                 headerComponents[1..<headerComponents.count] | 28                 headerComponents[1..<headerComponents.count] | 
| 29                     .filter { $0.contains("=") } | 29                 .filter { $0.contains("=") } | 
| 30                     .map { $0.components(separatedBy: "=") } | 30                 .map { $0.components(separatedBy: "=") } | 
| 31                     .toDictionary { ($0[0], $0[1]) } | 31                 .toDictionary { ($0[0], $0[1]) } | 
| 32 | 32 | 
| 33             // Default according to RFC is ISO-8859-1, but probably nothing obey
     s that, so default | 33             // Default according to RFC is ISO-8859-1, but probably nothing obey
     s that, so default | 
| 34             // to UTF-8 instead. | 34             // to UTF-8 instead. | 
| 35             var encoding = String.Encoding.utf8 | 35             var encoding = String.Encoding.utf8 | 
| 36             if let charset = parameters["charset"], let parsedEncoding = charset
     .parseAsStringEncoding() { | 36             if let charset = parameters["charset"], let parsedEncoding = charset
     .parseAsStringEncoding() { | 
| 37                 encoding = parsedEncoding | 37                 encoding = parsedEncoding | 
| 38             } | 38             } | 
| 39 | 39 | 
| 40             return (mimeType: headerComponents[0], encoding: encoding) | 40             return (mimeType: headerComponents[0], encoding: encoding) | 
| 41         } else { | 41         } else { | 
| 42             return (mimeType: headerComponents[0], encoding: String.Encoding.utf
     8) | 42             return (mimeType: headerComponents[0], encoding: String.Encoding.utf
     8) | 
| 43         } | 43         } | 
| 44     } | 44     } | 
| 45 | 45 | 
| 46     /// - Returns: The Cocoa encoding identifier for the encoding name in this s
     tring, or `nil` | 46     /// - Returns: The Cocoa encoding identifier for the encoding name in this s
     tring, or `nil` | 
| 47     ///            if the encoding is not supported or known. | 47     ///            if the encoding is not supported or known. | 
| 48     //swiftlint:disable cyclomatic_complexity | 48     //swiftlint:disable cyclomatic_complexity | 
| 49     func parseAsStringEncoding() -> String.Encoding? { | 49     func parseAsStringEncoding() -> String.Encoding? { | 
| 50         switch lowercased() { | 50         switch lowercased() { | 
| 51         case "iso-8859-1", "latin1": return String.Encoding.isoLatin1 | 51         case "iso-8859-1", "latin1": | 
| 52         case "iso-8859-2", "latin2": return String.Encoding.isoLatin2 | 52             return String.Encoding.isoLatin1 | 
| 53         case "iso-2022-jp": return String.Encoding.iso2022JP | 53         case "iso-8859-2", "latin2": | 
| 54         case "shift_jis": return String.Encoding.shiftJIS | 54             return String.Encoding.isoLatin2 | 
| 55         case "us-ascii": return String.Encoding.ascii | 55         case "iso-2022-jp": | 
| 56         case "utf-8": return String.Encoding.utf8 | 56             return String.Encoding.iso2022JP | 
| 57         case "utf-16": return String.Encoding.utf16 | 57         case "shift_jis": | 
| 58         case "utf-32": return String.Encoding.utf32 | 58             return String.Encoding.shiftJIS | 
| 59         case "utf-32be": return String.Encoding.utf32BigEndian | 59         case "us-ascii": | 
| 60         case "utf-32le": return String.Encoding.utf32LittleEndian | 60             return String.Encoding.ascii | 
| 61         case "windows-1250": return String.Encoding.windowsCP1250 | 61         case "utf-8": | 
| 62         case "windows-1251": return String.Encoding.windowsCP1251 | 62             return String.Encoding.utf8 | 
| 63         case "windows-1252": return String.Encoding.windowsCP1252 | 63         case "utf-16": | 
| 64         case "windows-1253": return String.Encoding.windowsCP1253 | 64             return String.Encoding.utf16 | 
| 65         case "windows-1254": return String.Encoding.windowsCP1254 | 65         case "utf-32": | 
| 66         case "x-mac-roman": return String.Encoding.macOSRoman | 66             return String.Encoding.utf32 | 
|  | 67         case "utf-32be": | 
|  | 68             return String.Encoding.utf32BigEndian | 
|  | 69         case "utf-32le": | 
|  | 70             return String.Encoding.utf32LittleEndian | 
|  | 71         case "windows-1250": | 
|  | 72             return String.Encoding.windowsCP1250 | 
|  | 73         case "windows-1251": | 
|  | 74             return String.Encoding.windowsCP1251 | 
|  | 75         case "windows-1252": | 
|  | 76             return String.Encoding.windowsCP1252 | 
|  | 77         case "windows-1253": | 
|  | 78             return String.Encoding.windowsCP1253 | 
|  | 79         case "windows-1254": | 
|  | 80             return String.Encoding.windowsCP1254 | 
|  | 81         case "x-mac-roman": | 
|  | 82             return String.Encoding.macOSRoman | 
| 67         default: | 83         default: | 
| 68             return nil | 84             return nil | 
| 69         } | 85         } | 
| 70     } | 86     } | 
| 71     //swiftlint:enable cyclomatic_complexity | 87     //swiftlint:enable cyclomatic_complexity | 
| 72 } | 88 } | 
| 73 | 89 | 
| 74 extension HTTPURLResponse { | 90 extension HTTPURLResponse { | 
| 75     /// Parses the `Content-Type` header in this response. | 91     /// Parses the `Content-Type` header in this response. | 
| 76     /// - returns: A `(mimeType: String, encoding: String.Encoding)` tuple. | 92     /// - returns: A `(mimeType: String, encoding: String.Encoding)` tuple. | 
| (...skipping 13 matching lines...) Expand all  Loading... | 
| 90     /// - returns: A dictionary having items of type `K` as keys, and type `V` a
     s values. | 106     /// - returns: A dictionary having items of type `K` as keys, and type `V` a
     s values. | 
| 91     func toDictionary<K, V>(_ transform: (Element) -> (K, V)) -> [K: V] { | 107     func toDictionary<K, V>(_ transform: (Element) -> (K, V)) -> [K: V] { | 
| 92         var dict: [K: V] = [:] | 108         var dict: [K: V] = [:] | 
| 93         for item in self { | 109         for item in self { | 
| 94             let (key, value) = transform(item) | 110             let (key, value) = transform(item) | 
| 95             dict[key] = value | 111             dict[key] = value | 
| 96         } | 112         } | 
| 97         return dict | 113         return dict | 
| 98     } | 114     } | 
| 99 } | 115 } | 
| OLD | NEW | 
|---|