OCWebDAVClient.m 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890
  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 "OCChunkDto.h"
  38. #import "UtilsFramework.h"
  39. #import "AFURLSessionManager.h"
  40. #import "NSString+Encode.h"
  41. #import "OCConstants.h"
  42. #define k_api_user_url_xml @"index.php/ocs/cloud/user"
  43. #define k_api_user_url_json @"index.php/ocs/cloud/user?format=json"
  44. #define k_server_information_json @"status.php"
  45. #define k_api_header_request @"OCS-APIREQUEST"
  46. #define k_group_sharee_type 1
  47. NSString const *OCWebDAVContentTypeKey = @"getcontenttype";
  48. NSString const *OCWebDAVETagKey = @"getetag";
  49. NSString const *OCWebDAVCTagKey = @"getctag";
  50. NSString const *OCWebDAVCreationDateKey = @"creationdate";
  51. NSString const *OCWebDAVModificationDateKey = @"modificationdate";
  52. @interface OCWebDAVClient()
  53. - (void)mr_listPath:(NSString *)path depth:(NSUInteger)depth onCommunication:
  54. (OCCommunication *)sharedOCCommunication
  55. success:(void(^)(NSHTTPURLResponse *, id))success
  56. failure:(void(^)(NSHTTPURLResponse *, id _Nullable responseObject, NSError *))failure;
  57. @end
  58. @implementation OCWebDAVClient
  59. - (id) init {
  60. self = [super init];
  61. if (self != nil) {
  62. self.defaultHeaders = [NSMutableDictionary new];
  63. }
  64. return self;
  65. }
  66. - (void)setAuthorizationHeaderWithUsername:(NSString *)username password:(NSString *)password {
  67. NSString *basicAuthCredentials = [NSString stringWithFormat:@"%@:%@", username, password];
  68. [self.defaultHeaders setObject:[NSString stringWithFormat:@"Basic %@", [UtilsFramework AFBase64EncodedStringFromString: basicAuthCredentials]] forKey:@"Authorization"];
  69. }
  70. - (void)setAuthorizationHeaderWithCookie:(NSString *) cookieString {
  71. [self.defaultHeaders setObject:cookieString forKey:@"Cookie"];
  72. }
  73. - (void)setAuthorizationHeaderWithToken:(NSString *)token {
  74. [self.defaultHeaders setObject:token forKey:@"Authorization"];
  75. }
  76. - (void)setDefaultHeader:(NSString *)header value:(NSString *)value {
  77. [self.defaultHeaders setObject:value forKey:header];
  78. }
  79. - (void)setUserAgent:(NSString *)userAgent{
  80. [self.defaultHeaders setObject:userAgent forKey:@"User-Agent"];
  81. }
  82. - (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 {
  83. //If is not nil is a redirection so we keep the original url server
  84. if (!self.originalUrlServer) {
  85. self.originalUrlServer = [request.URL absoluteString];
  86. }
  87. if (sharedOCCommunication.isCookiesAvailable) {
  88. //We add the cookies of that URL
  89. request = [UtilsFramework getRequestWithCookiesByRequest:request andOriginalUrlServer:self.originalUrlServer];
  90. } else {
  91. [UtilsFramework deleteAllCookies];
  92. }
  93. OCHTTPRequestOperation *operation = (OCHTTPRequestOperation*) [sharedOCCommunication.networkSessionManager dataTaskWithRequest:request completionHandler:^(NSURLResponse * _Nonnull response, id _Nullable responseObject, NSError * _Nullable error) {
  94. if (!error) {
  95. success((NSHTTPURLResponse*)response,responseObject, token);
  96. } else {
  97. failure((NSHTTPURLResponse*)response, responseObject, error, token);
  98. }
  99. }];
  100. return operation;
  101. }
  102. - (OCHTTPRequestOperation *)mr_operationWithRequest:(NSMutableURLRequest *)request onCommunication:(OCCommunication *)sharedOCCommunication success:(void(^)(NSHTTPURLResponse *, id))success failure:(void(^)(NSHTTPURLResponse *, id _Nullable responseObject, NSError *))failure {
  103. //If is not nil is a redirection so we keep the original url server
  104. if (!self.originalUrlServer) {
  105. self.originalUrlServer = [request.URL absoluteString];
  106. }
  107. if (sharedOCCommunication.isCookiesAvailable) {
  108. //We add the cookies of that URL
  109. request = [UtilsFramework getRequestWithCookiesByRequest:request andOriginalUrlServer:self.originalUrlServer];
  110. } else {
  111. [UtilsFramework deleteAllCookies];
  112. }
  113. OCHTTPRequestOperation *operation = (OCHTTPRequestOperation*) [sharedOCCommunication.networkSessionManager dataTaskWithRequest:request completionHandler:^(NSURLResponse * _Nonnull response, id _Nullable responseObject, NSError * _Nullable error) {
  114. if (!error) {
  115. success((NSHTTPURLResponse*)response,responseObject);
  116. } else {
  117. failure((NSHTTPURLResponse*)response, responseObject, error);
  118. }
  119. }];
  120. return operation;
  121. }
  122. - (NSMutableURLRequest *)requestWithMethod:(NSString *)method path:(NSString *)path parameters:(NSDictionary *)parameters {
  123. NSMutableURLRequest *request = [[AFHTTPRequestSerializer new] requestWithMethod:method URLString:path parameters:nil error:nil];
  124. [request setAllHTTPHeaderFields:self.defaultHeaders];
  125. [request setCachePolicy: NSURLRequestReloadIgnoringLocalCacheData];
  126. [request setTimeoutInterval: k_timeout_webdav];
  127. return request;
  128. }
  129. - (NSMutableURLRequest *)sharedRequestWithMethod:(NSString *)method path:(NSString *)path parameters:(NSDictionary *)parameters {
  130. NSMutableURLRequest *request = [[AFHTTPRequestSerializer new] requestWithMethod:method URLString:path parameters:nil error:nil];
  131. [request setAllHTTPHeaderFields:self.defaultHeaders];
  132. //NSMutableURLRequest *request = [super requestWithMethod:method path:path parameters:parameters];
  133. [request setCachePolicy: NSURLRequestReloadIgnoringLocalCacheData];
  134. [request setTimeoutInterval: k_timeout_webdav];
  135. //Header for use the OC API CALL
  136. NSString *ocs_apiquests = @"true";
  137. [request setValue:ocs_apiquests forHTTPHeaderField:k_api_header_request];
  138. [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
  139. [request addValue:@"gzip, deflate" forHTTPHeaderField:@"Accept-Encoding"];
  140. return request;
  141. }
  142. - (void)movePath:(NSString *)source toPath:(NSString *)destination
  143. onCommunication:(OCCommunication *)sharedOCCommunication
  144. success:(void(^)(NSHTTPURLResponse *, id))success
  145. failure:(void(^)(NSHTTPURLResponse *, id _Nullable responseObject, NSError *))failure {
  146. _requestMethod = @"MOVE";
  147. NSMutableURLRequest *request = [self requestWithMethod:_requestMethod path:source parameters:nil];
  148. [request setValue:destination forHTTPHeaderField:@"Destination"];
  149. [request setValue:@"T" forHTTPHeaderField:@"Overwrite"];
  150. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  151. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  152. [operation resume];
  153. }
  154. - (void)deletePath:(NSString *)path
  155. onCommunication:(OCCommunication *)sharedOCCommunication
  156. success:(void(^)(NSHTTPURLResponse *, id))success
  157. failure:(void(^)(NSHTTPURLResponse *, id _Nullable responseObject, NSError *))failure {
  158. _requestMethod = @"DELETE";
  159. NSMutableURLRequest *request = [self requestWithMethod:_requestMethod path:path parameters:nil];
  160. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  161. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  162. [operation resume];
  163. }
  164. - (void)mr_listPath:(NSString *)path depth:(NSUInteger)depth onCommunication:
  165. (OCCommunication *)sharedOCCommunication
  166. success:(void(^)(NSHTTPURLResponse *, id))success
  167. failure:(void(^)(NSHTTPURLResponse *, id _Nullable responseObject, NSError *))failure {
  168. NSParameterAssert(success);
  169. _requestMethod = @"PROPFIND";
  170. NSMutableURLRequest *request = [self requestWithMethod:_requestMethod path:path parameters:nil];
  171. NSString *depthHeader = nil;
  172. if (depth <= 0)
  173. depthHeader = @"0";
  174. else if (depth == 1)
  175. depthHeader = @"1";
  176. else
  177. depthHeader = @"infinity";
  178. [request setValue: depthHeader forHTTPHeaderField: @"Depth"];
  179. [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\"/><D:creationdate/><id xmlns=\"http://owncloud.org/ns\"/><D:getcontentlength/><D:displayname/><D:quota-available-bytes/><D:getetag/><permissions xmlns=\"http://owncloud.org/ns\"/><D:quota-used-bytes/><D:getcontenttype/></D:prop></D:propfind>" dataUsingEncoding:NSUTF8StringEncoding]];
  180. [request setValue:@"application/xml" forHTTPHeaderField:@"Content-Type"];
  181. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  182. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  183. [operation resume];
  184. }
  185. - (void)mr_listPath:(NSString *)path depth:(NSUInteger)depth withUserSessionToken:(NSString*)token onCommunication:
  186. (OCCommunication *)sharedOCCommunication
  187. success:(void(^)(NSHTTPURLResponse *operation, id response, NSString *token))success
  188. failure:(void(^)(NSHTTPURLResponse *response, id _Nullable responseObject, NSError *, NSString *token))failure {
  189. NSParameterAssert(success);
  190. _requestMethod = @"PROPFIND";
  191. NSMutableURLRequest *request = [self requestWithMethod:_requestMethod path:path parameters:nil];
  192. NSString *depthHeader = nil;
  193. if (depth <= 0)
  194. depthHeader = @"0";
  195. else if (depth == 1)
  196. depthHeader = @"1";
  197. else
  198. depthHeader = @"infinity";
  199. [request setValue: depthHeader forHTTPHeaderField: @"Depth"];
  200. [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\"/><D:creationdate/><id xmlns=\"http://owncloud.org/ns\"/><D:getcontentlength/><D:displayname/><D:quota-available-bytes/><D:getetag/><permissions xmlns=\"http://owncloud.org/ns\"/><D:quota-used-bytes/><D:getcontenttype/></D:prop></D:propfind>" dataUsingEncoding:NSUTF8StringEncoding]];
  201. [request setValue:@"application/xml" forHTTPHeaderField:@"Content-Type"];
  202. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication withUserSessionToken:token success:success failure:failure];
  203. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  204. [operation resume];
  205. }
  206. - (void)propertiesOfPath:(NSString *)path
  207. onCommunication: (OCCommunication *)sharedOCCommunication
  208. success:(void(^)(NSHTTPURLResponse *, id ))success
  209. failure:(void(^)(NSHTTPURLResponse *, id _Nullable responseObject, NSError *))failure {
  210. [self mr_listPath:path depth:0 onCommunication:sharedOCCommunication success:success failure:failure];
  211. }
  212. - (void)listPath:(NSString *)path
  213. onCommunication:(OCCommunication *)sharedOCCommunication
  214. success:(void(^)(NSHTTPURLResponse *, id))success
  215. failure:(void(^)(NSHTTPURLResponse *, id _Nullable responseObject, NSError *))failure {
  216. [self mr_listPath:path depth:1 onCommunication:sharedOCCommunication success:success failure:failure];
  217. }
  218. - (void)listPath:(NSString *)path
  219. onCommunication:(OCCommunication *)sharedOCCommunication withUserSessionToken:(NSString *)token
  220. success:(void(^)(NSHTTPURLResponse *, id, NSString *token))success
  221. failure:(void(^)(NSHTTPURLResponse *, id _Nullable responseObject, NSError *, NSString *token))failure {
  222. [self mr_listPath:path depth:1 withUserSessionToken:token onCommunication:sharedOCCommunication success:success failure:failure];
  223. }
  224. - (void)search:(NSString *)path folder:(NSString *)folder fileName:(NSString *)fileName depth:(NSString *)depth user:(NSString *)user onCommunication:(OCCommunication *)sharedOCCommunication withUserSessionToken:(NSString *)token success:(void(^)(NSHTTPURLResponse *, id, NSString *token))success failure:(void(^)(NSHTTPURLResponse *, id _Nullable responseObject, NSError *, NSString *token))failure {
  225. NSString *body;
  226. NSParameterAssert(success);
  227. _requestMethod = @"SEARCH";
  228. NSMutableURLRequest *request = [self requestWithMethod:_requestMethod path:path parameters:nil];
  229. body = @"<?xml version=\"1.0\"?><d:searchrequest xmlns:d=\"DAV:\" xmlns:oc=\"http://owncloud.org/ns\"><d:basicsearch><d:select><d:prop>";
  230. // OCFileDto
  231. body = [body stringByAppendingString:@"<d:resourcetype/><oc:fileid/><d:getcontenttype/><d:getetag/><d:creationdate/><oc:size/><d:getcontentlength/><d:getlastmodified/><oc:id/><oc:permissions/><d:quota-available-bytes/><d:quota-used-bytes/><oc:favorite/>"];
  232. 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, user, folder, fileName];
  233. [request setHTTPBody:[body dataUsingEncoding:NSUTF8StringEncoding]];
  234. [request setValue:@"text/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)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 {
  240. NSParameterAssert(success);
  241. _requestMethod = @"PROPPATCH";
  242. NSMutableURLRequest *request = [self requestWithMethod:_requestMethod path:path parameters:nil];
  243. 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)];
  244. [request setHTTPBody:[body dataUsingEncoding:NSUTF8StringEncoding]];
  245. [request setValue:@"application/xml" forHTTPHeaderField:@"Content-Type"];
  246. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication withUserSessionToken:token success:success failure:failure];
  247. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  248. [operation resume];
  249. }
  250. - (void)listingFavorites:(NSString *)path folder:(NSString *)folder user:(NSString *)user onCommunication:(OCCommunication *)sharedOCCommunication withUserSessionToken:(NSString *)token success:(void(^)(NSHTTPURLResponse *, id, NSString *token))success failure:(void(^)(NSHTTPURLResponse *, id _Nullable responseObject, NSError *, NSString *token))failure {
  251. NSString *body;
  252. NSParameterAssert(success);
  253. _requestMethod = @"REPORT";
  254. //REPORT remote.php/dav/files/user/path/to/folder
  255. path = [NSString stringWithFormat:@"%@/files/%@%@", path, user, folder];
  256. NSMutableURLRequest *request = [self requestWithMethod:_requestMethod path:path parameters:nil];
  257. 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>";
  258. // OCFileDto
  259. body = [body stringByAppendingString:@"<d:resourcetype/><oc:fileid/><d:getcontenttype/><d:getetag/><d:creationdate/><oc:size/><d:getcontentlength/><d:getlastmodified/><oc:id/><oc:permissions/><d:quota-available-bytes/><d:quota-used-bytes/><oc:favorite/>"];
  260. body = [NSString stringWithFormat:@"%@</d:prop></oc:filter-files>", body];
  261. [request setHTTPBody:[body dataUsingEncoding:NSUTF8StringEncoding]];
  262. [request setValue:@"text/xml" forHTTPHeaderField:@"Content-Type"];
  263. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication withUserSessionToken:token success:success failure:failure];
  264. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  265. [operation resume];
  266. }
  267. - (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{
  268. NSMutableURLRequest *request = [self requestWithMethod:@"GET" path:remoteSource parameters:nil];
  269. //If is not nil is a redirection so we keep the original url server
  270. if (!self.originalUrlServer) {
  271. self.originalUrlServer = [request.URL absoluteString];
  272. }
  273. //We add the cookies of that URL
  274. request = [UtilsFramework getRequestWithCookiesByRequest:request andOriginalUrlServer:self.originalUrlServer];
  275. NSURL *localDestinationUrl = [NSURL fileURLWithPath:localDestination];
  276. NSURLSessionDownloadTask *downloadTask = [sharedOCCommunication.downloadSessionManager downloadTaskWithRequest:request progress:^(NSProgress * _Nonnull progress) {
  277. downloadProgress(progress);
  278. } destination:^NSURL * _Nonnull(NSURL * _Nonnull targetPath, NSURLResponse * _Nonnull response) {
  279. return localDestinationUrl;
  280. } completionHandler:^(NSURLResponse * _Nonnull response, NSURL * _Nullable filePath, NSError * _Nullable error) {
  281. if (error) {
  282. failure(response, error);
  283. } else {
  284. success(response,filePath);
  285. }
  286. }];
  287. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.downloadSessionManager];
  288. if (defaultPriority) {
  289. [downloadTask resume];
  290. }
  291. return downloadTask;
  292. }
  293. - (void)checkServer:(NSString *)path onCommunication:
  294. (OCCommunication *)sharedOCCommunication
  295. success:(void(^)(NSHTTPURLResponse *, id))success
  296. failure:(void(^)(NSHTTPURLResponse *, id _Nullable responseObject, NSError *))failure {
  297. _requestMethod = @"HEAD";
  298. NSMutableURLRequest *request = [self requestWithMethod:_requestMethod path:path parameters:nil];
  299. request.HTTPShouldHandleCookies = false;
  300. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  301. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  302. [operation resume];
  303. }
  304. - (void)makeCollection:(NSString *)path onCommunication:
  305. (OCCommunication *)sharedOCCommunication
  306. success:(void(^)(NSHTTPURLResponse *, id))success
  307. failure:(void(^)(NSHTTPURLResponse *, id _Nullable responseObject, NSError *))failure {
  308. _requestMethod = @"MKCOL";
  309. NSMutableURLRequest *request = [self requestWithMethod:_requestMethod path:path parameters:nil];
  310. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  311. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  312. [operation resume];
  313. }
  314. - (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 {
  315. NSFileManager *fileManager = [NSFileManager defaultManager];
  316. if (localSource == nil || ![fileManager fileExistsAtPath:localSource]) {
  317. NSMutableDictionary* details = [NSMutableDictionary dictionary];
  318. [details setValue:@"You are trying upload a file that does not exist" forKey:NSLocalizedDescriptionKey];
  319. NSError *error = [NSError errorWithDomain:k_domain_error_code code:OCErrorFileToUploadDoesNotExist userInfo:details];
  320. failureBeforeRequest(error);
  321. return nil;
  322. } else {
  323. NSMutableURLRequest *request = [self requestWithMethod:@"PUT" path:remoteDestination parameters:nil];
  324. [request setTimeoutInterval:k_timeout_upload];
  325. [request setValue:[NSString stringWithFormat:@"%lld", [UtilsFramework getSizeInBytesByPath:localSource]] forHTTPHeaderField:@"Content-Length"];
  326. [request setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData];
  327. //If is not nil is a redirection so we keep the original url server
  328. if (!self.originalUrlServer) {
  329. self.originalUrlServer = [request.URL absoluteString];
  330. }
  331. if (sharedOCCommunication.isCookiesAvailable) {
  332. //We add the cookies of that URL
  333. request = [UtilsFramework getRequestWithCookiesByRequest:request andOriginalUrlServer:self.originalUrlServer];
  334. } else {
  335. [UtilsFramework deleteAllCookies];
  336. }
  337. NSURL *file = [NSURL fileURLWithPath:localSource];
  338. sharedOCCommunication.uploadSessionManager.responseSerializer = [AFHTTPResponseSerializer serializer];
  339. NSURLSessionUploadTask *uploadTask = [sharedOCCommunication.uploadSessionManager uploadTaskWithRequest:request fromFile:file progress:^(NSProgress * _Nonnull progress) {
  340. uploadProgress(progress);
  341. } completionHandler:^(NSURLResponse * _Nonnull response, id _Nullable responseObject, NSError * _Nullable error) {
  342. if (error) {
  343. failure(response, responseObject, error);
  344. } else {
  345. success(response,responseObject);
  346. }
  347. }];
  348. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.uploadSessionManager];
  349. [uploadTask resume];
  350. return uploadTask;
  351. }
  352. }
  353. - (void) requestUserNameOfServer:(NSString * _Nonnull) path byCookie:(NSString * _Nonnull) cookieString onCommunication:
  354. (OCCommunication * _Nonnull)sharedOCCommunication success:(void(^ _Nonnull)(NSHTTPURLResponse * _Nonnull, id _Nonnull))success
  355. failure:(void(^ _Nonnull)(NSHTTPURLResponse * _Nonnull, id _Nullable responseObject, NSError * _Nonnull))failure {
  356. NSString *apiUserUrl = nil;
  357. apiUserUrl = [NSString stringWithFormat:@"%@%@", path, k_api_user_url_json];
  358. NSLog(@"api user name call: %@", apiUserUrl);
  359. _requestMethod = @"GET";
  360. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path: apiUserUrl parameters: nil];
  361. [request setValue:@"application/xml" forHTTPHeaderField:@"Content-Type"];
  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) getStatusOfTheServer:(NSString *)serverPath onCommunication:
  367. (OCCommunication *)sharedOCCommunication success:(void(^)(NSHTTPURLResponse *operation, id responseObject))success
  368. failure:(void(^)(NSHTTPURLResponse *operation, id _Nullable responseObject, NSError *error))failure {
  369. NSString *urlString = [NSString stringWithFormat:@"%@%@", serverPath, k_server_information_json];
  370. _requestMethod = @"GET";
  371. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path: urlString parameters: nil];
  372. request.HTTPShouldHandleCookies = false;
  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
  378. onCommunication:(OCCommunication *)sharedOCCommunication
  379. success:(void(^)(NSHTTPURLResponse *, id))success
  380. failure:(void(^)(NSHTTPURLResponse *, id _Nullable responseObject, NSError *))failure {
  381. NSParameterAssert(success);
  382. _requestMethod = @"GET";
  383. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil];
  384. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  385. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  386. [operation resume];
  387. }
  388. - (void)listSharedByServer:(NSString *)serverPath andPath:(NSString *) path
  389. onCommunication:(OCCommunication *)sharedOCCommunication
  390. success:(void(^)(NSHTTPURLResponse *, id))success
  391. failure:(void(^)(NSHTTPURLResponse *, id _Nullable responseObject, NSError *))failure {
  392. NSParameterAssert(success);
  393. NSString *postString = [NSString stringWithFormat: @"?path=%@&subfiles=true",path];
  394. serverPath = [serverPath stringByAppendingString:postString];
  395. _requestMethod = @"GET";
  396. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil];
  397. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  398. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  399. [operation resume];
  400. }
  401. - (void)shareByLinkFileOrFolderByServer:(NSString *)serverPath andPath:(NSString *) filePath andPassword:(NSString *)password
  402. onCommunication:(OCCommunication *)sharedOCCommunication
  403. success:(void(^)(NSHTTPURLResponse *, id))success
  404. failure:(void(^)(NSHTTPURLResponse *, id _Nullable responseObject, NSError *))failure {
  405. NSParameterAssert(success);
  406. _requestMethod = @"POST";
  407. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil];
  408. _postStringForShare = [NSString stringWithFormat: @"path=%@&shareType=3&password=%@",filePath,password];
  409. [request setHTTPBody:[_postStringForShare dataUsingEncoding:NSUTF8StringEncoding]];
  410. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  411. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  412. [operation resume];
  413. }
  414. - (void)shareByLinkFileOrFolderByServer:(NSString *)serverPath andPath:(NSString *) filePath
  415. onCommunication:(OCCommunication *)sharedOCCommunication
  416. success:(void(^)(NSHTTPURLResponse *, id))success
  417. failure:(void(^)(NSHTTPURLResponse *, id _Nullable responseObject, NSError *))failure {
  418. NSParameterAssert(success);
  419. _requestMethod = @"POST";
  420. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil];
  421. _postStringForShare = [NSString stringWithFormat: @"path=%@&shareType=3",filePath];
  422. [request setHTTPBody:[_postStringForShare dataUsingEncoding:NSUTF8StringEncoding]];
  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)shareWith:(NSString *)userOrGroup shareeType:(NSInteger)shareeType inServer:(NSString *) serverPath andPath:(NSString *) filePath andPermissions:(NSInteger) permissions onCommunication:(OCCommunication *)sharedOCCommunication
  428. success:(void(^)(NSHTTPURLResponse *, id))success
  429. failure:(void(^)(NSHTTPURLResponse *, id _Nullable responseObject, NSError *))failure {
  430. NSParameterAssert(success);
  431. _requestMethod = @"POST";
  432. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil];
  433. self.postStringForShare = [NSString stringWithFormat: @"path=%@&shareType=%ld&shareWith=%@&permissions=%ld",filePath, (long)shareeType, userOrGroup, (long)permissions];
  434. [request setHTTPBody:[_postStringForShare dataUsingEncoding:NSUTF8StringEncoding]];
  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)unShareFileOrFolderByServer:(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 = @"DELETE";
  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)isShareFileOrFolderByServer:(NSString *)serverPath
  451. onCommunication:(OCCommunication *)sharedOCCommunication
  452. success:(void(^)(NSHTTPURLResponse *, id))success
  453. failure:(void(^)(NSHTTPURLResponse *, id _Nullable responseObject, NSError *))failure {
  454. NSParameterAssert(success);
  455. _requestMethod = @"GET";
  456. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil];
  457. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  458. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  459. [operation resume];
  460. }
  461. - (void) updateShareItem:(NSInteger)shareId ofServerPath:(NSString*)serverPath withPasswordProtect:(NSString*)password andExpirationTime:(NSString*)expirationTime andPermissions:(NSInteger)permissions
  462. onCommunication:(OCCommunication *)sharedOCCommunication
  463. success:(void(^)(NSHTTPURLResponse *, id response))success
  464. failure:(void(^)(NSHTTPURLResponse *, id _Nullable responseObject, NSError *error))failure{
  465. NSParameterAssert(success);
  466. _requestMethod = @"PUT";
  467. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil];
  468. if (password) {
  469. self.postStringForShare = [NSString stringWithFormat:@"password=%@",password];
  470. } else if (expirationTime) {
  471. self.postStringForShare = [NSString stringWithFormat:@"expireDate=%@",expirationTime];
  472. }else if (permissions > 0) {
  473. self.postStringForShare = [NSString stringWithFormat:@"permissions=%ld",(long)permissions];
  474. }
  475. [request setHTTPBody:[_postStringForShare dataUsingEncoding:NSUTF8StringEncoding]];
  476. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  477. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  478. [operation resume];
  479. }
  480. - (void) searchUsersAndGroupsWith:(NSString *)searchString forPage:(NSInteger)page with:(NSInteger)resultsPerPage ofServer:(NSString*)serverPath onCommunication:(OCCommunication *)sharedOCCommunication success:(void(^)(NSHTTPURLResponse *operation, id response))success
  481. failure:(void(^)(NSHTTPURLResponse *operation, id _Nullable responseObject, NSError *error))failure {
  482. NSParameterAssert(success);
  483. _requestMethod = @"GET";
  484. NSString *searchQuery = [NSString stringWithFormat: @"&search=%@",searchString];
  485. NSString *jsonQuery = [NSString stringWithFormat:@"?format=json"];
  486. NSString *queryType = [NSString stringWithFormat:@"&itemType=file"];
  487. NSString *pagination = [NSString stringWithFormat:@"&page=%ld&perPage=%ld", (long)page, (long)resultsPerPage];
  488. serverPath = [serverPath stringByAppendingString:jsonQuery];
  489. serverPath = [serverPath stringByAppendingString:queryType];
  490. serverPath = [serverPath stringByAppendingString:searchQuery];
  491. serverPath = [serverPath stringByAppendingString:pagination];
  492. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil];
  493. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  494. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  495. [operation resume];
  496. }
  497. - (void) getCapabilitiesOfServer:(NSString*)serverPath onCommunication:(OCCommunication *)sharedOCCommunication success:(void(^)(NSHTTPURLResponse *operation, id response))success
  498. failure:(void(^)(NSHTTPURLResponse *operation, id _Nullable responseObject, NSError *error))failure{
  499. _requestMethod = @"GET";
  500. NSString *jsonQuery = [NSString stringWithFormat:@"?format=json"];
  501. serverPath = [serverPath stringByAppendingString:jsonQuery];
  502. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil];
  503. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  504. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  505. [operation resume];
  506. }
  507. #pragma mark - Remote thumbnails
  508. - (OCHTTPRequestOperation *) getRemoteThumbnailByServer:(NSString*)serverPath ofFilePath:(NSString*)filePath withWidth:(NSInteger)fileWidth andHeight:(NSInteger)fileHeight onCommunication:(OCCommunication *)sharedOCCommunication
  509. success:(void(^)(NSHTTPURLResponse *operation, id response))success
  510. failure:(void(^)(NSHTTPURLResponse *operation, id _Nullable responseObject, NSError *error))failure{
  511. _requestMethod = @"GET";
  512. NSString *query = [NSString stringWithFormat:@"/%i/%i/%@", (int)fileWidth, (int)fileHeight, filePath];
  513. serverPath = [serverPath stringByAppendingString:query];
  514. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil];
  515. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  516. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  517. return operation;
  518. }
  519. #pragma mark - Get Notification
  520. - (void)getNotificationServer:(NSString*)serverPath onCommunication:(OCCommunication *)sharedOCCommunication success:(void(^)(NSHTTPURLResponse *operation, id response))success
  521. failure:(void(^)(NSHTTPURLResponse *operation, id _Nullable responseObject, NSError *error))failure{
  522. _requestMethod = @"GET";
  523. NSString *jsonQuery = [NSString stringWithFormat:@"?format=json"];
  524. serverPath = [serverPath stringByAppendingString:jsonQuery];
  525. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil];
  526. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  527. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  528. [operation resume];
  529. }
  530. - (void)setNotificationServer:(NSString *)serverPath type:(NSString *)type onCommunication:(OCCommunication *)sharedOCCommunication success:(void(^)(NSHTTPURLResponse *, id))success failure:(void(^)(NSHTTPURLResponse *, id _Nullable responseObject, NSError *))failure {
  531. NSParameterAssert(success);
  532. _requestMethod = type;
  533. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil];
  534. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  535. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  536. [operation resume];
  537. }
  538. - (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 {
  539. NSParameterAssert(success);
  540. _requestMethod = @"POST";
  541. NSString *pushTokenHashParam = [NSString stringWithFormat:@"?pushTokenHash=%@",pushTokenHash];
  542. NSString *devicePublicKeyParam = [NSString stringWithFormat:@"&devicePublicKey=%@",devicePublicKey];
  543. NSString *proxyServerPathParam = [NSString stringWithFormat:@"&proxyServer=%@",proxyServerPath];
  544. serverPath = [serverPath stringByAppendingString:pushTokenHashParam];
  545. serverPath = [serverPath stringByAppendingString:devicePublicKeyParam];
  546. serverPath = [serverPath stringByAppendingString:proxyServerPathParam];
  547. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil];
  548. [request setValue:[NSString stringWithFormat:@"token %@", authorizationToken] forHTTPHeaderField:@"Authorization"];
  549. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  550. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  551. [operation resume];
  552. }
  553. - (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 {
  554. NSParameterAssert(success);
  555. _requestMethod = @"POST";
  556. pushToken = [NSString stringWithFormat:@"?pushToken=%@",pushToken];
  557. deviceIdentifier = [NSString stringWithFormat:@"&deviceIdentifier=%@",deviceIdentifier];
  558. deviceIdentifierSignature = [NSString stringWithFormat:@"&deviceIdentifierSignature=%@",deviceIdentifierSignature];
  559. userPublicKey = [NSString stringWithFormat:@"&userPublicKey=%@",userPublicKey];
  560. serverPath = [serverPath stringByAppendingString:pushToken];
  561. serverPath = [serverPath stringByAppendingString:deviceIdentifier];
  562. serverPath = [serverPath stringByAppendingString:deviceIdentifierSignature];
  563. serverPath = [serverPath stringByAppendingString:userPublicKey];
  564. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil];
  565. [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
  566. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  567. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  568. [operation resume];
  569. }
  570. #pragma mark - Get Activity
  571. - (void) getActivityServer:(NSString*)serverPath onCommunication:(OCCommunication *)sharedOCCommunication success:(void(^)(NSHTTPURLResponse *operation, id response))success
  572. failure:(void(^)(NSHTTPURLResponse *operation, id _Nullable responseObject, NSError *error))failure{
  573. _requestMethod = @"GET";
  574. NSString *jsonQuery = [NSString stringWithFormat:@"?format=json"];
  575. //NSString *startParamater = [NSString stringWithFormat:@"&start=%@", start];
  576. serverPath = [serverPath stringByAppendingString:jsonQuery];
  577. //serverPath = [serverPath stringByAppendingString:startParamater];
  578. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil];
  579. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  580. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  581. [operation resume];
  582. }
  583. #pragma mark - Get External sites
  584. - (void) getExternalSitesServer:(NSString*)serverPath onCommunication:(OCCommunication *)sharedOCCommunication success:(void(^)(NSHTTPURLResponse *operation, id response))success
  585. failure:(void(^)(NSHTTPURLResponse *operation, id _Nullable responseObject, NSError *error))failure{
  586. _requestMethod = @"GET";
  587. NSString *jsonQuery = [NSString stringWithFormat:@"?format=json"];
  588. serverPath = [serverPath stringByAppendingString:jsonQuery];
  589. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil];
  590. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  591. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  592. [operation resume];
  593. }
  594. #pragma mark - Get User Profile
  595. - (void) getUserProfileServer:(NSString*)serverPath onCommunication:(OCCommunication *)sharedOCCommunication success:(void(^)(NSHTTPURLResponse *operation, id response))success
  596. failure:(void(^)(NSHTTPURLResponse *operation, id _Nullable responseObject, NSError *error))failure{
  597. _requestMethod = @"GET";
  598. NSString *jsonQuery = [NSString stringWithFormat:@"?format=json"];
  599. serverPath = [serverPath stringByAppendingString:jsonQuery];
  600. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil];
  601. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  602. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  603. [operation resume];
  604. }
  605. #pragma mark - Manage Redirections
  606. - (void) setRedirectionBlockOnDatataskWithOCCommunication: (OCCommunication *) sharedOCCommunication andSessionManager:(AFURLSessionManager *) sessionManager{
  607. [sessionManager setTaskWillPerformHTTPRedirectionBlock:^NSURLRequest * _Nonnull(NSURLSession * _Nonnull session, NSURLSessionTask * _Nonnull task, NSURLResponse * _Nonnull response, NSURLRequest * _Nonnull request) {
  608. if (response == nil) {
  609. // needed to handle fake redirects to canonical addresses, as explained in https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/URLLoadingSystem/Articles/RequestChanges.html
  610. return request;
  611. }
  612. NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *) response;
  613. NSDictionary *dict = [httpResponse allHeaderFields];
  614. //Server path of redirected server
  615. NSString *responseURLString = [dict objectForKey:@"Location"];
  616. if (responseURLString) {
  617. if ([UtilsFramework isURLWithSamlFragment:responseURLString] || httpResponse.statusCode == k_redirected_code_1) {
  618. //We set the redirectedServer in case SAML or is a permanent redirection
  619. self.redirectedServer = responseURLString;
  620. if ([UtilsFramework isURLWithSamlFragment:responseURLString]) {
  621. // if SAML request, we don't want to follow it; WebView takes care, not here -> nil to NO FOLLOW
  622. return nil;
  623. }
  624. }
  625. NSMutableURLRequest *requestRedirect = [request mutableCopy];
  626. [requestRedirect setURL: [NSURL URLWithString:responseURLString]];
  627. requestRedirect = [sharedOCCommunication getRequestWithCredentials:requestRedirect];
  628. requestRedirect.HTTPMethod = _requestMethod;
  629. if (_postStringForShare) {
  630. //It is a request to share a file by link
  631. requestRedirect = [self sharedRequestWithMethod:_requestMethod path:responseURLString parameters:nil];
  632. [requestRedirect setHTTPBody:[_postStringForShare dataUsingEncoding:NSUTF8StringEncoding]];
  633. }
  634. return requestRedirect;
  635. } else {
  636. // no location to redirect -> nil to NO FOLLOW
  637. return nil;
  638. }
  639. }];
  640. }
  641. @end