FLEXNetworkObserver.m 58 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123
  1. //
  2. // FLEXNetworkObserver.m
  3. // Derived from:
  4. //
  5. // PDAFNetworkDomainController.m
  6. // PonyDebugger
  7. //
  8. // Created by Mike Lewis on 2/27/12.
  9. //
  10. // Licensed to Square, Inc. under one or more contributor license agreements.
  11. // See the LICENSE file distributed with this work for the terms under
  12. // which Square, Inc. licenses this file to you.
  13. //
  14. #import "FLEXNetworkObserver.h"
  15. #import "FLEXNetworkRecorder.h"
  16. #import "FLEXUtility.h"
  17. #import <objc/runtime.h>
  18. #import <objc/message.h>
  19. #import <dispatch/queue.h>
  20. NSString *const kFLEXNetworkObserverEnabledStateChangedNotification = @"kFLEXNetworkObserverEnabledStateChangedNotification";
  21. static NSString *const kFLEXNetworkObserverEnabledDefaultsKey = @"com.flex.FLEXNetworkObserver.enableOnLaunch";
  22. typedef void (^NSURLSessionAsyncCompletion)(id fileURLOrData, NSURLResponse *response, NSError *error);
  23. @interface FLEXInternalRequestState : NSObject
  24. @property (nonatomic, copy) NSURLRequest *request;
  25. @property (nonatomic, strong) NSMutableData *dataAccumulator;
  26. @end
  27. @implementation FLEXInternalRequestState
  28. @end
  29. @interface FLEXNetworkObserver (NSURLConnectionHelpers)
  30. - (void)connection:(NSURLConnection *)connection willSendRequest:(NSURLRequest *)request redirectResponse:(NSURLResponse *)response delegate:(id <NSURLConnectionDelegate>)delegate;
  31. - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response delegate:(id <NSURLConnectionDelegate>)delegate;
  32. - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data delegate:(id <NSURLConnectionDelegate>)delegate;
  33. - (void)connectionDidFinishLoading:(NSURLConnection *)connection delegate:(id <NSURLConnectionDelegate>)delegate;
  34. - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error delegate:(id <NSURLConnectionDelegate>)delegate;
  35. - (void)connectionWillCancel:(NSURLConnection *)connection;
  36. @end
  37. @interface FLEXNetworkObserver (NSURLSessionTaskHelpers)
  38. - (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task willPerformHTTPRedirection:(NSHTTPURLResponse *)response newRequest:(NSURLRequest *)request completionHandler:(void (^)(NSURLRequest *))completionHandler delegate:(id <NSURLSessionDelegate>)delegate;
  39. - (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didReceiveResponse:(NSURLResponse *)response completionHandler:(void (^)(NSURLSessionResponseDisposition disposition))completionHandler delegate:(id <NSURLSessionDelegate>)delegate;
  40. - (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didReceiveData:(NSData *)data delegate:(id <NSURLSessionDelegate>)delegate;
  41. - (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask
  42. didBecomeDownloadTask:(NSURLSessionDownloadTask *)downloadTask delegate:(id <NSURLSessionDelegate>)delegate;
  43. - (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(NSError *)error delegate:(id <NSURLSessionDelegate>)delegate;
  44. - (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didWriteData:(int64_t)bytesWritten totalBytesWritten:(int64_t)totalBytesWritten totalBytesExpectedToWrite:(int64_t)totalBytesExpectedToWrite delegate:(id <NSURLSessionDelegate>)delegate;
  45. - (void)URLSession:(NSURLSession *)session task:(NSURLSessionDownloadTask *)downloadTask didFinishDownloadingToURL:(NSURL *)location data:(NSData *)data delegate:(id <NSURLSessionDelegate>)delegate;
  46. - (void)URLSessionTaskWillResume:(NSURLSessionTask *)task;
  47. @end
  48. @interface FLEXNetworkObserver ()
  49. @property (nonatomic, strong) NSMutableDictionary *requestStatesForRequestIDs;
  50. @property (nonatomic, strong) dispatch_queue_t queue;
  51. @end
  52. @implementation FLEXNetworkObserver
  53. #pragma mark - Public Methods
  54. + (void)setEnabled:(BOOL)enabled
  55. {
  56. BOOL previouslyEnabled = [self isEnabled];
  57. [[NSUserDefaults standardUserDefaults] setBool:enabled forKey:kFLEXNetworkObserverEnabledDefaultsKey];
  58. if (enabled) {
  59. // Inject if needed. This injection is protected with a dispatch_once, so we're ok calling it multiple times.
  60. // By doing the injection lazily, we keep the impact of the tool lower when this feature isn't enabled.
  61. [self injectIntoAllNSURLConnectionDelegateClasses];
  62. }
  63. if (previouslyEnabled != enabled) {
  64. [[NSNotificationCenter defaultCenter] postNotificationName:kFLEXNetworkObserverEnabledStateChangedNotification object:self];
  65. }
  66. }
  67. + (BOOL)isEnabled
  68. {
  69. return [[[NSUserDefaults standardUserDefaults] objectForKey:kFLEXNetworkObserverEnabledDefaultsKey] boolValue];
  70. }
  71. + (void)load
  72. {
  73. // We don't want to do the swizzling from +load because not all the classes may be loaded at this point.
  74. dispatch_async(dispatch_get_main_queue(), ^{
  75. if ([self isEnabled]) {
  76. [self injectIntoAllNSURLConnectionDelegateClasses];
  77. }
  78. });
  79. }
  80. #pragma mark - Statics
  81. + (instancetype)sharedObserver
  82. {
  83. static FLEXNetworkObserver *sharedObserver = nil;
  84. static dispatch_once_t onceToken;
  85. dispatch_once(&onceToken, ^{
  86. sharedObserver = [[[self class] alloc] init];
  87. });
  88. return sharedObserver;
  89. }
  90. + (NSString *)nextRequestID
  91. {
  92. return [[NSUUID UUID] UUIDString];
  93. }
  94. #pragma mark Delegate Injection Convenience Methods
  95. /// All swizzled delegate methods should make use of this guard.
  96. /// This will prevent duplicated sniffing when the original implementation calls up to a superclass implementation which we've also swizzled.
  97. /// The superclass implementation (and implementations in classes above that) will be executed without inteference if called from the original implementation.
  98. + (void)sniffWithoutDuplicationForObject:(NSObject *)object selector:(SEL)selector sniffingBlock:(void (^)(void))sniffingBlock originalImplementationBlock:(void (^)(void))originalImplementationBlock
  99. {
  100. // If we don't have an object to detect nested calls on, just run the original implmentation and bail.
  101. // This case can happen if someone besides the URL loading system calls the delegate methods directly.
  102. // See https://github.com/Flipboard/FLEX/issues/61 for an example.
  103. if (!object) {
  104. originalImplementationBlock();
  105. return;
  106. }
  107. const void *key = selector;
  108. // Don't run the sniffing block if we're inside a nested call
  109. if (!objc_getAssociatedObject(object, key)) {
  110. sniffingBlock();
  111. }
  112. // Mark that we're calling through to the original so we can detect nested calls
  113. objc_setAssociatedObject(object, key, @YES, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
  114. originalImplementationBlock();
  115. objc_setAssociatedObject(object, key, nil, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
  116. }
  117. #pragma mark - Delegate Injection
  118. + (void)injectIntoAllNSURLConnectionDelegateClasses
  119. {
  120. // Only allow swizzling once.
  121. static dispatch_once_t onceToken;
  122. dispatch_once(&onceToken, ^{
  123. // Swizzle any classes that implement one of these selectors.
  124. const SEL selectors[] = {
  125. @selector(connectionDidFinishLoading:),
  126. @selector(connection:willSendRequest:redirectResponse:),
  127. @selector(connection:didReceiveResponse:),
  128. @selector(connection:didReceiveData:),
  129. @selector(connection:didFailWithError:),
  130. @selector(URLSession:task:willPerformHTTPRedirection:newRequest:completionHandler:),
  131. @selector(URLSession:dataTask:didReceiveData:),
  132. @selector(URLSession:dataTask:didReceiveResponse:completionHandler:),
  133. @selector(URLSession:task:didCompleteWithError:),
  134. @selector(URLSession:dataTask:didBecomeDownloadTask:delegate:),
  135. @selector(URLSession:downloadTask:didWriteData:totalBytesWritten:totalBytesExpectedToWrite:),
  136. @selector(URLSession:downloadTask:didFinishDownloadingToURL:)
  137. };
  138. const int numSelectors = sizeof(selectors) / sizeof(SEL);
  139. Class *classes = NULL;
  140. int numClasses = objc_getClassList(NULL, 0);
  141. if (numClasses > 0) {
  142. classes = (__unsafe_unretained Class *)malloc(sizeof(Class) * numClasses);
  143. numClasses = objc_getClassList(classes, numClasses);
  144. for (NSInteger classIndex = 0; classIndex < numClasses; ++classIndex) {
  145. Class class = classes[classIndex];
  146. if (class == [FLEXNetworkObserver class]) {
  147. continue;
  148. }
  149. // Use the runtime API rather than the methods on NSObject to avoid sending messages to
  150. // classes we're not interested in swizzling. Otherwise we hit +initialize on all classes.
  151. // NOTE: calling class_getInstanceMethod() DOES send +initialize to the class. That's why we iterate through the method list.
  152. unsigned int methodCount = 0;
  153. Method *methods = class_copyMethodList(class, &methodCount);
  154. BOOL matchingSelectorFound = NO;
  155. for (unsigned int methodIndex = 0; methodIndex < methodCount; methodIndex++) {
  156. for (int selectorIndex = 0; selectorIndex < numSelectors; ++selectorIndex) {
  157. if (method_getName(methods[methodIndex]) == selectors[selectorIndex]) {
  158. [self injectIntoDelegateClass:class];
  159. matchingSelectorFound = YES;
  160. break;
  161. }
  162. }
  163. if (matchingSelectorFound) {
  164. break;
  165. }
  166. }
  167. free(methods);
  168. }
  169. free(classes);
  170. }
  171. [self injectIntoNSURLConnectionCancel];
  172. [self injectIntoNSURLSessionTaskResume];
  173. [self injectIntoNSURLConnectionAsynchronousClassMethod];
  174. [self injectIntoNSURLConnectionSynchronousClassMethod];
  175. [self injectIntoNSURLSessionAsyncDataAndDownloadTaskMethods];
  176. [self injectIntoNSURLSessionAsyncUploadTaskMethods];
  177. });
  178. }
  179. + (void)injectIntoDelegateClass:(Class)cls
  180. {
  181. // Connections
  182. [self injectWillSendRequestIntoDelegateClass:cls];
  183. [self injectDidReceiveDataIntoDelegateClass:cls];
  184. [self injectDidReceiveResponseIntoDelegateClass:cls];
  185. [self injectDidFinishLoadingIntoDelegateClass:cls];
  186. [self injectDidFailWithErrorIntoDelegateClass:cls];
  187. // Sessions
  188. [self injectTaskWillPerformHTTPRedirectionIntoDelegateClass:cls];
  189. [self injectTaskDidReceiveDataIntoDelegateClass:cls];
  190. [self injectTaskDidReceiveResponseIntoDelegateClass:cls];
  191. [self injectTaskDidCompleteWithErrorIntoDelegateClass:cls];
  192. [self injectRespondsToSelectorIntoDelegateClass:cls];
  193. // Data tasks
  194. [self injectDataTaskDidBecomeDownloadTaskIntoDelegateClass:cls];
  195. // Download tasks
  196. [self injectDownloadTaskDidWriteDataIntoDelegateClass:cls];
  197. [self injectDownloadTaskDidFinishDownloadingIntoDelegateClass:cls];
  198. }
  199. + (void)injectIntoNSURLConnectionCancel
  200. {
  201. static dispatch_once_t onceToken;
  202. dispatch_once(&onceToken, ^{
  203. Class class = [NSURLConnection class];
  204. SEL selector = @selector(cancel);
  205. SEL swizzledSelector = [FLEXUtility swizzledSelectorForSelector:selector];
  206. Method originalCancel = class_getInstanceMethod(class, selector);
  207. void (^swizzleBlock)(NSURLConnection *) = ^(NSURLConnection *slf) {
  208. [[FLEXNetworkObserver sharedObserver] connectionWillCancel:slf];
  209. ((void(*)(id, SEL))objc_msgSend)(slf, swizzledSelector);
  210. };
  211. IMP implementation = imp_implementationWithBlock(swizzleBlock);
  212. class_addMethod(class, swizzledSelector, implementation, method_getTypeEncoding(originalCancel));
  213. Method newCancel = class_getInstanceMethod(class, swizzledSelector);
  214. method_exchangeImplementations(originalCancel, newCancel);
  215. });
  216. }
  217. + (void)injectIntoNSURLSessionTaskResume
  218. {
  219. static dispatch_once_t onceToken;
  220. dispatch_once(&onceToken, ^{
  221. // In iOS 7 resume lives in __NSCFLocalSessionTask
  222. // In iOS 8 resume lives in NSURLSessionTask
  223. // In iOS 9 resume lives in __NSCFURLSessionTask
  224. Class class = Nil;
  225. if (![[NSProcessInfo processInfo] respondsToSelector:@selector(operatingSystemVersion)]) {
  226. class = NSClassFromString([@[@"__", @"NSC", @"FLocalS", @"ession", @"Task"] componentsJoinedByString:@""]);
  227. } else if ([[NSProcessInfo processInfo] operatingSystemVersion].majorVersion < 9) {
  228. class = [NSURLSessionTask class];
  229. } else {
  230. class = NSClassFromString([@[@"__", @"NSC", @"FURLS", @"ession", @"Task"] componentsJoinedByString:@""]);
  231. }
  232. SEL selector = @selector(resume);
  233. SEL swizzledSelector = [FLEXUtility swizzledSelectorForSelector:selector];
  234. Method originalResume = class_getInstanceMethod(class, selector);
  235. void (^swizzleBlock)(NSURLSessionTask *) = ^(NSURLSessionTask *slf) {
  236. [[FLEXNetworkObserver sharedObserver] URLSessionTaskWillResume:slf];
  237. ((void(*)(id, SEL))objc_msgSend)(slf, swizzledSelector);
  238. };
  239. IMP implementation = imp_implementationWithBlock(swizzleBlock);
  240. class_addMethod(class, swizzledSelector, implementation, method_getTypeEncoding(originalResume));
  241. Method newResume = class_getInstanceMethod(class, swizzledSelector);
  242. method_exchangeImplementations(originalResume, newResume);
  243. });
  244. }
  245. + (void)injectIntoNSURLConnectionAsynchronousClassMethod
  246. {
  247. static dispatch_once_t onceToken;
  248. dispatch_once(&onceToken, ^{
  249. Class class = objc_getMetaClass(class_getName([NSURLConnection class]));
  250. SEL selector = @selector(sendAsynchronousRequest:queue:completionHandler:);
  251. SEL swizzledSelector = [FLEXUtility swizzledSelectorForSelector:selector];
  252. typedef void (^NSURLConnectionAsyncCompletion)(NSURLResponse* response, NSData* data, NSError* connectionError);
  253. void (^asyncSwizzleBlock)(Class, NSURLRequest *, NSOperationQueue *, NSURLConnectionAsyncCompletion) = ^(Class slf, NSURLRequest *request, NSOperationQueue *queue, NSURLConnectionAsyncCompletion completion) {
  254. if ([FLEXNetworkObserver isEnabled]) {
  255. NSString *requestID = [self nextRequestID];
  256. [[FLEXNetworkRecorder defaultRecorder] recordRequestWillBeSentWithRequestID:requestID request:request redirectResponse:nil];
  257. NSString *mechanism = [self mechansimFromClassMethod:selector onClass:class];
  258. [[FLEXNetworkRecorder defaultRecorder] recordMechanism:mechanism forRequestID:requestID];
  259. NSURLConnectionAsyncCompletion completionWrapper = ^(NSURLResponse *response, NSData *data, NSError *connectionError) {
  260. [[FLEXNetworkRecorder defaultRecorder] recordResponseReceivedWithRequestID:requestID response:response];
  261. [[FLEXNetworkRecorder defaultRecorder] recordDataReceivedWithRequestID:requestID dataLength:[data length]];
  262. if (connectionError) {
  263. [[FLEXNetworkRecorder defaultRecorder] recordLoadingFailedWithRequestID:requestID error:connectionError];
  264. } else {
  265. [[FLEXNetworkRecorder defaultRecorder] recordLoadingFinishedWithRequestID:requestID responseBody:data];
  266. }
  267. // Call through to the original completion handler
  268. if (completion) {
  269. completion(response, data, connectionError);
  270. }
  271. };
  272. ((void(*)(id, SEL, id, id, id))objc_msgSend)(slf, swizzledSelector, request, queue, completionWrapper);
  273. } else {
  274. ((void(*)(id, SEL, id, id, id))objc_msgSend)(slf, swizzledSelector, request, queue, completion);
  275. }
  276. };
  277. [FLEXUtility replaceImplementationOfKnownSelector:selector onClass:class withBlock:asyncSwizzleBlock swizzledSelector:swizzledSelector];
  278. });
  279. }
  280. + (void)injectIntoNSURLConnectionSynchronousClassMethod
  281. {
  282. static dispatch_once_t onceToken;
  283. dispatch_once(&onceToken, ^{
  284. Class class = objc_getMetaClass(class_getName([NSURLConnection class]));
  285. SEL selector = @selector(sendSynchronousRequest:returningResponse:error:);
  286. SEL swizzledSelector = [FLEXUtility swizzledSelectorForSelector:selector];
  287. NSData *(^syncSwizzleBlock)(Class, NSURLRequest *, NSURLResponse **, NSError **) = ^NSData *(Class slf, NSURLRequest *request, NSURLResponse **response, NSError **error) {
  288. NSData *data = nil;
  289. if ([FLEXNetworkObserver isEnabled]) {
  290. NSString *requestID = [self nextRequestID];
  291. [[FLEXNetworkRecorder defaultRecorder] recordRequestWillBeSentWithRequestID:requestID request:request redirectResponse:nil];
  292. NSString *mechanism = [self mechansimFromClassMethod:selector onClass:class];
  293. [[FLEXNetworkRecorder defaultRecorder] recordMechanism:mechanism forRequestID:requestID];
  294. NSError *temporaryError = nil;
  295. NSURLResponse *temporaryResponse = nil;
  296. data = ((id(*)(id, SEL, id, NSURLResponse **, NSError **))objc_msgSend)(slf, swizzledSelector, request, &temporaryResponse, &temporaryError);
  297. [[FLEXNetworkRecorder defaultRecorder] recordResponseReceivedWithRequestID:requestID response:temporaryResponse];
  298. [[FLEXNetworkRecorder defaultRecorder] recordDataReceivedWithRequestID:requestID dataLength:[data length]];
  299. if (temporaryError) {
  300. [[FLEXNetworkRecorder defaultRecorder] recordLoadingFailedWithRequestID:requestID error:temporaryError];
  301. } else {
  302. [[FLEXNetworkRecorder defaultRecorder] recordLoadingFinishedWithRequestID:requestID responseBody:data];
  303. }
  304. if (error) {
  305. *error = temporaryError;
  306. }
  307. if (response) {
  308. *response = temporaryResponse;
  309. }
  310. } else {
  311. data = ((id(*)(id, SEL, id, NSURLResponse **, NSError **))objc_msgSend)(slf, swizzledSelector, request, response, error);
  312. }
  313. return data;
  314. };
  315. [FLEXUtility replaceImplementationOfKnownSelector:selector onClass:class withBlock:syncSwizzleBlock swizzledSelector:swizzledSelector];
  316. });
  317. }
  318. + (void)injectIntoNSURLSessionAsyncDataAndDownloadTaskMethods
  319. {
  320. static dispatch_once_t onceToken;
  321. dispatch_once(&onceToken, ^{
  322. Class class = [NSURLSession class];
  323. // The method signatures here are close enough that we can use the same logic to inject into all of them.
  324. const SEL selectors[] = {
  325. @selector(dataTaskWithRequest:completionHandler:),
  326. @selector(dataTaskWithURL:completionHandler:),
  327. @selector(downloadTaskWithRequest:completionHandler:),
  328. @selector(downloadTaskWithResumeData:completionHandler:),
  329. @selector(downloadTaskWithURL:completionHandler:)
  330. };
  331. const int numSelectors = sizeof(selectors) / sizeof(SEL);
  332. for (int selectorIndex = 0; selectorIndex < numSelectors; selectorIndex++) {
  333. SEL selector = selectors[selectorIndex];
  334. SEL swizzledSelector = [FLEXUtility swizzledSelectorForSelector:selector];
  335. if ([FLEXUtility instanceRespondsButDoesNotImplementSelector:selector class:class]) {
  336. // iOS 7 does not implement these methods on NSURLSession. We actually want to
  337. // swizzle __NSCFURLSession, which we can get from the class of the shared session
  338. class = [[NSURLSession sharedSession] class];
  339. }
  340. NSURLSessionTask *(^asyncDataOrDownloadSwizzleBlock)(Class, id, NSURLSessionAsyncCompletion) = ^NSURLSessionTask *(Class slf, id argument, NSURLSessionAsyncCompletion completion) {
  341. NSURLSessionTask *task = nil;
  342. // If completion block was not provided sender expect to receive delegated methods or does not
  343. // interested in callback at all. In this case we should just call original method implementation
  344. // with nil completion block.
  345. if ([FLEXNetworkObserver isEnabled] && completion) {
  346. NSString *requestID = [self nextRequestID];
  347. NSString *mechanism = [self mechansimFromClassMethod:selector onClass:class];
  348. NSURLSessionAsyncCompletion completionWrapper = [self asyncCompletionWrapperForRequestID:requestID mechanism:mechanism completion:completion];
  349. task = ((id(*)(id, SEL, id, id))objc_msgSend)(slf, swizzledSelector, argument, completionWrapper);
  350. [self setRequestID:requestID forConnectionOrTask:task];
  351. } else {
  352. task = ((id(*)(id, SEL, id, id))objc_msgSend)(slf, swizzledSelector, argument, completion);
  353. }
  354. return task;
  355. };
  356. [FLEXUtility replaceImplementationOfKnownSelector:selector onClass:class withBlock:asyncDataOrDownloadSwizzleBlock swizzledSelector:swizzledSelector];
  357. }
  358. });
  359. }
  360. + (void)injectIntoNSURLSessionAsyncUploadTaskMethods
  361. {
  362. static dispatch_once_t onceToken;
  363. dispatch_once(&onceToken, ^{
  364. Class class = [NSURLSession class];
  365. // The method signatures here are close enough that we can use the same logic to inject into both of them.
  366. // Note that they have 3 arguments, so we can't easily combine with the data and download method above.
  367. const SEL selectors[] = {
  368. @selector(uploadTaskWithRequest:fromData:completionHandler:),
  369. @selector(uploadTaskWithRequest:fromFile:completionHandler:)
  370. };
  371. const int numSelectors = sizeof(selectors) / sizeof(SEL);
  372. for (int selectorIndex = 0; selectorIndex < numSelectors; selectorIndex++) {
  373. SEL selector = selectors[selectorIndex];
  374. SEL swizzledSelector = [FLEXUtility swizzledSelectorForSelector:selector];
  375. if ([FLEXUtility instanceRespondsButDoesNotImplementSelector:selector class:class]) {
  376. // iOS 7 does not implement these methods on NSURLSession. We actually want to
  377. // swizzle __NSCFURLSession, which we can get from the class of the shared session
  378. class = [[NSURLSession sharedSession] class];
  379. }
  380. NSURLSessionUploadTask *(^asyncUploadTaskSwizzleBlock)(Class, NSURLRequest *, id, NSURLSessionAsyncCompletion) = ^NSURLSessionUploadTask *(Class slf, NSURLRequest *request, id argument, NSURLSessionAsyncCompletion completion) {
  381. NSURLSessionUploadTask *task = nil;
  382. if ([FLEXNetworkObserver isEnabled]) {
  383. NSString *requestID = [self nextRequestID];
  384. NSString *mechanism = [self mechansimFromClassMethod:selector onClass:class];
  385. NSURLSessionAsyncCompletion completionWrapper = [self asyncCompletionWrapperForRequestID:requestID mechanism:mechanism completion:completion];
  386. task = ((id(*)(id, SEL, id, id, id))objc_msgSend)(slf, swizzledSelector, request, argument, completionWrapper);
  387. [self setRequestID:requestID forConnectionOrTask:task];
  388. } else {
  389. task = ((id(*)(id, SEL, id, id, id))objc_msgSend)(slf, swizzledSelector, request, argument, completion);
  390. }
  391. return task;
  392. };
  393. [FLEXUtility replaceImplementationOfKnownSelector:selector onClass:class withBlock:asyncUploadTaskSwizzleBlock swizzledSelector:swizzledSelector];
  394. }
  395. });
  396. }
  397. + (NSString *)mechansimFromClassMethod:(SEL)selector onClass:(Class)class
  398. {
  399. return [NSString stringWithFormat:@"+[%@ %@]", NSStringFromClass(class), NSStringFromSelector(selector)];
  400. }
  401. + (NSURLSessionAsyncCompletion)asyncCompletionWrapperForRequestID:(NSString *)requestID mechanism:(NSString *)mechanism completion:(NSURLSessionAsyncCompletion)completion
  402. {
  403. NSURLSessionAsyncCompletion completionWrapper = ^(id fileURLOrData, NSURLResponse *response, NSError *error) {
  404. [[FLEXNetworkRecorder defaultRecorder] recordMechanism:mechanism forRequestID:requestID];
  405. [[FLEXNetworkRecorder defaultRecorder] recordResponseReceivedWithRequestID:requestID response:response];
  406. NSData *data = nil;
  407. if ([fileURLOrData isKindOfClass:[NSURL class]]) {
  408. data = [NSData dataWithContentsOfURL:fileURLOrData];
  409. } else if ([fileURLOrData isKindOfClass:[NSData class]]) {
  410. data = fileURLOrData;
  411. }
  412. [[FLEXNetworkRecorder defaultRecorder] recordDataReceivedWithRequestID:requestID dataLength:[data length]];
  413. if (error) {
  414. [[FLEXNetworkRecorder defaultRecorder] recordLoadingFailedWithRequestID:requestID error:error];
  415. } else {
  416. [[FLEXNetworkRecorder defaultRecorder] recordLoadingFinishedWithRequestID:requestID responseBody:data];
  417. }
  418. // Call through to the original completion handler
  419. if (completion) {
  420. completion(fileURLOrData, response, error);
  421. }
  422. };
  423. return completionWrapper;
  424. }
  425. + (void)injectWillSendRequestIntoDelegateClass:(Class)cls
  426. {
  427. SEL selector = @selector(connection:willSendRequest:redirectResponse:);
  428. SEL swizzledSelector = [FLEXUtility swizzledSelectorForSelector:selector];
  429. Protocol *protocol = @protocol(NSURLConnectionDataDelegate);
  430. if (!protocol) {
  431. protocol = @protocol(NSURLConnectionDelegate);
  432. }
  433. struct objc_method_description methodDescription = protocol_getMethodDescription(protocol, selector, NO, YES);
  434. typedef NSURLRequest *(^NSURLConnectionWillSendRequestBlock)(id <NSURLConnectionDelegate> slf, NSURLConnection *connection, NSURLRequest *request, NSURLResponse *response);
  435. NSURLConnectionWillSendRequestBlock undefinedBlock = ^NSURLRequest *(id <NSURLConnectionDelegate> slf, NSURLConnection *connection, NSURLRequest *request, NSURLResponse *response) {
  436. [[FLEXNetworkObserver sharedObserver] connection:connection willSendRequest:request redirectResponse:response delegate:slf];
  437. return request;
  438. };
  439. NSURLConnectionWillSendRequestBlock implementationBlock = ^NSURLRequest *(id <NSURLConnectionDelegate> slf, NSURLConnection *connection, NSURLRequest *request, NSURLResponse *response) {
  440. __block NSURLRequest *returnValue = nil;
  441. [self sniffWithoutDuplicationForObject:connection selector:selector sniffingBlock:^{
  442. undefinedBlock(slf, connection, request, response);
  443. } originalImplementationBlock:^{
  444. returnValue = ((id(*)(id, SEL, id, id, id))objc_msgSend)(slf, swizzledSelector, connection, request, response);
  445. }];
  446. return returnValue;
  447. };
  448. [FLEXUtility replaceImplementationOfSelector:selector withSelector:swizzledSelector forClass:cls withMethodDescription:methodDescription implementationBlock:implementationBlock undefinedBlock:undefinedBlock];
  449. }
  450. + (void)injectDidReceiveResponseIntoDelegateClass:(Class)cls
  451. {
  452. SEL selector = @selector(connection:didReceiveResponse:);
  453. SEL swizzledSelector = [FLEXUtility swizzledSelectorForSelector:selector];
  454. Protocol *protocol = @protocol(NSURLConnectionDataDelegate);
  455. if (!protocol) {
  456. protocol = @protocol(NSURLConnectionDelegate);
  457. }
  458. struct objc_method_description methodDescription = protocol_getMethodDescription(protocol, selector, NO, YES);
  459. typedef void (^NSURLConnectionDidReceiveResponseBlock)(id <NSURLConnectionDelegate> slf, NSURLConnection *connection, NSURLResponse *response);
  460. NSURLConnectionDidReceiveResponseBlock undefinedBlock = ^(id <NSURLConnectionDelegate> slf, NSURLConnection *connection, NSURLResponse *response) {
  461. [[FLEXNetworkObserver sharedObserver] connection:connection didReceiveResponse:response delegate:slf];
  462. };
  463. NSURLConnectionDidReceiveResponseBlock implementationBlock = ^(id <NSURLConnectionDelegate> slf, NSURLConnection *connection, NSURLResponse *response) {
  464. [self sniffWithoutDuplicationForObject:connection selector:selector sniffingBlock:^{
  465. undefinedBlock(slf, connection, response);
  466. } originalImplementationBlock:^{
  467. ((void(*)(id, SEL, id, id))objc_msgSend)(slf, swizzledSelector, connection, response);
  468. }];
  469. };
  470. [FLEXUtility replaceImplementationOfSelector:selector withSelector:swizzledSelector forClass:cls withMethodDescription:methodDescription implementationBlock:implementationBlock undefinedBlock:undefinedBlock];
  471. }
  472. + (void)injectDidReceiveDataIntoDelegateClass:(Class)cls
  473. {
  474. SEL selector = @selector(connection:didReceiveData:);
  475. SEL swizzledSelector = [FLEXUtility swizzledSelectorForSelector:selector];
  476. Protocol *protocol = @protocol(NSURLConnectionDataDelegate);
  477. if (!protocol) {
  478. protocol = @protocol(NSURLConnectionDelegate);
  479. }
  480. struct objc_method_description methodDescription = protocol_getMethodDescription(protocol, selector, NO, YES);
  481. typedef void (^NSURLConnectionDidReceiveDataBlock)(id <NSURLConnectionDelegate> slf, NSURLConnection *connection, NSData *data);
  482. NSURLConnectionDidReceiveDataBlock undefinedBlock = ^(id <NSURLConnectionDelegate> slf, NSURLConnection *connection, NSData *data) {
  483. [[FLEXNetworkObserver sharedObserver] connection:connection didReceiveData:data delegate:slf];
  484. };
  485. NSURLConnectionDidReceiveDataBlock implementationBlock = ^(id <NSURLConnectionDelegate> slf, NSURLConnection *connection, NSData *data) {
  486. [self sniffWithoutDuplicationForObject:connection selector:selector sniffingBlock:^{
  487. undefinedBlock(slf, connection, data);
  488. } originalImplementationBlock:^{
  489. ((void(*)(id, SEL, id, id))objc_msgSend)(slf, swizzledSelector, connection, data);
  490. }];
  491. };
  492. [FLEXUtility replaceImplementationOfSelector:selector withSelector:swizzledSelector forClass:cls withMethodDescription:methodDescription implementationBlock:implementationBlock undefinedBlock:undefinedBlock];
  493. }
  494. + (void)injectDidFinishLoadingIntoDelegateClass:(Class)cls
  495. {
  496. SEL selector = @selector(connectionDidFinishLoading:);
  497. SEL swizzledSelector = [FLEXUtility swizzledSelectorForSelector:selector];
  498. Protocol *protocol = @protocol(NSURLConnectionDataDelegate);
  499. if (!protocol) {
  500. protocol = @protocol(NSURLConnectionDelegate);
  501. }
  502. struct objc_method_description methodDescription = protocol_getMethodDescription(protocol, selector, NO, YES);
  503. typedef void (^NSURLConnectionDidFinishLoadingBlock)(id <NSURLConnectionDelegate> slf, NSURLConnection *connection);
  504. NSURLConnectionDidFinishLoadingBlock undefinedBlock = ^(id <NSURLConnectionDelegate> slf, NSURLConnection *connection) {
  505. [[FLEXNetworkObserver sharedObserver] connectionDidFinishLoading:connection delegate:slf];
  506. };
  507. NSURLConnectionDidFinishLoadingBlock implementationBlock = ^(id <NSURLConnectionDelegate> slf, NSURLConnection *connection) {
  508. [self sniffWithoutDuplicationForObject:connection selector:selector sniffingBlock:^{
  509. undefinedBlock(slf, connection);
  510. } originalImplementationBlock:^{
  511. ((void(*)(id, SEL, id))objc_msgSend)(slf, swizzledSelector, connection);
  512. }];
  513. };
  514. [FLEXUtility replaceImplementationOfSelector:selector withSelector:swizzledSelector forClass:cls withMethodDescription:methodDescription implementationBlock:implementationBlock undefinedBlock:undefinedBlock];
  515. }
  516. + (void)injectDidFailWithErrorIntoDelegateClass:(Class)cls
  517. {
  518. SEL selector = @selector(connection:didFailWithError:);
  519. SEL swizzledSelector = [FLEXUtility swizzledSelectorForSelector:selector];
  520. Protocol *protocol = @protocol(NSURLConnectionDelegate);
  521. struct objc_method_description methodDescription = protocol_getMethodDescription(protocol, selector, NO, YES);
  522. typedef void (^NSURLConnectionDidFailWithErrorBlock)(id <NSURLConnectionDelegate> slf, NSURLConnection *connection, NSError *error);
  523. NSURLConnectionDidFailWithErrorBlock undefinedBlock = ^(id <NSURLConnectionDelegate> slf, NSURLConnection *connection, NSError *error) {
  524. [[FLEXNetworkObserver sharedObserver] connection:connection didFailWithError:error delegate:slf];
  525. };
  526. NSURLConnectionDidFailWithErrorBlock implementationBlock = ^(id <NSURLConnectionDelegate> slf, NSURLConnection *connection, NSError *error) {
  527. [self sniffWithoutDuplicationForObject:connection selector:selector sniffingBlock:^{
  528. undefinedBlock(slf, connection, error);
  529. } originalImplementationBlock:^{
  530. ((void(*)(id, SEL, id, id))objc_msgSend)(slf, swizzledSelector, connection, error);
  531. }];
  532. };
  533. [FLEXUtility replaceImplementationOfSelector:selector withSelector:swizzledSelector forClass:cls withMethodDescription:methodDescription implementationBlock:implementationBlock undefinedBlock:undefinedBlock];
  534. }
  535. + (void)injectTaskWillPerformHTTPRedirectionIntoDelegateClass:(Class)cls
  536. {
  537. SEL selector = @selector(URLSession:task:willPerformHTTPRedirection:newRequest:completionHandler:);
  538. SEL swizzledSelector = [FLEXUtility swizzledSelectorForSelector:selector];
  539. Protocol *protocol = @protocol(NSURLSessionTaskDelegate);
  540. struct objc_method_description methodDescription = protocol_getMethodDescription(protocol, selector, NO, YES);
  541. typedef void (^NSURLSessionWillPerformHTTPRedirectionBlock)(id <NSURLSessionTaskDelegate> slf, NSURLSession *session, NSURLSessionTask *task, NSHTTPURLResponse *response, NSURLRequest *newRequest, void(^completionHandler)(NSURLRequest *));
  542. NSURLSessionWillPerformHTTPRedirectionBlock undefinedBlock = ^(id <NSURLSessionTaskDelegate> slf, NSURLSession *session, NSURLSessionTask *task, NSHTTPURLResponse *response, NSURLRequest *newRequest, void(^completionHandler)(NSURLRequest *)) {
  543. [[FLEXNetworkObserver sharedObserver] URLSession:session task:task willPerformHTTPRedirection:response newRequest:newRequest completionHandler:completionHandler delegate:slf];
  544. completionHandler(newRequest);
  545. };
  546. NSURLSessionWillPerformHTTPRedirectionBlock implementationBlock = ^(id <NSURLSessionTaskDelegate> slf, NSURLSession *session, NSURLSessionTask *task, NSHTTPURLResponse *response, NSURLRequest *newRequest, void(^completionHandler)(NSURLRequest *)) {
  547. [self sniffWithoutDuplicationForObject:session selector:selector sniffingBlock:^{
  548. [[FLEXNetworkObserver sharedObserver] URLSession:session task:task willPerformHTTPRedirection:response newRequest:newRequest completionHandler:completionHandler delegate:slf];
  549. } originalImplementationBlock:^{
  550. ((id(*)(id, SEL, id, id, id, id, void(^)()))objc_msgSend)(slf, swizzledSelector, session, task, response, newRequest, completionHandler);
  551. }];
  552. };
  553. [FLEXUtility replaceImplementationOfSelector:selector withSelector:swizzledSelector forClass:cls withMethodDescription:methodDescription implementationBlock:implementationBlock undefinedBlock:undefinedBlock];
  554. }
  555. + (void)injectTaskDidReceiveDataIntoDelegateClass:(Class)cls
  556. {
  557. SEL selector = @selector(URLSession:dataTask:didReceiveData:);
  558. SEL swizzledSelector = [FLEXUtility swizzledSelectorForSelector:selector];
  559. Protocol *protocol = @protocol(NSURLSessionDataDelegate);
  560. struct objc_method_description methodDescription = protocol_getMethodDescription(protocol, selector, NO, YES);
  561. typedef void (^NSURLSessionDidReceiveDataBlock)(id <NSURLSessionDataDelegate> slf, NSURLSession *session, NSURLSessionDataTask *dataTask, NSData *data);
  562. NSURLSessionDidReceiveDataBlock undefinedBlock = ^(id <NSURLSessionDataDelegate> slf, NSURLSession *session, NSURLSessionDataTask *dataTask, NSData *data) {
  563. [[FLEXNetworkObserver sharedObserver] URLSession:session dataTask:dataTask didReceiveData:data delegate:slf];
  564. };
  565. NSURLSessionDidReceiveDataBlock implementationBlock = ^(id <NSURLSessionDataDelegate> slf, NSURLSession *session, NSURLSessionDataTask *dataTask, NSData *data) {
  566. [self sniffWithoutDuplicationForObject:session selector:selector sniffingBlock:^{
  567. undefinedBlock(slf, session, dataTask, data);
  568. } originalImplementationBlock:^{
  569. ((void(*)(id, SEL, id, id, id))objc_msgSend)(slf, swizzledSelector, session, dataTask, data);
  570. }];
  571. };
  572. [FLEXUtility replaceImplementationOfSelector:selector withSelector:swizzledSelector forClass:cls withMethodDescription:methodDescription implementationBlock:implementationBlock undefinedBlock:undefinedBlock];
  573. }
  574. + (void)injectDataTaskDidBecomeDownloadTaskIntoDelegateClass:(Class)cls
  575. {
  576. SEL selector = @selector(URLSession:dataTask:didBecomeDownloadTask:);
  577. SEL swizzledSelector = [FLEXUtility swizzledSelectorForSelector:selector];
  578. Protocol *protocol = @protocol(NSURLSessionDataDelegate);
  579. struct objc_method_description methodDescription = protocol_getMethodDescription(protocol, selector, NO, YES);
  580. typedef void (^NSURLSessionDidBecomeDownloadTaskBlock)(id <NSURLSessionDataDelegate> slf, NSURLSession *session, NSURLSessionDataTask *dataTask, NSURLSessionDownloadTask *downloadTask);
  581. NSURLSessionDidBecomeDownloadTaskBlock undefinedBlock = ^(id <NSURLSessionDataDelegate> slf, NSURLSession *session, NSURLSessionDataTask *dataTask, NSURLSessionDownloadTask *downloadTask) {
  582. [[FLEXNetworkObserver sharedObserver] URLSession:session dataTask:dataTask didBecomeDownloadTask:downloadTask delegate:slf];
  583. };
  584. NSURLSessionDidBecomeDownloadTaskBlock implementationBlock = ^(id <NSURLSessionDataDelegate> slf, NSURLSession *session, NSURLSessionDataTask *dataTask, NSURLSessionDownloadTask *downloadTask) {
  585. [self sniffWithoutDuplicationForObject:session selector:selector sniffingBlock:^{
  586. undefinedBlock(slf, session, dataTask, downloadTask);
  587. } originalImplementationBlock:^{
  588. ((void(*)(id, SEL, id, id, id))objc_msgSend)(slf, swizzledSelector, session, dataTask, downloadTask);
  589. }];
  590. };
  591. [FLEXUtility replaceImplementationOfSelector:selector withSelector:swizzledSelector forClass:cls withMethodDescription:methodDescription implementationBlock:implementationBlock undefinedBlock:undefinedBlock];
  592. }
  593. + (void)injectTaskDidReceiveResponseIntoDelegateClass:(Class)cls
  594. {
  595. SEL selector = @selector(URLSession:dataTask:didReceiveResponse:completionHandler:);
  596. SEL swizzledSelector = [FLEXUtility swizzledSelectorForSelector:selector];
  597. Protocol *protocol = @protocol(NSURLSessionDataDelegate);
  598. struct objc_method_description methodDescription = protocol_getMethodDescription(protocol, selector, NO, YES);
  599. typedef void (^NSURLSessionDidReceiveResponseBlock)(id <NSURLSessionDelegate> slf, NSURLSession *session, NSURLSessionDataTask *dataTask, NSURLResponse *response, void(^completionHandler)(NSURLSessionResponseDisposition disposition));
  600. NSURLSessionDidReceiveResponseBlock undefinedBlock = ^(id <NSURLSessionDelegate> slf, NSURLSession *session, NSURLSessionDataTask *dataTask, NSURLResponse *response, void(^completionHandler)(NSURLSessionResponseDisposition disposition)) {
  601. [[FLEXNetworkObserver sharedObserver] URLSession:session dataTask:dataTask didReceiveResponse:response completionHandler:completionHandler delegate:slf];
  602. completionHandler(NSURLSessionResponseAllow);
  603. };
  604. NSURLSessionDidReceiveResponseBlock implementationBlock = ^(id <NSURLSessionDelegate> slf, NSURLSession *session, NSURLSessionDataTask *dataTask, NSURLResponse *response, void(^completionHandler)(NSURLSessionResponseDisposition disposition)) {
  605. [self sniffWithoutDuplicationForObject:session selector:selector sniffingBlock:^{
  606. [[FLEXNetworkObserver sharedObserver] URLSession:session dataTask:dataTask didReceiveResponse:response completionHandler:completionHandler delegate:slf];
  607. } originalImplementationBlock:^{
  608. ((void(*)(id, SEL, id, id, id, void(^)()))objc_msgSend)(slf, swizzledSelector, session, dataTask, response, completionHandler);
  609. }];
  610. };
  611. [FLEXUtility replaceImplementationOfSelector:selector withSelector:swizzledSelector forClass:cls withMethodDescription:methodDescription implementationBlock:implementationBlock undefinedBlock:undefinedBlock];
  612. }
  613. + (void)injectTaskDidCompleteWithErrorIntoDelegateClass:(Class)cls
  614. {
  615. SEL selector = @selector(URLSession:task:didCompleteWithError:);
  616. SEL swizzledSelector = [FLEXUtility swizzledSelectorForSelector:selector];
  617. Protocol *protocol = @protocol(NSURLSessionTaskDelegate);
  618. struct objc_method_description methodDescription = protocol_getMethodDescription(protocol, selector, NO, YES);
  619. typedef void (^NSURLSessionTaskDidCompleteWithErrorBlock)(id <NSURLSessionTaskDelegate> slf, NSURLSession *session, NSURLSessionTask *task, NSError *error);
  620. NSURLSessionTaskDidCompleteWithErrorBlock undefinedBlock = ^(id <NSURLSessionTaskDelegate> slf, NSURLSession *session, NSURLSessionTask *task, NSError *error) {
  621. [[FLEXNetworkObserver sharedObserver] URLSession:session task:task didCompleteWithError:error delegate:slf];
  622. };
  623. NSURLSessionTaskDidCompleteWithErrorBlock implementationBlock = ^(id <NSURLSessionTaskDelegate> slf, NSURLSession *session, NSURLSessionTask *task, NSError *error) {
  624. [self sniffWithoutDuplicationForObject:session selector:selector sniffingBlock:^{
  625. undefinedBlock(slf, session, task, error);
  626. } originalImplementationBlock:^{
  627. ((void(*)(id, SEL, id, id, id))objc_msgSend)(slf, swizzledSelector, session, task, error);
  628. }];
  629. };
  630. [FLEXUtility replaceImplementationOfSelector:selector withSelector:swizzledSelector forClass:cls withMethodDescription:methodDescription implementationBlock:implementationBlock undefinedBlock:undefinedBlock];
  631. }
  632. // Used for overriding AFNetworking behavior
  633. + (void)injectRespondsToSelectorIntoDelegateClass:(Class)cls
  634. {
  635. SEL selector = @selector(respondsToSelector:);
  636. SEL swizzledSelector = [FLEXUtility swizzledSelectorForSelector:selector];
  637. //Protocol *protocol = @protocol(NSURLSessionTaskDelegate);
  638. Method method = class_getInstanceMethod(cls, selector);
  639. struct objc_method_description methodDescription = *method_getDescription(method);
  640. BOOL (^undefinedBlock)(id <NSURLSessionTaskDelegate>, SEL) = ^(id slf, SEL sel) {
  641. return YES;
  642. };
  643. BOOL (^implementationBlock)(id <NSURLSessionTaskDelegate>, SEL) = ^(id <NSURLSessionTaskDelegate> slf, SEL sel) {
  644. if (sel == @selector(URLSession:dataTask:didReceiveResponse:completionHandler:)) {
  645. return undefinedBlock(slf, sel);
  646. }
  647. return ((BOOL(*)(id, SEL, SEL))objc_msgSend)(slf, swizzledSelector, sel);
  648. };
  649. [FLEXUtility replaceImplementationOfSelector:selector withSelector:swizzledSelector forClass:cls withMethodDescription:methodDescription implementationBlock:implementationBlock undefinedBlock:undefinedBlock];
  650. }
  651. + (void)injectDownloadTaskDidFinishDownloadingIntoDelegateClass:(Class)cls
  652. {
  653. SEL selector = @selector(URLSession:downloadTask:didFinishDownloadingToURL:);
  654. SEL swizzledSelector = [FLEXUtility swizzledSelectorForSelector:selector];
  655. Protocol *protocol = @protocol(NSURLSessionDownloadDelegate);
  656. struct objc_method_description methodDescription = protocol_getMethodDescription(protocol, selector, NO, YES);
  657. typedef void (^NSURLSessionDownloadTaskDidFinishDownloadingBlock)(id <NSURLSessionTaskDelegate> slf, NSURLSession *session, NSURLSessionDownloadTask *task, NSURL *location);
  658. NSURLSessionDownloadTaskDidFinishDownloadingBlock undefinedBlock = ^(id <NSURLSessionTaskDelegate> slf, NSURLSession *session, NSURLSessionDownloadTask *task, NSURL *location) {
  659. NSData *data = [NSData dataWithContentsOfFile:location.relativePath];
  660. [[FLEXNetworkObserver sharedObserver] URLSession:session task:task didFinishDownloadingToURL:location data:data delegate:slf];
  661. };
  662. NSURLSessionDownloadTaskDidFinishDownloadingBlock implementationBlock = ^(id <NSURLSessionTaskDelegate> slf, NSURLSession *session, NSURLSessionDownloadTask *task, NSURL *location) {
  663. [self sniffWithoutDuplicationForObject:session selector:selector sniffingBlock:^{
  664. undefinedBlock(slf, session, task, location);
  665. } originalImplementationBlock:^{
  666. ((void(*)(id, SEL, id, id, id))objc_msgSend)(slf, swizzledSelector, session, task, location);
  667. }];
  668. };
  669. [FLEXUtility replaceImplementationOfSelector:selector withSelector:swizzledSelector forClass:cls withMethodDescription:methodDescription implementationBlock:implementationBlock undefinedBlock:undefinedBlock];
  670. }
  671. + (void)injectDownloadTaskDidWriteDataIntoDelegateClass:(Class)cls
  672. {
  673. SEL selector = @selector(URLSession:downloadTask:didWriteData:totalBytesWritten:totalBytesExpectedToWrite:);
  674. SEL swizzledSelector = [FLEXUtility swizzledSelectorForSelector:selector];
  675. Protocol *protocol = @protocol(NSURLSessionDownloadDelegate);
  676. struct objc_method_description methodDescription = protocol_getMethodDescription(protocol, selector, NO, YES);
  677. typedef void (^NSURLSessionDownloadTaskDidWriteDataBlock)(id <NSURLSessionTaskDelegate> slf, NSURLSession *session, NSURLSessionDownloadTask *task, int64_t bytesWritten, int64_t totalBytesWritten, int64_t totalBytesExpectedToWrite);
  678. NSURLSessionDownloadTaskDidWriteDataBlock undefinedBlock = ^(id <NSURLSessionTaskDelegate> slf, NSURLSession *session, NSURLSessionDownloadTask *task, int64_t bytesWritten, int64_t totalBytesWritten, int64_t totalBytesExpectedToWrite) {
  679. [[FLEXNetworkObserver sharedObserver] URLSession:session downloadTask:task didWriteData:bytesWritten totalBytesWritten:totalBytesWritten totalBytesExpectedToWrite:totalBytesExpectedToWrite delegate:slf];
  680. };
  681. NSURLSessionDownloadTaskDidWriteDataBlock implementationBlock = ^(id <NSURLSessionTaskDelegate> slf, NSURLSession *session, NSURLSessionDownloadTask *task, int64_t bytesWritten, int64_t totalBytesWritten, int64_t totalBytesExpectedToWrite) {
  682. [self sniffWithoutDuplicationForObject:session selector:selector sniffingBlock:^{
  683. undefinedBlock(slf, session, task, bytesWritten, totalBytesWritten, totalBytesExpectedToWrite);
  684. } originalImplementationBlock:^{
  685. ((void(*)(id, SEL, id, id, int64_t, int64_t, int64_t))objc_msgSend)(slf, swizzledSelector, session, task, bytesWritten, totalBytesWritten, totalBytesExpectedToWrite);
  686. }];
  687. };
  688. [FLEXUtility replaceImplementationOfSelector:selector withSelector:swizzledSelector forClass:cls withMethodDescription:methodDescription implementationBlock:implementationBlock undefinedBlock:undefinedBlock];
  689. }
  690. static char const * const kFLEXRequestIDKey = "kFLEXRequestIDKey";
  691. + (NSString *)requestIDForConnectionOrTask:(id)connectionOrTask
  692. {
  693. NSString *requestID = objc_getAssociatedObject(connectionOrTask, kFLEXRequestIDKey);
  694. if (!requestID) {
  695. requestID = [self nextRequestID];
  696. [self setRequestID:requestID forConnectionOrTask:connectionOrTask];
  697. }
  698. return requestID;
  699. }
  700. + (void)setRequestID:(NSString *)requestID forConnectionOrTask:(id)connectionOrTask
  701. {
  702. objc_setAssociatedObject(connectionOrTask, kFLEXRequestIDKey, requestID, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
  703. }
  704. #pragma mark - Initialization
  705. - (id)init
  706. {
  707. self = [super init];
  708. if (self) {
  709. self.requestStatesForRequestIDs = [[NSMutableDictionary alloc] init];
  710. self.queue = dispatch_queue_create("com.flex.FLEXNetworkObserver", DISPATCH_QUEUE_SERIAL);
  711. }
  712. return self;
  713. }
  714. #pragma mark - Private Methods
  715. - (void)performBlock:(dispatch_block_t)block
  716. {
  717. if ([[self class] isEnabled]) {
  718. dispatch_async(_queue, block);
  719. }
  720. }
  721. - (FLEXInternalRequestState *)requestStateForRequestID:(NSString *)requestID
  722. {
  723. FLEXInternalRequestState *requestState = self.requestStatesForRequestIDs[requestID];
  724. if (!requestState) {
  725. requestState = [[FLEXInternalRequestState alloc] init];
  726. [self.requestStatesForRequestIDs setObject:requestState forKey:requestID];
  727. }
  728. return requestState;
  729. }
  730. - (void)removeRequestStateForRequestID:(NSString *)requestID
  731. {
  732. [self.requestStatesForRequestIDs removeObjectForKey:requestID];
  733. }
  734. @end
  735. @implementation FLEXNetworkObserver (NSURLConnectionHelpers)
  736. - (void)connection:(NSURLConnection *)connection willSendRequest:(NSURLRequest *)request redirectResponse:(NSURLResponse *)response delegate:(id<NSURLConnectionDelegate>)delegate
  737. {
  738. [self performBlock:^{
  739. NSString *requestID = [[self class] requestIDForConnectionOrTask:connection];
  740. FLEXInternalRequestState *requestState = [self requestStateForRequestID:requestID];
  741. requestState.request = request;
  742. [[FLEXNetworkRecorder defaultRecorder] recordRequestWillBeSentWithRequestID:requestID request:request redirectResponse:response];
  743. NSString *mechanism = [NSString stringWithFormat:@"NSURLConnection (delegate: %@)", [delegate class]];
  744. [[FLEXNetworkRecorder defaultRecorder] recordMechanism:mechanism forRequestID:requestID];
  745. }];
  746. }
  747. - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response delegate:(id<NSURLConnectionDelegate>)delegate
  748. {
  749. [self performBlock:^{
  750. NSString *requestID = [[self class] requestIDForConnectionOrTask:connection];
  751. FLEXInternalRequestState *requestState = [self requestStateForRequestID:requestID];
  752. NSMutableData *dataAccumulator = nil;
  753. if (response.expectedContentLength < 0) {
  754. dataAccumulator = [[NSMutableData alloc] init];
  755. } else {
  756. dataAccumulator = [[NSMutableData alloc] initWithCapacity:(NSUInteger)response.expectedContentLength];
  757. }
  758. requestState.dataAccumulator = dataAccumulator;
  759. [[FLEXNetworkRecorder defaultRecorder] recordResponseReceivedWithRequestID:requestID response:response];
  760. }];
  761. }
  762. - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data delegate:(id<NSURLConnectionDelegate>)delegate
  763. {
  764. // Just to be safe since we're doing this async
  765. data = [data copy];
  766. [self performBlock:^{
  767. NSString *requestID = [[self class] requestIDForConnectionOrTask:connection];
  768. FLEXInternalRequestState *requestState = [self requestStateForRequestID:requestID];
  769. [requestState.dataAccumulator appendData:data];
  770. [[FLEXNetworkRecorder defaultRecorder] recordDataReceivedWithRequestID:requestID dataLength:data.length];
  771. }];
  772. }
  773. - (void)connectionDidFinishLoading:(NSURLConnection *)connection delegate:(id<NSURLConnectionDelegate>)delegate
  774. {
  775. [self performBlock:^{
  776. NSString *requestID = [[self class] requestIDForConnectionOrTask:connection];
  777. FLEXInternalRequestState *requestState = [self requestStateForRequestID:requestID];
  778. [[FLEXNetworkRecorder defaultRecorder] recordLoadingFinishedWithRequestID:requestID responseBody:requestState.dataAccumulator];
  779. [self removeRequestStateForRequestID:requestID];
  780. }];
  781. }
  782. - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error delegate:(id<NSURLConnectionDelegate>)delegate
  783. {
  784. [self performBlock:^{
  785. NSString *requestID = [[self class] requestIDForConnectionOrTask:connection];
  786. FLEXInternalRequestState *requestState = [self requestStateForRequestID:requestID];
  787. // Cancellations can occur prior to the willSendRequest:... NSURLConnection delegate call.
  788. // These are pretty common and clutter up the logs. Only record the failure if the recorder already knows about the request through willSendRequest:...
  789. if (requestState.request) {
  790. [[FLEXNetworkRecorder defaultRecorder] recordLoadingFailedWithRequestID:requestID error:error];
  791. }
  792. [self removeRequestStateForRequestID:requestID];
  793. }];
  794. }
  795. - (void)connectionWillCancel:(NSURLConnection *)connection
  796. {
  797. [self performBlock:^{
  798. // Mimic the behavior of NSURLSession which is to create an error on cancellation.
  799. NSDictionary *userInfo = @{ NSLocalizedDescriptionKey : @"cancelled" };
  800. NSError *error = [NSError errorWithDomain:NSURLErrorDomain code:NSURLErrorCancelled userInfo:userInfo];
  801. [self connection:connection didFailWithError:error delegate:nil];
  802. }];
  803. }
  804. @end
  805. @implementation FLEXNetworkObserver (NSURLSessionTaskHelpers)
  806. - (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task willPerformHTTPRedirection:(NSHTTPURLResponse *)response newRequest:(NSURLRequest *)request completionHandler:(void (^)(NSURLRequest *))completionHandler delegate:(id<NSURLSessionDelegate>)delegate
  807. {
  808. [self performBlock:^{
  809. NSString *requestID = [[self class] requestIDForConnectionOrTask:task];
  810. [[FLEXNetworkRecorder defaultRecorder] recordRequestWillBeSentWithRequestID:requestID request:request redirectResponse:response];
  811. }];
  812. }
  813. - (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didReceiveResponse:(NSURLResponse *)response completionHandler:(void (^)(NSURLSessionResponseDisposition disposition))completionHandler delegate:(id<NSURLSessionDelegate>)delegate
  814. {
  815. [self performBlock:^{
  816. NSString *requestID = [[self class] requestIDForConnectionOrTask:dataTask];
  817. FLEXInternalRequestState *requestState = [self requestStateForRequestID:requestID];
  818. NSMutableData *dataAccumulator = nil;
  819. if (response.expectedContentLength < 0) {
  820. dataAccumulator = [[NSMutableData alloc] init];
  821. } else {
  822. dataAccumulator = [[NSMutableData alloc] initWithCapacity:(NSUInteger)response.expectedContentLength];
  823. }
  824. requestState.dataAccumulator = dataAccumulator;
  825. NSString *requestMechanism = [NSString stringWithFormat:@"NSURLSessionDataTask (delegate: %@)", [delegate class]];
  826. [[FLEXNetworkRecorder defaultRecorder] recordMechanism:requestMechanism forRequestID:requestID];
  827. [[FLEXNetworkRecorder defaultRecorder] recordResponseReceivedWithRequestID:requestID response:response];
  828. }];
  829. }
  830. - (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didBecomeDownloadTask:(NSURLSessionDownloadTask *)downloadTask delegate:(id<NSURLSessionDelegate>)delegate
  831. {
  832. [self performBlock:^{
  833. // By setting the request ID of the download task to match the data task,
  834. // it can pick up where the data task left off.
  835. NSString *requestID = [[self class] requestIDForConnectionOrTask:dataTask];
  836. [[self class] setRequestID:requestID forConnectionOrTask:downloadTask];
  837. }];
  838. }
  839. - (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didReceiveData:(NSData *)data delegate:(id<NSURLSessionDelegate>)delegate
  840. {
  841. // Just to be safe since we're doing this async
  842. data = [data copy];
  843. [self performBlock:^{
  844. NSString *requestID = [[self class] requestIDForConnectionOrTask:dataTask];
  845. FLEXInternalRequestState *requestState = [self requestStateForRequestID:requestID];
  846. [requestState.dataAccumulator appendData:data];
  847. [[FLEXNetworkRecorder defaultRecorder] recordDataReceivedWithRequestID:requestID dataLength:data.length];
  848. }];
  849. }
  850. - (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(NSError *)error delegate:(id<NSURLSessionDelegate>)delegate
  851. {
  852. [self performBlock:^{
  853. NSString *requestID = [[self class] requestIDForConnectionOrTask:task];
  854. FLEXInternalRequestState *requestState = [self requestStateForRequestID:requestID];
  855. if (error) {
  856. [[FLEXNetworkRecorder defaultRecorder] recordLoadingFailedWithRequestID:requestID error:error];
  857. } else {
  858. [[FLEXNetworkRecorder defaultRecorder] recordLoadingFinishedWithRequestID:requestID responseBody:requestState.dataAccumulator];
  859. }
  860. [self removeRequestStateForRequestID:requestID];
  861. }];
  862. }
  863. - (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didWriteData:(int64_t)bytesWritten totalBytesWritten:(int64_t)totalBytesWritten totalBytesExpectedToWrite:(int64_t)totalBytesExpectedToWrite delegate:(id<NSURLSessionDelegate>)delegate
  864. {
  865. [self performBlock:^{
  866. NSString *requestID = [[self class] requestIDForConnectionOrTask:downloadTask];
  867. FLEXInternalRequestState *requestState = [self requestStateForRequestID:requestID];
  868. if (!requestState.dataAccumulator) {
  869. NSUInteger unsignedBytesExpectedToWrite = totalBytesExpectedToWrite > 0 ? (NSUInteger)totalBytesExpectedToWrite : 0;
  870. requestState.dataAccumulator = [[NSMutableData alloc] initWithCapacity:unsignedBytesExpectedToWrite];
  871. [[FLEXNetworkRecorder defaultRecorder] recordResponseReceivedWithRequestID:requestID response:downloadTask.response];
  872. NSString *requestMechanism = [NSString stringWithFormat:@"NSURLSessionDownloadTask (delegate: %@)", [delegate class]];
  873. [[FLEXNetworkRecorder defaultRecorder] recordMechanism:requestMechanism forRequestID:requestID];
  874. }
  875. [[FLEXNetworkRecorder defaultRecorder] recordDataReceivedWithRequestID:requestID dataLength:bytesWritten];
  876. }];
  877. }
  878. - (void)URLSession:(NSURLSession *)session task:(NSURLSessionDownloadTask *)downloadTask didFinishDownloadingToURL:(NSURL *)location data:(NSData *)data delegate:(id<NSURLSessionDelegate>)delegate
  879. {
  880. data = [data copy];
  881. [self performBlock:^{
  882. NSString *requestID = [[self class] requestIDForConnectionOrTask:downloadTask];
  883. FLEXInternalRequestState *requestState = [self requestStateForRequestID:requestID];
  884. [requestState.dataAccumulator appendData:data];
  885. }];
  886. }
  887. - (void)URLSessionTaskWillResume:(NSURLSessionTask *)task
  888. {
  889. // Since resume can be called multiple times on the same task, only treat the first resume as
  890. // the equivalent to connection:willSendRequest:...
  891. [self performBlock:^{
  892. NSString *requestID = [[self class] requestIDForConnectionOrTask:task];
  893. FLEXInternalRequestState *requestState = [self requestStateForRequestID:requestID];
  894. if (!requestState.request) {
  895. requestState.request = task.currentRequest;
  896. [[FLEXNetworkRecorder defaultRecorder] recordRequestWillBeSentWithRequestID:requestID request:task.currentRequest redirectResponse:nil];
  897. }
  898. }];
  899. }
  900. @end