浏览代码

Apply fix to #266

Ben Hsieh 7 年前
父节点
当前提交
8c1db27074
共有 6 个文件被更改,包括 222 次插入217 次删除
  1. 55
    23
      ios/RNFetchBlob/RNFetchBlob.m
  2. 1
    1
      ios/RNFetchBlobFS.h
  3. 46
    51
      ios/RNFetchBlobFS.m
  4. 1
    20
      ios/RNFetchBlobNetwork.h
  5. 72
    98
      ios/RNFetchBlobNetwork.m
  6. 47
    24
      ios/RNFetchBlobReqBuilder.m

+ 55
- 23
ios/RNFetchBlob/RNFetchBlob.m 查看文件

@@ -80,10 +80,25 @@ RCT_EXPORT_METHOD(fetchBlobForm:(NSDictionary *)options
80 80
                   callback:(RCTResponseSenderBlock)callback)
81 81
 {
82 82
 
83
-    [RNFetchBlobReqBuilder buildMultipartRequest:options taskId:taskId method:method url:url headers:headers form:form onComplete:^(__weak NSURLRequest *req, long bodyLength) {
83
+    [RNFetchBlobReqBuilder buildMultipartRequest:options
84
+                                          taskId:taskId
85
+                                          method:method
86
+                                             url:url
87
+                                         headers:headers
88
+                                            form:form
89
+                                      onComplete:^(__weak NSURLRequest *req, long bodyLength)
90
+    {
91
+        // something went wrong when building the request body
92
+        if(req == nil)
93
+        {
94
+            callback(@[@"RNFetchBlob.fetchBlobForm failed to create request body"]);
95
+        }
84 96
         // send HTTP request
85
-        RNFetchBlobNetwork * utils = [[RNFetchBlobNetwork alloc] init];
86
-        [utils sendRequest:options contentLength:bodyLength bridge:self.bridge taskId:taskId withRequest:req callback:callback];
97
+        else
98
+        {
99
+            RNFetchBlobNetwork * utils = [[RNFetchBlobNetwork alloc] init];
100
+            [utils sendRequest:options contentLength:bodyLength bridge:self.bridge taskId:taskId withRequest:req callback:callback];
101
+        }
87 102
     }];
88 103
 
89 104
 }
@@ -97,10 +112,25 @@ RCT_EXPORT_METHOD(fetchBlob:(NSDictionary *)options
97 112
                   headers:(NSDictionary *)headers
98 113
                   body:(NSString *)body callback:(RCTResponseSenderBlock)callback)
99 114
 {
100
-    [RNFetchBlobReqBuilder buildOctetRequest:options taskId:taskId method:method url:url headers:headers body:body onComplete:^(NSURLRequest *req, long bodyLength) {
115
+    [RNFetchBlobReqBuilder buildOctetRequest:options
116
+                                      taskId:taskId
117
+                                      method:method
118
+                                         url:url
119
+                                     headers:headers
120
+                                        body:body
121
+                                  onComplete:^(NSURLRequest *req, long bodyLength)
122
+    {
123
+        // something went wrong when building the request body
124
+        if(req == nil)
125
+        {
126
+            callback(@[@"RNFetchBlob.fetchBlob failed to create request body"]);
127
+        }
101 128
         // send HTTP request
102
-        __block RNFetchBlobNetwork * utils = [[RNFetchBlobNetwork alloc] init];
103
-        [utils sendRequest:options contentLength:bodyLength bridge:self.bridge taskId:taskId withRequest:req callback:callback];
129
+        else
130
+        {
131
+            __block RNFetchBlobNetwork * utils = [[RNFetchBlobNetwork alloc] init];
132
+            [utils sendRequest:options contentLength:bodyLength bridge:self.bridge taskId:taskId withRequest:req callback:callback];
133
+        }
104 134
     }];
105 135
 }
106 136
 
@@ -394,10 +424,27 @@ RCT_EXPORT_METHOD(mkdir:(NSString *)path callback:(RCTResponseSenderBlock) callb
394 424
 }
395 425
 
396 426
 #pragma mark - fs.readFile
397
-RCT_EXPORT_METHOD(readFile:(NSString *)path encoding:(NSString *)encoding resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject)
427
+RCT_EXPORT_METHOD(readFile:(NSString *)path
428
+                  encoding:(NSString *)encoding
429
+                  resolver:(RCTPromiseResolveBlock)resolve
430
+                  rejecter:(RCTPromiseRejectBlock)reject)
398 431
 {
399 432
 
400
-    [RNFetchBlobFS readFile:path encoding:encoding resolver:resolve rejecter:reject onComplete:nil];
433
+    [RNFetchBlobFS readFile:path encoding:encoding onComplete:^(id content, NSString * err) {
434
+        if(err != nil)
435
+        {
436
+            reject(@"RNFetchBlob failed to read file", err, nil);
437
+            return;
438
+        }
439
+        if(encoding == @"ascii")
440
+        {
441
+            resolve((NSMutableArray *)content);
442
+        }
443
+        else
444
+        {
445
+            resolve((NSString *)content);
446
+        }
447
+    }];
401 448
 }
402 449
 
403 450
 #pragma mark - fs.readStream
@@ -519,24 +566,11 @@ RCT_EXPORT_METHOD(df:(RCTResponseSenderBlock)callback)
519 566
 }
520 567
 
521 568
 # pragma mark - getCookies
522
-
523 569
 RCT_EXPORT_METHOD(getCookies:(NSString *)url resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject)
524 570
 {
525 571
     resolve([RNFetchBlobNetwork getCookies:url]);
526 572
 }
527 573
 
528
-# pragma mark - removeCookie
529
-
530
-RCT_EXPORT_METHOD(removeCookies:(NSString *)domain resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject)
531
-{
532
-    NSError * err = nil;
533
-    [RNFetchBlobNetwork removeCookies:domain error:&err];
534
-    if(err)
535
-        reject(@"RNFetchBlob failed to remove cookie", @"RNFetchBlob failed to remove cookie", nil);
536
-    else
537
-        resolve(@[[NSNull null]]);
538
-}
539
-
540 574
 # pragma mark - check expired network events
541 575
 
542 576
 RCT_EXPORT_METHOD(emitExpiredEvent:(RCTResponseSenderBlock)callback)
@@ -545,6 +579,4 @@ RCT_EXPORT_METHOD(emitExpiredEvent:(RCTResponseSenderBlock)callback)
545 579
 }
546 580
 
547 581
 
548
-
549
-
550 582
 @end

+ 1
- 1
ios/RNFetchBlobFS.h 查看文件

@@ -61,7 +61,7 @@
61 61
 + (void) exists:(NSString *) path callback:(RCTResponseSenderBlock)callback;
62 62
 + (void) writeFileArray:(NSString *)path data:(NSArray *)data append:(BOOL)append resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject;
63 63
 + (void) writeFile:(NSString *)path encoding:(NSString *)encoding data:(NSString *)data append:(BOOL)append resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject;
64
-+ (void) readFile:(NSString *)path encoding:(NSString *)encoding resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject onComplete:(void (^)(NSData * content))onComplete;
64
++ (void) readFile:(NSString *)path encoding:(NSString *)encoding onComplete:(void (^)(NSData * content, NSString * errMsg))onComplete;
65 65
 + (void) readAssetFile:(NSData *)assetUrl completionBlock:(void(^)(NSData * content))completionBlock failBlock:(void(^)(NSError * err))failBlock;
66 66
 + (void) slice:(NSString *)path
67 67
          dest:(NSString *)dest

+ 46
- 51
ios/RNFetchBlobFS.m 查看文件

@@ -418,54 +418,49 @@ NSMutableDictionary *fileStreams = nil;
418 418
 
419 419
 # pragma mark - read file
420 420
 
421
-+ (void) readFile:(NSString *)path encoding:(NSString *)encoding
422
-         resolver:(RCTPromiseResolveBlock)resolve
423
-         rejecter:(RCTPromiseRejectBlock)reject
424
-       onComplete:(void (^)(NSData * content))onComplete
421
++ (void) readFile:(NSString *)path
422
+         encoding:(NSString *)encoding
423
+       onComplete:(void (^)(id content, NSString * errMsg))onComplete
425 424
 {
426
-    @try
427
-    {
428
-        [[self class] getPathFromUri:path completionHandler:^(NSString *path, ALAssetRepresentation *asset) {
429
-            __block NSData * fileContent;
430
-            NSError * err;
431
-            __block Byte * buffer;
432
-            if(asset != nil)
425
+    [[self class] getPathFromUri:path completionHandler:^(NSString *path, ALAssetRepresentation *asset) {
426
+        __block NSData * fileContent;
427
+        NSError * err;
428
+        __block Byte * buffer;
429
+        if(asset != nil)
430
+        {
431
+            buffer = malloc(asset.size);
432
+            [asset getBytes:buffer fromOffset:0 length:asset.size error:&err];
433
+            if(err != nil)
433 434
             {
434
-                buffer = malloc(asset.size);
435
-                [asset getBytes:buffer fromOffset:0 length:asset.size error:&err];
436
-                if(err != nil)
437
-                {
438
-                    reject(@"RNFetchBlobFS readFile error", @"failed to read asset", [err localizedDescription]);
439
-                    return;
440
-                }
441
-                fileContent = [NSData dataWithBytes:buffer length:asset.size];
435
+                onComplete(nil, [err description]);
442 436
                 free(buffer);
437
+                return;
443 438
             }
444
-            else
445
-            {
446
-                if(![[NSFileManager defaultManager] fileExistsAtPath:path]) {
447
-
448
-                    reject(@"RNFetchBlobFS readFile error", @"file not exists", nil);
449
-                    return;
450
-                }
451
-                fileContent = [NSData dataWithContentsOfFile:path];
452
-
439
+            fileContent = [NSData dataWithBytes:buffer length:asset.size];
440
+            free(buffer);
441
+        }
442
+        else
443
+        {
444
+            if(![[NSFileManager defaultManager] fileExistsAtPath:path]) {
445
+                onComplete(nil, @"file not exists");
446
+                return;
453 447
             }
454
-            if(onComplete != nil)
455
-                onComplete(fileContent);
456
-
457
-            if([[encoding lowercaseString] isEqualToString:@"utf8"]) {
458
-                if(resolve != nil) {
459
-                    NSString * utf8 = [[NSString alloc] initWithData:fileContent encoding:NSUTF8StringEncoding];
460
-                    if(utf8 == nil)
461
-                        resolve([[NSString alloc] initWithData:fileContent encoding:NSISOLatin1StringEncoding]);
462
-                    else
463
-                        resolve(utf8);
464
-                }
448
+            fileContent = [NSData dataWithContentsOfFile:path];
449
+            
450
+        }
451
+        
452
+        if(encoding != nil)
453
+        {
454
+            if([[encoding lowercaseString] isEqualToString:@"utf8"])
455
+            {
456
+                NSString * utf8 = [[NSString alloc] initWithData:fileContent encoding:NSUTF8StringEncoding];
457
+                if(utf8 == nil)
458
+                    onComplete([[NSString alloc] initWithData:fileContent encoding:NSISOLatin1StringEncoding], nil);
459
+                else
460
+                    onComplete(utf8, nil);
465 461
             }
466 462
             else if ([[encoding lowercaseString] isEqualToString:@"base64"]) {
467
-                if(resolve != nil)
468
-                    resolve([fileContent base64EncodedStringWithOptions:0]);
463
+                onComplete([fileContent base64EncodedStringWithOptions:0], nil);
469 464
             }
470 465
             else if ([[encoding lowercaseString] isEqualToString:@"ascii"]) {
471 466
                 NSMutableArray * resultArray = [NSMutableArray array];
@@ -473,18 +468,18 @@ NSMutableDictionary *fileStreams = nil;
473 468
                 for(int i=0;i<[fileContent length];i++) {
474 469
                     [resultArray addObject:[NSNumber numberWithChar:bytes[i]]];
475 470
                 }
476
-                if(resolve != nil)
477
-                    resolve(resultArray);
471
+                onComplete(resultArray, nil);
478 472
             }
479
-        }];
480
-    }
481
-    @catch(NSException * e)
482
-    {
483
-        if(reject != nil)
484
-            reject(@"RNFetchBlobFS readFile error", @"error", [e description]);
485
-    }
473
+        }
474
+        else
475
+        {
476
+            onComplete(fileContent, nil);
477
+        }
478
+        
479
+    }];
486 480
 }
487 481
 
482
+
488 483
 # pragma mark - mkdir
489 484
 
490 485
 + (BOOL) mkdir:(NSString *) path {
@@ -516,7 +511,7 @@ NSMutableDictionary *fileStreams = nil;
516 511
              @"size" : size,
517 512
              @"filename" : filename,
518 513
              @"path" : path,
519
-             @"lastModified" : [NSString stringWithFormat:@"%@", [NSNumber numberWithLong:(time_t) [lastModified timeIntervalSince1970]*1000]],
514
+             @"lastModified" : [NSNumber numberWithLong:(time_t) [lastModified timeIntervalSince1970]*1000],
520 515
              @"type" : isDir ? @"directory" : @"file"
521 516
             };
522 517
 

+ 1
- 20
ios/RNFetchBlobNetwork.h 查看文件

@@ -23,11 +23,6 @@
23 23
 
24 24
 typedef void(^CompletionHander)(NSURL * _Nullable location, NSURLResponse * _Nullable response, NSError * _Nullable error);
25 25
 typedef void(^DataTaskCompletionHander) (NSData * _Nullable resp, NSURLResponse * _Nullable response, NSError * _Nullable error);
26
-typedef NS_ENUM(NSUInteger, ResponseFormat) {
27
-    UTF8,
28
-    BASE64,
29
-    AUTO
30
-};
31 26
 
32 27
 @interface RNFetchBlobNetwork : NSObject  <NSURLSessionDelegate, NSURLSessionTaskDelegate, NSURLSessionDataDelegate>
33 28
 
@@ -43,19 +38,6 @@ typedef NS_ENUM(NSUInteger, ResponseFormat) {
43 38
 @property (strong, nonatomic) CompletionHander fileTaskCompletionHandler;
44 39
 @property (strong, nonatomic) DataTaskCompletionHander dataTaskCompletionHandler;
45 40
 @property (nullable, nonatomic) NSError * error;
46
-@property (nullable, nonatomic) NSMutableArray * redirects;
47
-
48
-@property (nonatomic) BOOL respFile;
49
-@property (nonatomic) BOOL isNewPart;
50
-@property (nonatomic) BOOL isIncrement;
51
-@property (nullable, nonatomic) NSMutableData * partBuffer;
52
-@property (nullable, nonatomic) NSString * destPath;
53
-@property (nullable, nonatomic) NSOutputStream * writeStream;
54
-@property (nonatomic) long bodyLength;
55
-@property (nullable, nonatomic) NSMutableDictionary * respInfo;
56
-@property (nonatomic) NSInteger respStatus;
57
-@property (nonatomic) ResponseFormat responseFormat;
58
-@property ( nonatomic) BOOL followRedirect;
59 41
 
60 42
 
61 43
 + (NSMutableDictionary  * _Nullable ) normalizeHeaders:(NSDictionary * _Nullable)headers;
@@ -67,10 +49,9 @@ typedef NS_ENUM(NSUInteger, ResponseFormat) {
67 49
 - (nullable id) init;
68 50
 - (void) sendRequest;
69 51
 - (void) sendRequest:(NSDictionary  * _Nullable )options contentLength:(long)contentLength bridge:(RCTBridge * _Nullable)bridgeRef taskId:(NSString * _Nullable)taskId withRequest:(NSURLRequest * _Nullable)req callback:(_Nullable RCTResponseSenderBlock) callback;
70
-+ (void) removeCookies:(NSString *) domain error:(NSError **)error;
71 52
 + (void) enableProgressReport:(NSString *) taskId config:(RNFetchBlobProgress *)config;
72 53
 + (void) enableUploadProgress:(NSString *) taskId config:(RNFetchBlobProgress *)config;
73
-+ (NSDictionary *) getCookies:(NSString *) url;
54
++ (NSArray *) getCookies:(NSString *) url;
74 55
 
75 56
 
76 57
 

+ 72
- 98
ios/RNFetchBlobNetwork.m 查看文件

@@ -37,6 +37,7 @@
37 37
 
38 38
 NSMapTable * taskTable;
39 39
 NSMapTable * expirationTable;
40
+NSMapTable * cookiesTable;
40 41
 NSMutableDictionary * progressTable;
41 42
 NSMutableDictionary * uploadProgressTable;
42 43
 
@@ -58,9 +59,37 @@ static void initialize_tables() {
58 59
     {
59 60
         uploadProgressTable = [[NSMutableDictionary alloc] init];
60 61
     }
62
+    if(cookiesTable == nil)
63
+    {
64
+        cookiesTable = [[NSMapTable alloc] init];
65
+    }
61 66
 }
62 67
 
63 68
 
69
+typedef NS_ENUM(NSUInteger, ResponseFormat) {
70
+    UTF8,
71
+    BASE64,
72
+    AUTO
73
+};
74
+
75
+
76
+@interface RNFetchBlobNetwork ()
77
+{
78
+    BOOL * respFile;
79
+    BOOL isNewPart;
80
+    BOOL * isIncrement;
81
+    NSMutableData * partBuffer;
82
+    NSString * destPath;
83
+    NSOutputStream * writeStream;
84
+    long bodyLength;
85
+    NSMutableDictionary * respInfo;
86
+    NSInteger respStatus;
87
+    NSMutableArray * redirects;
88
+    ResponseFormat responseFormat;
89
+    BOOL * followRedirect;
90
+}
91
+
92
+@end
64 93
 
65 94
 @implementation RNFetchBlobNetwork
66 95
 
@@ -72,23 +101,10 @@ NSOperationQueue *taskQueue;
72 101
 @synthesize callback;
73 102
 @synthesize bridge;
74 103
 @synthesize options;
75
-@synthesize redirects;
76 104
 @synthesize fileTaskCompletionHandler;
77 105
 @synthesize dataTaskCompletionHandler;
78 106
 @synthesize error;
79 107
 
80
-@synthesize respFile;
81
-@synthesize isNewPart;
82
-@synthesize isIncrement;
83
-@synthesize partBuffer;
84
-@synthesize destPath;
85
-@synthesize writeStream;
86
-@synthesize bodyLength;
87
-@synthesize respInfo;
88
-@synthesize respStatus;
89
-@synthesize responseFormat;
90
-@synthesize followRedirect;
91
-
92 108
 
93 109
 // constructor
94 110
 - (id)init {
@@ -100,6 +116,48 @@ NSOperationQueue *taskQueue;
100 116
     return self;
101 117
 }
102 118
 
119
++ (NSArray *) getCookies:(NSString *) url
120
+{
121
+    NSString * hostname = [[NSURL URLWithString:url] host];
122
+    NSMutableArray * cookies = [NSMutableArray new];
123
+    NSArray * list = [cookiesTable objectForKey:hostname];
124
+    for(NSHTTPCookie * cookie in list)
125
+    {
126
+        NSMutableString * cookieStr = [[NSMutableString alloc] init];
127
+        [cookieStr appendString:cookie.name];
128
+        [cookieStr appendString:@"="];
129
+        [cookieStr appendString:cookie.value];
130
+
131
+        if(cookie.expiresDate == nil) {
132
+            [cookieStr appendString:@"; max-age=0"];
133
+        }
134
+        else {
135
+            [cookieStr appendString:@"; expires="];
136
+            NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
137
+            [dateFormatter setDateFormat:@"EEE, dd MM yyyy HH:mm:ss ZZZ"];
138
+            NSString *strDate = [dateFormatter stringFromDate:cookie.expiresDate];
139
+            [cookieStr appendString:strDate];
140
+        }
141
+
142
+
143
+        [cookieStr appendString:@"; domain="];
144
+        [cookieStr appendString:hostname];
145
+        [cookieStr appendString:@"; path="];
146
+        [cookieStr appendString:cookie.path];
147
+
148
+
149
+        if (cookie.isSecure) {
150
+            [cookieStr appendString:@"; secure"];
151
+        }
152
+
153
+        if (cookie.isHTTPOnly) {
154
+            [cookieStr appendString:@"; httponly"];
155
+        }
156
+        [cookies addObject:cookieStr];
157
+    }
158
+    return cookies;
159
+}
160
+
103 161
 + (void) enableProgressReport:(NSString *) taskId config:(RNFetchBlobProgress *)config
104 162
 {
105 163
     if(progressTable == nil)
@@ -359,10 +417,9 @@ NSOperationQueue *taskQueue;
359 417
         // # 153 get cookies
360 418
         if(response.URL != nil)
361 419
         {
362
-            NSHTTPCookieStorage * cookieStore = [NSHTTPCookieStorage sharedHTTPCookieStorage];
363 420
             NSArray<NSHTTPCookie *> * cookies = [NSHTTPCookie cookiesWithResponseHeaderFields: headers forURL:response.URL];
364 421
             if(cookies != nil && [cookies count] > 0) {
365
-                [cookieStore setCookies:cookies forURL:response.URL mainDocumentURL:nil];
422
+                [cookiesTable setObject:cookies forKey:response.URL.host];
366 423
             }
367 424
         }
368 425
 
@@ -566,89 +623,6 @@ NSOperationQueue *taskQueue;
566 623
     }
567 624
 }
568 625
 
569
-# pragma mark - cookies handling API
570
-
571
-+ (NSDictionary *) getCookies:(NSString *) domain
572
-{
573
-    NSMutableDictionary * result = [NSMutableDictionary new];
574
-    NSHTTPCookieStorage * cookieStore = [NSHTTPCookieStorage sharedHTTPCookieStorage];
575
-    for(NSHTTPCookie * cookie in [cookieStore cookies])
576
-    {
577
-        NSString * cDomain = [cookie domain];
578
-        if([result objectForKey:cDomain] == nil)
579
-        {
580
-            [result setObject:[NSMutableArray new] forKey:cDomain];
581
-        }
582
-        if([cDomain isEqualToString:domain] || [domain length] == 0)
583
-        {
584
-            NSMutableString * cookieStr = [[NSMutableString alloc] init];
585
-            cookieStr = [[self class] getCookieString:cookie];
586
-            NSMutableArray * ary = [result objectForKey:cDomain];
587
-            [ary addObject:cookieStr];
588
-            [result setObject:ary forKey:cDomain];
589
-        }
590
-    }
591
-    return result;
592
-}
593
-
594
-// remove cookies for given domain, if domain is empty remove all cookies in shared cookie storage.
595
-+ (void) removeCookies:(NSString *) domain error:(NSError **)error
596
-{
597
-    @try
598
-    {
599
-        NSHTTPCookieStorage * cookies = [NSHTTPCookieStorage sharedHTTPCookieStorage];
600
-        for(NSHTTPCookie * cookie in [cookies cookies])
601
-        {
602
-            BOOL shouldRemove = domain == nil || [domain length] < 1 || [[cookie domain] isEqualToString:domain];
603
-            if(shouldRemove)
604
-            {
605
-                [cookies deleteCookie:cookie];
606
-            }
607
-        }
608
-    }
609
-    @catch(NSError * err)
610
-    {
611
-        *error = err;
612
-    }
613
-}
614
-
615
-// convert NSHTTPCookie to string
616
-+ (NSString *) getCookieString:(NSHTTPCookie *) cookie
617
-{
618
-    NSMutableString * cookieStr = [[NSMutableString alloc] init];
619
-    [cookieStr appendString:cookie.name];
620
-    [cookieStr appendString:@"="];
621
-    [cookieStr appendString:cookie.value];
622
-    
623
-    if(cookie.expiresDate == nil) {
624
-        [cookieStr appendString:@"; max-age=0"];
625
-    }
626
-    else {
627
-        [cookieStr appendString:@"; expires="];
628
-        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
629
-        [dateFormatter setDateFormat:@"EEE, dd MM yyyy HH:mm:ss ZZZ"];
630
-        NSString *strDate = [dateFormatter stringFromDate:cookie.expiresDate];
631
-        [cookieStr appendString:strDate];
632
-    }
633
-    
634
-    
635
-    [cookieStr appendString:@"; domain="];
636
-    [cookieStr appendString: [cookie domain]];
637
-    [cookieStr appendString:@"; path="];
638
-    [cookieStr appendString:cookie.path];
639
-    
640
-    
641
-    if (cookie.isSecure) {
642
-        [cookieStr appendString:@"; secure"];
643
-    }
644
-    
645
-    if (cookie.isHTTPOnly) {
646
-        [cookieStr appendString:@"; httponly"];
647
-    }
648
-    return cookieStr;
649
-
650
-}
651
-
652 626
 + (void) cancelRequest:(NSString *)taskId
653 627
 {
654 628
     NSURLSessionDataTask * task = [taskTable objectForKey:taskId];

+ 47
- 24
ios/RNFetchBlobReqBuilder.m 查看文件

@@ -51,21 +51,29 @@
51 51
     dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
52 52
         __block NSMutableData * postData = [[NSMutableData alloc] init];
53 53
         // combine multipart/form-data body
54
-        [[self class] buildFormBody:form boundary:boundary onComplete:^(NSData *formData) {
55
-            if(formData != nil) {
56
-                [postData appendData:formData];
57
-                // close form data
58
-                [postData appendData: [[NSString stringWithFormat:@"--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
59
-                [request setHTTPBody:postData];
54
+        [[self class] buildFormBody:form boundary:boundary onComplete:^(NSData *formData, BOOL hasError) {
55
+            if(hasError)
56
+            {
57
+                onComplete(nil, nil);
58
+            }
59
+            else
60
+            {
61
+                if(formData != nil)
62
+                {
63
+                    [postData appendData:formData];
64
+                    // close form data
65
+                    [postData appendData: [[NSString stringWithFormat:@"--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
66
+                    [request setHTTPBody:postData];
67
+                }
68
+                // set content-length
69
+                [mheaders setValue:[NSString stringWithFormat:@"%lu",[postData length]] forKey:@"Content-Length"];
70
+                [mheaders setValue:@"100-continue" forKey:@"Expect"];
71
+                // appaned boundary to content-type
72
+                [mheaders setValue:[NSString stringWithFormat:@"multipart/form-data; charset=utf-8; boundary=%@", boundary] forKey:@"content-type"];
73
+                [request setHTTPMethod: method];
74
+                [request setAllHTTPHeaderFields:mheaders];
75
+                onComplete(request, [formData length]);
60 76
             }
61
-            // set content-length
62
-            [mheaders setValue:[NSString stringWithFormat:@"%lu",[postData length]] forKey:@"Content-Length"];
63
-            [mheaders setValue:@"100-continue" forKey:@"Expect"];
64
-            // appaned boundary to content-type
65
-            [mheaders setValue:[NSString stringWithFormat:@"multipart/form-data; charset=utf-8; boundary=%@", boundary] forKey:@"content-type"];
66
-            [request setHTTPMethod: method];
67
-            [request setAllHTTPHeaderFields:mheaders];
68
-            onComplete(request, [formData length]);
69 77
         }];
70 78
 
71 79
     });
@@ -109,12 +117,21 @@
109 117
                     orgPath = [RNFetchBlobFS getPathOfAsset:orgPath];
110 118
                     if([orgPath hasPrefix:AL_PREFIX])
111 119
                     {
112
-                        [RNFetchBlobFS readFile:orgPath encoding:@"utf8" resolver:nil rejecter:nil onComplete:^(NSData *content) {
113
-                            [request setHTTPBody:content];
114
-                            [request setHTTPMethod: method];
115
-                            [request setAllHTTPHeaderFields:mheaders];
116
-                            onComplete(request, [content length]);
120
+                        
121
+                        [RNFetchBlobFS readFile:orgPath encoding:nil onComplete:^(NSData *content, NSString * err) {
122
+                            if(err != nil)
123
+                            {
124
+                                onComplete(nil, nil);
125
+                            }
126
+                            else
127
+                            {
128
+                                [request setHTTPBody:content];
129
+                                [request setHTTPMethod: method];
130
+                                [request setAllHTTPHeaderFields:mheaders];
131
+                                onComplete(request, [content length]);
132
+                            }
117 133
                         }];
134
+                        
118 135
                         return;
119 136
                     }
120 137
                     size = [[[NSFileManager defaultManager] attributesOfItemAtPath:orgPath error:nil] fileSize];
@@ -161,11 +178,11 @@
161 178
     });
162 179
 }
163 180
 
164
-+(void) buildFormBody:(NSArray *)form boundary:(NSString *)boundary onComplete:(void(^)(NSData * formData))onComplete
181
++(void) buildFormBody:(NSArray *)form boundary:(NSString *)boundary onComplete:(void(^)(NSData * formData, BOOL hasError))onComplete
165 182
 {
166 183
     __block NSMutableData * formData = [[NSMutableData alloc] init];
167 184
     if(form == nil)
168
-        onComplete(nil);
185
+        onComplete(nil, NO);
169 186
     else
170 187
     {
171 188
         __block int i = 0;
@@ -202,7 +219,13 @@
202 219
                     {
203 220
                         NSString * orgPath = [content substringFromIndex:[FILE_PREFIX length]];
204 221
                         orgPath = [RNFetchBlobFS getPathOfAsset:orgPath];
205
-                        [RNFetchBlobFS readFile:orgPath encoding:@"utf8" resolver:nil rejecter:nil onComplete:^(NSData *content) {
222
+
223
+                        [RNFetchBlobFS readFile:orgPath encoding:nil onComplete:^(NSData *content, NSString * err) {
224
+                            if(err != nil)
225
+                            {
226
+                                onComplete(formData, YES);
227
+                                return;
228
+                            }
206 229
                             NSString * filename = [field valueForKey:@"filename"];
207 230
                             [formData appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
208 231
                             [formData appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"; filename=\"%@\"\r\n", name, filename] dataUsingEncoding:NSUTF8StringEncoding]];
@@ -217,7 +240,7 @@
217 240
                             }
218 241
                             else
219 242
                             {
220
-                                onComplete(formData);
243
+                                onComplete(formData, NO);
221 244
                                 getFieldData = nil;
222 245
                             }
223 246
                         }];
@@ -242,7 +265,7 @@
242 265
             }
243 266
             else
244 267
             {
245
-                onComplete(formData);
268
+                onComplete(formData, NO);
246 269
                 getFieldData = nil;
247 270
             }
248 271