ObjFW  Diff

Differences From Artifact [abf9a790cd]:

To Artifact [ac3250a22a]:


143
144
145
146
147
148
149

150
151






152
153
154
155
156
157
158
143
144
145
146
147
148
149
150


151
152
153
154
155
156
157
158
159
160
161
162
163







+
-
-
+
+
+
+
+
+







		    ? of_ascii_toupper(*tmp)
		    : of_ascii_tolower(*tmp));

		firstLetter = false;
		tmp++;
	}

	@try {
	return [OFString stringWithUTF8StringNoCopy: cString
				       freeWhenDone: true];
		return [OFString stringWithUTF8StringNoCopy: cString
					       freeWhenDone: true];
	} @catch (id e) {
		free(cString);
		@throw e;
	}
}

@implementation OFHTTPServerResponse
- (instancetype)initWithSocket: (OFStreamSocket *)sock
			server: (OFHTTPServer *)server
		       request: (OFHTTPRequest *)request
{
180
181
182
183
184
185
186
187

188
189
190
191
192
193
194
185
186
187
188
189
190
191

192
193
194
195
196
197
198
199







-
+







- (void)of_sendHeaders
{
	void *pool = objc_autoreleasePoolPush();
	OFMutableDictionary OF_GENERIC(OFString *, OFString *) *headers;
	OFEnumerator *keyEnumerator, *valueEnumerator;
	OFString *key, *value;

	[_socket writeFormat: @"HTTP/%@ %d %@\r\n",
	[_socket writeFormat: @"HTTP/%@ %hd %@\r\n",
			      self.protocolVersionString, _statusCode,
			      of_http_status_code_to_string(_statusCode)];

	headers = [[_headers mutableCopy] autorelease];

	if ([headers objectForKey: @"Date"] == nil) {
		OFString *date = [[OFDate date]
372
373
374
375
376
377
378
379

380
381
382
383
384
385
386
377
378
379
380
381
382
383

384
385
386
387
388
389
390
391







-
+







		return [self sendErrorAndClose: 400];
	}

	pos = [line rangeOfString: @" "].location;
	if (pos == OF_NOT_FOUND)
		return [self sendErrorAndClose: 400];

	method = [line substringWithRange: of_range(0, pos)];
	method = [line substringToIndex: pos];
	@try {
		_method = of_http_request_method_from_string(method);
	} @catch (OFInvalidArgumentException *e) {
		return [self sendErrorAndClose: 405];
	}

	@try {
448
449
450
451
452
453
454
455
456


457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476

477
478
479
480
481
482
483

484
485
486
487
488
489
490
453
454
455
456
457
458
459


460
461

462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479

480

481
482


483

484
485
486
487
488
489
490
491







-
-
+
+
-


















-
+
-


-
-

-
+







		return false;
	}

	pos = [line rangeOfString: @":"].location;
	if (pos == OF_NOT_FOUND)
		return [self sendErrorAndClose: 400];

	key = [line substringWithRange: of_range(0, pos)];
	value = [line substringWithRange:
	key = [line substringToIndex: pos];
	value = [line substringFromIndex: pos + 1];
	    of_range(pos + 1, line.length - pos - 1)];

	key = normalizedKey(key.stringByDeletingTrailingWhitespaces);
	value = value.stringByDeletingLeadingWhitespaces;

	old = [_headers objectForKey: key];
	if (old != nil)
		value = [old stringByAppendingFormat: @",%@", value];

	[_headers setObject: value
		     forKey: key];

	if ([key isEqual: @"Host"]) {
		pos = [value
		    rangeOfString: @":"
			  options: OF_STRING_SEARCH_BACKWARDS].location;

		if (pos != OF_NOT_FOUND) {
			[_host release];
			_host = [[value substringWithRange:
			_host = [[value substringToIndex: pos] retain];
			    of_range(0, pos)] retain];

			@try {
				of_range_t range =
				    of_range(pos + 1, value.length - pos - 1);
				unsigned long long portTmp =
				    [value substringWithRange: range]
				    [value substringFromIndex: pos + 1]
				    .unsignedLongLongValue;

				if (portTmp < 1 || portTmp > UINT16_MAX)
					return [self sendErrorAndClose: 400];

				_port = (uint16_t)portTmp;
			} @catch (OFInvalidFormatException *e) {
501
502
503
504
505
506
507
508

509
510
511
512
513
514
515
502
503
504
505
506
507
508

509
510
511
512
513
514
515
516







-
+







}

- (bool)sendErrorAndClose: (short)statusCode
{
	OFString *date = [[OFDate date]
	    dateStringWithFormat: @"%a, %d %b %Y %H:%M:%S GMT"];

	[_socket writeFormat: @"HTTP/1.1 %d %@\r\n"
	[_socket writeFormat: @"HTTP/1.1 %hd %@\r\n"
			      @"Date: %@\r\n"
			      @"Server: %@\r\n"
			      @"\r\n",
			      statusCode,
			      of_http_status_code_to_string(statusCode),
			      date, _server.name];

544
545
546
547
548
549
550
551
552


553
554
555
556
557
558
559
560
545
546
547
548
549
550
551


552
553

554
555
556
557
558
559
560







-
-
+
+
-







	URL.host = _host;
	if (_port != 80)
		URL.port = [OFNumber numberWithUnsignedShort: _port];

	if ((pos = [_path rangeOfString: @"?"].location) != OF_NOT_FOUND) {
		OFString *path, *query;

		path = [_path substringWithRange: of_range(0, pos)];
		query = [_path substringWithRange:
		path = [_path substringToIndex: pos];
		query = [_path substringFromIndex: pos + 1];
		    of_range(pos + 1, _path.length - pos - 1)];

		URL.URLEncodedPath = path;
		URL.URLEncodedQuery = query;
	} else
		URL.URLEncodedPath = _path;

	[URL makeImmutable];
694
695
696
697
698
699
700
701

702
703
704
705
706
707
708
709
710
711
712
713
714
715



716
717
718
719
720
721
722
723

724
725
726
727
728
729
730
731
694
695
696
697
698
699
700

701
702
703
704
705
706
707
708
709
710
711
712



713
714
715

716
717
718
719
720
721

722

723
724
725
726
727
728
729







-
+











-
-
-
+
+
+
-






-
+
-







		if (_toRead == 0)
			_toRead = -2;

		return length;
	} else {
		void *pool = objc_autoreleasePoolPush();
		OFString *line;
		of_range_t range;
		size_t pos;
		unsigned long long toRead;

		@try {
			line = [_socket tryReadLine];
		} @catch (OFInvalidEncodingException *e) {
			@throw [OFInvalidFormatException exception];
		}

		if (line == nil)
			return 0;

		range = [line rangeOfString: @";"];
		if (range.location != OF_NOT_FOUND)
			line = [line substringWithRange:
		pos = [line rangeOfString: @";"].location;
		if (pos != OF_NOT_FOUND)
			line = [line substringToIndex: pos];
			    of_range(0, range.location)];

		if (line.length < 1) {
			/*
			 * We have read the empty string because the socket is
			 * at end of stream.
			 */
			if (_socket.atEndOfStream &&
			if (_socket.atEndOfStream && pos == OF_NOT_FOUND)
			    range.location == OF_NOT_FOUND)
				@throw [OFTruncatedDataException exception];
			else
				@throw [OFInvalidFormatException exception];
		}

		toRead = [line unsignedLongLongValueWithBase: 16];
		if (toRead > LLONG_MAX)