FLEXNetworkTransactionDetailTableViewController.m 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545
  1. //
  2. // FLEXNetworkTransactionDetailTableViewController.m
  3. // Flipboard
  4. //
  5. // Created by Ryan Olson on 2/10/15.
  6. // Copyright (c) 2015 Flipboard. All rights reserved.
  7. //
  8. #import "FLEXColor.h"
  9. #import "FLEXNetworkTransactionDetailTableViewController.h"
  10. #import "FLEXNetworkCurlLogger.h"
  11. #import "FLEXNetworkRecorder.h"
  12. #import "FLEXNetworkTransaction.h"
  13. #import "FLEXWebViewController.h"
  14. #import "FLEXImagePreviewViewController.h"
  15. #import "FLEXMultilineTableViewCell.h"
  16. #import "FLEXUtility.h"
  17. #import "FLEXManager+Private.h"
  18. typedef UIViewController *(^FLEXNetworkDetailRowSelectionFuture)(void);
  19. @interface FLEXNetworkDetailRow : NSObject
  20. @property (nonatomic, copy) NSString *title;
  21. @property (nonatomic, copy) NSString *detailText;
  22. @property (nonatomic, copy) FLEXNetworkDetailRowSelectionFuture selectionFuture;
  23. @end
  24. @implementation FLEXNetworkDetailRow
  25. @end
  26. @interface FLEXNetworkDetailSection : NSObject
  27. @property (nonatomic, copy) NSString *title;
  28. @property (nonatomic, copy) NSArray<FLEXNetworkDetailRow *> *rows;
  29. @end
  30. @implementation FLEXNetworkDetailSection
  31. @end
  32. @interface FLEXNetworkTransactionDetailTableViewController ()
  33. @property (nonatomic, copy) NSArray<FLEXNetworkDetailSection *> *sections;
  34. @end
  35. @implementation FLEXNetworkTransactionDetailTableViewController
  36. - (instancetype)initWithStyle:(UITableViewStyle)style
  37. {
  38. // Force grouped style
  39. self = [super initWithStyle:UITableViewStyleGrouped];
  40. if (self) {
  41. [NSNotificationCenter.defaultCenter addObserver:self selector:@selector(handleTransactionUpdatedNotification:) name:kFLEXNetworkRecorderTransactionUpdatedNotification object:nil];
  42. self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Copy curl" style:UIBarButtonItemStylePlain target:self action:@selector(copyButtonPressed:)];
  43. }
  44. return self;
  45. }
  46. - (void)viewDidLoad
  47. {
  48. [super viewDidLoad];
  49. [self.tableView registerClass:[FLEXMultilineTableViewCell class] forCellReuseIdentifier:kFLEXMultilineTableViewCellIdentifier];
  50. }
  51. - (void)setTransaction:(FLEXNetworkTransaction *)transaction
  52. {
  53. if (![_transaction isEqual:transaction]) {
  54. _transaction = transaction;
  55. self.title = [transaction.request.URL lastPathComponent];
  56. [self rebuildTableSections];
  57. }
  58. }
  59. - (void)setSections:(NSArray<FLEXNetworkDetailSection *> *)sections
  60. {
  61. if (![_sections isEqual:sections]) {
  62. _sections = [sections copy];
  63. [self.tableView reloadData];
  64. }
  65. }
  66. - (void)rebuildTableSections
  67. {
  68. NSMutableArray<FLEXNetworkDetailSection *> *sections = [NSMutableArray array];
  69. FLEXNetworkDetailSection *generalSection = [[self class] generalSectionForTransaction:self.transaction];
  70. if (generalSection.rows.count > 0) {
  71. [sections addObject:generalSection];
  72. }
  73. FLEXNetworkDetailSection *requestHeadersSection = [[self class] requestHeadersSectionForTransaction:self.transaction];
  74. if (requestHeadersSection.rows.count > 0) {
  75. [sections addObject:requestHeadersSection];
  76. }
  77. FLEXNetworkDetailSection *queryParametersSection = [[self class] queryParametersSectionForTransaction:self.transaction];
  78. if (queryParametersSection.rows.count > 0) {
  79. [sections addObject:queryParametersSection];
  80. }
  81. FLEXNetworkDetailSection *postBodySection = [[self class] postBodySectionForTransaction:self.transaction];
  82. if (postBodySection.rows.count > 0) {
  83. [sections addObject:postBodySection];
  84. }
  85. FLEXNetworkDetailSection *responseHeadersSection = [[self class] responseHeadersSectionForTransaction:self.transaction];
  86. if (responseHeadersSection.rows.count > 0) {
  87. [sections addObject:responseHeadersSection];
  88. }
  89. self.sections = sections;
  90. }
  91. - (void)handleTransactionUpdatedNotification:(NSNotification *)notification
  92. {
  93. FLEXNetworkTransaction *transaction = [[notification userInfo] objectForKey:kFLEXNetworkRecorderUserInfoTransactionKey];
  94. if (transaction == self.transaction) {
  95. [self rebuildTableSections];
  96. }
  97. }
  98. - (void)copyButtonPressed:(id)sender
  99. {
  100. [UIPasteboard.generalPasteboard setString:[FLEXNetworkCurlLogger curlCommandString:_transaction.request]];
  101. }
  102. #pragma mark - Table view data source
  103. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  104. {
  105. return self.sections.count;
  106. }
  107. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  108. {
  109. FLEXNetworkDetailSection *sectionModel = self.sections[section];
  110. return sectionModel.rows.count;
  111. }
  112. - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
  113. {
  114. FLEXNetworkDetailSection *sectionModel = self.sections[section];
  115. return sectionModel.title;
  116. }
  117. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  118. {
  119. FLEXMultilineTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kFLEXMultilineTableViewCellIdentifier forIndexPath:indexPath];
  120. FLEXNetworkDetailRow *rowModel = [self rowModelAtIndexPath:indexPath];
  121. cell.textLabel.attributedText = [[self class] attributedTextForRow:rowModel];
  122. cell.accessoryType = rowModel.selectionFuture ? UITableViewCellAccessoryDisclosureIndicator : UITableViewCellAccessoryNone;
  123. cell.selectionStyle = rowModel.selectionFuture ? UITableViewCellSelectionStyleDefault : UITableViewCellSelectionStyleNone;
  124. return cell;
  125. }
  126. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  127. {
  128. FLEXNetworkDetailRow *rowModel = [self rowModelAtIndexPath:indexPath];
  129. UIViewController *viewController = nil;
  130. if (rowModel.selectionFuture) {
  131. viewController = rowModel.selectionFuture();
  132. }
  133. if ([viewController isKindOfClass:UIAlertController.class]) {
  134. [self presentViewController:viewController animated:YES completion:nil];
  135. } else if (viewController) {
  136. [self.navigationController pushViewController:viewController animated:YES];
  137. }
  138. [tableView deselectRowAtIndexPath:indexPath animated:YES];
  139. }
  140. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
  141. {
  142. FLEXNetworkDetailRow *row = [self rowModelAtIndexPath:indexPath];
  143. NSAttributedString *attributedText = [[self class] attributedTextForRow:row];
  144. BOOL showsAccessory = row.selectionFuture != nil;
  145. return [FLEXMultilineTableViewCell preferredHeightWithAttributedText:attributedText inTableViewWidth:self.tableView.bounds.size.width style:UITableViewStyleGrouped showsAccessory:showsAccessory];
  146. }
  147. - (FLEXNetworkDetailRow *)rowModelAtIndexPath:(NSIndexPath *)indexPath
  148. {
  149. FLEXNetworkDetailSection *sectionModel = self.sections[indexPath.section];
  150. return sectionModel.rows[indexPath.row];
  151. }
  152. #pragma mark - Cell Copying
  153. - (BOOL)tableView:(UITableView *)tableView shouldShowMenuForRowAtIndexPath:(NSIndexPath *)indexPath
  154. {
  155. return YES;
  156. }
  157. - (BOOL)tableView:(UITableView *)tableView canPerformAction:(SEL)action forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender
  158. {
  159. return action == @selector(copy:);
  160. }
  161. - (void)tableView:(UITableView *)tableView performAction:(SEL)action forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender
  162. {
  163. if (action == @selector(copy:)) {
  164. FLEXNetworkDetailRow *row = [self rowModelAtIndexPath:indexPath];
  165. UIPasteboard.generalPasteboard.string = row.detailText;
  166. }
  167. }
  168. #if FLEX_AT_LEAST_IOS13_SDK
  169. - (UIContextMenuConfiguration *)tableView:(UITableView *)tableView contextMenuConfigurationForRowAtIndexPath:(NSIndexPath *)indexPath point:(CGPoint)point __IOS_AVAILABLE(13.0)
  170. {
  171. return [UIContextMenuConfiguration
  172. configurationWithIdentifier:nil
  173. previewProvider:nil
  174. actionProvider:^UIMenu *(NSArray<UIMenuElement *> *suggestedActions) {
  175. UIAction *copy = [UIAction
  176. actionWithTitle:@"Copy"
  177. image:nil
  178. identifier:nil
  179. handler:^(__kindof UIAction *action) {
  180. FLEXNetworkDetailRow *row = [self rowModelAtIndexPath:indexPath];
  181. UIPasteboard.generalPasteboard.string = row.detailText;
  182. }
  183. ];
  184. return [UIMenu
  185. menuWithTitle:@"" image:nil identifier:nil
  186. options:UIMenuOptionsDisplayInline
  187. children:@[copy]
  188. ];
  189. }
  190. ];
  191. }
  192. #endif
  193. #pragma mark - View Configuration
  194. + (NSAttributedString *)attributedTextForRow:(FLEXNetworkDetailRow *)row
  195. {
  196. NSDictionary<NSString *, id> *titleAttributes = @{ NSFontAttributeName : [UIFont fontWithName:@"HelveticaNeue-Medium" size:12.0],
  197. NSForegroundColorAttributeName : [UIColor colorWithWhite:0.5 alpha:1.0] };
  198. NSDictionary<NSString *, id> *detailAttributes = @{ NSFontAttributeName : [FLEXUtility defaultTableViewCellLabelFont],
  199. NSForegroundColorAttributeName : [FLEXColor primaryTextColor] };
  200. NSString *title = [NSString stringWithFormat:@"%@: ", row.title];
  201. NSString *detailText = row.detailText ?: @"";
  202. NSMutableAttributedString *attributedText = [NSMutableAttributedString new];
  203. [attributedText appendAttributedString:[[NSAttributedString alloc] initWithString:title attributes:titleAttributes]];
  204. [attributedText appendAttributedString:[[NSAttributedString alloc] initWithString:detailText attributes:detailAttributes]];
  205. return attributedText;
  206. }
  207. #pragma mark - Table Data Generation
  208. + (FLEXNetworkDetailSection *)generalSectionForTransaction:(FLEXNetworkTransaction *)transaction
  209. {
  210. NSMutableArray<FLEXNetworkDetailRow *> *rows = [NSMutableArray array];
  211. FLEXNetworkDetailRow *requestURLRow = [FLEXNetworkDetailRow new];
  212. requestURLRow.title = @"Request URL";
  213. NSURL *url = transaction.request.URL;
  214. requestURLRow.detailText = url.absoluteString;
  215. requestURLRow.selectionFuture = ^{
  216. UIViewController *urlWebViewController = [[FLEXWebViewController alloc] initWithURL:url];
  217. urlWebViewController.title = url.absoluteString;
  218. return urlWebViewController;
  219. };
  220. [rows addObject:requestURLRow];
  221. FLEXNetworkDetailRow *requestMethodRow = [FLEXNetworkDetailRow new];
  222. requestMethodRow.title = @"Request Method";
  223. requestMethodRow.detailText = transaction.request.HTTPMethod;
  224. [rows addObject:requestMethodRow];
  225. if (transaction.cachedRequestBody.length > 0) {
  226. FLEXNetworkDetailRow *postBodySizeRow = [FLEXNetworkDetailRow new];
  227. postBodySizeRow.title = @"Request Body Size";
  228. postBodySizeRow.detailText = [NSByteCountFormatter stringFromByteCount:transaction.cachedRequestBody.length countStyle:NSByteCountFormatterCountStyleBinary];
  229. [rows addObject:postBodySizeRow];
  230. FLEXNetworkDetailRow *postBodyRow = [FLEXNetworkDetailRow new];
  231. postBodyRow.title = @"Request Body";
  232. postBodyRow.detailText = @"tap to view";
  233. postBodyRow.selectionFuture = ^UIViewController * () {
  234. // Show the body if we can
  235. NSString *contentType = [transaction.request valueForHTTPHeaderField:@"Content-Type"];
  236. UIViewController *detailViewController = [self detailViewControllerForMIMEType:contentType data:[self postBodyDataForTransaction:transaction]];
  237. if (detailViewController) {
  238. detailViewController.title = @"Request Body";
  239. return detailViewController;
  240. }
  241. // We can't show the body, alert user
  242. return [FLEXAlert makeAlert:^(FLEXAlert *make) {
  243. make.title(@"Can't View HTTP Body Data");
  244. make.message(@"FLEX does not have a viewer for request body data with MIME type: ");
  245. make.message(contentType);
  246. make.button(@"Dismiss").cancelStyle();
  247. }];
  248. };
  249. [rows addObject:postBodyRow];
  250. }
  251. NSString *statusCodeString = [FLEXUtility statusCodeStringFromURLResponse:transaction.response];
  252. if (statusCodeString.length > 0) {
  253. FLEXNetworkDetailRow *statusCodeRow = [FLEXNetworkDetailRow new];
  254. statusCodeRow.title = @"Status Code";
  255. statusCodeRow.detailText = statusCodeString;
  256. [rows addObject:statusCodeRow];
  257. }
  258. if (transaction.error) {
  259. FLEXNetworkDetailRow *errorRow = [FLEXNetworkDetailRow new];
  260. errorRow.title = @"Error";
  261. errorRow.detailText = transaction.error.localizedDescription;
  262. [rows addObject:errorRow];
  263. }
  264. FLEXNetworkDetailRow *responseBodyRow = [FLEXNetworkDetailRow new];
  265. responseBodyRow.title = @"Response Body";
  266. NSData *responseData = [[FLEXNetworkRecorder defaultRecorder] cachedResponseBodyForTransaction:transaction];
  267. if (responseData.length > 0) {
  268. responseBodyRow.detailText = @"tap to view";
  269. // Avoid a long lived strong reference to the response data in case we need to purge it from the cache.
  270. __weak NSData *weakResponseData = responseData;
  271. responseBodyRow.selectionFuture = ^UIViewController * () {
  272. // Show the response if we can
  273. NSString *contentType = transaction.response.MIMEType;
  274. NSData *strongResponseData = weakResponseData;
  275. if (strongResponseData) {
  276. UIViewController *bodyDetailController = [self detailViewControllerForMIMEType:contentType data:strongResponseData];
  277. if (bodyDetailController) {
  278. bodyDetailController.title = @"Response";
  279. return bodyDetailController;
  280. }
  281. }
  282. // We can't show the response, alert user
  283. return [FLEXAlert makeAlert:^(FLEXAlert *make) {
  284. make.title(@"Unable to View Response");
  285. if (strongResponseData) {
  286. make.message(@"No viewer content type: ").message(contentType);
  287. } else {
  288. make.message(@"The response has been purged from the cache");
  289. }
  290. make.button(@"OK").cancelStyle();
  291. }];
  292. };
  293. } else {
  294. BOOL emptyResponse = transaction.receivedDataLength == 0;
  295. responseBodyRow.detailText = emptyResponse ? @"empty" : @"not in cache";
  296. }
  297. [rows addObject:responseBodyRow];
  298. FLEXNetworkDetailRow *responseSizeRow = [FLEXNetworkDetailRow new];
  299. responseSizeRow.title = @"Response Size";
  300. responseSizeRow.detailText = [NSByteCountFormatter stringFromByteCount:transaction.receivedDataLength countStyle:NSByteCountFormatterCountStyleBinary];
  301. [rows addObject:responseSizeRow];
  302. FLEXNetworkDetailRow *mimeTypeRow = [FLEXNetworkDetailRow new];
  303. mimeTypeRow.title = @"MIME Type";
  304. mimeTypeRow.detailText = transaction.response.MIMEType;
  305. [rows addObject:mimeTypeRow];
  306. FLEXNetworkDetailRow *mechanismRow = [FLEXNetworkDetailRow new];
  307. mechanismRow.title = @"Mechanism";
  308. mechanismRow.detailText = transaction.requestMechanism;
  309. [rows addObject:mechanismRow];
  310. NSDateFormatter *startTimeFormatter = [NSDateFormatter new];
  311. startTimeFormatter.dateFormat = @"yyyy-MM-dd HH:mm:ss.SSS";
  312. FLEXNetworkDetailRow *localStartTimeRow = [FLEXNetworkDetailRow new];
  313. localStartTimeRow.title = [NSString stringWithFormat:@"Start Time (%@)", [[NSTimeZone localTimeZone] abbreviationForDate:transaction.startTime]];
  314. localStartTimeRow.detailText = [startTimeFormatter stringFromDate:transaction.startTime];
  315. [rows addObject:localStartTimeRow];
  316. startTimeFormatter.timeZone = [NSTimeZone timeZoneWithAbbreviation:@"UTC"];
  317. FLEXNetworkDetailRow *utcStartTimeRow = [FLEXNetworkDetailRow new];
  318. utcStartTimeRow.title = @"Start Time (UTC)";
  319. utcStartTimeRow.detailText = [startTimeFormatter stringFromDate:transaction.startTime];
  320. [rows addObject:utcStartTimeRow];
  321. FLEXNetworkDetailRow *unixStartTime = [FLEXNetworkDetailRow new];
  322. unixStartTime.title = @"Unix Start Time";
  323. unixStartTime.detailText = [NSString stringWithFormat:@"%f", [transaction.startTime timeIntervalSince1970]];
  324. [rows addObject:unixStartTime];
  325. FLEXNetworkDetailRow *durationRow = [FLEXNetworkDetailRow new];
  326. durationRow.title = @"Total Duration";
  327. durationRow.detailText = [FLEXUtility stringFromRequestDuration:transaction.duration];
  328. [rows addObject:durationRow];
  329. FLEXNetworkDetailRow *latencyRow = [FLEXNetworkDetailRow new];
  330. latencyRow.title = @"Latency";
  331. latencyRow.detailText = [FLEXUtility stringFromRequestDuration:transaction.latency];
  332. [rows addObject:latencyRow];
  333. FLEXNetworkDetailSection *generalSection = [FLEXNetworkDetailSection new];
  334. generalSection.title = @"General";
  335. generalSection.rows = rows;
  336. return generalSection;
  337. }
  338. + (FLEXNetworkDetailSection *)requestHeadersSectionForTransaction:(FLEXNetworkTransaction *)transaction
  339. {
  340. FLEXNetworkDetailSection *requestHeadersSection = [FLEXNetworkDetailSection new];
  341. requestHeadersSection.title = @"Request Headers";
  342. requestHeadersSection.rows = [self networkDetailRowsFromDictionary:transaction.request.allHTTPHeaderFields];
  343. return requestHeadersSection;
  344. }
  345. + (FLEXNetworkDetailSection *)postBodySectionForTransaction:(FLEXNetworkTransaction *)transaction
  346. {
  347. FLEXNetworkDetailSection *postBodySection = [FLEXNetworkDetailSection new];
  348. postBodySection.title = @"Request Body Parameters";
  349. if (transaction.cachedRequestBody.length > 0) {
  350. NSString *contentType = [transaction.request valueForHTTPHeaderField:@"Content-Type"];
  351. if ([contentType hasPrefix:@"application/x-www-form-urlencoded"]) {
  352. NSString *bodyString = [NSString stringWithCString:[self postBodyDataForTransaction:transaction].bytes encoding:NSUTF8StringEncoding];
  353. postBodySection.rows = [self networkDetailRowsFromQueryItems:[FLEXUtility itemsFromQueryString:bodyString]];
  354. }
  355. }
  356. return postBodySection;
  357. }
  358. + (FLEXNetworkDetailSection *)queryParametersSectionForTransaction:(FLEXNetworkTransaction *)transaction
  359. {
  360. NSArray<NSURLQueryItem *> *queries = [FLEXUtility itemsFromQueryString:transaction.request.URL.query];
  361. FLEXNetworkDetailSection *querySection = [FLEXNetworkDetailSection new];
  362. querySection.title = @"Query Parameters";
  363. querySection.rows = [self networkDetailRowsFromQueryItems:queries];
  364. return querySection;
  365. }
  366. + (FLEXNetworkDetailSection *)responseHeadersSectionForTransaction:(FLEXNetworkTransaction *)transaction
  367. {
  368. FLEXNetworkDetailSection *responseHeadersSection = [FLEXNetworkDetailSection new];
  369. responseHeadersSection.title = @"Response Headers";
  370. if ([transaction.response isKindOfClass:[NSHTTPURLResponse class]]) {
  371. NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)transaction.response;
  372. responseHeadersSection.rows = [self networkDetailRowsFromDictionary:httpResponse.allHeaderFields];
  373. }
  374. return responseHeadersSection;
  375. }
  376. + (NSArray<FLEXNetworkDetailRow *> *)networkDetailRowsFromDictionary:(NSDictionary<NSString *, id> *)dictionary
  377. {
  378. NSMutableArray<FLEXNetworkDetailRow *> *rows = [NSMutableArray new];
  379. NSArray<NSString *> *sortedKeys = [dictionary.allKeys sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)];
  380. for (NSString *key in sortedKeys) {
  381. id value = dictionary[key];
  382. FLEXNetworkDetailRow *row = [FLEXNetworkDetailRow new];
  383. row.title = key;
  384. row.detailText = [value description];
  385. [rows addObject:row];
  386. }
  387. return rows.copy;
  388. }
  389. + (NSArray<FLEXNetworkDetailRow *> *)networkDetailRowsFromQueryItems:(NSArray<NSURLQueryItem *> *)items
  390. {
  391. // Sort the items by name
  392. items = [items sortedArrayUsingComparator:^NSComparisonResult(NSURLQueryItem *item1, NSURLQueryItem *item2) {
  393. return [item1.name caseInsensitiveCompare:item2.name];
  394. }];
  395. NSMutableArray<FLEXNetworkDetailRow *> *rows = [NSMutableArray new];
  396. for (NSURLQueryItem *item in items) {
  397. FLEXNetworkDetailRow *row = [FLEXNetworkDetailRow new];
  398. row.title = item.name;
  399. row.detailText = item.value;
  400. [rows addObject:row];
  401. }
  402. return [rows copy];
  403. }
  404. + (UIViewController *)detailViewControllerForMIMEType:(NSString *)mimeType data:(NSData *)data
  405. {
  406. FLEXCustomContentViewerFuture makeCustomViewer = [FLEXManager sharedManager].customContentTypeViewers[mimeType.lowercaseString];
  407. if (makeCustomViewer) {
  408. UIViewController *viewer = makeCustomViewer(data);
  409. if (viewer) {
  410. return viewer;
  411. }
  412. }
  413. // FIXME (RKO): Don't rely on UTF8 string encoding
  414. UIViewController *detailViewController = nil;
  415. if ([FLEXUtility isValidJSONData:data]) {
  416. NSString *prettyJSON = [FLEXUtility prettyJSONStringFromData:data];
  417. if (prettyJSON.length > 0) {
  418. detailViewController = [[FLEXWebViewController alloc] initWithText:prettyJSON];
  419. }
  420. } else if ([mimeType hasPrefix:@"image/"]) {
  421. UIImage *image = [UIImage imageWithData:data];
  422. detailViewController = [[FLEXImagePreviewViewController alloc] initWithImage:image];
  423. } else if ([mimeType isEqual:@"application/x-plist"]) {
  424. id propertyList = [NSPropertyListSerialization propertyListWithData:data options:0 format:NULL error:NULL];
  425. detailViewController = [[FLEXWebViewController alloc] initWithText:[propertyList description]];
  426. }
  427. // Fall back to trying to show the response as text
  428. if (!detailViewController) {
  429. NSString *text = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
  430. if (text.length > 0) {
  431. detailViewController = [[FLEXWebViewController alloc] initWithText:text];
  432. }
  433. }
  434. return detailViewController;
  435. }
  436. + (NSData *)postBodyDataForTransaction:(FLEXNetworkTransaction *)transaction
  437. {
  438. NSData *bodyData = transaction.cachedRequestBody;
  439. if (bodyData.length > 0) {
  440. NSString *contentEncoding = [transaction.request valueForHTTPHeaderField:@"Content-Encoding"];
  441. if ([contentEncoding rangeOfString:@"deflate" options:NSCaseInsensitiveSearch].length > 0 || [contentEncoding rangeOfString:@"gzip" options:NSCaseInsensitiveSearch].length > 0) {
  442. bodyData = [FLEXUtility inflatedDataFromCompressedData:bodyData];
  443. }
  444. }
  445. return bodyData;
  446. }
  447. @end