3737
3838NSMapTable * taskTable;
3939NSMapTable * expirationTable;
40- NSMapTable * cookiesTable;
4140NSMutableDictionary * progressTable;
4241NSMutableDictionary * 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];
0 commit comments