37
37
38
38
NSMapTable * taskTable;
39
39
NSMapTable * expirationTable;
40
- NSMapTable * cookiesTable;
41
40
NSMutableDictionary * progressTable;
42
41
NSMutableDictionary * uploadProgressTable;
43
42
@@ -59,10 +58,6 @@ static void initialize_tables() {
59
58
{
60
59
uploadProgressTable = [[NSMutableDictionary alloc ] init ];
61
60
}
62
- if (cookiesTable == nil )
63
- {
64
- cookiesTable = [[NSMapTable alloc ] init ];
65
- }
66
61
}
67
62
68
63
@@ -116,48 +111,6 @@ - (id)init {
116
111
return self;
117
112
}
118
113
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
-
161
114
+ (void ) enableProgressReport : (NSString *) taskId config : (RNFetchBlobProgress *)config
162
115
{
163
116
if (progressTable == nil )
@@ -418,9 +371,10 @@ - (void) URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dat
418
371
// # 153 get cookies
419
372
if (response.URL != nil )
420
373
{
374
+ NSHTTPCookieStorage * cookieStore = [NSHTTPCookieStorage sharedHTTPCookieStorage ];
421
375
NSArray <NSHTTPCookie *> * cookies = [NSHTTPCookie cookiesWithResponseHeaderFields: headers forURL: response.URL];
422
376
if (cookies != nil && [cookies count ] > 0 ) {
423
- [cookiesTable setObject : cookies forKey : response.URL.host ];
377
+ [cookieStore setCookies : cookies forURL : response.URL mainDocumentURL: nil ];
424
378
}
425
379
}
426
380
@@ -624,6 +578,89 @@ - (void) URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didSen
624
578
}
625
579
}
626
580
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
+
627
664
+ (void ) cancelRequest : (NSString *)taskId
628
665
{
629
666
NSURLSessionDataTask * task = [taskTable objectForKey: taskId];
0 commit comments