Ben Copsey

Moved cookie support over to NSHTTPCookie

1 -//  
2 -// ASIHTTPCookie.h  
3 -// asi-http-request  
4 -//  
5 -// Created by Ben Copsey on 25/08/2008.  
6 -// Copyright 2008 All-Seeing Interactive. All rights reserved.  
7 -//  
8 -  
9 -#import <Cocoa/Cocoa.h>  
10 -  
11 -  
12 -@interface ASIHTTPCookie : NSObject {  
13 - NSString *name;  
14 - NSString *value;  
15 - NSDate *expires;  
16 - NSString *path;  
17 - NSString *domain;  
18 - BOOL requiresHTTPS;  
19 -}  
20 -  
21 -- (void)setValue:(NSString *)newValue forProperty:(NSString *)property;  
22 -  
23 -+ (NSMutableArray *)cookiesFromHeader:(NSString *)header;  
24 -+ (NSString *)urlEncodedValue:(NSString *)string;  
25 -+ (NSString *)urlDecodedValue:(NSString *)string;  
26 -  
27 -@property (retain) NSString *name;  
28 -@property (retain) NSString *value;  
29 -@property (retain) NSDate *expires;  
30 -@property (retain) NSString *path;  
31 -@property (retain) NSString *domain;  
32 -@property (assign) BOOL requiresHTTPS;  
33 -  
34 -@end  
1 -//  
2 -// ASIHTTPCookie.m  
3 -// asi-http-request  
4 -//  
5 -// Created by Ben Copsey on 25/08/2008.  
6 -// Copyright 2008 All-Seeing Interactive. All rights reserved.  
7 -//  
8 -  
9 -#import "ASIHTTPCookie.h"  
10 -  
11 -@implementation ASIHTTPCookie  
12 -  
13 -- (void)setValue:(NSString *)newValue forProperty:(NSString *)property  
14 -{  
15 - NSString *prop = [property lowercaseString];  
16 - if ([prop isEqualToString:@"expires"]) {  
17 - [self setExpires:[NSDate dateWithNaturalLanguageString:newValue]];  
18 - return;  
19 - } else if ([prop isEqualToString:@"domain"]) {  
20 - [self setDomain:newValue];  
21 - return;  
22 - } else if ([prop isEqualToString:@"path"]) {  
23 - [self setPath:newValue];  
24 - return;  
25 - } else if ([prop isEqualToString:@"secure"]) {  
26 - [self setRequiresHTTPS:[newValue isEqualToString:@"1"]];  
27 - return;  
28 - }  
29 - if (![self name] && ![self value]) {  
30 - [self setName:property];  
31 - [self setValue:newValue];  
32 - }  
33 -}  
34 -  
35 -  
36 -// 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!  
37 -// 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  
38 -  
39 -  
40 -+ (NSMutableArray *)cookiesFromHeader:(NSString *)header  
41 -{  
42 - NSMutableArray *cookies = [[[NSMutableArray alloc] init] autorelease];  
43 - ASIHTTPCookie *cookie = [[[ASIHTTPCookie alloc] init] autorelease];  
44 -  
45 - NSArray *parts = [header componentsSeparatedByString:@"="];  
46 - int i;  
47 - NSString *name;  
48 - NSString *value;  
49 - NSArray *components;  
50 - NSString *newKey;  
51 - NSString *terminator;  
52 - for (i=0; i<[parts count]; i++) {  
53 - NSString *part = [parts objectAtIndex:i];  
54 - if (i == 0) {  
55 - name = part;  
56 - continue;  
57 - } else if (i == [parts count]-1) {  
58 - [cookie setValue:[ASIHTTPCookie urlDecodedValue:part] forProperty:name];  
59 - [cookies addObject:cookie];  
60 - continue;  
61 - }  
62 - components = [part componentsSeparatedByString:@" "];  
63 - newKey = [components lastObject];  
64 - value = [part substringWithRange:NSMakeRange(0,[part length]-[newKey length]-2)];  
65 - [cookie setValue:[ASIHTTPCookie urlDecodedValue:value] forProperty:name];  
66 -  
67 - terminator = [part substringWithRange:NSMakeRange([part length]-[newKey length]-2,1)];  
68 - if ([terminator isEqualToString:@","]) {  
69 - [cookies addObject:cookie];  
70 - cookie = [[[ASIHTTPCookie alloc] init] autorelease];  
71 - }  
72 - name = newKey;  
73 - }  
74 -  
75 - return cookies;  
76 -  
77 -}  
78 -  
79 -+ (NSString *)urlDecodedValue:(NSString *)string  
80 -{  
81 - NSMutableString *s = [NSMutableString stringWithString:[string stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];  
82 - //Also swap plus signs for spaces  
83 - [s replaceOccurrencesOfString:@"+" withString:@" " options:NSLiteralSearch range:NSMakeRange(0, [s length])];  
84 - return [NSString stringWithString:s];  
85 -}  
86 -  
87 -+ (NSString *)urlEncodedValue:(NSString *)string  
88 -{  
89 - return [string stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];  
90 -}  
91 -  
92 -@synthesize name;  
93 -@synthesize value;  
94 -@synthesize expires;  
95 -@synthesize path;  
96 -@synthesize domain;  
97 -@synthesize requiresHTTPS;  
98 -@end  
99 -  
100 -  
@@ -39,7 +39,7 @@ @@ -39,7 +39,7 @@
39 NSMutableArray *requestCookies; 39 NSMutableArray *requestCookies;
40 40
41 //Will be populated with Cookies 41 //Will be populated with Cookies
42 - NSMutableArray *responseCookies; 42 + NSArray *responseCookies;
43 43
44 //If use cokie persistance is true, network requests will present valid cookies from previous requests 44 //If use cokie persistance is true, network requests will present valid cookies from previous requests
45 BOOL useCookiePersistance; 45 BOOL useCookiePersistance;
@@ -224,17 +224,13 @@ @@ -224,17 +224,13 @@
224 // Remove credentials from the keychain 224 // Remove credentials from the keychain
225 + (void)removeCredentialsForHost:(NSString *)host port:(int)port protocol:(NSString *)protocol realm:(NSString *)realm; 225 + (void)removeCredentialsForHost:(NSString *)host port:(int)port protocol:(NSString *)protocol realm:(NSString *)realm;
226 226
227 -// Store cookies for a particular request in the session 227 +// We keep track of any cookies we accept, so that we can remove them from the persistent store later
228 -+ (void)recordCookiesInSessionForRequest:(ASIHTTPRequest *)request;  
229 -  
230 + (void)setSessionCookies:(NSMutableArray *)newSessionCookies; 228 + (void)setSessionCookies:(NSMutableArray *)newSessionCookies;
231 + (NSMutableArray *)sessionCookies; 229 + (NSMutableArray *)sessionCookies;
232 230
233 // Dump all session data (authentication and cookies) 231 // Dump all session data (authentication and cookies)
234 + (void)clearSession; 232 + (void)clearSession;
235 233
236 -  
237 -  
238 @property (retain) NSString *username; 234 @property (retain) NSString *username;
239 @property (retain) NSString *password; 235 @property (retain) NSString *password;
240 @property (retain) NSString *domain; 236 @property (retain) NSString *domain;
@@ -253,7 +249,7 @@ @@ -253,7 +249,7 @@
253 @property (assign,readonly) BOOL complete; 249 @property (assign,readonly) BOOL complete;
254 @property (retain) NSDictionary *responseHeaders; 250 @property (retain) NSDictionary *responseHeaders;
255 @property (retain) NSMutableArray *requestCookies; 251 @property (retain) NSMutableArray *requestCookies;
256 -@property (retain) NSMutableArray *responseCookies; 252 +@property (retain) NSArray *responseCookies;
257 @property (assign) BOOL useCookiePersistance; 253 @property (assign) BOOL useCookiePersistance;
258 @property (retain) NSDictionary *requestCredentials; 254 @property (retain) NSDictionary *requestCredentials;
259 @property (assign) int responseStatusCode; 255 @property (assign) int responseStatusCode;
@@ -11,7 +11,7 @@ @@ -11,7 +11,7 @@
11 // See: http://developer.apple.com/samplecode/ImageClient/listing37.html 11 // See: http://developer.apple.com/samplecode/ImageClient/listing37.html
12 12
13 #import "ASIHTTPRequest.h" 13 #import "ASIHTTPRequest.h"
14 -#import "ASIHTTPCookie.h" 14 +#import "NSHTTPCookieAdditions.h"
15 15
16 static NSString *NetworkRequestErrorDomain = @"com.Your-Company.Your-Product.NetworkError."; 16 static NSString *NetworkRequestErrorDomain = @"com.Your-Company.Your-Product.NetworkError.";
17 17
@@ -168,47 +168,23 @@ static void ReadStreamClientCallBack(CFReadStreamRef readStream, CFStreamEventTy @@ -168,47 +168,23 @@ static void ReadStreamClientCallBack(CFReadStreamRef readStream, CFStreamEventTy
168 //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. 168 //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.
169 NSString *stringBoundary = @"0xKhTmLbOuNdArY"; 169 NSString *stringBoundary = @"0xKhTmLbOuNdArY";
170 170
171 - //Add cookies from session 171 + //Add cookies from the persistant (mac os global) store
172 - if (useCookiePersistance && [[ASIHTTPRequest sessionCookies] count] > 0) { 172 + if (useCookiePersistance) {
173 - ASIHTTPCookie *requestCookie; 173 + NSArray *cookies = [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookiesForURL:url];
174 - ASIHTTPCookie *storedCookie; 174 + if (cookies) {
175 - for (storedCookie in sessionCookies) { 175 + [requestCookies addObjectsFromArray:cookies];
176 - BOOL foundExistingCookie = NO;  
177 - //Look for existing cookies in the request - these will always take precedence over session stored cookies  
178 - for (requestCookie in requestCookies) {  
179 - if ([[requestCookie domain] isEqualToString:[storedCookie domain]]) {  
180 - if ([[requestCookie path] isEqualToString:[storedCookie path]] || (![requestCookie path] && ![storedCookie path])) {  
181 - if ([[requestCookie name] isEqualToString:[storedCookie name]]) {  
182 - foundExistingCookie = YES;  
183 - break;  
184 - }  
185 - }  
186 - }  
187 - }  
188 - if (!foundExistingCookie) {  
189 - [requestCookies addObject:storedCookie];  
190 - }  
191 } 176 }
192 } 177 }
193 178
194 //Apply request cookies 179 //Apply request cookies
195 if ([requestCookies count] > 0) { 180 if ([requestCookies count] > 0) {
196 - ASIHTTPCookie *cookie; 181 + NSHTTPCookie *cookie;
197 NSString *cookieHeader = nil; 182 NSString *cookieHeader = nil;
198 for (cookie in requestCookies) { 183 for (cookie in requestCookies) {
199 - //Ensure the cookie is valid for this request 184 + if (!cookieHeader) {
200 - if ([[[url host] substringWithRange:NSMakeRange([[url host] length]-[[cookie domain] length],[[cookie domain] length])] isEqualToString:[cookie domain]]) { 185 + cookieHeader = [NSString stringWithFormat: @"%@=%@",[cookie name],[cookie encodedValue]];
201 - if ([[[url path] substringWithRange:NSMakeRange(0,[[cookie path] length])] isEqualToString:[cookie path]]) { 186 + } else {
202 - if (![cookie requiresHTTPS] || [[url port] intValue] == 443) { 187 + cookieHeader = [NSString stringWithFormat: @"%@; %@=%@",cookieHeader,[cookie name],[cookie encodedValue]];
203 - if (![cookie expires] || [[cookie expires] timeIntervalSinceNow] > 0) {  
204 - if (!cookieHeader) {  
205 - cookieHeader = [NSString stringWithFormat: @"%@=%@",[cookie name],[ASIHTTPCookie urlEncodedValue:[cookie value]]];  
206 - } else {  
207 - cookieHeader = [NSString stringWithFormat: @"%@; %@=%@",cookieHeader,[cookie name],[ASIHTTPCookie urlEncodedValue:[cookie value]]];  
208 - }  
209 - }  
210 - }  
211 - }  
212 } 188 }
213 } 189 }
214 if (cookieHeader) { 190 if (cookieHeader) {
@@ -475,11 +451,20 @@ static void ReadStreamClientCallBack(CFReadStreamRef readStream, CFStreamEventTy @@ -475,11 +451,20 @@ static void ReadStreamClientCallBack(CFReadStreamRef readStream, CFStreamEventTy
475 } 451 }
476 452
477 //Handle cookies 453 //Handle cookies
478 - NSString *cookieHeader = [responseHeaders valueForKey:@"Set-Cookie"]; 454 + NSArray *cookies = [NSHTTPCookie cookiesWithResponseHeaderFields:responseHeaders forURL:url];
479 - if (cookieHeader) { 455 + [self setResponseCookies:cookies];
480 - [self setResponseCookies:[ASIHTTPCookie cookiesFromHeader:cookieHeader]]; 456 +
481 - if (useCookiePersistance) { 457 + if (useCookiePersistance) {
482 - [ASIHTTPRequest recordCookiesInSessionForRequest:self]; 458 + //Store cookies in global persistent store
  459 + [[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookies:cookies forURL:url mainDocumentURL:nil];
  460 +
  461 + //We also keep any cookies in the sessionCookies array, so that we have a reference to them if we need to remove them later
  462 + if (!sessionCookies) {
  463 + [ASIHTTPRequest setSessionCookies:[[[NSMutableArray alloc] init] autorelease]];
  464 + NSHTTPCookie *cookie;
  465 + for (cookie in cookies) {
  466 + [[ASIHTTPRequest sessionCookies] addObject:cookie];
  467 + }
483 } 468 }
484 } 469 }
485 470
@@ -829,37 +814,6 @@ static void ReadStreamClientCallBack(CFReadStreamRef readStream, CFStreamEventTy @@ -829,37 +814,6 @@ static void ReadStreamClientCallBack(CFReadStreamRef readStream, CFStreamEventTy
829 814
830 } 815 }
831 816
832 -+ (void)recordCookiesInSessionForRequest:(ASIHTTPRequest *)request  
833 -{  
834 - if (!sessionCookies) {  
835 - [self setSessionCookies:[[[NSMutableArray alloc] init] autorelease]];  
836 - }  
837 - ASIHTTPCookie *newCookie;  
838 - ASIHTTPCookie *storedCookie;  
839 - for (newCookie in [request responseCookies]) {  
840 - //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  
841 - if (![newCookie domain]) {  
842 - [newCookie setDomain:[[request url] host]];  
843 - }  
844 - int i = 0;  
845 - BOOL foundExistingCookie = NO;  
846 - for (storedCookie in sessionCookies) {  
847 - if ([[storedCookie domain] isEqualToString:[newCookie domain]]) {  
848 - if ([[storedCookie path] isEqualToString:[newCookie path]] || (![storedCookie path] && ![newCookie path])) {  
849 - if ([[storedCookie name] isEqualToString:[newCookie name]]) {  
850 - foundExistingCookie = YES;  
851 - [sessionCookies replaceObjectAtIndex:i withObject:newCookie];  
852 - break;  
853 - }  
854 - }  
855 - }  
856 - i++;  
857 - }  
858 - if (!foundExistingCookie) {  
859 - [sessionCookies addObject:newCookie];  
860 - }  
861 - }  
862 -}  
863 817
864 + (NSMutableArray *)sessionCookies 818 + (NSMutableArray *)sessionCookies
865 { 819 {
@@ -868,6 +822,11 @@ static void ReadStreamClientCallBack(CFReadStreamRef readStream, CFStreamEventTy @@ -868,6 +822,11 @@ static void ReadStreamClientCallBack(CFReadStreamRef readStream, CFStreamEventTy
868 822
869 + (void)setSessionCookies:(NSMutableArray *)newSessionCookies 823 + (void)setSessionCookies:(NSMutableArray *)newSessionCookies
870 { 824 {
  825 + //Remove existing cookies from the persistent store
  826 + NSHTTPCookie *cookie;
  827 + for (cookie in newSessionCookies) {
  828 + [[NSHTTPCookieStorage sharedHTTPCookieStorage] deleteCookie:cookie];
  829 + }
871 [sessionCookies release]; 830 [sessionCookies release];
872 sessionCookies = [newSessionCookies retain]; 831 sessionCookies = [newSessionCookies retain];
873 } 832 }
@@ -881,6 +840,7 @@ static void ReadStreamClientCallBack(CFReadStreamRef readStream, CFStreamEventTy @@ -881,6 +840,7 @@ static void ReadStreamClientCallBack(CFReadStreamRef readStream, CFStreamEventTy
881 } 840 }
882 841
883 842
  843 +
884 @synthesize username; 844 @synthesize username;
885 @synthesize password; 845 @synthesize password;
886 @synthesize domain; 846 @synthesize domain;
@@ -8,7 +8,7 @@ @@ -8,7 +8,7 @@
8 8
9 #import "ASIHTTPRequestTests.h" 9 #import "ASIHTTPRequestTests.h"
10 #import "ASIHTTPRequest.h" 10 #import "ASIHTTPRequest.h"
11 -#import "ASIHTTPCookie.h" 11 +#import "NSHTTPCookieAdditions.h"
12 12
13 @implementation ASIHTTPRequestTests 13 @implementation ASIHTTPRequestTests
14 14
@@ -120,24 +120,6 @@ More tests needed for: @@ -120,24 +120,6 @@ More tests needed for:
120 { 120 {
121 BOOL success; 121 BOOL success;
122 122
123 - //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  
124 - NSString *dte = @"Sun, 06 Nov 1994 08:49:37 GMT";  
125 -  
126 - NSDate *date = [NSDate dateWithNaturalLanguageString:dte];  
127 - NSDate *referenceDate = [NSDate dateWithString:@"1994-11-06 08:49:37 +0000"];  
128 - success = [date isEqualToDate:referenceDate];  
129 - STAssertTrue(success,@"Date parse 1 failed");  
130 -  
131 - dte = @"Sunday, 06-Nov-94 08:49:37 GMT";  
132 - date = [NSDate dateWithNaturalLanguageString:dte];  
133 - success = [date isEqualToDate:referenceDate];  
134 - STAssertTrue(success,@"Date parse 2 failed");  
135 -  
136 - dte = @"Sun Nov 6 08:49:37 1994";  
137 - date = [NSDate dateWithNaturalLanguageString:dte];  
138 - success = [date isEqualToDate:referenceDate];  
139 - STAssertTrue(success,@"Date parse 3 failed");  
140 -  
141 NSURL *url = [[[NSURL alloc] initWithString:@"http://allseeing-i.com/asi-http-request/tests/set_cookie"] autorelease]; 123 NSURL *url = [[[NSURL alloc] initWithString:@"http://allseeing-i.com/asi-http-request/tests/set_cookie"] autorelease];
142 ASIHTTPRequest *request = [[[ASIHTTPRequest alloc] initWithURL:url] autorelease]; 124 ASIHTTPRequest *request = [[[ASIHTTPRequest alloc] initWithURL:url] autorelease];
143 [request setUseCookiePersistance:YES]; 125 [request setUseCookiePersistance:YES];
@@ -149,14 +131,15 @@ More tests needed for: @@ -149,14 +131,15 @@ More tests needed for:
149 NSArray *cookies = [request responseCookies]; 131 NSArray *cookies = [request responseCookies];
150 STAssertNotNil(cookies,@"Failed to store cookie data in responseCookies"); 132 STAssertNotNil(cookies,@"Failed to store cookie data in responseCookies");
151 133
152 - ASIHTTPCookie *cookie = nil; 134 + NSHTTPCookie *cookie = nil;
153 BOOL foundCookie = NO; 135 BOOL foundCookie = NO;
154 for (cookie in cookies) { 136 for (cookie in cookies) {
155 if ([[cookie name] isEqualToString:@"ASIHTTPRequestTestCookie"]) { 137 if ([[cookie name] isEqualToString:@"ASIHTTPRequestTestCookie"]) {
156 foundCookie = YES; 138 foundCookie = YES;
157 - success = [[cookie value] isEqualToString:@"This is the value"]; 139 + NSLog(@"%@",cookie);
  140 + success = [[cookie decodedValue] isEqualToString:@"This is the value"];
158 STAssertTrue(success,@"Failed to store the correct value for a cookie"); 141 STAssertTrue(success,@"Failed to store the correct value for a cookie");
159 - success = [[cookie domain] isEqualToString:@"allseeing-i.com"]; 142 + success = [[cookie domain] isEqualToString:@".allseeing-i.com"];
160 STAssertTrue(success,@"Failed to store the correct domain for a cookie"); 143 STAssertTrue(success,@"Failed to store the correct domain for a cookie");
161 success = [[cookie path] isEqualToString:@"/asi-http-request/tests"]; 144 success = [[cookie path] isEqualToString:@"/asi-http-request/tests"];
162 STAssertTrue(success,@"Failed to store the correct path for a cookie"); 145 STAssertTrue(success,@"Failed to store the correct path for a cookie");
@@ -26,7 +26,7 @@ @@ -26,7 +26,7 @@
26 26
27 - (IBAction)simpleURLFetch:(id)sender 27 - (IBAction)simpleURLFetch:(id)sender
28 { 28 {
29 - ASIHTTPRequest *request = [[[ASIHTTPRequest alloc] initWithURL:[NSURL URLWithString:@"http://allseeing-i.com"]] autorelease]; 29 + ASIHTTPRequest *request = [[[ASIHTTPRequest alloc] initWithURL:[NSURL URLWithString:@"http://asi/"]] autorelease];
30 30
31 //Customise our user agent, for no real reason 31 //Customise our user agent, for no real reason
32 [request addRequestHeader:@"User-Agent" value:@"ASIHTTPRequest"]; 32 [request addRequestHeader:@"User-Agent" value:@"ASIHTTPRequest"];
  1 +//
  2 +// NSHTTPCookieAdditions.h
  3 +// asi-http-request
  4 +//
  5 +// Created by Ben Copsey on 12/09/2008.
  6 +// Copyright 2008 All-Seeing Interactive. All rights reserved.
  7 +//
  8 +
  9 +#import <Cocoa/Cocoa.h>
  10 +
  11 +@interface NSHTTPCookie (ValueEncodingAdditions)
  12 +
  13 +- (NSString *)encodedValue;
  14 +- (NSString *)decodedValue;
  15 +
  16 +@end
  1 +//
  2 +// NSHTTPCookieAdditions.m
  3 +// asi-http-request
  4 +//
  5 +// Created by Ben Copsey on 12/09/2008.
  6 +// Copyright 2008 All-Seeing Interactive. All rights reserved.
  7 +//
  8 +
  9 +#import "NSHTTPCookieAdditions.h"
  10 +
  11 +@implementation NSHTTPCookie (ValueEncodingAdditions)
  12 +
  13 +- (NSString *)decodedValue
  14 +{
  15 + NSMutableString *s = [NSMutableString stringWithString:[[self value] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
  16 + //Also swap plus signs for spaces
  17 + [s replaceOccurrencesOfString:@"+" withString:@" " options:NSLiteralSearch range:NSMakeRange(0, [s length])];
  18 + return [NSString stringWithString:s];
  19 +}
  20 +
  21 +- (NSString *)encodedValue
  22 +{
  23 + return [[self value] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
  24 +}
  25 +
  26 +@end
  27 +
  28 +
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.