]> git.saurik.com Git - cydia.git/commitdiff
Put CydiaURLCache/NSURLConnection hook in CyteKit.
authorJay Freeman (saurik) <saurik@saurik.com>
Thu, 16 Feb 2017 08:07:10 +0000 (00:07 -0800)
committerJay Freeman (saurik) <saurik@saurik.com>
Thu, 16 Feb 2017 08:07:10 +0000 (00:07 -0800)
CyteKit/URLCache.h [new file with mode: 0644]
CyteKit/URLCache.mm [new file with mode: 0644]
Menes/ObjectHandle.h
MobileCydia.mm
Substrate.hpp

diff --git a/CyteKit/URLCache.h b/CyteKit/URLCache.h
new file mode 100644 (file)
index 0000000..e363b8f
--- /dev/null
@@ -0,0 +1,31 @@
+/* Cydia - iPhone UIKit Front-End for Debian APT
+ * Copyright (C) 2008-2015  Jay Freeman (saurik)
+*/
+
+/* GNU General Public License, Version 3 {{{ */
+/*
+ * Cydia is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published
+ * by the Free Software Foundation, either version 3 of the License,
+ * or (at your option) any later version.
+ *
+ * Cydia is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with Cydia.  If not, see <http://www.gnu.org/licenses/>.
+**/
+/* }}} */
+
+#ifndef CyteKit_URLCache_H
+#define CyteKit_URLCache_H
+
+#include "SDURLCache/SDURLCache.h"
+
+@interface CyteURLCache : SDURLCache
+
+@end
+
+#endif//CyteKit_URLCache_H
diff --git a/CyteKit/URLCache.mm b/CyteKit/URLCache.mm
new file mode 100644 (file)
index 0000000..e506130
--- /dev/null
@@ -0,0 +1,105 @@
+/* Cydia - iPhone UIKit Front-End for Debian APT
+ * Copyright (C) 2008-2015  Jay Freeman (saurik)
+*/
+
+/* GNU General Public License, Version 3 {{{ */
+/*
+ * Cydia is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published
+ * by the Free Software Foundation, either version 3 of the License,
+ * or (at your option) any later version.
+ *
+ * Cydia is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with Cydia.  If not, see <http://www.gnu.org/licenses/>.
+**/
+/* }}} */
+
+#include "CyteKit/UCPlatform.h"
+
+#include "CyteKit/URLCache.h"
+#include "Substrate.hpp"
+
+#include <Menes/ObjectHandle.h>
+
+static _H<NSMutableSet> CachedURLs_([NSMutableSet setWithCapacity:32]);
+
+@implementation CyteURLCache {
+}
+
+- (void) logEvent:(NSString *)event forRequest:(NSURLRequest *)request {
+#if !ForRelease
+    if (false);
+    else if ([event isEqualToString:@"no-cache"])
+        event = @"!!!";
+    else if ([event isEqualToString:@"store"])
+        event = @">>>";
+    else if ([event isEqualToString:@"invalid"])
+        event = @"???";
+    else if ([event isEqualToString:@"memory"])
+        event = @"mem";
+    else if ([event isEqualToString:@"disk"])
+        event = @"ssd";
+    else if ([event isEqualToString:@"miss"])
+        event = @"---";
+
+    NSLog(@"%@: %@", event, [[request URL] absoluteString]);
+#endif
+}
+
+- (void) storeCachedResponse:(NSCachedURLResponse *)cached forRequest:(NSURLRequest *)request {
+    if (NSURLResponse *response = [cached response])
+        if (NSString *mime = [response MIMEType])
+            if ([mime isEqualToString:@"text/cache-manifest"]) {
+                NSURL *url([response URL]);
+
+#if !ForRelease
+                NSLog(@"###: %@", [url absoluteString]);
+#endif
+
+                @synchronized (CachedURLs_) {
+                    [CachedURLs_ addObject:url];
+                }
+            }
+
+    [super storeCachedResponse:cached forRequest:request];
+}
+
+- (void) createDiskCachePath {
+    [super createDiskCachePath];
+}
+
+@end
+
+MSClassHook(NSURLConnection)
+
+MSHook(id, NSURLConnection$init$, NSURLConnection *self, SEL _cmd, NSURLRequest *request, id delegate, BOOL usesCache, int64_t maxContentLength, BOOL startImmediately, NSDictionary *connectionProperties) {
+    NSMutableURLRequest *copy([[request mutableCopy] autorelease]);
+
+    NSURL *url([copy URL]);
+
+    @synchronized (CachedURLs_) {
+        if (NSString *control = [copy valueForHTTPHeaderField:@"Cache-Control"])
+            if ([control isEqualToString:@"max-age=0"])
+                if ([CachedURLs_ containsObject:url]) {
+#if !ForRelease
+                    NSLog(@"~~~: %@", url);
+#endif
+
+                    [copy setCachePolicy:NSURLRequestReturnCacheDataDontLoad];
+
+                    [copy setValue:nil forHTTPHeaderField:@"Cache-Control"];
+                    [copy setValue:nil forHTTPHeaderField:@"If-Modified-Since"];
+                    [copy setValue:nil forHTTPHeaderField:@"If-None-Match"];
+                }
+    }
+
+    if ((self = _NSURLConnection$init$(self, _cmd, copy, delegate, usesCache, maxContentLength, startImmediately, connectionProperties)) != nil) {
+    } return self;
+}
+
+CYHook(NSURLConnection, init$, _initWithRequest:delegate:usesCache:maxContentLength:startImmediately:connectionProperties:)
index 9b8d57d76ed65444e0c5f31e886edc7c20109764..727338b700f07f2c512c7e3a626949885450f466 100644 (file)
@@ -128,4 +128,8 @@ rproperty_(Class, field) \
 wproperty_(Class, field, Field) \
 @end
 
+// XXX: I hate clang. Apple: please get over your petty hatred of GPL and fix your gcc fork
+#define synchronized(lock) \
+    synchronized(static_cast<NSObject *>(lock))
+
 #endif//Menes_ObjectHandle_H
index a5e9dbccc9ef1941211de0e48250d7d056f50804..d52a38eed100668914577aed7fdbfc6d0caec4fe 100644 (file)
@@ -117,6 +117,7 @@ extern "C" {
 #include "CyteKit/RegEx.hpp"
 #include "CyteKit/TableViewCell.h"
 #include "CyteKit/TabBarController.h"
+#include "CyteKit/URLCache.h"
 #include "CyteKit/WebScriptObject-Cyte.h"
 #include "CyteKit/WebViewController.h"
 #include "CyteKit/WebViewTableViewCell.h"
@@ -125,8 +126,6 @@ extern "C" {
 #include "Cydia/MIMEAddress.h"
 #include "Cydia/LoadingViewController.h"
 #include "Cydia/ProgressEvent.h"
-
-#include "SDURLCache/SDURLCache.h"
 /* }}} */
 
 /* Profiler {{{ */
@@ -199,10 +198,6 @@ void PrintTimes() {
 #define _end }
 /* }}} */
 
-// XXX: I hate clang. Apple: please get over your petty hatred of GPL and fix your gcc fork
-#define synchronized(lock) \
-    synchronized(static_cast<NSObject *>(lock))
-
 extern NSString *Cydia_;
 
 #define lprintf(args...) fprintf(stderr, args)
@@ -800,7 +795,6 @@ static _H<NSMutableDictionary> SessionData_;
 static _H<NSObject> HostConfig_;
 static _H<NSMutableSet> BridgedHosts_;
 static _H<NSMutableSet> InsecureHosts_;
-static _H<NSMutableSet> CachedURLs_;
 
 static NSString *kCydiaProgressEventTypeError = @"Error";
 static NSString *kCydiaProgressEventTypeInformation = @"Information";
@@ -8910,57 +8904,6 @@ static void HomeControllerReachabilityCallback(SCNetworkReachabilityRef reachabi
 @end
 /* }}} */
 
-@interface CYURLCache : SDURLCache {
-}
-
-@end
-
-@implementation CYURLCache
-
-- (void) logEvent:(NSString *)event forRequest:(NSURLRequest *)request {
-#if !ForRelease
-    if (false);
-    else if ([event isEqualToString:@"no-cache"])
-        event = @"!!!";
-    else if ([event isEqualToString:@"store"])
-        event = @">>>";
-    else if ([event isEqualToString:@"invalid"])
-        event = @"???";
-    else if ([event isEqualToString:@"memory"])
-        event = @"mem";
-    else if ([event isEqualToString:@"disk"])
-        event = @"ssd";
-    else if ([event isEqualToString:@"miss"])
-        event = @"---";
-
-    NSLog(@"%@: %@", event, [[request URL] absoluteString]);
-#endif
-}
-
-- (void) storeCachedResponse:(NSCachedURLResponse *)cached forRequest:(NSURLRequest *)request {
-    if (NSURLResponse *response = [cached response])
-        if (NSString *mime = [response MIMEType])
-            if ([mime isEqualToString:@"text/cache-manifest"]) {
-                NSURL *url([response URL]);
-
-#if !ForRelease
-                NSLog(@"###: %@", [url absoluteString]);
-#endif
-
-                @synchronized (HostConfig_) {
-                    [CachedURLs_ addObject:url];
-                }
-            }
-
-    [super storeCachedResponse:cached forRequest:request];
-}
-
-- (void) createDiskCachePath {
-    [super createDiskCachePath];
-}
-
-@end
-
 @interface Cydia : CyteApplication <
     ConfirmationControllerDelegate,
     DatabaseDelegate,
@@ -9868,7 +9811,7 @@ _trace();
         [BridgedHosts_ addObject:[[NSURL URLWithString:CydiaURL(@"")] host]];
     }
 
-    [NSURLCache setSharedURLCache:[[[CYURLCache alloc]
+    [NSURLCache setSharedURLCache:[[[CyteURLCache alloc]
         initWithMemoryCapacity:524288
         diskCapacity:10485760
         diskPath:Cache("SDURLCache")
@@ -10095,33 +10038,6 @@ id Dealloc_(id self, SEL selector) {
     return object;
 }*/
 
-Class $NSURLConnection;
-
-MSHook(id, NSURLConnection$init$, NSURLConnection *self, SEL _cmd, NSURLRequest *request, id delegate, BOOL usesCache, int64_t maxContentLength, BOOL startImmediately, NSDictionary *connectionProperties) {
-    NSMutableURLRequest *copy([[request mutableCopy] autorelease]);
-
-    NSURL *url([copy URL]);
-
-    @synchronized (HostConfig_) {
-        if (NSString *control = [copy valueForHTTPHeaderField:@"Cache-Control"])
-            if ([control isEqualToString:@"max-age=0"])
-                if ([CachedURLs_ containsObject:url]) {
-#if !ForRelease
-                    NSLog(@"~~~: %@", url);
-#endif
-
-                    [copy setCachePolicy:NSURLRequestReturnCacheDataDontLoad];
-
-                    [copy setValue:nil forHTTPHeaderField:@"Cache-Control"];
-                    [copy setValue:nil forHTTPHeaderField:@"If-Modified-Since"];
-                    [copy setValue:nil forHTTPHeaderField:@"If-None-Match"];
-                }
-    }
-
-    if ((self = _NSURLConnection$init$(self, _cmd, copy, delegate, usesCache, maxContentLength, startImmediately, connectionProperties)) != nil) {
-    } return self;
-}
-
 Class $WAKWindow;
 
 static CGSize $WAKWindow$screenSize(WAKWindow *self, SEL _cmd) {
@@ -10200,7 +10116,6 @@ int main(int argc, char *argv[]) {
     @synchronized (HostConfig_) {
         BridgedHosts_ = [NSMutableSet setWithCapacity:4];
         InsecureHosts_ = [NSMutableSet setWithCapacity:4];
-        CachedURLs_ = [NSMutableSet setWithCapacity:32];
     }
 
     NSString *ui(@"ui/ios");
@@ -10219,13 +10134,6 @@ int main(int argc, char *argv[]) {
         if (Method method = class_getInstanceMethod($WAKWindow, @selector(screenSize)))
             method_setImplementation(method, (IMP) &$WAKWindow$screenSize);
 
-    $NSURLConnection = objc_getClass("NSURLConnection");
-    Method NSURLConnection$init$(class_getInstanceMethod($NSURLConnection, @selector(_initWithRequest:delegate:usesCache:maxContentLength:startImmediately:connectionProperties:)));
-    if (NSURLConnection$init$ != NULL) {
-        _NSURLConnection$init$ = reinterpret_cast<id (*)(NSURLConnection *, SEL, NSURLRequest *, id, BOOL, int64_t, BOOL, NSDictionary *)>(method_getImplementation(NSURLConnection$init$));
-        method_setImplementation(NSURLConnection$init$, reinterpret_cast<IMP>(&$NSURLConnection$init$));
-    }
-
     $NSUserDefaults = objc_getClass("NSUserDefaults");
     Method NSUserDefaults$objectForKey$(class_getInstanceMethod($NSUserDefaults, @selector(objectForKey:)));
     if (NSUserDefaults$objectForKey$ != NULL) {
index 9826b567398be84c4d0c06ad5cf2b9fd049665e6..57e89fbb5b80ebd5726d32fb7dcaea2053590f25 100644 (file)
@@ -31,8 +31,21 @@ static inline Type_ &MSHookIvar(id self, const char *name) {
     return *reinterpret_cast<Type_ *>(pointer);
 }
 
+#define MSClassHook(name) \
+    @class name; \
+    static Class $ ## name = objc_getClass(#name);
+
 #define MSHook(type, name, args...) \
     static type (*_ ## name)(args); \
     static type $ ## name(args)
 
+#define CYHook(Type, Code, Name) \
+static struct Type ## $ ## Code { Type ## $ ## Code() { \
+    Method Type ## $ ## Code(class_getInstanceMethod($ ## Type, @selector(Name))); \
+    if (Type ## $ ## Code != NULL) { \
+        _ ## Type ## $ ## Code = reinterpret_cast<decltype(_ ## Type ## $ ## Code)>(method_getImplementation(Type ## $ ## Code)); \
+        method_setImplementation(Type ## $ ## Code, reinterpret_cast<IMP>(&$ ## Type ## $ ## Code)); \
+    } \
+} } Type ## $ ## Code;
+
 #endif//Substrate_HPP