]> git.saurik.com Git - cydia.git/blob - CyteKit/URLCache.mm
Put CydiaURLCache/NSURLConnection hook in CyteKit.
[cydia.git] / CyteKit / URLCache.mm
1 /* Cydia - iPhone UIKit Front-End for Debian APT
2 * Copyright (C) 2008-2015 Jay Freeman (saurik)
3 */
4
5 /* GNU General Public License, Version 3 {{{ */
6 /*
7 * Cydia is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published
9 * by the Free Software Foundation, either version 3 of the License,
10 * or (at your option) any later version.
11 *
12 * Cydia is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with Cydia. If not, see <http://www.gnu.org/licenses/>.
19 **/
20 /* }}} */
21
22 #include "CyteKit/UCPlatform.h"
23
24 #include "CyteKit/URLCache.h"
25 #include "Substrate.hpp"
26
27 #include <Menes/ObjectHandle.h>
28
29 static _H<NSMutableSet> CachedURLs_([NSMutableSet setWithCapacity:32]);
30
31 @implementation CyteURLCache {
32 }
33
34 - (void) logEvent:(NSString *)event forRequest:(NSURLRequest *)request {
35 #if !ForRelease
36 if (false);
37 else if ([event isEqualToString:@"no-cache"])
38 event = @"!!!";
39 else if ([event isEqualToString:@"store"])
40 event = @">>>";
41 else if ([event isEqualToString:@"invalid"])
42 event = @"???";
43 else if ([event isEqualToString:@"memory"])
44 event = @"mem";
45 else if ([event isEqualToString:@"disk"])
46 event = @"ssd";
47 else if ([event isEqualToString:@"miss"])
48 event = @"---";
49
50 NSLog(@"%@: %@", event, [[request URL] absoluteString]);
51 #endif
52 }
53
54 - (void) storeCachedResponse:(NSCachedURLResponse *)cached forRequest:(NSURLRequest *)request {
55 if (NSURLResponse *response = [cached response])
56 if (NSString *mime = [response MIMEType])
57 if ([mime isEqualToString:@"text/cache-manifest"]) {
58 NSURL *url([response URL]);
59
60 #if !ForRelease
61 NSLog(@"###: %@", [url absoluteString]);
62 #endif
63
64 @synchronized (CachedURLs_) {
65 [CachedURLs_ addObject:url];
66 }
67 }
68
69 [super storeCachedResponse:cached forRequest:request];
70 }
71
72 - (void) createDiskCachePath {
73 [super createDiskCachePath];
74 }
75
76 @end
77
78 MSClassHook(NSURLConnection)
79
80 MSHook(id, NSURLConnection$init$, NSURLConnection *self, SEL _cmd, NSURLRequest *request, id delegate, BOOL usesCache, int64_t maxContentLength, BOOL startImmediately, NSDictionary *connectionProperties) {
81 NSMutableURLRequest *copy([[request mutableCopy] autorelease]);
82
83 NSURL *url([copy URL]);
84
85 @synchronized (CachedURLs_) {
86 if (NSString *control = [copy valueForHTTPHeaderField:@"Cache-Control"])
87 if ([control isEqualToString:@"max-age=0"])
88 if ([CachedURLs_ containsObject:url]) {
89 #if !ForRelease
90 NSLog(@"~~~: %@", url);
91 #endif
92
93 [copy setCachePolicy:NSURLRequestReturnCacheDataDontLoad];
94
95 [copy setValue:nil forHTTPHeaderField:@"Cache-Control"];
96 [copy setValue:nil forHTTPHeaderField:@"If-Modified-Since"];
97 [copy setValue:nil forHTTPHeaderField:@"If-None-Match"];
98 }
99 }
100
101 if ((self = _NSURLConnection$init$(self, _cmd, copy, delegate, usesCache, maxContentLength, startImmediately, connectionProperties)) != nil) {
102 } return self;
103 }
104
105 CYHook(NSURLConnection, init$, _initWithRequest:delegate:usesCache:maxContentLength:startImmediately:connectionProperties:)