FLEXNetworkTransactionDetailController.m 22 KB

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