FLEXNetworkTransactionDetailTableViewController.m 20 KB

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