ObjFW  Check-in [59a1fd63eb]

Overview
Comment:OFHTTPRequest: Handle OFInvalidEncodingException due to incorrect reply.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: 59a1fd63eb526e7961d4229759b59be2eec622a1ab24e9004bd662830d41f6e1
User & Date: js on 2012-03-08 18:04:33
Other Links: manifest | tags
Context
2012-03-08
23:20
Add +[OFFile sizeOfFile:]. check-in: 6bcf417920 user: js tags: trunk
18:04
OFHTTPRequest: Handle OFInvalidEncodingException due to incorrect reply. check-in: 59a1fd63eb user: js tags: trunk
16:30
OFHTTPRequest: HTTP/1.1 introduces 307, handle it. check-in: 9dd8014c27 user: js tags: trunk
Changes

Modified src/OFHTTPRequest.m from [96bd1084f3] to [65e7fe1190].

26
27
28
29
30
31
32


33
34
35
36
37
38
39
#import "OFURL.h"
#import "OFTCPSocket.h"
#import "OFDictionary.h"
#import "OFDataArray.h"
#import "OFAutoreleasePool.h"

#import "OFHTTPRequestFailedException.h"


#import "OFInvalidServerReplyException.h"
#import "OFOutOfRangeException.h"
#import "OFTruncatedDataException.h"
#import "OFUnsupportedProtocolException.h"

#import "macros.h"








>
>







26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#import "OFURL.h"
#import "OFTCPSocket.h"
#import "OFDictionary.h"
#import "OFDataArray.h"
#import "OFAutoreleasePool.h"

#import "OFHTTPRequestFailedException.h"
#import "OFInvalidEncodingException.h"
#import "OFInvalidFormatException.h"
#import "OFInvalidServerReplyException.h"
#import "OFOutOfRangeException.h"
#import "OFTruncatedDataException.h"
#import "OFUnsupportedProtocolException.h"

#import "macros.h"

272
273
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
299
	/* Work around a bug in lighttpd, see above */
	[sock flushWriteBuffer];
	[sock setBuffersWrites: NO];

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


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







>
|
>
>
>
>







|

|
>
>
>
>
>
>
>
>
>
>
>



>
>







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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
	/* Work around a bug in lighttpd, see above */
	[sock flushWriteBuffer];
	[sock setBuffersWrites: NO];

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

	@try {
		line = [sock readLine];
	} @catch (OFInvalidEncodingException *e) {
		@throw [OFInvalidServerReplyException exceptionWithClass: isa];
	}

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

	for (;;) {
		OFString *key, *value;
		const char *line_c, *tmp;

		@try {
			line = [sock readLine];
		} @catch (OFInvalidEncodingException *e) {
			@throw [OFInvalidServerReplyException
			    exceptionWithClass: isa];
		}

		if (line == nil)
			@throw [OFInvalidServerReplyException
			    exceptionWithClass: isa];

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

		line_c = [line UTF8String];

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

		key = [OFString stringWithUTF8String: line_c
					      length: tmp - line_c];
358
359
360
361
362
363
364

365




366
367
368
369
370
371
372


373





374
375
376
377
378
379
380
	@try {
		OFAutoreleasePool *pool2 = [[OFAutoreleasePool alloc] init];

		if (chunked) {
			for (;;) {
				size_t pos, toRead;


				line = [sock readLine];





				pos = [line
				    indexOfFirstOccurrenceOfString: @";"];
				if (pos != OF_INVALID_INDEX)
					line = [line substringWithRange:
					    of_range(0, pos)];



				toRead = (size_t)[line hexadecimalValue];





				if (toRead == 0)
					break;

				while (toRead > 0) {
					size_t length = (toRead < of_pagesize
					    ? toRead : of_pagesize);








>
|
>
>
>
>







>
>
|
>
>
>
>
>







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
411
412
	@try {
		OFAutoreleasePool *pool2 = [[OFAutoreleasePool alloc] init];

		if (chunked) {
			for (;;) {
				size_t pos, toRead;

				@try {
					line = [sock readLine];
				} @catch (OFInvalidEncodingException *e) {
					@throw [OFInvalidServerReplyException
					    exceptionWithClass: isa];
				}

				pos = [line
				    indexOfFirstOccurrenceOfString: @";"];
				if (pos != OF_INVALID_INDEX)
					line = [line substringWithRange:
					    of_range(0, pos)];

				@try {
					toRead =
					    (size_t)[line hexadecimalValue];
				} @catch (OFInvalidFormatException *e) {
					@throw [OFInvalidServerReplyException
					    exceptionWithClass: isa];
				}

				if (toRead == 0)
					break;

				while (toRead > 0) {
					size_t length = (toRead < of_pagesize
					    ? toRead : of_pagesize);

389
390
391
392
393
394
395

396






397
398
399
400
401
402
403
					bytesReceived += length;
					[data addNItems: length
					     fromCArray: buffer];

					toRead -= length;
				}


				if (![[sock readLine] isEqual: @""])






					@throw [OFInvalidServerReplyException
					    exceptionWithClass: isa];

				[pool2 releaseObjects];
			}
		} else {
			size_t length;







>
|
>
>
>
>
>
>







421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
					bytesReceived += length;
					[data addNItems: length
					     fromCArray: buffer];

					toRead -= length;
				}

				@try {
					line = [sock readLine];
				} @catch (OFInvalidEncodingException *e) {
					@throw [OFInvalidServerReplyException
					    exceptionWithClass: isa];
				}

				if (![line isEqual: @""])
					@throw [OFInvalidServerReplyException
					    exceptionWithClass: isa];

				[pool2 releaseObjects];
			}
		} else {
			size_t length;