OCNetworking.m 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974
  1. //
  2. // OCnetworking.m
  3. // Crypto Cloud Technology Nextcloud
  4. //
  5. // Created by Marino Faggiana on 10/05/15.
  6. // Copyright (c) 2014 TWS. All rights reserved.
  7. //
  8. // Author Marino Faggiana <m.faggiana@twsweb.it>
  9. //
  10. // This program is free software: you can redistribute it and/or modify
  11. // it under the terms of the GNU General Public License as published by
  12. // the Free Software Foundation, either version 3 of the License, or
  13. // (at your option) any later version.
  14. //
  15. // This program is distributed in the hope that it will be useful,
  16. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. // GNU General Public License for more details.
  19. //
  20. // You should have received a copy of the GNU General Public License
  21. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. //
  23. #import "OCNetworking.h"
  24. #import "AppDelegate.h"
  25. #import "CCGraphics.h"
  26. #import "CCCertificate.h"
  27. #import "NSString+Encode.h"
  28. #ifdef CC
  29. #import "CCSharedDBSession.h"
  30. #endif
  31. #pragma clang diagnostic ignored "-Warc-performSelector-leaks"
  32. @interface OCnetworking ()
  33. {
  34. NSString *_activeUser;
  35. NSString *_activePassword;
  36. NSString *_activeUrl;
  37. NSString *_typeCloud;
  38. NSURLSessionDownloadTask *_downloadTask;
  39. NSURLSessionUploadTask *_uploadTask;
  40. BOOL _oneByOne;
  41. BOOL _activityIndicator;
  42. }
  43. @end
  44. @implementation OCnetworking
  45. - (id)initWithDelegate:(id <OCNetworkingDelegate>)delegate metadataNet:(CCMetadataNet *)metadataNet withUser:(NSString *)withUser withPassword:(NSString *)withPassword withUrl:(NSString *)withUrl withTypeCloud:(NSString *)withTypeCloud oneByOne:(BOOL)oneByOne activityIndicator:(BOOL)activityIndicator
  46. {
  47. self = [super init];
  48. if (self) {
  49. _delegate = delegate;
  50. _metadataNet = [[CCMetadataNet alloc] init];
  51. _metadataNet = [metadataNet copy];
  52. _activeUser = withUser;
  53. _activePassword = withPassword;
  54. _activeUrl = withUrl;
  55. _typeCloud = withTypeCloud;
  56. _oneByOne = oneByOne;
  57. _activityIndicator = activityIndicator;
  58. }
  59. return self;
  60. }
  61. - (void)start
  62. {
  63. if (![NSThread isMainThread]) {
  64. [self performSelectorOnMainThread:@selector(start) withObject:nil waitUntilDone:NO];
  65. return;
  66. }
  67. [self willChangeValueForKey:@"isExecuting"];
  68. _isExecuting = YES;
  69. [self didChangeValueForKey:@"isExecuting"];
  70. if (self.isCancelled) {
  71. [self finish];
  72. } else {
  73. [self poolNetworking];
  74. }
  75. }
  76. - (void)finish
  77. {
  78. [self willChangeValueForKey:@"isExecuting"];
  79. [self willChangeValueForKey:@"isFinished"];
  80. _isExecuting = NO;
  81. _isFinished = YES;
  82. [self didChangeValueForKey:@"isExecuting"];
  83. [self didChangeValueForKey:@"isFinished"];
  84. }
  85. - (void)cancel
  86. {
  87. if (_isExecuting) {
  88. if (_downloadTask)
  89. [_downloadTask cancel];
  90. if (_uploadTask)
  91. [_uploadTask cancel];
  92. [self complete];
  93. }
  94. [super cancel];
  95. }
  96. - (void)poolNetworking
  97. {
  98. #ifndef SHARE_IN
  99. // Animation network
  100. if (_activityIndicator) {
  101. dispatch_async(dispatch_get_main_queue(), ^{
  102. [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
  103. [[NSNotificationCenter defaultCenter] postNotificationName:@"setTitleCCMainYESAnimation" object:nil];
  104. });
  105. }
  106. #endif
  107. if([self respondsToSelector:NSSelectorFromString(_metadataNet.action)])
  108. [self performSelector:NSSelectorFromString(_metadataNet.action)];
  109. }
  110. #pragma --------------------------------------------------------------------------------------------
  111. #pragma mark ===== Delegate =====
  112. #pragma --------------------------------------------------------------------------------------------
  113. - (void)complete
  114. {
  115. #ifndef SHARE_IN
  116. // Animation network
  117. if (_activityIndicator) {
  118. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 0.5 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
  119. [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
  120. [[NSNotificationCenter defaultCenter] postNotificationName:@"setTitleCCMainNOAnimation" object:nil];
  121. });
  122. }
  123. #endif
  124. [self finish];
  125. }
  126. #pragma --------------------------------------------------------------------------------------------
  127. #pragma mark ===== downloadFile =====
  128. #pragma --------------------------------------------------------------------------------------------
  129. - (void)downloadFile
  130. {
  131. [[CCNetworking sharedNetworking] downloadFile:_metadataNet.metadata serverUrl:_metadataNet.serverUrl downloadData:_metadataNet.downloadData downloadPlist:_metadataNet.downloadPlist selector:_metadataNet.selector selectorPost:_metadataNet.selectorPost session:_metadataNet.session taskStatus:_metadataNet.taskStatus delegate:self];
  132. if (!_oneByOne)
  133. [self complete];
  134. }
  135. - (void)downloadTaskSave:(NSURLSessionDownloadTask *)downloadTask
  136. {
  137. _downloadTask= downloadTask;
  138. if ([self.delegate respondsToSelector:@selector(downloadTaskSave:)])
  139. [self.delegate downloadTaskSave:downloadTask];
  140. }
  141. - (void)downloadFileSuccess:(NSString *)fileID serverUrl:(NSString *)serverUrl selector:(NSString *)selector selectorPost:(NSString *)selectorPost
  142. {
  143. [self complete];
  144. if ([self.delegate respondsToSelector:@selector(downloadFileSuccess:serverUrl:selector:selectorPost:)])
  145. [self.delegate downloadFileSuccess:fileID serverUrl:serverUrl selector:selector selectorPost:selectorPost];
  146. }
  147. - (void)downloadFileFailure:(NSString *)fileID serverUrl:(NSString *)serverUrl selector:(NSString *)selector message:(NSString *)message errorCode:(NSInteger)errorCode
  148. {
  149. [self complete];
  150. if ([self.delegate respondsToSelector:@selector(downloadFileFailure:serverUrl:selector:message:errorCode:)])
  151. [self.delegate downloadFileFailure:fileID serverUrl:serverUrl selector:selector message:message errorCode:errorCode];
  152. }
  153. #pragma --------------------------------------------------------------------------------------------
  154. #pragma mark ===== uploadFile =====
  155. #pragma --------------------------------------------------------------------------------------------
  156. - (void)uploadFile
  157. {
  158. [[CCNetworking sharedNetworking] uploadFile:_metadataNet.fileName serverUrl:_metadataNet.serverUrl cryptated:_metadataNet.cryptated onlyPlist:NO session:_metadataNet.session taskStatus:_metadataNet.taskStatus selector:_metadataNet.selector selectorPost:_metadataNet.selectorPost parentRev:nil errorCode:_metadataNet.errorCode delegate:self];
  159. if (!_oneByOne)
  160. [self complete];
  161. }
  162. - (void)uploadOnlyPlist
  163. {
  164. [[CCNetworking sharedNetworking] uploadFile:_metadataNet.fileName serverUrl:_metadataNet.serverUrl cryptated:YES onlyPlist:YES session:_metadataNet.session taskStatus:_metadataNet.taskStatus selector:_metadataNet.selector selectorPost:_metadataNet.selectorPost parentRev:nil errorCode:_metadataNet.errorCode delegate:self];
  165. if (!_oneByOne)
  166. [self complete];
  167. }
  168. - (void)uploadAsset
  169. {
  170. [[CCNetworking sharedNetworking] uploadFileFromAssetLocalIdentifier:_metadataNet.assetLocalItentifier serverUrl:_metadataNet.serverUrl cryptated:_metadataNet.cryptated session:_metadataNet.session taskStatus:_metadataNet.taskStatus selector:_metadataNet.selector selectorPost:_metadataNet.selectorPost parentRev:nil errorCode:_metadataNet.errorCode delegate:self];
  171. if (!_oneByOne)
  172. [self complete];
  173. }
  174. - (void)uploadTemplate
  175. {
  176. [[CCNetworking sharedNetworking] uploadTemplate:_metadataNet.fileNamePrint fileNameCrypto:_metadataNet.fileName serverUrl:_metadataNet.serverUrl session:_metadataNet.session taskStatus:_metadataNet.taskStatus selector:_metadataNet.selector selectorPost:_metadataNet.selectorPost parentRev:nil errorCode:_metadataNet.errorCode delegate:self];
  177. if (!_oneByOne)
  178. [self complete];
  179. }
  180. - (void)uploadTaskSave:(NSURLSessionUploadTask *)uploadTask
  181. {
  182. _uploadTask = uploadTask;
  183. if ([self.delegate respondsToSelector:@selector(uploadTaskSave:)])
  184. [self.delegate uploadTaskSave:uploadTask];
  185. }
  186. - (void)uploadFileSuccess:(NSString *)fileID serverUrl:(NSString *)serverUrl selector:(NSString *)selector selectorPost:(NSString *)selectorPost
  187. {
  188. [self complete];
  189. if ([self.delegate respondsToSelector:@selector(uploadFileSuccess:serverUrl:selector:selectorPost:)])
  190. [self.delegate uploadFileSuccess:fileID serverUrl:serverUrl selector:selector selectorPost:selectorPost];
  191. }
  192. - (void)uploadFileFailure:(NSString *)fileID serverUrl:(NSString *)serverUrl selector:(NSString *)selector message:(NSString *)message errorCode:(NSInteger)errorCode
  193. {
  194. [self complete];
  195. if ([self.delegate respondsToSelector:@selector(uploadFileFailure:serverUrl:selector:message:errorCode:)])
  196. [self.delegate uploadFileFailure:fileID serverUrl:serverUrl selector:selector message:message errorCode:errorCode];
  197. }
  198. #pragma --------------------------------------------------------------------------------------------
  199. #pragma mark ===== downloadThumbnail =====
  200. #pragma --------------------------------------------------------------------------------------------
  201. - (void)downloadThumbnail
  202. {
  203. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  204. __block NSString *ext;
  205. NSInteger width = 0, height = 0;
  206. NSString *directoryUser = [CCUtility getDirectoryActiveUser:_activeUser activeUrl:_activeUrl];
  207. if ([_metadataNet.options.lowercaseString isEqualToString:@"xs"]) { width = 32; height = 32; ext = @"ico"; }
  208. else if ([_metadataNet.options.lowercaseString isEqualToString:@"s"]) { width = 64; height = 64; ext = @"ico"; }
  209. else if ([_metadataNet.options.lowercaseString isEqualToString:@"m"]) { width = 128; height = 128; ext = @"ico"; }
  210. else if ([_metadataNet.options.lowercaseString isEqualToString:@"l"]) { width = 640; height = 640; ext = @"pvw"; }
  211. else if ([_metadataNet.options.lowercaseString isEqualToString:@"xl"]) { width = 1024; height = 1024; ext = @"pvw"; }
  212. if ([[NSFileManager defaultManager] fileExistsAtPath:[NSString stringWithFormat:@"%@/%@.%@", directoryUser, _metadataNet.fileNameLocal, ext]]) {
  213. [self.delegate downloadThumbnailSuccess:_metadataNet];
  214. [self complete];
  215. return;
  216. }
  217. [communication setCredentialsWithUser:_activeUser andPassword:_activePassword];
  218. [communication getRemoteThumbnailByServer:[_activeUrl stringByAppendingString:@"/"] ofFilePath:_metadataNet.fileName withWidth:width andHeight:height onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSData *thumbnail, NSString *redirectedServer) {
  219. TableAccount *recordAccount = [CCCoreData getActiveAccount];
  220. if ([recordAccount.account isEqualToString:_metadataNet.account]) {
  221. UIImage *thumbnailImage = [UIImage imageWithData:thumbnail];
  222. NSString *directoryUser = [CCUtility getDirectoryActiveUser:_activeUser activeUrl:_activeUrl];
  223. [CCGraphics saveIcoWithFileID:_metadataNet.fileNameLocal image:thumbnailImage writeToFile:[NSString stringWithFormat:@"%@/%@.%@", directoryUser, _metadataNet.fileNameLocal, ext] copy:NO move:NO fromPath:nil toPath:nil];
  224. if ([self.delegate respondsToSelector:@selector(downloadThumbnailSuccess:)] && [_metadataNet.action isEqualToString:actionDownloadThumbnail])
  225. [self.delegate downloadThumbnailSuccess:_metadataNet];
  226. }
  227. [self complete];
  228. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  229. NSInteger errorCode = response.statusCode;
  230. if (errorCode == 0)
  231. errorCode = error.code;
  232. if ([self.delegate respondsToSelector:@selector(downloadThumbnailFailure:message:errorCode:)] && [_metadataNet.action isEqualToString:actionDownloadThumbnail])
  233. [self.delegate downloadThumbnailFailure:_metadataNet message:[CCError manageErrorOC:response.statusCode error:error] errorCode:errorCode];
  234. [self complete];
  235. }];
  236. }
  237. #pragma --------------------------------------------------------------------------------------------
  238. #pragma mark ===== Read Folder =====
  239. #pragma --------------------------------------------------------------------------------------------
  240. - (void)readFolder
  241. {
  242. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  243. [communication setCredentialsWithUser:_activeUser andPassword:_activePassword];
  244. [communication readFolder:_metadataNet.serverUrl withUserSessionToken:nil onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSArray *items, NSString *redirectedServer, NSString *token) {
  245. NSMutableArray *metadatas = [[NSMutableArray alloc] init];
  246. // directory [0]
  247. OCFileDto *itemDtoDirectory = [items objectAtIndex:0];
  248. NSString *permissions = itemDtoDirectory.permissions;
  249. NSString *rev = itemDtoDirectory.etag;
  250. NSDate *date = [NSDate dateWithTimeIntervalSince1970:itemDtoDirectory.date];
  251. NSString *directoryID = [CCCoreData addDirectory:_metadataNet.serverUrl date:date permissions:permissions activeAccount:_metadataNet.account];
  252. NSString *cameraFolderName = [CCCoreData getCameraUploadFolderNameActiveAccount:_metadataNet.account];
  253. NSString *cameraFolderPath = [CCCoreData getCameraUploadFolderPathActiveAccount:_metadataNet.account activeUrl:_activeUrl typeCloud:_typeCloud];
  254. NSString *directoryUser = [CCUtility getDirectoryActiveUser:_activeUser activeUrl:_activeUrl];
  255. // Update metadataNet.directoryID
  256. _metadataNet.directoryID = directoryID;
  257. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
  258. NSArray *itemsSortedArray = [items sortedArrayUsingComparator:^NSComparisonResult(id a, id b) {
  259. NSString *first = [(OCFileDto*)a fileName];
  260. NSString *second = [(OCFileDto*)b fileName];
  261. return [[first lowercaseString] compare:[second lowercaseString]];
  262. }];
  263. for (NSUInteger i=1; i < [itemsSortedArray count]; i++) {
  264. OCFileDto *itemDto = [itemsSortedArray objectAtIndex:i];
  265. itemDto.fileName = [CCUtility clearFile:itemDto.fileName];
  266. // ----- BUG #942 ---------
  267. if ([itemDto.etag length] == 0) {
  268. #ifndef SHARE_IN
  269. [app messageNotification:@"Server error" description:@"Metadata etag absent, record excluded, please fix" visible:YES delay:dismissAfterSecond type:TWMessageBarMessageTypeError];
  270. #endif
  271. continue;
  272. }
  273. // ------------------------
  274. [metadatas addObject:[CCUtility trasformedOCFileToCCMetadata:itemDto fileNamePrint:itemDto.fileName serverUrl:_metadataNet.serverUrl directoryID:directoryID cameraFolderName:cameraFolderName cameraFolderPath:cameraFolderPath activeAccount:_metadataNet.account directoryUser:directoryUser typeCloud:_typeCloud]];
  275. }
  276. dispatch_async(dispatch_get_main_queue(), ^{
  277. if ([self.delegate respondsToSelector:@selector(readFolderSuccess:permissions:rev:metadatas:)])
  278. [self.delegate readFolderSuccess:_metadataNet permissions:permissions rev:rev metadatas:metadatas];
  279. });
  280. });
  281. [self complete];
  282. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *token, NSString *redirectedServer) {
  283. NSInteger errorCode = response.statusCode;
  284. if (errorCode == 0)
  285. errorCode = error.code;
  286. if ([self.delegate respondsToSelector:@selector(readFolderFailure:message:errorCode:)])
  287. [self.delegate readFolderFailure:_metadataNet message:[error.userInfo valueForKey:@"NSLocalizedDescription"] errorCode:errorCode];
  288. // Request trusted certificated
  289. if ([error code] == NSURLErrorServerCertificateUntrusted)
  290. [[CCCertificate sharedManager] presentViewControllerCertificateWithTitle:[error localizedDescription] viewController:(UIViewController *)self.delegate delegate:self];
  291. [self complete];
  292. }];
  293. }
  294. #pragma --------------------------------------------------------------------------------------------
  295. #pragma mark ===== Create Folder =====
  296. #pragma --------------------------------------------------------------------------------------------
  297. - (void)createFolder
  298. {
  299. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  300. BOOL hasServerForbiddenCharactersSupport;
  301. NSString *nameFolderURL = [NSString stringWithFormat:@"%@/%@", _metadataNet.serverUrl, _metadataNet.fileName];
  302. [communication setCredentialsWithUser:_activeUser andPassword:_activePassword];
  303. [communication createFolder:nameFolderURL onCommunication:communication withForbiddenCharactersSupported:hasServerForbiddenCharactersSupport successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  304. if ([self.delegate respondsToSelector:@selector(createFolderSuccess:)])
  305. [self.delegate createFolderSuccess:_metadataNet];
  306. [self complete];
  307. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  308. NSString *message;
  309. if (([_metadataNet.fileName isEqualToString:[CCCoreData getCameraUploadFolderNameActiveAccount:_metadataNet.account]] == YES && [_metadataNet.serverUrl isEqualToString:[CCCoreData getCameraUploadFolderPathActiveAccount:_metadataNet.account activeUrl:_activeUrl typeCloud:_typeCloud]] == YES))
  310. message = nil;
  311. else
  312. message = [CCError manageErrorOC:response.statusCode error:error];
  313. NSInteger errorCode = response.statusCode;
  314. if (errorCode == 0)
  315. errorCode = error.code;
  316. if ([self.delegate respondsToSelector:@selector(createFolderFailure:message:errorCode:)])
  317. [self.delegate createFolderFailure:_metadataNet message:message errorCode:errorCode];
  318. // Request trusted certificated
  319. if ([error code] == NSURLErrorServerCertificateUntrusted)
  320. [[CCCertificate sharedManager] presentViewControllerCertificateWithTitle:[error localizedDescription] viewController:(UIViewController *)self.delegate delegate:self];
  321. [self complete];
  322. } errorBeforeRequest:^(NSError *error) {
  323. NSString *message;
  324. if (([_metadataNet.fileName isEqualToString:[CCCoreData getCameraUploadFolderNameActiveAccount:_metadataNet.account]] == YES && [_metadataNet.serverUrl isEqualToString:[CCCoreData getCameraUploadFolderPathActiveAccount:_metadataNet.account activeUrl:_activeUrl typeCloud:_typeCloud]] == YES))
  325. message = nil;
  326. else {
  327. if (error.code == OCErrorForbidenCharacters)
  328. message = NSLocalizedStringFromTable(@"_forbidden_characters_from_server_", @"Error", nil);
  329. else
  330. message = NSLocalizedStringFromTable(@"_unknow_response_server_", @"Error", nil);
  331. }
  332. if ([self.delegate respondsToSelector:@selector(createFolderFailure:message:errorCode:)])
  333. [self.delegate createFolderFailure:_metadataNet message:message errorCode:error.code];
  334. [self complete];
  335. }];
  336. }
  337. - (NSError *)createFolderSync:(NSString *)folderPathName
  338. {
  339. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  340. BOOL hasServerForbiddenCharactersSupport;
  341. __block NSError *returnError;
  342. dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
  343. [communication setCredentialsWithUser:_activeUser andPassword:_activePassword];
  344. [communication createFolder:folderPathName onCommunication:communication withForbiddenCharactersSupported:hasServerForbiddenCharactersSupport successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  345. dispatch_semaphore_signal(semaphore);
  346. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  347. // Request trusted certificated
  348. if ([error code] == NSURLErrorServerCertificateUntrusted)
  349. [[CCCertificate sharedManager] presentViewControllerCertificateWithTitle:[error localizedDescription] viewController:(UIViewController *)self.delegate delegate:self];
  350. returnError = error;
  351. dispatch_semaphore_signal(semaphore);
  352. } errorBeforeRequest:^(NSError *error) {
  353. returnError = error;
  354. dispatch_semaphore_signal(semaphore);
  355. }];
  356. while (dispatch_semaphore_wait(semaphore, DISPATCH_TIME_NOW))
  357. [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate dateWithTimeIntervalSinceNow:k_timeout_webdav]];
  358. return returnError;
  359. }
  360. #pragma --------------------------------------------------------------------------------------------
  361. #pragma mark ===== Delete =====
  362. #pragma --------------------------------------------------------------------------------------------
  363. - (void)deleteFileOrFolder
  364. {
  365. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  366. NSString *serverFileUrl = [NSString stringWithFormat:@"%@/%@", _metadataNet.serverUrl, _metadataNet.fileName];
  367. [communication setCredentialsWithUser:_activeUser andPassword:_activePassword];
  368. [communication deleteFileOrFolder:serverFileUrl onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  369. if ([_metadataNet.selector rangeOfString:selectorDelete].location != NSNotFound && [self.delegate respondsToSelector:@selector(deleteFileOrFolderSuccess:)])
  370. [self.delegate deleteFileOrFolderSuccess:_metadataNet];
  371. [self complete];
  372. } failureRquest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  373. NSInteger errorCode = response.statusCode;
  374. if (errorCode == 0)
  375. errorCode = error.code;
  376. if ([self.delegate respondsToSelector:@selector(deleteFileOrFolderFailure:message:errorCode:)])
  377. [self.delegate deleteFileOrFolderFailure:_metadataNet message:[error.userInfo valueForKey:@"NSLocalizedDescription"] errorCode:errorCode];
  378. // Request trusted certificated
  379. if ([error code] == NSURLErrorServerCertificateUntrusted)
  380. [[CCCertificate sharedManager] presentViewControllerCertificateWithTitle:[error localizedDescription] viewController:(UIViewController *)self.delegate delegate:self];
  381. [self complete];
  382. }];
  383. }
  384. #pragma --------------------------------------------------------------------------------------------
  385. #pragma mark ===== Move =====
  386. #pragma --------------------------------------------------------------------------------------------
  387. - (void)moveFileOrFolder
  388. {
  389. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  390. BOOL hasServerForbiddenCharactersSupport;
  391. NSString *origineURL = [NSString stringWithFormat:@"%@/%@", _metadataNet.serverUrl, _metadataNet.fileName];
  392. NSString *destinazioneURL = [NSString stringWithFormat:@"%@/%@", _metadataNet.serverUrlTo, _metadataNet.fileNameTo];
  393. [communication setCredentialsWithUser:_activeUser andPassword:_activePassword];
  394. [communication moveFileOrFolder:origineURL toDestiny:destinazioneURL onCommunication:communication withForbiddenCharactersSupported:hasServerForbiddenCharactersSupport successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  395. if ([_metadataNet.selector isEqualToString:selectorRename] && [self.delegate respondsToSelector:@selector(renameSuccess:revTo:)])
  396. [self.delegate renameSuccess:_metadataNet revTo:nil];
  397. if ([_metadataNet.selector rangeOfString:selectorMove].location != NSNotFound && [self.delegate respondsToSelector:@selector(moveSuccess:revTo:)])
  398. [self.delegate moveSuccess:_metadataNet revTo:nil];
  399. [self complete];
  400. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  401. NSInteger errorCode = response.statusCode;
  402. if (errorCode == 0)
  403. errorCode = error.code;
  404. if ([self.delegate respondsToSelector:@selector(moveFileOrFolderFailure:message:errorCode:)])
  405. [self.delegate moveFileOrFolderFailure:_metadataNet message:[CCError manageErrorOC:response.statusCode error:error] errorCode:errorCode];
  406. // Request trusted certificated
  407. if ([error code] == NSURLErrorServerCertificateUntrusted)
  408. [[CCCertificate sharedManager] presentViewControllerCertificateWithTitle:[error localizedDescription] viewController:(UIViewController *)self.delegate delegate:self];
  409. [self complete];
  410. } errorBeforeRequest:^(NSError *error) {
  411. NSString *message;
  412. if (error.code == OCErrorMovingTheDestinyAndOriginAreTheSame) {
  413. message = NSLocalizedStringFromTable(@"_error_folder_destiny_is_the_same_", @"Error", nil);
  414. } else if (error.code == OCErrorMovingFolderInsideHimself) {
  415. message = NSLocalizedStringFromTable(@"_error_folder_destiny_is_the_same_", @"Error", nil);
  416. } else if (error.code == OCErrorMovingDestinyNameHaveForbiddenCharacters) {
  417. message = NSLocalizedStringFromTable(@"_forbidden_characters_from_server_", @"Error", nil);
  418. } else {
  419. message = NSLocalizedStringFromTable(@"_unknow_response_server_", @"Error", nil);
  420. }
  421. if ([self.delegate respondsToSelector:@selector(moveFileOrFolderFailure:message:errorCode:)])
  422. [self.delegate moveFileOrFolderFailure:_metadataNet message:message errorCode:error.code];
  423. [self complete];
  424. }];
  425. }
  426. #pragma --------------------------------------------------------------------------------------------
  427. #pragma mark ===== ReadFile =====
  428. #pragma --------------------------------------------------------------------------------------------
  429. - (void)readFile
  430. {
  431. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  432. NSString *fileName;
  433. if (_metadataNet.fileName)
  434. fileName = [NSString stringWithFormat:@"%@/%@", _metadataNet.serverUrl, _metadataNet.fileName];
  435. else
  436. fileName = _metadataNet.serverUrl;
  437. [communication setCredentialsWithUser:_activeUser andPassword:_activePassword];
  438. [communication readFile:fileName onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSArray *items, NSString *redirectedServer) {
  439. TableAccount *recordAccount = [CCCoreData getActiveAccount];
  440. if ([recordAccount.account isEqualToString:_metadataNet.account] && [items count] > 0) {
  441. CCMetadata *metadata = [[CCMetadata alloc] init];
  442. OCFileDto *itemDto = [items objectAtIndex:0];
  443. itemDto.fileName = _metadataNet.fileName;
  444. NSString *directoryID = [CCCoreData getDirectoryIDFromServerUrl:_metadataNet.serverUrl activeAccount:_metadataNet.account];
  445. NSString *cameraFolderName = [CCCoreData getCameraUploadFolderNameActiveAccount:_metadataNet.account];
  446. NSString *cameraFolderPath = [CCCoreData getCameraUploadFolderPathActiveAccount:_metadataNet.account activeUrl:_activeUrl typeCloud:_typeCloud];
  447. NSString *directoryUser = [CCUtility getDirectoryActiveUser:_activeUser activeUrl:_activeUrl];
  448. metadata = [CCUtility trasformedOCFileToCCMetadata:itemDto fileNamePrint:_metadataNet.fileNamePrint serverUrl:_metadataNet.serverUrl directoryID:directoryID cameraFolderName:cameraFolderName cameraFolderPath:cameraFolderPath activeAccount:_metadataNet.account directoryUser:directoryUser typeCloud:_typeCloud];
  449. if([self.delegate respondsToSelector:@selector(readFileSuccess:metadata:)])
  450. [self.delegate readFileSuccess:_metadataNet metadata:metadata];
  451. }
  452. // BUG 1038
  453. if ([items count] == 0) {
  454. #ifndef SHARE_IN
  455. [app messageNotification:@"Server error" description:@"readFile, items NULL, please fix" visible:YES delay:dismissAfterSecond type:TWMessageBarMessageTypeError];
  456. #endif
  457. }
  458. [self complete];
  459. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  460. TableAccount *recordAccount = [CCCoreData getActiveAccount];
  461. _metadataNet.errorRetry++;
  462. NSInteger errorCode = response.statusCode;
  463. if (errorCode == 0)
  464. errorCode = error.code;
  465. if([self.delegate respondsToSelector:@selector(readFileFailure:message:errorCode:)] && [recordAccount.account isEqualToString:_metadataNet.account])
  466. [self.delegate readFileFailure:_metadataNet message:[CCError manageErrorOC:response.statusCode error:error] errorCode:errorCode];
  467. // Request trusted certificated
  468. if ([error code] == NSURLErrorServerCertificateUntrusted)
  469. [[CCCertificate sharedManager] presentViewControllerCertificateWithTitle:[error localizedDescription] viewController:(UIViewController *)self.delegate delegate:self];
  470. [self complete];
  471. }];
  472. }
  473. - (NSError *)readFileSync:(NSString *)filePathName
  474. {
  475. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  476. __block NSError *returnError;
  477. dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
  478. [communication setCredentialsWithUser:_activeUser andPassword:_activePassword];
  479. [communication readFile:filePathName onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSArray *items, NSString *redirectedServer) {
  480. returnError = nil;
  481. dispatch_semaphore_signal(semaphore);
  482. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  483. // Request trusted certificated
  484. if ([error code] == NSURLErrorServerCertificateUntrusted)
  485. [[CCCertificate sharedManager] presentViewControllerCertificateWithTitle:[error localizedDescription] viewController:(UIViewController *)self.delegate delegate:self];
  486. returnError = error;
  487. dispatch_semaphore_signal(semaphore);
  488. }];
  489. while (dispatch_semaphore_wait(semaphore, DISPATCH_TIME_NOW))
  490. [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate dateWithTimeIntervalSinceNow:k_timeout_webdav]];
  491. return returnError;
  492. }
  493. #pragma --------------------------------------------------------------------------------------------
  494. #pragma mark ===== Shared =====
  495. #pragma --------------------------------------------------------------------------------------------
  496. - (NSMutableDictionary *)getShareID
  497. {
  498. #ifndef SHARE_IN
  499. return app.sharesID;
  500. #endif
  501. return [NSMutableDictionary new];
  502. }
  503. - (void)readShareServer
  504. {
  505. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  506. [communication setCredentialsWithUser:_activeUser andPassword:_activePassword];
  507. [communication readSharedByServer:[_activeUrl stringByAppendingString:@"/"] onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSArray *items, NSString *redirectedServer) {
  508. BOOL openWindow = NO;
  509. [[self getShareID] removeAllObjects];
  510. TableAccount *recordAccount = [CCCoreData getActiveAccount];
  511. if ([recordAccount.account isEqualToString:_metadataNet.account]) {
  512. for (OCSharedDto *item in items)
  513. [[self getShareID] setObject:item forKey:[@(item.idRemoteShared) stringValue]];
  514. if ([_metadataNet.selector isEqual:selectorOpenWindowShare]) openWindow = YES;
  515. if ([_metadataNet.action isEqual:actionUpdateShare]) openWindow = YES;
  516. if ([_metadataNet.action isEqual:actionShare]) openWindow = YES;
  517. if ([_metadataNet.action isEqual:actionShareWith]) openWindow = YES;
  518. }
  519. if([self.delegate respondsToSelector:@selector(readSharedSuccess:items:openWindow:)])
  520. [self.delegate readSharedSuccess:_metadataNet items:[self getShareID] openWindow:openWindow];
  521. [self complete];
  522. } failureRequest :^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  523. NSInteger errorCode = response.statusCode;
  524. if (errorCode == 0)
  525. errorCode = error.code;
  526. if([self.delegate respondsToSelector:@selector(shareFailure:message:errorCode:)])
  527. [self.delegate shareFailure:_metadataNet message:[error.userInfo valueForKey:@"NSLocalizedDescription"] errorCode:errorCode];
  528. // Request trusted certificated
  529. if ([error code] == NSURLErrorServerCertificateUntrusted)
  530. [[CCCertificate sharedManager] presentViewControllerCertificateWithTitle:[error localizedDescription] viewController:(UIViewController *)self.delegate delegate:self];
  531. [self complete];
  532. }];
  533. }
  534. - (void)share
  535. {
  536. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  537. [communication setCredentialsWithUser:_activeUser andPassword:_activePassword];
  538. [communication shareFileOrFolderByServer:[_activeUrl stringByAppendingString:@"/"] andFileOrFolderPath:[_metadataNet.fileName encodeString:NSUTF8StringEncoding] andPassword:[_metadataNet.password encodeString:NSUTF8StringEncoding] onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *token, NSString *redirectedServer) {
  539. [self readShareServer];
  540. } failureRequest :^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  541. NSInteger errorCode = response.statusCode;
  542. if (errorCode == 0)
  543. errorCode = error.code;
  544. if([self.delegate respondsToSelector:@selector(shareFailure:message:errorCode:)])
  545. [self.delegate shareFailure:_metadataNet message:[error.userInfo valueForKey:@"NSLocalizedDescription"] errorCode:errorCode];
  546. // Request trusted certificated
  547. if ([error code] == NSURLErrorServerCertificateUntrusted)
  548. [[CCCertificate sharedManager] presentViewControllerCertificateWithTitle:[error localizedDescription] viewController:(UIViewController *)self.delegate delegate:self];
  549. [self complete];
  550. }];
  551. }
  552. // * @param shareeType -> NSInteger: to set the type of sharee (user/group/federated)
  553. - (void)shareWith
  554. {
  555. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  556. [communication setCredentialsWithUser:_activeUser andPassword:_activePassword];
  557. [communication shareWith:_metadataNet.share shareeType:_metadataNet.shareeType inServer:[_activeUrl stringByAppendingString:@"/"] andFileOrFolderPath:[_metadataNet.fileName encodeString:NSUTF8StringEncoding] andPermissions:_metadataNet.sharePermission onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  558. [self readShareServer];
  559. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  560. NSInteger errorCode = response.statusCode;
  561. if (errorCode == 0)
  562. errorCode = error.code;
  563. if([self.delegate respondsToSelector:@selector(shareFailure:message:errorCode:)])
  564. [self.delegate shareFailure:_metadataNet message:[error.userInfo valueForKey:@"NSLocalizedDescription"] errorCode:errorCode];
  565. // Request trusted certificated
  566. if ([error code] == NSURLErrorServerCertificateUntrusted)
  567. [[CCCertificate sharedManager] presentViewControllerCertificateWithTitle:[error localizedDescription] viewController:(UIViewController *)self.delegate delegate:self];
  568. [self complete];
  569. }];
  570. }
  571. - (void)updateShare
  572. {
  573. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  574. [communication setCredentialsWithUser:_activeUser andPassword:_activePassword];
  575. [communication updateShare:[_metadataNet.share intValue] ofServerPath:[_activeUrl stringByAppendingString:@"/"] withPasswordProtect:[_metadataNet.password encodeString:NSUTF8StringEncoding] andExpirationTime:_metadataNet.expirationTime andPermissions:_metadataNet.sharePermission onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  576. [self readShareServer];
  577. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  578. #ifndef SHARE_IN
  579. [app messageNotification:@"_error_" description:[CCError manageErrorOC:response.statusCode error:error] visible:YES delay:dismissAfterSecond type:TWMessageBarMessageTypeError];
  580. #endif
  581. NSInteger errorCode = response.statusCode;
  582. if (errorCode == 0)
  583. errorCode = error.code;
  584. if([self.delegate respondsToSelector:@selector(shareFailure:message:errorCode:)])
  585. [self.delegate shareFailure:_metadataNet message:[error.userInfo valueForKey:@"NSLocalizedDescription"] errorCode:errorCode];
  586. // Request trusted certificated
  587. if ([error code] == NSURLErrorServerCertificateUntrusted)
  588. [[CCCertificate sharedManager] presentViewControllerCertificateWithTitle:[error localizedDescription] viewController:(UIViewController *)self.delegate delegate:self];
  589. [self complete];
  590. }];
  591. }
  592. - (void)unShare
  593. {
  594. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  595. [communication setCredentialsWithUser:_activeUser andPassword:_activePassword];
  596. [communication unShareFileOrFolderByServer:[_activeUrl stringByAppendingString:@"/"] andIdRemoteShared:[_metadataNet.share intValue] onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  597. if([self.delegate respondsToSelector:@selector(unShareSuccess:)])
  598. [self.delegate unShareSuccess:_metadataNet];
  599. [self complete];
  600. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  601. #ifndef SHARE_IN
  602. [app messageNotification:@"_error_" description:[CCError manageErrorOC:response.statusCode error:error] visible:YES delay:dismissAfterSecond type:TWMessageBarMessageTypeError];
  603. #endif
  604. NSInteger errorCode = response.statusCode;
  605. if (errorCode == 0)
  606. errorCode = error.code;
  607. if([self.delegate respondsToSelector:@selector(shareFailure:message:errorCode:)])
  608. [self.delegate shareFailure:_metadataNet message:[error.userInfo valueForKey:@"NSLocalizedDescription"] errorCode:errorCode];
  609. // Request trusted certificated
  610. if ([error code] == NSURLErrorServerCertificateUntrusted)
  611. [[CCCertificate sharedManager] presentViewControllerCertificateWithTitle:[error localizedDescription] viewController:(UIViewController *)self.delegate delegate:self];
  612. [self complete];
  613. }];
  614. }
  615. - (void)getUserAndGroup
  616. {
  617. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  618. [communication setCredentialsWithUser:_activeUser andPassword:_activePassword];
  619. [communication searchUsersAndGroupsWith:_metadataNet.options forPage:1 with:50 ofServer:[_activeUrl stringByAppendingString:@"/"] onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSArray *itemList, NSString *redirectedServer) {
  620. if([self.delegate respondsToSelector:@selector(getUserAndGroupSuccess:items:)])
  621. [self.delegate getUserAndGroupSuccess:_metadataNet items:itemList];
  622. [self complete];
  623. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  624. NSInteger errorCode = response.statusCode;
  625. if (errorCode == 0)
  626. errorCode = error.code;
  627. if([self.delegate respondsToSelector:@selector(getUserAndGroupFailure:message:errorCode:)])
  628. [self.delegate getUserAndGroupFailure:_metadataNet message:[error.userInfo valueForKey:@"NSLocalizedDescription"] errorCode:errorCode];
  629. // Request trusted certificated
  630. if ([error code] == NSURLErrorServerCertificateUntrusted)
  631. [[CCCertificate sharedManager] presentViewControllerCertificateWithTitle:[error localizedDescription] viewController:(UIViewController *)self.delegate delegate:self];
  632. [self complete];
  633. }];
  634. }
  635. #pragma --------------------------------------------------------------------------------------------
  636. #pragma mark ===== Server =====
  637. #pragma --------------------------------------------------------------------------------------------
  638. - (void)getFeaturesSupportedByServer
  639. {
  640. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  641. [communication setCredentialsWithUser:_activeUser andPassword:_activePassword];
  642. [communication getFeaturesSupportedByServer:[_activeUrl stringByAppendingString:@"/"] onCommunication:communication successRequest:^(NSHTTPURLResponse *response, BOOL hasShareSupport, BOOL hasShareeSupport, BOOL hasCookiesSupport, BOOL hasForbiddenCharactersSupport, BOOL hasCapabilitiesSupport, BOOL hasFedSharesOptionShareSupport, NSString *redirectedServer) {
  643. TableAccount *recordAccount = [CCCoreData getActiveAccount];
  644. if ([self.delegate respondsToSelector:@selector(getFeaturesSupportedByServerSuccess:hasForbiddenCharactersSupport:hasShareSupport:hasShareeSupport:)] && [recordAccount.account isEqualToString:_metadataNet.account])
  645. [self.delegate getFeaturesSupportedByServerSuccess:hasCapabilitiesSupport hasForbiddenCharactersSupport:hasForbiddenCharactersSupport hasShareSupport:hasShareSupport hasShareeSupport:hasShareeSupport];
  646. [self complete];
  647. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  648. NSInteger errorCode = response.statusCode;
  649. if (errorCode == 0)
  650. errorCode = error.code;
  651. if([self.delegate respondsToSelector:@selector(getInfoServerFailure:message:errorCode:)])
  652. [self.delegate getInfoServerFailure:_metadataNet message:[error.userInfo valueForKey:@"NSLocalizedDescription"] errorCode:errorCode];
  653. // Request trusted certificated
  654. if ([error code] == NSURLErrorServerCertificateUntrusted)
  655. [[CCCertificate sharedManager] presentViewControllerCertificateWithTitle:[error localizedDescription] viewController:(UIViewController *)self.delegate delegate:self];
  656. [self complete];
  657. }];
  658. }
  659. - (void)getCapabilitiesOfServer
  660. {
  661. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  662. [communication setCredentialsWithUser:_activeUser andPassword:_activePassword];
  663. [communication getCapabilitiesOfServer:[_activeUrl stringByAppendingString:@"/"] onCommunication:communication successRequest:^(NSHTTPURLResponse *response, OCCapabilities *capabilities, NSString *redirectedServer) {
  664. TableAccount *recordAccount = [CCCoreData getActiveAccount];
  665. if ([self.delegate respondsToSelector:@selector(getCapabilitiesOfServerSuccess:)] && [recordAccount.account isEqualToString:_metadataNet.account])
  666. [self.delegate getCapabilitiesOfServerSuccess:capabilities];
  667. [self complete];
  668. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  669. NSInteger errorCode = response.statusCode;
  670. if (errorCode == 0)
  671. errorCode = error.code;
  672. if([self.delegate respondsToSelector:@selector(getInfoServerFailure:message:errorCode:)])
  673. [self.delegate getInfoServerFailure:_metadataNet message:[error.userInfo valueForKey:@"NSLocalizedDescription"] errorCode:errorCode];
  674. // Request trusted certificated
  675. if ([error code] == NSURLErrorServerCertificateUntrusted)
  676. [[CCCertificate sharedManager] presentViewControllerCertificateWithTitle:[error localizedDescription] viewController:(UIViewController *)self.delegate delegate:self];
  677. [self complete];
  678. }];
  679. }
  680. @end
  681. #pragma --------------------------------------------------------------------------------------------
  682. #pragma mark ===== OCURLSessionManager =====
  683. #pragma --------------------------------------------------------------------------------------------
  684. @implementation OCURLSessionManager
  685. - (void)URLSession:(NSURLSession *)session didReceiveChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition disposition, NSURLCredential *credential))completionHandler
  686. {
  687. // The pinnning check
  688. if ([[CCCertificate sharedManager] checkTrustedChallenge:challenge]) {
  689. completionHandler(NSURLSessionAuthChallengeUseCredential, [NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust]);
  690. } else {
  691. completionHandler(NSURLSessionAuthChallengePerformDefaultHandling, nil);
  692. }
  693. }
  694. @end