Skip to content

Commit d7f5543

Browse files
committed
Fix errors on non-Darwin platforms
HTTPURLResponse is part of FoundationNetworking instead of Foundation on non-Darwin platforms. HTTPURLResponse.init is not accessible in FoundationNetworking, so init(url:statusCode:httpVersion:headerfields:) must be used to test for a missing date header. HTTPURLResponse.value(forHTTPHeaderField:) is not present in the current release of FoundationNetworking, and allHeaderFields uses keys with different capitalization than the Foundation version. Fixes for both have been implemented in Swift 5.3. Extraneous whitespace has been removed.
1 parent 3ff1139 commit d7f5543

File tree

2 files changed

+33
-6
lines changed

2 files changed

+33
-6
lines changed
Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,21 @@
11
import Foundation
22

3+
#if canImport(FoundationNetworking)
4+
import class FoundationNetworking.HTTPURLResponse
5+
#endif
6+
37
public extension HTTPURLResponse {
48
private static let dateFormatters = [
59
// GMT is required in all timestamps, but section 19.3 of RFC 2616 requires clients to convert timestamps incorrectly given with a different time zone into GMT.
610

711
// RFC 1123
812
"EEE, dd MMM yyyy HH:mm:ss z",
9-
13+
1014
// RFC 850
1115
"EEEE, dd-MMM-yy HH:mm:ss z",
12-
16+
1317
// ANSI C's asctime() format
14-
"EEE MMM d HH:mm:ss yyyy"
18+
"EEE MMM d HH:mm:ss yyyy",
1519
]
1620
.lazy // It's very unlikely that any DateFormatter beyond the first will be necessary.
1721
.map { formatString -> DateFormatter in
@@ -21,27 +25,33 @@ public extension HTTPURLResponse {
2125
formatter.dateFormat = formatString
2226
return formatter
2327
}
24-
28+
2529
/// The date that the response was sent.
2630
/// - Note: This is parsed from the `date` header of the response according to [RFC 2616](https://tools.ietf.org/html/rfc2616#section-3.3.1).
2731
var date: Date? {
2832
// Servers are required to send a date header whenever possible. It isn't always possible.
2933
let dateValue: String?
34+
35+
#if canImport(FoundationNetworking) && swift(<5.3) // See https://bugs.swift.org/browse/SR-12300
36+
dateValue = allHeaderFields["date"] as? String
37+
#else
3038
if #available(iOS 13, macCatalyst 13, OSX 10.15, tvOS 13, watchOS 6, *) {
3139
dateValue = value(forHTTPHeaderField: "date")
3240
} else {
3341
dateValue = allHeaderFields["Date"] as? String
3442
}
43+
#endif
44+
3545
guard let dateString = dateValue else {
3646
return nil
3747
}
38-
48+
3949
for dateFormatter in Self.dateFormatters {
4050
if let date = dateFormatter.date(from: dateString) {
4151
return date
4252
}
4353
}
44-
54+
4555
return nil
4656
}
4757
}

Tests/HTTPResponseDateTests/HTTPResponseDateTests.swift

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@ import Foundation
22
import HTTPResponseDate
33
import XCTest
44

5+
#if canImport(FoundationNetworking)
6+
import class FoundationNetworking.HTTPURLResponse
7+
#endif
8+
59
final class HTTPResponseDateTests: XCTestCase {
610
/// A set of dates along with an `HTTPURLResponse` for each supported `date` header representation of them.
711
///
@@ -153,7 +157,20 @@ final class HTTPResponseDateTests: XCTestCase {
153157
}
154158

155159
func testMalformedTimestampParsing() {
160+
#if canImport(FoundationNetworking) // HTTPURLResponse.init isn't present in FoundationNetworking
161+
XCTAssertNil(
162+
HTTPURLResponse(
163+
url: URL(string: "example.com")!,
164+
statusCode: 0,
165+
httpVersion: nil,
166+
headerFields: nil
167+
)!
168+
.date
169+
)
170+
#else
156171
XCTAssertNil(HTTPURLResponse().date)
172+
#endif
173+
157174
XCTAssertNil(
158175
HTTPURLResponse(
159176
url: URL(string: "example.com")!,

0 commit comments

Comments
 (0)