Apply patches by Lubomir Rintel <lubo.rintel@gooddata.com> to enable cookies

git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/base/trunk@34746 72102866-910b-0410-8b05-ffd578937521
This commit is contained in:
Richard Frith-MacDonald 2012-02-08 17:01:31 +00:00
parent 06ba8d9c58
commit 8b2aba3580
6 changed files with 105 additions and 14 deletions

View file

@ -1,3 +1,14 @@
2012-02-08 Lubomir Rintel <lubo.rintel@gooddata.com>
* Source/NSHTTPCookie.m:
* Source/NSHTTPCookieStorage.m:
* Source/NSURLConnection.m:
* Source/NSURLProtocol.m:
* Source/NSURLRequest.m:
Enable the use of cookies by default ... addse setting of cookies
in requests and improves caching/storing. Original patches rewritten
by maintainer to conform to coding standards and for clarity.
2012-02-08 Richard Frith-Macdonald <rfm@gnu.org>
* Source/GSConcreteValueTemplate.m: Don't ask a non-retained object

View file

@ -199,6 +199,7 @@ static NSRange GSRangeOfCookie(NSString *string);
NSEnumerator *henum = [headerFields keyEnumerator];
NSMutableArray *a = [NSMutableArray array];
NSString *header;
while ((header = [henum nextObject]))
{
NSMutableArray *suba
@ -288,10 +289,10 @@ static NSRange GSRangeOfCookie(NSString *string);
/* Check a few values. Based on Mac OS X tests. */
if (![self _isValidProperty: [properties objectForKey: NSHTTPCookiePath]]
|| ![self _isValidProperty: [properties objectForKey: NSHTTPCookieDomain]]
|| ![self _isValidProperty: [properties objectForKey: NSHTTPCookieName]]
|| ![self _isValidProperty: [properties objectForKey: NSHTTPCookieValue]]
)
|| ![self _isValidProperty: [properties objectForKey: NSHTTPCookieDomain]]
|| ![self _isValidProperty: [properties objectForKey: NSHTTPCookieName]]
|| ![self _isValidProperty: [properties objectForKey: NSHTTPCookieValue]]
)
{
[self release];
return nil;
@ -299,12 +300,25 @@ static NSRange GSRangeOfCookie(NSString *string);
rawProps = [[properties mutableCopy] autorelease];
if ([rawProps objectForKey: @"Created"] == nil)
[rawProps setObject: [NSDate date] forKey: @"Created"];
{
NSInteger seconds;
NSDate *now;
/* Round to whole seconds, so that a serialization/deserialisation
* cycle produces an identical object whic hcan be used to eliminate
* duplicates.
*/
seconds = [NSDate timeIntervalSinceReferenceDate];
now = [NSDate dateWithTimeIntervalSinceReferenceDate: seconds];
[rawProps setObject: now forKey: @"Created"];
}
if ([rawProps objectForKey: NSHTTPCookieExpires] == nil
|| [[rawProps objectForKey: NSHTTPCookieExpires]
|| [[rawProps objectForKey: NSHTTPCookieExpires]
isKindOfClass: [NSDate class]] == NO)
[rawProps setObject: [NSNumber numberWithBool: YES]
forKey: NSHTTPCookieDiscard];
{
[rawProps setObject: [NSNumber numberWithBool: YES]
forKey: NSHTTPCookieDiscard];
}
this->_properties = [rawProps copy];
return self;
@ -357,6 +371,15 @@ static NSRange GSRangeOfCookie(NSString *string);
[self name], [self value]];
}
- (NSUInteger) hash
{
return [[self properties] hash];
}
- (BOOL) isEqual: (id)other
{
return [[other properties] isEqual: [self properties]];
}
@end

View file

@ -163,6 +163,7 @@ static NSHTTPCookieStorage *storage = nil;
int i;
NSArray *properties;
NSString *path = [self _cookieStorePath];
if (path == nil)
{
return;
@ -173,11 +174,20 @@ static NSHTTPCookieStorage *storage = nil;
NS_HANDLER
NSLog(@"NSHTTPCookieStorage: Error reading cookies plist");
NS_ENDHANDLER
if (properties == nil)
if (nil == properties)
return;
for (i = 0; i < [properties count]; i++)
[this->_cookies addObject:
[NSHTTPCookie cookieWithProperties: [properties objectAtIndex: i]]];
{
NSDictionary *props;
NSHTTPCookie *cookie;
props = [properties objectAtIndex: i];
cookie = [NSHTTPCookie cookieWithProperties: props];
if (NO == [this->_cookies containsObject: cookie])
{
[this->_cookies addObject:cookie];
}
}
}
- (void) _updateToCookieStore
@ -185,6 +195,7 @@ static NSHTTPCookieStorage *storage = nil;
int i, count;
NSMutableArray *properties;
NSString *path = [self _cookieStorePath];
if (path == nil)
{
return;

View file

@ -115,10 +115,9 @@
@end
typedef struct
{
NSURLRequest *_request;
NSMutableURLRequest *_request;
NSURLProtocol *_protocol;
id _delegate; // Not retained
BOOL _debug;
@ -190,7 +189,33 @@ typedef struct
{
if ((self = [super init]) != nil)
{
this->_request = [request copy];
this->_request = [request mutableCopyWithZone: [self zone]];
/* Enrich the request with the appropriate HTTP cookies,
* if desired.
*/
if ([this->_request HTTPShouldHandleCookies] == YES)
{
NSArray *cookies;
cookies = [[NSHTTPCookieStorage sharedHTTPCookieStorage]
cookiesForURL: [this->_request URL]];
if ([cookies count] > 0)
{
NSDictionary *headers;
NSEnumerator *enumerator;
NSString *header;
headers = [NSHTTPCookie requestHeaderFieldsWithCookies: cookies];
enumerator = [headers keyEnumerator];
while (nil != (header = [enumerator nextObject]))
{
[this->_request addValue: [headers valueForKey: header]
forHTTPHeaderField: header];
}
}
}
this->_delegate = delegate;
this->_protocol = [[NSURLProtocol alloc]
initWithRequest: this->_request

View file

@ -976,6 +976,26 @@ static NSURLProtocol *placeholder = nil;
{
NSURLCacheStoragePolicy policy;
/* Get cookies from the response and accept them into
* shared storage if policy permits
*/
if ([this->request HTTPShouldHandleCookies] == YES
&& [_response isKindOfClass: [NSHTTPURLResponse class]] == YES)
{
NSDictionary *hdrs;
NSArray *cookies;
NSURL *url;
url = [_response URL];
hdrs = [_response allHeaderFields];
cookies = [NSHTTPCookie cookiesWithResponseHeaderFields: hdrs
forURL: url];
[[NSHTTPCookieStorage sharedHTTPCookieStorage]
setCookies: cookies
forURL: url
mainDocumentURL: [this->request mainDocumentURL]];
}
/* Tell the client that we have a response and how
* it should be cached.
*/

View file

@ -197,6 +197,7 @@ typedef struct {
this->timeoutInterval = timeoutInterval;
this->mainDocumentURL = nil;
this->method = @"GET";
this->shouldHandleCookies = YES;
}
return self;
}