20 lines
804 B
Swift
20 lines
804 B
Swift
import Foundation
|
||
|
||
extension HTTPURLResponse {
|
||
/// 大小写不敏感地读取 HTTP 响应头。
|
||
///
|
||
/// HTTP/2(RFC 7540 §8.1.2)强制把头字段名小写(如 `content-length`),而
|
||
/// `allHeaderFields` 的字典下标是大小写敏感精确匹配,直接用 `["Content-Length"]`
|
||
/// 在 HTTP/2 响应下会取不到值。系统的 `value(forHTTPHeaderField:)` 能规避,但要求
|
||
/// iOS 13+,本 SDK 最低支持 iOS 12,故手动遍历比较。
|
||
func headerValue(caseInsensitive name: String) -> String? {
|
||
for (key, value) in allHeaderFields {
|
||
if let keyStr = key as? String,
|
||
keyStr.caseInsensitiveCompare(name) == .orderedSame {
|
||
return value as? String
|
||
}
|
||
}
|
||
return nil
|
||
}
|
||
}
|