ObjFW  Diff

Differences From Artifact [bba59f52c5]:

To Artifact [222aca0da5]:


199
200
201
202
203
204
205
206
207



208
209
210
211
212
213
214

215
216
217
218
219
220
221
199
200
201
202
203
204
205


206
207
208
209
210
211
212
213
214

215
216
217
218
219
220
221
222







-
-
+
+
+






-
+







	const char *type = NULL;
	size_t contentLength = 0;
	BOOL chunked;
	char *buffer;
	size_t bytesReceived;

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

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

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

	[delegate request: self
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
320
321
322
323
324
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

320
321
322
323
324
325
326
327







-
+
+



-
+
+













-
+




-
+








-
+








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

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

	if (![line hasPrefix: @"HTTP/1.0 "] && ![line hasPrefix: @"HTTP/1.1 "])
		@throw [OFInvalidServerReplyException exceptionWithClass: isa];
		@throw [OFInvalidServerReplyException
		    exceptionWithClass: [self class]];

	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];
			    exceptionWithClass: [self class]];
		}

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

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

		line_c = [line UTF8String];

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

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

		do {
			tmp++;
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
411
412
413

414
415
416
417
418
419
420
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
413
414
415
416

417
418
419
420
421
422
423
424







-
+
+















-
+













-
+








	contentLengthHeader = [serverHeaders objectForKey: @"Content-Length"];

	if (contentLengthHeader != nil) {
		contentLength = (size_t)[contentLengthHeader decimalValue];

		if (contentLength > SIZE_MAX)
			@throw [OFOutOfRangeException exceptionWithClass: isa];
			@throw [OFOutOfRangeException
			    exceptionWithClass: [self class]];
	}

	buffer = [self allocMemoryWithSize: of_pagesize];
	bytesReceived = 0;
	@try {
		OFAutoreleasePool *pool2 = [[OFAutoreleasePool alloc] init];

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

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

				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];
					    exceptionWithClass: [self class]];
				}

				if (toRead == 0 ||
				    (contentLengthHeader != nil &&
				    contentLength >= bytesReceived))
					break;

437
438
439
440
441
442
443
444

445
446
447
448
449

450
451
452
453
454
455
456
441
442
443
444
445
446
447

448
449
450
451
452

453
454
455
456
457
458
459
460







-
+




-
+







					toRead -= length;
				}

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

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

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

			while ((length = [sock
481
482
483
484
485
486
487
488


489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506

507
508
509
510
511
512
513
485
486
487
488
489
490
491

492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510

511
512
513
514
515
516
517
518







-
+
+

















-
+







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

	[serverHeaders makeImmutable];

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

	switch (status) {
	case 200:
	case 301:
	case 302:
	case 303:
	case 307:
		break;
	default:
		[result release];
		@throw [OFHTTPRequestFailedException
		    exceptionWithClass: isa
		    exceptionWithClass: [self class]
			   HTTPRequest: self
				result: result];
	}

	[pool release];

	return [result autorelease];