FLEXNetworkTransactionDetailController.m 22 KB

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