feat(sdk): 添加大小写不敏感的 HTTP 响应头读取方法
This commit is contained in:
19
Sources/DuooomiBleSDK/Utils/HTTPURLResponse+Header.swift
Normal file
19
Sources/DuooomiBleSDK/Utils/HTTPURLResponse+Header.swift
Normal file
@@ -0,0 +1,19 @@
|
||||
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
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user