DBRequestDetailsViewController.m 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  1. // The MIT License
  2. //
  3. // Copyright (c) 2016 Dariusz Bukowski
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. #import "DBRequestDetailsViewController.h"
  23. #import "NSBundle+DBDebugToolkit.h"
  24. #import "DBMenuSegmentedControlTableViewCell.h"
  25. #import "DBTitleValueTableViewCell.h"
  26. #import "DBBodyPreviewViewController.h"
  27. typedef NS_ENUM(NSInteger, DBRequestDetailsViewControllerTab) {
  28. DBRequestDetailsViewControllerTabRequest,
  29. DBRequestDetailsViewControllerTabResponse,
  30. DBRequestDetailsViewControllerTabError
  31. };
  32. static NSString *const DBRequestDetailsViewControllerSegmentedControlCellIdentifier = @"DBMenuSegmentedControlTableViewCell";
  33. static NSString *const DBRequestDetailsViewControllerTitleValueCellIdentifier = @"DBTitleValueTableViewCell";
  34. static NSString *const DBRequestDetailsViewControllerPrototypeSimpleCellIdentifier = @"OpenBodyCell";
  35. @interface DBRequestDetailsViewController () <UITableViewDelegate, UITableViewDataSource, DBMenuSegmentedControlTableViewCellDelegate>
  36. @property (nonatomic, weak) IBOutlet UITableView *tableView;
  37. @property (nonatomic, strong) DBRequestModel *requestModel;
  38. @property (nonatomic, assign) DBRequestDetailsViewControllerTab selectedTab;
  39. @property (nonatomic, strong) NSArray *requestDetailsDataSources;
  40. @property (nonatomic, strong) NSArray *requestHeaderFieldsDataSources;
  41. @property (nonatomic, strong) NSArray *responseDetailsDataSources;
  42. @property (nonatomic, strong) NSArray *responseHeaderFieldsDataSources;
  43. @property (nonatomic, strong) NSArray *errorDetailsDataSources;
  44. @end
  45. @implementation DBRequestDetailsViewController
  46. - (void)viewDidLoad {
  47. [super viewDidLoad];
  48. NSBundle *bundle = [NSBundle debugToolkitBundle];
  49. [self.tableView registerNib:[UINib nibWithNibName:@"DBMenuSegmentedControlTableViewCell" bundle:bundle]
  50. forCellReuseIdentifier:DBRequestDetailsViewControllerSegmentedControlCellIdentifier];
  51. [self.tableView registerNib:[UINib nibWithNibName:@"DBTitleValueTableViewCell" bundle:bundle]
  52. forCellReuseIdentifier:DBRequestDetailsViewControllerTitleValueCellIdentifier];
  53. self.tableView.delegate = self;
  54. self.tableView.dataSource = self;
  55. self.tableView.rowHeight = UITableViewAutomaticDimension;
  56. self.tableView.estimatedRowHeight = 44.0;
  57. }
  58. - (void)viewDidDisappear:(BOOL)animated {
  59. [super viewDidDisappear:animated];
  60. if (self.isMovingFromParentViewController) {
  61. [self.delegate requestDetailsViewControllerDidDismiss:self];
  62. }
  63. }
  64. - (void)configureWithRequestModel:(DBRequestModel *)requestModel {
  65. self.requestModel = requestModel;
  66. [self createDataSources];
  67. [self.tableView reloadData];
  68. }
  69. #pragma mark - Opening body preview
  70. - (void)openBodyPreview {
  71. NSBundle *bundle = [NSBundle debugToolkitBundle];
  72. UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"DBBodyPreviewViewController" bundle:bundle];
  73. DBBodyPreviewViewController *bodyPreviewViewController = [storyboard instantiateInitialViewController];
  74. DBBodyPreviewViewControllerMode mode = self.selectedTab == DBRequestDetailsViewControllerTabRequest ? DBBodyPreviewViewControllerModeRequest : DBBodyPreviewViewControllerModeResponse;
  75. [bodyPreviewViewController configureWithRequestModel:self.requestModel mode:mode];
  76. [self.navigationController pushViewController:bodyPreviewViewController animated:true];
  77. }
  78. #pragma mark - UITableViewDelegate
  79. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  80. if (indexPath.section == 3 && indexPath.row == 1) {
  81. [self openBodyPreview];
  82. [tableView deselectRowAtIndexPath:indexPath animated:YES];
  83. }
  84. }
  85. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
  86. switch (indexPath.section) {
  87. case 0:
  88. return 44.0;
  89. case 3:
  90. return indexPath.row == 0 ? UITableViewAutomaticDimension : 44.0;
  91. default:
  92. return UITableViewAutomaticDimension;
  93. }
  94. }
  95. - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
  96. return [self heightForFooterAndHeaderInSection:section];
  97. }
  98. - (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
  99. return [self heightForFooterAndHeaderInSection:section];
  100. }
  101. #pragma mark - UITableViewDataSource
  102. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
  103. switch (self.selectedTab) {
  104. case DBRequestDetailsViewControllerTabRequest:
  105. return self.requestModel.requestBodySynchronizationStatus == DBRequestModelBodySynchronizationStatusFinished ? 4 : 3;
  106. case DBRequestDetailsViewControllerTabResponse:
  107. return self.requestModel.responseBodySynchronizationStatus == DBRequestModelBodySynchronizationStatusFinished ? 4 : 3;
  108. case DBRequestDetailsViewControllerTabError:
  109. return 2;
  110. }
  111. }
  112. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  113. switch (section) {
  114. case 0:
  115. return 1;
  116. case 1:
  117. return [self numberOfRowsInDetailsSection];
  118. case 2:
  119. return [self numberOfRowsInHeaderFieldsSection];
  120. case 3:
  121. return [self numberOfRowsInBodySection];
  122. }
  123. return 0;
  124. }
  125. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  126. switch (indexPath.section) {
  127. case 0: {
  128. DBMenuSegmentedControlTableViewCell *segmentedControlCell = [tableView dequeueReusableCellWithIdentifier:DBRequestDetailsViewControllerSegmentedControlCellIdentifier];
  129. [segmentedControlCell configureWithTitles:[self segmentedControlTitles] selectedIndex:[self segmentedControlSelectedIndex]];
  130. segmentedControlCell.delegate = self;
  131. return segmentedControlCell;
  132. }
  133. case 3: {
  134. if (indexPath.row == 1) {
  135. UITableViewCell *openBodyCell = [tableView dequeueReusableCellWithIdentifier:DBRequestDetailsViewControllerPrototypeSimpleCellIdentifier];
  136. openBodyCell.textLabel.text = @"Body preview";
  137. return openBodyCell;
  138. }
  139. }
  140. default: {
  141. return [self titleValueCellWithIndexPath:indexPath];
  142. }
  143. }
  144. }
  145. - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
  146. switch (section) {
  147. case 0:
  148. return nil;
  149. case 1:
  150. return [self firstSectionTitle];
  151. case 2:
  152. return [self numberOfRowsInHeaderFieldsSection] > 0 ? @"HTTP header fields" : @"";
  153. case 3:
  154. return @"Body";
  155. }
  156. return nil;
  157. }
  158. #pragma mark - DBMenuSegmentedControlTableViewCellDelegate
  159. - (void)menuSegmentedControlTableViewCell:(DBMenuSegmentedControlTableViewCell *)menuSegmentedControlTableViewCell didSelectSegmentAtIndex:(NSUInteger)index {
  160. if (index == 0) {
  161. self.selectedTab = DBRequestDetailsViewControllerTabRequest;
  162. } else {
  163. self.selectedTab = self.requestModel.didFinishWithError ? DBRequestDetailsViewControllerTabError : DBRequestDetailsViewControllerTabResponse;
  164. }
  165. [self.tableView reloadData];
  166. }
  167. #pragma mark - Private methods
  168. #pragma mark - - Section heights
  169. - (CGFloat)heightForFooterAndHeaderInSection:(NSInteger)section {
  170. return [self tableView:self.tableView numberOfRowsInSection:section] > 0 ? UITableViewAutomaticDimension : CGFLOAT_MIN;
  171. }
  172. #pragma mark - - Title value cells
  173. - (DBTitleValueTableViewCell *)titleValueCellWithIndexPath:(NSIndexPath *)indexPath {
  174. DBTitleValueTableViewCell *titleValueCell = [self.tableView dequeueReusableCellWithIdentifier:DBRequestDetailsViewControllerTitleValueCellIdentifier];
  175. DBTitleValueTableViewCellDataSource *dataSource = [self titleValueCellDataSourceWithIndexPath:indexPath];
  176. [titleValueCell configureWithDataSource:dataSource];
  177. return titleValueCell;
  178. }
  179. - (DBTitleValueTableViewCellDataSource *)titleValueCellDataSourceWithIndexPath:(NSIndexPath *)indexPath {
  180. switch (indexPath.section) {
  181. case 1:
  182. return [self titleValueDetailsCellDataSourceForRow:indexPath.row];
  183. case 2:
  184. return [self titleValueHeaderFieldsCellDataSourceForRow:indexPath.row];
  185. case 3:
  186. return [self titleValueBodyCellDataSourceForRow:indexPath.row];
  187. }
  188. return nil;
  189. }
  190. - (DBTitleValueTableViewCellDataSource *)titleValueDetailsCellDataSourceForRow:(NSInteger)row {
  191. switch (self.selectedTab) {
  192. case DBRequestDetailsViewControllerTabRequest:
  193. return self.requestDetailsDataSources[row];
  194. case DBRequestDetailsViewControllerTabResponse:
  195. return self.responseDetailsDataSources[row];
  196. case DBRequestDetailsViewControllerTabError:
  197. return self.errorDetailsDataSources[row];
  198. }
  199. }
  200. - (DBTitleValueTableViewCellDataSource *)titleValueHeaderFieldsCellDataSourceForRow:(NSInteger)row {
  201. switch (self.selectedTab) {
  202. case DBRequestDetailsViewControllerTabRequest:
  203. return self.requestHeaderFieldsDataSources[row];
  204. case DBRequestDetailsViewControllerTabResponse:
  205. return self.responseHeaderFieldsDataSources[row];
  206. case DBRequestDetailsViewControllerTabError:
  207. return nil;
  208. }
  209. }
  210. - (DBTitleValueTableViewCellDataSource *)titleValueBodyCellDataSourceForRow:(NSInteger)row {
  211. if (row == 0) {
  212. NSInteger dataLength = self.selectedTab == DBRequestDetailsViewControllerTabResponse ? self.requestModel.responseBodyLength : self.requestModel.requestBodyLength;
  213. return [DBTitleValueTableViewCellDataSource dataSourceWithTitle:@"Body length" value:[@(dataLength) stringValue]];
  214. }
  215. return nil;
  216. }
  217. #pragma mark - - Data sources
  218. - (void)createDataSources {
  219. if (self.requestModel.finished) {
  220. if (self.requestModel.didFinishWithError) {
  221. [self createErrorDetailsDataSources];
  222. } else {
  223. [self createResponseDetailsDataSources];
  224. self.responseHeaderFieldsDataSources = [self dataSourcesWithHTTPHeaderFields:self.requestModel.allResponseHTTPHeaderFields];
  225. }
  226. }
  227. [self createRequestDetailsDataSources];
  228. self.requestHeaderFieldsDataSources = [self dataSourcesWithHTTPHeaderFields:self.requestModel.allRequestHTTPHeaderFields];
  229. }
  230. - (void)createRequestDetailsDataSources {
  231. NSMutableArray *dataSources = [NSMutableArray array];
  232. [dataSources addObject:[DBTitleValueTableViewCellDataSource dataSourceWithTitle:@"URL" value:self.requestModel.url.absoluteString]];
  233. [dataSources addObject:[DBTitleValueTableViewCellDataSource dataSourceWithTitle:@"Cache policy" value:[self cachePolicyString]]];
  234. [dataSources addObject:[DBTitleValueTableViewCellDataSource dataSourceWithTitle:@"Timeout interval" value:[self stringWithTimeInterval:self.requestModel.timeoutInterval]]];
  235. [dataSources addObject:[DBTitleValueTableViewCellDataSource dataSourceWithTitle:@"Sending date" value:[self stringWithDate:self.requestModel.sendingDate]]];
  236. if (self.requestModel.httpMethod.length > 0) {
  237. [dataSources addObject:[DBTitleValueTableViewCellDataSource dataSourceWithTitle:@"HTTP method" value:self.requestModel.httpMethod]];
  238. }
  239. self.requestDetailsDataSources = [dataSources copy];
  240. }
  241. - (void)createResponseDetailsDataSources {
  242. NSMutableArray *dataSources = [NSMutableArray array];
  243. [dataSources addObject:[DBTitleValueTableViewCellDataSource dataSourceWithTitle:@"MIME type" value:self.requestModel.MIMEType]];
  244. if (self.requestModel.textEncodingName.length > 0) {
  245. [dataSources addObject:[DBTitleValueTableViewCellDataSource dataSourceWithTitle:@"Text encoding name" value:self.requestModel.textEncodingName]];
  246. }
  247. [dataSources addObject:[DBTitleValueTableViewCellDataSource dataSourceWithTitle:@"Receiving date" value:[self stringWithDate:self.requestModel.receivingDate]]];
  248. [dataSources addObject:[DBTitleValueTableViewCellDataSource dataSourceWithTitle:@"Duration" value:[self stringWithTimeInterval:self.requestModel.duration]]];
  249. if (self.requestModel.statusCode != nil) {
  250. NSString *statusCodeString = [NSString stringWithFormat:@"%@ - %@", self.requestModel.statusCode, self.requestModel.localizedStatusCodeString];
  251. [dataSources addObject:[DBTitleValueTableViewCellDataSource dataSourceWithTitle:@"HTTP status code" value:statusCodeString]];
  252. }
  253. self.responseDetailsDataSources = [dataSources copy];
  254. }
  255. - (void)createErrorDetailsDataSources {
  256. self.errorDetailsDataSources = @[
  257. [DBTitleValueTableViewCellDataSource dataSourceWithTitle:@"Error code" value:[@(self.requestModel.errorCode) stringValue]],
  258. [DBTitleValueTableViewCellDataSource dataSourceWithTitle:@"Error description" value:self.requestModel.localizedErrorDescription]
  259. ];
  260. }
  261. - (NSArray *)dataSourcesWithHTTPHeaderFields:(NSDictionary<NSString *, NSString *> *)headerFields {
  262. NSMutableArray *dataSources = [NSMutableArray array];
  263. for (NSString *key in headerFields.allKeys) {
  264. [dataSources addObject:[DBTitleValueTableViewCellDataSource dataSourceWithTitle:key value:headerFields[key]]];
  265. }
  266. return [dataSources copy];
  267. }
  268. #pragma mark - - Number of rows
  269. - (NSInteger)numberOfRowsInDetailsSection {
  270. switch (self.selectedTab) {
  271. case DBRequestDetailsViewControllerTabRequest:
  272. return self.requestDetailsDataSources.count;
  273. case DBRequestDetailsViewControllerTabResponse:
  274. return self.responseDetailsDataSources.count;
  275. case DBRequestDetailsViewControllerTabError:
  276. return self.errorDetailsDataSources.count;
  277. }
  278. }
  279. - (NSInteger)numberOfRowsInHeaderFieldsSection {
  280. switch (self.selectedTab) {
  281. case DBRequestDetailsViewControllerTabRequest:
  282. return self.requestModel.allRequestHTTPHeaderFields.count;
  283. case DBRequestDetailsViewControllerTabResponse:
  284. return self.requestModel.allResponseHTTPHeaderFields.count;
  285. case DBRequestDetailsViewControllerTabError:
  286. return 0;
  287. }
  288. }
  289. - (NSInteger)numberOfRowsInBodySection {
  290. switch (self.selectedTab) {
  291. case DBRequestDetailsViewControllerTabRequest: {
  292. if (self.requestModel.requestBodySynchronizationStatus == DBRequestModelBodySynchronizationStatusFinished) {
  293. return self.requestModel.requestBodyLength > 0 ? 2 : 1;
  294. }
  295. return 0;
  296. }
  297. case DBRequestDetailsViewControllerTabResponse: {
  298. if (self.requestModel.responseBodySynchronizationStatus == DBRequestModelBodySynchronizationStatusFinished) {
  299. return self.requestModel.responseBodyLength > 0 ? 2 : 1;
  300. }
  301. return 0;
  302. }
  303. case DBRequestDetailsViewControllerTabError:
  304. return 0;
  305. }
  306. }
  307. #pragma mark - - Section title
  308. - (NSString *)firstSectionTitle {
  309. switch (self.selectedTab) {
  310. case DBRequestDetailsViewControllerTabRequest:
  311. return @"Request";
  312. case DBRequestDetailsViewControllerTabResponse:
  313. return @"Response";
  314. case DBRequestDetailsViewControllerTabError:
  315. return @"Error";
  316. }
  317. }
  318. #pragma mark - - Value strings
  319. - (NSString *)cachePolicyString {
  320. switch (self.requestModel.cachePolicy) {
  321. case NSURLRequestReloadIgnoringLocalAndRemoteCacheData:
  322. return @"Reload ignoring local and remote cache data";
  323. case NSURLRequestReloadIgnoringLocalCacheData:
  324. return @"Reload ignoring local cache data";
  325. case NSURLRequestReturnCacheDataElseLoad:
  326. return @"Return cache data else load";
  327. case NSURLRequestReloadRevalidatingCacheData:
  328. return @"Reload revalidating cache data";
  329. case NSURLRequestReturnCacheDataDontLoad:
  330. return @"Return cache data, don't load";
  331. case NSURLRequestUseProtocolCachePolicy:
  332. return @"Use protocol cache policy";
  333. }
  334. }
  335. - (NSString *)stringWithTimeInterval:(NSTimeInterval)timeInterval {
  336. return [NSString stringWithFormat:@"%.2lfs", timeInterval];
  337. }
  338. - (NSString *)stringWithDate:(NSDate *)date {
  339. return [NSDateFormatter localizedStringFromDate:date
  340. dateStyle:NSDateFormatterMediumStyle
  341. timeStyle:NSDateFormatterMediumStyle];
  342. }
  343. #pragma mark - - Segmented control
  344. - (NSArray *)segmentedControlTitles {
  345. NSMutableArray *titles = [NSMutableArray arrayWithObject:@"Request"];
  346. if (self.requestModel.finished) {
  347. [titles addObject:self.requestModel.didFinishWithError ? @"Error" : @"Response"];
  348. }
  349. return [titles copy];
  350. }
  351. - (NSInteger)segmentedControlSelectedIndex {
  352. return self.selectedTab == DBRequestDetailsViewControllerTabRequest ? 0 : 1;
  353. }
  354. @end