|
@@ -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];
|