Browse Source

#249 #230 Add removeCookies API

Ben Hsieh 8 years ago
parent
commit
bc2a5b8c40

+ 15
- 0
src/android/src/main/java/com/RNFetchBlob/RNFetchBlob.java View File

@@ -241,6 +241,21 @@ public class RNFetchBlob extends ReactContextBaseJavaModule {
241 241
         }
242 242
     }
243 243
 
244
+    @ReactMethod
245
+    /**
246
+     * Remove cookies for specific domain
247
+     * @param domain String of the domain
248
+     * @param promise JSC promise injected by RN
249
+     */
250
+    public void removeCookies(String domain, Promise promise) {
251
+        try {
252
+            RNFBCookieJar.removeCookies(domain);
253
+            promise.resolve(null);
254
+        } catch(Exception err) {
255
+            promise.reject("RNFetchBlob.removeCookies", err.getMessage());
256
+        }
257
+    }
258
+
244 259
     @ReactMethod
245 260
     /**
246 261
      * @param path Stream file path

+ 8
- 0
src/android/src/main/java/com/RNFetchBlob/Utils/RNFBCookieJar.java View File

@@ -35,6 +35,14 @@ public class RNFBCookieJar implements CookieJar {
35 35
         return cookies != null ? cookies : new ArrayList<Cookie>();
36 36
     }
37 37
 
38
+    public static void removeCookies(String domain) {
39
+        if(domain == null) {
40
+            cookieStore.clear();
41
+        }
42
+        else if(cookieStore.containsKey(domain))
43
+            cookieStore.remove(domain);
44
+    }
45
+
38 46
     public static WritableArray getCookies(String host) {
39 47
         HttpUrl url = HttpUrl.parse(host);
40 48
         List<Cookie> cookies = null;

+ 15
- 0
src/ios/RNFetchBlob/RNFetchBlob.m View File

@@ -519,11 +519,24 @@ RCT_EXPORT_METHOD(df:(RCTResponseSenderBlock)callback)
519 519
 }
520 520
 
521 521
 # pragma mark - getCookies
522
+
522 523
 RCT_EXPORT_METHOD(getCookies:(NSString *)url resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject)
523 524
 {
524 525
     resolve([RNFetchBlobNetwork getCookies:url]);
525 526
 }
526 527
 
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
+
527 540
 # pragma mark - check expired network events
528 541
 
529 542
 RCT_EXPORT_METHOD(emitExpiredEvent:(RCTResponseSenderBlock)callback)
@@ -532,4 +545,6 @@ RCT_EXPORT_METHOD(emitExpiredEvent:(RCTResponseSenderBlock)callback)
532 545
 }
533 546
 
534 547
 
548
+
549
+
535 550
 @end

+ 1
- 0
src/ios/RNFetchBlobNetwork.h View File

@@ -49,6 +49,7 @@ 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 55
 + (NSArray *) getCookies:(NSString *) url;

+ 78
- 48
src/ios/RNFetchBlobNetwork.m View File

@@ -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)
@@ -417,9 +370,10 @@ NSOperationQueue *taskQueue;
417 370
         // # 153 get cookies
418 371
         if(response.URL != nil)
419 372
         {
373
+            NSHTTPCookieStorage * cookieStore = [NSHTTPCookieStorage sharedHTTPCookieStorage];
420 374
             NSArray<NSHTTPCookie *> * cookies = [NSHTTPCookie cookiesWithResponseHeaderFields: headers forURL:response.URL];
421 375
             if(cookies != nil && [cookies count] > 0) {
422
-                [cookiesTable setObject:cookies forKey:response.URL.host];
376
+                [cookieStore setCookies:cookies forURL:response.URL mainDocumentURL:nil];
423 377
             }
424 378
         }
425 379
 
@@ -623,6 +577,82 @@ NSOperationQueue *taskQueue;
623 577
     }
624 578
 }
625 579
 
580
+# pragma mark - cookies handling API
581
+
582
++ (NSArray *) getCookies:(NSString *) domain
583
+{
584
+    NSMutableArray * cookies = [NSMutableArray new];
585
+    NSHTTPCookieStorage * cookieStore = [NSHTTPCookieStorage sharedHTTPCookieStorage];
586
+    for(NSHTTPCookie * cookie in cookieStore)
587
+    {
588
+        if([[cookie domain] isEqualToString:domain])
589
+        {
590
+            NSMutableString * cookieStr = [[NSMutableString alloc] init];
591
+            cookieStr = [[self class] getCookieString:cookie];
592
+            [cookies addObject:cookieStr];
593
+        }
594
+    }
595
+    return cookies;
596
+}
597
+
598
+// remove cookies for given domain, if domain is empty remove all cookies in shared cookie storage.
599
++ (void) removeCookies:(NSString *) domain error:(NSError **)error
600
+{
601
+    @try
602
+    {
603
+        NSHTTPCookieStorage * cookies = [NSHTTPCookieStorage sharedHTTPCookieStorage];
604
+        for(NSHTTPCookie * cookie in cookies)
605
+        {
606
+            BOOL shouldRemove = domain == nil || [[cookie domain] isEqualToString:domain];
607
+            if(shouldRemove)
608
+            {
609
+                [cookies deleteCookie:cookie];
610
+            }
611
+        }
612
+    }
613
+    @catch(NSError * err)
614
+    {
615
+        *error = err;
616
+    }
617
+}
618
+
619
+// convert NSHTTPCookie to string
620
++ (NSString *) getCookieString:(NSHTTPCookie *) cookie
621
+{
622
+    NSMutableString * cookieStr = [[NSMutableString alloc] init];
623
+    [cookieStr appendString:cookie.name];
624
+    [cookieStr appendString:@"="];
625
+    [cookieStr appendString:cookie.value];
626
+    
627
+    if(cookie.expiresDate == nil) {
628
+        [cookieStr appendString:@"; max-age=0"];
629
+    }
630
+    else {
631
+        [cookieStr appendString:@"; expires="];
632
+        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
633
+        [dateFormatter setDateFormat:@"EEE, dd MM yyyy HH:mm:ss ZZZ"];
634
+        NSString *strDate = [dateFormatter stringFromDate:cookie.expiresDate];
635
+        [cookieStr appendString:strDate];
636
+    }
637
+    
638
+    
639
+    [cookieStr appendString:@"; domain="];
640
+    [cookieStr appendString: [cookie domain]];
641
+    [cookieStr appendString:@"; path="];
642
+    [cookieStr appendString:cookie.path];
643
+    
644
+    
645
+    if (cookie.isSecure) {
646
+        [cookieStr appendString:@"; secure"];
647
+    }
648
+    
649
+    if (cookie.isHTTPOnly) {
650
+        [cookieStr appendString:@"; httponly"];
651
+    }
652
+    return cookieStr;
653
+
654
+}
655
+
626 656
 + (void) cancelRequest:(NSString *)taskId
627 657
 {
628 658
     NSURLSessionDataTask * task = [taskTable objectForKey:taskId];

+ 12
- 1
src/net.js View File

@@ -20,6 +20,17 @@ function getCookies(url:string):Promise<Array<String>> {
20 20
   return RNFetchBlob.getCookies(url)
21 21
 }
22 22
 
23
+/**
24
+ * Remove cookies for a specific domain
25
+ * @param  {?string} domain Domain of the cookies to be removed, remove all
26
+ * cookies when this is null.
27
+ * @return {Promise<null>}
28
+ */
29
+function removeCookies(domain:?string):Promise<null> {
30
+  return RNFetchBlob.removeCookies(url || null)
31
+}
32
+
23 33
 export default {
24
-  getCookies
34
+  getCookies,
35
+  removeCookies
25 36
 }