FLEXNetworkTransactionDetailTableViewController.m 22 KB

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