ASIHTTPCookie.m
2.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
//
// 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 dateFrom
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