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