OCWebDAVClient.m 64 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169
  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:\" xmlns:oc=\"http://owncloud.org/ns\" xmlns:nc=\"http://nextcloud.org/ns\"><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/><nc:is-encrypted/></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:\" xmlns:oc=\"http://owncloud.org/ns\" xmlns:nc=\"http://nextcloud.org/ns\"><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/><nc:is-encrypted/></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\" xmlns:nc=\"http://nextcloud.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. "<nc:is-encrypted/>"
  231. "</d:prop>"
  232. "</d:select>"
  233. "<d:from>"
  234. "<d:scope>"
  235. "<d:href>/files/%@%@</d:href>"
  236. "<d:depth>infinity</d:depth>"
  237. "</d:scope>"
  238. "</d:from>"
  239. "<d:where>"
  240. "<d:like>"
  241. //"<d:prop><d:getcontenttype/></d:prop>"
  242. //"<d:literal>image/%</d:literal>"
  243. "<d:prop><d:displayname/></d:prop>"
  244. "<d:literal>%@</d:literal>"
  245. "</d:like>"
  246. "</d:where>"
  247. "</d:basicsearch>"
  248. "</d:searchrequest>", userID, folder, fileName];
  249. [request setHTTPBody:[body dataUsingEncoding:NSUTF8StringEncoding]];
  250. [request setValue:@"text/xml" forHTTPHeaderField:@"Content-Type"];
  251. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication withUserSessionToken:token success:success failure:failure];
  252. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  253. [operation resume];
  254. }
  255. - (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 {
  256. NSParameterAssert(success);
  257. _requestMethod = @"PROPPATCH";
  258. NSMutableURLRequest *request = [self requestWithMethod:_requestMethod path:path parameters:nil];
  259. NSString *body;
  260. body = [NSString stringWithFormat: @""
  261. "<?xml version=\"1.0\"?>"
  262. "<d:propertyupdate xmlns:d=\"DAV:\" xmlns:oc=\"http://owncloud.org/ns\" xmlns:nc=\"http://nextcloud.org/ns\">"
  263. "<d:set>"
  264. "<d:prop>"
  265. "<oc:favorite>%i</oc:favorite>"
  266. "</d:prop>"
  267. "</d:set>"
  268. "</d:propertyupdate>", (favorite ? 1 : 0)];
  269. [request setHTTPBody:[body dataUsingEncoding:NSUTF8StringEncoding]];
  270. [request setValue:@"application/xml" forHTTPHeaderField:@"Content-Type"];
  271. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication withUserSessionToken:token success:success failure:failure];
  272. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  273. [operation resume];
  274. }
  275. - (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 {
  276. NSString *body;
  277. NSParameterAssert(success);
  278. _requestMethod = @"REPORT";
  279. userID = [userID stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLFragmentAllowedCharacterSet]];
  280. NSMutableURLRequest *request = [self requestWithMethod:_requestMethod path:[NSString stringWithFormat:@"%@/files/%@%@", path, userID, folder] parameters:nil];
  281. body = [NSString stringWithFormat: @""
  282. "<?xml version=\"1.0\"?>"
  283. "<oc:filter-files xmlns:d=\"DAV:\" xmlns:oc=\"http://owncloud.org/ns\" xmlns:nc=\"http://nextcloud.org/ns\">"
  284. "<d:prop>"
  285. "<d:resourcetype/>"
  286. "<oc:fileid/>"
  287. "<d:getetag/>"
  288. "<d:getcontentlength/>"
  289. "<oc:size/>"
  290. "<d:getlastmodified/>"
  291. "<oc:id/>"
  292. "<oc:permissions/>"
  293. "<oc:favorite/>"
  294. "<nc:is-encrypted/>"
  295. "</d:prop>"
  296. "<oc:filter-rules>"
  297. "<oc:favorite>1</oc:favorite>"
  298. "</oc:filter-rules>"
  299. "</oc:filter-files>"];
  300. [request setHTTPBody:[body dataUsingEncoding:NSUTF8StringEncoding]];
  301. [request setValue:@"text/xml" forHTTPHeaderField:@"Content-Type"];
  302. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication withUserSessionToken:token success:success failure:failure];
  303. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  304. [operation resume];
  305. }
  306. - (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{
  307. NSMutableURLRequest *request = [self requestWithMethod:@"GET" path:remoteSource parameters:nil];
  308. //If is not nil is a redirection so we keep the original url server
  309. if (!self.originalUrlServer) {
  310. self.originalUrlServer = [request.URL absoluteString];
  311. }
  312. //We add the cookies of that URL
  313. request = [UtilsFramework getRequestWithCookiesByRequest:request andOriginalUrlServer:self.originalUrlServer];
  314. NSURL *localDestinationUrl = [NSURL fileURLWithPath:localDestination];
  315. NSURLSessionDownloadTask *downloadTask = [sharedOCCommunication.downloadSessionManager downloadTaskWithRequest:request progress:^(NSProgress * _Nonnull progress) {
  316. downloadProgress(progress);
  317. } destination:^NSURL * _Nonnull(NSURL * _Nonnull targetPath, NSURLResponse * _Nonnull response) {
  318. return localDestinationUrl;
  319. } completionHandler:^(NSURLResponse * _Nonnull response, NSURL * _Nullable filePath, NSError * _Nullable error) {
  320. if (error) {
  321. failure(response, error);
  322. } else {
  323. success(response,filePath);
  324. }
  325. }];
  326. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.downloadSessionManager];
  327. if (defaultPriority) {
  328. [downloadTask resume];
  329. }
  330. return downloadTask;
  331. }
  332. - (void)checkServer:(NSString *)path onCommunication:
  333. (OCCommunication *)sharedOCCommunication
  334. success:(void(^)(NSHTTPURLResponse *, id))success
  335. failure:(void(^)(NSHTTPURLResponse *, id _Nullable responseObject, NSError *))failure {
  336. _requestMethod = @"HEAD";
  337. NSMutableURLRequest *request = [self requestWithMethod:_requestMethod path:path parameters:nil];
  338. request.HTTPShouldHandleCookies = false;
  339. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  340. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  341. [operation resume];
  342. }
  343. - (void)makeCollection:(NSString *)path onCommunication:
  344. (OCCommunication *)sharedOCCommunication
  345. success:(void(^)(NSHTTPURLResponse *, id))success
  346. failure:(void(^)(NSHTTPURLResponse *, id _Nullable responseObject, NSError *))failure {
  347. _requestMethod = @"MKCOL";
  348. NSMutableURLRequest *request = [self requestWithMethod:_requestMethod path:path parameters:nil];
  349. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  350. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  351. [operation resume];
  352. }
  353. - (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 {
  354. NSFileManager *fileManager = [NSFileManager defaultManager];
  355. if (localSource == nil || ![fileManager fileExistsAtPath:localSource]) {
  356. NSMutableDictionary* details = [NSMutableDictionary dictionary];
  357. [details setValue:@"You are trying upload a file that does not exist" forKey:NSLocalizedDescriptionKey];
  358. NSError *error = [NSError errorWithDomain:k_domain_error_code code:OCErrorFileToUploadDoesNotExist userInfo:details];
  359. failureBeforeRequest(error);
  360. return nil;
  361. } else {
  362. NSMutableURLRequest *request = [self requestWithMethod:@"PUT" path:remoteDestination parameters:nil];
  363. [request setTimeoutInterval:k_timeout_upload];
  364. [request setValue:[NSString stringWithFormat:@"%lld", [UtilsFramework getSizeInBytesByPath:localSource]] forHTTPHeaderField:@"Content-Length"];
  365. [request setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData];
  366. //If is not nil is a redirection so we keep the original url server
  367. if (!self.originalUrlServer) {
  368. self.originalUrlServer = [request.URL absoluteString];
  369. }
  370. if (sharedOCCommunication.isCookiesAvailable) {
  371. //We add the cookies of that URL
  372. request = [UtilsFramework getRequestWithCookiesByRequest:request andOriginalUrlServer:self.originalUrlServer];
  373. } else {
  374. [UtilsFramework deleteAllCookies];
  375. }
  376. NSURL *file = [NSURL fileURLWithPath:localSource];
  377. sharedOCCommunication.uploadSessionManager.responseSerializer = [AFHTTPResponseSerializer serializer];
  378. NSURLSessionUploadTask *uploadTask = [sharedOCCommunication.uploadSessionManager uploadTaskWithRequest:request fromFile:file progress:^(NSProgress * _Nonnull progress) {
  379. uploadProgress(progress);
  380. } completionHandler:^(NSURLResponse * _Nonnull response, id _Nullable responseObject, NSError * _Nullable error) {
  381. if (error) {
  382. failure(response, responseObject, error);
  383. } else {
  384. success(response,responseObject);
  385. }
  386. }];
  387. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.uploadSessionManager];
  388. [uploadTask resume];
  389. return uploadTask;
  390. }
  391. }
  392. - (void) requestUserNameOfServer:(NSString * _Nonnull) path byCookie:(NSString * _Nonnull) cookieString onCommunication:
  393. (OCCommunication * _Nonnull)sharedOCCommunication success:(void(^ _Nonnull)(NSHTTPURLResponse * _Nonnull, id _Nonnull))success
  394. failure:(void(^ _Nonnull)(NSHTTPURLResponse * _Nonnull, id _Nullable responseObject, NSError * _Nonnull))failure {
  395. NSString *apiUserUrl = nil;
  396. apiUserUrl = [NSString stringWithFormat:@"%@%@", path, k_api_user_url_json];
  397. NSLog(@"api user name call: %@", apiUserUrl);
  398. _requestMethod = @"GET";
  399. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path: apiUserUrl parameters: nil];
  400. [request setValue:@"application/xml" forHTTPHeaderField:@"Content-Type"];
  401. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  402. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  403. [operation resume];
  404. }
  405. - (void) getStatusOfTheServer:(NSString *)serverPath onCommunication:
  406. (OCCommunication *)sharedOCCommunication success:(void(^)(NSHTTPURLResponse *operation, id responseObject))success
  407. failure:(void(^)(NSHTTPURLResponse *operation, id _Nullable responseObject, NSError *error))failure {
  408. NSString *urlString = [NSString stringWithFormat:@"%@%@", serverPath, k_server_information_json];
  409. _requestMethod = @"GET";
  410. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path: urlString parameters: nil];
  411. request.HTTPShouldHandleCookies = false;
  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)listSharedByServer:(NSString *)serverPath
  417. onCommunication:(OCCommunication *)sharedOCCommunication
  418. success:(void(^)(NSHTTPURLResponse *, id))success
  419. failure:(void(^)(NSHTTPURLResponse *, id _Nullable responseObject, NSError *))failure {
  420. NSParameterAssert(success);
  421. _requestMethod = @"GET";
  422. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil];
  423. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  424. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  425. [operation resume];
  426. }
  427. - (void)listSharedByServer:(NSString *)serverPath andPath:(NSString *) path
  428. onCommunication:(OCCommunication *)sharedOCCommunication
  429. success:(void(^)(NSHTTPURLResponse *, id))success
  430. failure:(void(^)(NSHTTPURLResponse *, id _Nullable responseObject, NSError *))failure {
  431. NSParameterAssert(success);
  432. NSString *postString = [NSString stringWithFormat: @"?path=%@&subfiles=true",path];
  433. serverPath = [serverPath stringByAppendingString:postString];
  434. _requestMethod = @"GET";
  435. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil];
  436. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  437. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  438. [operation resume];
  439. }
  440. - (void)shareByLinkFileOrFolderByServer:(NSString *)serverPath andPath:(NSString *) filePath andPassword:(NSString *)password
  441. onCommunication:(OCCommunication *)sharedOCCommunication
  442. success:(void(^)(NSHTTPURLResponse *, id))success
  443. failure:(void(^)(NSHTTPURLResponse *, id _Nullable responseObject, NSError *))failure {
  444. NSParameterAssert(success);
  445. _requestMethod = @"POST";
  446. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil];
  447. _postStringForShare = [NSString stringWithFormat: @"path=%@&shareType=3&password=%@",filePath,password];
  448. [request setHTTPBody:[_postStringForShare dataUsingEncoding:NSUTF8StringEncoding]];
  449. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  450. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  451. [operation resume];
  452. }
  453. - (void)shareByLinkFileOrFolderByServer:(NSString *)serverPath andPath:(NSString *) filePath
  454. onCommunication:(OCCommunication *)sharedOCCommunication
  455. success:(void(^)(NSHTTPURLResponse *, id))success
  456. failure:(void(^)(NSHTTPURLResponse *, id _Nullable responseObject, NSError *))failure {
  457. NSParameterAssert(success);
  458. _requestMethod = @"POST";
  459. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil];
  460. _postStringForShare = [NSString stringWithFormat: @"path=%@&shareType=3",filePath];
  461. [request setHTTPBody:[_postStringForShare dataUsingEncoding:NSUTF8StringEncoding]];
  462. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  463. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  464. [operation resume];
  465. }
  466. - (void)shareWith:(NSString *)userOrGroup shareeType:(NSInteger)shareeType inServer:(NSString *) serverPath andPath:(NSString *) filePath andPermissions:(NSInteger) permissions onCommunication:(OCCommunication *)sharedOCCommunication
  467. success:(void(^)(NSHTTPURLResponse *, id))success
  468. failure:(void(^)(NSHTTPURLResponse *, id _Nullable responseObject, NSError *))failure {
  469. NSParameterAssert(success);
  470. _requestMethod = @"POST";
  471. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil];
  472. self.postStringForShare = [NSString stringWithFormat: @"path=%@&shareType=%ld&shareWith=%@&permissions=%ld",filePath, (long)shareeType, userOrGroup, (long)permissions];
  473. [request setHTTPBody:[_postStringForShare dataUsingEncoding:NSUTF8StringEncoding]];
  474. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  475. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  476. [operation resume];
  477. }
  478. - (void)unShareFileOrFolderByServer:(NSString *)serverPath
  479. onCommunication:(OCCommunication *)sharedOCCommunication
  480. success:(void(^)(NSHTTPURLResponse *, id))success
  481. failure:(void(^)(NSHTTPURLResponse *, id _Nullable responseObject, NSError *))failure {
  482. NSParameterAssert(success);
  483. _requestMethod = @"DELETE";
  484. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil];
  485. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  486. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  487. [operation resume];
  488. }
  489. - (void)isShareFileOrFolderByServer:(NSString *)serverPath
  490. onCommunication:(OCCommunication *)sharedOCCommunication
  491. success:(void(^)(NSHTTPURLResponse *, id))success
  492. failure:(void(^)(NSHTTPURLResponse *, id _Nullable responseObject, NSError *))failure {
  493. NSParameterAssert(success);
  494. _requestMethod = @"GET";
  495. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil];
  496. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  497. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  498. [operation resume];
  499. }
  500. - (void) updateShareItem:(NSInteger)shareId ofServerPath:(NSString*)serverPath withPasswordProtect:(NSString*)password andExpirationTime:(NSString*)expirationTime andPermissions:(NSInteger)permissions
  501. onCommunication:(OCCommunication *)sharedOCCommunication
  502. success:(void(^)(NSHTTPURLResponse *, id response))success
  503. failure:(void(^)(NSHTTPURLResponse *, id _Nullable responseObject, NSError *error))failure{
  504. NSParameterAssert(success);
  505. _requestMethod = @"PUT";
  506. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil];
  507. if (password) {
  508. self.postStringForShare = [NSString stringWithFormat:@"password=%@",password];
  509. } else if (expirationTime) {
  510. self.postStringForShare = [NSString stringWithFormat:@"expireDate=%@",expirationTime];
  511. }else if (permissions > 0) {
  512. self.postStringForShare = [NSString stringWithFormat:@"permissions=%ld",(long)permissions];
  513. }
  514. [request setHTTPBody:[_postStringForShare dataUsingEncoding:NSUTF8StringEncoding]];
  515. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  516. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  517. [operation resume];
  518. }
  519. - (void) searchUsersAndGroupsWith:(NSString *)searchString forPage:(NSInteger)page with:(NSInteger)resultsPerPage ofServer:(NSString*)serverPath onCommunication:(OCCommunication *)sharedOCCommunication success:(void(^)(NSHTTPURLResponse *operation, id response))success
  520. failure:(void(^)(NSHTTPURLResponse *operation, id _Nullable responseObject, NSError *error))failure {
  521. NSParameterAssert(success);
  522. _requestMethod = @"GET";
  523. NSString *searchQuery = [NSString stringWithFormat: @"&search=%@",searchString];
  524. NSString *jsonQuery = [NSString stringWithFormat:@"?format=json"];
  525. NSString *queryType = [NSString stringWithFormat:@"&itemType=file"];
  526. NSString *pagination = [NSString stringWithFormat:@"&page=%ld&perPage=%ld", (long)page, (long)resultsPerPage];
  527. serverPath = [serverPath stringByAppendingString:jsonQuery];
  528. serverPath = [serverPath stringByAppendingString:queryType];
  529. serverPath = [serverPath stringByAppendingString:searchQuery];
  530. serverPath = [serverPath stringByAppendingString:pagination];
  531. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil];
  532. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  533. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  534. [operation resume];
  535. }
  536. - (void)getSharePermissionsFile:(NSString*)fileName onCommunication:(OCCommunication *)sharedOCCommunication
  537. success:(void(^)(NSHTTPURLResponse *, id))success
  538. failure:(void(^)(NSHTTPURLResponse *, id _Nullable responseObject, NSError *))failure {
  539. NSParameterAssert(success);
  540. _requestMethod = @"PROPFIND";
  541. NSMutableURLRequest *request = [self requestWithMethod:_requestMethod path:fileName parameters:nil];
  542. [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]];
  543. [request setValue:@"application/xml" forHTTPHeaderField:@"Content-Type"];
  544. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  545. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  546. [operation resume];
  547. }
  548. - (void) getCapabilitiesOfServer:(NSString*)serverPath onCommunication:(OCCommunication *)sharedOCCommunication success:(void(^)(NSHTTPURLResponse *operation, id response))success
  549. failure:(void(^)(NSHTTPURLResponse *operation, id _Nullable responseObject, NSError *error))failure{
  550. _requestMethod = @"GET";
  551. serverPath = [serverPath stringByAppendingString:[NSString stringWithFormat:@"?format=json"]];
  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. #pragma mark - Remote thumbnails
  558. - (OCHTTPRequestOperation *) getRemoteThumbnailByServer:(NSString*)serverPath ofFilePath:(NSString*)filePath withWidth:(NSInteger)fileWidth andHeight:(NSInteger)fileHeight onCommunication:(OCCommunication *)sharedOCCommunication
  559. success:(void(^)(NSHTTPURLResponse *operation, id response))success
  560. failure:(void(^)(NSHTTPURLResponse *operation, id _Nullable responseObject, NSError *error))failure{
  561. _requestMethod = @"GET";
  562. NSString *query = [NSString stringWithFormat:@"/%i/%i/%@", (int)fileWidth, (int)fileHeight, filePath];
  563. serverPath = [serverPath stringByAppendingString:query];
  564. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil];
  565. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  566. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  567. return operation;
  568. }
  569. #pragma mark - Get Notification
  570. - (void)getNotificationServer:(NSString*)serverPath onCommunication:(OCCommunication *)sharedOCCommunication success:(void(^)(NSHTTPURLResponse *operation, id response))success
  571. failure:(void(^)(NSHTTPURLResponse *operation, id _Nullable responseObject, NSError *error))failure{
  572. _requestMethod = @"GET";
  573. serverPath = [serverPath stringByAppendingString:[NSString stringWithFormat:@"?format=json"]];
  574. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil];
  575. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  576. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  577. [operation resume];
  578. }
  579. - (void)setNotificationServer:(NSString *)serverPath type:(NSString *)type onCommunication:(OCCommunication *)sharedOCCommunication success:(void(^)(NSHTTPURLResponse *, id))success failure:(void(^)(NSHTTPURLResponse *, id _Nullable responseObject, NSError *))failure {
  580. NSParameterAssert(success);
  581. _requestMethod = type;
  582. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil];
  583. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  584. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  585. [operation resume];
  586. }
  587. - (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 {
  588. NSParameterAssert(success);
  589. _requestMethod = @"POST";
  590. NSString *pushTokenHashParam = [NSString stringWithFormat:@"?pushTokenHash=%@",pushTokenHash];
  591. NSString *devicePublicKeyParam = [NSString stringWithFormat:@"&devicePublicKey=%@",devicePublicKey];
  592. NSString *proxyServerPathParam = [NSString stringWithFormat:@"&proxyServer=%@",proxyServerPath];
  593. serverPath = [serverPath stringByAppendingString:pushTokenHashParam];
  594. serverPath = [serverPath stringByAppendingString:devicePublicKeyParam];
  595. serverPath = [serverPath stringByAppendingString:proxyServerPathParam];
  596. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil];
  597. [request setValue:[NSString stringWithFormat:@"token %@", authorizationToken] forHTTPHeaderField:@"Authorization"];
  598. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  599. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  600. [operation resume];
  601. }
  602. - (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 {
  603. NSParameterAssert(success);
  604. _requestMethod = @"POST";
  605. pushToken = [NSString stringWithFormat:@"?pushToken=%@",pushToken];
  606. deviceIdentifier = [NSString stringWithFormat:@"&deviceIdentifier=%@",deviceIdentifier];
  607. deviceIdentifierSignature = [NSString stringWithFormat:@"&deviceIdentifierSignature=%@",deviceIdentifierSignature];
  608. userPublicKey = [NSString stringWithFormat:@"&userPublicKey=%@",userPublicKey];
  609. serverPath = [serverPath stringByAppendingString:pushToken];
  610. serverPath = [serverPath stringByAppendingString:deviceIdentifier];
  611. serverPath = [serverPath stringByAppendingString:deviceIdentifierSignature];
  612. serverPath = [serverPath stringByAppendingString:userPublicKey];
  613. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil];
  614. [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
  615. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  616. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  617. [operation resume];
  618. }
  619. #pragma mark - Get Activity
  620. - (void) getActivityServer:(NSString*)serverPath onCommunication:(OCCommunication *)sharedOCCommunication success:(void(^)(NSHTTPURLResponse *operation, id response))success
  621. failure:(void(^)(NSHTTPURLResponse *operation, id _Nullable responseObject, NSError *error))failure{
  622. _requestMethod = @"GET";
  623. serverPath = [serverPath stringByAppendingString:[NSString stringWithFormat:@"?format=json"]];
  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. #pragma mark - Get External sites
  630. - (void) getExternalSitesServer:(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. serverPath = [serverPath stringByAppendingString:[NSString stringWithFormat:@"?format=json"]];
  634. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil];
  635. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  636. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  637. [operation resume];
  638. }
  639. #pragma mark - Get User Profile
  640. - (void) getUserProfileServer:(NSString*)serverPath onCommunication:(OCCommunication *)sharedOCCommunication success:(void(^)(NSHTTPURLResponse *operation, id response))success
  641. failure:(void(^)(NSHTTPURLResponse *operation, id _Nullable responseObject, NSError *error))failure{
  642. _requestMethod = @"GET";
  643. serverPath = [serverPath stringByAppendingString:[NSString stringWithFormat:@"?format=json"]];
  644. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil];
  645. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  646. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  647. [operation resume];
  648. }
  649. #pragma mark - End-to-End Encryption
  650. - (void)getEndToEndPublicKeys:(NSString*)serverPath onCommunication:(OCCommunication *)sharedOCCommunication success:(void(^)(NSHTTPURLResponse *operation, id response))success
  651. failure:(void(^)(NSHTTPURLResponse *operation, id _Nullable responseObject, NSError *error))failure{
  652. _requestMethod = @"GET";
  653. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil];
  654. [request setValue:@"true" forHTTPHeaderField:@"OCS-APIRequest"];
  655. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  656. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  657. [operation resume];
  658. }
  659. - (void)getEndToEndPrivateKeyCipher:(NSString*)serverPath onCommunication:(OCCommunication *)sharedOCCommunication success:(void(^)(NSHTTPURLResponse *operation, id response))success
  660. failure:(void(^)(NSHTTPURLResponse *operation, id _Nullable responseObject, NSError *error))failure{
  661. _requestMethod = @"GET";
  662. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil];
  663. [request setValue:@"true" forHTTPHeaderField:@"OCS-APIRequest"];
  664. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  665. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  666. [operation resume];
  667. }
  668. - (void)getEndToEndServerPublicKey:(NSString*)serverPath onCommunication:(OCCommunication *)sharedOCCommunication success:(void(^)(NSHTTPURLResponse *operation, id response))success
  669. failure:(void(^)(NSHTTPURLResponse *operation, id _Nullable responseObject, NSError *error))failure{
  670. _requestMethod = @"GET";
  671. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil];
  672. [request setValue:@"true" forHTTPHeaderField:@"OCS-APIRequest"];
  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. - (void)signEndToEndPublicKey:(NSString*)serverPath key:(NSString *)key onCommunication:(OCCommunication *)sharedOCCommunication success:(void(^)(NSHTTPURLResponse *operation, id response))success
  678. failure:(void(^)(NSHTTPURLResponse *operation, id _Nullable responseObject, NSError *error))failure{
  679. NSParameterAssert(success);
  680. _requestMethod = @"POST";
  681. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil];
  682. [request setValue:@"true" forHTTPHeaderField:@"OCS-APIRequest"];
  683. _postStringKey = [NSString stringWithFormat: @"csr=%@",key];
  684. [request setHTTPBody:[_postStringKey dataUsingEncoding:NSUTF8StringEncoding]];
  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)storeEndToEndPrivateKeyCipher:(NSString*)serverPath key:(NSString *)key onCommunication:(OCCommunication *)sharedOCCommunication success:(void(^)(NSHTTPURLResponse *operation, id response))success
  690. failure:(void(^)(NSHTTPURLResponse *operation, id _Nullable responseObject, NSError *error))failure{
  691. NSParameterAssert(success);
  692. _requestMethod = @"POST";
  693. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil];
  694. [request setValue:@"true" forHTTPHeaderField:@"OCS-APIRequest"];
  695. _postStringKey = [NSString stringWithFormat: @"privateKey=%@",key];
  696. [request setHTTPBody:[_postStringKey dataUsingEncoding:NSUTF8StringEncoding]];
  697. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  698. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  699. [operation resume];
  700. }
  701. - (void)deleteEndToEndPublicKey:(NSString*)serverPath onCommunication:(OCCommunication *)sharedOCCommunication success:(void(^)(NSHTTPURLResponse *operation, id response))success
  702. failure:(void(^)(NSHTTPURLResponse *operation, id _Nullable responseObject, NSError *error))failure{
  703. _requestMethod = @"DELETE";
  704. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil];
  705. [request setValue:@"true" forHTTPHeaderField:@"OCS-APIRequest"];
  706. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  707. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  708. [operation resume];
  709. }
  710. - (void)deleteEndToEndPrivateKey:(NSString*)serverPath onCommunication:(OCCommunication *)sharedOCCommunication success:(void(^)(NSHTTPURLResponse *operation, id response))success
  711. failure:(void(^)(NSHTTPURLResponse *operation, id _Nullable responseObject, NSError *error))failure{
  712. _requestMethod = @"DELETE";
  713. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil];
  714. [request setValue:@"true" forHTTPHeaderField:@"OCS-APIRequest"];
  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)markEndToEndFolderEncrypted:(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 = @"PUT";
  722. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil];
  723. [request setValue:@"true" forHTTPHeaderField:@"OCS-APIRequest"];
  724. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  725. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  726. [operation resume];
  727. }
  728. - (void)deletemarkEndToEndFolderEncrypted:(NSString*)serverPath onCommunication:(OCCommunication *)sharedOCCommunication success:(void(^)(NSHTTPURLResponse *operation, id response))success
  729. failure:(void(^)(NSHTTPURLResponse *operation, id _Nullable responseObject, NSError *error))failure{
  730. _requestMethod = @"DELETE";
  731. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil];
  732. [request setValue:@"true" forHTTPHeaderField:@"OCS-APIRequest"];
  733. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  734. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  735. [operation resume];
  736. }
  737. - (void)lockEndToEndFolderEncrypted:(NSString*)serverPath onCommunication:(OCCommunication *)sharedOCCommunication success:(void(^)(NSHTTPURLResponse *operation, id response))success
  738. failure:(void(^)(NSHTTPURLResponse *operation, id _Nullable responseObject, NSError *error))failure{
  739. _requestMethod = @"POST";
  740. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil];
  741. [request setValue:@"true" forHTTPHeaderField:@"OCS-APIRequest"];
  742. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  743. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  744. [operation resume];
  745. }
  746. - (void)unlockEndToEndFolderEncrypted:(NSString*)serverPath token:(NSString *)token onCommunication:(OCCommunication *)sharedOCCommunication success:(void(^)(NSHTTPURLResponse *operation, id response))success
  747. failure:(void(^)(NSHTTPURLResponse *operation, id _Nullable responseObject, NSError *error))failure{
  748. _requestMethod = @"DELETE";
  749. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil];
  750. [request setValue:@"true" forHTTPHeaderField:@"OCS-APIRequest"];
  751. // Add token
  752. [request setValue:token forHTTPHeaderField:@"token"];
  753. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  754. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  755. [operation resume];
  756. }
  757. - (void)getEndToEndMetadata:(NSString*)serverPath onCommunication:(OCCommunication *)sharedOCCommunication success:(void(^)(NSHTTPURLResponse *operation, id response))success
  758. failure:(void(^)(NSHTTPURLResponse *operation, id _Nullable responseObject, NSError *error))failure{
  759. NSParameterAssert(success);
  760. _requestMethod = @"GET";
  761. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil];
  762. [request setValue:@"true" forHTTPHeaderField:@"OCS-APIRequest"];
  763. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  764. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  765. [operation resume];
  766. }
  767. - (void)storeEndToEndMetadata:(NSString*)serverPath metadata:(NSString *)metadata onCommunication:(OCCommunication *)sharedOCCommunication success:(void(^)(NSHTTPURLResponse *operation, id response))success
  768. failure:(void(^)(NSHTTPURLResponse *operation, id _Nullable responseObject, NSError *error))failure{
  769. NSParameterAssert(success);
  770. _requestMethod = @"POST";
  771. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil];
  772. [request setValue:@"true" forHTTPHeaderField:@"OCS-APIRequest"];
  773. _postStringMetadata = [NSString stringWithFormat: @"metaData=%@",metadata];
  774. [request setHTTPBody:[_postStringMetadata dataUsingEncoding:NSUTF8StringEncoding]];
  775. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  776. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  777. [operation resume];
  778. }
  779. - (void)updateEndToEndMetadata:(NSString*)serverPath metadata:(NSString *)metadata onCommunication:(OCCommunication *)sharedOCCommunication success:(void(^)(NSHTTPURLResponse *operation, id response))success
  780. failure:(void(^)(NSHTTPURLResponse *operation, id _Nullable responseObject, NSError *error))failure{
  781. NSParameterAssert(success);
  782. _requestMethod = @"PUT";
  783. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil];
  784. [request setValue:@"true" forHTTPHeaderField:@"OCS-APIRequest"];
  785. _postStringMetadata = [NSString stringWithFormat: @"metaData=%@",metadata];
  786. [request setHTTPBody:[_postStringMetadata dataUsingEncoding:NSUTF8StringEncoding]];
  787. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  788. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  789. [operation resume];
  790. }
  791. - (void)deleteEndToEndMetadata:(NSString*)serverPath onCommunication:(OCCommunication *)sharedOCCommunication success:(void(^)(NSHTTPURLResponse *operation, id response))success
  792. failure:(void(^)(NSHTTPURLResponse *operation, id _Nullable responseObject, NSError *error))failure{
  793. NSParameterAssert(success);
  794. _requestMethod = @"DELETE";
  795. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil];
  796. [request setValue:@"true" forHTTPHeaderField:@"OCS-APIRequest"];
  797. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  798. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  799. [operation resume];
  800. }
  801. #pragma mark - Manage Redirections
  802. - (void) setRedirectionBlockOnDatataskWithOCCommunication: (OCCommunication *) sharedOCCommunication andSessionManager:(AFURLSessionManager *) sessionManager{
  803. [sessionManager setTaskWillPerformHTTPRedirectionBlock:^NSURLRequest * _Nonnull(NSURLSession * _Nonnull session, NSURLSessionTask * _Nonnull task, NSURLResponse * _Nonnull response, NSURLRequest * _Nonnull request) {
  804. if (response == nil) {
  805. // needed to handle fake redirects to canonical addresses, as explained in https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/URLLoadingSystem/Articles/RequestChanges.html
  806. return request;
  807. }
  808. NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *) response;
  809. NSDictionary *dict = [httpResponse allHeaderFields];
  810. //Server path of redirected server
  811. NSString *responseURLString = [dict objectForKey:@"Location"];
  812. if (responseURLString) {
  813. if ([UtilsFramework isURLWithSamlFragment:responseURLString] || httpResponse.statusCode == k_redirected_code_1) {
  814. //We set the redirectedServer in case SAML or is a permanent redirection
  815. self.redirectedServer = responseURLString;
  816. if ([UtilsFramework isURLWithSamlFragment:responseURLString]) {
  817. // if SAML request, we don't want to follow it; WebView takes care, not here -> nil to NO FOLLOW
  818. return nil;
  819. }
  820. }
  821. NSMutableURLRequest *requestRedirect = [request mutableCopy];
  822. [requestRedirect setURL: [NSURL URLWithString:responseURLString]];
  823. requestRedirect = [sharedOCCommunication getRequestWithCredentials:requestRedirect];
  824. requestRedirect.HTTPMethod = _requestMethod;
  825. if (_postStringForShare) {
  826. //It is a request to share a file by link
  827. requestRedirect = [self sharedRequestWithMethod:_requestMethod path:responseURLString parameters:nil];
  828. [requestRedirect setHTTPBody:[_postStringForShare dataUsingEncoding:NSUTF8StringEncoding]];
  829. }
  830. return requestRedirect;
  831. } else {
  832. // no location to redirect -> nil to NO FOLLOW
  833. return nil;
  834. }
  835. }];
  836. }
  837. @end