ObjFW  Diff

Differences From Artifact [1dd74fe07e]:

To Artifact [3e1112d548]:


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
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

-
+



















-
+







/*
 * Copyright (c) 2008-2022 Jonathan Schleifer <js@nil.im>
 * Copyright (c) 2008-2024 Jonathan Schleifer <js@nil.im>
 *
 * All rights reserved.
 *
 * This file is part of ObjFW. It may be distributed under the terms of the
 * Q Public License 1.0, which can be found in the file LICENSE.QPL included in
 * the packaging of this file.
 *
 * Alternatively, it may be distributed under the terms of the GNU General
 * Public License, either version 2 or 3, which can be found in the file
 * LICENSE.GPLv2 or LICENSE.GPLv3 respectively included in the packaging of this
 * file.
 */

#include "config.h"

#import "OFHTTPCookieManager.h"
#import "OFArray.h"
#import "OFDate.h"
#import "OFHTTPCookie.h"
#import "OFURI.h"
#import "OFIRI.h"

@implementation OFHTTPCookieManager
+ (instancetype)manager
{
	return [[[self alloc] init] autorelease];
}

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
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







-
+


-
+

+
+





-
+







-
-
-
+
+
+

-
+







}

- (OFArray OF_GENERIC(OFHTTPCookie *) *)cookies
{
	return [[_cookies copy] autorelease];
}

- (void)addCookie: (OFHTTPCookie *)cookie forURI: (OFURI *)URI
- (void)addCookie: (OFHTTPCookie *)cookie forIRI: (OFIRI *)IRI
{
	void *pool = objc_autoreleasePoolPush();
	OFString *cookieDomain, *URIHost;
	OFString *cookieDomain, *IRIHost;
	size_t i;

	IRI = IRI.IRIByAddingPercentEncodingForUnicodeCharacters;

	if (![cookie.path hasPrefix: @"/"])
		cookie.path = @"/";

	if (cookie.secure &&
	    [URI.scheme caseInsensitiveCompare: @"https"] != OFOrderedSame) {
	    [IRI.scheme caseInsensitiveCompare: @"https"] != OFOrderedSame) {
		objc_autoreleasePoolPop(pool);
		return;
	}

	cookieDomain = cookie.domain.lowercaseString;
	cookie.domain = cookieDomain;

	URIHost = URI.host.lowercaseString;
	if (![cookieDomain isEqual: URIHost]) {
		URIHost = [@"." stringByAppendingString: URIHost];
	IRIHost = IRI.host.lowercaseString;
	if (![cookieDomain isEqual: IRIHost]) {
		IRIHost = [@"." stringByAppendingString: IRIHost];

		if (![cookieDomain hasSuffix: URIHost]) {
		if (![cookieDomain hasSuffix: IRIHost]) {
			objc_autoreleasePoolPop(pool);
			return;
		}
	}

	i = 0;
	for (OFHTTPCookie *iter in _cookies) {
96
97
98
99
100
101
102
103

104
105
106

107
108
109

110
111

112


113
114

115
116

117
118
119
120
121
122
123

124
125
126
127

128
129
130

131
132

133
134
135
136
137
138

139
140
141

142
143
144

145
146
147
148
149

150
151

152
153
154
155
156
157
158

159
160
161
162

163
164
165
166
167
168
169
170


171
172
173
174
175
176
177
98
99
100
101
102
103
104

105
106
107

108
109
110

111
112
113
114
115
116
117
118

119
120

121
122
123
124
125
126
127

128
129
130
131

132
133
134

135
136

137
138
139
140
141
142

143
144
145

146
147
148

149
150
151
152
153

154
155

156
157
158
159
160
161
162

163
164
165
166

167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184







-
+


-
+


-
+


+

+
+

-
+

-
+






-
+



-
+


-
+

-
+





-
+


-
+


-
+




-
+

-
+






-
+



-
+








+
+








	[_cookies addObject: cookie];

	objc_autoreleasePoolPop(pool);
}

- (void)addCookies: (OFArray OF_GENERIC(OFHTTPCookie *) *)cookies
	    forURI: (OFURI *)URI
	    forIRI: (OFIRI *)IRI
{
	for (OFHTTPCookie *cookie in cookies)
		[self addCookie: cookie forURI: URI];
		[self addCookie: cookie forIRI: IRI];
}

- (OFArray OF_GENERIC(OFHTTPCookie *) *)cookiesForURI: (OFURI *)URI
- (OFArray OF_GENERIC(OFHTTPCookie *) *)cookiesForIRI: (OFIRI *)IRI
{
	OFMutableArray *ret = [OFMutableArray array];
	void *pool = objc_autoreleasePoolPush();

	IRI = IRI.IRIByAddingPercentEncodingForUnicodeCharacters;

	for (OFHTTPCookie *cookie in _cookies) {
		void *pool;
		void *pool2;
		OFDate *expires;
		OFString *cookieDomain, *URIHost, *cookiePath, *URIPath;
		OFString *cookieDomain, *IRIHost, *cookiePath, *IRIPath;
		bool match;

		expires = cookie.expires;
		if (expires != nil && expires.timeIntervalSinceNow <= 0)
			continue;

		if (cookie.secure && [URI.scheme caseInsensitiveCompare:
		if (cookie.secure && [IRI.scheme caseInsensitiveCompare:
		    @"https"] != OFOrderedSame)
			continue;

		pool = objc_autoreleasePoolPush();
		pool2 = objc_autoreleasePoolPush();

		cookieDomain = cookie.domain.lowercaseString;
		URIHost = URI.host.lowercaseString;
		IRIHost = IRI.host.lowercaseString;
		if ([cookieDomain hasPrefix: @"."]) {
			if ([URIHost hasSuffix: cookieDomain])
			if ([IRIHost hasSuffix: cookieDomain])
				match = true;
			else {
				cookieDomain =
				    [cookieDomain substringFromIndex: 1];

				match = [cookieDomain isEqual: URIHost];
				match = [cookieDomain isEqual: IRIHost];
			}
		} else
			match = [cookieDomain isEqual: URIHost];
			match = [cookieDomain isEqual: IRIHost];

		if (!match) {
			objc_autoreleasePoolPop(pool);
			objc_autoreleasePoolPop(pool2);
			continue;
		}

		cookiePath = cookie.path;
		URIPath = URI.path;
		IRIPath = IRI.path;
		if (![cookiePath isEqual: @"/"]) {
			if ([cookiePath isEqual: URIPath])
			if ([cookiePath isEqual: IRIPath])
				match = true;
			else {
				if (![cookiePath hasSuffix: @"/"])
					cookiePath = [cookiePath
					    stringByAppendingString: @"/"];

				match = [URIPath hasPrefix: cookiePath];
				match = [IRIPath hasPrefix: cookiePath];
			}

			if (!match) {
				objc_autoreleasePoolPop(pool);
				objc_autoreleasePoolPop(pool2);
				continue;
			}
		}

		[ret addObject: cookie];
	}

	[ret makeImmutable];

	objc_autoreleasePoolPop(pool);

	return ret;
}

- (void)purgeExpiredCookies
{
	for (size_t i = 0, count = _cookies.count; i < count; i++) {