暂无描述

RNFetchBlobReqBuilder.m 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. //
  2. // RNFetchBlobReqBuilder.m
  3. // RNFetchBlob
  4. //
  5. // Created by Ben Hsieh on 2016/7/9.
  6. // Copyright © 2016年 wkh237. All rights reserved.
  7. //
  8. #import <Foundation/Foundation.h>
  9. #import "RNFetchBlobReqBuilder.h"
  10. #import "RNFetchBlobNetwork.h"
  11. #import "RNFetchBlobConst.h"
  12. #import "RNFetchBlobFS.h"
  13. #import "IOS7Polyfill.h"
  14. #if __has_include(<React/RCTAssert.h>)
  15. #import <React/RCTLog.h>
  16. #else
  17. #import "RCTLog.h"
  18. #endif
  19. @interface RNFetchBlobReqBuilder()
  20. {
  21. }
  22. @end
  23. @implementation RNFetchBlobReqBuilder
  24. // Fetch blob data request
  25. +(void) buildMultipartRequest:(NSDictionary *)options
  26. taskId:(NSString *)taskId
  27. method:(NSString *)method
  28. url:(NSString *)url
  29. headers:(NSDictionary *)headers
  30. form:(NSArray *)form
  31. onComplete:(void(^)(NSURLRequest * req, long bodyLength))onComplete
  32. {
  33. // NSString * encodedUrl = [url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
  34. NSString * encodedUrl = url;
  35. // send request
  36. __block NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString: encodedUrl]];
  37. __block NSMutableDictionary *mheaders = [[NSMutableDictionary alloc] initWithDictionary:[RNFetchBlobNetwork normalizeHeaders:headers]];
  38. NSTimeInterval timeStamp = [[NSDate date] timeIntervalSince1970];
  39. NSNumber * timeStampObj = [NSNumber numberWithDouble: timeStamp];
  40. // generate boundary
  41. __block NSString * boundary = [NSString stringWithFormat:@"RNFetchBlob%d", timeStampObj];
  42. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  43. __block NSMutableData * postData = [[NSMutableData alloc] init];
  44. // combine multipart/form-data body
  45. [[self class] buildFormBody:form boundary:boundary onComplete:^(NSData *formData, BOOL hasError) {
  46. if(hasError)
  47. {
  48. onComplete(nil, nil);
  49. }
  50. else
  51. {
  52. if(formData != nil)
  53. {
  54. [postData appendData:formData];
  55. // close form data
  56. [postData appendData: [[NSString stringWithFormat:@"--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
  57. [request setHTTPBody:postData];
  58. }
  59. // set content-length
  60. [mheaders setValue:[NSString stringWithFormat:@"%lu",[postData length]] forKey:@"Content-Length"];
  61. [mheaders setValue:@"100-continue" forKey:@"Expect"];
  62. // appaned boundary to content-type
  63. [mheaders setValue:[NSString stringWithFormat:@"multipart/form-data; charset=utf-8; boundary=%@", boundary] forKey:@"content-type"];
  64. [request setHTTPMethod: method];
  65. [request setAllHTTPHeaderFields:mheaders];
  66. onComplete(request, [formData length]);
  67. }
  68. }];
  69. });
  70. }
  71. // Fetch blob data request
  72. +(void) buildOctetRequest:(NSDictionary *)options
  73. taskId:(NSString *)taskId
  74. method:(NSString *)method
  75. url:(NSString *)url
  76. headers:(NSDictionary *)headers
  77. body:(NSString *)body
  78. onComplete:(void(^)(__weak NSURLRequest * req, long bodyLength))onComplete
  79. {
  80. // NSString * encodedUrl = [url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
  81. NSString * encodedUrl = url;
  82. // send request
  83. __block NSMutableURLRequest *request = [[NSMutableURLRequest alloc]
  84. initWithURL:[NSURL URLWithString: encodedUrl]];
  85. __block NSMutableDictionary *mheaders = [[NSMutableDictionary alloc] initWithDictionary:[RNFetchBlobNetwork normalizeHeaders:headers]];
  86. // move heavy task to another thread
  87. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  88. NSMutableData * blobData;
  89. long size = -1;
  90. // if method is POST, PUT or PATCH, convert data string format
  91. if([[method lowercaseString] isEqualToString:@"post"] || [[method lowercaseString] isEqualToString:@"put"] || [[method lowercaseString] isEqualToString:@"patch"]) {
  92. // generate octet-stream body
  93. if(body != nil) {
  94. __block NSString * cType = [[self class] getHeaderIgnoreCases:@"content-type" fromHeaders:mheaders];
  95. __block NSString * transferEncoding = [[self class] getHeaderIgnoreCases:@"transfer-encoding" fromHeaders:mheaders];
  96. // when headers does not contain a key named "content-type" (case ignored), use default content type
  97. if(cType == nil)
  98. {
  99. [mheaders setValue:@"application/octet-stream" forKey:@"Content-Type"];
  100. }
  101. // when body is a string contains file path prefix, try load file from the path
  102. if([body hasPrefix:FILE_PREFIX]) {
  103. __block NSString * orgPath = [body substringFromIndex:[FILE_PREFIX length]];
  104. orgPath = [RNFetchBlobFS getPathOfAsset:orgPath];
  105. if([orgPath hasPrefix:AL_PREFIX])
  106. {
  107. [RNFetchBlobFS readFile:orgPath encoding:nil onComplete:^(NSData *content, NSString * err) {
  108. if(err != nil)
  109. {
  110. onComplete(nil, nil);
  111. }
  112. else
  113. {
  114. [request setHTTPBody:content];
  115. [request setHTTPMethod: method];
  116. [request setAllHTTPHeaderFields:mheaders];
  117. onComplete(request, [content length]);
  118. }
  119. }];
  120. return;
  121. }
  122. size = [[[NSFileManager defaultManager] attributesOfItemAtPath:orgPath error:nil] fileSize];
  123. if(transferEncoding != nil && [[transferEncoding lowercaseString] isEqualToString:@"chunked"])
  124. {
  125. [request setHTTPBodyStream: [NSInputStream inputStreamWithFileAtPath:orgPath ]];
  126. }
  127. else
  128. {
  129. __block NSData * bodyBytes = [NSData dataWithContentsOfFile:orgPath ];
  130. [request setHTTPBody:bodyBytes];
  131. }
  132. }
  133. // otherwise convert it as BASE64 data string
  134. else {
  135. __block NSString * cType = [[self class]getHeaderIgnoreCases:@"content-type" fromHeaders:mheaders];
  136. // when content-type is application/octet* decode body string using BASE64 decoder
  137. if([[cType lowercaseString] hasPrefix:@"application/octet"] || [[cType lowercaseString] RNFBContainsString:@";base64"])
  138. {
  139. __block NSString * ncType = [[cType stringByReplacingOccurrencesOfString:@";base64" withString:@""]stringByReplacingOccurrencesOfString:@";BASE64" withString:@""];
  140. if([mheaders valueForKey:@"content-type"] != nil)
  141. [mheaders setValue:ncType forKey:@"content-type"];
  142. if([mheaders valueForKey:@"Content-Type"] != nil)
  143. [mheaders setValue:ncType forKey:@"Content-Type"];
  144. blobData = [[NSData alloc] initWithBase64EncodedString:body options:0];
  145. [request setHTTPBody:blobData];
  146. size = [blobData length];
  147. }
  148. // otherwise use the body as-is
  149. else
  150. {
  151. size = [body length];
  152. [request setHTTPBody: [body dataUsingEncoding:NSUTF8StringEncoding]];
  153. }
  154. }
  155. }
  156. }
  157. [request setHTTPMethod: method];
  158. [request setAllHTTPHeaderFields:mheaders];
  159. onComplete(request, size);
  160. });
  161. }
  162. +(void) buildFormBody:(NSArray *)form boundary:(NSString *)boundary onComplete:(void(^)(NSData * formData, BOOL hasError))onComplete
  163. {
  164. __block NSMutableData * formData = [[NSMutableData alloc] init];
  165. if(form == nil)
  166. onComplete(nil, NO);
  167. else
  168. {
  169. __block int i = 0;
  170. __block int count = [form count];
  171. // a recursive block that builds multipart body asynchornously
  172. void __block (^getFieldData)(id field) = ^(id field)
  173. {
  174. NSString * name = [field valueForKey:@"name"];
  175. __block NSString * content = [field valueForKey:@"data"];
  176. NSString * contentType = [field valueForKey:@"type"];
  177. // skip when the form field `name` or `data` is empty
  178. if(content == nil || name == nil)
  179. {
  180. i++;
  181. getFieldData([form objectAtIndex:i]);
  182. RCTLogWarn(@"RNFetchBlob multipart request builder has found a field without `data` or `name` property, the field will be removed implicitly.");
  183. return;
  184. }
  185. // field is a text field
  186. if([field valueForKey:@"filename"] == nil || content == nil) {
  187. contentType = contentType == nil ? @"text/plain" : contentType;
  188. [formData appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
  189. [formData appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"\r\n", name] dataUsingEncoding:NSUTF8StringEncoding]];
  190. [formData appendData:[[NSString stringWithFormat:@"Content-Type: %@\r\n\r\n", contentType] dataUsingEncoding:NSUTF8StringEncoding]];
  191. [formData appendData:[[NSString stringWithFormat:@"%@\r\n", content] dataUsingEncoding:NSUTF8StringEncoding]];
  192. }
  193. // field contains a file
  194. else {
  195. contentType = contentType == nil ? @"application/octet-stream" : contentType;
  196. NSMutableData * blobData;
  197. if(content != nil)
  198. {
  199. // append data from file asynchronously
  200. if([content hasPrefix:FILE_PREFIX])
  201. {
  202. NSString * orgPath = [content substringFromIndex:[FILE_PREFIX length]];
  203. orgPath = [RNFetchBlobFS getPathOfAsset:orgPath];
  204. [RNFetchBlobFS readFile:orgPath encoding:nil onComplete:^(NSData *content, NSString * err) {
  205. if(err != nil)
  206. {
  207. onComplete(formData, YES);
  208. return;
  209. }
  210. NSString * filename = [field valueForKey:@"filename"];
  211. [formData appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
  212. [formData appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"; filename=\"%@\"\r\n", name, filename] dataUsingEncoding:NSUTF8StringEncoding]];
  213. [formData appendData:[[NSString stringWithFormat:@"Content-Type: %@\r\n\r\n", contentType] dataUsingEncoding:NSUTF8StringEncoding]];
  214. [formData appendData:content];
  215. [formData appendData:[[NSString stringWithFormat:@"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
  216. i++;
  217. if(i < count)
  218. {
  219. __block NSDictionary * nextField = [form objectAtIndex:i];
  220. getFieldData(nextField);
  221. }
  222. else
  223. {
  224. onComplete(formData, NO);
  225. getFieldData = nil;
  226. }
  227. }];
  228. return ;
  229. }
  230. else
  231. blobData = [[NSData alloc] initWithBase64EncodedString:content options:0];
  232. }
  233. NSString * filename = [field valueForKey:@"filename"];
  234. [formData appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
  235. [formData appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"; filename=\"%@\"\r\n", name, filename] dataUsingEncoding:NSUTF8StringEncoding]];
  236. [formData appendData:[[NSString stringWithFormat:@"Content-Type: %@\r\n\r\n", contentType] dataUsingEncoding:NSUTF8StringEncoding]];
  237. [formData appendData:blobData];
  238. [formData appendData:[[NSString stringWithFormat:@"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
  239. blobData = nil;
  240. }
  241. i++;
  242. if(i < count)
  243. {
  244. __block NSDictionary * nextField = [form objectAtIndex:i];
  245. getFieldData(nextField);
  246. }
  247. else
  248. {
  249. onComplete(formData, NO);
  250. getFieldData = nil;
  251. }
  252. };
  253. __block NSDictionary * nextField = [form objectAtIndex:i];
  254. getFieldData(nextField);
  255. }
  256. }
  257. +(NSString *) getHeaderIgnoreCases:(NSString *)field fromHeaders:(NSMutableDictionary *) headers {
  258. NSString * normalCase = [headers valueForKey:field];
  259. NSString * ignoredCase = [headers valueForKey:[field lowercaseString]];
  260. if( normalCase != nil)
  261. return normalCase;
  262. else
  263. return ignoredCase;
  264. }
  265. @end