ObjFW  Diff

Differences From Artifact [7c931c50fb]:

To Artifact [66f489833f]:

  • File src/OFHTTPRequest.m — part of check-in [e1e7ffa903] at 2011-09-22 23:25:42 on branch trunk — Exceptions are now autoreleased.

    This is safe as an "exception loop" can't happen, since if allocating
    an exception fails, it throws an OFAllocFailedException which is
    preallocated and can always be thrown.

    So, the worst case would be that an autorelease of an exception fails,
    triggering an OFOutOfMemoryException for which there is no memory,
    resulting in an OFAllocFailedException to be thrown. (user: js, size: 10086) [annotate] [blame] [check-ins using]


187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
{
	OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init];
	OFString *scheme = [URL scheme];
	OFTCPSocket *sock;
	OFHTTPRequestResult *result;

	if (![scheme isEqual: @"http"] && ![scheme isEqual: @"https"])
		@throw [OFUnsupportedProtocolException newWithClass: isa
								URL: URL];

	if ([scheme isEqual: @"http"])
		sock = [OFTCPSocket socket];
	else {
		if (of_http_request_tls_socket_class == Nil)
			@throw [OFUnsupportedProtocolException
			    newWithClass: isa
				     URL: URL];

		sock = [[[of_http_request_tls_socket_class alloc] init]
		    autorelease];
	}

	@try {
		OFString *line, *path;







|
|






|
|







187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
{
	OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init];
	OFString *scheme = [URL scheme];
	OFTCPSocket *sock;
	OFHTTPRequestResult *result;

	if (![scheme isEqual: @"http"] && ![scheme isEqual: @"https"])
		@throw [OFUnsupportedProtocolException exceptionWithClass: isa
								      URL: URL];

	if ([scheme isEqual: @"http"])
		sock = [OFTCPSocket socket];
	else {
		if (of_http_request_tls_socket_class == Nil)
			@throw [OFUnsupportedProtocolException
			    exceptionWithClass: isa
					   URL: URL];

		sock = [[[of_http_request_tls_socket_class alloc] init]
		    autorelease];
	}

	@try {
		OFString *line, *path;
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
		 * We also need to check for HTTP/1.1 since Apache always
		 * declares the reply to be HTTP/1.1.
		 */
		line = [sock readLine];
		if (![line hasPrefix: @"HTTP/1.0 "] &&
		    ![line hasPrefix: @"HTTP/1.1 "])
			@throw [OFInvalidServerReplyException
			    newWithClass: isa];

		status = (int)[[line substringWithRange:
		    of_range(9, 3)] decimalValue];

		serverHeaders = [OFMutableDictionary dictionary];

		while ((line = [sock readLine]) != nil) {
			OFString *key, *value;
			const char *line_c = [line UTF8String], *tmp;

			if ([line isEqual: @""])
				break;

			if ((tmp = strchr(line_c, ':')) == NULL)
				@throw [OFInvalidServerReplyException
				    newWithClass: isa];

			key = [OFString stringWithUTF8String: line_c
						      length: tmp - line_c];
			normalize_key(key);

			do {
				tmp++;







|















|







279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
		 * We also need to check for HTTP/1.1 since Apache always
		 * declares the reply to be HTTP/1.1.
		 */
		line = [sock readLine];
		if (![line hasPrefix: @"HTTP/1.0 "] &&
		    ![line hasPrefix: @"HTTP/1.1 "])
			@throw [OFInvalidServerReplyException
			    exceptionWithClass: isa];

		status = (int)[[line substringWithRange:
		    of_range(9, 3)] decimalValue];

		serverHeaders = [OFMutableDictionary dictionary];

		while ((line = [sock readLine]) != nil) {
			OFString *key, *value;
			const char *line_c = [line UTF8String], *tmp;

			if ([line isEqual: @""])
				break;

			if ((tmp = strchr(line_c, ':')) == NULL)
				@throw [OFInvalidServerReplyException
				    exceptionWithClass: isa];

			key = [OFString stringWithUTF8String: line_c
						      length: tmp - line_c];
			normalize_key(key);

			do {
				tmp++;
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421

		if ((contentLengthHeader =
		    [serverHeaders objectForKey: @"Content-Length"]) != nil) {
			intmax_t cl = [contentLengthHeader decimalValue];

			if (cl > SIZE_MAX)
				@throw [OFOutOfRangeException
				    newWithClass: isa];

			/*
			 * We only want to throw on these status codes as we
			 * will throw an OFHTTPRequestFailedException for all
			 * other status codes later.
			 */
			if (cl != bytesReceived && (status == 200 ||
			    status == 301 || status == 302 || status == 303))
				@throw [OFTruncatedDataException
				    newWithClass: isa];
		}

		[serverHeaders makeImmutable];

		result = [[OFHTTPRequestResult alloc]
		    initWithStatusCode: status
			       headers: serverHeaders
				  data: data];

		if (status != 200 && status != 301 && status != 302 &&
		    status != 303)
			@throw [OFHTTPRequestFailedException
			    newWithClass: isa
			     HTTPRequest: self
				  result: result];
	} @finally {
		[pool release];
	}

	return [result autorelease];
}
@end







|









|












|
|
|







382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421

		if ((contentLengthHeader =
		    [serverHeaders objectForKey: @"Content-Length"]) != nil) {
			intmax_t cl = [contentLengthHeader decimalValue];

			if (cl > SIZE_MAX)
				@throw [OFOutOfRangeException
				    exceptionWithClass: isa];

			/*
			 * We only want to throw on these status codes as we
			 * will throw an OFHTTPRequestFailedException for all
			 * other status codes later.
			 */
			if (cl != bytesReceived && (status == 200 ||
			    status == 301 || status == 302 || status == 303))
				@throw [OFTruncatedDataException
				    exceptionWithClass: isa];
		}

		[serverHeaders makeImmutable];

		result = [[OFHTTPRequestResult alloc]
		    initWithStatusCode: status
			       headers: serverHeaders
				  data: data];

		if (status != 200 && status != 301 && status != 302 &&
		    status != 303)
			@throw [OFHTTPRequestFailedException
			    exceptionWithClass: isa
				   HTTPRequest: self
					result: result];
	} @finally {
		[pool release];
	}

	return [result autorelease];
}
@end