@@ -35,7 +35,6 @@ open class URLResponse : NSObject, NSSecureCoding, NSCopying, @unchecked Sendabl
3535 guard let nsurl = aDecoder. decodeObject ( of: NSURL . self, forKey: " NS.url " ) else { return nil }
3636 self . url = nsurl as URL
3737
38-
3938 if let mimetype = aDecoder. decodeObject ( of: NSString . self, forKey: " NS.mimeType " ) {
4039 self . mimeType = mimetype as String
4140 }
@@ -46,8 +45,11 @@ open class URLResponse : NSObject, NSSecureCoding, NSCopying, @unchecked Sendabl
4645 self . textEncodingName = encodedEncodingName as String
4746 }
4847
49- if let encodedFilename = aDecoder. decodeObject ( of: NSString . self, forKey: " NS.suggestedFilename " ) {
50- self . suggestedFilename = encodedFilename as String
48+ // re-sanitizing with lastPathComponent because of supportsSecureCoding
49+ if let encodedFilename = aDecoder. decodeObject ( of: NSString . self, forKey: " NS.suggestedFilename " ) ? . lastPathComponent, !encodedFilename. isEmpty {
50+ self . suggestedFilename = encodedFilename
51+ } else {
52+ self . suggestedFilename = " Unknown "
5153 }
5254 }
5355
@@ -177,6 +179,25 @@ open class URLResponse : NSObject, NSSecureCoding, NSCopying, @unchecked Sendabl
177179/// protocol responses.
178180open class HTTPURLResponse : URLResponse , @unchecked Sendable {
179181
182+ private static func sanitize( headerFields: [ String : String ] ? ) -> [ String : String ] {
183+ // Canonicalize the header fields by capitalizing the field names, but not X- Headers
184+ // This matches the behaviour of Darwin.
185+ guard let headerFields = headerFields else { return [ : ] }
186+ var canonicalizedFields : [ String : String ] = [ : ]
187+
188+ for (key, value) in headerFields {
189+ if key. isEmpty { continue }
190+ if key. hasPrefix ( " x- " ) || key. hasPrefix ( " X- " ) {
191+ canonicalizedFields [ key] = value
192+ } else if key. caseInsensitiveCompare ( " WWW-Authenticate " ) == . orderedSame {
193+ canonicalizedFields [ " WWW-Authenticate " ] = value
194+ } else {
195+ canonicalizedFields [ key. capitalized] = value
196+ }
197+ }
198+ return canonicalizedFields
199+ }
200+
180201 /// Initializer for HTTPURLResponse objects.
181202 ///
182203 /// - Parameter url: the URL from which the response was generated.
@@ -186,30 +207,13 @@ open class HTTPURLResponse : URLResponse, @unchecked Sendable {
186207 /// - Returns: the instance of the object, or `nil` if an error occurred during initialization.
187208 public init ? ( url: URL , statusCode: Int , httpVersion: String ? , headerFields: [ String : String ] ? ) {
188209 self . statusCode = statusCode
189-
190- self . _allHeaderFields = {
191- // Canonicalize the header fields by capitalizing the field names, but not X- Headers
192- // This matches the behaviour of Darwin.
193- guard let headerFields = headerFields else { return [ : ] }
194- var canonicalizedFields : [ String : String ] = [ : ]
195-
196- for (key, value) in headerFields {
197- if key. isEmpty { continue }
198- if key. hasPrefix ( " x- " ) || key. hasPrefix ( " X- " ) {
199- canonicalizedFields [ key] = value
200- } else if key. caseInsensitiveCompare ( " WWW-Authenticate " ) == . orderedSame {
201- canonicalizedFields [ " WWW-Authenticate " ] = value
202- } else {
203- canonicalizedFields [ key. capitalized] = value
204- }
205- }
206- return canonicalizedFields
207- } ( )
208-
210+
211+ self . _allHeaderFields = HTTPURLResponse . sanitize ( headerFields: headerFields)
212+
209213 super. init ( url: url, mimeType: nil , expectedContentLength: 0 , textEncodingName: nil )
210- expectedContentLength = getExpectedContentLength ( fromHeaderFields: headerFields ) ?? - 1
211- suggestedFilename = getSuggestedFilename ( fromHeaderFields: headerFields ) ?? " Unknown "
212- if let type = ContentTypeComponents ( headerFields: headerFields ) {
214+ expectedContentLength = getExpectedContentLength ( fromHeaderFields: _allHeaderFields ) ?? - 1
215+ suggestedFilename = getSuggestedFilename ( fromHeaderFields: _allHeaderFields ) ?? " Unknown "
216+ if let type = ContentTypeComponents ( headerFields: _allHeaderFields ) {
213217 mimeType = type. mimeType. lowercased ( )
214218 textEncodingName = type. textEncoding? . lowercased ( )
215219 }
@@ -222,13 +226,18 @@ open class HTTPURLResponse : URLResponse, @unchecked Sendable {
222226
223227 self . statusCode = aDecoder. decodeInteger ( forKey: " NS.statusCode " )
224228
225- if aDecoder. containsValue ( forKey: " NS.allHeaderFields " ) {
226- self . _allHeaderFields = aDecoder. decodeObject ( of: NSDictionary . self, forKey: " NS.allHeaderFields " ) as! [ String : String ]
227- } else {
228- self . _allHeaderFields = [ : ]
229- }
229+ // re-sanitizing dictionary because of supportsSecureCoding
230+ self . _allHeaderFields = HTTPURLResponse . sanitize ( headerFields: aDecoder. decodeObject ( of: NSDictionary . self, forKey: " NS.allHeaderFields " ) as? [ String : String ] )
230231
231232 super. init ( coder: aDecoder)
233+
234+ // re-sanitizing from _allHeaderFields because of supportsSecureCoding
235+ expectedContentLength = getExpectedContentLength ( fromHeaderFields: _allHeaderFields) ?? - 1
236+ suggestedFilename = getSuggestedFilename ( fromHeaderFields: _allHeaderFields) ?? " Unknown "
237+ if let type = ContentTypeComponents ( headerFields: _allHeaderFields) {
238+ mimeType = type. mimeType. lowercased ( )
239+ textEncodingName = type. textEncoding? . lowercased ( )
240+ }
232241 }
233242
234243 open override func encode( with aCoder: NSCoder ) {
0 commit comments