Ben Copsey

Moved cookie support over to NSHTTPCookie

//
// ASIHTTPCookie.h
// asi-http-request
//
// Created by Ben Copsey on 25/08/2008.
// Copyright 2008 All-Seeing Interactive. All rights reserved.
//
#import <Cocoa/Cocoa.h>
@interface ASIHTTPCookie : NSObject {
NSString *name;
NSString *value;
NSDate *expires;
NSString *path;
NSString *domain;
BOOL requiresHTTPS;
}
- (void)setValue:(NSString *)newValue forProperty:(NSString *)property;
+ (NSMutableArray *)cookiesFromHeader:(NSString *)header;
+ (NSString *)urlEncodedValue:(NSString *)string;
+ (NSString *)urlDecodedValue:(NSString *)string;
@property (retain) NSString *name;
@property (retain) NSString *value;
@property (retain) NSDate *expires;
@property (retain) NSString *path;
@property (retain) NSString *domain;
@property (assign) BOOL requiresHTTPS;
@end
//
// ASIHTTPCookie.m
// asi-http-request
//
// Created by Ben Copsey on 25/08/2008.
// Copyright 2008 All-Seeing Interactive. All rights reserved.
//
#import "ASIHTTPCookie.h"
@implementation ASIHTTPCookie
- (void)setValue:(NSString *)newValue forProperty:(NSString *)property
{
NSString *prop = [property lowercaseString];
if ([prop isEqualToString:@"expires"]) {
[self setExpires:[NSDate dateWithNaturalLanguageString:newValue]];
return;
} else if ([prop isEqualToString:@"domain"]) {
[self setDomain:newValue];
return;
} else if ([prop isEqualToString:@"path"]) {
[self setPath:newValue];
return;
} else if ([prop isEqualToString:@"secure"]) {
[self setRequiresHTTPS:[newValue isEqualToString:@"1"]];
return;
}
if (![self name] && ![self value]) {
[self setName:property];
[self setValue:newValue];
}
}
// I know this looks like a really ugly way to parse the Set-Cookie header, but I'd guess this is probably one of the simplest methods!
// You can't rely on a comma being a cookie delimeter, since it's quite likely that the expiry date for a cookie will contain a comma
+ (NSMutableArray *)cookiesFromHeader:(NSString *)header
{
NSMutableArray *cookies = [[[NSMutableArray alloc] init] autorelease];
ASIHTTPCookie *cookie = [[[ASIHTTPCookie alloc] init] autorelease];
NSArray *parts = [header componentsSeparatedByString:@"="];
int i;
NSString *name;
NSString *value;
NSArray *components;
NSString *newKey;
NSString *terminator;
for (i=0; i<[parts count]; i++) {
NSString *part = [parts objectAtIndex:i];
if (i == 0) {
name = part;
continue;
} else if (i == [parts count]-1) {
[cookie setValue:[ASIHTTPCookie urlDecodedValue:part] forProperty:name];
[cookies addObject:cookie];
continue;
}
components = [part componentsSeparatedByString:@" "];
newKey = [components lastObject];
value = [part substringWithRange:NSMakeRange(0,[part length]-[newKey length]-2)];
[cookie setValue:[ASIHTTPCookie urlDecodedValue:value] forProperty:name];
terminator = [part substringWithRange:NSMakeRange([part length]-[newKey length]-2,1)];
if ([terminator isEqualToString:@","]) {
[cookies addObject:cookie];
cookie = [[[ASIHTTPCookie alloc] init] autorelease];
}
name = newKey;
}
return cookies;
}
+ (NSString *)urlDecodedValue:(NSString *)string
{
NSMutableString *s = [NSMutableString stringWithString:[string stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
//Also swap plus signs for spaces
[s replaceOccurrencesOfString:@"+" withString:@" " options:NSLiteralSearch range:NSMakeRange(0, [s length])];
return [NSString stringWithString:s];
}
+ (NSString *)urlEncodedValue:(NSString *)string
{
return [string stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
}
@synthesize name;
@synthesize value;
@synthesize expires;
@synthesize path;
@synthesize domain;
@synthesize requiresHTTPS;
@end
... ... @@ -39,7 +39,7 @@
NSMutableArray *requestCookies;
//Will be populated with Cookies
NSMutableArray *responseCookies;
NSArray *responseCookies;
//If use cokie persistance is true, network requests will present valid cookies from previous requests
BOOL useCookiePersistance;
... ... @@ -224,17 +224,13 @@
// Remove credentials from the keychain
+ (void)removeCredentialsForHost:(NSString *)host port:(int)port protocol:(NSString *)protocol realm:(NSString *)realm;
// Store cookies for a particular request in the session
+ (void)recordCookiesInSessionForRequest:(ASIHTTPRequest *)request;
// We keep track of any cookies we accept, so that we can remove them from the persistent store later
+ (void)setSessionCookies:(NSMutableArray *)newSessionCookies;
+ (NSMutableArray *)sessionCookies;
// Dump all session data (authentication and cookies)
+ (void)clearSession;
@property (retain) NSString *username;
@property (retain) NSString *password;
@property (retain) NSString *domain;
... ... @@ -253,7 +249,7 @@
@property (assign,readonly) BOOL complete;
@property (retain) NSDictionary *responseHeaders;
@property (retain) NSMutableArray *requestCookies;
@property (retain) NSMutableArray *responseCookies;
@property (retain) NSArray *responseCookies;
@property (assign) BOOL useCookiePersistance;
@property (retain) NSDictionary *requestCredentials;
@property (assign) int responseStatusCode;
... ...
... ... @@ -11,7 +11,7 @@
// See: http://developer.apple.com/samplecode/ImageClient/listing37.html
#import "ASIHTTPRequest.h"
#import "ASIHTTPCookie.h"
#import "NSHTTPCookieAdditions.h"
static NSString *NetworkRequestErrorDomain = @"com.Your-Company.Your-Product.NetworkError.";
... ... @@ -168,47 +168,23 @@ static void ReadStreamClientCallBack(CFReadStreamRef readStream, CFStreamEventTy
//Set your own boundary string only if really obsessive. We don't bother to check if post data contains the boundary, since it's pretty unlikely that it does.
NSString *stringBoundary = @"0xKhTmLbOuNdArY";
//Add cookies from session
if (useCookiePersistance && [[ASIHTTPRequest sessionCookies] count] > 0) {
ASIHTTPCookie *requestCookie;
ASIHTTPCookie *storedCookie;
for (storedCookie in sessionCookies) {
BOOL foundExistingCookie = NO;
//Look for existing cookies in the request - these will always take precedence over session stored cookies
for (requestCookie in requestCookies) {
if ([[requestCookie domain] isEqualToString:[storedCookie domain]]) {
if ([[requestCookie path] isEqualToString:[storedCookie path]] || (![requestCookie path] && ![storedCookie path])) {
if ([[requestCookie name] isEqualToString:[storedCookie name]]) {
foundExistingCookie = YES;
break;
}
}
}
}
if (!foundExistingCookie) {
[requestCookies addObject:storedCookie];
}
//Add cookies from the persistant (mac os global) store
if (useCookiePersistance) {
NSArray *cookies = [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookiesForURL:url];
if (cookies) {
[requestCookies addObjectsFromArray:cookies];
}
}
//Apply request cookies
if ([requestCookies count] > 0) {
ASIHTTPCookie *cookie;
NSHTTPCookie *cookie;
NSString *cookieHeader = nil;
for (cookie in requestCookies) {
//Ensure the cookie is valid for this request
if ([[[url host] substringWithRange:NSMakeRange([[url host] length]-[[cookie domain] length],[[cookie domain] length])] isEqualToString:[cookie domain]]) {
if ([[[url path] substringWithRange:NSMakeRange(0,[[cookie path] length])] isEqualToString:[cookie path]]) {
if (![cookie requiresHTTPS] || [[url port] intValue] == 443) {
if (![cookie expires] || [[cookie expires] timeIntervalSinceNow] > 0) {
if (!cookieHeader) {
cookieHeader = [NSString stringWithFormat: @"%@=%@",[cookie name],[ASIHTTPCookie urlEncodedValue:[cookie value]]];
} else {
cookieHeader = [NSString stringWithFormat: @"%@; %@=%@",cookieHeader,[cookie name],[ASIHTTPCookie urlEncodedValue:[cookie value]]];
}
}
}
}
if (!cookieHeader) {
cookieHeader = [NSString stringWithFormat: @"%@=%@",[cookie name],[cookie encodedValue]];
} else {
cookieHeader = [NSString stringWithFormat: @"%@; %@=%@",cookieHeader,[cookie name],[cookie encodedValue]];
}
}
if (cookieHeader) {
... ... @@ -475,11 +451,20 @@ static void ReadStreamClientCallBack(CFReadStreamRef readStream, CFStreamEventTy
}
//Handle cookies
NSString *cookieHeader = [responseHeaders valueForKey:@"Set-Cookie"];
if (cookieHeader) {
[self setResponseCookies:[ASIHTTPCookie cookiesFromHeader:cookieHeader]];
if (useCookiePersistance) {
[ASIHTTPRequest recordCookiesInSessionForRequest:self];
NSArray *cookies = [NSHTTPCookie cookiesWithResponseHeaderFields:responseHeaders forURL:url];
[self setResponseCookies:cookies];
if (useCookiePersistance) {
//Store cookies in global persistent store
[[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookies:cookies forURL:url mainDocumentURL:nil];
//We also keep any cookies in the sessionCookies array, so that we have a reference to them if we need to remove them later
if (!sessionCookies) {
[ASIHTTPRequest setSessionCookies:[[[NSMutableArray alloc] init] autorelease]];
NSHTTPCookie *cookie;
for (cookie in cookies) {
[[ASIHTTPRequest sessionCookies] addObject:cookie];
}
}
}
... ... @@ -829,37 +814,6 @@ static void ReadStreamClientCallBack(CFReadStreamRef readStream, CFStreamEventTy
}
+ (void)recordCookiesInSessionForRequest:(ASIHTTPRequest *)request
{
if (!sessionCookies) {
[self setSessionCookies:[[[NSMutableArray alloc] init] autorelease]];
}
ASIHTTPCookie *newCookie;
ASIHTTPCookie *storedCookie;
for (newCookie in [request responseCookies]) {
//If we didn't get a domain for the cookie, let's add the one from this request, so we aren't sending cookies from the wrong server later on
if (![newCookie domain]) {
[newCookie setDomain:[[request url] host]];
}
int i = 0;
BOOL foundExistingCookie = NO;
for (storedCookie in sessionCookies) {
if ([[storedCookie domain] isEqualToString:[newCookie domain]]) {
if ([[storedCookie path] isEqualToString:[newCookie path]] || (![storedCookie path] && ![newCookie path])) {
if ([[storedCookie name] isEqualToString:[newCookie name]]) {
foundExistingCookie = YES;
[sessionCookies replaceObjectAtIndex:i withObject:newCookie];
break;
}
}
}
i++;
}
if (!foundExistingCookie) {
[sessionCookies addObject:newCookie];
}
}
}
+ (NSMutableArray *)sessionCookies
{
... ... @@ -868,6 +822,11 @@ static void ReadStreamClientCallBack(CFReadStreamRef readStream, CFStreamEventTy
+ (void)setSessionCookies:(NSMutableArray *)newSessionCookies
{
//Remove existing cookies from the persistent store
NSHTTPCookie *cookie;
for (cookie in newSessionCookies) {
[[NSHTTPCookieStorage sharedHTTPCookieStorage] deleteCookie:cookie];
}
[sessionCookies release];
sessionCookies = [newSessionCookies retain];
}
... ... @@ -881,6 +840,7 @@ static void ReadStreamClientCallBack(CFReadStreamRef readStream, CFStreamEventTy
}
@synthesize username;
@synthesize password;
@synthesize domain;
... ...
... ... @@ -8,7 +8,7 @@
#import "ASIHTTPRequestTests.h"
#import "ASIHTTPRequest.h"
#import "ASIHTTPCookie.h"
#import "NSHTTPCookieAdditions.h"
@implementation ASIHTTPRequestTests
... ... @@ -120,24 +120,6 @@ More tests needed for:
{
BOOL success;
//Firstly, let's make sure cocoa still parses cookie dates correctly using the three examples at http://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html#sec3.3
NSString *dte = @"Sun, 06 Nov 1994 08:49:37 GMT";
NSDate *date = [NSDate dateWithNaturalLanguageString:dte];
NSDate *referenceDate = [NSDate dateWithString:@"1994-11-06 08:49:37 +0000"];
success = [date isEqualToDate:referenceDate];
STAssertTrue(success,@"Date parse 1 failed");
dte = @"Sunday, 06-Nov-94 08:49:37 GMT";
date = [NSDate dateWithNaturalLanguageString:dte];
success = [date isEqualToDate:referenceDate];
STAssertTrue(success,@"Date parse 2 failed");
dte = @"Sun Nov 6 08:49:37 1994";
date = [NSDate dateWithNaturalLanguageString:dte];
success = [date isEqualToDate:referenceDate];
STAssertTrue(success,@"Date parse 3 failed");
NSURL *url = [[[NSURL alloc] initWithString:@"http://allseeing-i.com/asi-http-request/tests/set_cookie"] autorelease];
ASIHTTPRequest *request = [[[ASIHTTPRequest alloc] initWithURL:url] autorelease];
[request setUseCookiePersistance:YES];
... ... @@ -149,14 +131,15 @@ More tests needed for:
NSArray *cookies = [request responseCookies];
STAssertNotNil(cookies,@"Failed to store cookie data in responseCookies");
ASIHTTPCookie *cookie = nil;
NSHTTPCookie *cookie = nil;
BOOL foundCookie = NO;
for (cookie in cookies) {
if ([[cookie name] isEqualToString:@"ASIHTTPRequestTestCookie"]) {
foundCookie = YES;
success = [[cookie value] isEqualToString:@"This is the value"];
NSLog(@"%@",cookie);
success = [[cookie decodedValue] isEqualToString:@"This is the value"];
STAssertTrue(success,@"Failed to store the correct value for a cookie");
success = [[cookie domain] isEqualToString:@"allseeing-i.com"];
success = [[cookie domain] isEqualToString:@".allseeing-i.com"];
STAssertTrue(success,@"Failed to store the correct domain for a cookie");
success = [[cookie path] isEqualToString:@"/asi-http-request/tests"];
STAssertTrue(success,@"Failed to store the correct path for a cookie");
... ...
... ... @@ -26,7 +26,7 @@
- (IBAction)simpleURLFetch:(id)sender
{
ASIHTTPRequest *request = [[[ASIHTTPRequest alloc] initWithURL:[NSURL URLWithString:@"http://allseeing-i.com"]] autorelease];
ASIHTTPRequest *request = [[[ASIHTTPRequest alloc] initWithURL:[NSURL URLWithString:@"http://asi/"]] autorelease];
//Customise our user agent, for no real reason
[request addRequestHeader:@"User-Agent" value:@"ASIHTTPRequest"];
... ...
//
// NSHTTPCookieAdditions.h
// asi-http-request
//
// Created by Ben Copsey on 12/09/2008.
// Copyright 2008 All-Seeing Interactive. All rights reserved.
//
#import <Cocoa/Cocoa.h>
@interface NSHTTPCookie (ValueEncodingAdditions)
- (NSString *)encodedValue;
- (NSString *)decodedValue;
@end
... ...
//
// NSHTTPCookieAdditions.m
// asi-http-request
//
// Created by Ben Copsey on 12/09/2008.
// Copyright 2008 All-Seeing Interactive. All rights reserved.
//
#import "NSHTTPCookieAdditions.h"
@implementation NSHTTPCookie (ValueEncodingAdditions)
- (NSString *)decodedValue
{
NSMutableString *s = [NSMutableString stringWithString:[[self value] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
//Also swap plus signs for spaces
[s replaceOccurrencesOfString:@"+" withString:@" " options:NSLiteralSearch range:NSMakeRange(0, [s length])];
return [NSString stringWithString:s];
}
- (NSString *)encodedValue
{
return [[self value] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
}
@end
... ...
This diff is collapsed. Click to expand it.
This diff could not be displayed because it is too large.
This diff was suppressed by a .gitattributes entry.