OCWebDAVClient.m 65 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185
  1. //
  2. // OCWebDAVClient.m
  3. // OCWebDAVClient
  4. //
  5. // This class is based in https://github.com/zwaldowski/DZWebDAVClient. Copyright (c) 2012 Zachary Waldowski, Troy Brant, Marcus Rohrmoser, and Sam Soffes.
  6. //
  7. // Copyright (C) 2016, ownCloud GmbH. ( http://www.owncloud.org/ )
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining a copy
  10. // of this software and associated documentation files (the "Software"), to deal
  11. // in the Software without restriction, including without limitation the rights
  12. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. // copies of the Software, and to permit persons to whom the Software is
  14. // furnished to do so, subject to the following conditions:
  15. // The above copyright notice and this permission notice shall be included in
  16. // all copies or substantial portions of the Software.
  17. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  23. // THE SOFTWARE.
  24. //
  25. //
  26. // Add : getNotificationServer & setNotificationServer
  27. // Add : getUserProfileServer
  28. // Add : Support for Favorite
  29. // Add : getActivityServer
  30. //
  31. // Author Marino Faggiana <m.faggiana@twsweb.it>
  32. // Copyright (c) 2017 TWS. All rights reserved.
  33. //
  34. #import "OCWebDAVClient.h"
  35. #import "OCFrameworkConstants.h"
  36. #import "OCCommunication.h"
  37. #import "UtilsFramework.h"
  38. #import "AFURLSessionManager.h"
  39. #import "NSString+Encode.h"
  40. #import "OCConstants.h"
  41. #define k_api_user_url_xml @"index.php/ocs/cloud/user"
  42. #define k_api_user_url_json @"index.php/ocs/cloud/user?format=json"
  43. #define k_server_information_json @"status.php"
  44. #define k_api_header_request @"OCS-APIREQUEST"
  45. #define k_group_sharee_type 1
  46. NSString const *OCWebDAVContentTypeKey = @"getcontenttype";
  47. NSString const *OCWebDAVETagKey = @"getetag";
  48. NSString const *OCWebDAVCTagKey = @"getctag";
  49. NSString const *OCWebDAVCreationDateKey = @"creationdate";
  50. NSString const *OCWebDAVModificationDateKey = @"modificationdate";
  51. @interface OCWebDAVClient()
  52. - (void)mr_listPath:(NSString *)path depth:(NSString *)depth onCommunication:
  53. (OCCommunication *)sharedOCCommunication
  54. success:(void(^)(NSHTTPURLResponse *, id))success
  55. failure:(void(^)(NSHTTPURLResponse *, id _Nullable responseObject, NSError *))failure;
  56. @end
  57. @implementation OCWebDAVClient
  58. - (id) init {
  59. self = [super init];
  60. if (self != nil) {
  61. self.defaultHeaders = [NSMutableDictionary new];
  62. }
  63. return self;
  64. }
  65. - (void)setAuthorizationHeaderWithUsername:(NSString *)username password:(NSString *)password {
  66. NSString *basicAuthCredentials = [NSString stringWithFormat:@"%@:%@", username, password];
  67. [self.defaultHeaders setObject:[NSString stringWithFormat:@"Basic %@", [UtilsFramework AFBase64EncodedStringFromString: basicAuthCredentials]] forKey:@"Authorization"];
  68. }
  69. - (void)setAuthorizationHeaderWithCookie:(NSString *) cookieString {
  70. [self.defaultHeaders setObject:cookieString forKey:@"Cookie"];
  71. }
  72. - (void)setAuthorizationHeaderWithToken:(NSString *)token {
  73. [self.defaultHeaders setObject:token forKey:@"Authorization"];
  74. }
  75. - (void)setDefaultHeader:(NSString *)header value:(NSString *)value {
  76. [self.defaultHeaders setObject:value forKey:header];
  77. }
  78. - (void)setUserAgent:(NSString *)userAgent{
  79. [self.defaultHeaders setObject:userAgent forKey:@"User-Agent"];
  80. }
  81. - (OCHTTPRequestOperation *)mr_operationWithRequest:(NSMutableURLRequest *)request onCommunication:(OCCommunication *)sharedOCCommunication withUserSessionToken:(NSString*)token success:(void(^)(NSHTTPURLResponse *operation, id response, NSString *token))success failure:(void(^)(NSHTTPURLResponse *operation, id _Nullable responseObject, NSError *error, NSString *token))failure {
  82. //If is not nil is a redirection so we keep the original url server
  83. if (!self.originalUrlServer) {
  84. self.originalUrlServer = [request.URL absoluteString];
  85. }
  86. if (sharedOCCommunication.isCookiesAvailable) {
  87. //We add the cookies of that URL
  88. request = [UtilsFramework getRequestWithCookiesByRequest:request andOriginalUrlServer:self.originalUrlServer];
  89. } else {
  90. [UtilsFramework deleteAllCookies];
  91. }
  92. OCHTTPRequestOperation *operation = (OCHTTPRequestOperation*) [sharedOCCommunication.networkSessionManager dataTaskWithRequest:request completionHandler:^(NSURLResponse * _Nonnull response, id _Nullable responseObject, NSError * _Nullable error) {
  93. if (!error) {
  94. success((NSHTTPURLResponse*)response,responseObject, token);
  95. } else {
  96. failure((NSHTTPURLResponse*)response, responseObject, error, token);
  97. }
  98. }];
  99. return operation;
  100. }
  101. - (OCHTTPRequestOperation *)mr_operationWithRequest:(NSMutableURLRequest *)request onCommunication:(OCCommunication *)sharedOCCommunication success:(void(^)(NSHTTPURLResponse *, id))success failure:(void(^)(NSHTTPURLResponse *, id _Nullable responseObject, NSError *))failure {
  102. //If is not nil is a redirection so we keep the original url server
  103. if (!self.originalUrlServer) {
  104. self.originalUrlServer = [request.URL absoluteString];
  105. }
  106. if (sharedOCCommunication.isCookiesAvailable) {
  107. //We add the cookies of that URL
  108. request = [UtilsFramework getRequestWithCookiesByRequest:request andOriginalUrlServer:self.originalUrlServer];
  109. } else {
  110. [UtilsFramework deleteAllCookies];
  111. }
  112. OCHTTPRequestOperation *operation = (OCHTTPRequestOperation*) [sharedOCCommunication.networkSessionManager dataTaskWithRequest:request completionHandler:^(NSURLResponse * _Nonnull response, id _Nullable responseObject, NSError * _Nullable error) {
  113. if (!error) {
  114. success((NSHTTPURLResponse*)response,responseObject);
  115. } else {
  116. failure((NSHTTPURLResponse*)response, responseObject, error);
  117. }
  118. }];
  119. return operation;
  120. }
  121. - (NSMutableURLRequest *)requestWithMethod:(NSString *)method path:(NSString *)path parameters:(NSDictionary *)parameters {
  122. NSMutableURLRequest *request = [[AFHTTPRequestSerializer new] requestWithMethod:method URLString:path parameters:nil error:nil];
  123. [request setAllHTTPHeaderFields:self.defaultHeaders];
  124. [request setCachePolicy: NSURLRequestReloadIgnoringLocalCacheData];
  125. [request setTimeoutInterval: k_timeout_webdav];
  126. return request;
  127. }
  128. - (NSMutableURLRequest *)sharedRequestWithMethod:(NSString *)method path:(NSString *)path parameters:(NSDictionary *)parameters {
  129. NSMutableURLRequest *request = [[AFHTTPRequestSerializer new] requestWithMethod:method URLString:path parameters:nil error:nil];
  130. [request setAllHTTPHeaderFields:self.defaultHeaders];
  131. //NSMutableURLRequest *request = [super requestWithMethod:method path:path parameters:parameters];
  132. [request setCachePolicy: NSURLRequestReloadIgnoringLocalCacheData];
  133. [request setTimeoutInterval: k_timeout_webdav];
  134. //Header for use the OC API CALL
  135. NSString *ocs_apiquests = @"true";
  136. [request setValue:ocs_apiquests forHTTPHeaderField:k_api_header_request];
  137. [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
  138. [request addValue:@"gzip, deflate" forHTTPHeaderField:@"Accept-Encoding"];
  139. return request;
  140. }
  141. - (void)movePath:(NSString *)source toPath:(NSString *)destination
  142. onCommunication:(OCCommunication *)sharedOCCommunication
  143. success:(void(^)(NSHTTPURLResponse *, id))success
  144. failure:(void(^)(NSHTTPURLResponse *, id _Nullable responseObject, NSError *))failure {
  145. _requestMethod = @"MOVE";
  146. NSMutableURLRequest *request = [self requestWithMethod:_requestMethod path:source parameters:nil];
  147. [request setValue:destination forHTTPHeaderField:@"Destination"];
  148. [request setValue:@"T" forHTTPHeaderField:@"Overwrite"];
  149. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  150. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  151. [operation resume];
  152. }
  153. - (void)deletePath:(NSString *)path
  154. onCommunication:(OCCommunication *)sharedOCCommunication
  155. success:(void(^)(NSHTTPURLResponse *, id))success
  156. failure:(void(^)(NSHTTPURLResponse *, id _Nullable responseObject, NSError *))failure {
  157. _requestMethod = @"DELETE";
  158. NSMutableURLRequest *request = [self requestWithMethod:_requestMethod path:path parameters:nil];
  159. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  160. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  161. [operation resume];
  162. }
  163. - (void)mr_listPath:(NSString *)path depth:(NSString *)depth onCommunication:
  164. (OCCommunication *)sharedOCCommunication
  165. success:(void(^)(NSHTTPURLResponse *, id))success
  166. failure:(void(^)(NSHTTPURLResponse *, id _Nullable responseObject, NSError *))failure {
  167. NSParameterAssert(success);
  168. _requestMethod = @"PROPFIND";
  169. NSMutableURLRequest *request = [self requestWithMethod:_requestMethod path:path parameters:nil];
  170. [request setValue: depth forHTTPHeaderField: @"Depth"];
  171. [request setHTTPBody:[@"<?xml version=\"1.0\" encoding=\"UTF-8\"?><D:propfind xmlns:D=\"DAV:\"><D:prop><D:resourcetype/><D:getlastmodified/><size xmlns=\"http://owncloud.org/ns\"/><favorite xmlns=\"http://owncloud.org/ns\"/><id xmlns=\"http://owncloud.org/ns\"/><D:getcontentlength/><D:getetag/><permissions xmlns=\"http://owncloud.org/ns\"/><D:getcontenttype/></D:prop></D:propfind>" dataUsingEncoding:NSUTF8StringEncoding]];
  172. [request setValue:@"application/xml" forHTTPHeaderField:@"Content-Type"];
  173. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  174. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  175. [operation resume];
  176. }
  177. - (void)mr_listPath:(NSString *)path depth:(NSString *)depth withUserSessionToken:(NSString*)token onCommunication:
  178. (OCCommunication *)sharedOCCommunication
  179. success:(void(^)(NSHTTPURLResponse *operation, id response, NSString *token))success
  180. failure:(void(^)(NSHTTPURLResponse *response, id _Nullable responseObject, NSError *, NSString *token))failure {
  181. NSParameterAssert(success);
  182. _requestMethod = @"PROPFIND";
  183. NSMutableURLRequest *request = [self requestWithMethod:_requestMethod path:path parameters:nil];
  184. [request setValue: depth forHTTPHeaderField: @"Depth"];
  185. [request setHTTPBody:[@"<?xml version=\"1.0\" encoding=\"UTF-8\"?><D:propfind xmlns:D=\"DAV:\"><D:prop><D:resourcetype/><D:getlastmodified/><size xmlns=\"http://owncloud.org/ns\"/><favorite xmlns=\"http://owncloud.org/ns\"/><id xmlns=\"http://owncloud.org/ns\"/><D:getcontentlength/><D:getetag/><permissions xmlns=\"http://owncloud.org/ns\"/><D:getcontenttype/></D:prop></D:propfind>" dataUsingEncoding:NSUTF8StringEncoding]];
  186. [request setValue:@"application/xml" forHTTPHeaderField:@"Content-Type"];
  187. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication withUserSessionToken:token success:success failure:failure];
  188. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  189. [operation resume];
  190. }
  191. - (void)propertiesOfPath:(NSString *)path
  192. onCommunication: (OCCommunication *)sharedOCCommunication
  193. success:(void(^)(NSHTTPURLResponse *, id ))success
  194. failure:(void(^)(NSHTTPURLResponse *, id _Nullable responseObject, NSError *))failure {
  195. [self mr_listPath:path depth:@"0" onCommunication:sharedOCCommunication success:success failure:failure];
  196. }
  197. - (void)listPath:(NSString *)path depth:(NSString *)depth
  198. onCommunication:(OCCommunication *)sharedOCCommunication
  199. success:(void(^)(NSHTTPURLResponse *, id))success
  200. failure:(void(^)(NSHTTPURLResponse *, id _Nullable responseObject, NSError *))failure {
  201. [self mr_listPath:path depth:depth onCommunication:sharedOCCommunication success:success failure:failure];
  202. }
  203. - (void)listPath:(NSString *)path depth:(NSString *)depth
  204. onCommunication:(OCCommunication *)sharedOCCommunication withUserSessionToken:(NSString *)token
  205. success:(void(^)(NSHTTPURLResponse *, id, NSString *token))success
  206. failure:(void(^)(NSHTTPURLResponse *, id _Nullable responseObject, NSError *, NSString *token))failure {
  207. [self mr_listPath:path depth:depth withUserSessionToken:token onCommunication:sharedOCCommunication success:success failure:failure];
  208. }
  209. - (void)search:(NSString *)path folder:(NSString *)folder fileName:(NSString *)fileName depth:(NSString *)depth dateLastModified:(NSString *)dateLastModified contentType:(NSString *)contentType user:(NSString *)user userID:(NSString *)userID onCommunication:(OCCommunication *)sharedOCCommunication withUserSessionToken:(NSString *)token success:(void(^)(NSHTTPURLResponse *, id, NSString *token))success failure:(void(^)(NSHTTPURLResponse *, id _Nullable responseObject, NSError *, NSString *token))failure {
  210. NSString *body;
  211. NSParameterAssert(success);
  212. _requestMethod = @"SEARCH";
  213. NSMutableURLRequest *request = [self requestWithMethod:_requestMethod path:path parameters:nil];
  214. body = [NSString stringWithFormat: @""
  215. "<?xml version=\"1.0\"?>"
  216. "<d:searchrequest xmlns:d=\"DAV:\" xmlns:oc=\"http://owncloud.org/ns\">"
  217. "<d:basicsearch>"
  218. "<d:select>"
  219. "<d:prop>"
  220. "<d:getlastmodified />"
  221. "<d:getetag />"
  222. "<d:getcontenttype />"
  223. "<d:resourcetype/>"
  224. "<d:getcontentlength />"
  225. "<oc:fileid/>"
  226. "<oc:id/>"
  227. "<oc:permissions />"
  228. "<oc:size />"
  229. "<oc:favorite/>"
  230. "</d:prop>"
  231. "</d:select>"
  232. "<d:from>"
  233. "<d:scope>"
  234. "<d:href>/files/%@%@</d:href>"
  235. "<d:depth>infinity</d:depth>"
  236. "</d:scope>"
  237. "</d:from>"
  238. "<d:where>"
  239. "<d:like>"
  240. //"<d:prop><d:getcontenttype/></d:prop>"
  241. //"<d:literal>image/%</d:literal>"
  242. "<d:prop><d:displayname/></d:prop>"
  243. "<d:literal>%@</d:literal>"
  244. "</d:like>"
  245. "</d:where>"
  246. "</d:basicsearch>"
  247. "</d:searchrequest>", userID, folder, fileName];
  248. [request setHTTPBody:[body dataUsingEncoding:NSUTF8StringEncoding]];
  249. [request setValue:@"text/xml" forHTTPHeaderField:@"Content-Type"];
  250. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication withUserSessionToken:token success:success failure:failure];
  251. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  252. [operation resume];
  253. }
  254. - (void)settingFavorite:(NSString * _Nonnull)path favorite:(BOOL)favorite onCommunication:(OCCommunication *)sharedOCCommunication withUserSessionToken:(NSString *)token success:(void(^)(NSHTTPURLResponse *, id, NSString *token))success failure:(void(^)(NSHTTPURLResponse *, id _Nullable responseObject, NSError *, NSString *token))failure {
  255. NSParameterAssert(success);
  256. _requestMethod = @"PROPPATCH";
  257. NSMutableURLRequest *request = [self requestWithMethod:_requestMethod path:path parameters:nil];
  258. NSString *body = [NSString stringWithFormat:@"<?xml version=\"1.0\"?><d:propertyupdate xmlns:d=\"DAV:\" xmlns:oc=\"http://owncloud.org/ns\"><d:set><d:prop><oc:favorite>%i</oc:favorite></d:prop></d:set></d:propertyupdate>", (favorite ? 1 : 0)];
  259. [request setHTTPBody:[body dataUsingEncoding:NSUTF8StringEncoding]];
  260. [request setValue:@"application/xml" forHTTPHeaderField:@"Content-Type"];
  261. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication withUserSessionToken:token success:success failure:failure];
  262. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  263. [operation resume];
  264. }
  265. - (void)listingFavorites:(NSString *)path folder:(NSString *)folder user:(NSString *)user userID:(NSString *)userID onCommunication:(OCCommunication *)sharedOCCommunication withUserSessionToken:(NSString *)token success:(void(^)(NSHTTPURLResponse *, id, NSString *token))success failure:(void(^)(NSHTTPURLResponse *, id _Nullable responseObject, NSError *, NSString *token))failure {
  266. NSString *body;
  267. NSParameterAssert(success);
  268. _requestMethod = @"REPORT";
  269. //REPORT remote.php/dav/files/user/path/to/folder
  270. path = [NSString stringWithFormat:@"%@/files/%@%@", path, userID, folder];
  271. NSMutableURLRequest *request = [self requestWithMethod:_requestMethod path:path parameters:nil];
  272. body = @"<?xml version=\"1.0\"?><oc:filter-files xmlns:d=\"DAV:\" xmlns:oc=\"http://owncloud.org/ns\" xmlns:nc=\"http://nextcloud.org/ns\"><oc:filter-rules><oc:favorite>1</oc:favorite></oc:filter-rules><d:prop>"; //<oc:id/></d:prop></oc:filter-files>";
  273. // OCFileDto
  274. body = [body stringByAppendingString:@"<d:resourcetype/><oc:fileid/><d:getetag/><d:getcontentlength/><oc:size/><d:getlastmodified/><oc:id/><oc:permissions/><oc:favorite/>"];
  275. body = [NSString stringWithFormat:@"%@</d:prop></oc:filter-files>", body];
  276. [request setHTTPBody:[body dataUsingEncoding:NSUTF8StringEncoding]];
  277. [request setValue:@"text/xml" forHTTPHeaderField:@"Content-Type"];
  278. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication withUserSessionToken:token success:success failure:failure];
  279. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  280. [operation resume];
  281. }
  282. - (NSURLSessionDownloadTask *)downloadWithSessionPath:(NSString *)remoteSource toPath:(NSString *)localDestination defaultPriority:(BOOL)defaultPriority onCommunication:(OCCommunication *)sharedOCCommunication progress:(void(^)(NSProgress *progress))downloadProgress success:(void(^)(NSURLResponse *response, NSURL *filePath))success failure:(void(^)(NSURLResponse *response, NSError *error))failure{
  283. NSMutableURLRequest *request = [self requestWithMethod:@"GET" path:remoteSource parameters:nil];
  284. //If is not nil is a redirection so we keep the original url server
  285. if (!self.originalUrlServer) {
  286. self.originalUrlServer = [request.URL absoluteString];
  287. }
  288. //We add the cookies of that URL
  289. request = [UtilsFramework getRequestWithCookiesByRequest:request andOriginalUrlServer:self.originalUrlServer];
  290. NSURL *localDestinationUrl = [NSURL fileURLWithPath:localDestination];
  291. NSURLSessionDownloadTask *downloadTask = [sharedOCCommunication.downloadSessionManager downloadTaskWithRequest:request progress:^(NSProgress * _Nonnull progress) {
  292. downloadProgress(progress);
  293. } destination:^NSURL * _Nonnull(NSURL * _Nonnull targetPath, NSURLResponse * _Nonnull response) {
  294. return localDestinationUrl;
  295. } completionHandler:^(NSURLResponse * _Nonnull response, NSURL * _Nullable filePath, NSError * _Nullable error) {
  296. if (error) {
  297. failure(response, error);
  298. } else {
  299. success(response,filePath);
  300. }
  301. }];
  302. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.downloadSessionManager];
  303. if (defaultPriority) {
  304. [downloadTask resume];
  305. }
  306. return downloadTask;
  307. }
  308. - (void)checkServer:(NSString *)path onCommunication:
  309. (OCCommunication *)sharedOCCommunication
  310. success:(void(^)(NSHTTPURLResponse *, id))success
  311. failure:(void(^)(NSHTTPURLResponse *, id _Nullable responseObject, NSError *))failure {
  312. _requestMethod = @"HEAD";
  313. NSMutableURLRequest *request = [self requestWithMethod:_requestMethod path:path parameters:nil];
  314. request.HTTPShouldHandleCookies = false;
  315. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  316. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  317. [operation resume];
  318. }
  319. - (void)makeCollection:(NSString *)path onCommunication:
  320. (OCCommunication *)sharedOCCommunication
  321. success:(void(^)(NSHTTPURLResponse *, id))success
  322. failure:(void(^)(NSHTTPURLResponse *, id _Nullable responseObject, NSError *))failure {
  323. _requestMethod = @"MKCOL";
  324. NSMutableURLRequest *request = [self requestWithMethod:_requestMethod path:path parameters:nil];
  325. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  326. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  327. [operation resume];
  328. }
  329. - (NSURLSessionUploadTask *)putWithSessionLocalPath:(NSString *)localSource atRemotePath:(NSString *)remoteDestination onCommunication:(OCCommunication *)sharedOCCommunication progress:(void(^)(NSProgress *progress))uploadProgress success:(void(^)(NSURLResponse *, NSString *))success failure:(void(^)(NSURLResponse *, id, NSError *))failure failureBeforeRequest:(void(^)(NSError *)) failureBeforeRequest {
  330. NSFileManager *fileManager = [NSFileManager defaultManager];
  331. if (localSource == nil || ![fileManager fileExistsAtPath:localSource]) {
  332. NSMutableDictionary* details = [NSMutableDictionary dictionary];
  333. [details setValue:@"You are trying upload a file that does not exist" forKey:NSLocalizedDescriptionKey];
  334. NSError *error = [NSError errorWithDomain:k_domain_error_code code:OCErrorFileToUploadDoesNotExist userInfo:details];
  335. failureBeforeRequest(error);
  336. return nil;
  337. } else {
  338. NSMutableURLRequest *request = [self requestWithMethod:@"PUT" path:remoteDestination parameters:nil];
  339. [request setTimeoutInterval:k_timeout_upload];
  340. [request setValue:[NSString stringWithFormat:@"%lld", [UtilsFramework getSizeInBytesByPath:localSource]] forHTTPHeaderField:@"Content-Length"];
  341. [request setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData];
  342. //If is not nil is a redirection so we keep the original url server
  343. if (!self.originalUrlServer) {
  344. self.originalUrlServer = [request.URL absoluteString];
  345. }
  346. if (sharedOCCommunication.isCookiesAvailable) {
  347. //We add the cookies of that URL
  348. request = [UtilsFramework getRequestWithCookiesByRequest:request andOriginalUrlServer:self.originalUrlServer];
  349. } else {
  350. [UtilsFramework deleteAllCookies];
  351. }
  352. NSURL *file = [NSURL fileURLWithPath:localSource];
  353. sharedOCCommunication.uploadSessionManager.responseSerializer = [AFHTTPResponseSerializer serializer];
  354. NSURLSessionUploadTask *uploadTask = [sharedOCCommunication.uploadSessionManager uploadTaskWithRequest:request fromFile:file progress:^(NSProgress * _Nonnull progress) {
  355. uploadProgress(progress);
  356. } completionHandler:^(NSURLResponse * _Nonnull response, id _Nullable responseObject, NSError * _Nullable error) {
  357. if (error) {
  358. failure(response, responseObject, error);
  359. } else {
  360. success(response,responseObject);
  361. }
  362. }];
  363. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.uploadSessionManager];
  364. [uploadTask resume];
  365. return uploadTask;
  366. }
  367. }
  368. - (void) requestUserNameOfServer:(NSString * _Nonnull) path byCookie:(NSString * _Nonnull) cookieString onCommunication:
  369. (OCCommunication * _Nonnull)sharedOCCommunication success:(void(^ _Nonnull)(NSHTTPURLResponse * _Nonnull, id _Nonnull))success
  370. failure:(void(^ _Nonnull)(NSHTTPURLResponse * _Nonnull, id _Nullable responseObject, NSError * _Nonnull))failure {
  371. NSString *apiUserUrl = nil;
  372. apiUserUrl = [NSString stringWithFormat:@"%@%@", path, k_api_user_url_json];
  373. NSLog(@"api user name call: %@", apiUserUrl);
  374. _requestMethod = @"GET";
  375. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path: apiUserUrl parameters: nil];
  376. [request setValue:@"application/xml" forHTTPHeaderField:@"Content-Type"];
  377. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  378. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  379. [operation resume];
  380. }
  381. - (void) getStatusOfTheServer:(NSString *)serverPath onCommunication:
  382. (OCCommunication *)sharedOCCommunication success:(void(^)(NSHTTPURLResponse *operation, id responseObject))success
  383. failure:(void(^)(NSHTTPURLResponse *operation, id _Nullable responseObject, NSError *error))failure {
  384. NSString *urlString = [NSString stringWithFormat:@"%@%@", serverPath, k_server_information_json];
  385. _requestMethod = @"GET";
  386. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path: urlString parameters: nil];
  387. request.HTTPShouldHandleCookies = false;
  388. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  389. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  390. [operation resume];
  391. }
  392. - (void)listSharedByServer:(NSString *)serverPath
  393. onCommunication:(OCCommunication *)sharedOCCommunication
  394. success:(void(^)(NSHTTPURLResponse *, id))success
  395. failure:(void(^)(NSHTTPURLResponse *, id _Nullable responseObject, NSError *))failure {
  396. NSParameterAssert(success);
  397. _requestMethod = @"GET";
  398. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil];
  399. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  400. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  401. [operation resume];
  402. }
  403. - (void)listSharedByServer:(NSString *)serverPath andPath:(NSString *) path
  404. onCommunication:(OCCommunication *)sharedOCCommunication
  405. success:(void(^)(NSHTTPURLResponse *, id))success
  406. failure:(void(^)(NSHTTPURLResponse *, id _Nullable responseObject, NSError *))failure {
  407. NSParameterAssert(success);
  408. NSString *postString = [NSString stringWithFormat: @"?path=%@&subfiles=true",path];
  409. serverPath = [serverPath stringByAppendingString:postString];
  410. _requestMethod = @"GET";
  411. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil];
  412. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  413. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  414. [operation resume];
  415. }
  416. - (void)shareByLinkFileOrFolderByServer:(NSString *)serverPath andPath:(NSString *) filePath andPassword:(NSString *)password
  417. onCommunication:(OCCommunication *)sharedOCCommunication
  418. success:(void(^)(NSHTTPURLResponse *, id))success
  419. failure:(void(^)(NSHTTPURLResponse *, id _Nullable responseObject, NSError *))failure {
  420. NSParameterAssert(success);
  421. _requestMethod = @"POST";
  422. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil];
  423. _postStringForShare = [NSString stringWithFormat: @"path=%@&shareType=3&password=%@",filePath,password];
  424. [request setHTTPBody:[_postStringForShare dataUsingEncoding:NSUTF8StringEncoding]];
  425. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  426. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  427. [operation resume];
  428. }
  429. - (void)shareByLinkFileOrFolderByServer:(NSString *)serverPath andPath:(NSString *) filePath
  430. onCommunication:(OCCommunication *)sharedOCCommunication
  431. success:(void(^)(NSHTTPURLResponse *, id))success
  432. failure:(void(^)(NSHTTPURLResponse *, id _Nullable responseObject, NSError *))failure {
  433. NSParameterAssert(success);
  434. _requestMethod = @"POST";
  435. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil];
  436. _postStringForShare = [NSString stringWithFormat: @"path=%@&shareType=3",filePath];
  437. [request setHTTPBody:[_postStringForShare dataUsingEncoding:NSUTF8StringEncoding]];
  438. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  439. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  440. [operation resume];
  441. }
  442. - (void)shareWith:(NSString *)userOrGroup shareeType:(NSInteger)shareeType inServer:(NSString *) serverPath andPath:(NSString *) filePath andPermissions:(NSInteger) permissions onCommunication:(OCCommunication *)sharedOCCommunication
  443. success:(void(^)(NSHTTPURLResponse *, id))success
  444. failure:(void(^)(NSHTTPURLResponse *, id _Nullable responseObject, NSError *))failure {
  445. NSParameterAssert(success);
  446. _requestMethod = @"POST";
  447. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil];
  448. self.postStringForShare = [NSString stringWithFormat: @"path=%@&shareType=%ld&shareWith=%@&permissions=%ld",filePath, (long)shareeType, userOrGroup, (long)permissions];
  449. [request setHTTPBody:[_postStringForShare dataUsingEncoding:NSUTF8StringEncoding]];
  450. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  451. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  452. [operation resume];
  453. }
  454. - (void)unShareFileOrFolderByServer:(NSString *)serverPath
  455. onCommunication:(OCCommunication *)sharedOCCommunication
  456. success:(void(^)(NSHTTPURLResponse *, id))success
  457. failure:(void(^)(NSHTTPURLResponse *, id _Nullable responseObject, NSError *))failure {
  458. NSParameterAssert(success);
  459. _requestMethod = @"DELETE";
  460. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil];
  461. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  462. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  463. [operation resume];
  464. }
  465. - (void)isShareFileOrFolderByServer:(NSString *)serverPath
  466. onCommunication:(OCCommunication *)sharedOCCommunication
  467. success:(void(^)(NSHTTPURLResponse *, id))success
  468. failure:(void(^)(NSHTTPURLResponse *, id _Nullable responseObject, NSError *))failure {
  469. NSParameterAssert(success);
  470. _requestMethod = @"GET";
  471. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil];
  472. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  473. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  474. [operation resume];
  475. }
  476. - (void) updateShareItem:(NSInteger)shareId ofServerPath:(NSString*)serverPath withPasswordProtect:(NSString*)password andExpirationTime:(NSString*)expirationTime andPermissions:(NSInteger)permissions
  477. onCommunication:(OCCommunication *)sharedOCCommunication
  478. success:(void(^)(NSHTTPURLResponse *, id response))success
  479. failure:(void(^)(NSHTTPURLResponse *, id _Nullable responseObject, NSError *error))failure{
  480. NSParameterAssert(success);
  481. _requestMethod = @"PUT";
  482. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil];
  483. if (password) {
  484. self.postStringForShare = [NSString stringWithFormat:@"password=%@",password];
  485. } else if (expirationTime) {
  486. self.postStringForShare = [NSString stringWithFormat:@"expireDate=%@",expirationTime];
  487. }else if (permissions > 0) {
  488. self.postStringForShare = [NSString stringWithFormat:@"permissions=%ld",(long)permissions];
  489. }
  490. [request setHTTPBody:[_postStringForShare dataUsingEncoding:NSUTF8StringEncoding]];
  491. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  492. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  493. [operation resume];
  494. }
  495. - (void) searchUsersAndGroupsWith:(NSString *)searchString forPage:(NSInteger)page with:(NSInteger)resultsPerPage ofServer:(NSString*)serverPath onCommunication:(OCCommunication *)sharedOCCommunication success:(void(^)(NSHTTPURLResponse *operation, id response))success
  496. failure:(void(^)(NSHTTPURLResponse *operation, id _Nullable responseObject, NSError *error))failure {
  497. NSParameterAssert(success);
  498. _requestMethod = @"GET";
  499. NSString *searchQuery = [NSString stringWithFormat: @"&search=%@",searchString];
  500. NSString *jsonQuery = [NSString stringWithFormat:@"?format=json"];
  501. NSString *queryType = [NSString stringWithFormat:@"&itemType=file"];
  502. NSString *pagination = [NSString stringWithFormat:@"&page=%ld&perPage=%ld", (long)page, (long)resultsPerPage];
  503. serverPath = [serverPath stringByAppendingString:jsonQuery];
  504. serverPath = [serverPath stringByAppendingString:queryType];
  505. serverPath = [serverPath stringByAppendingString:searchQuery];
  506. serverPath = [serverPath stringByAppendingString:pagination];
  507. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil];
  508. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  509. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  510. [operation resume];
  511. }
  512. - (void)getSharePermissionsFile:(NSString*)fileName onCommunication:(OCCommunication *)sharedOCCommunication
  513. success:(void(^)(NSHTTPURLResponse *, id))success
  514. failure:(void(^)(NSHTTPURLResponse *, id _Nullable responseObject, NSError *))failure {
  515. NSParameterAssert(success);
  516. _requestMethod = @"PROPFIND";
  517. NSMutableURLRequest *request = [self requestWithMethod:_requestMethod path:fileName parameters:nil];
  518. [request setHTTPBody:[@"<?xml version=\"1.0\" encoding=\"UTF-8\"?><a:propfind xmlns:a=\"DAV:\" xmlns:b=\"http://open-collaboration-services.org/ns\"><a:prop><b:share-permissions/></a:prop></a:propfind>" dataUsingEncoding:NSUTF8StringEncoding]];
  519. [request setValue:@"application/xml" forHTTPHeaderField:@"Content-Type"];
  520. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  521. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  522. [operation resume];
  523. }
  524. - (void) getCapabilitiesOfServer:(NSString*)serverPath onCommunication:(OCCommunication *)sharedOCCommunication success:(void(^)(NSHTTPURLResponse *operation, id response))success
  525. failure:(void(^)(NSHTTPURLResponse *operation, id _Nullable responseObject, NSError *error))failure{
  526. _requestMethod = @"GET";
  527. NSString *jsonQuery = [NSString stringWithFormat:@"?format=json"];
  528. serverPath = [serverPath stringByAppendingString:jsonQuery];
  529. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil];
  530. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  531. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  532. [operation resume];
  533. }
  534. #pragma mark - Remote thumbnails
  535. - (OCHTTPRequestOperation *) getRemoteThumbnailByServer:(NSString*)serverPath ofFilePath:(NSString*)filePath withWidth:(NSInteger)fileWidth andHeight:(NSInteger)fileHeight onCommunication:(OCCommunication *)sharedOCCommunication
  536. success:(void(^)(NSHTTPURLResponse *operation, id response))success
  537. failure:(void(^)(NSHTTPURLResponse *operation, id _Nullable responseObject, NSError *error))failure{
  538. _requestMethod = @"GET";
  539. NSString *query = [NSString stringWithFormat:@"/%i/%i/%@", (int)fileWidth, (int)fileHeight, filePath];
  540. serverPath = [serverPath stringByAppendingString:query];
  541. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil];
  542. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  543. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  544. return operation;
  545. }
  546. #pragma mark - Get Notification
  547. - (void)getNotificationServer:(NSString*)serverPath onCommunication:(OCCommunication *)sharedOCCommunication success:(void(^)(NSHTTPURLResponse *operation, id response))success
  548. failure:(void(^)(NSHTTPURLResponse *operation, id _Nullable responseObject, NSError *error))failure{
  549. _requestMethod = @"GET";
  550. NSString *jsonQuery = [NSString stringWithFormat:@"?format=json"];
  551. serverPath = [serverPath stringByAppendingString:jsonQuery];
  552. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil];
  553. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  554. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  555. [operation resume];
  556. }
  557. - (void)setNotificationServer:(NSString *)serverPath type:(NSString *)type onCommunication:(OCCommunication *)sharedOCCommunication success:(void(^)(NSHTTPURLResponse *, id))success failure:(void(^)(NSHTTPURLResponse *, id _Nullable responseObject, NSError *))failure {
  558. NSParameterAssert(success);
  559. _requestMethod = type;
  560. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil];
  561. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  562. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  563. [operation resume];
  564. }
  565. - (void)subscribingNextcloudServerPush:(NSString *)serverPath authorizationToken:(NSString *)authorizationToken pushTokenHash:(NSString *)pushTokenHash devicePublicKey:(NSString *)devicePublicKey proxyServerPath:(NSString *)proxyServerPath onCommunication:(OCCommunication *)sharedOCCommunication success:(void(^)(NSHTTPURLResponse *, id))success failure:(void(^)(NSHTTPURLResponse *, id _Nullable responseObject, NSError *))failure {
  566. NSParameterAssert(success);
  567. _requestMethod = @"POST";
  568. NSString *pushTokenHashParam = [NSString stringWithFormat:@"?pushTokenHash=%@",pushTokenHash];
  569. NSString *devicePublicKeyParam = [NSString stringWithFormat:@"&devicePublicKey=%@",devicePublicKey];
  570. NSString *proxyServerPathParam = [NSString stringWithFormat:@"&proxyServer=%@",proxyServerPath];
  571. serverPath = [serverPath stringByAppendingString:pushTokenHashParam];
  572. serverPath = [serverPath stringByAppendingString:devicePublicKeyParam];
  573. serverPath = [serverPath stringByAppendingString:proxyServerPathParam];
  574. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil];
  575. [request setValue:[NSString stringWithFormat:@"token %@", authorizationToken] forHTTPHeaderField:@"Authorization"];
  576. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  577. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  578. [operation resume];
  579. }
  580. - (void)subscribingPushProxy:(NSString *)serverPath authorizationToken:(NSString *)authorizationToken pushToken:(NSString *)pushToken deviceIdentifier:(NSString *)deviceIdentifier deviceIdentifierSignature:(NSString *)deviceIdentifierSignature userPublicKey:(NSString *)userPublicKey onCommunication:(OCCommunication *)sharedOCCommunication success:(void(^)(NSHTTPURLResponse *, id))success failure:(void(^)(NSHTTPURLResponse *, id _Nullable responseObject, NSError *))failure {
  581. NSParameterAssert(success);
  582. _requestMethod = @"POST";
  583. pushToken = [NSString stringWithFormat:@"?pushToken=%@",pushToken];
  584. deviceIdentifier = [NSString stringWithFormat:@"&deviceIdentifier=%@",deviceIdentifier];
  585. deviceIdentifierSignature = [NSString stringWithFormat:@"&deviceIdentifierSignature=%@",deviceIdentifierSignature];
  586. userPublicKey = [NSString stringWithFormat:@"&userPublicKey=%@",userPublicKey];
  587. serverPath = [serverPath stringByAppendingString:pushToken];
  588. serverPath = [serverPath stringByAppendingString:deviceIdentifier];
  589. serverPath = [serverPath stringByAppendingString:deviceIdentifierSignature];
  590. serverPath = [serverPath stringByAppendingString:userPublicKey];
  591. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil];
  592. [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
  593. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  594. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  595. [operation resume];
  596. }
  597. #pragma mark - Get Activity
  598. - (void) getActivityServer:(NSString*)serverPath onCommunication:(OCCommunication *)sharedOCCommunication success:(void(^)(NSHTTPURLResponse *operation, id response))success
  599. failure:(void(^)(NSHTTPURLResponse *operation, id _Nullable responseObject, NSError *error))failure{
  600. _requestMethod = @"GET";
  601. NSString *jsonQuery = [NSString stringWithFormat:@"?format=json"];
  602. //NSString *startParamater = [NSString stringWithFormat:@"&start=%@", start];
  603. serverPath = [serverPath stringByAppendingString:jsonQuery];
  604. //serverPath = [serverPath stringByAppendingString:startParamater];
  605. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil];
  606. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  607. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  608. [operation resume];
  609. }
  610. #pragma mark - Get External sites
  611. - (void) getExternalSitesServer:(NSString*)serverPath onCommunication:(OCCommunication *)sharedOCCommunication success:(void(^)(NSHTTPURLResponse *operation, id response))success
  612. failure:(void(^)(NSHTTPURLResponse *operation, id _Nullable responseObject, NSError *error))failure{
  613. _requestMethod = @"GET";
  614. NSString *jsonQuery = [NSString stringWithFormat:@"?format=json"];
  615. serverPath = [serverPath stringByAppendingString:jsonQuery];
  616. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil];
  617. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  618. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  619. [operation resume];
  620. }
  621. #pragma mark - Get User Profile
  622. - (void) getUserProfileServer:(NSString*)serverPath onCommunication:(OCCommunication *)sharedOCCommunication success:(void(^)(NSHTTPURLResponse *operation, id response))success
  623. failure:(void(^)(NSHTTPURLResponse *operation, id _Nullable responseObject, NSError *error))failure{
  624. _requestMethod = @"GET";
  625. NSString *jsonQuery = [NSString stringWithFormat:@"?format=json"];
  626. serverPath = [serverPath stringByAppendingString:jsonQuery];
  627. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil];
  628. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  629. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  630. [operation resume];
  631. }
  632. #pragma mark - End-to-End Encryption
  633. - (void)getEndToEndPublicKeys:(NSString*)serverPath onCommunication:(OCCommunication *)sharedOCCommunication success:(void(^)(NSHTTPURLResponse *operation, id response))success
  634. failure:(void(^)(NSHTTPURLResponse *operation, id _Nullable responseObject, NSError *error))failure{
  635. _requestMethod = @"GET";
  636. NSString *jsonQuery = [NSString stringWithFormat:@"?format=json"];
  637. serverPath = [serverPath stringByAppendingString:jsonQuery];
  638. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil];
  639. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  640. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  641. [operation resume];
  642. }
  643. - (void)getEndToEndPrivateKeyCipher:(NSString*)serverPath onCommunication:(OCCommunication *)sharedOCCommunication success:(void(^)(NSHTTPURLResponse *operation, id response))success
  644. failure:(void(^)(NSHTTPURLResponse *operation, id _Nullable responseObject, NSError *error))failure{
  645. _requestMethod = @"GET";
  646. NSString *jsonQuery = [NSString stringWithFormat:@"?format=json"];
  647. serverPath = [serverPath stringByAppendingString:jsonQuery];
  648. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil];
  649. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  650. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  651. [operation resume];
  652. }
  653. - (void)getEndToEndServerPublicKey:(NSString*)serverPath onCommunication:(OCCommunication *)sharedOCCommunication success:(void(^)(NSHTTPURLResponse *operation, id response))success
  654. failure:(void(^)(NSHTTPURLResponse *operation, id _Nullable responseObject, NSError *error))failure{
  655. _requestMethod = @"GET";
  656. NSString *jsonQuery = [NSString stringWithFormat:@"?format=json"];
  657. serverPath = [serverPath stringByAppendingString:jsonQuery];
  658. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil];
  659. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  660. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  661. [operation resume];
  662. }
  663. - (void)signEndToEndPublicKey:(NSString*)serverPath publicKey:(NSString *)publicKey onCommunication:(OCCommunication *)sharedOCCommunication success:(void(^)(NSHTTPURLResponse *operation, id response))success
  664. failure:(void(^)(NSHTTPURLResponse *operation, id _Nullable responseObject, NSError *error))failure{
  665. NSParameterAssert(success);
  666. _requestMethod = @"POST";
  667. publicKey = [NSString stringWithFormat:@"?csr=%@",publicKey];
  668. serverPath = [serverPath stringByAppendingString:publicKey];
  669. serverPath = [serverPath stringByAppendingString:@"&format=json"];
  670. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil];
  671. [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
  672. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  673. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  674. [operation resume];
  675. }
  676. - (void)storeEndToEndPrivateKeyCipher:(NSString*)serverPath privateKeyChiper:(NSString *)privateKeyChiper onCommunication:(OCCommunication *)sharedOCCommunication success:(void(^)(NSHTTPURLResponse *operation, id response))success
  677. failure:(void(^)(NSHTTPURLResponse *operation, id _Nullable responseObject, NSError *error))failure{
  678. NSParameterAssert(success);
  679. _requestMethod = @"POST";
  680. privateKeyChiper = [NSString stringWithFormat:@"?privateKey=%@",privateKeyChiper];
  681. serverPath = [serverPath stringByAppendingString:privateKeyChiper];
  682. serverPath = [serverPath stringByAppendingString:@"&format=json"];
  683. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil];
  684. [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
  685. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  686. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  687. [operation resume];
  688. }
  689. - (void)deleteEndToEndPublicKey:(NSString*)serverPath onCommunication:(OCCommunication *)sharedOCCommunication success:(void(^)(NSHTTPURLResponse *operation, id response))success
  690. failure:(void(^)(NSHTTPURLResponse *operation, id _Nullable responseObject, NSError *error))failure{
  691. _requestMethod = @"DELETE";
  692. NSString *jsonQuery = [NSString stringWithFormat:@"?format=json"];
  693. serverPath = [serverPath stringByAppendingString:jsonQuery];
  694. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil];
  695. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  696. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  697. [operation resume];
  698. }
  699. - (void)deleteEndToEndPrivateKey:(NSString*)serverPath onCommunication:(OCCommunication *)sharedOCCommunication success:(void(^)(NSHTTPURLResponse *operation, id response))success
  700. failure:(void(^)(NSHTTPURLResponse *operation, id _Nullable responseObject, NSError *error))failure{
  701. _requestMethod = @"DELETE";
  702. NSString *jsonQuery = [NSString stringWithFormat:@"?format=json"];
  703. serverPath = [serverPath stringByAppendingString:jsonQuery];
  704. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil];
  705. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  706. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  707. [operation resume];
  708. }
  709. - (void)markEndToEndFolderEncrypted:(NSString*)serverPath onCommunication:(OCCommunication *)sharedOCCommunication success:(void(^)(NSHTTPURLResponse *operation, id response))success
  710. failure:(void(^)(NSHTTPURLResponse *operation, id _Nullable responseObject, NSError *error))failure{
  711. _requestMethod = @"PUT";
  712. NSString *jsonQuery = [NSString stringWithFormat:@"?format=json"];
  713. serverPath = [serverPath stringByAppendingString:jsonQuery];
  714. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil];
  715. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  716. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  717. [operation resume];
  718. }
  719. - (void)deletemarkEndToEndFolderEncrypted:(NSString*)serverPath onCommunication:(OCCommunication *)sharedOCCommunication success:(void(^)(NSHTTPURLResponse *operation, id response))success
  720. failure:(void(^)(NSHTTPURLResponse *operation, id _Nullable responseObject, NSError *error))failure{
  721. _requestMethod = @"DELETE";
  722. NSString *jsonQuery = [NSString stringWithFormat:@"?format=json"];
  723. serverPath = [serverPath stringByAppendingString:jsonQuery];
  724. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil];
  725. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  726. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  727. [operation resume];
  728. }
  729. - (void)lockEndToEndFolderEncrypted:(NSString*)serverPath onCommunication:(OCCommunication *)sharedOCCommunication success:(void(^)(NSHTTPURLResponse *operation, id response))success
  730. failure:(void(^)(NSHTTPURLResponse *operation, id _Nullable responseObject, NSError *error))failure{
  731. _requestMethod = @"POST";
  732. NSString *jsonQuery = [NSString stringWithFormat:@"?format=json"];
  733. serverPath = [serverPath stringByAppendingString:jsonQuery];
  734. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil];
  735. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  736. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  737. [operation resume];
  738. }
  739. - (void)unlockEndToEndFolderEncrypted:(NSString*)serverPath onCommunication:(OCCommunication *)sharedOCCommunication success:(void(^)(NSHTTPURLResponse *operation, id response))success
  740. failure:(void(^)(NSHTTPURLResponse *operation, id _Nullable responseObject, NSError *error))failure{
  741. _requestMethod = @"DELETE";
  742. NSString *jsonQuery = [NSString stringWithFormat:@"?format=json"];
  743. serverPath = [serverPath stringByAppendingString:jsonQuery];
  744. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil];
  745. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  746. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  747. [operation resume];
  748. }
  749. - (void)storeEndToEndMetadata:(NSString*)serverPath encryptedMetadata:(NSString *)encryptedMetadata onCommunication:(OCCommunication *)sharedOCCommunication success:(void(^)(NSHTTPURLResponse *operation, id response))success
  750. failure:(void(^)(NSHTTPURLResponse *operation, id _Nullable responseObject, NSError *error))failure{
  751. NSParameterAssert(success);
  752. _requestMethod = @"POST";
  753. encryptedMetadata = [NSString stringWithFormat:@"?metaData=%@", encryptedMetadata];
  754. serverPath = [serverPath stringByAppendingString:encryptedMetadata];
  755. serverPath = [serverPath stringByAppendingString:@"&format=json"];
  756. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil];
  757. [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
  758. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  759. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  760. [operation resume];
  761. }
  762. - (void)getEndToEndMetadata:(NSString*)serverPath onCommunication:(OCCommunication *)sharedOCCommunication success:(void(^)(NSHTTPURLResponse *operation, id response))success
  763. failure:(void(^)(NSHTTPURLResponse *operation, id _Nullable responseObject, NSError *error))failure{
  764. NSParameterAssert(success);
  765. _requestMethod = @"GET";
  766. NSString *jsonQuery = [NSString stringWithFormat:@"?format=json"];
  767. serverPath = [serverPath stringByAppendingString:jsonQuery];
  768. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil];
  769. [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
  770. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  771. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  772. [operation resume];
  773. }
  774. - (void)updateEndToEndMetadata:(NSString*)serverPath encryptedMetadata:(NSString *)encryptedMetadata onCommunication:(OCCommunication *)sharedOCCommunication success:(void(^)(NSHTTPURLResponse *operation, id response))success
  775. failure:(void(^)(NSHTTPURLResponse *operation, id _Nullable responseObject, NSError *error))failure{
  776. NSParameterAssert(success);
  777. _requestMethod = @"PUT";
  778. encryptedMetadata = [NSString stringWithFormat:@"?metaData=%@", encryptedMetadata];
  779. serverPath = [serverPath stringByAppendingString:encryptedMetadata];
  780. serverPath = [serverPath stringByAppendingString:@"&format=json"];
  781. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil];
  782. [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
  783. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  784. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  785. [operation resume];
  786. }
  787. - (void)deleteEndToEndMetadata:(NSString*)serverPath onCommunication:(OCCommunication *)sharedOCCommunication success:(void(^)(NSHTTPURLResponse *operation, id response))success
  788. failure:(void(^)(NSHTTPURLResponse *operation, id _Nullable responseObject, NSError *error))failure{
  789. NSParameterAssert(success);
  790. _requestMethod = @"DELETE";
  791. NSString *jsonQuery = [NSString stringWithFormat:@"?format=json"];
  792. serverPath = [serverPath stringByAppendingString:jsonQuery];
  793. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil];
  794. [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
  795. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  796. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  797. [operation resume];
  798. }
  799. #pragma mark - Manage Redirections
  800. - (void) setRedirectionBlockOnDatataskWithOCCommunication: (OCCommunication *) sharedOCCommunication andSessionManager:(AFURLSessionManager *) sessionManager{
  801. [sessionManager setTaskWillPerformHTTPRedirectionBlock:^NSURLRequest * _Nonnull(NSURLSession * _Nonnull session, NSURLSessionTask * _Nonnull task, NSURLResponse * _Nonnull response, NSURLRequest * _Nonnull request) {
  802. if (response == nil) {
  803. // needed to handle fake redirects to canonical addresses, as explained in https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/URLLoadingSystem/Articles/RequestChanges.html
  804. return request;
  805. }
  806. NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *) response;
  807. NSDictionary *dict = [httpResponse allHeaderFields];
  808. //Server path of redirected server
  809. NSString *responseURLString = [dict objectForKey:@"Location"];
  810. if (responseURLString) {
  811. if ([UtilsFramework isURLWithSamlFragment:responseURLString] || httpResponse.statusCode == k_redirected_code_1) {
  812. //We set the redirectedServer in case SAML or is a permanent redirection
  813. self.redirectedServer = responseURLString;
  814. if ([UtilsFramework isURLWithSamlFragment:responseURLString]) {
  815. // if SAML request, we don't want to follow it; WebView takes care, not here -> nil to NO FOLLOW
  816. return nil;
  817. }
  818. }
  819. NSMutableURLRequest *requestRedirect = [request mutableCopy];
  820. [requestRedirect setURL: [NSURL URLWithString:responseURLString]];
  821. requestRedirect = [sharedOCCommunication getRequestWithCredentials:requestRedirect];
  822. requestRedirect.HTTPMethod = _requestMethod;
  823. if (_postStringForShare) {
  824. //It is a request to share a file by link
  825. requestRedirect = [self sharedRequestWithMethod:_requestMethod path:responseURLString parameters:nil];
  826. [requestRedirect setHTTPBody:[_postStringForShare dataUsingEncoding:NSUTF8StringEncoding]];
  827. }
  828. return requestRedirect;
  829. } else {
  830. // no location to redirect -> nil to NO FOLLOW
  831. return nil;
  832. }
  833. }];
  834. }
  835. @end