FLEXNetworkObserver.m 68 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593
  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 "NSObject+FLEX_Reflection.h"
  18. #import "FLEXMethod.h"
  19. #import <objc/runtime.h>
  20. #import <objc/message.h>
  21. #import <dispatch/queue.h>
  22. #include <dlfcn.h>
  23. NSString *const kFLEXNetworkObserverEnabledStateChangedNotification = @"kFLEXNetworkObserverEnabledStateChangedNotification";
  24. static NSString *const kFLEXNetworkObserverEnabledDefaultsKey = @"com.flex.FLEXNetworkObserver.enableOnLaunch";
  25. typedef void (^NSURLSessionAsyncCompletion)(id fileURLOrData, NSURLResponse *response, NSError *error);
  26. typedef NSURLSessionTask * (^NSURLSessionNewTaskMethod)(NSURLSession *, id, NSURLSessionAsyncCompletion);
  27. @interface FLEXInternalRequestState : NSObject
  28. @property (nonatomic, copy) NSURLRequest *request;
  29. @property (nonatomic) NSMutableData *dataAccumulator;
  30. @end
  31. @implementation FLEXInternalRequestState
  32. @end
  33. @interface FLEXNetworkObserver (NSURLConnectionHelpers)
  34. - (void)connection:(NSURLConnection *)connection willSendRequest:(NSURLRequest *)request redirectResponse:(NSURLResponse *)response delegate:(id<NSURLConnectionDelegate>)delegate;
  35. - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response delegate:(id<NSURLConnectionDelegate>)delegate;
  36. - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data delegate:(id<NSURLConnectionDelegate>)delegate;
  37. - (void)connectionDidFinishLoading:(NSURLConnection *)connection delegate:(id<NSURLConnectionDelegate>)delegate;
  38. - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error delegate:(id<NSURLConnectionDelegate>)delegate;
  39. - (void)connectionWillCancel:(NSURLConnection *)connection;
  40. @end
  41. @interface FLEXNetworkObserver (NSURLSessionTaskHelpers)
  42. - (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task willPerformHTTPRedirection:(NSHTTPURLResponse *)response newRequest:(NSURLRequest *)request completionHandler:(void (^)(NSURLRequest *))completionHandler delegate:(id<NSURLSessionDelegate>)delegate;
  43. - (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didReceiveResponse:(NSURLResponse *)response completionHandler:(void (^)(NSURLSessionResponseDisposition disposition))completionHandler delegate:(id<NSURLSessionDelegate>)delegate;
  44. - (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didReceiveData:(NSData *)data delegate:(id<NSURLSessionDelegate>)delegate;
  45. - (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask
  46. didBecomeDownloadTask:(NSURLSessionDownloadTask *)downloadTask delegate:(id<NSURLSessionDelegate>)delegate;
  47. - (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(NSError *)error delegate:(id<NSURLSessionDelegate>)delegate;
  48. - (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didWriteData:(int64_t)bytesWritten totalBytesWritten:(int64_t)totalBytesWritten totalBytesExpectedToWrite:(int64_t)totalBytesExpectedToWrite delegate:(id<NSURLSessionDelegate>)delegate;
  49. - (void)URLSession:(NSURLSession *)session task:(NSURLSessionDownloadTask *)downloadTask didFinishDownloadingToURL:(NSURL *)location data:(NSData *)data delegate:(id<NSURLSessionDelegate>)delegate;
  50. - (void)URLSessionTaskWillResume:(NSURLSessionTask *)task;
  51. @end
  52. @interface FLEXNetworkObserver ()
  53. @property (nonatomic) NSMutableDictionary<NSString *, FLEXInternalRequestState *> *requestStatesForRequestIDs;
  54. @property (nonatomic) dispatch_queue_t queue;
  55. @end
  56. @implementation FLEXNetworkObserver
  57. #pragma mark - Public Methods
  58. + (void)setEnabled:(BOOL)enabled {
  59. BOOL previouslyEnabled = [self isEnabled];
  60. [NSUserDefaults.standardUserDefaults setBool:enabled forKey:kFLEXNetworkObserverEnabledDefaultsKey];
  61. if (enabled) {
  62. // Inject if needed. This injection is protected with a dispatch_once, so we're ok calling it multiple times.
  63. // By doing the injection lazily, we keep the impact of the tool lower when this feature isn't enabled.
  64. [self injectIntoAllNSURLConnectionDelegateClasses];
  65. }
  66. if (previouslyEnabled != enabled) {
  67. [NSNotificationCenter.defaultCenter postNotificationName:kFLEXNetworkObserverEnabledStateChangedNotification object:self];
  68. }
  69. }
  70. + (BOOL)isEnabled {
  71. return [[NSUserDefaults.standardUserDefaults objectForKey:kFLEXNetworkObserverEnabledDefaultsKey] boolValue];
  72. }
  73. + (void)load {
  74. // We don't want to do the swizzling from +load because not all the classes may be loaded at this point.
  75. dispatch_async(dispatch_get_main_queue(), ^{
  76. if ([self isEnabled]) {
  77. [self injectIntoAllNSURLConnectionDelegateClasses];
  78. }
  79. });
  80. }
  81. #pragma mark - Statics
  82. + (instancetype)sharedObserver {
  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. return NSUUID.UUID.UUIDString;
  92. }
  93. #pragma mark Delegate Injection Convenience Methods
  94. /// All swizzled delegate methods should make use of this guard.
  95. /// This will prevent duplicated sniffing when the original implementation calls up to a superclass implementation which we've also swizzled.
  96. /// The superclass implementation (and implementations in classes above that) will be executed without interference if called from the original implementation.
  97. + (void)sniffWithoutDuplicationForObject:(NSObject *)object selector:(SEL)selector sniffingBlock:(void (^)(void))sniffingBlock originalImplementationBlock:(void (^)(void))originalImplementationBlock {
  98. // If we don't have an object to detect nested calls on, just run the original implementation and bail.
  99. // This case can happen if someone besides the URL loading system calls the delegate methods directly.
  100. // See https://github.com/Flipboard/FLEX/issues/61 for an example.
  101. if (!object) {
  102. originalImplementationBlock();
  103. return;
  104. }
  105. const void *key = selector;
  106. // Don't run the sniffing block if we're inside a nested call
  107. if (!objc_getAssociatedObject(object, key)) {
  108. sniffingBlock();
  109. }
  110. // Mark that we're calling through to the original so we can detect nested calls
  111. objc_setAssociatedObject(object, key, @YES, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
  112. originalImplementationBlock();
  113. objc_setAssociatedObject(object, key, nil, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
  114. }
  115. #pragma mark - Delegate Injection
  116. + (void)injectIntoAllNSURLConnectionDelegateClasses {
  117. // Only allow swizzling once.
  118. static dispatch_once_t onceToken;
  119. dispatch_once(&onceToken, ^{
  120. // Swizzle any classes that implement one of these selectors.
  121. const SEL selectors[] = {
  122. @selector(connectionDidFinishLoading:),
  123. @selector(connection:willSendRequest:redirectResponse:),
  124. @selector(connection:didReceiveResponse:),
  125. @selector(connection:didReceiveData:),
  126. @selector(connection:didFailWithError:),
  127. @selector(URLSession:task:willPerformHTTPRedirection:newRequest:completionHandler:),
  128. @selector(URLSession:dataTask:didReceiveData:),
  129. @selector(URLSession:dataTask:didReceiveResponse:completionHandler:),
  130. @selector(URLSession:task:didCompleteWithError:),
  131. @selector(URLSession:dataTask:didBecomeDownloadTask:),
  132. @selector(URLSession:downloadTask:didWriteData:totalBytesWritten:totalBytesExpectedToWrite:),
  133. @selector(URLSession:downloadTask:didFinishDownloadingToURL:)
  134. };
  135. const int numSelectors = sizeof(selectors) / sizeof(SEL);
  136. Class *classes = NULL;
  137. int numClasses = objc_getClassList(NULL, 0);
  138. if (numClasses > 0) {
  139. classes = (__unsafe_unretained Class *)malloc(sizeof(Class) * numClasses);
  140. numClasses = objc_getClassList(classes, numClasses);
  141. for (NSInteger classIndex = 0; classIndex < numClasses; ++classIndex) {
  142. Class class = classes[classIndex];
  143. if (class == [FLEXNetworkObserver class]) {
  144. continue;
  145. }
  146. // Use the C API rather than NSObject methods to avoid sending messages
  147. // to classes we're not interested in swizzling, which could result
  148. // in us calling +initialize on potentially uninitialized classes.
  149. // NOTE: calling class_getInstanceMethod() DOES send +initialize
  150. // to the class. That's why we iterate through the method list.
  151. unsigned int methodCount = 0;
  152. Method *methods = class_copyMethodList(class, &methodCount);
  153. BOOL matchingSelectorFound = NO;
  154. for (unsigned int methodIndex = 0; methodIndex < methodCount; methodIndex++) {
  155. for (int selectorIndex = 0; selectorIndex < numSelectors; ++selectorIndex) {
  156. if (method_getName(methods[methodIndex]) == selectors[selectorIndex]) {
  157. [self injectIntoDelegateClass:class];
  158. matchingSelectorFound = YES;
  159. break;
  160. }
  161. }
  162. if (matchingSelectorFound) {
  163. break;
  164. }
  165. }
  166. free(methods);
  167. }
  168. free(classes);
  169. }
  170. [self injectIntoNSURLConnectionCancel];
  171. [self injectIntoNSURLSessionTaskResume];
  172. [self injectIntoNSURLConnectionAsynchronousClassMethod];
  173. [self injectIntoNSURLConnectionSynchronousClassMethod];
  174. [self injectIntoNSURLSessionAsyncDataAndDownloadTaskMethods];
  175. [self injectIntoNSURLSessionAsyncUploadTaskMethods];
  176. });
  177. }
  178. + (void)injectIntoDelegateClass:(Class)cls {
  179. // Connections
  180. [self injectWillSendRequestIntoDelegateClass:cls];
  181. [self injectDidReceiveDataIntoDelegateClass:cls];
  182. [self injectDidReceiveResponseIntoDelegateClass:cls];
  183. [self injectDidFinishLoadingIntoDelegateClass:cls];
  184. [self injectDidFailWithErrorIntoDelegateClass:cls];
  185. // Sessions
  186. [self injectTaskWillPerformHTTPRedirectionIntoDelegateClass:cls];
  187. [self injectTaskDidReceiveDataIntoDelegateClass:cls];
  188. [self injectTaskDidReceiveResponseIntoDelegateClass:cls];
  189. [self injectTaskDidCompleteWithErrorIntoDelegateClass:cls];
  190. [self injectRespondsToSelectorIntoDelegateClass:cls];
  191. // Data tasks
  192. [self injectDataTaskDidBecomeDownloadTaskIntoDelegateClass:cls];
  193. // Download tasks
  194. [self injectDownloadTaskDidWriteDataIntoDelegateClass:cls];
  195. [self injectDownloadTaskDidFinishDownloadingIntoDelegateClass:cls];
  196. }
  197. + (void)injectIntoNSURLConnectionCancel {
  198. static dispatch_once_t onceToken;
  199. dispatch_once(&onceToken, ^{
  200. Class class = [NSURLConnection class];
  201. SEL selector = @selector(cancel);
  202. SEL swizzledSelector = [FLEXUtility swizzledSelectorForSelector:selector];
  203. Method originalCancel = class_getInstanceMethod(class, selector);
  204. void (^swizzleBlock)(NSURLConnection *) = ^(NSURLConnection *slf) {
  205. [FLEXNetworkObserver.sharedObserver connectionWillCancel:slf];
  206. ((void(*)(id, SEL))objc_msgSend)(
  207. slf, swizzledSelector
  208. );
  209. };
  210. IMP implementation = imp_implementationWithBlock(swizzleBlock);
  211. class_addMethod(class, swizzledSelector, implementation, method_getTypeEncoding(originalCancel));
  212. Method newCancel = class_getInstanceMethod(class, swizzledSelector);
  213. method_exchangeImplementations(originalCancel, newCancel);
  214. });
  215. }
  216. + (void)injectIntoNSURLSessionTaskResume {
  217. static dispatch_once_t onceToken;
  218. dispatch_once(&onceToken, ^{
  219. // In iOS 7 resume lives in __NSCFLocalSessionTask
  220. // In iOS 8 resume lives in NSURLSessionTask
  221. // In iOS 9 resume lives in __NSCFURLSessionTask
  222. // In iOS 14 resume lives in NSURLSessionTask
  223. Class baseResumeClass = Nil;
  224. if (![NSProcessInfo.processInfo respondsToSelector:@selector(operatingSystemVersion)]) {
  225. // iOS ... 7
  226. baseResumeClass = NSClassFromString(@"__NSCFLocalSessionTask");
  227. } else {
  228. NSInteger majorVersion = NSProcessInfo.processInfo.operatingSystemVersion.majorVersion;
  229. if (majorVersion < 9 || majorVersion >= 14) {
  230. // iOS 8 or iOS 14+
  231. baseResumeClass = [NSURLSessionTask class];
  232. } else {
  233. // iOS 9 ... 13
  234. baseResumeClass = NSClassFromString(@"__NSCFURLSessionTask");
  235. }
  236. }
  237. // Hook the base implementation of -resume
  238. IMP originalResume = [baseResumeClass instanceMethodForSelector:@selector(resume)];
  239. [self swizzleResumeSelector:@selector(resume) forClass:baseResumeClass];
  240. // *Sigh*
  241. //
  242. // So, multiple versions of AFNetworking 2.5.X swizzle -resume in various and
  243. // short-sighted ways. If you look through the version history from 2.5.0 upwards,
  244. // you'll see a variety of techniques were tried, including taking a private
  245. // subclass of NSURLSessionTask and calling class_addMethod with `originalResume`
  246. // below, so that a duplicate implementation of -resume exists in that class.
  247. //
  248. // This technique in particular is troublesome, because the implementation in
  249. // `baseResumeClass` is never called at all, which means our swizzle is never invoked.
  250. //
  251. // The only solution is a brute-force one: we must loop over the class tree
  252. // below `baseResumeClass` and check for all classes that implement `af_resume`.
  253. // if the IMP corresponding to that method is equal to `originalResume` then we
  254. // swizzle that in addition to swizzling `resume` on `baseResumeClass` above.
  255. //
  256. // However, we only go to the trouble at all if NSSelectorFromString
  257. // can even find an `"af_resume"` selector in the first place.
  258. SEL sel_af_resume = NSSelectorFromString(@"af_resume");
  259. if (sel_af_resume) {
  260. NSMutableArray<Class> *classTree = FLEXGetAllSubclasses(baseResumeClass, NO).mutableCopy;
  261. for (NSInteger i = 0; i < classTree.count; i++) {
  262. [classTree addObjectsFromArray:FLEXGetAllSubclasses(classTree[i], NO)];
  263. }
  264. for (Class current in classTree) {
  265. IMP af_resume = [current instanceMethodForSelector:sel_af_resume];
  266. if (af_resume == originalResume) {
  267. [self swizzleResumeSelector:sel_af_resume forClass:current];
  268. }
  269. }
  270. }
  271. });
  272. }
  273. + (void)swizzleResumeSelector:(SEL)selector forClass:(Class)class {
  274. SEL swizzledSelector = [FLEXUtility swizzledSelectorForSelector:selector];
  275. Method originalResume = class_getInstanceMethod(class, selector);
  276. IMP implementation = imp_implementationWithBlock(^(NSURLSessionTask *slf) {
  277. // iOS's internal HTTP parser finalization code is mysteriously not thread safe,
  278. // invoking it asynchronously has a chance to cause a `double free` crash.
  279. // This line below will ask for HTTPBody synchronously, make the HTTPParser
  280. // parse the request, and cache them in advance. After that the HTTPParser
  281. // will be finalized. Make sure other threads inspecting the request
  282. // won't trigger a race to finalize the parser.
  283. [slf.currentRequest HTTPBody];
  284. [FLEXNetworkObserver.sharedObserver URLSessionTaskWillResume:slf];
  285. ((void(*)(id, SEL))objc_msgSend)(
  286. slf, swizzledSelector
  287. );
  288. });
  289. class_addMethod(class, swizzledSelector, implementation, method_getTypeEncoding(originalResume));
  290. Method newResume = class_getInstanceMethod(class, swizzledSelector);
  291. method_exchangeImplementations(originalResume, newResume);
  292. }
  293. + (void)injectIntoNSURLConnectionAsynchronousClassMethod {
  294. static dispatch_once_t onceToken;
  295. dispatch_once(&onceToken, ^{
  296. Class class = objc_getMetaClass(class_getName([NSURLConnection class]));
  297. SEL selector = @selector(sendAsynchronousRequest:queue:completionHandler:);
  298. SEL swizzledSelector = [FLEXUtility swizzledSelectorForSelector:selector];
  299. typedef void (^AsyncCompletion)(
  300. NSURLResponse *response, NSData *data, NSError *error
  301. );
  302. typedef void (^SendAsyncRequestBlock)(
  303. Class, NSURLRequest *, NSOperationQueue *, AsyncCompletion
  304. );
  305. SendAsyncRequestBlock swizzleBlock = ^(Class slf,
  306. NSURLRequest *request,
  307. NSOperationQueue *queue,
  308. AsyncCompletion completion) {
  309. if (FLEXNetworkObserver.isEnabled) {
  310. NSString *requestID = [self nextRequestID];
  311. [FLEXNetworkRecorder.defaultRecorder
  312. recordRequestWillBeSentWithRequestID:requestID
  313. request:request
  314. redirectResponse:nil
  315. ];
  316. NSString *mechanism = [self mechanismFromClassMethod:selector onClass:class];
  317. [FLEXNetworkRecorder.defaultRecorder recordMechanism:mechanism forRequestID:requestID];
  318. AsyncCompletion wrapper = ^(NSURLResponse *response, NSData *data, NSError *error) {
  319. [FLEXNetworkRecorder.defaultRecorder
  320. recordResponseReceivedWithRequestID:requestID
  321. response:response
  322. ];
  323. [FLEXNetworkRecorder.defaultRecorder
  324. recordDataReceivedWithRequestID:requestID
  325. dataLength:data.length
  326. ];
  327. if (error) {
  328. [FLEXNetworkRecorder.defaultRecorder
  329. recordLoadingFailedWithRequestID:requestID
  330. error:error
  331. ];
  332. } else {
  333. [FLEXNetworkRecorder.defaultRecorder
  334. recordLoadingFinishedWithRequestID:requestID
  335. responseBody:data
  336. ];
  337. }
  338. // Call through to the original completion handler
  339. if (completion) {
  340. completion(response, data, error);
  341. }
  342. };
  343. ((void(*)(id, SEL, id, id, id))objc_msgSend)(
  344. slf, swizzledSelector, request, queue, wrapper
  345. );
  346. } else {
  347. ((void(*)(id, SEL, id, id, id))objc_msgSend)(
  348. slf, swizzledSelector, request, queue, completion
  349. );
  350. }
  351. };
  352. [FLEXUtility replaceImplementationOfKnownSelector:selector
  353. onClass:class withBlock:swizzleBlock swizzledSelector:swizzledSelector
  354. ];
  355. });
  356. }
  357. + (void)injectIntoNSURLConnectionSynchronousClassMethod {
  358. static dispatch_once_t onceToken;
  359. dispatch_once(&onceToken, ^{
  360. Class class = objc_getMetaClass(class_getName([NSURLConnection class]));
  361. SEL selector = @selector(sendSynchronousRequest:returningResponse:error:);
  362. SEL swizzledSelector = [FLEXUtility swizzledSelectorForSelector:selector];
  363. typedef NSData * (^AsyncCompletion)(Class, NSURLRequest *, NSURLResponse **, NSError **);
  364. AsyncCompletion swizzleBlock = ^NSData *(Class slf,
  365. NSURLRequest *request,
  366. NSURLResponse **response,
  367. NSError **error) {
  368. NSData *data = nil;
  369. if (FLEXNetworkObserver.isEnabled) {
  370. NSString *requestID = [self nextRequestID];
  371. [FLEXNetworkRecorder.defaultRecorder
  372. recordRequestWillBeSentWithRequestID:requestID
  373. request:request
  374. redirectResponse:nil
  375. ];
  376. NSString *mechanism = [self mechanismFromClassMethod:selector onClass:class];
  377. [FLEXNetworkRecorder.defaultRecorder recordMechanism:mechanism forRequestID:requestID];
  378. NSError *temporaryError = nil;
  379. NSURLResponse *temporaryResponse = nil;
  380. data = ((id(*)(id, SEL, id, NSURLResponse **, NSError **))objc_msgSend)(
  381. slf, swizzledSelector, request, &temporaryResponse, &temporaryError
  382. );
  383. [FLEXNetworkRecorder.defaultRecorder
  384. recordResponseReceivedWithRequestID:requestID
  385. response:temporaryResponse
  386. ];
  387. [FLEXNetworkRecorder.defaultRecorder
  388. recordDataReceivedWithRequestID:requestID
  389. dataLength:data.length
  390. ];
  391. if (temporaryError) {
  392. [FLEXNetworkRecorder.defaultRecorder
  393. recordLoadingFailedWithRequestID:requestID
  394. error:temporaryError
  395. ];
  396. } else {
  397. [FLEXNetworkRecorder.defaultRecorder
  398. recordLoadingFinishedWithRequestID:requestID
  399. responseBody:data
  400. ];
  401. }
  402. if (error) {
  403. *error = temporaryError;
  404. }
  405. if (response) {
  406. *response = temporaryResponse;
  407. }
  408. } else {
  409. data = ((id(*)(id, SEL, id, NSURLResponse **, NSError **))objc_msgSend)(
  410. slf, swizzledSelector, request, response, error
  411. );
  412. }
  413. return data;
  414. };
  415. [FLEXUtility replaceImplementationOfKnownSelector:selector
  416. onClass:class withBlock:swizzleBlock swizzledSelector:swizzledSelector
  417. ];
  418. });
  419. }
  420. + (void)injectIntoNSURLSessionAsyncDataAndDownloadTaskMethods {
  421. static dispatch_once_t onceToken;
  422. dispatch_once(&onceToken, ^{
  423. Class class = [NSURLSession class];
  424. // The method signatures here are close enough that
  425. // we can use the same logic to inject into all of them.
  426. const SEL selectors[] = {
  427. @selector(dataTaskWithRequest:completionHandler:),
  428. @selector(dataTaskWithURL:completionHandler:),
  429. @selector(downloadTaskWithRequest:completionHandler:),
  430. @selector(downloadTaskWithResumeData:completionHandler:),
  431. @selector(downloadTaskWithURL:completionHandler:)
  432. };
  433. const int numSelectors = sizeof(selectors) / sizeof(SEL);
  434. for (int selectorIndex = 0; selectorIndex < numSelectors; selectorIndex++) {
  435. SEL selector = selectors[selectorIndex];
  436. SEL swizzledSelector = [FLEXUtility swizzledSelectorForSelector:selector];
  437. if ([FLEXUtility instanceRespondsButDoesNotImplementSelector:selector class:class]) {
  438. // iOS 7 does not implement these methods on NSURLSession. We actually want to
  439. // swizzle __NSCFURLSession, which we can get from the class of the shared session
  440. class = [NSURLSession.sharedSession class];
  441. }
  442. typedef NSURLSessionTask * (^NSURLSessionNewTaskMethod)(
  443. NSURLSession *, id, NSURLSessionAsyncCompletion
  444. );
  445. NSURLSessionNewTaskMethod swizzleBlock = ^NSURLSessionTask *(NSURLSession *slf,
  446. id argument,
  447. NSURLSessionAsyncCompletion completion) {
  448. NSURLSessionTask *task = nil;
  449. // Check if network observing is on and a callback was provided
  450. if (FLEXNetworkObserver.isEnabled && completion) {
  451. NSString *requestID = [self nextRequestID];
  452. NSString *mechanism = [self mechanismFromClassMethod:selector onClass:class];
  453. // "Hook" the completion block
  454. NSURLSessionAsyncCompletion completionWrapper = [self
  455. asyncCompletionWrapperForRequestID:requestID
  456. mechanism:mechanism
  457. completion:completion
  458. ];
  459. // Call the original method
  460. task = ((id(*)(id, SEL, id, id))objc_msgSend)(
  461. slf, swizzledSelector, argument, completionWrapper
  462. );
  463. [self setRequestID:requestID forConnectionOrTask:task];
  464. } else {
  465. // Network observer disabled or no callback provided,
  466. // just pass through to the original method
  467. task = ((id(*)(id, SEL, id, id))objc_msgSend)(
  468. slf, swizzledSelector, argument, completion
  469. );
  470. }
  471. return task;
  472. };
  473. // Actually swizzle
  474. [FLEXUtility replaceImplementationOfKnownSelector:selector
  475. onClass:class withBlock:swizzleBlock swizzledSelector:swizzledSelector
  476. ];
  477. }
  478. });
  479. }
  480. + (void)injectIntoNSURLSessionAsyncUploadTaskMethods {
  481. static dispatch_once_t onceToken;
  482. dispatch_once(&onceToken, ^{
  483. Class class = [NSURLSession class];
  484. // The method signatures here are close enough that we can use the same logic to inject into both of them.
  485. // Note that they have 3 arguments, so we can't easily combine with the data and download method above.
  486. typedef NSURLSessionUploadTask *(^UploadTaskMethod)(
  487. NSURLSession *, NSURLRequest *, id, NSURLSessionAsyncCompletion
  488. );
  489. const SEL selectors[] = {
  490. @selector(uploadTaskWithRequest:fromData:completionHandler:),
  491. @selector(uploadTaskWithRequest:fromFile:completionHandler:)
  492. };
  493. const int numSelectors = sizeof(selectors) / sizeof(SEL);
  494. for (int selectorIndex = 0; selectorIndex < numSelectors; selectorIndex++) {
  495. SEL selector = selectors[selectorIndex];
  496. SEL swizzledSelector = [FLEXUtility swizzledSelectorForSelector:selector];
  497. if ([FLEXUtility instanceRespondsButDoesNotImplementSelector:selector class:class]) {
  498. // iOS 7 does not implement these methods on NSURLSession. We actually want to
  499. // swizzle __NSCFURLSession, which we can get from the class of the shared session
  500. class = [NSURLSession.sharedSession class];
  501. }
  502. UploadTaskMethod swizzleBlock = ^NSURLSessionUploadTask *(NSURLSession * slf,
  503. NSURLRequest *request,
  504. id argument,
  505. NSURLSessionAsyncCompletion completion) {
  506. NSURLSessionUploadTask *task = nil;
  507. if (FLEXNetworkObserver.isEnabled && completion) {
  508. NSString *requestID = [self nextRequestID];
  509. NSString *mechanism = [self mechanismFromClassMethod:selector onClass:class];
  510. NSURLSessionAsyncCompletion completionWrapper = [self
  511. asyncCompletionWrapperForRequestID:requestID
  512. mechanism:mechanism
  513. completion:completion
  514. ];
  515. task = ((id(*)(id, SEL, id, id, id))objc_msgSend)(
  516. slf, swizzledSelector, request, argument, completionWrapper
  517. );
  518. [self setRequestID:requestID forConnectionOrTask:task];
  519. } else {
  520. task = ((id(*)(id, SEL, id, id, id))objc_msgSend)(
  521. slf, swizzledSelector, request, argument, completion
  522. );
  523. }
  524. return task;
  525. };
  526. [FLEXUtility replaceImplementationOfKnownSelector:selector
  527. onClass:class withBlock:swizzleBlock swizzledSelector:swizzledSelector
  528. ];
  529. }
  530. });
  531. }
  532. + (NSString *)mechanismFromClassMethod:(SEL)selector onClass:(Class)class {
  533. return [NSString stringWithFormat:@"+[%@ %@]", NSStringFromClass(class), NSStringFromSelector(selector)];
  534. }
  535. + (NSURLSessionAsyncCompletion)asyncCompletionWrapperForRequestID:(NSString *)requestID
  536. mechanism:(NSString *)mechanism
  537. completion:(NSURLSessionAsyncCompletion)completion {
  538. NSURLSessionAsyncCompletion completionWrapper = ^(id fileURLOrData, NSURLResponse *response, NSError *error) {
  539. [FLEXNetworkRecorder.defaultRecorder recordMechanism:mechanism forRequestID:requestID];
  540. [FLEXNetworkRecorder.defaultRecorder
  541. recordResponseReceivedWithRequestID:requestID
  542. response:response
  543. ];
  544. NSData *data = nil;
  545. if ([fileURLOrData isKindOfClass:[NSURL class]]) {
  546. data = [NSData dataWithContentsOfURL:fileURLOrData];
  547. } else if ([fileURLOrData isKindOfClass:[NSData class]]) {
  548. data = fileURLOrData;
  549. }
  550. [FLEXNetworkRecorder.defaultRecorder
  551. recordDataReceivedWithRequestID:requestID
  552. dataLength:data.length
  553. ];
  554. if (error) {
  555. [FLEXNetworkRecorder.defaultRecorder
  556. recordLoadingFailedWithRequestID:requestID
  557. error:error
  558. ];
  559. } else {
  560. [FLEXNetworkRecorder.defaultRecorder
  561. recordLoadingFinishedWithRequestID:requestID
  562. responseBody:data
  563. ];
  564. }
  565. // Call through to the original completion handler
  566. if (completion) {
  567. completion(fileURLOrData, response, error);
  568. }
  569. };
  570. return completionWrapper;
  571. }
  572. + (void)injectWillSendRequestIntoDelegateClass:(Class)cls {
  573. SEL selector = @selector(connection:willSendRequest:redirectResponse:);
  574. SEL swizzledSelector = [FLEXUtility swizzledSelectorForSelector:selector];
  575. Protocol *protocol = @protocol(NSURLConnectionDataDelegate);
  576. protocol = protocol ?: @protocol(NSURLConnectionDelegate);
  577. struct objc_method_description methodDescription = protocol_getMethodDescription(
  578. protocol, selector, NO, YES
  579. );
  580. typedef NSURLRequest *(^WillSendRequestBlock)(
  581. id<NSURLConnectionDelegate> slf, NSURLConnection *connection,
  582. NSURLRequest *request, NSURLResponse *response
  583. );
  584. WillSendRequestBlock undefinedBlock = ^NSURLRequest *(id slf,
  585. NSURLConnection *connection,
  586. NSURLRequest *request,
  587. NSURLResponse *response) {
  588. [FLEXNetworkObserver.sharedObserver
  589. connection:connection
  590. willSendRequest:request
  591. redirectResponse:response
  592. delegate:slf
  593. ];
  594. return request;
  595. };
  596. WillSendRequestBlock implementationBlock = ^NSURLRequest *(id slf,
  597. NSURLConnection *connection,
  598. NSURLRequest *request,
  599. NSURLResponse *response) {
  600. __block NSURLRequest *returnValue = nil;
  601. [self sniffWithoutDuplicationForObject:connection selector:selector sniffingBlock:^{
  602. undefinedBlock(slf, connection, request, response);
  603. } originalImplementationBlock:^{
  604. returnValue = ((id(*)(id, SEL, id, id, id))objc_msgSend)(
  605. slf, swizzledSelector, connection, request, response
  606. );
  607. }];
  608. return returnValue;
  609. };
  610. [FLEXUtility replaceImplementationOfSelector:selector
  611. withSelector:swizzledSelector
  612. forClass:cls
  613. withMethodDescription:methodDescription
  614. implementationBlock:implementationBlock
  615. undefinedBlock:undefinedBlock
  616. ];
  617. }
  618. + (void)injectDidReceiveResponseIntoDelegateClass:(Class)cls {
  619. SEL selector = @selector(connection:didReceiveResponse:);
  620. SEL swizzledSelector = [FLEXUtility swizzledSelectorForSelector:selector];
  621. Protocol *protocol = @protocol(NSURLConnectionDataDelegate);
  622. protocol = protocol ?: @protocol(NSURLConnectionDelegate);
  623. struct objc_method_description description = protocol_getMethodDescription(
  624. protocol, selector, NO, YES
  625. );
  626. typedef void (^DidReceiveResponseBlock)(
  627. id<NSURLConnectionDelegate> slf, NSURLConnection *connection, NSURLResponse *response
  628. );
  629. DidReceiveResponseBlock undefinedBlock = ^(id<NSURLConnectionDelegate> slf,
  630. NSURLConnection *connection,
  631. NSURLResponse *response) {
  632. [FLEXNetworkObserver.sharedObserver connection:connection
  633. didReceiveResponse:response delegate:slf
  634. ];
  635. };
  636. DidReceiveResponseBlock implementationBlock = ^(id<NSURLConnectionDelegate> slf,
  637. NSURLConnection *connection,
  638. NSURLResponse *response) {
  639. [self sniffWithoutDuplicationForObject:connection selector:selector sniffingBlock:^{
  640. undefinedBlock(slf, connection, response);
  641. } originalImplementationBlock:^{
  642. ((void(*)(id, SEL, id, id))objc_msgSend)(
  643. slf, swizzledSelector, connection, response
  644. );
  645. }];
  646. };
  647. [FLEXUtility replaceImplementationOfSelector:selector
  648. withSelector:swizzledSelector
  649. forClass:cls
  650. withMethodDescription:description
  651. implementationBlock:implementationBlock
  652. undefinedBlock:undefinedBlock
  653. ];
  654. }
  655. + (void)injectDidReceiveDataIntoDelegateClass:(Class)cls {
  656. SEL selector = @selector(connection:didReceiveData:);
  657. SEL swizzledSelector = [FLEXUtility swizzledSelectorForSelector:selector];
  658. Protocol *protocol = @protocol(NSURLConnectionDataDelegate);
  659. protocol = protocol ?: @protocol(NSURLConnectionDelegate);
  660. struct objc_method_description description = protocol_getMethodDescription(
  661. protocol, selector, NO, YES
  662. );
  663. typedef void (^DidReceiveDataBlock)(
  664. id<NSURLConnectionDelegate> slf, NSURLConnection *connection, NSData *data
  665. );
  666. DidReceiveDataBlock undefinedBlock = ^(id<NSURLConnectionDelegate> slf,
  667. NSURLConnection *connection,
  668. NSData *data) {
  669. [FLEXNetworkObserver.sharedObserver connection:connection
  670. didReceiveData:data delegate:slf
  671. ];
  672. };
  673. DidReceiveDataBlock implementationBlock = ^(id<NSURLConnectionDelegate> slf,
  674. NSURLConnection *connection,
  675. NSData *data) {
  676. [self sniffWithoutDuplicationForObject:connection selector:selector sniffingBlock:^{
  677. undefinedBlock(slf, connection, data);
  678. } originalImplementationBlock:^{
  679. ((void(*)(id, SEL, id, id))objc_msgSend)(
  680. slf, swizzledSelector, connection, data
  681. );
  682. }];
  683. };
  684. [FLEXUtility replaceImplementationOfSelector:selector
  685. withSelector:swizzledSelector
  686. forClass:cls
  687. withMethodDescription:description
  688. implementationBlock:implementationBlock
  689. undefinedBlock:undefinedBlock
  690. ];
  691. }
  692. + (void)injectDidFinishLoadingIntoDelegateClass:(Class)cls {
  693. SEL selector = @selector(connectionDidFinishLoading:);
  694. SEL swizzledSelector = [FLEXUtility swizzledSelectorForSelector:selector];
  695. Protocol *protocol = @protocol(NSURLConnectionDataDelegate);
  696. protocol = protocol ?: @protocol(NSURLConnectionDelegate);
  697. struct objc_method_description description = protocol_getMethodDescription(
  698. protocol, selector, NO, YES
  699. );
  700. typedef void (^FinishLoadingBlock)(id<NSURLConnectionDelegate> slf, NSURLConnection *connection);
  701. FinishLoadingBlock undefinedBlock = ^(id<NSURLConnectionDelegate> slf, NSURLConnection *connection) {
  702. [FLEXNetworkObserver.sharedObserver connectionDidFinishLoading:connection delegate:slf];
  703. };
  704. FinishLoadingBlock implementationBlock = ^(id<NSURLConnectionDelegate> slf, NSURLConnection *connection) {
  705. [self sniffWithoutDuplicationForObject:connection selector:selector sniffingBlock:^{
  706. undefinedBlock(slf, connection);
  707. } originalImplementationBlock:^{
  708. ((void(*)(id, SEL, id))objc_msgSend)(
  709. slf, swizzledSelector, connection
  710. );
  711. }];
  712. };
  713. [FLEXUtility replaceImplementationOfSelector:selector
  714. withSelector:swizzledSelector forClass:cls
  715. withMethodDescription:description
  716. implementationBlock:implementationBlock
  717. undefinedBlock:undefinedBlock
  718. ];
  719. }
  720. + (void)injectDidFailWithErrorIntoDelegateClass:(Class)cls {
  721. SEL selector = @selector(connection:didFailWithError:);
  722. SEL swizzledSelector = [FLEXUtility swizzledSelectorForSelector:selector];
  723. struct objc_method_description description = protocol_getMethodDescription(
  724. @protocol(NSURLConnectionDelegate), selector, NO, YES
  725. );
  726. typedef void (^DidFailWithErrorBlock)(
  727. id<NSURLConnectionDelegate> slf, NSURLConnection *connection, NSError *error
  728. );
  729. DidFailWithErrorBlock undefinedBlock = ^(id<NSURLConnectionDelegate> slf,
  730. NSURLConnection *connection,
  731. NSError *error) {
  732. [FLEXNetworkObserver.sharedObserver connection:connection
  733. didFailWithError:error delegate:slf
  734. ];
  735. };
  736. DidFailWithErrorBlock implementationBlock = ^(id<NSURLConnectionDelegate> slf,
  737. NSURLConnection *connection,
  738. NSError *error) {
  739. [self sniffWithoutDuplicationForObject:connection selector:selector sniffingBlock:^{
  740. undefinedBlock(slf, connection, error);
  741. } originalImplementationBlock:^{
  742. ((void(*)(id, SEL, id, id))objc_msgSend)(
  743. slf, swizzledSelector, connection, error
  744. );
  745. }];
  746. };
  747. [FLEXUtility replaceImplementationOfSelector:selector
  748. withSelector:swizzledSelector forClass:cls
  749. withMethodDescription:description
  750. implementationBlock:implementationBlock
  751. undefinedBlock:undefinedBlock
  752. ];
  753. }
  754. + (void)injectTaskWillPerformHTTPRedirectionIntoDelegateClass:(Class)cls {
  755. SEL selector = @selector(URLSession:task:willPerformHTTPRedirection:newRequest:completionHandler:);
  756. SEL swizzledSelector = [FLEXUtility swizzledSelectorForSelector:selector];
  757. struct objc_method_description description = protocol_getMethodDescription(
  758. @protocol(NSURLSessionTaskDelegate), selector, NO, YES
  759. );
  760. typedef void (^HTTPRedirectionBlock)(id<NSURLSessionTaskDelegate> slf,
  761. NSURLSession *session,
  762. NSURLSessionTask *task,
  763. NSHTTPURLResponse *response,
  764. NSURLRequest *newRequest,
  765. void(^completionHandler)(NSURLRequest *));
  766. HTTPRedirectionBlock undefinedBlock = ^(id<NSURLSessionTaskDelegate> slf,
  767. NSURLSession *session,
  768. NSURLSessionTask *task,
  769. NSHTTPURLResponse *response,
  770. NSURLRequest *newRequest,
  771. void(^completionHandler)(NSURLRequest *)) {
  772. [FLEXNetworkObserver.sharedObserver
  773. URLSession:session task:task
  774. willPerformHTTPRedirection:response
  775. newRequest:newRequest
  776. completionHandler:completionHandler
  777. delegate:slf
  778. ];
  779. completionHandler(newRequest);
  780. };
  781. HTTPRedirectionBlock implementationBlock = ^(id<NSURLSessionTaskDelegate> slf,
  782. NSURLSession *session,
  783. NSURLSessionTask *task,
  784. NSHTTPURLResponse *response,
  785. NSURLRequest *newRequest,
  786. void(^completionHandler)(NSURLRequest *)) {
  787. [self sniffWithoutDuplicationForObject:session selector:selector sniffingBlock:^{
  788. [FLEXNetworkObserver.sharedObserver
  789. URLSession:session task:task
  790. willPerformHTTPRedirection:response
  791. newRequest:newRequest
  792. completionHandler:completionHandler
  793. delegate:slf
  794. ];
  795. } originalImplementationBlock:^{
  796. ((id(*)(id, SEL, id, id, id, id, void(^)(NSURLRequest *)))objc_msgSend)(
  797. slf, swizzledSelector, session, task, response, newRequest, completionHandler
  798. );
  799. }];
  800. };
  801. [FLEXUtility replaceImplementationOfSelector:selector
  802. withSelector:swizzledSelector
  803. forClass:cls
  804. withMethodDescription:description
  805. implementationBlock:implementationBlock
  806. undefinedBlock:undefinedBlock
  807. ];
  808. }
  809. + (void)injectTaskDidReceiveDataIntoDelegateClass:(Class)cls {
  810. SEL selector = @selector(URLSession:dataTask:didReceiveData:);
  811. SEL swizzledSelector = [FLEXUtility swizzledSelectorForSelector:selector];
  812. struct objc_method_description description = protocol_getMethodDescription(
  813. @protocol(NSURLSessionDataDelegate), selector, NO, YES
  814. );
  815. typedef void (^DidReceiveDataBlock)(id<NSURLSessionDataDelegate> slf,
  816. NSURLSession *session,
  817. NSURLSessionDataTask *dataTask,
  818. NSData *data);
  819. DidReceiveDataBlock undefinedBlock = ^(id<NSURLSessionDataDelegate> slf,
  820. NSURLSession *session,
  821. NSURLSessionDataTask *dataTask,
  822. NSData *data) {
  823. [FLEXNetworkObserver.sharedObserver URLSession:session
  824. dataTask:dataTask didReceiveData:data delegate:slf
  825. ];
  826. };
  827. DidReceiveDataBlock implementationBlock = ^(id<NSURLSessionDataDelegate> slf,
  828. NSURLSession *session,
  829. NSURLSessionDataTask *dataTask,
  830. NSData *data) {
  831. [self sniffWithoutDuplicationForObject:session selector:selector sniffingBlock:^{
  832. undefinedBlock(slf, session, dataTask, data);
  833. } originalImplementationBlock:^{
  834. ((void(*)(id, SEL, id, id, id))objc_msgSend)(
  835. slf, swizzledSelector, session, dataTask, data
  836. );
  837. }];
  838. };
  839. [FLEXUtility replaceImplementationOfSelector:selector
  840. withSelector:swizzledSelector
  841. forClass:cls
  842. withMethodDescription:description
  843. implementationBlock:implementationBlock
  844. undefinedBlock:undefinedBlock
  845. ];
  846. }
  847. + (void)injectDataTaskDidBecomeDownloadTaskIntoDelegateClass:(Class)cls {
  848. SEL selector = @selector(URLSession:dataTask:didBecomeDownloadTask:);
  849. SEL swizzledSelector = [FLEXUtility swizzledSelectorForSelector:selector];
  850. struct objc_method_description description = protocol_getMethodDescription(
  851. @protocol(NSURLSessionDataDelegate), selector, NO, YES
  852. );
  853. typedef void (^DidBecomeDownloadTaskBlock)(id<NSURLSessionDataDelegate> slf,
  854. NSURLSession *session,
  855. NSURLSessionDataTask *dataTask,
  856. NSURLSessionDownloadTask *downloadTask);
  857. DidBecomeDownloadTaskBlock undefinedBlock = ^(id<NSURLSessionDataDelegate> slf,
  858. NSURLSession *session,
  859. NSURLSessionDataTask *dataTask,
  860. NSURLSessionDownloadTask *downloadTask) {
  861. [FLEXNetworkObserver.sharedObserver URLSession:session
  862. dataTask:dataTask didBecomeDownloadTask:downloadTask delegate:slf
  863. ];
  864. };
  865. DidBecomeDownloadTaskBlock implementationBlock = ^(id<NSURLSessionDataDelegate> slf,
  866. NSURLSession *session,
  867. NSURLSessionDataTask *dataTask,
  868. NSURLSessionDownloadTask *downloadTask) {
  869. [self sniffWithoutDuplicationForObject:session selector:selector sniffingBlock:^{
  870. undefinedBlock(slf, session, dataTask, downloadTask);
  871. } originalImplementationBlock:^{
  872. ((void(*)(id, SEL, id, id, id))objc_msgSend)(
  873. slf, swizzledSelector, session, dataTask, downloadTask
  874. );
  875. }];
  876. };
  877. [FLEXUtility replaceImplementationOfSelector:selector
  878. withSelector:swizzledSelector
  879. forClass:cls
  880. withMethodDescription:description
  881. implementationBlock:implementationBlock
  882. undefinedBlock:undefinedBlock
  883. ];
  884. }
  885. + (void)injectTaskDidReceiveResponseIntoDelegateClass:(Class)cls {
  886. SEL selector = @selector(URLSession:dataTask:didReceiveResponse:completionHandler:);
  887. SEL swizzledSelector = [FLEXUtility swizzledSelectorForSelector:selector];
  888. struct objc_method_description description = protocol_getMethodDescription(
  889. @protocol(NSURLSessionDataDelegate), selector, NO, YES
  890. );
  891. typedef void (^DidReceiveResponseBlock)(id<NSURLSessionDelegate> slf,
  892. NSURLSession *session,
  893. NSURLSessionDataTask *dataTask,
  894. NSURLResponse *response,
  895. void(^completion)(NSURLSessionResponseDisposition));
  896. DidReceiveResponseBlock undefinedBlock = ^(id<NSURLSessionDelegate> slf,
  897. NSURLSession *session,
  898. NSURLSessionDataTask *dataTask,
  899. NSURLResponse *response,
  900. void(^completion)(NSURLSessionResponseDisposition)) {
  901. [FLEXNetworkObserver.sharedObserver
  902. URLSession:session
  903. dataTask:dataTask
  904. didReceiveResponse:response
  905. completionHandler:completion
  906. delegate:slf
  907. ];
  908. completion(NSURLSessionResponseAllow);
  909. };
  910. DidReceiveResponseBlock implementationBlock = ^(id<NSURLSessionDelegate> slf,
  911. NSURLSession *session,
  912. NSURLSessionDataTask *dataTask,
  913. NSURLResponse *response,
  914. void(^completion)(NSURLSessionResponseDisposition )) {
  915. [self sniffWithoutDuplicationForObject:session selector:selector sniffingBlock:^{
  916. [FLEXNetworkObserver.sharedObserver
  917. URLSession:session
  918. dataTask:dataTask
  919. didReceiveResponse:response
  920. completionHandler:completion
  921. delegate:slf
  922. ];
  923. } originalImplementationBlock:^{
  924. ((void(*)(id, SEL, id, id, id, void(^)(NSURLSessionResponseDisposition)))objc_msgSend)(
  925. slf, swizzledSelector, session, dataTask, response, completion
  926. );
  927. }];
  928. };
  929. [FLEXUtility replaceImplementationOfSelector:selector
  930. withSelector:swizzledSelector
  931. forClass:cls
  932. withMethodDescription:description
  933. implementationBlock:implementationBlock
  934. undefinedBlock:undefinedBlock
  935. ];
  936. }
  937. + (void)injectTaskDidCompleteWithErrorIntoDelegateClass:(Class)cls {
  938. SEL selector = @selector(URLSession:task:didCompleteWithError:);
  939. SEL swizzledSelector = [FLEXUtility swizzledSelectorForSelector:selector];
  940. struct objc_method_description description = protocol_getMethodDescription(
  941. @protocol(NSURLSessionDataDelegate), selector, NO, YES
  942. );
  943. typedef void (^DidCompleteWithErrorBlock)(id<NSURLSessionTaskDelegate> slf,
  944. NSURLSession *session,
  945. NSURLSessionTask *task,
  946. NSError *error);
  947. DidCompleteWithErrorBlock undefinedBlock = ^(id<NSURLSessionTaskDelegate> slf,
  948. NSURLSession *session,
  949. NSURLSessionTask *task,
  950. NSError *error) {
  951. [FLEXNetworkObserver.sharedObserver URLSession:session
  952. task:task didCompleteWithError:error delegate:slf
  953. ];
  954. };
  955. DidCompleteWithErrorBlock implementationBlock = ^(id<NSURLSessionTaskDelegate> slf,
  956. NSURLSession *session,
  957. NSURLSessionTask *task,
  958. NSError *error) {
  959. [self sniffWithoutDuplicationForObject:session selector:selector sniffingBlock:^{
  960. undefinedBlock(slf, session, task, error);
  961. } originalImplementationBlock:^{
  962. ((void(*)(id, SEL, id, id, id))objc_msgSend)(
  963. slf, swizzledSelector, session, task, error
  964. );
  965. }];
  966. };
  967. [FLEXUtility replaceImplementationOfSelector:selector
  968. withSelector:swizzledSelector
  969. forClass:cls
  970. withMethodDescription:description
  971. implementationBlock:implementationBlock
  972. undefinedBlock:undefinedBlock
  973. ];
  974. }
  975. // Used for overriding AFNetworking behavior
  976. + (void)injectRespondsToSelectorIntoDelegateClass:(Class)cls {
  977. SEL selector = @selector(respondsToSelector:);
  978. SEL swizzledSelector = [FLEXUtility swizzledSelectorForSelector:selector];
  979. //Protocol *protocol = @protocol(NSURLSessionTaskDelegate);
  980. Method method = class_getInstanceMethod(cls, selector);
  981. struct objc_method_description methodDescription = *method_getDescription(method);
  982. typedef BOOL (^RespondsToSelectorImpl)(id self, SEL sel);
  983. RespondsToSelectorImpl undefinedBlock = ^(id slf, SEL sel) {
  984. return YES;
  985. };
  986. RespondsToSelectorImpl implementationBlock = ^(id<NSURLSessionTaskDelegate> slf, SEL sel) {
  987. if (sel == @selector(URLSession:dataTask:didReceiveResponse:completionHandler:)) {
  988. return undefinedBlock(slf, sel);
  989. }
  990. return ((BOOL(*)(id, SEL, SEL))objc_msgSend)(slf, swizzledSelector, sel);
  991. };
  992. [FLEXUtility replaceImplementationOfSelector:selector
  993. withSelector:swizzledSelector
  994. forClass:cls
  995. withMethodDescription:methodDescription
  996. implementationBlock:implementationBlock
  997. undefinedBlock:undefinedBlock
  998. ];
  999. }
  1000. + (void)injectDownloadTaskDidFinishDownloadingIntoDelegateClass:(Class)cls {
  1001. SEL selector = @selector(URLSession:downloadTask:didFinishDownloadingToURL:);
  1002. SEL swizzledSelector = [FLEXUtility swizzledSelectorForSelector:selector];
  1003. struct objc_method_description description = protocol_getMethodDescription(
  1004. @protocol(NSURLSessionDownloadDelegate), selector, NO, YES
  1005. );
  1006. typedef void (^DidFinishDownloadingBlock)(id<NSURLSessionTaskDelegate> slf,
  1007. NSURLSession *session,
  1008. NSURLSessionDownloadTask *task,
  1009. NSURL *location);
  1010. DidFinishDownloadingBlock undefinedBlock = ^(id<NSURLSessionTaskDelegate> slf,
  1011. NSURLSession *session,
  1012. NSURLSessionDownloadTask *task,
  1013. NSURL *location) {
  1014. NSData *data = [NSData dataWithContentsOfFile:location.relativePath];
  1015. [FLEXNetworkObserver.sharedObserver URLSession:session
  1016. task:task didFinishDownloadingToURL:location data:data delegate:slf
  1017. ];
  1018. };
  1019. DidFinishDownloadingBlock implementationBlock = ^(id<NSURLSessionTaskDelegate> slf,
  1020. NSURLSession *session,
  1021. NSURLSessionDownloadTask *task,
  1022. NSURL *location) {
  1023. [self sniffWithoutDuplicationForObject:session selector:selector sniffingBlock:^{
  1024. undefinedBlock(slf, session, task, location);
  1025. } originalImplementationBlock:^{
  1026. ((void(*)(id, SEL, id, id, id))objc_msgSend)(
  1027. slf, swizzledSelector, session, task, location
  1028. );
  1029. }];
  1030. };
  1031. [FLEXUtility replaceImplementationOfSelector:selector
  1032. withSelector:swizzledSelector
  1033. forClass:cls
  1034. withMethodDescription:description
  1035. implementationBlock:implementationBlock
  1036. undefinedBlock:undefinedBlock
  1037. ];
  1038. }
  1039. + (void)injectDownloadTaskDidWriteDataIntoDelegateClass:(Class)cls {
  1040. SEL selector = @selector(URLSession:downloadTask:didWriteData:totalBytesWritten:totalBytesExpectedToWrite:);
  1041. SEL swizzledSelector = [FLEXUtility swizzledSelectorForSelector:selector];
  1042. struct objc_method_description description = protocol_getMethodDescription(
  1043. @protocol(NSURLSessionDownloadDelegate), selector, NO, YES
  1044. );
  1045. typedef void (^DidWriteDataBlock)(id<NSURLSessionTaskDelegate> slf,
  1046. NSURLSession *session,
  1047. NSURLSessionDownloadTask *task,
  1048. int64_t bytesWritten,
  1049. int64_t totalBytesWritten,
  1050. int64_t totalBytesExpectedToWrite);
  1051. DidWriteDataBlock undefinedBlock = ^(id<NSURLSessionTaskDelegate> slf,
  1052. NSURLSession *session,
  1053. NSURLSessionDownloadTask *task,
  1054. int64_t bytesWritten,
  1055. int64_t totalBytesWritten,
  1056. int64_t totalBytesExpectedToWrite) {
  1057. [FLEXNetworkObserver.sharedObserver URLSession:session
  1058. downloadTask:task didWriteData:bytesWritten
  1059. totalBytesWritten:totalBytesWritten
  1060. totalBytesExpectedToWrite:totalBytesExpectedToWrite
  1061. delegate:slf
  1062. ];
  1063. };
  1064. DidWriteDataBlock implementationBlock = ^(id<NSURLSessionTaskDelegate> slf,
  1065. NSURLSession *session,
  1066. NSURLSessionDownloadTask *task,
  1067. int64_t bytesWritten,
  1068. int64_t totalBytesWritten,
  1069. int64_t totalBytesExpectedToWrite) {
  1070. [self sniffWithoutDuplicationForObject:session selector:selector sniffingBlock:^{
  1071. undefinedBlock(
  1072. slf, session, task, bytesWritten,
  1073. totalBytesWritten, totalBytesExpectedToWrite
  1074. );
  1075. } originalImplementationBlock:^{
  1076. ((void(*)(id, SEL, id, id, int64_t, int64_t, int64_t))objc_msgSend)(
  1077. slf, swizzledSelector, session, task, bytesWritten,
  1078. totalBytesWritten, totalBytesExpectedToWrite
  1079. );
  1080. }];
  1081. };
  1082. [FLEXUtility replaceImplementationOfSelector:selector
  1083. withSelector:swizzledSelector
  1084. forClass:cls
  1085. withMethodDescription:description
  1086. implementationBlock:implementationBlock
  1087. undefinedBlock:undefinedBlock
  1088. ];
  1089. }
  1090. static char const * const kFLEXRequestIDKey = "kFLEXRequestIDKey";
  1091. + (NSString *)requestIDForConnectionOrTask:(id)connectionOrTask {
  1092. NSString *requestID = objc_getAssociatedObject(connectionOrTask, kFLEXRequestIDKey);
  1093. if (!requestID) {
  1094. requestID = [self nextRequestID];
  1095. [self setRequestID:requestID forConnectionOrTask:connectionOrTask];
  1096. }
  1097. return requestID;
  1098. }
  1099. + (void)setRequestID:(NSString *)requestID forConnectionOrTask:(id)connectionOrTask {
  1100. objc_setAssociatedObject(
  1101. connectionOrTask, kFLEXRequestIDKey, requestID, OBJC_ASSOCIATION_RETAIN_NONATOMIC
  1102. );
  1103. }
  1104. #pragma mark - Initialization
  1105. - (id)init {
  1106. self = [super init];
  1107. if (self) {
  1108. self.requestStatesForRequestIDs = [NSMutableDictionary new];
  1109. self.queue = dispatch_queue_create(
  1110. "com.flex.FLEXNetworkObserver", DISPATCH_QUEUE_SERIAL
  1111. );
  1112. }
  1113. return self;
  1114. }
  1115. #pragma mark - Private Methods
  1116. - (void)performBlock:(dispatch_block_t)block {
  1117. if ([[self class] isEnabled]) {
  1118. dispatch_async(_queue, block);
  1119. }
  1120. }
  1121. - (FLEXInternalRequestState *)requestStateForRequestID:(NSString *)requestID {
  1122. FLEXInternalRequestState *requestState = self.requestStatesForRequestIDs[requestID];
  1123. if (!requestState) {
  1124. requestState = [FLEXInternalRequestState new];
  1125. [self.requestStatesForRequestIDs setObject:requestState forKey:requestID];
  1126. }
  1127. return requestState;
  1128. }
  1129. - (void)removeRequestStateForRequestID:(NSString *)requestID {
  1130. [self.requestStatesForRequestIDs removeObjectForKey:requestID];
  1131. }
  1132. @end
  1133. @implementation FLEXNetworkObserver (NSURLConnectionHelpers)
  1134. - (void)connection:(NSURLConnection *)connection
  1135. willSendRequest:(NSURLRequest *)request
  1136. redirectResponse:(NSURLResponse *)response
  1137. delegate:(id<NSURLConnectionDelegate>)delegate {
  1138. [self performBlock:^{
  1139. NSString *requestID = [[self class] requestIDForConnectionOrTask:connection];
  1140. FLEXInternalRequestState *requestState = [self requestStateForRequestID:requestID];
  1141. requestState.request = request;
  1142. [FLEXNetworkRecorder.defaultRecorder
  1143. recordRequestWillBeSentWithRequestID:requestID
  1144. request:request
  1145. redirectResponse:response
  1146. ];
  1147. NSString *mechanism = [NSString stringWithFormat:
  1148. @"NSURLConnection (delegate: %@)", [delegate class]
  1149. ];
  1150. [FLEXNetworkRecorder.defaultRecorder recordMechanism:mechanism forRequestID:requestID];
  1151. }];
  1152. }
  1153. - (void)connection:(NSURLConnection *)connection
  1154. didReceiveResponse:(NSURLResponse *)response
  1155. delegate:(id<NSURLConnectionDelegate>)delegate {
  1156. [self performBlock:^{
  1157. NSString *requestID = [[self class] requestIDForConnectionOrTask:connection];
  1158. FLEXInternalRequestState *requestState = [self requestStateForRequestID:requestID];
  1159. requestState.dataAccumulator = [NSMutableData new];
  1160. [FLEXNetworkRecorder.defaultRecorder
  1161. recordResponseReceivedWithRequestID:requestID
  1162. response:response
  1163. ];
  1164. }];
  1165. }
  1166. - (void)connection:(NSURLConnection *)connection
  1167. didReceiveData:(NSData *)data
  1168. delegate:(id<NSURLConnectionDelegate>)delegate {
  1169. // Just to be safe since we're doing this async
  1170. data = [data copy];
  1171. [self performBlock:^{
  1172. NSString *requestID = [[self class] requestIDForConnectionOrTask:connection];
  1173. FLEXInternalRequestState *requestState = [self requestStateForRequestID:requestID];
  1174. [requestState.dataAccumulator appendData:data];
  1175. [FLEXNetworkRecorder.defaultRecorder
  1176. recordDataReceivedWithRequestID:requestID
  1177. dataLength:data.length
  1178. ];
  1179. }];
  1180. }
  1181. - (void)connectionDidFinishLoading:(NSURLConnection *)connection
  1182. delegate:(id<NSURLConnectionDelegate>)delegate {
  1183. [self performBlock:^{
  1184. NSString *requestID = [[self class] requestIDForConnectionOrTask:connection];
  1185. FLEXInternalRequestState *requestState = [self requestStateForRequestID:requestID];
  1186. [FLEXNetworkRecorder.defaultRecorder
  1187. recordLoadingFinishedWithRequestID:requestID
  1188. responseBody:requestState.dataAccumulator
  1189. ];
  1190. [self removeRequestStateForRequestID:requestID];
  1191. }];
  1192. }
  1193. - (void)connection:(NSURLConnection *)connection
  1194. didFailWithError:(NSError *)error
  1195. delegate:(id<NSURLConnectionDelegate>)delegate {
  1196. [self performBlock:^{
  1197. NSString *requestID = [[self class] requestIDForConnectionOrTask:connection];
  1198. FLEXInternalRequestState *requestState = [self requestStateForRequestID:requestID];
  1199. // Cancellations can occur prior to the willSendRequest:...
  1200. // NSURLConnection delegate call. These are pretty common
  1201. // and clutter up the logs. Only record the failure if the
  1202. // recorder already knows about the request through willSendRequest:...
  1203. if (requestState.request) {
  1204. [FLEXNetworkRecorder.defaultRecorder
  1205. recordLoadingFailedWithRequestID:requestID error:error
  1206. ];
  1207. }
  1208. [self removeRequestStateForRequestID:requestID];
  1209. }];
  1210. }
  1211. - (void)connectionWillCancel:(NSURLConnection *)connection {
  1212. [self performBlock:^{
  1213. // Mimic the behavior of NSURLSession which is to create an error on cancellation.
  1214. NSDictionary<NSString *, id> *userInfo = @{ NSLocalizedDescriptionKey : @"cancelled" };
  1215. NSError *error = [NSError errorWithDomain:NSURLErrorDomain
  1216. code:NSURLErrorCancelled userInfo:userInfo
  1217. ];
  1218. [self connection:connection didFailWithError:error delegate:nil];
  1219. }];
  1220. }
  1221. @end
  1222. @implementation FLEXNetworkObserver (NSURLSessionTaskHelpers)
  1223. - (void)URLSession:(NSURLSession *)session
  1224. task:(NSURLSessionTask *)task
  1225. willPerformHTTPRedirection:(NSHTTPURLResponse *)response
  1226. newRequest:(NSURLRequest *)request
  1227. completionHandler:(void (^)(NSURLRequest *))completionHandler
  1228. delegate:(id<NSURLSessionDelegate>)delegate {
  1229. [self performBlock:^{
  1230. NSString *requestID = [[self class] requestIDForConnectionOrTask:task];
  1231. [FLEXNetworkRecorder.defaultRecorder
  1232. recordRequestWillBeSentWithRequestID:requestID
  1233. request:request
  1234. redirectResponse:response
  1235. ];
  1236. }];
  1237. }
  1238. - (void)URLSession:(NSURLSession *)session
  1239. dataTask:(NSURLSessionDataTask *)dataTask
  1240. didReceiveResponse:(NSURLResponse *)response
  1241. completionHandler:(void (^)(NSURLSessionResponseDisposition))completionHandler
  1242. delegate:(id<NSURLSessionDelegate>)delegate {
  1243. [self performBlock:^{
  1244. NSString *requestID = [[self class] requestIDForConnectionOrTask:dataTask];
  1245. FLEXInternalRequestState *requestState = [self requestStateForRequestID:requestID];
  1246. requestState.dataAccumulator = [NSMutableData new];
  1247. NSString *requestMechanism = [NSString stringWithFormat:
  1248. @"NSURLSessionDataTask (delegate: %@)", [delegate class]
  1249. ];
  1250. [FLEXNetworkRecorder.defaultRecorder
  1251. recordMechanism:requestMechanism
  1252. forRequestID:requestID
  1253. ];
  1254. [FLEXNetworkRecorder.defaultRecorder
  1255. recordResponseReceivedWithRequestID:requestID
  1256. response:response
  1257. ];
  1258. }];
  1259. }
  1260. - (void)URLSession:(NSURLSession *)session
  1261. dataTask:(NSURLSessionDataTask *)dataTask
  1262. didBecomeDownloadTask:(NSURLSessionDownloadTask *)downloadTask
  1263. delegate:(id<NSURLSessionDelegate>)delegate {
  1264. [self performBlock:^{
  1265. // By setting the request ID of the download task to match the data task,
  1266. // it can pick up where the data task left off.
  1267. NSString *requestID = [[self class] requestIDForConnectionOrTask:dataTask];
  1268. [[self class] setRequestID:requestID forConnectionOrTask:downloadTask];
  1269. }];
  1270. }
  1271. - (void)URLSession:(NSURLSession *)session
  1272. dataTask:(NSURLSessionDataTask *)dataTask
  1273. didReceiveData:(NSData *)data
  1274. delegate:(id<NSURLSessionDelegate>)delegate {
  1275. // Just to be safe since we're doing this async
  1276. data = [data copy];
  1277. [self performBlock:^{
  1278. NSString *requestID = [[self class] requestIDForConnectionOrTask:dataTask];
  1279. FLEXInternalRequestState *requestState = [self requestStateForRequestID:requestID];
  1280. [requestState.dataAccumulator appendData:data];
  1281. [FLEXNetworkRecorder.defaultRecorder
  1282. recordDataReceivedWithRequestID:requestID
  1283. dataLength:data.length
  1284. ];
  1285. }];
  1286. }
  1287. - (void)URLSession:(NSURLSession *)session
  1288. task:(NSURLSessionTask *)task
  1289. didCompleteWithError:(NSError *)error
  1290. delegate:(id<NSURLSessionDelegate>)delegate {
  1291. [self performBlock:^{
  1292. NSString *requestID = [[self class] requestIDForConnectionOrTask:task];
  1293. FLEXInternalRequestState *requestState = [self requestStateForRequestID:requestID];
  1294. if (error) {
  1295. [FLEXNetworkRecorder.defaultRecorder
  1296. recordLoadingFailedWithRequestID:requestID error:error
  1297. ];
  1298. } else {
  1299. [FLEXNetworkRecorder.defaultRecorder
  1300. recordLoadingFinishedWithRequestID:requestID
  1301. responseBody:requestState.dataAccumulator
  1302. ];
  1303. }
  1304. [self removeRequestStateForRequestID:requestID];
  1305. }];
  1306. }
  1307. - (void)URLSession:(NSURLSession *)session
  1308. downloadTask:(NSURLSessionDownloadTask *)downloadTask
  1309. didWriteData:(int64_t)bytesWritten
  1310. totalBytesWritten:(int64_t)totalBytesWritten
  1311. totalBytesExpectedToWrite:(int64_t)totalBytesExpectedToWrite
  1312. delegate:(id<NSURLSessionDelegate>)delegate {
  1313. [self performBlock:^{
  1314. NSString *requestID = [[self class] requestIDForConnectionOrTask:downloadTask];
  1315. FLEXInternalRequestState *requestState = [self requestStateForRequestID:requestID];
  1316. if (!requestState.dataAccumulator) {
  1317. requestState.dataAccumulator = [NSMutableData new];
  1318. [FLEXNetworkRecorder.defaultRecorder
  1319. recordResponseReceivedWithRequestID:requestID
  1320. response:downloadTask.response
  1321. ];
  1322. NSString *requestMechanism = [NSString stringWithFormat:
  1323. @"NSURLSessionDownloadTask (delegate: %@)", [delegate class]
  1324. ];
  1325. [FLEXNetworkRecorder.defaultRecorder
  1326. recordMechanism:requestMechanism
  1327. forRequestID:requestID
  1328. ];
  1329. }
  1330. [FLEXNetworkRecorder.defaultRecorder
  1331. recordDataReceivedWithRequestID:requestID
  1332. dataLength:bytesWritten
  1333. ];
  1334. }];
  1335. }
  1336. - (void)URLSession:(NSURLSession *)session
  1337. task:(NSURLSessionDownloadTask *)downloadTask
  1338. didFinishDownloadingToURL:(NSURL *)location data:(NSData *)data
  1339. delegate:(id<NSURLSessionDelegate>)delegate {
  1340. data = [data copy];
  1341. [self performBlock:^{
  1342. NSString *requestID = [[self class] requestIDForConnectionOrTask:downloadTask];
  1343. FLEXInternalRequestState *requestState = [self requestStateForRequestID:requestID];
  1344. [requestState.dataAccumulator appendData:data];
  1345. }];
  1346. }
  1347. - (void)URLSessionTaskWillResume:(NSURLSessionTask *)task {
  1348. // Since resume can be called multiple times on the same task, only treat the first resume as
  1349. // the equivalent to connection:willSendRequest:...
  1350. [self performBlock:^{
  1351. NSString *requestID = [[self class] requestIDForConnectionOrTask:task];
  1352. FLEXInternalRequestState *requestState = [self requestStateForRequestID:requestID];
  1353. if (!requestState.request) {
  1354. requestState.request = task.currentRequest;
  1355. [FLEXNetworkRecorder.defaultRecorder
  1356. recordRequestWillBeSentWithRequestID:requestID
  1357. request:task.currentRequest
  1358. redirectResponse:nil
  1359. ];
  1360. }
  1361. }];
  1362. }
  1363. @end