Bläddra i källkod

#249 #230 Add removeCookies API

Ben Hsieh 8 år sedan
förälder
incheckning
bc2a5b8c40

+ 15
- 0
src/android/src/main/java/com/RNFetchBlob/RNFetchBlob.java Visa fil

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
     @ReactMethod
259
     @ReactMethod
245
     /**
260
     /**
246
      * @param path Stream file path
261
      * @param path Stream file path

+ 8
- 0
src/android/src/main/java/com/RNFetchBlob/Utils/RNFBCookieJar.java Visa fil

35
         return cookies != null ? cookies : new ArrayList<Cookie>();
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
     public static WritableArray getCookies(String host) {
46
     public static WritableArray getCookies(String host) {
39
         HttpUrl url = HttpUrl.parse(host);
47
         HttpUrl url = HttpUrl.parse(host);
40
         List<Cookie> cookies = null;
48
         List<Cookie> cookies = null;

+ 15
- 0
src/ios/RNFetchBlob/RNFetchBlob.m Visa fil

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

+ 1
- 0
src/ios/RNFetchBlobNetwork.h Visa fil

49
 - (nullable id) init;
49
 - (nullable id) init;
50
 - (void) sendRequest;
50
 - (void) sendRequest;
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;
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
 + (void) enableProgressReport:(NSString *) taskId config:(RNFetchBlobProgress *)config;
53
 + (void) enableProgressReport:(NSString *) taskId config:(RNFetchBlobProgress *)config;
53
 + (void) enableUploadProgress:(NSString *) taskId config:(RNFetchBlobProgress *)config;
54
 + (void) enableUploadProgress:(NSString *) taskId config:(RNFetchBlobProgress *)config;
54
 + (NSArray *) getCookies:(NSString *) url;
55
 + (NSArray *) getCookies:(NSString *) url;

+ 78
- 48
src/ios/RNFetchBlobNetwork.m Visa fil

37
 
37
 
38
 NSMapTable * taskTable;
38
 NSMapTable * taskTable;
39
 NSMapTable * expirationTable;
39
 NSMapTable * expirationTable;
40
-NSMapTable * cookiesTable;
41
 NSMutableDictionary * progressTable;
40
 NSMutableDictionary * progressTable;
42
 NSMutableDictionary * uploadProgressTable;
41
 NSMutableDictionary * uploadProgressTable;
43
 
42
 
59
     {
58
     {
60
         uploadProgressTable = [[NSMutableDictionary alloc] init];
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
     return self;
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
 + (void) enableProgressReport:(NSString *) taskId config:(RNFetchBlobProgress *)config
114
 + (void) enableProgressReport:(NSString *) taskId config:(RNFetchBlobProgress *)config
162
 {
115
 {
163
     if(progressTable == nil)
116
     if(progressTable == nil)
417
         // # 153 get cookies
370
         // # 153 get cookies
418
         if(response.URL != nil)
371
         if(response.URL != nil)
419
         {
372
         {
373
+            NSHTTPCookieStorage * cookieStore = [NSHTTPCookieStorage sharedHTTPCookieStorage];
420
             NSArray<NSHTTPCookie *> * cookies = [NSHTTPCookie cookiesWithResponseHeaderFields: headers forURL:response.URL];
374
             NSArray<NSHTTPCookie *> * cookies = [NSHTTPCookie cookiesWithResponseHeaderFields: headers forURL:response.URL];
421
             if(cookies != nil && [cookies count] > 0) {
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
     }
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
 + (void) cancelRequest:(NSString *)taskId
656
 + (void) cancelRequest:(NSString *)taskId
627
 {
657
 {
628
     NSURLSessionDataTask * task = [taskTable objectForKey:taskId];
658
     NSURLSessionDataTask * task = [taskTable objectForKey:taskId];

+ 12
- 1
src/net.js Visa fil

20
   return RNFetchBlob.getCookies(url)
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
 export default {
33
 export default {
24
-  getCookies
34
+  getCookies,
35
+  removeCookies
25
 }
36
 }