OSDN Git Service

ColorSetを変更
[kcd/KCD.git] / KCD / CacheStoragePolicy.swift
1 //
2 //  CacheStoragePolicy.swift
3 //  KCD
4 //
5 //  Created by Hori,Masaki on 2017/02/11.
6 //  Copyright © 2017年 Hori,Masaki. All rights reserved.
7 //
8
9 import Foundation
10
11 private func cacheable(status: Int) -> Bool {
12     
13     switch status {
14         
15     case 200, 203, 206,
16          301, 304,
17          404, 410:
18         
19         return true
20         
21     default:
22         
23         return false
24         
25     }
26 }
27
28 private func cacheable(response: HTTPURLResponse) -> Bool {
29     
30     guard let cc = response.allHeaderFields["Cache-Control"] as? String,
31         let _ = cc.lowercased().range(of: "no-store") else {
32             
33             return true
34     }
35     
36     return false
37 }
38
39 private func cacheable(request: URLRequest) -> Bool {
40     
41     guard let cc: String = request.allHTTPHeaderFields?["Cache-Control"] else {
42         
43         return true
44     }
45     
46     if let _ = cc.lowercased().range(of: "no-store") {
47         
48         return false
49     }
50     
51     if let _ = cc.lowercased().range(of: "no-cache") {
52         
53         return false
54     }
55     
56     return true
57 }
58
59 private func policy(request: URLRequest) -> URLCache.StoragePolicy {
60     
61     if let scheme = request.url?.scheme?.lowercased(),
62         scheme == "https" {
63         
64         return .allowedInMemoryOnly
65     }
66     
67     return .allowed
68 }
69
70 func cacheStoragePolicy(for request: URLRequest, response: HTTPURLResponse) -> URLCache.StoragePolicy {
71     
72     if cacheable(status: response.statusCode),
73         cacheable(response: response),
74         cacheable(request: request) {
75         
76         return policy(request: request)
77     }
78     
79     return .notAllowed
80 }