OSDN Git Service

コメントを追加
[kcd/KCD.git] / KCD / CustomHTTPProtocol.swift
1 //
2 //  CustomHTTPProtocol.swift
3 //  KCD
4 //
5 //  Created by Hori,Masaki on 2017/02/10.
6 //  Copyright © 2017年 Hori,Masaki. All rights reserved.
7 //
8
9 import Cocoa
10
11
12 protocol CustomHTTPProtocolDelegate: class {
13     
14     func customHTTPProtocol(_ proto: CustomHTTPProtocol, didRecieve response: URLResponse)
15     func customHTTPProtocol(_ proto: CustomHTTPProtocol, didRecieve data: Data)
16     func customHTTPProtocolDidFinishLoading(_ proto: CustomHTTPProtocol)
17     func customHTTPProtocol(_ proto: CustomHTTPProtocol, didFailWithError error: Error)
18 }
19
20 private final class ThreadOperator: NSObject {
21     
22     private let thread: Thread
23     private let modes: [String]
24     private var operation: (() -> Void)?
25     
26     override init() {
27         
28         thread = Thread.current
29         let mode = RunLoop.current.currentMode ?? .defaultRunLoopMode
30         
31         if mode == .defaultRunLoopMode {
32             
33             modes = [mode.rawValue]
34             
35         } else {
36             
37             modes = [mode, .defaultRunLoopMode].map { $0.rawValue }
38         }
39         
40         super.init()
41     }
42     
43     func execute(_ operation: @escaping () -> Void) {
44         
45         self.operation = operation
46         perform(#selector(ThreadOperator.operate),
47                 on: thread,
48                 with: nil,
49                 waitUntilDone: true,
50                 modes: modes)
51         self.operation = nil
52     }
53     
54     @objc func operate() {
55         
56         operation?()
57     }
58 }
59
60 extension HTTPURLResponse {
61     
62     private var httpDateFormatter: DateFormatter {
63         
64         let formatter = DateFormatter()
65         formatter.dateFormat = "EEE',' dd' 'MMM' 'yyyy HH':'mm':'ss zzz"
66         formatter.locale = Locale(identifier: "en_US")
67         
68         return formatter
69     }
70     
71     func expires() -> Date? {
72         
73         if let cc = (allHeaderFields["Cache-Control"] as? String)?.lowercased(),
74             let range = cc.range(of: "max-age="),
75             let s = cc[range.upperBound...]
76                 .components(separatedBy: ",")
77                 .first,
78             let age = TimeInterval(s) {
79             
80             return Date(timeIntervalSinceNow: age)
81         }
82         
83         if let ex = (allHeaderFields["Expires"] as? String)?.lowercased(),
84             let exp = httpDateFormatter.date(from: ex) {
85             
86             return exp
87         }
88         
89         return nil
90     }
91 }
92
93 extension URLCache {
94     
95     static let kcd = URLCache(memoryCapacity: 32 * 1024 * 1024,
96                               diskCapacity: 1024 * 1024 * 1024,
97                               diskPath: ApplicationDirecrories.support.appendingPathComponent("Caches").path)
98     static let cachedExtensions = ["swf", "flv", "png", "jpg", "jpeg", "mp3"]
99     
100     func storeIfNeeded(for task: URLSessionTask, data: Data) {
101         
102         if let request = task.originalRequest,
103             let response = task.response as? HTTPURLResponse,
104             let ext = request.url?.pathExtension,
105             URLCache.cachedExtensions.contains(ext),
106             let expires = response.expires() {
107             
108             let cache = CachedURLResponse(response: response,
109                                           data: data,
110                                           userInfo: ["Expires": expires],
111                                           storagePolicy: .allowed)
112             storeCachedResponse(cache, for: request)
113         }
114     }
115     
116     func validCach(for request: URLRequest) -> CachedURLResponse? {
117         
118         if let cache = cachedResponse(for: request),
119             let info = cache.userInfo,
120             let expires = info["Expires"] as? Date,
121             Date().compare(expires) == .orderedAscending {
122             
123                 return cache
124         }
125         
126         return nil
127     }
128 }
129
130 final class CustomHTTPProtocol: URLProtocol {
131     
132     private static let requestProperty = "com.masakih.KCD.requestProperty"
133     static var classDelegate: CustomHTTPProtocolDelegate?
134     
135     class func clearCache() { URLCache.kcd.removeAllCachedResponses() }
136     class func start() { URLProtocol.registerClass(CustomHTTPProtocol.self) }
137     
138     override class func canInit(with request: URLRequest) -> Bool {
139         
140         if let _ = property(forKey: requestProperty, in: request) { return false }
141         
142         if let scheme = request.url?.scheme?.lowercased(),
143             (scheme == "http" || scheme == "https") {
144             
145             return true
146         }
147         
148         return false
149     }
150     
151     override class func canonicalRequest(for request: URLRequest) -> URLRequest {
152         
153         return request
154     }
155     
156     private var delegate: CustomHTTPProtocolDelegate? { return CustomHTTPProtocol.classDelegate }
157     
158     private var session: URLSession?
159     private var dataTask: URLSessionDataTask?
160     private var cachePolicy: URLCache.StoragePolicy = .notAllowed
161     private var data: Data = Data()
162     private var didRetry: Bool = false
163     private var didRecieveData: Bool = false
164     
165     private var threadOperator: ThreadOperator?
166     
167     private func use(_ cache: CachedURLResponse) {
168         
169         delegate?.customHTTPProtocol(self, didRecieve: cache.response)
170         client?.urlProtocol(self, didReceive: cache.response, cacheStoragePolicy: .allowed)
171         
172         delegate?.customHTTPProtocol(self, didRecieve: cache.data)
173         client?.urlProtocol(self, didLoad: cache.data)
174         
175         delegate?.customHTTPProtocolDidFinishLoading(self)
176         client?.urlProtocolDidFinishLoading(self)
177     }
178     
179     override func startLoading() {
180         
181         guard let newRequest = (request as NSObject).mutableCopy() as? NSMutableURLRequest else {
182             
183             fatalError("Can not convert to NSMutableURLRequest")
184         }
185         
186         URLProtocol.setProperty(true,
187                                 forKey: CustomHTTPProtocol.requestProperty,
188                                 in: newRequest)
189         
190         if let cache = URLCache.kcd.validCach(for: request) {
191             
192             use(cache)
193             
194             Debug.excute(level: .full) {
195                 
196                 if let name = request.url?.lastPathComponent {
197                     
198                     print("Use cache for", name)
199                     
200                 } else {
201                     
202                     print("Use cache")
203                 }
204             }
205             
206             return
207         }
208         
209         threadOperator = ThreadOperator()
210         
211         let config = URLSessionConfiguration.default
212         session = URLSession(configuration: config, delegate: self, delegateQueue: nil)
213         dataTask = session?.dataTask(with: newRequest as URLRequest)
214         dataTask?.resume()
215     }
216     
217     override func stopLoading() {
218         
219         dataTask?.cancel()
220     }
221 }
222
223 extension CustomHTTPProtocol: URLSessionDataDelegate {
224     
225     func urlSession(_ session: URLSession,
226                     task: URLSessionTask,
227                     willPerformHTTPRedirection response: HTTPURLResponse,
228                     newRequest request: URLRequest,
229                     completionHandler: @escaping (URLRequest?) -> Void) {
230         
231         threadOperator?.execute { [weak self] in
232             
233             guard let `self` = self else { return }
234             
235             Debug.print("willPerformHTTPRedirection", level: .full)
236             
237             self.client?.urlProtocol(self, wasRedirectedTo: request, redirectResponse: response)
238             
239             completionHandler(request)
240         }
241     }
242     
243     func urlSession(_ session: URLSession,
244                     dataTask: URLSessionDataTask,
245                     didReceive response: URLResponse,
246                     completionHandler: @escaping (URLSession.ResponseDisposition) -> Void) {
247         
248         threadOperator?.execute { [weak self] in
249             
250             guard let `self` = self else { return }
251             
252             Debug.print("didReceive response", level: .full)
253             
254             if let response = response as? HTTPURLResponse,
255                 let request = dataTask.originalRequest {
256                 
257                 self.cachePolicy = cacheStoragePolicy(for: request, response: response)
258             }
259             
260             self.delegate?.customHTTPProtocol(self, didRecieve: response)
261             self.client?.urlProtocol(self, didReceive: response, cacheStoragePolicy: self.cachePolicy)
262             
263             completionHandler(.allow)
264         }
265     }
266     
267     func urlSession(_ session: URLSession, dataTask: URLSessionDataTask, didReceive data: Data) {
268         
269         threadOperator?.execute { [weak self] in
270             
271             guard let `self` = self else { return }
272             
273             Debug.print("didReceive data", level: .full)
274             if self.cachePolicy == .allowed {
275                 
276                 self.data.append(data)
277             }
278             
279             self.delegate?.customHTTPProtocol(self, didRecieve: data)
280             self.client?.urlProtocol(self, didLoad: data)
281             self.didRecieveData = true
282         }
283     }
284     
285     // cfurlErrorNetworkConnectionLost の場合はもう一度試す
286     private func canRetry(error: NSError) -> Bool {
287         
288         guard error.code == Int(CFNetworkErrors.cfurlErrorNetworkConnectionLost.rawValue),
289             !didRetry,
290             !didRecieveData else {
291                 
292                 return false
293         }
294         
295         print("Retry download...")
296         
297         return true
298     }
299     
300     func urlSession(_ session: URLSession, task: URLSessionTask, didCompleteWithError error: Error?) {
301         
302         threadOperator?.execute { [weak self] in
303             
304             guard let `self` = self else { return }
305             
306             if let error = error {
307                 
308                 if self.canRetry(error: error as NSError),
309                     let request = task.originalRequest {
310                     
311                     self.didRetry = true
312                     self.dataTask = session.dataTask(with: request)
313                     self.dataTask?.resume()
314                     
315                     return
316                 }
317                 
318                 Debug.print("didCompleteWithError ERROR", level: .full)
319                 
320                 self.delegate?.customHTTPProtocol(self, didFailWithError: error)
321                 self.client?.urlProtocol(self, didFailWithError: error)
322                 
323                 return
324             }
325             
326             Debug.print("didCompleteWithError SUCCESS", level: .full)
327             
328             self.delegate?.customHTTPProtocolDidFinishLoading(self)
329             self.client?.urlProtocolDidFinishLoading(self)
330             
331             if self.cachePolicy == .allowed {
332                 
333                 URLCache.kcd.storeIfNeeded(for: task, data: self.data)
334             }
335         }
336     }
337 }