ObjFW  Diff

Differences From Artifact [e267b58d94]:

To Artifact [f1cfec9f86]:


24
25
26
27
28
29
30
31

32
33
34
35
36
37
38
24
25
26
27
28
29
30

31
32
33
34
35
36
37
38







-
+







#import "OFInvalidArgumentException.h"
#import "OFInvalidFormatException.h"
#import "OFOutOfRangeException.h"
#import "OFTruncatedDataException.h"
#import "OFUnsupportedVersionException.h"

OFString *
of_http_status_code_to_string(short code)
OFHTTPStatusCodeString(short code)
{
	switch (code) {
	case 100:
		return @"Continue";
	case 101:
		return @"Switching Protocols";
	case 200:
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
185
186
187

188
189
190
191
192
193
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
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
185
186

187
188
189
190
191
192
193
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







-
+





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

-
+




-
+

-
+



-
+



-
+



-
+





-
+



-
+

-
+


-
+



-
+










-
+



-
+








-
+


-
+

-
+


-
+



-
+









-
+

-
+







	case 505:
		return @"HTTP Version Not Supported";
	default:
		return @"(unknown)";
	}
}

static of_string_encoding_t
static OFStringEncoding
encodingForContentType(OFString *contentType)
{
	const char *UTF8String = contentType.UTF8String;
	size_t last, length = contentType.UTF8StringLength;
	enum {
		STATE_TYPE,
		STATE_BEFORE_PARAM_NAME,
		STATE_PARAM_NAME,
		STATE_PARAM_VALUE_OR_QUOTE,
		STATE_PARAM_VALUE,
		STATE_PARAM_QUOTED_VALUE,
		STATE_AFTER_PARAM_VALUE
	} state = STATE_TYPE;
		stateType,
		stateBeforeParamName,
		stateParamName,
		stateParamValueOrQuote,
		stateParamValue,
		stateParamQuotedValue,
		stateAfterParamValue
	} state = stateType;
	OFString *name = nil, *value = nil, *charset = nil;
	of_string_encoding_t ret;
	OFStringEncoding ret;

	last = 0;
	for (size_t i = 0; i < length; i++) {
		switch (state) {
		case STATE_TYPE:
		case stateType:
			if (UTF8String[i] == ';') {
				state = STATE_BEFORE_PARAM_NAME;
				state = stateBeforeParamName;
				last = i + 1;
			}
			break;
		case STATE_BEFORE_PARAM_NAME:
		case stateBeforeParamName:
			if (UTF8String[i] == ' ')
				last = i + 1;
			else {
				state = STATE_PARAM_NAME;
				state = stateParamName;
				i--;
			}
			break;
		case STATE_PARAM_NAME:
		case stateParamName:
			if (UTF8String[i] == '=') {
				name = [OFString
				    stringWithUTF8String: UTF8String + last
						  length: i - last];

				state = STATE_PARAM_VALUE_OR_QUOTE;
				state = stateParamValueOrQuote;
				last = i + 1;
			}
			break;
		case STATE_PARAM_VALUE_OR_QUOTE:
		case stateParamValueOrQuote:
			if (UTF8String[i] == '"') {
				state = STATE_PARAM_QUOTED_VALUE;
				state = stateParamQuotedValue;
				last = i + 1;
			} else {
				state = STATE_PARAM_VALUE;
				state = stateParamValue;
				i--;
			}
			break;
		case STATE_PARAM_VALUE:
		case stateParamValue:
			if (UTF8String[i] == ';') {
				value = [OFString
				    stringWithUTF8String: UTF8String + last
						  length: i - last];
				value =
				    value.stringByDeletingTrailingWhitespaces;

				if ([name isEqual: @"charset"])
					charset = value;

				state = STATE_BEFORE_PARAM_NAME;
				state = stateBeforeParamName;
				last = i + 1;
			}
			break;
		case STATE_PARAM_QUOTED_VALUE:
		case stateParamQuotedValue:
			if (UTF8String[i] == '"') {
				value = [OFString
				    stringWithUTF8String: UTF8String + last
						  length: i - last];

				if ([name isEqual: @"charset"])
					charset = value;

				state = STATE_AFTER_PARAM_VALUE;
				state = stateAfterParamValue;
			}
			break;
		case STATE_AFTER_PARAM_VALUE:
		case stateAfterParamValue:
			if (UTF8String[i] == ';') {
				state = STATE_BEFORE_PARAM_NAME;
				state = stateBeforeParamName;
				last = i + 1;
			} else if (UTF8String[i] != ' ')
				return OF_STRING_ENCODING_AUTODETECT;
				return OFStringEncodingAutodetect;
			break;
		}
	}
	if (state == STATE_PARAM_VALUE) {
	if (state == stateParamValue) {
		value = [OFString stringWithUTF8String: UTF8String + last
						length: length - last];
		value = value.stringByDeletingTrailingWhitespaces;

		if ([name isEqual: @"charset"])
			charset = value;
	}

	@try {
		ret = of_string_parse_encoding(charset);
		ret = OFStringEncodingParseName(charset);
	} @catch (OFInvalidArgumentException *e) {
		ret = OF_STRING_ENCODING_AUTODETECT;
		ret = OFStringEncodingAutodetect;
	}

	return ret;
}

@implementation OFHTTPResponse
@synthesize statusCode = _statusCode, headers = _headers;
245
246
247
248
249
250
251
252

253
254
255
256
257
258
259
260
261
262
263

264
265
266
267
268
269
270
271
272
273

274
275
276
277
278
279
280
245
246
247
248
249
250
251

252
253
254
255
256
257
258
259
260
261
262

263
264
265
266
267
268
269
270
271
272

273
274
275
276
277
278
279
280







-
+










-
+









-
+







- (void)dealloc
{
	[_headers release];

	[super dealloc];
}

- (void)setProtocolVersion: (of_http_request_protocol_version_t)protocolVersion
- (void)setProtocolVersion: (OFHTTPRequestProtocolVersion)protocolVersion
{
	if (protocolVersion.major != 1 || protocolVersion.minor > 1)
		@throw [OFUnsupportedVersionException exceptionWithVersion:
		    [OFString stringWithFormat: @"%hhu.%hhu",
						protocolVersion.major,
						protocolVersion.minor]];

	_protocolVersion = protocolVersion;
}

- (of_http_request_protocol_version_t)protocolVersion
- (OFHTTPRequestProtocolVersion)protocolVersion
{
	return _protocolVersion;
}

- (void)setProtocolVersionString: (OFString *)string
{
	void *pool = objc_autoreleasePoolPush();
	OFArray *components = [string componentsSeparatedByString: @"."];
	unsigned long long major, minor;
	of_http_request_protocol_version_t protocolVersion;
	OFHTTPRequestProtocolVersion protocolVersion;

	if (components.count != 2)
		@throw [OFInvalidFormatException exception];

	major = [components.firstObject unsignedLongLongValue];
	minor = [components.lastObject unsignedLongLongValue];

294
295
296
297
298
299
300
301

302
303
304

305
306
307
308
309
310

311
312
313
314
315


316
317
318
319
320
321
322
294
295
296
297
298
299
300

301
302
303

304
305
306
307
308
309

310
311
312
313


314
315
316
317
318
319
320
321
322







-
+


-
+





-
+



-
-
+
+







	return [OFString stringWithFormat: @"%hhu.%hhu",
					   _protocolVersion.major,
					   _protocolVersion.minor];
}

- (OFString *)string
{
	return [self stringWithEncoding: OF_STRING_ENCODING_AUTODETECT];
	return [self stringWithEncoding: OFStringEncodingAutodetect];
}

- (OFString *)stringWithEncoding: (of_string_encoding_t)encoding
- (OFString *)stringWithEncoding: (OFStringEncoding)encoding
{
	void *pool = objc_autoreleasePoolPush();
	OFString *contentType, *contentLengthString, *ret;
	OFData *data;

	if (encoding == OF_STRING_ENCODING_AUTODETECT &&
	if (encoding == OFStringEncodingAutodetect &&
	    (contentType = [_headers objectForKey: @"Content-Type"]) != nil)
		encoding = encodingForContentType(contentType);

	if (encoding == OF_STRING_ENCODING_AUTODETECT)
		encoding = OF_STRING_ENCODING_UTF_8;
	if (encoding == OFStringEncodingAutodetect)
		encoding = OFStringEncodingUTF8;

	data = [self readDataUntilEndOfStream];

	contentLengthString = [_headers objectForKey: @"Content-Length"];
	if (contentLengthString != nil) {
		unsigned long long contentLength =
		    contentLengthString.unsignedLongLongValue;