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

Re-apply iOS removeCookies API #325

Merged
merged 1 commit into from
Apr 13, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions ios/RNFetchBlob/RNFetchBlob.m
Original file line number Diff line number Diff line change
Expand Up @@ -566,11 +566,24 @@ - (UIViewController *) documentInteractionControllerViewControllerForPreview: (U
}

# pragma mark - getCookies

RCT_EXPORT_METHOD(getCookies:(NSString *)url resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject)
{
resolve([RNFetchBlobNetwork getCookies:url]);
}

# pragma mark - removeCookie

RCT_EXPORT_METHOD(removeCookies:(NSString *)domain resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject)
{
NSError * err = nil;
[RNFetchBlobNetwork removeCookies:domain error:&err];
if(err)
reject(@"RNFetchBlob failed to remove cookie", @"RNFetchBlob failed to remove cookie", nil);
else
resolve(@[[NSNull null]]);
}

# pragma mark - check expired network events

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




@end
3 changes: 2 additions & 1 deletion ios/RNFetchBlobNetwork.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,10 @@ typedef void(^DataTaskCompletionHander) (NSData * _Nullable resp, NSURLResponse
- (nullable id) init;
- (void) sendRequest;
- (void) sendRequest:(NSDictionary * _Nullable )options contentLength:(long)contentLength bridge:(RCTBridge * _Nullable)bridgeRef taskId:(NSString * _Nullable)taskId withRequest:(NSURLRequest * _Nullable)req callback:(_Nullable RCTResponseSenderBlock) callback;
+ (void) removeCookies:(NSString *) domain error:(NSError **)error;
+ (void) enableProgressReport:(NSString *) taskId config:(RNFetchBlobProgress *)config;
+ (void) enableUploadProgress:(NSString *) taskId config:(RNFetchBlobProgress *)config;
+ (NSArray *) getCookies:(NSString *) url;
+ (NSDictionary *) getCookies:(NSString *) url;



Expand Down
133 changes: 85 additions & 48 deletions ios/RNFetchBlobNetwork.m
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@

NSMapTable * taskTable;
NSMapTable * expirationTable;
NSMapTable * cookiesTable;
NSMutableDictionary * progressTable;
NSMutableDictionary * uploadProgressTable;

Expand All @@ -59,10 +58,6 @@ static void initialize_tables() {
{
uploadProgressTable = [[NSMutableDictionary alloc] init];
}
if(cookiesTable == nil)
{
cookiesTable = [[NSMapTable alloc] init];
}
}


Expand Down Expand Up @@ -116,48 +111,6 @@ - (id)init {
return self;
}

+ (NSArray *) getCookies:(NSString *) url
{
NSString * hostname = [[NSURL URLWithString:url] host];
NSMutableArray * cookies = [NSMutableArray new];
NSArray * list = [cookiesTable objectForKey:hostname];
for(NSHTTPCookie * cookie in list)
{
NSMutableString * cookieStr = [[NSMutableString alloc] init];
[cookieStr appendString:cookie.name];
[cookieStr appendString:@"="];
[cookieStr appendString:cookie.value];

if(cookie.expiresDate == nil) {
[cookieStr appendString:@"; max-age=0"];
}
else {
[cookieStr appendString:@"; expires="];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"EEE, dd MM yyyy HH:mm:ss ZZZ"];
NSString *strDate = [dateFormatter stringFromDate:cookie.expiresDate];
[cookieStr appendString:strDate];
}


[cookieStr appendString:@"; domain="];
[cookieStr appendString:hostname];
[cookieStr appendString:@"; path="];
[cookieStr appendString:cookie.path];


if (cookie.isSecure) {
[cookieStr appendString:@"; secure"];
}

if (cookie.isHTTPOnly) {
[cookieStr appendString:@"; httponly"];
}
[cookies addObject:cookieStr];
}
return cookies;
}

+ (void) enableProgressReport:(NSString *) taskId config:(RNFetchBlobProgress *)config
{
if(progressTable == nil)
Expand Down Expand Up @@ -418,9 +371,10 @@ - (void) URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dat
// # 153 get cookies
if(response.URL != nil)
{
NSHTTPCookieStorage * cookieStore = [NSHTTPCookieStorage sharedHTTPCookieStorage];
NSArray<NSHTTPCookie *> * cookies = [NSHTTPCookie cookiesWithResponseHeaderFields: headers forURL:response.URL];
if(cookies != nil && [cookies count] > 0) {
[cookiesTable setObject:cookies forKey:response.URL.host];
[cookieStore setCookies:cookies forURL:response.URL mainDocumentURL:nil];
}
}

Expand Down Expand Up @@ -624,6 +578,89 @@ - (void) URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didSen
}
}

# pragma mark - cookies handling API

+ (NSDictionary *) getCookies:(NSString *) domain
{
NSMutableDictionary * result = [NSMutableDictionary new];
NSHTTPCookieStorage * cookieStore = [NSHTTPCookieStorage sharedHTTPCookieStorage];
for(NSHTTPCookie * cookie in [cookieStore cookies])
{
NSString * cDomain = [cookie domain];
if([result objectForKey:cDomain] == nil)
{
[result setObject:[NSMutableArray new] forKey:cDomain];
}
if([cDomain isEqualToString:domain] || [domain length] == 0)
{
NSMutableString * cookieStr = [[NSMutableString alloc] init];
cookieStr = [[self class] getCookieString:cookie];
NSMutableArray * ary = [result objectForKey:cDomain];
[ary addObject:cookieStr];
[result setObject:ary forKey:cDomain];
}
}
return result;
}

// remove cookies for given domain, if domain is empty remove all cookies in shared cookie storage.
+ (void) removeCookies:(NSString *) domain error:(NSError **)error
{
@try
{
NSHTTPCookieStorage * cookies = [NSHTTPCookieStorage sharedHTTPCookieStorage];
for(NSHTTPCookie * cookie in [cookies cookies])
{
BOOL shouldRemove = domain == nil || [domain length] < 1 || [[cookie domain] isEqualToString:domain];
if(shouldRemove)
{
[cookies deleteCookie:cookie];
}
}
}
@catch(NSError * err)
{
*error = err;
}
}

// convert NSHTTPCookie to string
+ (NSString *) getCookieString:(NSHTTPCookie *) cookie
{
NSMutableString * cookieStr = [[NSMutableString alloc] init];
[cookieStr appendString:cookie.name];
[cookieStr appendString:@"="];
[cookieStr appendString:cookie.value];

if(cookie.expiresDate == nil) {
[cookieStr appendString:@"; max-age=0"];
}
else {
[cookieStr appendString:@"; expires="];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"EEE, dd MM yyyy HH:mm:ss ZZZ"];
NSString *strDate = [dateFormatter stringFromDate:cookie.expiresDate];
[cookieStr appendString:strDate];
}


[cookieStr appendString:@"; domain="];
[cookieStr appendString: [cookie domain]];
[cookieStr appendString:@"; path="];
[cookieStr appendString:cookie.path];


if (cookie.isSecure) {
[cookieStr appendString:@"; secure"];
}

if (cookie.isHTTPOnly) {
[cookieStr appendString:@"; httponly"];
}
return cookieStr;

}

+ (void) cancelRequest:(NSString *)taskId
{
NSURLSessionDataTask * task = [taskTable objectForKey:taskId];
Expand Down