Skip to content

Commit 1cc4359

Browse files
maxpowawkh237
authored andcommitted
Re-apply iOS removeCookies API (wkh237#325)
1 parent 264ff7d commit 1cc4359

File tree

3 files changed

+102
-49
lines changed

3 files changed

+102
-49
lines changed

ios/RNFetchBlob/RNFetchBlob.m

+15
Original file line numberDiff line numberDiff line change
@@ -566,11 +566,24 @@ - (UIViewController *) documentInteractionControllerViewControllerForPreview: (U
566566
}
567567

568568
# pragma mark - getCookies
569+
569570
RCT_EXPORT_METHOD(getCookies:(NSString *)url resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject)
570571
{
571572
resolve([RNFetchBlobNetwork getCookies:url]);
572573
}
573574

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

576589
RCT_EXPORT_METHOD(emitExpiredEvent:(RCTResponseSenderBlock)callback)
@@ -579,4 +592,6 @@ - (UIViewController *) documentInteractionControllerViewControllerForPreview: (U
579592
}
580593

581594

595+
596+
582597
@end

ios/RNFetchBlobNetwork.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,10 @@ 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;
54-
+ (NSArray *) getCookies:(NSString *) url;
55+
+ (NSDictionary *) getCookies:(NSString *) url;
5556

5657

5758

ios/RNFetchBlobNetwork.m

+85-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)
@@ -418,9 +371,10 @@ - (void) URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dat
418371
// # 153 get cookies
419372
if(response.URL != nil)
420373
{
374+
NSHTTPCookieStorage * cookieStore = [NSHTTPCookieStorage sharedHTTPCookieStorage];
421375
NSArray<NSHTTPCookie *> * cookies = [NSHTTPCookie cookiesWithResponseHeaderFields: headers forURL:response.URL];
422376
if(cookies != nil && [cookies count] > 0) {
423-
[cookiesTable setObject:cookies forKey:response.URL.host];
377+
[cookieStore setCookies:cookies forURL:response.URL mainDocumentURL:nil];
424378
}
425379
}
426380

@@ -624,6 +578,89 @@ - (void) URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didSen
624578
}
625579
}
626580

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

0 commit comments

Comments
 (0)