ObjFW  Diff

Differences From Artifact [d82c9df2c7]:

To Artifact [a0376d23d9]:


67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83










84
85
86
87
88

89
90

91
92
93
94
95

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
178
179
180
181
67
68
69
70
71
72
73










74
75
76
77
78
79
80
81
82
83
84
85
86
87

88
89

90
91
92
93
94

95
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
178
179
180
181







-
-
-
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+




-
+

-
+




-
+



-
+


-
+

-
+


-
+





-
+










-
+


-
+








-
+


-
+

-
+

-
+




-
+

-
+




-
+




-
+









-
+



-
+







	OFMutableArray OF_GENERIC(OFHTTPCookie *) *ret = [OFMutableArray array];
	void *pool = objc_autoreleasePoolPush();
	OFString *string = [headerFields objectForKey: @"Set-Cookie"];
	OFString *domain = URL.host;
	const OFUnichar *characters = string.characters;
	size_t length = string.length, last = 0;
	enum {
		StatePreName,
		StateName,
		StateExpectValue,
		StateValue,
		StateQuotedValue,
		StatePostQuotedValue,
		StatePreAttrName,
		StateAttrName,
		StateAttrValue
	} state = StatePreName;
		statePreName,
		stateName,
		stateExpectValue,
		stateValue,
		stateQuotedValue,
		statePostQuotedValue,
		statePreAttrName,
		stateAttrName,
		stateAttrValue
	} state = statePreName;
	OFString *name = nil, *value = nil;

	for (size_t i = 0; i < length; i++) {
		switch (state) {
		case StatePreName:
		case statePreName:
			if (characters[i] != ' ') {
				state = StateName;
				state = stateName;
				last = i;
				i--;
			}
			break;
		case StateName:
		case stateName:
			if (characters[i] == '=') {
				name = [string substringWithRange:
				    OFRangeMake(last, i - last)];
				state = StateExpectValue;
				state = stateExpectValue;
			}
			break;
		case StateExpectValue:
		case stateExpectValue:
			if (characters[i] == '"') {
				state = StateQuotedValue;
				state = stateQuotedValue;
				last = i + 1;
			} else {
				state = StateValue;
				state = stateValue;
				last = i;
			}

			i--;
			break;
		case StateValue:
		case stateValue:
			if (characters[i] == ';' || characters[i] == ',') {
				value = [string substringWithRange:
				    OFRangeMake(last, i - last)];

				[ret addObject:
				    [OFHTTPCookie cookieWithName: name
							   value: value
							  domain: domain]];

				state = (characters[i] == ';'
				    ? StatePreAttrName : StatePreName);
				    ? statePreAttrName : statePreName);
			}
			break;
		case StateQuotedValue:
		case stateQuotedValue:
			if (characters[i] == '"') {
				value = [string substringWithRange:
				    OFRangeMake(last, i - last)];
				[ret addObject:
				    [OFHTTPCookie cookieWithName: name
							   value: value
							  domain: domain]];

				state = StatePostQuotedValue;
				state = statePostQuotedValue;
			}
			break;
		case StatePostQuotedValue:
		case statePostQuotedValue:
			if (characters[i] == ';')
				state = StatePreAttrName;
				state = statePreAttrName;
			else if (characters[i] == ',')
				state = StatePreName;
				state = statePreName;
			else
				@throw [OFInvalidFormatException exception];

			break;
		case StatePreAttrName:
		case statePreAttrName:
			if (characters[i] != ' ') {
				state = StateAttrName;
				state = stateAttrName;
				last = i;
				i--;
			}
			break;
		case StateAttrName:
		case stateAttrName:
			if (characters[i] == '=') {
				name = [string substringWithRange:
				    OFRangeMake(last, i - last)];

				state = StateAttrValue;
				state = stateAttrValue;
				last = i + 1;
			} else if (characters[i] == ';' ||
			    characters[i] == ',') {
				name = [string substringWithRange:
				    OFRangeMake(last, i - last)];

				handleAttribute(ret.lastObject, name, nil);

				state = (characters[i] == ';'
				    ? StatePreAttrName : StatePreName);
				    ? statePreAttrName : statePreName);
			}

			break;
		case StateAttrValue:
		case stateAttrValue:
			if (characters[i] == ';' || characters[i] == ',') {
				value = [string substringWithRange:
				    OFRangeMake(last, i - last)];

				/*
				 * Expires often contains a comma, even though
				 * the comma is used as a separator for
194
195
196
197
198
199
200
201

202
203
204
205
206
207
208
209
210



211
212
213


214
215
216

217
218
219
220
221
222
223
224

225
226
227
228
229

230
231
232
233
234
235
236
237

238
239
240
241
242
243
244
194
195
196
197
198
199
200

201
202
203
204
205
206
207



208
209
210
211


212
213
214
215

216
217
218
219
220
221
222
223

224
225
226
227
228

229
230
231
232
233
234
235
236

237
238
239
240
241
242
243
244







-
+






-
-
-
+
+
+

-
-
+
+


-
+







-
+




-
+







-
+







				    [value isEqual: @"Sat"] ||
				    [value isEqual: @"Sun"]))
					break;

				handleAttribute(ret.lastObject, name, value);

				state = (characters[i] == ';'
				    ? StatePreAttrName : StatePreName);
				    ? statePreAttrName : statePreName);
			}
			break;
		}
	}

	switch (state) {
	case StatePreName:
	case StatePostQuotedValue:
	case StatePreAttrName:
	case statePreName:
	case statePostQuotedValue:
	case statePreAttrName:
		break;
	case StateName:
	case StateQuotedValue:
	case stateName:
	case stateQuotedValue:
		@throw [OFInvalidFormatException exception];
		break;
	case StateValue:
	case stateValue:
		value = [string substringWithRange:
		    OFRangeMake(last, length - last)];
		[ret addObject: [OFHTTPCookie cookieWithName: name
						       value: value
						      domain: domain]];
		break;
	/* We end up here if the cookie is just foo= */
	case StateExpectValue:
	case stateExpectValue:
		[ret addObject: [OFHTTPCookie cookieWithName: name
						       value: @""
						      domain: domain]];
		break;
	case StateAttrName:
	case stateAttrName:
		if (last != length) {
			name = [string substringWithRange:
			    OFRangeMake(last, length - last)];

			handleAttribute(ret.lastObject, name, nil);
		}
		break;
	case StateAttrValue:
	case stateAttrValue:
		value = [string substringWithRange:
		    OFRangeMake(last, length - last)];

		handleAttribute(ret.lastObject, name, value);

		break;
	}