Selaa lähdekoodia

Re-apply iOS removeCookies API (#325)

Max Gurela 7 vuotta sitten
vanhempi
commit
1cc4359469
3 muutettua tiedostoa jossa 102 lisäystä ja 49 poistoa
  1. 15
    0
      ios/RNFetchBlob/RNFetchBlob.m
  2. 2
    1
      ios/RNFetchBlobNetwork.h
  3. 85
    48
      ios/RNFetchBlobNetwork.m

+ 15
- 0
ios/RNFetchBlob/RNFetchBlob.m Näytä tiedosto

@@ -566,11 +566,24 @@ RCT_EXPORT_METHOD(df:(RCTResponseSenderBlock)callback)
566 566
 }
567 567
 
568 568
 # pragma mark - getCookies
569
+
569 570
 RCT_EXPORT_METHOD(getCookies:(NSString *)url resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject)
570 571
 {
571 572
     resolve([RNFetchBlobNetwork getCookies:url]);
572 573
 }
573 574
 
575
+# pragma mark - removeCookie
576
+
577
+RCT_EXPORT_METHOD(removeCookies:(NSString *)domain resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject)
578
+{
579
+    NSError * err = nil;
580
+    [RNFetchBlobNetwork removeCookies:domain error:&err];
581
+    if(err)
582
+        reject(@"RNFetchBlob failed to remove cookie", @"RNFetchBlob failed to remove cookie", nil);
583
+    else
584
+        resolve(@[[NSNull null]]);
585
+}
586
+
574 587
 # pragma mark - check expired network events
575 588
 
576 589
 RCT_EXPORT_METHOD(emitExpiredEvent:(RCTResponseSenderBlock)callback)
@@ -579,4 +592,6 @@ RCT_EXPORT_METHOD(emitExpiredEvent:(RCTResponseSenderBlock)callback)
579 592
 }
580 593
 
581 594
 
595
+
596
+
582 597
 @end

+ 2
- 1
ios/RNFetchBlobNetwork.h Näytä tiedosto

@@ -49,9 +49,10 @@ typedef void(^DataTaskCompletionHander) (NSData * _Nullable resp, NSURLResponse
49 49
 - (nullable id) init;
50 50
 - (void) sendRequest;
51 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;
52
++ (void) removeCookies:(NSString *) domain error:(NSError **)error;
52 53
 + (void) enableProgressReport:(NSString *) taskId config:(RNFetchBlobProgress *)config;
53 54
 + (void) enableUploadProgress:(NSString *) taskId config:(RNFetchBlobProgress *)config;
54
-+ (NSArray *) getCookies:(NSString *) url;
55
++ (NSDictionary *) getCookies:(NSString *) url;
55 56
 
56 57
 
57 58
 

+ 85
- 48
ios/RNFetchBlobNetwork.m Näytä tiedosto

@@ -37,7 +37,6 @@
37 37
 
38 38
 NSMapTable * taskTable;
39 39
 NSMapTable * expirationTable;
40
-NSMapTable * cookiesTable;
41 40
 NSMutableDictionary * progressTable;
42 41
 NSMutableDictionary * uploadProgressTable;
43 42
 
@@ -59,10 +58,6 @@ static void initialize_tables() {
59 58
     {
60 59
         uploadProgressTable = [[NSMutableDictionary alloc] init];
61 60
     }
62
-    if(cookiesTable == nil)
63
-    {
64
-        cookiesTable = [[NSMapTable alloc] init];
65
-    }
66 61
 }
67 62
 
68 63
 
@@ -116,48 +111,6 @@ NSOperationQueue *taskQueue;
116 111
     return self;
117 112
 }
118 113
 
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
-
161 114
 + (void) enableProgressReport:(NSString *) taskId config:(RNFetchBlobProgress *)config
162 115
 {
163 116
     if(progressTable == nil)
@@ -418,9 +371,10 @@ NSOperationQueue *taskQueue;
418 371
         // # 153 get cookies
419 372
         if(response.URL != nil)
420 373
         {
374
+            NSHTTPCookieStorage * cookieStore = [NSHTTPCookieStorage sharedHTTPCookieStorage];
421 375
             NSArray<NSHTTPCookie *> * cookies = [NSHTTPCookie cookiesWithResponseHeaderFields: headers forURL:response.URL];
422 376
             if(cookies != nil && [cookies count] > 0) {
423
-                [cookiesTable setObject:cookies forKey:response.URL.host];
377
+                [cookieStore setCookies:cookies forURL:response.URL mainDocumentURL:nil];
424 378
             }
425 379
         }
426 380
 
@@ -624,6 +578,89 @@ NSOperationQueue *taskQueue;
624 578
     }
625 579
 }
626 580
 
581
+# pragma mark - cookies handling API
582
+
583
++ (NSDictionary *) getCookies:(NSString *) domain
584
+{
585
+    NSMutableDictionary * result = [NSMutableDictionary new];
586
+    NSHTTPCookieStorage * cookieStore = [NSHTTPCookieStorage sharedHTTPCookieStorage];
587
+    for(NSHTTPCookie * cookie in [cookieStore cookies])
588
+    {
589
+        NSString * cDomain = [cookie domain];
590
+        if([result objectForKey:cDomain] == nil)
591
+        {
592
+            [result setObject:[NSMutableArray new] forKey:cDomain];
593
+        }
594
+        if([cDomain isEqualToString:domain] || [domain length] == 0)
595
+        {
596
+            NSMutableString * cookieStr = [[NSMutableString alloc] init];
597
+            cookieStr = [[self class] getCookieString:cookie];
598
+            NSMutableArray * ary = [result objectForKey:cDomain];
599
+            [ary addObject:cookieStr];
600
+            [result setObject:ary forKey:cDomain];
601
+        }
602
+    }
603
+    return result;
604
+}
605
+
606
+// remove cookies for given domain, if domain is empty remove all cookies in shared cookie storage.
607
++ (void) removeCookies:(NSString *) domain error:(NSError **)error
608
+{
609
+    @try
610
+    {
611
+        NSHTTPCookieStorage * cookies = [NSHTTPCookieStorage sharedHTTPCookieStorage];
612
+        for(NSHTTPCookie * cookie in [cookies cookies])
613
+        {
614
+            BOOL shouldRemove = domain == nil || [domain length] < 1 || [[cookie domain] isEqualToString:domain];
615
+            if(shouldRemove)
616
+            {
617
+                [cookies deleteCookie:cookie];
618
+            }
619
+        }
620
+    }
621
+    @catch(NSError * err)
622
+    {
623
+        *error = err;
624
+    }
625
+}
626
+
627
+// convert NSHTTPCookie to string
628
++ (NSString *) getCookieString:(NSHTTPCookie *) cookie
629
+{
630
+    NSMutableString * cookieStr = [[NSMutableString alloc] init];
631
+    [cookieStr appendString:cookie.name];
632
+    [cookieStr appendString:@"="];
633
+    [cookieStr appendString:cookie.value];
634
+    
635
+    if(cookie.expiresDate == nil) {
636
+        [cookieStr appendString:@"; max-age=0"];
637
+    }
638
+    else {
639
+        [cookieStr appendString:@"; expires="];
640
+        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
641
+        [dateFormatter setDateFormat:@"EEE, dd MM yyyy HH:mm:ss ZZZ"];
642
+        NSString *strDate = [dateFormatter stringFromDate:cookie.expiresDate];
643
+        [cookieStr appendString:strDate];
644
+    }
645
+    
646
+    
647
+    [cookieStr appendString:@"; domain="];
648
+    [cookieStr appendString: [cookie domain]];
649
+    [cookieStr appendString:@"; path="];
650
+    [cookieStr appendString:cookie.path];
651
+    
652
+    
653
+    if (cookie.isSecure) {
654
+        [cookieStr appendString:@"; secure"];
655
+    }
656
+    
657
+    if (cookie.isHTTPOnly) {
658
+        [cookieStr appendString:@"; httponly"];
659
+    }
660
+    return cookieStr;
661
+
662
+}
663
+
627 664
 + (void) cancelRequest:(NSString *)taskId
628 665
 {
629 666
     NSURLSessionDataTask * task = [taskTable objectForKey:taskId];