2 Copyright (C) 2000-2005 SKYRIX Software AG
4 This file is part of SOPE.
6 SOPE is free software; you can redistribute it and/or modify it under
7 the terms of the GNU Lesser General Public License as published by the
8 Free Software Foundation; either version 2, or (at your option) any
11 SOPE is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
14 License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with SOPE; see the file COPYING. If not, write to the
18 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
23 #import "NGHttpCookie.h"
25 @implementation NGHttpCookie
27 // abbr weekday, day-of-month, abbr-month, year hour:min:sec GMT
28 static NSString *cookieDateFormat = @"%a, %d-%b-%Y %H:%M:%S %Z";
30 + (id)cookieWithName:(NSString *)_name {
31 return [[[self alloc] initWithName:_name value:nil] autorelease];
34 - (id)initWithName:(NSString *)_name value:(id)_value {
35 if ((self = [super init])) {
36 self->name = [_name copy];
37 self->value = [_value retain];
44 [self->value release];
45 [self->expireDate release];
47 [self->domainName release];
53 - (void)setCookieName:(NSString *)_name {
54 if (_name != self->name) {
55 [self->name autorelease];
56 self->name = [_name copy];
59 - (NSString *)cookieName {
63 - (void)setValue:(id)_value {
64 if (_value != self->value) {
65 [self->value autorelease];
66 self->value = [_value retain];
73 - (void)addAdditionalValue:(id)_value {
74 if (![self->value isKindOfClass:[NSMutableArray class]]) {
75 NSMutableArray *array = [[NSMutableArray alloc] init];
77 if (self->value) [array addObject:self->value];
78 [self->value release];
83 NSAssert([self->value isKindOfClass:[NSMutableArray class]],
84 @"invalid object state, value should be mutable array");
87 [self->value addObject:_value];
90 - (void)setExpireDate:(NSDate *)_date {
91 if (_date != self->expireDate) {
92 [self->expireDate autorelease];
93 self->expireDate = [_date copy];
96 - (NSDate *)expireDate {
97 return self->expireDate;
99 - (BOOL)doesExpireWhenUserSessionEnds {
100 return (self->expireDate == nil) ? YES : NO;
103 - (void)setPath:(NSString *)_path {
104 if (_path != self->path) {
106 self->path = [_path copyWithZone:[self zone]];
113 - (void)setDomainName:(NSString *)_domainName {
114 if (_domainName != self->domainName) {
115 RELEASE(self->domainName);
116 self->domainName = [_domainName copyWithZone:[self zone]];
119 - (NSString *)domainName {
120 return self->domainName;
123 - (void)setNeedsSecureChannel:(BOOL)_flag {
124 self->onlyIfSecure = _flag;
126 - (BOOL)needsSecureChannel {
127 return self->onlyIfSecure;
132 - (NSString *)stringValue {
133 NSMutableString *str = [NSMutableString stringWithCapacity:64];
135 [str appendString:[self->name stringByEscapingURL]];
136 [str appendString:@"="];
137 [str appendString:[[self->value stringValue] stringByEscapingURL]];
139 if (self->expireDate) {
140 // TODO: may deliver in wrong timezone due to buggy NSDate
142 [str appendString:@"; expires="];
143 s = [self->expireDate
144 descriptionWithCalendarFormat:cookieDateFormat
145 timeZone:[NSTimeZone timeZoneWithAbbreviation:@"GMT"]
147 [str appendString:s];
150 [str appendString:@"; path="];
151 [str appendString:self->path];
153 if (self->domainName) {
154 [str appendString:@"; domain="];
155 [str appendString:self->domainName];
157 if (self->onlyIfSecure)
158 [str appendString:@"; secure"];
163 - (NSString *)description {
164 NSMutableString *str = [NSMutableString stringWithCapacity:128];
166 [str appendFormat:@"<%@[0x%p]: name=%@ value=%@",
167 NSStringFromClass([self class]), self,
168 self->name, self->value];
170 if (self->expireDate) {
171 [str appendString:@" expires="];
172 [str appendString:[self->expireDate description]];
176 [str appendString:@" path="];
177 [str appendString:self->path];
179 if (self->domainName) {
180 [str appendString:@" domain="];
181 [str appendString:self->domainName];
183 if (self->onlyIfSecure)
184 [str appendString:@" secure"];
186 [str appendString:@">"];
191 @end /* NGHttpCookie */