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

Commit

Permalink
#249 #230 Add removeCookies API
Browse files Browse the repository at this point in the history
  • Loading branch information
wkh237 committed Feb 4, 2017
1 parent 5ed25ce commit bc2a5b8
Show file tree
Hide file tree
Showing 6 changed files with 129 additions and 49 deletions.
15 changes: 15 additions & 0 deletions src/android/src/main/java/com/RNFetchBlob/RNFetchBlob.java
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,21 @@ public void getCookies(String host, Promise promise) {
}
}

@ReactMethod
/**
* Remove cookies for specific domain
* @param domain String of the domain
* @param promise JSC promise injected by RN
*/
public void removeCookies(String domain, Promise promise) {
try {
RNFBCookieJar.removeCookies(domain);
promise.resolve(null);
} catch(Exception err) {
promise.reject("RNFetchBlob.removeCookies", err.getMessage());
}
}

@ReactMethod
/**
* @param path Stream file path
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,14 @@ public List<Cookie> loadForRequest(HttpUrl url) {
return cookies != null ? cookies : new ArrayList<Cookie>();
}

public static void removeCookies(String domain) {
if(domain == null) {
cookieStore.clear();
}
else if(cookieStore.containsKey(domain))
cookieStore.remove(domain);
}

public static WritableArray getCookies(String host) {
HttpUrl url = HttpUrl.parse(host);
List<Cookie> cookies = null;
Expand Down
15 changes: 15 additions & 0 deletions src/ios/RNFetchBlob/RNFetchBlob.m
Original file line number Diff line number Diff line change
Expand Up @@ -519,11 +519,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 @@ -532,4 +545,6 @@ - (UIViewController *) documentInteractionControllerViewControllerForPreview: (U
}




@end
1 change: 1 addition & 0 deletions src/ios/RNFetchBlobNetwork.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ 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;
Expand Down
126 changes: 78 additions & 48 deletions src/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 @@ -417,9 +370,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 @@ -623,6 +577,82 @@ - (void) URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didSen
}
}

# pragma mark - cookies handling API

+ (NSArray *) getCookies:(NSString *) domain
{
NSMutableArray * cookies = [NSMutableArray new];
NSHTTPCookieStorage * cookieStore = [NSHTTPCookieStorage sharedHTTPCookieStorage];
for(NSHTTPCookie * cookie in cookieStore)
{
if([[cookie domain] isEqualToString:domain])
{
NSMutableString * cookieStr = [[NSMutableString alloc] init];
cookieStr = [[self class] getCookieString:cookie];
[cookies addObject:cookieStr];
}
}
return cookies;
}

// 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)
{
BOOL shouldRemove = domain == nil || [[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
13 changes: 12 additions & 1 deletion src/net.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,17 @@ function getCookies(url:string):Promise<Array<String>> {
return RNFetchBlob.getCookies(url)
}

/**
* Remove cookies for a specific domain
* @param {?string} domain Domain of the cookies to be removed, remove all
* cookies when this is null.
* @return {Promise<null>}
*/
function removeCookies(domain:?string):Promise<null> {
return RNFetchBlob.removeCookies(url || null)
}

export default {
getCookies
getCookies,
removeCookies
}

0 comments on commit bc2a5b8

Please sign in to comment.