ObjFW  Diff

Differences From Artifact [84e4421360]:

To Artifact [6f3529bdbf]:


43
44
45
46
47
48
49
50
51
52



53
54
55

56
57
58
59
60
61
62
43
44
45
46
47
48
49



50
51
52
53
54

55
56
57
58
59
60
61
62







-
-
-
+
+
+


-
+








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

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

static const char*
static const char *
statusCodeToString(short code)
{
	switch (code) {
	case 100:
		return "Continue";
	case 101:
		return "Switching Protocols";
137
138
139
140
141
142
143
144

145
146
147
148

149
150
151
152
153
154
155
137
138
139
140
141
142
143

144
145
146
147

148
149
150
151
152
153
154
155







-
+



-
+







	case 505:
		return "HTTP Version Not Supported";
	default:
		return NULL;
	}
}

static OF_INLINE OFString*
static OF_INLINE OFString *
normalizedKey(OFString *key)
{
	char *cString = of_strdup([key UTF8String]);
	unsigned char *tmp = (unsigned char*)cString;
	unsigned char *tmp = (unsigned char *)cString;
	bool firstLetter = true;

	if (cString == NULL)
		@throw [OFOutOfMemoryException
		    exceptionWithRequestedSize: strlen([key UTF8String])];

	while (*tmp != '\0') {
175
176
177
178
179
180
181
182
183
184



185
186
187
188
189
190



191
192
193
194
195
196
197
175
176
177
178
179
180
181



182
183
184
185
186
187



188
189
190
191
192
193
194
195
196
197







-
-
-
+
+
+



-
-
-
+
+
+







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

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

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

	_statusCode = 500;
	_socket = [socket retain];
	_server = [server retain];
	_request = [request retain];
209
210
211
212
213
214
215
216

217
218
219
220
221
222
223
209
210
211
212
213
214
215

216
217
218
219
220
221
222
223







-
+








	[super dealloc];
}

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

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

250
251
252
253
254
255
256
257

258
259
260
261
262
263
264
250
251
252
253
254
255
256

257
258
259
260
261
262
263
264







-
+







	_headersSent = true;
	_chunked = [[headers objectForKey: @"Transfer-Encoding"]
	    isEqual: @"chunked"];

	objc_autoreleasePoolPop(pool);
}

- (void)lowlevelWriteBuffer: (const void*)buffer
- (void)lowlevelWriteBuffer: (const void *)buffer
		     length: (size_t)length
{
	void *pool;

	if (_socket == nil)
		@throw [OFNotOpenException exceptionWithObject: self];

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







-
-
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+

-
+





-
-
+
+







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

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

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

	@try {
		_socket = [socket retain];
		_server = [server retain];
		_timer = [[OFTimer
389
390
391
392
393
394
395
396
397
398



399
400
401
402
403
404
405
389
390
391
392
393
394
395



396
397
398
399
400
401
402
403
404
405







-
-
-
+
+
+







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

	[super dealloc];
}

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

	@try {
		switch (_state) {
		case AWAITING_PROLOG:
420
421
422
423
424
425
426
427

428
429
430
431
432
433
434
420
421
422
423
424
425
426

427
428
429
430
431
432
433
434







-
+







	} @catch (OFWriteFailedException *e) {
		return false;
	}

	OF_ENSURE(0);
}

- (bool)parseProlog: (OFString*)line
- (bool)parseProlog: (OFString *)line
{
	OFString *method;
	OFMutableString *path;
	size_t pos;

	@try {
		OFString *version = [line
479
480
481
482
483
484
485
486

487
488
489
490
491
492
493
479
480
481
482
483
484
485

486
487
488
489
490
491
492
493







-
+







	_headers = [[OFMutableDictionary alloc] init];
	_path = [path copy];
	_state = PARSING_HEADERS;

	return true;
}

- (bool)parseHeaders: (OFString*)line
- (bool)parseHeaders: (OFString *)line
{
	OFString *key, *value, *old;
	size_t pos;

	if ([line length] == 0) {
		intmax_t contentLength;

567
568
569
570
571
572
573
574
575


576
577

578
579
580
581
582
583
584
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
  didReadIntoBuffer: (char*)buffer
-      (bool)socket: (OFTCPSocket *)socket
  didReadIntoBuffer: (char *)buffer
	     length: (size_t)length
	  exception: (OFException*)exception
	  exception: (OFException *)exception
{
	if ([socket isAtEndOfStream] || exception != nil)
		return false;

	[_body addItems: buffer
		  count: length];

727
728
729
730
731
732
733
734
735
736



737
738
739
740
741
742
743
727
728
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
  didAcceptSocket: (OFTCPSocket*)clientSocket
	exception: (OFException*)exception
- (bool)OF_socket: (OFTCPSocket *)socket
  didAcceptSocket: (OFTCPSocket *)clientSocket
	exception: (OFException *)exception
{
	OFHTTPServer_Connection *connection;

	if (exception != nil) {
		if ([_delegate respondsToSelector:
		    @selector(server:didReceiveExceptionOnListeningSocket:)])
			return [_delegate		  server: self