2 Copyright (C) 2000-2003 SKYRIX Software AG
4 This file is part of OGo
6 OGo 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 OGo 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 OGo; see the file COPYING. If not, write to the
18 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
24 #import "NGHttpCookie.h"
26 @implementation NGHttpCookie
28 // abbr weekday, day-of-month, abbr-month, year hour:min:sec GMT
29 static NSString *cookieDateFormat = @"%a, %d-%b-%Y %H:%M:%S %Z";
31 + (id)cookieWithName:(NSString *)_name {
32 return [[[self alloc] initWithName:_name value:nil] autorelease];
35 - (id)initWithName:(NSString *)_name value:(id)_value {
36 if ((self = [super init])) {
37 self->name = [_name copy];
38 self->value = [_value retain];
45 [self->value release];
46 [self->expireDate release];
48 [self->domainName release];
54 - (void)setCookieName:(NSString *)_name {
55 if (_name != self->name) {
56 [self->name autorelease];
57 self->name = [_name copy];
60 - (NSString *)cookieName {
64 - (void)setValue:(id)_value {
65 if (_value != self->value) {
66 [self->value autorelease];
67 self->value = [_value retain];
74 - (void)addAdditionalValue:(id)_value {
75 if (![self->value isKindOfClass:[NSMutableArray class]]) {
76 NSMutableArray *array = [[NSMutableArray alloc] init];
78 if (self->value) [array addObject:self->value];
79 [self->value release];
84 NSAssert([self->value isKindOfClass:[NSMutableArray class]],
85 @"invalid object state, value should be mutable array");
88 [self->value addObject:_value];
91 - (void)setExpireDate:(NSDate *)_date {
92 if (_date != self->expireDate) {
93 [self->expireDate autorelease];
94 self->expireDate = [_date copy];
97 - (NSDate *)expireDate {
98 return self->expireDate;
100 - (BOOL)doesExpireWhenUserSessionEnds {
101 return (self->expireDate == nil) ? YES : NO;
104 - (void)setPath:(NSString *)_path {
105 if (_path != self->path) {
107 self->path = [_path copyWithZone:[self zone]];
114 - (void)setDomainName:(NSString *)_domainName {
115 if (_domainName != self->domainName) {
116 RELEASE(self->domainName);
117 self->domainName = [_domainName copyWithZone:[self zone]];
120 - (NSString *)domainName {
121 return self->domainName;
124 - (void)setNeedsSecureChannel:(BOOL)_flag {
125 self->onlyIfSecure = _flag;
127 - (BOOL)needsSecureChannel {
128 return self->onlyIfSecure;
133 - (NSString *)stringValue {
134 NSMutableString *str = [NSMutableString stringWithCapacity:64];
136 [str appendString:[self->name stringByEscapingURL]];
137 [str appendString:@"="];
138 [str appendString:[[self->value stringValue] stringByEscapingURL]];
140 if (self->expireDate) {
141 // TODO: may deliver in wrong timezone due to buggy NSDate
143 [str appendString:@"; expires="];
144 s = [self->expireDate
145 descriptionWithCalendarFormat:cookieDateFormat
146 timeZone:[NSTimeZone timeZoneWithAbbreviation:@"GMT"]
148 [str appendString:s];
151 [str appendString:@"; path="];
152 [str appendString:self->path];
154 if (self->domainName) {
155 [str appendString:@"; domain="];
156 [str appendString:self->domainName];
158 if (self->onlyIfSecure)
159 [str appendString:@"; secure"];
164 - (NSString *)description {
165 NSMutableString *str = [NSMutableString stringWithCapacity:128];
167 [str appendFormat:@"<%@[0x%08X]: name=%@ value=%@",
168 NSStringFromClass([self class]), self,
169 self->name, self->value];
171 if (self->expireDate) {
172 [str appendString:@" expires="];
173 [str appendString:[self->expireDate description]];
177 [str appendString:@" path="];
178 [str appendString:self->path];
180 if (self->domainName) {
181 [str appendString:@" domain="];
182 [str appendString:self->domainName];
184 if (self->onlyIfSecure)
185 [str appendString:@" secure"];
187 [str appendString:@">"];
192 @end /* NGHttpCookie */