/
CacheRelatedHeadersSetter.swift
85 lines (72 loc) · 3.29 KB
/
CacheRelatedHeadersSetter.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
/*
* Copyright IBM Corporation 2016
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import KituraNet
import Foundation
extension StaticFileServer {
class CacheRelatedHeadersSetter: ResponseHeadersSetter {
/// Uses the file system's last modified value.
private let addLastModifiedHeader: Bool
/// Value of max-age in Cache-Control header.
private let maxAgeCacheControlHeader: Int
/// Generate ETag.
private var generateETag: Bool
init(addLastModifiedHeader: Bool, maxAgeCacheControlHeader: Int, generateETag: Bool) {
self.addLastModifiedHeader = addLastModifiedHeader
self.maxAgeCacheControlHeader = maxAgeCacheControlHeader
self.generateETag = generateETag
}
func setCustomResponseHeaders(response: RouterResponse, filePath _: String,
fileAttributes: [FileAttributeKey : Any]) {
addLastModified(response: response, fileAttributes: fileAttributes)
addETag(response: response, fileAttributes: fileAttributes)
setMaxAge(response: response)
}
private func addLastModified(response: RouterResponse,
fileAttributes: [FileAttributeKey : Any]) {
if addLastModifiedHeader {
let date = fileAttributes[FileAttributeKey.modificationDate] as? Date
if let date = date {
response.headers["Last-Modified"] = SPIUtils.httpDate(date)
}
}
}
private func addETag(response: RouterResponse,
fileAttributes: [FileAttributeKey : Any]) {
if generateETag,
let etag = CacheRelatedHeadersSetter.calculateETag(from: fileAttributes) {
response.headers["Etag"] = etag
}
}
private func setMaxAge(response: RouterResponse) {
response.headers["Cache-Control"] = "max-age=\(maxAgeCacheControlHeader)"
}
static func calculateETag(from fileAttributes: [FileAttributeKey : Any]) -> String? {
guard let date = fileAttributes[FileAttributeKey.modificationDate] as? Date,
let size = fileAttributes[FileAttributeKey.size] as? NSNumber else {
return nil
}
#if !os(Linux) || swift(>=4.0.2)
// https://bugs.swift.org/browse/SR-5850
let sizeHex = String(Int(truncating: size), radix: 16, uppercase: false)
#else
let sizeHex = String(Int(size), radix: 16, uppercase: false)
#endif
let timeHex = String(Int(date.timeIntervalSince1970), radix: 16, uppercase: false)
let etag = "W/\"\(sizeHex)-\(timeHex)\""
return etag
}
}
}