You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1130 lines
58 KiB

  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) 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) NSMutableDictionary<NSString *, FLEXInternalRequestState *> *requestStatesForRequestIDs;
  50. @property (nonatomic) 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 new];
  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 interference 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 implementation 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:),
  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. // iOS's internal HTTP parser finalization code is mysteriously not thread safe,
  237. // invoke it asynchronously has a chance to cause a `double free` crash.
  238. // This line below will ask for HTTPBody synchronously, make the HTTPParser parse the request and cache them in advance,
  239. // After that the HTTPParser will be finalized,
  240. // make sure other threads inspecting the request won't trigger a race to finalize the parser.
  241. [slf.currentRequest HTTPBody];
  242. [[FLEXNetworkObserver sharedObserver] URLSessionTaskWillResume:slf];
  243. ((void(*)(id, SEL))objc_msgSend)(slf, swizzledSelector);
  244. };
  245. IMP implementation = imp_implementationWithBlock(swizzleBlock);
  246. class_addMethod(class, swizzledSelector, implementation, method_getTypeEncoding(originalResume));
  247. Method newResume = class_getInstanceMethod(class, swizzledSelector);
  248. method_exchangeImplementations(originalResume, newResume);
  249. });
  250. }
  251. + (void)injectIntoNSURLConnectionAsynchronousClassMethod
  252. {
  253. static dispatch_once_t onceToken;
  254. dispatch_once(&onceToken, ^{
  255. Class class = objc_getMetaClass(class_getName([NSURLConnection class]));
  256. SEL selector = @selector(sendAsynchronousRequest:queue:completionHandler:);
  257. SEL swizzledSelector = [FLEXUtility swizzledSelectorForSelector:selector];
  258. typedef void (^NSURLConnectionAsyncCompletion)(NSURLResponse* response, NSData* data, NSError* connectionError);
  259. void (^asyncSwizzleBlock)(Class, NSURLRequest *, NSOperationQueue *, NSURLConnectionAsyncCompletion) = ^(Class slf, NSURLRequest *request, NSOperationQueue *queue, NSURLConnectionAsyncCompletion completion) {
  260. if ([FLEXNetworkObserver isEnabled]) {
  261. NSString *requestID = [self nextRequestID];
  262. [[FLEXNetworkRecorder defaultRecorder] recordRequestWillBeSentWithRequestID:requestID request:request redirectResponse:nil];
  263. NSString *mechanism = [self mechanismFromClassMethod:selector onClass:class];
  264. [[FLEXNetworkRecorder defaultRecorder] recordMechanism:mechanism forRequestID:requestID];
  265. NSURLConnectionAsyncCompletion completionWrapper = ^(NSURLResponse *response, NSData *data, NSError *connectionError) {
  266. [[FLEXNetworkRecorder defaultRecorder] recordResponseReceivedWithRequestID:requestID response:response];
  267. [[FLEXNetworkRecorder defaultRecorder] recordDataReceivedWithRequestID:requestID dataLength:data.length];
  268. if (connectionError) {
  269. [[FLEXNetworkRecorder defaultRecorder] recordLoadingFailedWithRequestID:requestID error:connectionError];
  270. } else {
  271. [[FLEXNetworkRecorder defaultRecorder] recordLoadingFinishedWithRequestID:requestID responseBody:data];
  272. }
  273. // Call through to the original completion handler
  274. if (completion) {
  275. completion(response, data, connectionError);
  276. }
  277. };
  278. ((void(*)(id, SEL, id, id, id))objc_msgSend)(slf, swizzledSelector, request, queue, completionWrapper);
  279. } else {
  280. ((void(*)(id, SEL, id, id, id))objc_msgSend)(slf, swizzledSelector, request, queue, completion);
  281. }
  282. };
  283. [FLEXUtility replaceImplementationOfKnownSelector:selector onClass:class withBlock:asyncSwizzleBlock swizzledSelector:swizzledSelector];
  284. });
  285. }
  286. + (void)injectIntoNSURLConnectionSynchronousClassMethod
  287. {
  288. static dispatch_once_t onceToken;
  289. dispatch_once(&onceToken, ^{
  290. Class class = objc_getMetaClass(class_getName([NSURLConnection class]));
  291. SEL selector = @selector(sendSynchronousRequest:returningResponse:error:);
  292. SEL swizzledSelector = [FLEXUtility swizzledSelectorForSelector:selector];
  293. NSData *(^syncSwizzleBlock)(Class, NSURLRequest *, NSURLResponse **, NSError **) = ^NSData *(Class slf, NSURLRequest *request, NSURLResponse **response, NSError **error) {
  294. NSData *data = nil;
  295. if ([FLEXNetworkObserver isEnabled]) {
  296. NSString *requestID = [self nextRequestID];
  297. [[FLEXNetworkRecorder defaultRecorder] recordRequestWillBeSentWithRequestID:requestID request:request redirectResponse:nil];
  298. NSString *mechanism = [self mechanismFromClassMethod:selector onClass:class];
  299. [[FLEXNetworkRecorder defaultRecorder] recordMechanism:mechanism forRequestID:requestID];
  300. NSError *temporaryError = nil;
  301. NSURLResponse *temporaryResponse = nil;
  302. data = ((id(*)(id, SEL, id, NSURLResponse **, NSError **))objc_msgSend)(slf, swizzledSelector, request, &temporaryResponse, &temporaryError);
  303. [[FLEXNetworkRecorder defaultRecorder] recordResponseReceivedWithRequestID:requestID response:temporaryResponse];
  304. [[FLEXNetworkRecorder defaultRecorder] recordDataReceivedWithRequestID:requestID dataLength:data.length];
  305. if (temporaryError) {
  306. [[FLEXNetworkRecorder defaultRecorder] recordLoadingFailedWithRequestID:requestID error:temporaryError];
  307. } else {
  308. [[FLEXNetworkRecorder defaultRecorder] recordLoadingFinishedWithRequestID:requestID responseBody:data];
  309. }
  310. if (error) {
  311. *error = temporaryError;
  312. }
  313. if (response) {
  314. *response = temporaryResponse;
  315. }
  316. } else {
  317. data = ((id(*)(id, SEL, id, NSURLResponse **, NSError **))objc_msgSend)(slf, swizzledSelector, request, response, error);
  318. }
  319. return data;
  320. };
  321. [FLEXUtility replaceImplementationOfKnownSelector:selector onClass:class withBlock:syncSwizzleBlock swizzledSelector:swizzledSelector];
  322. });
  323. }
  324. + (void)injectIntoNSURLSessionAsyncDataAndDownloadTaskMethods
  325. {
  326. static dispatch_once_t onceToken;
  327. dispatch_once(&onceToken, ^{
  328. Class class = [NSURLSession class];
  329. // The method signatures here are close enough that we can use the same logic to inject into all of them.
  330. const SEL selectors[] = {
  331. @selector(dataTaskWithRequest:completionHandler:),
  332. @selector(dataTaskWithURL:completionHandler:),
  333. @selector(downloadTaskWithRequest:completionHandler:),
  334. @selector(downloadTaskWithResumeData:completionHandler:),
  335. @selector(downloadTaskWithURL:completionHandler:)
  336. };
  337. const int numSelectors = sizeof(selectors) / sizeof(SEL);
  338. for (int selectorIndex = 0; selectorIndex < numSelectors; selectorIndex++) {
  339. SEL selector = selectors[selectorIndex];
  340. SEL swizzledSelector = [FLEXUtility swizzledSelectorForSelector:selector];
  341. if ([FLEXUtility instanceRespondsButDoesNotImplementSelector:selector class:class]) {
  342. // iOS 7 does not implement these methods on NSURLSession. We actually want to
  343. // swizzle __NSCFURLSession, which we can get from the class of the shared session
  344. class = [NSURLSession.sharedSession class];
  345. }
  346. NSURLSessionTask *(^asyncDataOrDownloadSwizzleBlock)(Class, id, NSURLSessionAsyncCompletion) = ^NSURLSessionTask *(Class slf, id argument, NSURLSessionAsyncCompletion completion) {
  347. NSURLSessionTask *task = nil;
  348. // If completion block was not provided sender expect to receive delegated methods or does not
  349. // interested in callback at all. In this case we should just call original method implementation
  350. // with nil completion block.
  351. if ([FLEXNetworkObserver isEnabled] && completion) {
  352. NSString *requestID = [self nextRequestID];
  353. NSString *mechanism = [self mechanismFromClassMethod:selector onClass:class];
  354. NSURLSessionAsyncCompletion completionWrapper = [self asyncCompletionWrapperForRequestID:requestID mechanism:mechanism completion:completion];
  355. task = ((id(*)(id, SEL, id, id))objc_msgSend)(slf, swizzledSelector, argument, completionWrapper);
  356. [self setRequestID:requestID forConnectionOrTask:task];
  357. } else {
  358. task = ((id(*)(id, SEL, id, id))objc_msgSend)(slf, swizzledSelector, argument, completion);
  359. }
  360. return task;
  361. };
  362. [FLEXUtility replaceImplementationOfKnownSelector:selector onClass:class withBlock:asyncDataOrDownloadSwizzleBlock swizzledSelector:swizzledSelector];
  363. }
  364. });
  365. }
  366. + (void)injectIntoNSURLSessionAsyncUploadTaskMethods
  367. {
  368. static dispatch_once_t onceToken;
  369. dispatch_once(&onceToken, ^{
  370. Class class = [NSURLSession class];
  371. // The method signatures here are close enough that we can use the same logic to inject into both of them.
  372. // Note that they have 3 arguments, so we can't easily combine with the data and download method above.
  373. const SEL selectors[] = {
  374. @selector(uploadTaskWithRequest:fromData:completionHandler:),
  375. @selector(uploadTaskWithRequest:fromFile:completionHandler:)
  376. };
  377. const int numSelectors = sizeof(selectors) / sizeof(SEL);
  378. for (int selectorIndex = 0; selectorIndex < numSelectors; selectorIndex++) {
  379. SEL selector = selectors[selectorIndex];
  380. SEL swizzledSelector = [FLEXUtility swizzledSelectorForSelector:selector];
  381. if ([FLEXUtility instanceRespondsButDoesNotImplementSelector:selector class:class]) {
  382. // iOS 7 does not implement these methods on NSURLSession. We actually want to
  383. // swizzle __NSCFURLSession, which we can get from the class of the shared session
  384. class = [NSURLSession.sharedSession class];
  385. }
  386. NSURLSessionUploadTask *(^asyncUploadTaskSwizzleBlock)(Class, NSURLRequest *, id, NSURLSessionAsyncCompletion) = ^NSURLSessionUploadTask *(Class slf, NSURLRequest *request, id argument, NSURLSessionAsyncCompletion completion) {
  387. NSURLSessionUploadTask *task = nil;
  388. if ([FLEXNetworkObserver isEnabled] && completion) {
  389. NSString *requestID = [self nextRequestID];
  390. NSString *mechanism = [self mechanismFromClassMethod:selector onClass:class];
  391. NSURLSessionAsyncCompletion completionWrapper = [self asyncCompletionWrapperForRequestID:requestID mechanism:mechanism completion:completion];
  392. task = ((id(*)(id, SEL, id, id, id))objc_msgSend)(slf, swizzledSelector, request, argument, completionWrapper);
  393. [self setRequestID:requestID forConnectionOrTask:task];
  394. } else {
  395. task = ((id(*)(id, SEL, id, id, id))objc_msgSend)(slf, swizzledSelector, request, argument, completion);
  396. }
  397. return task;
  398. };
  399. [FLEXUtility replaceImplementationOfKnownSelector:selector onClass:class withBlock:asyncUploadTaskSwizzleBlock swizzledSelector:swizzledSelector];
  400. }
  401. });
  402. }
  403. + (NSString *)mechanismFromClassMethod:(SEL)selector onClass:(Class)class
  404. {
  405. return [NSString stringWithFormat:@"+[%@ %@]", NSStringFromClass(class), NSStringFromSelector(selector)];
  406. }
  407. + (NSURLSessionAsyncCompletion)asyncCompletionWrapperForRequestID:(NSString *)requestID mechanism:(NSString *)mechanism completion:(NSURLSessionAsyncCompletion)completion
  408. {
  409. NSURLSessionAsyncCompletion completionWrapper = ^(id fileURLOrData, NSURLResponse *response, NSError *error) {
  410. [[FLEXNetworkRecorder defaultRecorder] recordMechanism:mechanism forRequestID:requestID];
  411. [[FLEXNetworkRecorder defaultRecorder] recordResponseReceivedWithRequestID:requestID response:response];
  412. NSData *data = nil;
  413. if ([fileURLOrData isKindOfClass:[NSURL class]]) {
  414. data = [NSData dataWithContentsOfURL:fileURLOrData];
  415. } else if ([fileURLOrData isKindOfClass:[NSData class]]) {
  416. data = fileURLOrData;
  417. }
  418. [[FLEXNetworkRecorder defaultRecorder] recordDataReceivedWithRequestID:requestID dataLength:data.length];
  419. if (error) {
  420. [[FLEXNetworkRecorder defaultRecorder] recordLoadingFailedWithRequestID:requestID error:error];
  421. } else {
  422. [[FLEXNetworkRecorder defaultRecorder] recordLoadingFinishedWithRequestID:requestID responseBody:data];
  423. }
  424. // Call through to the original completion handler
  425. if (completion) {
  426. completion(fileURLOrData, response, error);
  427. }
  428. };
  429. return completionWrapper;
  430. }
  431. + (void)injectWillSendRequestIntoDelegateClass:(Class)cls
  432. {
  433. SEL selector = @selector(connection:willSendRequest:redirectResponse:);
  434. SEL swizzledSelector = [FLEXUtility swizzledSelectorForSelector:selector];
  435. Protocol *protocol = @protocol(NSURLConnectionDataDelegate);
  436. if (!protocol) {
  437. protocol = @protocol(NSURLConnectionDelegate);
  438. }
  439. struct objc_method_description methodDescription = protocol_getMethodDescription(protocol, selector, NO, YES);
  440. typedef NSURLRequest *(^NSURLConnectionWillSendRequestBlock)(id <NSURLConnectionDelegate> slf, NSURLConnection *connection, NSURLRequest *request, NSURLResponse *response);
  441. NSURLConnectionWillSendRequestBlock undefinedBlock = ^NSURLRequest *(id <NSURLConnectionDelegate> slf, NSURLConnection *connection, NSURLRequest *request, NSURLResponse *response) {
  442. [[FLEXNetworkObserver sharedObserver] connection:connection willSendRequest:request redirectResponse:response delegate:slf];
  443. return request;
  444. };
  445. NSURLConnectionWillSendRequestBlock implementationBlock = ^NSURLRequest *(id <NSURLConnectionDelegate> slf, NSURLConnection *connection, NSURLRequest *request, NSURLResponse *response) {
  446. __block NSURLRequest *returnValue = nil;
  447. [self sniffWithoutDuplicationForObject:connection selector:selector sniffingBlock:^{
  448. undefinedBlock(slf, connection, request, response);
  449. } originalImplementationBlock:^{
  450. returnValue = ((id(*)(id, SEL, id, id, id))objc_msgSend)(slf, swizzledSelector, connection, request, response);
  451. }];
  452. return returnValue;
  453. };
  454. [FLEXUtility replaceImplementationOfSelector:selector withSelector:swizzledSelector forClass:cls withMethodDescription:methodDescription implementationBlock:implementationBlock undefinedBlock:undefinedBlock];
  455. }
  456. + (void)injectDidReceiveResponseIntoDelegateClass:(Class)cls
  457. {
  458. SEL selector = @selector(connection:didReceiveResponse:);
  459. SEL swizzledSelector = [FLEXUtility swizzledSelectorForSelector:selector];
  460. Protocol *protocol = @protocol(NSURLConnectionDataDelegate);
  461. if (!protocol) {
  462. protocol = @protocol(NSURLConnectionDelegate);
  463. }
  464. struct objc_method_description methodDescription = protocol_getMethodDescription(protocol, selector, NO, YES);
  465. typedef void (^NSURLConnectionDidReceiveResponseBlock)(id <NSURLConnectionDelegate> slf, NSURLConnection *connection, NSURLResponse *response);
  466. NSURLConnectionDidReceiveResponseBlock undefinedBlock = ^(id <NSURLConnectionDelegate> slf, NSURLConnection *connection, NSURLResponse *response) {
  467. [[FLEXNetworkObserver sharedObserver] connection:connection didReceiveResponse:response delegate:slf];
  468. };
  469. NSURLConnectionDidReceiveResponseBlock implementationBlock = ^(id <NSURLConnectionDelegate> slf, NSURLConnection *connection, NSURLResponse *response) {
  470. [self sniffWithoutDuplicationForObject:connection selector:selector sniffingBlock:^{
  471. undefinedBlock(slf, connection, response);
  472. } originalImplementationBlock:^{
  473. ((void(*)(id, SEL, id, id))objc_msgSend)(slf, swizzledSelector, connection, response);
  474. }];
  475. };
  476. [FLEXUtility replaceImplementationOfSelector:selector withSelector:swizzledSelector forClass:cls withMethodDescription:methodDescription implementationBlock:implementationBlock undefinedBlock:undefinedBlock];
  477. }
  478. + (void)injectDidReceiveDataIntoDelegateClass:(Class)cls
  479. {
  480. SEL selector = @selector(connection:didReceiveData:);
  481. SEL swizzledSelector = [FLEXUtility swizzledSelectorForSelector:selector];
  482. Protocol *protocol = @protocol(NSURLConnectionDataDelegate);
  483. if (!protocol) {
  484. protocol = @protocol(NSURLConnectionDelegate);
  485. }
  486. struct objc_method_description methodDescription = protocol_getMethodDescription(protocol, selector, NO, YES);
  487. typedef void (^NSURLConnectionDidReceiveDataBlock)(id <NSURLConnectionDelegate> slf, NSURLConnection *connection, NSData *data);
  488. NSURLConnectionDidReceiveDataBlock undefinedBlock = ^(id <NSURLConnectionDelegate> slf, NSURLConnection *connection, NSData *data) {
  489. [[FLEXNetworkObserver sharedObserver] connection:connection didReceiveData:data delegate:slf];
  490. };
  491. NSURLConnectionDidReceiveDataBlock implementationBlock = ^(id <NSURLConnectionDelegate> slf, NSURLConnection *connection, NSData *data) {
  492. [self sniffWithoutDuplicationForObject:connection selector:selector sniffingBlock:^{
  493. undefinedBlock(slf, connection, data);
  494. } originalImplementationBlock:^{
  495. ((void(*)(id, SEL, id, id))objc_msgSend)(slf, swizzledSelector, connection, data);
  496. }];
  497. };
  498. [FLEXUtility replaceImplementationOfSelector:selector withSelector:swizzledSelector forClass:cls withMethodDescription:methodDescription implementationBlock:implementationBlock undefinedBlock:undefinedBlock];
  499. }
  500. + (void)injectDidFinishLoadingIntoDelegateClass:(Class)cls
  501. {
  502. SEL selector = @selector(connectionDidFinishLoading:);
  503. SEL swizzledSelector = [FLEXUtility swizzledSelectorForSelector:selector];
  504. Protocol *protocol = @protocol(NSURLConnectionDataDelegate);
  505. if (!protocol) {
  506. protocol = @protocol(NSURLConnectionDelegate);
  507. }
  508. struct objc_method_description methodDescription = protocol_getMethodDescription(protocol, selector, NO, YES);
  509. typedef void (^NSURLConnectionDidFinishLoadingBlock)(id <NSURLConnectionDelegate> slf, NSURLConnection *connection);
  510. NSURLConnectionDidFinishLoadingBlock undefinedBlock = ^(id <NSURLConnectionDelegate> slf, NSURLConnection *connection) {
  511. [[FLEXNetworkObserver sharedObserver] connectionDidFinishLoading:connection delegate:slf];
  512. };
  513. NSURLConnectionDidFinishLoadingBlock implementationBlock = ^(id <NSURLConnectionDelegate> slf, NSURLConnection *connection) {
  514. [self sniffWithoutDuplicationForObject:connection selector:selector sniffingBlock:^{
  515. undefinedBlock(slf, connection);
  516. } originalImplementationBlock:^{
  517. ((void(*)(id, SEL, id))objc_msgSend)(slf, swizzledSelector, connection);
  518. }];
  519. };
  520. [FLEXUtility replaceImplementationOfSelector:selector withSelector:swizzledSelector forClass:cls withMethodDescription:methodDescription implementationBlock:implementationBlock undefinedBlock:undefinedBlock];
  521. }
  522. + (void)injectDidFailWithErrorIntoDelegateClass:(Class)cls
  523. {
  524. SEL selector = @selector(connection:didFailWithError:);
  525. SEL swizzledSelector = [FLEXUtility swizzledSelectorForSelector:selector];
  526. Protocol *protocol = @protocol(NSURLConnectionDelegate);
  527. struct objc_method_description methodDescription = protocol_getMethodDescription(protocol, selector, NO, YES);
  528. typedef void (^NSURLConnectionDidFailWithErrorBlock)(id <NSURLConnectionDelegate> slf, NSURLConnection *connection, NSError *error);
  529. NSURLConnectionDidFailWithErrorBlock undefinedBlock = ^(id <NSURLConnectionDelegate> slf, NSURLConnection *connection, NSError *error) {
  530. [[FLEXNetworkObserver sharedObserver] connection:connection didFailWithError:error delegate:slf];
  531. };
  532. NSURLConnectionDidFailWithErrorBlock implementationBlock = ^(id <NSURLConnectionDelegate> slf, NSURLConnection *connection, NSError *error) {
  533. [self sniffWithoutDuplicationForObject:connection selector:selector sniffingBlock:^{
  534. undefinedBlock(slf, connection, error);
  535. } originalImplementationBlock:^{
  536. ((void(*)(id, SEL, id, id))objc_msgSend)(slf, swizzledSelector, connection, error);
  537. }];
  538. };
  539. [FLEXUtility replaceImplementationOfSelector:selector withSelector:swizzledSelector forClass:cls withMethodDescription:methodDescription implementationBlock:implementationBlock undefinedBlock:undefinedBlock];
  540. }
  541. + (void)injectTaskWillPerformHTTPRedirectionIntoDelegateClass:(Class)cls
  542. {
  543. SEL selector = @selector(URLSession:task:willPerformHTTPRedirection:newRequest:completionHandler:);
  544. SEL swizzledSelector = [FLEXUtility swizzledSelectorForSelector:selector];
  545. Protocol *protocol = @protocol(NSURLSessionTaskDelegate);
  546. struct objc_method_description methodDescription = protocol_getMethodDescription(protocol, selector, NO, YES);
  547. typedef void (^NSURLSessionWillPerformHTTPRedirectionBlock)(id <NSURLSessionTaskDelegate> slf, NSURLSession *session, NSURLSessionTask *task, NSHTTPURLResponse *response, NSURLRequest *newRequest, void(^completionHandler)(NSURLRequest *));
  548. NSURLSessionWillPerformHTTPRedirectionBlock undefinedBlock = ^(id <NSURLSessionTaskDelegate> slf, NSURLSession *session, NSURLSessionTask *task, NSHTTPURLResponse *response, NSURLRequest *newRequest, void(^completionHandler)(NSURLRequest *)) {
  549. [[FLEXNetworkObserver sharedObserver] URLSession:session task:task willPerformHTTPRedirection:response newRequest:newRequest completionHandler:completionHandler delegate:slf];
  550. completionHandler(newRequest);
  551. };
  552. NSURLSessionWillPerformHTTPRedirectionBlock implementationBlock = ^(id <NSURLSessionTaskDelegate> slf, NSURLSession *session, NSURLSessionTask *task, NSHTTPURLResponse *response, NSURLRequest *newRequest, void(^completionHandler)(NSURLRequest *)) {
  553. [self sniffWithoutDuplicationForObject:session selector:selector sniffingBlock:^{
  554. [[FLEXNetworkObserver sharedObserver] URLSession:session task:task willPerformHTTPRedirection:response newRequest:newRequest completionHandler:completionHandler delegate:slf];
  555. } originalImplementationBlock:^{
  556. ((id(*)(id, SEL, id, id, id, id, void(^)(NSURLRequest *)))objc_msgSend)(slf, swizzledSelector, session, task, response, newRequest, completionHandler);
  557. }];
  558. };
  559. [FLEXUtility replaceImplementationOfSelector:selector withSelector:swizzledSelector forClass:cls withMethodDescription:methodDescription implementationBlock:implementationBlock undefinedBlock:undefinedBlock];
  560. }
  561. + (void)injectTaskDidReceiveDataIntoDelegateClass:(Class)cls
  562. {
  563. SEL selector = @selector(URLSession:dataTask:didReceiveData:);
  564. SEL swizzledSelector = [FLEXUtility swizzledSelectorForSelector:selector];
  565. Protocol *protocol = @protocol(NSURLSessionDataDelegate);
  566. struct objc_method_description methodDescription = protocol_getMethodDescription(protocol, selector, NO, YES);
  567. typedef void (^NSURLSessionDidReceiveDataBlock)(id <NSURLSessionDataDelegate> slf, NSURLSession *session, NSURLSessionDataTask *dataTask, NSData *data);
  568. NSURLSessionDidReceiveDataBlock undefinedBlock = ^(id <NSURLSessionDataDelegate> slf, NSURLSession *session, NSURLSessionDataTask *dataTask, NSData *data) {
  569. [[FLEXNetworkObserver sharedObserver] URLSession:session dataTask:dataTask didReceiveData:data delegate:slf];
  570. };
  571. NSURLSessionDidReceiveDataBlock implementationBlock = ^(id <NSURLSessionDataDelegate> slf, NSURLSession *session, NSURLSessionDataTask *dataTask, NSData *data) {
  572. [self sniffWithoutDuplicationForObject:session selector:selector sniffingBlock:^{
  573. undefinedBlock(slf, session, dataTask, data);
  574. } originalImplementationBlock:^{
  575. ((void(*)(id, SEL, id, id, id))objc_msgSend)(slf, swizzledSelector, session, dataTask, data);
  576. }];
  577. };
  578. [FLEXUtility replaceImplementationOfSelector:selector withSelector:swizzledSelector forClass:cls withMethodDescription:methodDescription implementationBlock:implementationBlock undefinedBlock:undefinedBlock];
  579. }
  580. + (void)injectDataTaskDidBecomeDownloadTaskIntoDelegateClass:(Class)cls
  581. {
  582. SEL selector = @selector(URLSession:dataTask:didBecomeDownloadTask:);
  583. SEL swizzledSelector = [FLEXUtility swizzledSelectorForSelector:selector];
  584. Protocol *protocol = @protocol(NSURLSessionDataDelegate);
  585. struct objc_method_description methodDescription = protocol_getMethodDescription(protocol, selector, NO, YES);
  586. typedef void (^NSURLSessionDidBecomeDownloadTaskBlock)(id <NSURLSessionDataDelegate> slf, NSURLSession *session, NSURLSessionDataTask *dataTask, NSURLSessionDownloadTask *downloadTask);
  587. NSURLSessionDidBecomeDownloadTaskBlock undefinedBlock = ^(id <NSURLSessionDataDelegate> slf, NSURLSession *session, NSURLSessionDataTask *dataTask, NSURLSessionDownloadTask *downloadTask) {
  588. [[FLEXNetworkObserver sharedObserver] URLSession:session dataTask:dataTask didBecomeDownloadTask:downloadTask delegate:slf];
  589. };
  590. NSURLSessionDidBecomeDownloadTaskBlock implementationBlock = ^(id <NSURLSessionDataDelegate> slf, NSURLSession *session, NSURLSessionDataTask *dataTask, NSURLSessionDownloadTask *downloadTask) {
  591. [self sniffWithoutDuplicationForObject:session selector:selector sniffingBlock:^{
  592. undefinedBlock(slf, session, dataTask, downloadTask);
  593. } originalImplementationBlock:^{
  594. ((void(*)(id, SEL, id, id, id))objc_msgSend)(slf, swizzledSelector, session, dataTask, downloadTask);
  595. }];
  596. };
  597. [FLEXUtility replaceImplementationOfSelector:selector withSelector:swizzledSelector forClass:cls withMethodDescription:methodDescription implementationBlock:implementationBlock undefinedBlock:undefinedBlock];
  598. }
  599. + (void)injectTaskDidReceiveResponseIntoDelegateClass:(Class)cls
  600. {
  601. SEL selector = @selector(URLSession:dataTask:didReceiveResponse:completionHandler:);
  602. SEL swizzledSelector = [FLEXUtility swizzledSelectorForSelector:selector];
  603. Protocol *protocol = @protocol(NSURLSessionDataDelegate);
  604. struct objc_method_description methodDescription = protocol_getMethodDescription(protocol, selector, NO, YES);
  605. typedef void (^NSURLSessionDidReceiveResponseBlock)(id <NSURLSessionDelegate> slf, NSURLSession *session, NSURLSessionDataTask *dataTask, NSURLResponse *response, void(^completionHandler)(NSURLSessionResponseDisposition disposition));
  606. NSURLSessionDidReceiveResponseBlock undefinedBlock = ^(id <NSURLSessionDelegate> slf, NSURLSession *session, NSURLSessionDataTask *dataTask, NSURLResponse *response, void(^completionHandler)(NSURLSessionResponseDisposition disposition)) {
  607. [[FLEXNetworkObserver sharedObserver] URLSession:session dataTask:dataTask didReceiveResponse:response completionHandler:completionHandler delegate:slf];
  608. completionHandler(NSURLSessionResponseAllow);
  609. };
  610. NSURLSessionDidReceiveResponseBlock implementationBlock = ^(id <NSURLSessionDelegate> slf, NSURLSession *session, NSURLSessionDataTask *dataTask, NSURLResponse *response, void(^completionHandler)(NSURLSessionResponseDisposition disposition)) {
  611. [self sniffWithoutDuplicationForObject:session selector:selector sniffingBlock:^{
  612. [[FLEXNetworkObserver sharedObserver] URLSession:session dataTask:dataTask didReceiveResponse:response completionHandler:completionHandler delegate:slf];
  613. } originalImplementationBlock:^{
  614. ((void(*)(id, SEL, id, id, id, void(^)(NSURLSessionResponseDisposition)))objc_msgSend)(slf, swizzledSelector, session, dataTask, response, completionHandler);
  615. }];
  616. };
  617. [FLEXUtility replaceImplementationOfSelector:selector withSelector:swizzledSelector forClass:cls withMethodDescription:methodDescription implementationBlock:implementationBlock undefinedBlock:undefinedBlock];
  618. }
  619. + (void)injectTaskDidCompleteWithErrorIntoDelegateClass:(Class)cls
  620. {
  621. SEL selector = @selector(URLSession:task:didCompleteWithError:);
  622. SEL swizzledSelector = [FLEXUtility swizzledSelectorForSelector:selector];
  623. Protocol *protocol = @protocol(NSURLSessionTaskDelegate);
  624. struct objc_method_description methodDescription = protocol_getMethodDescription(protocol, selector, NO, YES);
  625. typedef void (^NSURLSessionTaskDidCompleteWithErrorBlock)(id <NSURLSessionTaskDelegate> slf, NSURLSession *session, NSURLSessionTask *task, NSError *error);
  626. NSURLSessionTaskDidCompleteWithErrorBlock undefinedBlock = ^(id <NSURLSessionTaskDelegate> slf, NSURLSession *session, NSURLSessionTask *task, NSError *error) {
  627. [[FLEXNetworkObserver sharedObserver] URLSession:session task:task didCompleteWithError:error delegate:slf];
  628. };
  629. NSURLSessionTaskDidCompleteWithErrorBlock implementationBlock = ^(id <NSURLSessionTaskDelegate> slf, NSURLSession *session, NSURLSessionTask *task, NSError *error) {
  630. [self sniffWithoutDuplicationForObject:session selector:selector sniffingBlock:^{
  631. undefinedBlock(slf, session, task, error);
  632. } originalImplementationBlock:^{
  633. ((void(*)(id, SEL, id, id, id))objc_msgSend)(slf, swizzledSelector, session, task, error);
  634. }];
  635. };
  636. [FLEXUtility replaceImplementationOfSelector:selector withSelector:swizzledSelector forClass:cls withMethodDescription:methodDescription implementationBlock:implementationBlock undefinedBlock:undefinedBlock];
  637. }
  638. // Used for overriding AFNetworking behavior
  639. + (void)injectRespondsToSelectorIntoDelegateClass:(Class)cls
  640. {
  641. SEL selector = @selector(respondsToSelector:);
  642. SEL swizzledSelector = [FLEXUtility swizzledSelectorForSelector:selector];
  643. //Protocol *protocol = @protocol(NSURLSessionTaskDelegate);
  644. Method method = class_getInstanceMethod(cls, selector);
  645. struct objc_method_description methodDescription = *method_getDescription(method);
  646. BOOL (^undefinedBlock)(id <NSURLSessionTaskDelegate>, SEL) = ^(id slf, SEL sel) {
  647. return YES;
  648. };
  649. BOOL (^implementationBlock)(id <NSURLSessionTaskDelegate>, SEL) = ^(id <NSURLSessionTaskDelegate> slf, SEL sel) {
  650. if (sel == @selector(URLSession:dataTask:didReceiveResponse:completionHandler:)) {
  651. return undefinedBlock(slf, sel);
  652. }
  653. return ((BOOL(*)(id, SEL, SEL))objc_msgSend)(slf, swizzledSelector, sel);
  654. };
  655. [FLEXUtility replaceImplementationOfSelector:selector withSelector:swizzledSelector forClass:cls withMethodDescription:methodDescription implementationBlock:implementationBlock undefinedBlock:undefinedBlock];
  656. }
  657. + (void)injectDownloadTaskDidFinishDownloadingIntoDelegateClass:(Class)cls
  658. {
  659. SEL selector = @selector(URLSession:downloadTask:didFinishDownloadingToURL:);
  660. SEL swizzledSelector = [FLEXUtility swizzledSelectorForSelector:selector];
  661. Protocol *protocol = @protocol(NSURLSessionDownloadDelegate);
  662. struct objc_method_description methodDescription = protocol_getMethodDescription(protocol, selector, NO, YES);
  663. typedef void (^NSURLSessionDownloadTaskDidFinishDownloadingBlock)(id <NSURLSessionTaskDelegate> slf, NSURLSession *session, NSURLSessionDownloadTask *task, NSURL *location);
  664. NSURLSessionDownloadTaskDidFinishDownloadingBlock undefinedBlock = ^(id <NSURLSessionTaskDelegate> slf, NSURLSession *session, NSURLSessionDownloadTask *task, NSURL *location) {
  665. NSData *data = [NSData dataWithContentsOfFile:location.relativePath];
  666. [[FLEXNetworkObserver sharedObserver] URLSession:session task:task didFinishDownloadingToURL:location data:data delegate:slf];
  667. };
  668. NSURLSessionDownloadTaskDidFinishDownloadingBlock implementationBlock = ^(id <NSURLSessionTaskDelegate> slf, NSURLSession *session, NSURLSessionDownloadTask *task, NSURL *location) {
  669. [self sniffWithoutDuplicationForObject:session selector:selector sniffingBlock:^{
  670. undefinedBlock(slf, session, task, location);
  671. } originalImplementationBlock:^{
  672. ((void(*)(id, SEL, id, id, id))objc_msgSend)(slf, swizzledSelector, session, task, location);
  673. }];
  674. };
  675. [FLEXUtility replaceImplementationOfSelector:selector withSelector:swizzledSelector forClass:cls withMethodDescription:methodDescription implementationBlock:implementationBlock undefinedBlock:undefinedBlock];
  676. }
  677. + (void)injectDownloadTaskDidWriteDataIntoDelegateClass:(Class)cls
  678. {
  679. SEL selector = @selector(URLSession:downloadTask:didWriteData:totalBytesWritten:totalBytesExpectedToWrite:);
  680. SEL swizzledSelector = [FLEXUtility swizzledSelectorForSelector:selector];
  681. Protocol *protocol = @protocol(NSURLSessionDownloadDelegate);
  682. struct objc_method_description methodDescription = protocol_getMethodDescription(protocol, selector, NO, YES);
  683. typedef void (^NSURLSessionDownloadTaskDidWriteDataBlock)(id <NSURLSessionTaskDelegate> slf, NSURLSession *session, NSURLSessionDownloadTask *task, int64_t bytesWritten, int64_t totalBytesWritten, int64_t totalBytesExpectedToWrite);
  684. NSURLSessionDownloadTaskDidWriteDataBlock undefinedBlock = ^(id <NSURLSessionTaskDelegate> slf, NSURLSession *session, NSURLSessionDownloadTask *task, int64_t bytesWritten, int64_t totalBytesWritten, int64_t totalBytesExpectedToWrite) {
  685. [[FLEXNetworkObserver sharedObserver] URLSession:session downloadTask:task didWriteData:bytesWritten totalBytesWritten:totalBytesWritten totalBytesExpectedToWrite:totalBytesExpectedToWrite delegate:slf];
  686. };
  687. NSURLSessionDownloadTaskDidWriteDataBlock implementationBlock = ^(id <NSURLSessionTaskDelegate> slf, NSURLSession *session, NSURLSessionDownloadTask *task, int64_t bytesWritten, int64_t totalBytesWritten, int64_t totalBytesExpectedToWrite) {
  688. [self sniffWithoutDuplicationForObject:session selector:selector sniffingBlock:^{
  689. undefinedBlock(slf, session, task, bytesWritten, totalBytesWritten, totalBytesExpectedToWrite);
  690. } originalImplementationBlock:^{
  691. ((void(*)(id, SEL, id, id, int64_t, int64_t, int64_t))objc_msgSend)(slf, swizzledSelector, session, task, bytesWritten, totalBytesWritten, totalBytesExpectedToWrite);
  692. }];
  693. };
  694. [FLEXUtility replaceImplementationOfSelector:selector withSelector:swizzledSelector forClass:cls withMethodDescription:methodDescription implementationBlock:implementationBlock undefinedBlock:undefinedBlock];
  695. }
  696. static char const * const kFLEXRequestIDKey = "kFLEXRequestIDKey";
  697. + (NSString *)requestIDForConnectionOrTask:(id)connectionOrTask
  698. {
  699. NSString *requestID = objc_getAssociatedObject(connectionOrTask, kFLEXRequestIDKey);
  700. if (!requestID) {
  701. requestID = [self nextRequestID];
  702. [self setRequestID:requestID forConnectionOrTask:connectionOrTask];
  703. }
  704. return requestID;
  705. }
  706. + (void)setRequestID:(NSString *)requestID forConnectionOrTask:(id)connectionOrTask
  707. {
  708. objc_setAssociatedObject(connectionOrTask, kFLEXRequestIDKey, requestID, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
  709. }
  710. #pragma mark - Initialization
  711. - (id)init
  712. {
  713. self = [super init];
  714. if (self) {
  715. self.requestStatesForRequestIDs = [NSMutableDictionary new];
  716. self.queue = dispatch_queue_create("com.flex.FLEXNetworkObserver", DISPATCH_QUEUE_SERIAL);
  717. }
  718. return self;
  719. }
  720. #pragma mark - Private Methods
  721. - (void)performBlock:(dispatch_block_t)block
  722. {
  723. if ([[self class] isEnabled]) {
  724. dispatch_async(_queue, block);
  725. }
  726. }
  727. - (FLEXInternalRequestState *)requestStateForRequestID:(NSString *)requestID
  728. {
  729. FLEXInternalRequestState *requestState = self.requestStatesForRequestIDs[requestID];
  730. if (!requestState) {
  731. requestState = [FLEXInternalRequestState new];
  732. [self.requestStatesForRequestIDs setObject:requestState forKey:requestID];
  733. }
  734. return requestState;
  735. }
  736. - (void)removeRequestStateForRequestID:(NSString *)requestID
  737. {
  738. [self.requestStatesForRequestIDs removeObjectForKey:requestID];
  739. }
  740. @end
  741. @implementation FLEXNetworkObserver (NSURLConnectionHelpers)
  742. - (void)connection:(NSURLConnection *)connection willSendRequest:(NSURLRequest *)request redirectResponse:(NSURLResponse *)response delegate:(id<NSURLConnectionDelegate>)delegate
  743. {
  744. [self performBlock:^{
  745. NSString *requestID = [[self class] requestIDForConnectionOrTask:connection];
  746. FLEXInternalRequestState *requestState = [self requestStateForRequestID:requestID];
  747. requestState.request = request;
  748. [[FLEXNetworkRecorder defaultRecorder] recordRequestWillBeSentWithRequestID:requestID request:request redirectResponse:response];
  749. NSString *mechanism = [NSString stringWithFormat:@"NSURLConnection (delegate: %@)", [delegate class]];
  750. [[FLEXNetworkRecorder defaultRecorder] recordMechanism:mechanism forRequestID:requestID];
  751. }];
  752. }
  753. - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response delegate:(id<NSURLConnectionDelegate>)delegate
  754. {
  755. [self performBlock:^{
  756. NSString *requestID = [[self class] requestIDForConnectionOrTask:connection];
  757. FLEXInternalRequestState *requestState = [self requestStateForRequestID:requestID];
  758. NSMutableData *dataAccumulator = nil;
  759. if (response.expectedContentLength < 0) {
  760. dataAccumulator = [NSMutableData new];
  761. } else if (response.expectedContentLength < 52428800) {
  762. dataAccumulator = [[NSMutableData alloc] initWithCapacity:(NSUInteger)response.expectedContentLength];
  763. }
  764. requestState.dataAccumulator = dataAccumulator;
  765. [[FLEXNetworkRecorder defaultRecorder] recordResponseReceivedWithRequestID:requestID response:response];
  766. }];
  767. }
  768. - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data delegate:(id<NSURLConnectionDelegate>)delegate
  769. {
  770. // Just to be safe since we're doing this async
  771. data = [data copy];
  772. [self performBlock:^{
  773. NSString *requestID = [[self class] requestIDForConnectionOrTask:connection];
  774. FLEXInternalRequestState *requestState = [self requestStateForRequestID:requestID];
  775. [requestState.dataAccumulator appendData:data];
  776. [[FLEXNetworkRecorder defaultRecorder] recordDataReceivedWithRequestID:requestID dataLength:data.length];
  777. }];
  778. }
  779. - (void)connectionDidFinishLoading:(NSURLConnection *)connection delegate:(id<NSURLConnectionDelegate>)delegate
  780. {
  781. [self performBlock:^{
  782. NSString *requestID = [[self class] requestIDForConnectionOrTask:connection];
  783. FLEXInternalRequestState *requestState = [self requestStateForRequestID:requestID];
  784. [[FLEXNetworkRecorder defaultRecorder] recordLoadingFinishedWithRequestID:requestID responseBody:requestState.dataAccumulator];
  785. [self removeRequestStateForRequestID:requestID];
  786. }];
  787. }
  788. - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error delegate:(id<NSURLConnectionDelegate>)delegate
  789. {
  790. [self performBlock:^{
  791. NSString *requestID = [[self class] requestIDForConnectionOrTask:connection];
  792. FLEXInternalRequestState *requestState = [self requestStateForRequestID:requestID];
  793. // Cancellations can occur prior to the willSendRequest:... NSURLConnection delegate call.
  794. // These are pretty common and clutter up the logs. Only record the failure if the recorder already knows about the request through willSendRequest:...
  795. if (requestState.request) {
  796. [[FLEXNetworkRecorder defaultRecorder] recordLoadingFailedWithRequestID:requestID error:error];
  797. }
  798. [self removeRequestStateForRequestID:requestID];
  799. }];
  800. }
  801. - (void)connectionWillCancel:(NSURLConnection *)connection
  802. {
  803. [self performBlock:^{
  804. // Mimic the behavior of NSURLSession which is to create an error on cancellation.
  805. NSDictionary<NSString *, id> *userInfo = @{ NSLocalizedDescriptionKey : @"cancelled" };
  806. NSError *error = [NSError errorWithDomain:NSURLErrorDomain code:NSURLErrorCancelled userInfo:userInfo];
  807. [self connection:connection didFailWithError:error delegate:nil];
  808. }];
  809. }
  810. @end
  811. @implementation FLEXNetworkObserver (NSURLSessionTaskHelpers)
  812. - (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task willPerformHTTPRedirection:(NSHTTPURLResponse *)response newRequest:(NSURLRequest *)request completionHandler:(void (^)(NSURLRequest *))completionHandler delegate:(id<NSURLSessionDelegate>)delegate
  813. {
  814. [self performBlock:^{
  815. NSString *requestID = [[self class] requestIDForConnectionOrTask:task];
  816. [[FLEXNetworkRecorder defaultRecorder] recordRequestWillBeSentWithRequestID:requestID request:request redirectResponse:response];
  817. }];
  818. }
  819. - (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didReceiveResponse:(NSURLResponse *)response completionHandler:(void (^)(NSURLSessionResponseDisposition disposition))completionHandler delegate:(id<NSURLSessionDelegate>)delegate
  820. {
  821. [self performBlock:^{
  822. NSString *requestID = [[self class] requestIDForConnectionOrTask:dataTask];
  823. FLEXInternalRequestState *requestState = [self requestStateForRequestID:requestID];
  824. NSMutableData *dataAccumulator = nil;
  825. if (response.expectedContentLength < 0) {
  826. dataAccumulator = [NSMutableData new];
  827. } else {
  828. dataAccumulator = [[NSMutableData alloc] initWithCapacity:(NSUInteger)response.expectedContentLength];
  829. }
  830. requestState.dataAccumulator = dataAccumulator;
  831. NSString *requestMechanism = [NSString stringWithFormat:@"NSURLSessionDataTask (delegate: %@)", [delegate class]];
  832. [[FLEXNetworkRecorder defaultRecorder] recordMechanism:requestMechanism forRequestID:requestID];
  833. [[FLEXNetworkRecorder defaultRecorder] recordResponseReceivedWithRequestID:requestID response:response];
  834. }];
  835. }
  836. - (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didBecomeDownloadTask:(NSURLSessionDownloadTask *)downloadTask delegate:(id<NSURLSessionDelegate>)delegate
  837. {
  838. [self performBlock:^{
  839. // By setting the request ID of the download task to match the data task,
  840. // it can pick up where the data task left off.
  841. NSString *requestID = [[self class] requestIDForConnectionOrTask:dataTask];
  842. [[self class] setRequestID:requestID forConnectionOrTask:downloadTask];
  843. }];
  844. }
  845. - (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didReceiveData:(NSData *)data delegate:(id<NSURLSessionDelegate>)delegate
  846. {
  847. // Just to be safe since we're doing this async
  848. data = [data copy];
  849. [self performBlock:^{
  850. NSString *requestID = [[self class] requestIDForConnectionOrTask:dataTask];
  851. FLEXInternalRequestState *requestState = [self requestStateForRequestID:requestID];
  852. [requestState.dataAccumulator appendData:data];
  853. [[FLEXNetworkRecorder defaultRecorder] recordDataReceivedWithRequestID:requestID dataLength:data.length];
  854. }];
  855. }
  856. - (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(NSError *)error delegate:(id<NSURLSessionDelegate>)delegate
  857. {
  858. [self performBlock:^{
  859. NSString *requestID = [[self class] requestIDForConnectionOrTask:task];
  860. FLEXInternalRequestState *requestState = [self requestStateForRequestID:requestID];
  861. if (error) {
  862. [[FLEXNetworkRecorder defaultRecorder] recordLoadingFailedWithRequestID:requestID error:error];
  863. } else {
  864. [[FLEXNetworkRecorder defaultRecorder] recordLoadingFinishedWithRequestID:requestID responseBody:requestState.dataAccumulator];
  865. }
  866. [self removeRequestStateForRequestID:requestID];
  867. }];
  868. }
  869. - (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didWriteData:(int64_t)bytesWritten totalBytesWritten:(int64_t)totalBytesWritten totalBytesExpectedToWrite:(int64_t)totalBytesExpectedToWrite delegate:(id<NSURLSessionDelegate>)delegate
  870. {
  871. [self performBlock:^{
  872. NSString *requestID = [[self class] requestIDForConnectionOrTask:downloadTask];
  873. FLEXInternalRequestState *requestState = [self requestStateForRequestID:requestID];
  874. if (!requestState.dataAccumulator) {
  875. NSUInteger unsignedBytesExpectedToWrite = totalBytesExpectedToWrite > 0 ? (NSUInteger)totalBytesExpectedToWrite : 0;
  876. requestState.dataAccumulator = [[NSMutableData alloc] initWithCapacity:unsignedBytesExpectedToWrite];
  877. [[FLEXNetworkRecorder defaultRecorder] recordResponseReceivedWithRequestID:requestID response:downloadTask.response];
  878. NSString *requestMechanism = [NSString stringWithFormat:@"NSURLSessionDownloadTask (delegate: %@)", [delegate class]];
  879. [[FLEXNetworkRecorder defaultRecorder] recordMechanism:requestMechanism forRequestID:requestID];
  880. }
  881. [[FLEXNetworkRecorder defaultRecorder] recordDataReceivedWithRequestID:requestID dataLength:bytesWritten];
  882. }];
  883. }
  884. - (void)URLSession:(NSURLSession *)session task:(NSURLSessionDownloadTask *)downloadTask didFinishDownloadingToURL:(NSURL *)location data:(NSData *)data delegate:(id<NSURLSessionDelegate>)delegate
  885. {
  886. data = [data copy];
  887. [self performBlock:^{
  888. NSString *requestID = [[self class] requestIDForConnectionOrTask:downloadTask];
  889. FLEXInternalRequestState *requestState = [self requestStateForRequestID:requestID];
  890. [requestState.dataAccumulator appendData:data];
  891. }];
  892. }
  893. - (void)URLSessionTaskWillResume:(NSURLSessionTask *)task
  894. {
  895. // Since resume can be called multiple times on the same task, only treat the first resume as
  896. // the equivalent to connection:willSendRequest:...
  897. [self performBlock:^{
  898. NSString *requestID = [[self class] requestIDForConnectionOrTask:task];
  899. FLEXInternalRequestState *requestState = [self requestStateForRequestID:requestID];
  900. if (!requestState.request) {
  901. requestState.request = task.currentRequest;
  902. [[FLEXNetworkRecorder defaultRecorder] recordRequestWillBeSentWithRequestID:requestID request:task.currentRequest redirectResponse:nil];
  903. }
  904. }];
  905. }
  906. @end