FLEXNetworkObserver.m 57 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121
  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. };
  545. NSURLSessionWillPerformHTTPRedirectionBlock implementationBlock = ^(id <NSURLSessionTaskDelegate> slf, NSURLSession *session, NSURLSessionTask *task, NSHTTPURLResponse *response, NSURLRequest *newRequest, void(^completionHandler)(NSURLRequest *)) {
  546. [self sniffWithoutDuplicationForObject:session selector:selector sniffingBlock:^{
  547. undefinedBlock(slf, session, task, response, newRequest, completionHandler);
  548. } originalImplementationBlock:^{
  549. ((id(*)(id, SEL, id, id, id, id, void(^)()))objc_msgSend)(slf, swizzledSelector, session, task, response, newRequest, completionHandler);
  550. }];
  551. };
  552. [FLEXUtility replaceImplementationOfSelector:selector withSelector:swizzledSelector forClass:cls withMethodDescription:methodDescription implementationBlock:implementationBlock undefinedBlock:undefinedBlock];
  553. }
  554. + (void)injectTaskDidReceiveDataIntoDelegateClass:(Class)cls
  555. {
  556. SEL selector = @selector(URLSession:dataTask:didReceiveData:);
  557. SEL swizzledSelector = [FLEXUtility swizzledSelectorForSelector:selector];
  558. Protocol *protocol = @protocol(NSURLSessionDataDelegate);
  559. struct objc_method_description methodDescription = protocol_getMethodDescription(protocol, selector, NO, YES);
  560. typedef void (^NSURLSessionDidReceiveDataBlock)(id <NSURLSessionDataDelegate> slf, NSURLSession *session, NSURLSessionDataTask *dataTask, NSData *data);
  561. NSURLSessionDidReceiveDataBlock undefinedBlock = ^(id <NSURLSessionDataDelegate> slf, NSURLSession *session, NSURLSessionDataTask *dataTask, NSData *data) {
  562. [[FLEXNetworkObserver sharedObserver] URLSession:session dataTask:dataTask didReceiveData:data delegate:slf];
  563. };
  564. NSURLSessionDidReceiveDataBlock implementationBlock = ^(id <NSURLSessionDataDelegate> slf, NSURLSession *session, NSURLSessionDataTask *dataTask, NSData *data) {
  565. [self sniffWithoutDuplicationForObject:session selector:selector sniffingBlock:^{
  566. undefinedBlock(slf, session, dataTask, data);
  567. } originalImplementationBlock:^{
  568. ((void(*)(id, SEL, id, id, id))objc_msgSend)(slf, swizzledSelector, session, dataTask, data);
  569. }];
  570. };
  571. [FLEXUtility replaceImplementationOfSelector:selector withSelector:swizzledSelector forClass:cls withMethodDescription:methodDescription implementationBlock:implementationBlock undefinedBlock:undefinedBlock];
  572. }
  573. + (void)injectDataTaskDidBecomeDownloadTaskIntoDelegateClass:(Class)cls
  574. {
  575. SEL selector = @selector(URLSession:dataTask:didBecomeDownloadTask:);
  576. SEL swizzledSelector = [FLEXUtility swizzledSelectorForSelector:selector];
  577. Protocol *protocol = @protocol(NSURLSessionDataDelegate);
  578. struct objc_method_description methodDescription = protocol_getMethodDescription(protocol, selector, NO, YES);
  579. typedef void (^NSURLSessionDidBecomeDownloadTaskBlock)(id <NSURLSessionDataDelegate> slf, NSURLSession *session, NSURLSessionDataTask *dataTask, NSURLSessionDownloadTask *downloadTask);
  580. NSURLSessionDidBecomeDownloadTaskBlock undefinedBlock = ^(id <NSURLSessionDataDelegate> slf, NSURLSession *session, NSURLSessionDataTask *dataTask, NSURLSessionDownloadTask *downloadTask) {
  581. [[FLEXNetworkObserver sharedObserver] URLSession:session dataTask:dataTask didBecomeDownloadTask:downloadTask delegate:slf];
  582. };
  583. NSURLSessionDidBecomeDownloadTaskBlock implementationBlock = ^(id <NSURLSessionDataDelegate> slf, NSURLSession *session, NSURLSessionDataTask *dataTask, NSURLSessionDownloadTask *downloadTask) {
  584. [self sniffWithoutDuplicationForObject:session selector:selector sniffingBlock:^{
  585. undefinedBlock(slf, session, dataTask, downloadTask);
  586. } originalImplementationBlock:^{
  587. ((void(*)(id, SEL, id, id, id))objc_msgSend)(slf, swizzledSelector, session, dataTask, downloadTask);
  588. }];
  589. };
  590. [FLEXUtility replaceImplementationOfSelector:selector withSelector:swizzledSelector forClass:cls withMethodDescription:methodDescription implementationBlock:implementationBlock undefinedBlock:undefinedBlock];
  591. }
  592. + (void)injectTaskDidReceiveResponseIntoDelegateClass:(Class)cls
  593. {
  594. SEL selector = @selector(URLSession:dataTask:didReceiveResponse:completionHandler:);
  595. SEL swizzledSelector = [FLEXUtility swizzledSelectorForSelector:selector];
  596. Protocol *protocol = @protocol(NSURLSessionDataDelegate);
  597. struct objc_method_description methodDescription = protocol_getMethodDescription(protocol, selector, NO, YES);
  598. typedef void (^NSURLSessionDidReceiveResponseBlock)(id <NSURLSessionDelegate> slf, NSURLSession *session, NSURLSessionDataTask *dataTask, NSURLResponse *response, void(^completionHandler)(NSURLSessionResponseDisposition disposition));
  599. NSURLSessionDidReceiveResponseBlock undefinedBlock = ^(id <NSURLSessionDelegate> slf, NSURLSession *session, NSURLSessionDataTask *dataTask, NSURLResponse *response, void(^completionHandler)(NSURLSessionResponseDisposition disposition)) {
  600. [[FLEXNetworkObserver sharedObserver] URLSession:session dataTask:dataTask didReceiveResponse:response completionHandler:completionHandler delegate:slf];
  601. };
  602. NSURLSessionDidReceiveResponseBlock implementationBlock = ^(id <NSURLSessionDelegate> slf, NSURLSession *session, NSURLSessionDataTask *dataTask, NSURLResponse *response, void(^completionHandler)(NSURLSessionResponseDisposition disposition)) {
  603. [self sniffWithoutDuplicationForObject:session selector:selector sniffingBlock:^{
  604. undefinedBlock(slf, session, dataTask, response, completionHandler);
  605. } originalImplementationBlock:^{
  606. ((void(*)(id, SEL, id, id, id, void(^)()))objc_msgSend)(slf, swizzledSelector, session, dataTask, response, completionHandler);
  607. }];
  608. };
  609. [FLEXUtility replaceImplementationOfSelector:selector withSelector:swizzledSelector forClass:cls withMethodDescription:methodDescription implementationBlock:implementationBlock undefinedBlock:undefinedBlock];
  610. }
  611. + (void)injectTaskDidCompleteWithErrorIntoDelegateClass:(Class)cls
  612. {
  613. SEL selector = @selector(URLSession:task:didCompleteWithError:);
  614. SEL swizzledSelector = [FLEXUtility swizzledSelectorForSelector:selector];
  615. Protocol *protocol = @protocol(NSURLSessionTaskDelegate);
  616. struct objc_method_description methodDescription = protocol_getMethodDescription(protocol, selector, NO, YES);
  617. typedef void (^NSURLSessionTaskDidCompleteWithErrorBlock)(id <NSURLSessionTaskDelegate> slf, NSURLSession *session, NSURLSessionTask *task, NSError *error);
  618. NSURLSessionTaskDidCompleteWithErrorBlock undefinedBlock = ^(id <NSURLSessionTaskDelegate> slf, NSURLSession *session, NSURLSessionTask *task, NSError *error) {
  619. [[FLEXNetworkObserver sharedObserver] URLSession:session task:task didCompleteWithError:error delegate:slf];
  620. };
  621. NSURLSessionTaskDidCompleteWithErrorBlock implementationBlock = ^(id <NSURLSessionTaskDelegate> slf, NSURLSession *session, NSURLSessionTask *task, NSError *error) {
  622. [self sniffWithoutDuplicationForObject:session selector:selector sniffingBlock:^{
  623. undefinedBlock(slf, session, task, error);
  624. } originalImplementationBlock:^{
  625. ((void(*)(id, SEL, id, id, id))objc_msgSend)(slf, swizzledSelector, session, task, error);
  626. }];
  627. };
  628. [FLEXUtility replaceImplementationOfSelector:selector withSelector:swizzledSelector forClass:cls withMethodDescription:methodDescription implementationBlock:implementationBlock undefinedBlock:undefinedBlock];
  629. }
  630. // Used for overriding AFNetworking behavior
  631. + (void)injectRespondsToSelectorIntoDelegateClass:(Class)cls
  632. {
  633. SEL selector = @selector(respondsToSelector:);
  634. SEL swizzledSelector = [FLEXUtility swizzledSelectorForSelector:selector];
  635. //Protocol *protocol = @protocol(NSURLSessionTaskDelegate);
  636. Method method = class_getInstanceMethod(cls, selector);
  637. struct objc_method_description methodDescription = *method_getDescription(method);
  638. BOOL (^undefinedBlock)(id <NSURLSessionTaskDelegate>, SEL) = ^(id slf, SEL sel) {
  639. return YES;
  640. };
  641. BOOL (^implementationBlock)(id <NSURLSessionTaskDelegate>, SEL) = ^(id <NSURLSessionTaskDelegate> slf, SEL sel) {
  642. if (sel == @selector(URLSession:dataTask:didReceiveResponse:completionHandler:)) {
  643. return undefinedBlock(slf, sel);
  644. }
  645. return ((BOOL(*)(id, SEL, SEL))objc_msgSend)(slf, swizzledSelector, sel);
  646. };
  647. [FLEXUtility replaceImplementationOfSelector:selector withSelector:swizzledSelector forClass:cls withMethodDescription:methodDescription implementationBlock:implementationBlock undefinedBlock:undefinedBlock];
  648. }
  649. + (void)injectDownloadTaskDidFinishDownloadingIntoDelegateClass:(Class)cls
  650. {
  651. SEL selector = @selector(URLSession:downloadTask:didFinishDownloadingToURL:);
  652. SEL swizzledSelector = [FLEXUtility swizzledSelectorForSelector:selector];
  653. Protocol *protocol = @protocol(NSURLSessionDownloadDelegate);
  654. struct objc_method_description methodDescription = protocol_getMethodDescription(protocol, selector, NO, YES);
  655. typedef void (^NSURLSessionDownloadTaskDidFinishDownloadingBlock)(id <NSURLSessionTaskDelegate> slf, NSURLSession *session, NSURLSessionDownloadTask *task, NSURL *location);
  656. NSURLSessionDownloadTaskDidFinishDownloadingBlock undefinedBlock = ^(id <NSURLSessionTaskDelegate> slf, NSURLSession *session, NSURLSessionDownloadTask *task, NSURL *location) {
  657. NSData *data = [NSData dataWithContentsOfFile:location.relativePath];
  658. [[FLEXNetworkObserver sharedObserver] URLSession:session task:task didFinishDownloadingToURL:location data:data delegate:slf];
  659. };
  660. NSURLSessionDownloadTaskDidFinishDownloadingBlock implementationBlock = ^(id <NSURLSessionTaskDelegate> slf, NSURLSession *session, NSURLSessionDownloadTask *task, NSURL *location) {
  661. [self sniffWithoutDuplicationForObject:session selector:selector sniffingBlock:^{
  662. undefinedBlock(slf, session, task, location);
  663. } originalImplementationBlock:^{
  664. ((void(*)(id, SEL, id, id, id))objc_msgSend)(slf, swizzledSelector, session, task, location);
  665. }];
  666. };
  667. [FLEXUtility replaceImplementationOfSelector:selector withSelector:swizzledSelector forClass:cls withMethodDescription:methodDescription implementationBlock:implementationBlock undefinedBlock:undefinedBlock];
  668. }
  669. + (void)injectDownloadTaskDidWriteDataIntoDelegateClass:(Class)cls
  670. {
  671. SEL selector = @selector(URLSession:downloadTask:didWriteData:totalBytesWritten:totalBytesExpectedToWrite:);
  672. SEL swizzledSelector = [FLEXUtility swizzledSelectorForSelector:selector];
  673. Protocol *protocol = @protocol(NSURLSessionDownloadDelegate);
  674. struct objc_method_description methodDescription = protocol_getMethodDescription(protocol, selector, NO, YES);
  675. typedef void (^NSURLSessionDownloadTaskDidWriteDataBlock)(id <NSURLSessionTaskDelegate> slf, NSURLSession *session, NSURLSessionDownloadTask *task, int64_t bytesWritten, int64_t totalBytesWritten, int64_t totalBytesExpectedToWrite);
  676. NSURLSessionDownloadTaskDidWriteDataBlock undefinedBlock = ^(id <NSURLSessionTaskDelegate> slf, NSURLSession *session, NSURLSessionDownloadTask *task, int64_t bytesWritten, int64_t totalBytesWritten, int64_t totalBytesExpectedToWrite) {
  677. [[FLEXNetworkObserver sharedObserver] URLSession:session downloadTask:task didWriteData:bytesWritten totalBytesWritten:totalBytesWritten totalBytesExpectedToWrite:totalBytesExpectedToWrite delegate:slf];
  678. };
  679. NSURLSessionDownloadTaskDidWriteDataBlock implementationBlock = ^(id <NSURLSessionTaskDelegate> slf, NSURLSession *session, NSURLSessionDownloadTask *task, int64_t bytesWritten, int64_t totalBytesWritten, int64_t totalBytesExpectedToWrite) {
  680. [self sniffWithoutDuplicationForObject:session selector:selector sniffingBlock:^{
  681. undefinedBlock(slf, session, task, bytesWritten, totalBytesWritten, totalBytesExpectedToWrite);
  682. } originalImplementationBlock:^{
  683. ((void(*)(id, SEL, id, id, int64_t, int64_t, int64_t))objc_msgSend)(slf, swizzledSelector, session, task, bytesWritten, totalBytesWritten, totalBytesExpectedToWrite);
  684. }];
  685. };
  686. [FLEXUtility replaceImplementationOfSelector:selector withSelector:swizzledSelector forClass:cls withMethodDescription:methodDescription implementationBlock:implementationBlock undefinedBlock:undefinedBlock];
  687. }
  688. static char const * const kFLEXRequestIDKey = "kFLEXRequestIDKey";
  689. + (NSString *)requestIDForConnectionOrTask:(id)connectionOrTask
  690. {
  691. NSString *requestID = objc_getAssociatedObject(connectionOrTask, kFLEXRequestIDKey);
  692. if (!requestID) {
  693. requestID = [self nextRequestID];
  694. [self setRequestID:requestID forConnectionOrTask:connectionOrTask];
  695. }
  696. return requestID;
  697. }
  698. + (void)setRequestID:(NSString *)requestID forConnectionOrTask:(id)connectionOrTask
  699. {
  700. objc_setAssociatedObject(connectionOrTask, kFLEXRequestIDKey, requestID, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
  701. }
  702. #pragma mark - Initialization
  703. - (id)init
  704. {
  705. self = [super init];
  706. if (self) {
  707. self.requestStatesForRequestIDs = [[NSMutableDictionary alloc] init];
  708. self.queue = dispatch_queue_create("com.flex.FLEXNetworkObserver", DISPATCH_QUEUE_SERIAL);
  709. }
  710. return self;
  711. }
  712. #pragma mark - Private Methods
  713. - (void)performBlock:(dispatch_block_t)block
  714. {
  715. if ([[self class] isEnabled]) {
  716. dispatch_async(_queue, block);
  717. }
  718. }
  719. - (FLEXInternalRequestState *)requestStateForRequestID:(NSString *)requestID
  720. {
  721. FLEXInternalRequestState *requestState = self.requestStatesForRequestIDs[requestID];
  722. if (!requestState) {
  723. requestState = [[FLEXInternalRequestState alloc] init];
  724. [self.requestStatesForRequestIDs setObject:requestState forKey:requestID];
  725. }
  726. return requestState;
  727. }
  728. - (void)removeRequestStateForRequestID:(NSString *)requestID
  729. {
  730. [self.requestStatesForRequestIDs removeObjectForKey:requestID];
  731. }
  732. @end
  733. @implementation FLEXNetworkObserver (NSURLConnectionHelpers)
  734. - (void)connection:(NSURLConnection *)connection willSendRequest:(NSURLRequest *)request redirectResponse:(NSURLResponse *)response delegate:(id<NSURLConnectionDelegate>)delegate
  735. {
  736. [self performBlock:^{
  737. NSString *requestID = [[self class] requestIDForConnectionOrTask:connection];
  738. FLEXInternalRequestState *requestState = [self requestStateForRequestID:requestID];
  739. requestState.request = request;
  740. [[FLEXNetworkRecorder defaultRecorder] recordRequestWillBeSentWithRequestID:requestID request:request redirectResponse:response];
  741. NSString *mechanism = [NSString stringWithFormat:@"NSURLConnection (delegate: %@)", [delegate class]];
  742. [[FLEXNetworkRecorder defaultRecorder] recordMechanism:mechanism forRequestID:requestID];
  743. }];
  744. }
  745. - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response delegate:(id<NSURLConnectionDelegate>)delegate
  746. {
  747. [self performBlock:^{
  748. NSString *requestID = [[self class] requestIDForConnectionOrTask:connection];
  749. FLEXInternalRequestState *requestState = [self requestStateForRequestID:requestID];
  750. NSMutableData *dataAccumulator = nil;
  751. if (response.expectedContentLength < 0) {
  752. dataAccumulator = [[NSMutableData alloc] init];
  753. } else {
  754. dataAccumulator = [[NSMutableData alloc] initWithCapacity:(NSUInteger)response.expectedContentLength];
  755. }
  756. requestState.dataAccumulator = dataAccumulator;
  757. [[FLEXNetworkRecorder defaultRecorder] recordResponseReceivedWithRequestID:requestID response:response];
  758. }];
  759. }
  760. - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data delegate:(id<NSURLConnectionDelegate>)delegate
  761. {
  762. // Just to be safe since we're doing this async
  763. data = [data copy];
  764. [self performBlock:^{
  765. NSString *requestID = [[self class] requestIDForConnectionOrTask:connection];
  766. FLEXInternalRequestState *requestState = [self requestStateForRequestID:requestID];
  767. [requestState.dataAccumulator appendData:data];
  768. [[FLEXNetworkRecorder defaultRecorder] recordDataReceivedWithRequestID:requestID dataLength:data.length];
  769. }];
  770. }
  771. - (void)connectionDidFinishLoading:(NSURLConnection *)connection delegate:(id<NSURLConnectionDelegate>)delegate
  772. {
  773. [self performBlock:^{
  774. NSString *requestID = [[self class] requestIDForConnectionOrTask:connection];
  775. FLEXInternalRequestState *requestState = [self requestStateForRequestID:requestID];
  776. [[FLEXNetworkRecorder defaultRecorder] recordLoadingFinishedWithRequestID:requestID responseBody:requestState.dataAccumulator];
  777. [self removeRequestStateForRequestID:requestID];
  778. }];
  779. }
  780. - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error delegate:(id<NSURLConnectionDelegate>)delegate
  781. {
  782. [self performBlock:^{
  783. NSString *requestID = [[self class] requestIDForConnectionOrTask:connection];
  784. FLEXInternalRequestState *requestState = [self requestStateForRequestID:requestID];
  785. // Cancellations can occur prior to the willSendRequest:... NSURLConnection delegate call.
  786. // These are pretty common and clutter up the logs. Only record the failure if the recorder already knows about the request through willSendRequest:...
  787. if (requestState.request) {
  788. [[FLEXNetworkRecorder defaultRecorder] recordLoadingFailedWithRequestID:requestID error:error];
  789. }
  790. [self removeRequestStateForRequestID:requestID];
  791. }];
  792. }
  793. - (void)connectionWillCancel:(NSURLConnection *)connection
  794. {
  795. [self performBlock:^{
  796. // Mimic the behavior of NSURLSession which is to create an error on cancellation.
  797. NSDictionary *userInfo = @{ NSLocalizedDescriptionKey : @"cancelled" };
  798. NSError *error = [NSError errorWithDomain:NSURLErrorDomain code:NSURLErrorCancelled userInfo:userInfo];
  799. [self connection:connection didFailWithError:error delegate:nil];
  800. }];
  801. }
  802. @end
  803. @implementation FLEXNetworkObserver (NSURLSessionTaskHelpers)
  804. - (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task willPerformHTTPRedirection:(NSHTTPURLResponse *)response newRequest:(NSURLRequest *)request completionHandler:(void (^)(NSURLRequest *))completionHandler delegate:(id<NSURLSessionDelegate>)delegate
  805. {
  806. [self performBlock:^{
  807. NSString *requestID = [[self class] requestIDForConnectionOrTask:task];
  808. [[FLEXNetworkRecorder defaultRecorder] recordRequestWillBeSentWithRequestID:requestID request:request redirectResponse:response];
  809. }];
  810. }
  811. - (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didReceiveResponse:(NSURLResponse *)response completionHandler:(void (^)(NSURLSessionResponseDisposition disposition))completionHandler delegate:(id<NSURLSessionDelegate>)delegate
  812. {
  813. [self performBlock:^{
  814. NSString *requestID = [[self class] requestIDForConnectionOrTask:dataTask];
  815. FLEXInternalRequestState *requestState = [self requestStateForRequestID:requestID];
  816. NSMutableData *dataAccumulator = nil;
  817. if (response.expectedContentLength < 0) {
  818. dataAccumulator = [[NSMutableData alloc] init];
  819. } else {
  820. dataAccumulator = [[NSMutableData alloc] initWithCapacity:(NSUInteger)response.expectedContentLength];
  821. }
  822. requestState.dataAccumulator = dataAccumulator;
  823. NSString *requestMechanism = [NSString stringWithFormat:@"NSURLSessionDataTask (delegate: %@)", [delegate class]];
  824. [[FLEXNetworkRecorder defaultRecorder] recordMechanism:requestMechanism forRequestID:requestID];
  825. [[FLEXNetworkRecorder defaultRecorder] recordResponseReceivedWithRequestID:requestID response:response];
  826. }];
  827. }
  828. - (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didBecomeDownloadTask:(NSURLSessionDownloadTask *)downloadTask delegate:(id<NSURLSessionDelegate>)delegate
  829. {
  830. [self performBlock:^{
  831. // By setting the request ID of the download task to match the data task,
  832. // it can pick up where the data task left off.
  833. NSString *requestID = [[self class] requestIDForConnectionOrTask:dataTask];
  834. [[self class] setRequestID:requestID forConnectionOrTask:downloadTask];
  835. }];
  836. }
  837. - (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didReceiveData:(NSData *)data delegate:(id<NSURLSessionDelegate>)delegate
  838. {
  839. // Just to be safe since we're doing this async
  840. data = [data copy];
  841. [self performBlock:^{
  842. NSString *requestID = [[self class] requestIDForConnectionOrTask:dataTask];
  843. FLEXInternalRequestState *requestState = [self requestStateForRequestID:requestID];
  844. [requestState.dataAccumulator appendData:data];
  845. [[FLEXNetworkRecorder defaultRecorder] recordDataReceivedWithRequestID:requestID dataLength:data.length];
  846. }];
  847. }
  848. - (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(NSError *)error delegate:(id<NSURLSessionDelegate>)delegate
  849. {
  850. [self performBlock:^{
  851. NSString *requestID = [[self class] requestIDForConnectionOrTask:task];
  852. FLEXInternalRequestState *requestState = [self requestStateForRequestID:requestID];
  853. if (error) {
  854. [[FLEXNetworkRecorder defaultRecorder] recordLoadingFailedWithRequestID:requestID error:error];
  855. } else {
  856. [[FLEXNetworkRecorder defaultRecorder] recordLoadingFinishedWithRequestID:requestID responseBody:requestState.dataAccumulator];
  857. }
  858. [self removeRequestStateForRequestID:requestID];
  859. }];
  860. }
  861. - (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didWriteData:(int64_t)bytesWritten totalBytesWritten:(int64_t)totalBytesWritten totalBytesExpectedToWrite:(int64_t)totalBytesExpectedToWrite delegate:(id<NSURLSessionDelegate>)delegate
  862. {
  863. [self performBlock:^{
  864. NSString *requestID = [[self class] requestIDForConnectionOrTask:downloadTask];
  865. FLEXInternalRequestState *requestState = [self requestStateForRequestID:requestID];
  866. if (!requestState.dataAccumulator) {
  867. NSUInteger unsignedBytesExpectedToWrite = totalBytesExpectedToWrite > 0 ? (NSUInteger)totalBytesExpectedToWrite : 0;
  868. requestState.dataAccumulator = [[NSMutableData alloc] initWithCapacity:unsignedBytesExpectedToWrite];
  869. [[FLEXNetworkRecorder defaultRecorder] recordResponseReceivedWithRequestID:requestID response:downloadTask.response];
  870. NSString *requestMechanism = [NSString stringWithFormat:@"NSURLSessionDownloadTask (delegate: %@)", [delegate class]];
  871. [[FLEXNetworkRecorder defaultRecorder] recordMechanism:requestMechanism forRequestID:requestID];
  872. }
  873. [[FLEXNetworkRecorder defaultRecorder] recordDataReceivedWithRequestID:requestID dataLength:bytesWritten];
  874. }];
  875. }
  876. - (void)URLSession:(NSURLSession *)session task:(NSURLSessionDownloadTask *)downloadTask didFinishDownloadingToURL:(NSURL *)location data:(NSData *)data delegate:(id<NSURLSessionDelegate>)delegate
  877. {
  878. data = [data copy];
  879. [self performBlock:^{
  880. NSString *requestID = [[self class] requestIDForConnectionOrTask:downloadTask];
  881. FLEXInternalRequestState *requestState = [self requestStateForRequestID:requestID];
  882. [requestState.dataAccumulator appendData:data];
  883. }];
  884. }
  885. - (void)URLSessionTaskWillResume:(NSURLSessionTask *)task
  886. {
  887. // Since resume can be called multiple times on the same task, only treat the first resume as
  888. // the equivalent to connection:willSendRequest:...
  889. [self performBlock:^{
  890. NSString *requestID = [[self class] requestIDForConnectionOrTask:task];
  891. FLEXInternalRequestState *requestState = [self requestStateForRequestID:requestID];
  892. if (!requestState.request) {
  893. requestState.request = task.currentRequest;
  894. [[FLEXNetworkRecorder defaultRecorder] recordRequestWillBeSentWithRequestID:requestID request:task.currentRequest redirectResponse:nil];
  895. }
  896. }];
  897. }
  898. @end