ObjFW  Diff

Differences From Artifact [c477e44d18]:

To Artifact [ae0dbd61af]:


43
44
45
46
47
48
49
50

51
52
53
54
55
56
57
43
44
45
46
47
48
49

50
51
52
53
54
55
56
57







-
+








/*
 * FIXME: Key normalization replaces headers like "DNT" with "Dnt".
 * FIXME: Errors are not reported to the user.
 */

@interface OFHTTPServer ()
- (bool)of_socket: (OFTCPSocket *)socket
- (bool)of_socket: (OFTCPSocket *)sock
  didAcceptSocket: (OFTCPSocket *)clientSocket
	exception: (OFException *)exception;
@end

static const char *
statusCodeToString(short code)
{
175
176
177
178
179
180
181
182

183
184
185
186
187
188

189
190
191
192
193
194
195

196
197
198
199
200
201
202
175
176
177
178
179
180
181

182
183
184
185
186
187

188
189
190
191
192
193
194

195
196
197
198
199
200
201
202







-
+





-
+






-
+







{
	OFTCPSocket *_socket;
	OFHTTPServer *_server;
	OFHTTPRequest *_request;
	bool _chunked, _headersSent;
}

- initWithSocket: (OFTCPSocket *)socket
- initWithSocket: (OFTCPSocket *)sock
	  server: (OFHTTPServer *)server
	 request: (OFHTTPRequest *)request;
@end

@implementation OFHTTPServerResponse
- initWithSocket: (OFTCPSocket *)socket
- initWithSocket: (OFTCPSocket *)sock
	  server: (OFHTTPServer *)server
	 request: (OFHTTPRequest *)request
{
	self = [super init];

	_statusCode = 500;
	_socket = [socket retain];
	_socket = [sock retain];
	_server = [server retain];
	_request = [request retain];

	return self;
}

- (void)dealloc
334
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
362
363

364
365
366
367

368
369
370
371
372
373
374
334
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
362

363
364
365
366

367
368
369
370
371
372
373
374







-
+

-
+




-
+








-
+





-
+



-
+







	OFString *_host, *_path;
	uint16_t _port;
	OFMutableDictionary *_headers;
	size_t _contentLength;
	OFMutableData *_body;
}

- initWithSocket: (OFTCPSocket *)socket
- initWithSocket: (OFTCPSocket *)sock
	  server: (OFHTTPServer *)server;
- (bool)socket: (OFTCPSocket *)socket
- (bool)socket: (OFTCPSocket *)sock
   didReadLine: (OFString *)line
     exception: (OFException *)exception;
- (bool)parseProlog: (OFString *)line;
- (bool)parseHeaders: (OFString *)line;
-      (bool)socket: (OFTCPSocket *)socket
-      (bool)socket: (OFTCPSocket *)sock
  didReadIntoBuffer: (char *)buffer
	     length: (size_t)length
	  exception: (OFException *)exception;
- (bool)sendErrorAndClose: (short)statusCode;
- (void)createResponse;
@end

@implementation OFHTTPServer_Connection
- initWithSocket: (OFTCPSocket *)socket
- initWithSocket: (OFTCPSocket *)sock
	  server: (OFHTTPServer *)server
{
	self = [super init];

	@try {
		_socket = [socket retain];
		_socket = [sock retain];
		_server = [server retain];
		_timer = [[OFTimer
		    scheduledTimerWithTimeInterval: 10
					    target: socket
					    target: _socket
					  selector: @selector(
							cancelAsyncRequests)
					   repeats: false] retain];
		_state = AWAITING_PROLOG;
	} @catch (id e) {
		[self release];
		@throw e;
389
390
391
392
393
394
395
396

397
398
399
400
401
402
403
389
390
391
392
393
394
395

396
397
398
399
400
401
402
403







-
+







	[_path release];
	[_headers release];
	[_body release];

	[super dealloc];
}

- (bool)socket: (OFTCPSocket *)socket
- (bool)socket: (OFTCPSocket *)sock
   didReadLine: (OFString *)line
     exception: (OFException *)exception
{
	if (line == nil || exception != nil)
		return false;

	@try {
565
566
567
568
569
570
571
572

573
574
575
576
577

578
579
580
581
582
583
584
565
566
567
568
569
570
571

572
573
574
575
576

577
578
579
580
581
582
583
584







-
+




-
+







			_port = 80;
		}
	}

	return true;
}

-      (bool)socket: (OFTCPSocket *)socket
-      (bool)socket: (OFTCPSocket *)sock
  didReadIntoBuffer: (char *)buffer
	     length: (size_t)length
	  exception: (OFException *)exception
{
	if ([socket isAtEndOfStream] || exception != nil)
	if ([sock isAtEndOfStream] || exception != nil)
		return false;

	[_body addItems: buffer
		  count: length];

	if ([_body count] >= _contentLength) {
		/*
729
730
731
732
733
734
735
736

737
738
739
740
741
742
743
729
730
731
732
733
734
735

736
737
738
739
740
741
742
743







-
+







- (void)stop
{
	[_listeningSocket cancelAsyncRequests];
	[_listeningSocket release];
	_listeningSocket = nil;
}

- (bool)of_socket: (OFTCPSocket *)socket
- (bool)of_socket: (OFTCPSocket *)sock
  didAcceptSocket: (OFTCPSocket *)clientSocket
	exception: (OFException *)exception
{
	OFHTTPServer_Connection *connection;

	if (exception != nil) {
		if ([_delegate respondsToSelector: