Skip to content
This repository was archived by the owner on Mar 16, 2019. It is now read-only.

Commit bc2a5b8

Browse files
committed
#249 #230 Add removeCookies API
1 parent 5ed25ce commit bc2a5b8

File tree

6 files changed

+129
-49
lines changed

6 files changed

+129
-49
lines changed

src/android/src/main/java/com/RNFetchBlob/RNFetchBlob.java

+15
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,21 @@ public void getCookies(String host, Promise promise) {
241241
}
242242
}
243243

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+
244259
@ReactMethod
245260
/**
246261
* @param path Stream file path

src/android/src/main/java/com/RNFetchBlob/Utils/RNFBCookieJar.java

+8
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,14 @@ public List<Cookie> loadForRequest(HttpUrl url) {
3535
return cookies != null ? cookies : new ArrayList<Cookie>();
3636
}
3737

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+
3846
public static WritableArray getCookies(String host) {
3947
HttpUrl url = HttpUrl.parse(host);
4048
List<Cookie> cookies = null;

src/ios/RNFetchBlob/RNFetchBlob.m

+15
Original file line numberDiff line numberDiff line change
@@ -519,11 +519,24 @@ - (UIViewController *) documentInteractionControllerViewControllerForPreview: (U
519519
}
520520

521521
# pragma mark - getCookies
522+
522523
RCT_EXPORT_METHOD(getCookies:(NSString *)url resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject)
523524
{
524525
resolve([RNFetchBlobNetwork getCookies:url]);
525526
}
526527

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+
527540
# pragma mark - check expired network events
528541

529542
RCT_EXPORT_METHOD(emitExpiredEvent:(RCTResponseSenderBlock)callback)
@@ -532,4 +545,6 @@ - (UIViewController *) documentInteractionControllerViewControllerForPreview: (U
532545
}
533546

534547

548+
549+
535550
@end

src/ios/RNFetchBlobNetwork.h

+1
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ typedef void(^DataTaskCompletionHander) (NSData * _Nullable resp, NSURLResponse
4949
- (nullable id) init;
5050
- (void) sendRequest;
5151
- (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;
5253
+ (void) enableProgressReport:(NSString *) taskId config:(RNFetchBlobProgress *)config;
5354
+ (void) enableUploadProgress:(NSString *) taskId config:(RNFetchBlobProgress *)config;
5455
+ (NSArray *) getCookies:(NSString *) url;

src/ios/RNFetchBlobNetwork.m

+78-48
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@
3737

3838
NSMapTable * taskTable;
3939
NSMapTable * expirationTable;
40-
NSMapTable * cookiesTable;
4140
NSMutableDictionary * progressTable;
4241
NSMutableDictionary * uploadProgressTable;
4342

@@ -59,10 +58,6 @@ static void initialize_tables() {
5958
{
6059
uploadProgressTable = [[NSMutableDictionary alloc] init];
6160
}
62-
if(cookiesTable == nil)
63-
{
64-
cookiesTable = [[NSMapTable alloc] init];
65-
}
6661
}
6762

6863

@@ -116,48 +111,6 @@ - (id)init {
116111
return self;
117112
}
118113

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-
161114
+ (void) enableProgressReport:(NSString *) taskId config:(RNFetchBlobProgress *)config
162115
{
163116
if(progressTable == nil)
@@ -417,9 +370,10 @@ - (void) URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dat
417370
// # 153 get cookies
418371
if(response.URL != nil)
419372
{
373+
NSHTTPCookieStorage * cookieStore = [NSHTTPCookieStorage sharedHTTPCookieStorage];
420374
NSArray<NSHTTPCookie *> * cookies = [NSHTTPCookie cookiesWithResponseHeaderFields: headers forURL:response.URL];
421375
if(cookies != nil && [cookies count] > 0) {
422-
[cookiesTable setObject:cookies forKey:response.URL.host];
376+
[cookieStore setCookies:cookies forURL:response.URL mainDocumentURL:nil];
423377
}
424378
}
425379

@@ -623,6 +577,82 @@ - (void) URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didSen
623577
}
624578
}
625579

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+
626656
+ (void) cancelRequest:(NSString *)taskId
627657
{
628658
NSURLSessionDataTask * task = [taskTable objectForKey:taskId];

src/net.js

+12-1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,17 @@ function getCookies(url:string):Promise<Array<String>> {
2020
return RNFetchBlob.getCookies(url)
2121
}
2222

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+
2333
export default {
24-
getCookies
34+
getCookies,
35+
removeCookies
2536
}

0 commit comments

Comments
 (0)