ObjFW  Diff

Differences From Artifact [34f30ba48f]:

To Artifact [3a7faef063]:


37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#import "OFWriteFailedException.h"

#import "macros.h"

#define BUFFER_SIZE 1024

/*
 * TODO: Add support for chunked transfer encoding.
 * FIXME: Key normalization replaces headers like "DNT" with "Dnt".
 * FIXME: Errors are not reported to the user.
 */

static const char*
status_code_to_string(short code)
{







<







37
38
39
40
41
42
43

44
45
46
47
48
49
50
#import "OFWriteFailedException.h"

#import "macros.h"

#define BUFFER_SIZE 1024

/*

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

static const char*
status_code_to_string(short code)
{
159
160
161
162
163
164
165














































































































166
167
168
169
170
171
172
		firstLetter = NO;
		tmp++;
	}

	return [OFString stringWithUTF8StringNoCopy: cString
				       freeWhenDone: YES];
}















































































































@interface OFHTTPServer_Connection: OFObject
{
	OFTCPSocket *sock;
	OFHTTPServer *server;
	OFTimer *timer;
	enum {







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
		firstLetter = NO;
		tmp++;
	}

	return [OFString stringWithUTF8StringNoCopy: cString
				       freeWhenDone: YES];
}

@interface OFHTTPServerReply: OFHTTPRequestReply
{
	OFTCPSocket *sock;
	OFHTTPServer *server;
	BOOL chunked, headersSent, closed;
}

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

@implementation OFHTTPServerReply
- initWithSocket: (OFTCPSocket*)sock_
	  server: (OFHTTPServer*)server_
{
	self = [super init];

	statusCode = 500;
	sock = [sock_ retain];
	server = [server_ retain];

	return self;
}

- (void)dealloc
{
	if (!closed)
		[self close];

	[sock release];
	[server release];

	[super dealloc];
}

- (void)sendHeaders
{
	void *pool = objc_autoreleasePoolPush();
	OFString *date = [[OFDate date]
	    dateStringWithFormat: @"%a, %d %b %Y %H:%M:%S GMT"];
	OFEnumerator *keyEnumerator, *valueEnumerator;
	OFString *key, *value;

	[sock writeFormat: @"HTTP/%@ %d %s\r\n"
			   @"Server: %@\r\n"
			   @"Date: %@\r\n",
			   [self protocolVersionString], statusCode,
			   status_code_to_string(statusCode),
			   [server name], date];

	keyEnumerator = [headers keyEnumerator];
	valueEnumerator = [headers objectEnumerator];
	while ((key = [keyEnumerator nextObject]) != nil &&
	    (value = [valueEnumerator nextObject]) != nil)
		if (![key isEqual: @"Server"] && ![key isEqual: @"Date"])
			[sock writeFormat: @"%@: %@\r\n", key, value];

	[sock writeString: @"\r\n"];

	headersSent = YES;
	chunked = [[headers objectForKey: @"Transfer-Encoding"]
	    isEqual: @"chunked"];

	objc_autoreleasePoolPop(pool);
}

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

	if (!headersSent)
		[self sendHeaders];

	if (!chunked) {
		[sock writeBuffer: buffer
			   length: length];
		return;
	}

	pool = objc_autoreleasePoolPush();
	[sock writeString: [OFString stringWithFormat: @"%zx\r\n", length]];
	objc_autoreleasePoolPop(pool);

	[sock writeBuffer: buffer
		   length: length];
	[sock writeBuffer: "\r\n"
		   length: 2];
}

- (void)close
{
	if (!headersSent)
		[self sendHeaders];

	if (chunked)
		[sock writeBuffer: "0\r\n\r\n"
			   length: 5];

	[sock close];

	closed = YES;
}

- (int)fileDescriptorForWriting
{
	return [sock fileDescriptorForWriting];
}
@end

@interface OFHTTPServer_Connection: OFObject
{
	OFTCPSocket *sock;
	OFHTTPServer *server;
	OFTimer *timer;
	enum {
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
- (BOOL)parseProlog: (OFString*)line;
- (BOOL)parseHeaders: (OFString*)line;
-      (BOOL)socket: (OFTCPSocket*)sock
  didReadIntoBuffer: (const char*)buffer
	     length: (size_t)length
	  exception: (OFException*)exception;
- (BOOL)sendErrorAndClose: (short)statusCode;
- (void)sendReply;
@end

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







|







300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
- (BOOL)parseProlog: (OFString*)line;
- (BOOL)parseHeaders: (OFString*)line;
-      (BOOL)socket: (OFTCPSocket*)sock
  didReadIntoBuffer: (const char*)buffer
	     length: (size_t)length
	  exception: (OFException*)exception;
- (BOOL)sendErrorAndClose: (short)statusCode;
- (void)createReply;
@end

@implementation OFHTTPServer_Connection
- initWithSocket: (OFTCPSocket*)sock_
	  server: (OFHTTPServer*)server_
{
	self = [super init];
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
		case AWAITING_PROLOG:
			return [self parseProlog: line];
		case PARSING_HEADERS:
			if (![self parseHeaders: line])
				return NO;

			if (state == SEND_REPLY) {
				[self sendReply];
				return NO;
			}

			return YES;
		default:
			return NO;
		}







|







359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
		case AWAITING_PROLOG:
			return [self parseProlog: line];
		case PARSING_HEADERS:
			if (![self parseHeaders: line])
				return NO;

			if (state == SEND_REPLY) {
				[self createReply];
				return NO;
			}

			return YES;
		default:
			return NO;
		}
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
		return NO;

	[POSTData addItems: buffer
		     count: length];

	if ([POSTData count] >= contentLength) {
		@try {
			[self sendReply];
		} @catch (OFWriteFailedException *e) {
			return NO;
		}

		return NO;
	}








|







528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
		return NO;

	[POSTData addItems: buffer
		     count: length];

	if ([POSTData count] >= contentLength) {
		@try {
			[self createReply];
		} @catch (OFWriteFailedException *e) {
			return NO;
		}

		return NO;
	}

448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
			   statusCode, status_code_to_string(statusCode), date,
			   [server name]];
	[sock close];

	return NO;
}

- (void)sendReply
{
	OFURL *URL;
	OFHTTPRequest *request;
	OFHTTPRequestReply *reply;
	OFDictionary *replyHeaders;
	OFDataArray *replyData;
	OFString *date;
	OFEnumerator *keyEnumerator, *valueEnumerator;
	OFString *key, *value;
	size_t pos;

	[timer invalidate];
	[timer release];
	timer = nil;

	if (host == nil || port == 0) {







|



|
<
<
<
<
<







557
558
559
560
561
562
563
564
565
566
567
568





569
570
571
572
573
574
575
			   statusCode, status_code_to_string(statusCode), date,
			   [server name]];
	[sock close];

	return NO;
}

- (void)createReply
{
	OFURL *URL;
	OFHTTPRequest *request;
	OFHTTPServerReply *reply;





	size_t pos;

	[timer invalidate];
	[timer release];
	timer = nil;

	if (host == nil || port == 0) {
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
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
		[URL setPath: path_];
		[URL setQuery: query];
	} else
		[URL setPath: path];

	request = [OFHTTPRequest requestWithURL: URL];
	[request setRequestType: requestType];


	[request setHeaders: headers];
	[request setPOSTData: POSTData];
	[request setRemoteAddress: [sock remoteAddress]];

	reply = [[server delegate] server: server
			didReceiveRequest: request];

	date = [[OFDate date]
	    dateStringWithFormat: @"%a, %d %b %Y %H:%M:%S GMT"];

	if (reply == nil) {
		[self sendErrorAndClose: 500];
		@throw [OFInvalidArgumentException
		    exceptionWithClass: [(id)[server delegate] class]
			      selector: @selector(server:didReceiveRequest:)];
	}

	replyHeaders = [reply headers];
	replyData = [reply data];

	[sock writeFormat: @"HTTP/1.1 %d %s\r\n"
			   @"Server: %@\r\n"
			   @"Date: %@\r\n",
			   [reply statusCode],
			   status_code_to_string([reply statusCode]),
			   [server name], date];

	if (requestType != OF_HTTP_REQUEST_TYPE_HEAD)
		[sock writeFormat: @"Content-Length: %zu\r\n",
				   [replyData count] * [replyData itemSize]];

	keyEnumerator = [replyHeaders keyEnumerator];
	valueEnumerator = [replyHeaders objectEnumerator];
	while ((key = [keyEnumerator nextObject]) != nil &&
	    (value = [valueEnumerator nextObject]) != nil)
		if (![key isEqual: @"Server"] && ![key isEqual: @"Date"])
			[sock writeFormat: @"%@: %@\r\n", key, value];

	[sock writeString: @"\r\n"];

	if (requestType != OF_HTTP_REQUEST_TYPE_HEAD)
		[sock writeBuffer: [replyData items]
			   length: [replyData count] * [replyData itemSize]];
}
@end

@implementation OFHTTPServer
+ (instancetype)server
{
	return [[[self alloc] init] autorelease];







>
>




<
<
|
<
<
|
<
<
<
<
<
<
|
<
<

<
<
<
<
<
<
|
<
<
<
|
<
<
<
<
<
<
|
<
<
<
<
<







597
598
599
600
601
602
603
604
605
606
607
608
609


610


611






612


613






614



615






616





617
618
619
620
621
622
623
		[URL setPath: path_];
		[URL setQuery: query];
	} else
		[URL setPath: path];

	request = [OFHTTPRequest requestWithURL: URL];
	[request setRequestType: requestType];
	[request setProtocolVersion:
	    (of_http_request_protocol_version_t){ 1, HTTPMinorVersion }];
	[request setHeaders: headers];
	[request setPOSTData: POSTData];
	[request setRemoteAddress: [sock remoteAddress]];



	reply = [[[OFHTTPServerReply alloc]


	    initWithSocket: sock






		    server: server] autorelease];









	[[server delegate] server: server



		didReceiveRequest: request






			    reply: reply];





}
@end

@implementation OFHTTPServer
+ (instancetype)server
{
	return [[[self alloc] init] autorelease];