ObjFW  Check-in [152fac94a7]

Overview
Comment:@finally { [pool release]; } is a bad idea when throwing exceptions.
The exception is released before it is ever caught.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | 0.6
Files: files | file ages | folders
SHA3-256: 152fac94a7fb6a7eff21d60b0654c333b4bdf07fc2aa560df0028a3728bc1937
User & Date: js on 2012-03-02 16:37:10
Other Links: branch diff | manifest | tags
Context
2012-03-04
22:12
Add missing files to Xcode project. check-in: 86b49f91d8 user: js tags: 0.6
2012-03-02
16:37
@finally { [pool release]; } is a bad idea when throwing exceptions.
The exception is released before it is ever caught.
check-in: 152fac94a7 user: js tags: 0.6
2012-03-01
15:03
Fix OFDate documentation. check-in: 3a88ce0e8f user: js tags: 0.6
Changes

Modified src/OFHTTPRequest.m from [29dc6862f8] to [4bbd6ecaac].

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
228
229
230
231
232
233
234

- (OFHTTPRequestResult*)performWithRedirects: (size_t)redirects
{
	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];
	}

	[delegate request: self
	  didCreateSocket: sock];

	@try {
		OFString *line, *path;
		OFMutableDictionary *serverHeaders;
		OFDataArray *data;
		OFEnumerator *keyEnumerator, *objectEnumerator;
		OFString *key, *object, *contentLengthHeader;
		int status;
		const char *type = NULL;
		char *buffer;
		size_t bytesReceived;

		[sock connectToHost: [URL host]
			       port: [URL port]];

		/*
		 * Work around a bug with packet bisection in lighttpd when
		 * using HTTPS.
		 */
		[sock setBuffersWrites: YES];

		if (requestType == OF_HTTP_REQUEST_TYPE_GET)
			type = "GET";
		if (requestType == OF_HTTP_REQUEST_TYPE_HEAD)
			type = "HEAD";







>
>
>
>
>
>
>
>
>




















<
<
<
<
<
<
<
<
<
<
<




|
|







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
228
229
230
231
232

- (OFHTTPRequestResult*)performWithRedirects: (size_t)redirects
{
	OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init];
	OFString *scheme = [URL scheme];
	OFTCPSocket *sock;
	OFHTTPRequestResult *result;
	OFString *line, *path;
	OFMutableDictionary *serverHeaders;
	OFDataArray *data;
	OFEnumerator *keyEnumerator, *objectEnumerator;
	OFString *key, *object, *contentLengthHeader;
	int status;
	const char *type = NULL;
	char *buffer;
	size_t bytesReceived;

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

	[delegate request: self
	  didCreateSocket: sock];












		[sock connectToHost: [URL host]
			       port: [URL port]];

		/*
	 * Work around a bug with packet bisection in lighttpd when using
	 * HTTPS.
		 */
		[sock setBuffersWrites: YES];

		if (requestType == OF_HTTP_REQUEST_TYPE_GET)
			type = "GET";
		if (requestType == OF_HTTP_REQUEST_TYPE_HEAD)
			type = "HEAD";
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
		[sock flushWriteBuffer];
		[sock setBuffersWrites: NO];

		if (requestType == OF_HTTP_REQUEST_TYPE_POST)
			[sock writeString: queryString];

		/*
		 * 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;








|
|


|
<
|
<

|
<







272
273
274
275
276
277
278
279
280
281
282
283

284

285
286

287
288
289
290
291
292
293
		[sock flushWriteBuffer];
		[sock setBuffersWrites: NO];

		if (requestType == OF_HTTP_REQUEST_TYPE_POST)
			[sock writeString: queryString];

		/*
	 * 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;

340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
				if (status == 303) {
					requestType = OF_HTTP_REQUEST_TYPE_GET;
					[queryString release];
					queryString = nil;
				}

				[pool release];
				pool = nil;

				return [self performWithRedirects:
				    redirects - 1];
			}

			[serverHeaders setObject: value
					  forKey: key];
		}

		[delegate request: self
		didReceiveHeaders: serverHeaders
		   withStatusCode: status];

		if (storesData)
			data = [OFDataArray dataArray];
		else
			data = nil;

		buffer = [self allocMemoryWithSize: of_pagesize];
		bytesReceived = 0;
		@try {
			size_t len;

			while ((len = [sock readNBytes: of_pagesize







<

|
<










<
|
<
<







335
336
337
338
339
340
341

342
343

344
345
346
347
348
349
350
351
352
353

354


355
356
357
358
359
360
361
				if (status == 303) {
					requestType = OF_HTTP_REQUEST_TYPE_GET;
					[queryString release];
					queryString = nil;
				}

				[pool release];


			return [self performWithRedirects: redirects - 1];

			}

			[serverHeaders setObject: value
					  forKey: key];
		}

		[delegate request: self
		didReceiveHeaders: serverHeaders
		   withStatusCode: status];


	data = (storesData ? [OFDataArray dataArray] : nil);



		buffer = [self allocMemoryWithSize: of_pagesize];
		bytesReceived = 0;
		@try {
			size_t len;

			while ((len = [sock readNBytes: of_pagesize
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
422
423
424
425
426
		}

		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

@implementation OFHTTPRequestResult
- initWithStatusCode: (short)status







|
<


|
|
|

|
|






|
<



|
<




<
<
<







373
374
375
376
377
378
379
380

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

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




	return [result autorelease];
}
@end

@implementation OFHTTPRequestResult
- initWithStatusCode: (short)status