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
	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;
	OFString *name = nil, *value = nil;

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

			i--;
			break;
		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);
			}
			break;
		case StateQuotedValue:
			if (characters[i] == '"') {
				value = [string substringWithRange:
				    OFRangeMake(last, i - last)];
				[ret addObject:
				    [OFHTTPCookie cookieWithName: name
							   value: value
							  domain: domain]];

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

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

				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);
			}

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







|
|
|
|
|
|
|
|
|
|




|

|




|



|


|

|


|





|










|


|








|


|

|

|




|

|




|




|









|



|







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;
	OFString *name = nil, *value = nil;

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

			i--;
			break;
		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);
			}
			break;
		case stateQuotedValue:
			if (characters[i] == '"') {
				value = [string substringWithRange:
				    OFRangeMake(last, i - last)];
				[ret addObject:
				    [OFHTTPCookie cookieWithName: name
							   value: value
							  domain: domain]];

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

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

				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);
			}

			break;
		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
				    [value isEqual: @"Sat"] ||
				    [value isEqual: @"Sun"]))
					break;

				handleAttribute(ret.lastObject, name, value);

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

	switch (state) {
	case StatePreName:
	case StatePostQuotedValue:
	case StatePreAttrName:
		break;
	case StateName:
	case StateQuotedValue:
		@throw [OFInvalidFormatException exception];
		break;
	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:
		[ret addObject: [OFHTTPCookie cookieWithName: name
						       value: @""
						      domain: domain]];
		break;
	case StateAttrName:
		if (last != length) {
			name = [string substringWithRange:
			    OFRangeMake(last, length - last)];

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

		handleAttribute(ret.lastObject, name, value);

		break;
	}







|






|
|
|

|
|


|







|




|







|







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);
			}
			break;
		}
	}

	switch (state) {
	case statePreName:
	case statePostQuotedValue:
	case statePreAttrName:
		break;
	case stateName:
	case stateQuotedValue:
		@throw [OFInvalidFormatException exception];
		break;
	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:
		[ret addObject: [OFHTTPCookie cookieWithName: name
						       value: @""
						      domain: domain]];
		break;
	case stateAttrName:
		if (last != length) {
			name = [string substringWithRange:
			    OFRangeMake(last, length - last)];

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

		handleAttribute(ret.lastObject, name, value);

		break;
	}