Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support three different formats #2722

Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 29 additions & 8 deletions Sources/FoundationNetworking/URLSession/HTTP/HTTPURLProtocol.swift
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ internal class _HTTPURLProtocol: _NativeProtocol {
let expirationStart: Date

if let dateString = httpResponse.allHeaderFields["Date"] as? String,
let date = _HTTPURLProtocol.dateFormatter.date(from: dateString) {
let date = _HTTPURLProtocol.date(from: dateString) {
expirationStart = min(date, response.date) // Do not accept a date in the future of the point where we stored it, or of now if we haven't stored it yet. That is: a Date header can only make a response expire _faster_ than if it was issued now, and can't be used to prolong its age.
} else {
expirationStart = response.date
Expand Down Expand Up @@ -221,7 +221,7 @@ internal class _HTTPURLProtocol: _NativeProtocol {
// We should not cache a response that has already expired. (This is also the expiration check for canRespondFromCaching(using:) below.)
// We MUST ignore this if we have Cache-Control: max-age or s-maxage.
if !hasMaxAge, let expires = httpResponse.allHeaderFields["Expires"] as? String {
guard let expiration = _HTTPURLProtocol.dateFormatter.date(from: expires) else {
guard let expiration = _HTTPURLProtocol.date(from: expires) else {
// From the spec:
/* "A cache recipient MUST interpret invalid date formats, especially the
value "0", as representing a time in the past (i.e., 'already
Expand All @@ -239,13 +239,34 @@ internal class _HTTPURLProtocol: _NativeProtocol {
return true
}

static let dateFormatter: DateFormatter = {
let x = DateFormatter()
x.locale = NSLocale.system
x.dateFormat = "EEE',' dd' 'MMM' 'yyyy HH':'mm':'ss zzz"
return x
static let dateFormatters: [DateFormatter] = {
// https://tools.ietf.org/html/rfc2616#section-3.3.1
// HTTP applications have historically allowed three different formats
// for the representation of date/time stamps

// RCF 822 --- Sun, 06 Nov 1994 08:49:37 GMT
let d1 = DateFormatter()
d1.dateFormat = "EEE, dd MMM yyyy HH:mm:ss zzz"

// RCF 855 --- Sunday, 06-Nov-94 08:49:37 GMT
let d2 = DateFormatter()
d2.dateFormat = "EEEE, dd-MMM-yy HH:mm:ss zzz"

// ANSI C's asctime() format --- Sun Nov 6 08:49:37 1994
let d3 = DateFormatter()
d3.dateFormat = "EEE MMM dd HH:mm:ss yy"
return [d1, d2, d3]
}()

static func date(from string: String) -> Date? {
for dateFormat in self.dateFormatters {
if let d = dateFormat.date(from: string) {
return d
}
}
return nil
}

override func canRespondFromCache(using response: CachedURLResponse) -> Bool {
// If somehow cached a response that shouldn't have been, we should remove it.
guard canCache(response) else {
Expand Down Expand Up @@ -461,7 +482,7 @@ fileprivate extension _HTTPURLProtocol {
func curlHeaders(for httpHeaders: [AnyHashable : Any]?) -> [String] {
var result: [String] = []
var names = Set<String>()
if let hh = httpHeaders as? [String : String] {
if let hh = httpHeaders as? [String : String] {
hh.forEach {
let name = $0.0.lowercased()
guard !names.contains(name) else { return }
Expand Down