OCWebDAVClient.m 57 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024
  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 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 = @"<?xml version=\"1.0\"?><d:searchrequest xmlns:d=\"DAV:\" xmlns:oc=\"http://owncloud.org/ns\"><d:basicsearch><d:select><d:prop>";
  215. // OCFileDto
  216. body = [body stringByAppendingString:@"<d:resourcetype/><oc:fileid/><d:getcontenttype/><d:getetag/><oc:size/><d:getcontentlength/><d:getlastmodified/><oc:id/><oc:permissions/><oc:favorite/>"];
  217. if (dateLastModified.length > 0) {
  218. body = [NSString stringWithFormat:@"%@</d:prop></d:select><d:from><d:scope><d:href>/files/%@%@</d:href><d:depth>infinity</d:depth></d:scope></d:from><d:where><d:like><d:prop><d:displayname/></d:prop><d:literal>%@</d:literal></d:like></d:where><d:orderby/></d:basicsearch></d:searchrequest>", body, userID, folder, fileName];
  219. } else {
  220. body = [NSString stringWithFormat:@"%@</d:prop></d:select><d:from><d:scope><d:href>/files/%@%@</d:href><d:depth>infinity</d:depth></d:scope></d:from><d:where><d:like><d:prop><d:displayname/></d:prop><d:literal>%@</d:literal></d:like></d:where><d:orderby/></d:basicsearch></d:searchrequest>", body, userID, folder, fileName];
  221. }
  222. [request setHTTPBody:[body dataUsingEncoding:NSUTF8StringEncoding]];
  223. [request setValue:@"text/xml" forHTTPHeaderField:@"Content-Type"];
  224. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication withUserSessionToken:token success:success failure:failure];
  225. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  226. [operation resume];
  227. }
  228. - (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 {
  229. NSParameterAssert(success);
  230. _requestMethod = @"PROPPATCH";
  231. NSMutableURLRequest *request = [self requestWithMethod:_requestMethod path:path parameters:nil];
  232. 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)];
  233. [request setHTTPBody:[body dataUsingEncoding:NSUTF8StringEncoding]];
  234. [request setValue:@"application/xml" forHTTPHeaderField:@"Content-Type"];
  235. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication withUserSessionToken:token success:success failure:failure];
  236. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  237. [operation resume];
  238. }
  239. - (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 {
  240. NSString *body;
  241. NSParameterAssert(success);
  242. _requestMethod = @"REPORT";
  243. //REPORT remote.php/dav/files/user/path/to/folder
  244. path = [NSString stringWithFormat:@"%@/files/%@%@", path, userID, folder];
  245. NSMutableURLRequest *request = [self requestWithMethod:_requestMethod path:path parameters:nil];
  246. 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>";
  247. // OCFileDto
  248. body = [body stringByAppendingString:@"<d:resourcetype/><oc:fileid/><d:getetag/><d:getcontentlength/><oc:size/><d:getlastmodified/><oc:id/><oc:permissions/><oc:favorite/>"];
  249. body = [NSString stringWithFormat:@"%@</d:prop></oc:filter-files>", body];
  250. [request setHTTPBody:[body dataUsingEncoding:NSUTF8StringEncoding]];
  251. [request setValue:@"text/xml" forHTTPHeaderField:@"Content-Type"];
  252. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication withUserSessionToken:token success:success failure:failure];
  253. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  254. [operation resume];
  255. }
  256. - (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{
  257. NSMutableURLRequest *request = [self requestWithMethod:@"GET" path:remoteSource parameters:nil];
  258. //If is not nil is a redirection so we keep the original url server
  259. if (!self.originalUrlServer) {
  260. self.originalUrlServer = [request.URL absoluteString];
  261. }
  262. //We add the cookies of that URL
  263. request = [UtilsFramework getRequestWithCookiesByRequest:request andOriginalUrlServer:self.originalUrlServer];
  264. NSURL *localDestinationUrl = [NSURL fileURLWithPath:localDestination];
  265. NSURLSessionDownloadTask *downloadTask = [sharedOCCommunication.downloadSessionManager downloadTaskWithRequest:request progress:^(NSProgress * _Nonnull progress) {
  266. downloadProgress(progress);
  267. } destination:^NSURL * _Nonnull(NSURL * _Nonnull targetPath, NSURLResponse * _Nonnull response) {
  268. return localDestinationUrl;
  269. } completionHandler:^(NSURLResponse * _Nonnull response, NSURL * _Nullable filePath, NSError * _Nullable error) {
  270. if (error) {
  271. failure(response, error);
  272. } else {
  273. success(response,filePath);
  274. }
  275. }];
  276. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.downloadSessionManager];
  277. if (defaultPriority) {
  278. [downloadTask resume];
  279. }
  280. return downloadTask;
  281. }
  282. - (void)checkServer:(NSString *)path onCommunication:
  283. (OCCommunication *)sharedOCCommunication
  284. success:(void(^)(NSHTTPURLResponse *, id))success
  285. failure:(void(^)(NSHTTPURLResponse *, id _Nullable responseObject, NSError *))failure {
  286. _requestMethod = @"HEAD";
  287. NSMutableURLRequest *request = [self requestWithMethod:_requestMethod path:path parameters:nil];
  288. request.HTTPShouldHandleCookies = false;
  289. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  290. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  291. [operation resume];
  292. }
  293. - (void)makeCollection:(NSString *)path onCommunication:
  294. (OCCommunication *)sharedOCCommunication
  295. success:(void(^)(NSHTTPURLResponse *, id))success
  296. failure:(void(^)(NSHTTPURLResponse *, id _Nullable responseObject, NSError *))failure {
  297. _requestMethod = @"MKCOL";
  298. NSMutableURLRequest *request = [self requestWithMethod:_requestMethod path:path parameters:nil];
  299. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  300. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  301. [operation resume];
  302. }
  303. - (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 {
  304. NSFileManager *fileManager = [NSFileManager defaultManager];
  305. if (localSource == nil || ![fileManager fileExistsAtPath:localSource]) {
  306. NSMutableDictionary* details = [NSMutableDictionary dictionary];
  307. [details setValue:@"You are trying upload a file that does not exist" forKey:NSLocalizedDescriptionKey];
  308. NSError *error = [NSError errorWithDomain:k_domain_error_code code:OCErrorFileToUploadDoesNotExist userInfo:details];
  309. failureBeforeRequest(error);
  310. return nil;
  311. } else {
  312. NSMutableURLRequest *request = [self requestWithMethod:@"PUT" path:remoteDestination parameters:nil];
  313. [request setTimeoutInterval:k_timeout_upload];
  314. [request setValue:[NSString stringWithFormat:@"%lld", [UtilsFramework getSizeInBytesByPath:localSource]] forHTTPHeaderField:@"Content-Length"];
  315. [request setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData];
  316. //If is not nil is a redirection so we keep the original url server
  317. if (!self.originalUrlServer) {
  318. self.originalUrlServer = [request.URL absoluteString];
  319. }
  320. if (sharedOCCommunication.isCookiesAvailable) {
  321. //We add the cookies of that URL
  322. request = [UtilsFramework getRequestWithCookiesByRequest:request andOriginalUrlServer:self.originalUrlServer];
  323. } else {
  324. [UtilsFramework deleteAllCookies];
  325. }
  326. NSURL *file = [NSURL fileURLWithPath:localSource];
  327. sharedOCCommunication.uploadSessionManager.responseSerializer = [AFHTTPResponseSerializer serializer];
  328. NSURLSessionUploadTask *uploadTask = [sharedOCCommunication.uploadSessionManager uploadTaskWithRequest:request fromFile:file progress:^(NSProgress * _Nonnull progress) {
  329. uploadProgress(progress);
  330. } completionHandler:^(NSURLResponse * _Nonnull response, id _Nullable responseObject, NSError * _Nullable error) {
  331. if (error) {
  332. failure(response, responseObject, error);
  333. } else {
  334. success(response,responseObject);
  335. }
  336. }];
  337. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.uploadSessionManager];
  338. [uploadTask resume];
  339. return uploadTask;
  340. }
  341. }
  342. - (void) requestUserNameOfServer:(NSString * _Nonnull) path byCookie:(NSString * _Nonnull) cookieString onCommunication:
  343. (OCCommunication * _Nonnull)sharedOCCommunication success:(void(^ _Nonnull)(NSHTTPURLResponse * _Nonnull, id _Nonnull))success
  344. failure:(void(^ _Nonnull)(NSHTTPURLResponse * _Nonnull, id _Nullable responseObject, NSError * _Nonnull))failure {
  345. NSString *apiUserUrl = nil;
  346. apiUserUrl = [NSString stringWithFormat:@"%@%@", path, k_api_user_url_json];
  347. NSLog(@"api user name call: %@", apiUserUrl);
  348. _requestMethod = @"GET";
  349. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path: apiUserUrl parameters: nil];
  350. [request setValue:@"application/xml" forHTTPHeaderField:@"Content-Type"];
  351. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  352. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  353. [operation resume];
  354. }
  355. - (void) getStatusOfTheServer:(NSString *)serverPath onCommunication:
  356. (OCCommunication *)sharedOCCommunication success:(void(^)(NSHTTPURLResponse *operation, id responseObject))success
  357. failure:(void(^)(NSHTTPURLResponse *operation, id _Nullable responseObject, NSError *error))failure {
  358. NSString *urlString = [NSString stringWithFormat:@"%@%@", serverPath, k_server_information_json];
  359. _requestMethod = @"GET";
  360. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path: urlString parameters: nil];
  361. request.HTTPShouldHandleCookies = false;
  362. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  363. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  364. [operation resume];
  365. }
  366. - (void)listSharedByServer:(NSString *)serverPath
  367. onCommunication:(OCCommunication *)sharedOCCommunication
  368. success:(void(^)(NSHTTPURLResponse *, id))success
  369. failure:(void(^)(NSHTTPURLResponse *, id _Nullable responseObject, NSError *))failure {
  370. NSParameterAssert(success);
  371. _requestMethod = @"GET";
  372. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil];
  373. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  374. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  375. [operation resume];
  376. }
  377. - (void)listSharedByServer:(NSString *)serverPath andPath:(NSString *) path
  378. onCommunication:(OCCommunication *)sharedOCCommunication
  379. success:(void(^)(NSHTTPURLResponse *, id))success
  380. failure:(void(^)(NSHTTPURLResponse *, id _Nullable responseObject, NSError *))failure {
  381. NSParameterAssert(success);
  382. NSString *postString = [NSString stringWithFormat: @"?path=%@&subfiles=true",path];
  383. serverPath = [serverPath stringByAppendingString:postString];
  384. _requestMethod = @"GET";
  385. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil];
  386. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  387. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  388. [operation resume];
  389. }
  390. - (void)shareByLinkFileOrFolderByServer:(NSString *)serverPath andPath:(NSString *) filePath andPassword:(NSString *)password
  391. onCommunication:(OCCommunication *)sharedOCCommunication
  392. success:(void(^)(NSHTTPURLResponse *, id))success
  393. failure:(void(^)(NSHTTPURLResponse *, id _Nullable responseObject, NSError *))failure {
  394. NSParameterAssert(success);
  395. _requestMethod = @"POST";
  396. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil];
  397. _postStringForShare = [NSString stringWithFormat: @"path=%@&shareType=3&password=%@",filePath,password];
  398. [request setHTTPBody:[_postStringForShare dataUsingEncoding:NSUTF8StringEncoding]];
  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)shareByLinkFileOrFolderByServer:(NSString *)serverPath andPath:(NSString *) filePath
  404. onCommunication:(OCCommunication *)sharedOCCommunication
  405. success:(void(^)(NSHTTPURLResponse *, id))success
  406. failure:(void(^)(NSHTTPURLResponse *, id _Nullable responseObject, NSError *))failure {
  407. NSParameterAssert(success);
  408. _requestMethod = @"POST";
  409. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil];
  410. _postStringForShare = [NSString stringWithFormat: @"path=%@&shareType=3",filePath];
  411. [request setHTTPBody:[_postStringForShare dataUsingEncoding:NSUTF8StringEncoding]];
  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)shareWith:(NSString *)userOrGroup shareeType:(NSInteger)shareeType inServer:(NSString *) serverPath andPath:(NSString *) filePath andPermissions:(NSInteger) permissions onCommunication:(OCCommunication *)sharedOCCommunication
  417. success:(void(^)(NSHTTPURLResponse *, id))success
  418. failure:(void(^)(NSHTTPURLResponse *, id _Nullable responseObject, NSError *))failure {
  419. NSParameterAssert(success);
  420. _requestMethod = @"POST";
  421. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil];
  422. self.postStringForShare = [NSString stringWithFormat: @"path=%@&shareType=%ld&shareWith=%@&permissions=%ld",filePath, (long)shareeType, userOrGroup, (long)permissions];
  423. [request setHTTPBody:[_postStringForShare dataUsingEncoding:NSUTF8StringEncoding]];
  424. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  425. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  426. [operation resume];
  427. }
  428. - (void)unShareFileOrFolderByServer:(NSString *)serverPath
  429. onCommunication:(OCCommunication *)sharedOCCommunication
  430. success:(void(^)(NSHTTPURLResponse *, id))success
  431. failure:(void(^)(NSHTTPURLResponse *, id _Nullable responseObject, NSError *))failure {
  432. NSParameterAssert(success);
  433. _requestMethod = @"DELETE";
  434. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil];
  435. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  436. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  437. [operation resume];
  438. }
  439. - (void)isShareFileOrFolderByServer:(NSString *)serverPath
  440. onCommunication:(OCCommunication *)sharedOCCommunication
  441. success:(void(^)(NSHTTPURLResponse *, id))success
  442. failure:(void(^)(NSHTTPURLResponse *, id _Nullable responseObject, NSError *))failure {
  443. NSParameterAssert(success);
  444. _requestMethod = @"GET";
  445. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil];
  446. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  447. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  448. [operation resume];
  449. }
  450. - (void) updateShareItem:(NSInteger)shareId ofServerPath:(NSString*)serverPath withPasswordProtect:(NSString*)password andExpirationTime:(NSString*)expirationTime andPermissions:(NSInteger)permissions
  451. onCommunication:(OCCommunication *)sharedOCCommunication
  452. success:(void(^)(NSHTTPURLResponse *, id response))success
  453. failure:(void(^)(NSHTTPURLResponse *, id _Nullable responseObject, NSError *error))failure{
  454. NSParameterAssert(success);
  455. _requestMethod = @"PUT";
  456. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil];
  457. if (password) {
  458. self.postStringForShare = [NSString stringWithFormat:@"password=%@",password];
  459. } else if (expirationTime) {
  460. self.postStringForShare = [NSString stringWithFormat:@"expireDate=%@",expirationTime];
  461. }else if (permissions > 0) {
  462. self.postStringForShare = [NSString stringWithFormat:@"permissions=%ld",(long)permissions];
  463. }
  464. [request setHTTPBody:[_postStringForShare dataUsingEncoding:NSUTF8StringEncoding]];
  465. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  466. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  467. [operation resume];
  468. }
  469. - (void) searchUsersAndGroupsWith:(NSString *)searchString forPage:(NSInteger)page with:(NSInteger)resultsPerPage ofServer:(NSString*)serverPath onCommunication:(OCCommunication *)sharedOCCommunication success:(void(^)(NSHTTPURLResponse *operation, id response))success
  470. failure:(void(^)(NSHTTPURLResponse *operation, id _Nullable responseObject, NSError *error))failure {
  471. NSParameterAssert(success);
  472. _requestMethod = @"GET";
  473. NSString *searchQuery = [NSString stringWithFormat: @"&search=%@",searchString];
  474. NSString *jsonQuery = [NSString stringWithFormat:@"?format=json"];
  475. NSString *queryType = [NSString stringWithFormat:@"&itemType=file"];
  476. NSString *pagination = [NSString stringWithFormat:@"&page=%ld&perPage=%ld", (long)page, (long)resultsPerPage];
  477. serverPath = [serverPath stringByAppendingString:jsonQuery];
  478. serverPath = [serverPath stringByAppendingString:queryType];
  479. serverPath = [serverPath stringByAppendingString:searchQuery];
  480. serverPath = [serverPath stringByAppendingString:pagination];
  481. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil];
  482. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  483. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  484. [operation resume];
  485. }
  486. - (void)getSharePermissionsFile:(NSString*)fileName onCommunication:(OCCommunication *)sharedOCCommunication
  487. success:(void(^)(NSHTTPURLResponse *, id))success
  488. failure:(void(^)(NSHTTPURLResponse *, id _Nullable responseObject, NSError *))failure {
  489. NSParameterAssert(success);
  490. _requestMethod = @"PROPFIND";
  491. NSMutableURLRequest *request = [self requestWithMethod:_requestMethod path:fileName parameters:nil];
  492. [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]];
  493. [request setValue:@"application/xml" forHTTPHeaderField:@"Content-Type"];
  494. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  495. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  496. [operation resume];
  497. }
  498. - (void) getCapabilitiesOfServer:(NSString*)serverPath onCommunication:(OCCommunication *)sharedOCCommunication success:(void(^)(NSHTTPURLResponse *operation, id response))success
  499. failure:(void(^)(NSHTTPURLResponse *operation, id _Nullable responseObject, NSError *error))failure{
  500. _requestMethod = @"GET";
  501. NSString *jsonQuery = [NSString stringWithFormat:@"?format=json"];
  502. serverPath = [serverPath stringByAppendingString:jsonQuery];
  503. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil];
  504. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  505. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  506. [operation resume];
  507. }
  508. #pragma mark - Remote thumbnails
  509. - (OCHTTPRequestOperation *) getRemoteThumbnailByServer:(NSString*)serverPath ofFilePath:(NSString*)filePath withWidth:(NSInteger)fileWidth andHeight:(NSInteger)fileHeight onCommunication:(OCCommunication *)sharedOCCommunication
  510. success:(void(^)(NSHTTPURLResponse *operation, id response))success
  511. failure:(void(^)(NSHTTPURLResponse *operation, id _Nullable responseObject, NSError *error))failure{
  512. _requestMethod = @"GET";
  513. NSString *query = [NSString stringWithFormat:@"/%i/%i/%@", (int)fileWidth, (int)fileHeight, filePath];
  514. serverPath = [serverPath stringByAppendingString:query];
  515. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil];
  516. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  517. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  518. return operation;
  519. }
  520. #pragma mark - Get Notification
  521. - (void)getNotificationServer:(NSString*)serverPath onCommunication:(OCCommunication *)sharedOCCommunication success:(void(^)(NSHTTPURLResponse *operation, id response))success
  522. failure:(void(^)(NSHTTPURLResponse *operation, id _Nullable responseObject, NSError *error))failure{
  523. _requestMethod = @"GET";
  524. NSString *jsonQuery = [NSString stringWithFormat:@"?format=json"];
  525. serverPath = [serverPath stringByAppendingString:jsonQuery];
  526. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil];
  527. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  528. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  529. [operation resume];
  530. }
  531. - (void)setNotificationServer:(NSString *)serverPath type:(NSString *)type onCommunication:(OCCommunication *)sharedOCCommunication success:(void(^)(NSHTTPURLResponse *, id))success failure:(void(^)(NSHTTPURLResponse *, id _Nullable responseObject, NSError *))failure {
  532. NSParameterAssert(success);
  533. _requestMethod = type;
  534. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil];
  535. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  536. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  537. [operation resume];
  538. }
  539. - (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 {
  540. NSParameterAssert(success);
  541. _requestMethod = @"POST";
  542. NSString *pushTokenHashParam = [NSString stringWithFormat:@"?pushTokenHash=%@",pushTokenHash];
  543. NSString *devicePublicKeyParam = [NSString stringWithFormat:@"&devicePublicKey=%@",devicePublicKey];
  544. NSString *proxyServerPathParam = [NSString stringWithFormat:@"&proxyServer=%@",proxyServerPath];
  545. serverPath = [serverPath stringByAppendingString:pushTokenHashParam];
  546. serverPath = [serverPath stringByAppendingString:devicePublicKeyParam];
  547. serverPath = [serverPath stringByAppendingString:proxyServerPathParam];
  548. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil];
  549. [request setValue:[NSString stringWithFormat:@"token %@", authorizationToken] forHTTPHeaderField:@"Authorization"];
  550. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  551. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  552. [operation resume];
  553. }
  554. - (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 {
  555. NSParameterAssert(success);
  556. _requestMethod = @"POST";
  557. pushToken = [NSString stringWithFormat:@"?pushToken=%@",pushToken];
  558. deviceIdentifier = [NSString stringWithFormat:@"&deviceIdentifier=%@",deviceIdentifier];
  559. deviceIdentifierSignature = [NSString stringWithFormat:@"&deviceIdentifierSignature=%@",deviceIdentifierSignature];
  560. userPublicKey = [NSString stringWithFormat:@"&userPublicKey=%@",userPublicKey];
  561. serverPath = [serverPath stringByAppendingString:pushToken];
  562. serverPath = [serverPath stringByAppendingString:deviceIdentifier];
  563. serverPath = [serverPath stringByAppendingString:deviceIdentifierSignature];
  564. serverPath = [serverPath stringByAppendingString:userPublicKey];
  565. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil];
  566. [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
  567. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  568. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  569. [operation resume];
  570. }
  571. #pragma mark - Get Activity
  572. - (void) getActivityServer:(NSString*)serverPath onCommunication:(OCCommunication *)sharedOCCommunication success:(void(^)(NSHTTPURLResponse *operation, id response))success
  573. failure:(void(^)(NSHTTPURLResponse *operation, id _Nullable responseObject, NSError *error))failure{
  574. _requestMethod = @"GET";
  575. NSString *jsonQuery = [NSString stringWithFormat:@"?format=json"];
  576. //NSString *startParamater = [NSString stringWithFormat:@"&start=%@", start];
  577. serverPath = [serverPath stringByAppendingString:jsonQuery];
  578. //serverPath = [serverPath stringByAppendingString:startParamater];
  579. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil];
  580. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  581. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  582. [operation resume];
  583. }
  584. #pragma mark - Get External sites
  585. - (void) getExternalSitesServer:(NSString*)serverPath onCommunication:(OCCommunication *)sharedOCCommunication success:(void(^)(NSHTTPURLResponse *operation, id response))success
  586. failure:(void(^)(NSHTTPURLResponse *operation, id _Nullable responseObject, NSError *error))failure{
  587. _requestMethod = @"GET";
  588. NSString *jsonQuery = [NSString stringWithFormat:@"?format=json"];
  589. serverPath = [serverPath stringByAppendingString:jsonQuery];
  590. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil];
  591. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  592. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  593. [operation resume];
  594. }
  595. #pragma mark - Get User Profile
  596. - (void) getUserProfileServer:(NSString*)serverPath onCommunication:(OCCommunication *)sharedOCCommunication success:(void(^)(NSHTTPURLResponse *operation, id response))success
  597. failure:(void(^)(NSHTTPURLResponse *operation, id _Nullable responseObject, NSError *error))failure{
  598. _requestMethod = @"GET";
  599. NSString *jsonQuery = [NSString stringWithFormat:@"?format=json"];
  600. serverPath = [serverPath stringByAppendingString:jsonQuery];
  601. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil];
  602. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  603. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  604. [operation resume];
  605. }
  606. #pragma mark - End-to-End Encryption
  607. // curl -X GET http://<user>:<password>@nextcloud/ocs/v2.php/apps/end_to_end_encryption/api/v1/public-key -H "OCS-APIRequest:true"
  608. - (void)getEndToEndPublicKeys:(NSString*)serverPath onCommunication:(OCCommunication *)sharedOCCommunication success:(void(^)(NSHTTPURLResponse *operation, id response))success
  609. failure:(void(^)(NSHTTPURLResponse *operation, id _Nullable responseObject, NSError *error))failure{
  610. _requestMethod = @"GET";
  611. NSString *jsonQuery = [NSString stringWithFormat:@"?format=json"];
  612. serverPath = [serverPath stringByAppendingString:jsonQuery];
  613. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil];
  614. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  615. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  616. [operation resume];
  617. }
  618. // curl -X GET http://<user>:<password>@nextcloud/ocs/v2.php/apps/end_to_end_encryption/api/v1/private-key -H "OCS-APIRequest:true"
  619. - (void)getEndToEndPrivateKey:(NSString*)serverPath onCommunication:(OCCommunication *)sharedOCCommunication success:(void(^)(NSHTTPURLResponse *operation, id response))success
  620. failure:(void(^)(NSHTTPURLResponse *operation, id _Nullable responseObject, NSError *error))failure{
  621. _requestMethod = @"GET";
  622. NSString *jsonQuery = [NSString stringWithFormat:@"?format=json"];
  623. serverPath = [serverPath stringByAppendingString:jsonQuery];
  624. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil];
  625. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  626. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  627. [operation resume];
  628. }
  629. //curl -X GET http://<user>:<password>@nextcloud/ocs/v2.php/apps/end_to_end_encryption/api/v1/server-key -H "OCS-APIRequest:true"
  630. - (void)getEndToEndServerPublicKey:(NSString*)serverPath onCommunication:(OCCommunication *)sharedOCCommunication success:(void(^)(NSHTTPURLResponse *operation, id response))success
  631. failure:(void(^)(NSHTTPURLResponse *operation, id _Nullable responseObject, NSError *error))failure{
  632. _requestMethod = @"GET";
  633. NSString *jsonQuery = [NSString stringWithFormat:@"?format=json"];
  634. serverPath = [serverPath stringByAppendingString:jsonQuery];
  635. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil];
  636. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  637. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  638. [operation resume];
  639. }
  640. //curl -X POST http://<user>:<password>@nextcloud/ocs/v2.php/apps/end_to_end_encryption/api/v1/public-key -H "OCS-APIRequest:true" -d csr="<urlencoded-csr>"
  641. - (void)signEndToEndPublicKey:(NSString*)serverPath publicKey:(NSString *)publicKey onCommunication:(OCCommunication *)sharedOCCommunication success:(void(^)(NSHTTPURLResponse *operation, id response))success
  642. failure:(void(^)(NSHTTPURLResponse *operation, id _Nullable responseObject, NSError *error))failure{
  643. NSParameterAssert(success);
  644. _requestMethod = @"POST";
  645. publicKey = [NSString stringWithFormat:@"?csr=%@",publicKey];
  646. serverPath = [serverPath stringByAppendingString:publicKey];
  647. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil];
  648. [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
  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. // curl -X POST http://<user>:<password>@nextcloud/ocs/v2.php/apps/end_to_end_encryption/api/v1/private-key -d privateKey="<urlencoded-private-key>" -H "OCS-APIRequest:true"
  654. - (void)storeEndToEndPrivateKey:(NSString*)serverPath privateKey:(NSString *)privateKey onCommunication:(OCCommunication *)sharedOCCommunication success:(void(^)(NSHTTPURLResponse *operation, id response))success
  655. failure:(void(^)(NSHTTPURLResponse *operation, id _Nullable responseObject, NSError *error))failure{
  656. NSParameterAssert(success);
  657. _requestMethod = @"POST";
  658. privateKey = [NSString stringWithFormat:@"?privateKey=%@",privateKey];
  659. serverPath = [serverPath stringByAppendingString:privateKey];
  660. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil];
  661. [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
  662. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  663. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  664. [operation resume];
  665. }
  666. // curl -X DELETE http://<user>:<password>@nextcloud/ocs/v2.php/apps/end_to_end_encryption/api/v1/public-key -H "OCS-APIRequest:true"
  667. - (void)deleteEndToEndPublicKey:(NSString*)serverPath onCommunication:(OCCommunication *)sharedOCCommunication success:(void(^)(NSHTTPURLResponse *operation, id response))success
  668. failure:(void(^)(NSHTTPURLResponse *operation, id _Nullable responseObject, NSError *error))failure{
  669. _requestMethod = @"DELETE";
  670. NSString *jsonQuery = [NSString stringWithFormat:@"?format=json"];
  671. serverPath = [serverPath stringByAppendingString:jsonQuery];
  672. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil];
  673. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  674. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  675. [operation resume];
  676. }
  677. // curl -X DELETE http://<user>:<password>@nextcloud/ocs/v2.php/apps/end_to_end_encryption/api/v1/private-key -H "OCS-APIRequest:true"
  678. - (void)deleteEndToEndPrivateKey:(NSString*)serverPath onCommunication:(OCCommunication *)sharedOCCommunication success:(void(^)(NSHTTPURLResponse *operation, id response))success
  679. failure:(void(^)(NSHTTPURLResponse *operation, id _Nullable responseObject, NSError *error))failure{
  680. _requestMethod = @"DELETE";
  681. NSString *jsonQuery = [NSString stringWithFormat:@"?format=json"];
  682. serverPath = [serverPath stringByAppendingString:jsonQuery];
  683. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil];
  684. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  685. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  686. [operation resume];
  687. }
  688. #pragma mark - Manage Redirections
  689. - (void) setRedirectionBlockOnDatataskWithOCCommunication: (OCCommunication *) sharedOCCommunication andSessionManager:(AFURLSessionManager *) sessionManager{
  690. [sessionManager setTaskWillPerformHTTPRedirectionBlock:^NSURLRequest * _Nonnull(NSURLSession * _Nonnull session, NSURLSessionTask * _Nonnull task, NSURLResponse * _Nonnull response, NSURLRequest * _Nonnull request) {
  691. if (response == nil) {
  692. // needed to handle fake redirects to canonical addresses, as explained in https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/URLLoadingSystem/Articles/RequestChanges.html
  693. return request;
  694. }
  695. NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *) response;
  696. NSDictionary *dict = [httpResponse allHeaderFields];
  697. //Server path of redirected server
  698. NSString *responseURLString = [dict objectForKey:@"Location"];
  699. if (responseURLString) {
  700. if ([UtilsFramework isURLWithSamlFragment:responseURLString] || httpResponse.statusCode == k_redirected_code_1) {
  701. //We set the redirectedServer in case SAML or is a permanent redirection
  702. self.redirectedServer = responseURLString;
  703. if ([UtilsFramework isURLWithSamlFragment:responseURLString]) {
  704. // if SAML request, we don't want to follow it; WebView takes care, not here -> nil to NO FOLLOW
  705. return nil;
  706. }
  707. }
  708. NSMutableURLRequest *requestRedirect = [request mutableCopy];
  709. [requestRedirect setURL: [NSURL URLWithString:responseURLString]];
  710. requestRedirect = [sharedOCCommunication getRequestWithCredentials:requestRedirect];
  711. requestRedirect.HTTPMethod = _requestMethod;
  712. if (_postStringForShare) {
  713. //It is a request to share a file by link
  714. requestRedirect = [self sharedRequestWithMethod:_requestMethod path:responseURLString parameters:nil];
  715. [requestRedirect setHTTPBody:[_postStringForShare dataUsingEncoding:NSUTF8StringEncoding]];
  716. }
  717. return requestRedirect;
  718. } else {
  719. // no location to redirect -> nil to NO FOLLOW
  720. return nil;
  721. }
  722. }];
  723. }
  724. @end