]> err.no Git - sope/blob - sope-appserver/NGObjWeb/NGHttp/NGHttpCookie.m
renamed packages as discussed in the developer list
[sope] / sope-appserver / NGObjWeb / NGHttp / NGHttpCookie.m
1 /*
2   Copyright (C) 2000-2003 SKYRIX Software AG
3
4   This file is part of OGo
5
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
9   later version.
10
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.
15
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
19   02111-1307, USA.
20 */
21 // $Id$
22
23 #import "common.h"
24 #import "NGHttpCookie.h"
25
26 @implementation NGHttpCookie
27
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";
30
31 + (id)cookieWithName:(NSString *)_name {
32   return [[[self alloc] initWithName:_name value:nil] autorelease];
33 }
34
35 - (id)initWithName:(NSString *)_name value:(id)_value {
36   if ((self = [super init])) {
37     self->name  = [_name copy];
38     self->value = [_value retain];
39   }
40   return self;
41 }
42
43 - (void)dealloc {
44   [self->name       release];
45   [self->value      release];
46   [self->expireDate release];
47   [self->path       release];
48   [self->domainName release];
49   [super dealloc];
50 }
51
52 // accessors
53
54 - (void)setCookieName:(NSString *)_name {
55   if (_name != self->name) {
56     [self->name autorelease];
57     self->name = [_name copy];
58   }
59 }
60 - (NSString *)cookieName {
61   return self->name;
62 }
63
64 - (void)setValue:(id)_value {
65   if (_value != self->value) {
66     [self->value autorelease];
67     self->value = [_value retain];
68   }
69 }
70 - (id)value {
71   return self->value;
72 }
73
74 - (void)addAdditionalValue:(id)_value {
75   if (![self->value isKindOfClass:[NSMutableArray class]]) {
76     NSMutableArray *array = [[NSMutableArray alloc] init];
77
78     if (self->value) [array addObject:self->value];
79     [self->value release];
80
81     self->value = array;
82   }
83
84   NSAssert([self->value isKindOfClass:[NSMutableArray class]],
85            @"invalid object state, value should be mutable array");
86
87   if (_value)
88     [self->value addObject:_value];
89 }
90
91 - (void)setExpireDate:(NSDate *)_date {
92   if (_date != self->expireDate) {
93     [self->expireDate autorelease];
94     self->expireDate = [_date copy];
95   }
96 }
97 - (NSDate *)expireDate {
98   return self->expireDate;
99 }
100 - (BOOL)doesExpireWhenUserSessionEnds {
101   return (self->expireDate == nil) ? YES : NO;
102 }
103
104 - (void)setPath:(NSString *)_path {
105   if (_path != self->path) {
106     RELEASE(self->path);
107     self->path = [_path copyWithZone:[self zone]];
108   }
109 }
110 - (NSString *)path {
111   return self->path;
112 }
113
114 - (void)setDomainName:(NSString *)_domainName {
115   if (_domainName != self->domainName) {
116     RELEASE(self->domainName);
117     self->domainName = [_domainName copyWithZone:[self zone]];
118   }
119 }
120 - (NSString *)domainName {
121   return self->domainName;
122 }
123
124 - (void)setNeedsSecureChannel:(BOOL)_flag {
125   self->onlyIfSecure = _flag;
126 }
127 - (BOOL)needsSecureChannel {
128   return self->onlyIfSecure;
129 }
130
131 /* description */
132
133 - (NSString *)stringValue {
134   NSMutableString *str = [NSMutableString stringWithCapacity:64];
135   
136   [str appendString:[self->name stringByEscapingURL]];
137   [str appendString:@"="];
138   [str appendString:[[self->value stringValue] stringByEscapingURL]];
139
140   if (self->expireDate) {
141     // TODO: may deliver in wrong timezone due to buggy NSDate
142     NSString *s;
143     [str appendString:@"; expires="];
144     s = [self->expireDate
145              descriptionWithCalendarFormat:cookieDateFormat
146              timeZone:[NSTimeZone timeZoneWithAbbreviation:@"GMT"]
147              locale:nil];
148     [str appendString:s];
149   }
150   if (self->path) {
151     [str appendString:@"; path="];
152     [str appendString:self->path];
153   }
154   if (self->domainName) {
155     [str appendString:@"; domain="];
156     [str appendString:self->domainName];
157   }
158   if (self->onlyIfSecure)
159     [str appendString:@"; secure"];
160
161   return str;
162 }
163
164 - (NSString *)description {
165   NSMutableString *str = [NSMutableString stringWithCapacity:128];
166
167   [str appendFormat:@"<%@[0x%08X]: name=%@ value=%@",
168          NSStringFromClass([self class]), self,
169          self->name, self->value];
170
171   if (self->expireDate) {
172     [str appendString:@" expires="];
173     [str appendString:[self->expireDate description]];
174   }
175   
176   if (self->path) {
177     [str appendString:@" path="];
178     [str appendString:self->path];
179   }
180   if (self->domainName) {
181     [str appendString:@" domain="];
182     [str appendString:self->domainName];
183   }
184   if (self->onlyIfSecure)
185     [str appendString:@" secure"];
186
187   [str appendString:@">"];
188
189   return str;
190 }
191
192 @end /* NGHttpCookie */