︙ | | |
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
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
|
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
|
+
+
-
+
-
+
-
+
-
+
-
-
-
+
+
+
+
-
+
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
|
#import "OFTLSSocket.h"
#import "OFThread.h"
#import "OFTimer.h"
#import "OFURL.h"
#import "OFAlreadyConnectedException.h"
#import "OFInvalidArgumentException.h"
#import "OFInvalidEncodingException.h"
#import "OFInvalidFormatException.h"
#import "OFNotOpenException.h"
#import "OFOutOfMemoryException.h"
#import "OFOutOfRangeException.h"
#import "OFTruncatedDataException.h"
#import "OFUnsupportedProtocolException.h"
#import "OFWriteFailedException.h"
#import "socket_helpers.h"
#define BUFFER_SIZE 1024
/*
* FIXME: Key normalization replaces headers like "DNT" with "Dnt".
* FIXME: Errors are not reported to the user.
*/
@interface OFHTTPServer () <OFTCPSocketDelegate>
@end
@interface OFHTTPServerResponse: OFHTTPResponse <OFReadyForWritingObserving>
{
OFTCPSocket *_socket;
OFStreamSocket *_socket;
OFHTTPServer *_server;
OFHTTPRequest *_request;
bool _chunked, _headersSent;
}
- (instancetype)initWithSocket: (OFTCPSocket *)sock
- (instancetype)initWithSocket: (OFStreamSocket *)sock
server: (OFHTTPServer *)server
request: (OFHTTPRequest *)request;
@end
@interface OFHTTPServerConnection: OFObject <OFTCPSocketDelegate>
{
@public
OFTCPSocket *_socket;
OFStreamSocket *_socket;
OFHTTPServer *_server;
OFTimer *_timer;
enum {
AWAITING_PROLOG,
PARSING_HEADERS,
SEND_RESPONSE
} _state;
uint8_t _HTTPMinorVersion;
of_http_request_method_t _method;
OFString *_host, *_path;
uint16_t _port;
OFMutableDictionary *_headers;
size_t _contentLength;
OFStream *_requestBody;
}
- (instancetype)initWithSocket: (OFTCPSocket *)sock
- (instancetype)initWithSocket: (OFStreamSocket *)sock
server: (OFHTTPServer *)server;
- (bool)parseProlog: (OFString *)line;
- (bool)parseHeaders: (OFString *)line;
- (bool)sendErrorAndClose: (short)statusCode;
- (void)createResponse;
@end
@interface OFHTTPServerRequestBodyStream: OFStream <OFReadyForReadingObserving>
{
OFTCPSocket *_socket;
uintmax_t _toRead;
bool _atEndOfStream;
OFStreamSocket *_socket;
bool _chunked;
intmax_t _toRead;
bool _atEndOfStream, _setAtEndOfStream;
}
- (instancetype)initWithSocket: (OFTCPSocket *)sock
- (instancetype)initWithSocket: (OFStreamSocket *)sock
chunked: (bool)chunked
contentLength: (uintmax_t)contentLength;
@end
#ifdef OF_HAVE_THREADS
@interface OFHTTPServerThread: OFThread
- (void)stop;
@end
#endif
static const char *
statusCodeToString(short code)
{
switch (code) {
case 100:
return "Continue";
case 101:
return "Switching Protocols";
case 200:
return "OK";
case 201:
return "Created";
case 202:
return "Accepted";
case 203:
return "Non-Authoritative Information";
case 204:
return "No Content";
case 205:
return "Reset Content";
case 206:
return "Partial Content";
case 300:
return "Multiple Choices";
case 301:
return "Moved Permanently";
case 302:
return "Found";
case 303:
return "See Other";
case 304:
return "Not Modified";
case 305:
return "Use Proxy";
case 307:
return "Temporary Redirect";
case 400:
return "Bad Request";
case 401:
return "Unauthorized";
case 402:
return "Payment Required";
case 403:
return "Forbidden";
case 404:
return "Not Found";
case 405:
return "Method Not Allowed";
case 406:
return "Not Acceptable";
case 407:
return "Proxy Authentication Required";
case 408:
return "Request Timeout";
case 409:
return "Conflict";
case 410:
return "Gone";
case 411:
return "Length Required";
case 412:
return "Precondition Failed";
case 413:
return "Request Entity Too Large";
case 414:
return "Request-URI Too Long";
case 415:
return "Unsupported Media Type";
case 416:
return "Requested Range Not Satisfiable";
case 417:
return "Expectation Failed";
case 500:
return "Internal Server Error";
case 501:
return "Not Implemented";
case 502:
return "Bad Gateway";
case 503:
return "Service Unavailable";
case 504:
return "Gateway Timeout";
case 505:
return "HTTP Version Not Supported";
default:
return NULL;
}
}
static OF_INLINE OFString *
normalizedKey(OFString *key)
{
char *cString = of_strdup(key.UTF8String);
unsigned char *tmp = (unsigned char *)cString;
bool firstLetter = true;
|
︙ | | |
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
|
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
|
-
+
|
}
return [OFString stringWithUTF8StringNoCopy: cString
freeWhenDone: true];
}
@implementation OFHTTPServerResponse
- (instancetype)initWithSocket: (OFTCPSocket *)sock
- (instancetype)initWithSocket: (OFStreamSocket *)sock
server: (OFHTTPServer *)server
request: (OFHTTPRequest *)request
{
self = [super init];
_statusCode = 500;
_socket = [sock retain];
|
︙ | | |
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
|
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
|
-
+
-
+
|
- (void)of_sendHeaders
{
void *pool = objc_autoreleasePoolPush();
OFMutableDictionary OF_GENERIC(OFString *, OFString *) *headers;
OFEnumerator *keyEnumerator, *valueEnumerator;
OFString *key, *value;
[_socket writeFormat: @"HTTP/%@ %d %s\r\n",
[_socket writeFormat: @"HTTP/%@ %d %@\r\n",
self.protocolVersionString, _statusCode,
statusCodeToString(_statusCode)];
of_http_status_code_to_string(_statusCode)];
headers = [[_headers mutableCopy] autorelease];
if ([headers objectForKey: @"Date"] == nil) {
OFString *date = [[OFDate date]
dateStringWithFormat: @"%a, %d %b %Y %H:%M:%S GMT"];
|
︙ | | |
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
|
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
|
-
+
-
+
-
-
+
-
|
[self of_sendHeaders];
if (!_chunked)
return [_socket writeBuffer: buffer
length: length];
pool = objc_autoreleasePoolPush();
[_socket writeString: [OFString stringWithFormat: @"%zx\r\n", length]];
[_socket writeString: [OFString stringWithFormat: @"%zX\r\n", length]];
objc_autoreleasePoolPop(pool);
[_socket writeBuffer: buffer
length: length];
[_socket writeBuffer: "\r\n"
[_socket writeString: @"\r\n"];
length: 2];
return length;
}
- (void)close
{
if (_socket == nil)
@throw [OFNotOpenException exceptionWithObject: self];
@try {
if (!_headersSent)
[self of_sendHeaders];
if (_chunked)
[_socket writeBuffer: "0\r\n\r\n"
[_socket writeString: @"0\r\n\r\n"];
length: 5];
} @catch (OFWriteFailedException *e) {
id <OFHTTPServerDelegate> delegate = _server.delegate;
if ([delegate respondsToSelector: @selector(server:
didReceiveExceptionForResponse:request:exception:)])
[delegate server: _server
didReceiveExceptionForResponse: self
|
︙ | | |
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
|
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
|
-
+
|
return -1;
return _socket.fileDescriptorForWriting;
}
@end
@implementation OFHTTPServerConnection
- (instancetype)initWithSocket: (OFTCPSocket *)sock
- (instancetype)initWithSocket: (OFStreamSocket *)sock
server: (OFHTTPServer *)server
{
self = [super init];
@try {
_socket = [sock retain];
_server = [server retain];
|
︙ | | |
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
|
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
|
-
-
+
+
|
pos = [line rangeOfString: @" "].location;
if (pos == OF_NOT_FOUND)
return [self sendErrorAndClose: 400];
method = [line substringWithRange: of_range(0, pos)];
@try {
_method = of_http_request_method_from_string(method.UTF8String);
} @catch (OFInvalidFormatException *e) {
_method = of_http_request_method_from_string(method);
} @catch (OFInvalidArgumentException *e) {
return [self sendErrorAndClose: 405];
}
@try {
of_range_t range = of_range(pos + 1, line.length - pos - 10);
path = [[[line substringWithRange:
|
︙ | | |
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
519
520
521
522
523
524
|
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
|
+
+
-
+
+
+
-
+
-
-
+
+
-
-
+
+
+
+
|
- (bool)parseHeaders: (OFString *)line
{
OFString *key, *value, *old;
size_t pos;
if (line.length == 0) {
bool chunked = [[_headers objectForKey: @"Transfer-Encoding"]
isEqual: @"chunked"];
OFString *contentLengthString;
OFString *contentLengthString =
[_headers objectForKey: @"Content-Length"];
intmax_t contentLength = 0;
if ((contentLengthString =
if (contentLengthString != nil) {
[_headers objectForKey: @"Content-Length"]) != nil) {
intmax_t contentLength;
if (chunked || contentLengthString.length == 0)
return [self sendErrorAndClose: 400];
@try {
contentLength =
contentLengthString.decimalValue;
} @catch (OFInvalidFormatException *e) {
return [self sendErrorAndClose: 400];
}
if (contentLength < 0)
return [self sendErrorAndClose: 400];
}
if (chunked || contentLengthString != nil) {
[_requestBody release];
_requestBody = nil;
_requestBody = [[OFHTTPServerRequestBodyStream alloc]
initWithSocket: _socket
chunked: chunked
contentLength: contentLength];
[_timer invalidate];
[_timer release];
_timer = nil;
}
|
︙ | | |
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
|
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
|
-
+
-
+
+
|
}
- (bool)sendErrorAndClose: (short)statusCode
{
OFString *date = [[OFDate date]
dateStringWithFormat: @"%a, %d %b %Y %H:%M:%S GMT"];
[_socket writeFormat: @"HTTP/1.1 %d %s\r\n"
[_socket writeFormat: @"HTTP/1.1 %d %@\r\n"
@"Date: %@\r\n"
@"Server: %@\r\n"
@"\r\n",
statusCode, statusCodeToString(statusCode),
statusCode,
of_http_status_code_to_string(statusCode),
date, _server.name];
return false;
}
- (void)createResponse
{
|
︙ | | |
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
|
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
|
-
+
+
+
+
+
+
|
response: response];
objc_autoreleasePoolPop(pool);
}
@end
@implementation OFHTTPServerRequestBodyStream
- (instancetype)initWithSocket: (OFTCPSocket *)sock
- (instancetype)initWithSocket: (OFStreamSocket *)sock
chunked: (bool)chunked
contentLength: (uintmax_t)contentLength
{
self = [super init];
@try {
_socket = [sock retain];
_chunked = chunked;
_toRead = contentLength;
if (_chunked && _toRead > 0)
@throw [OFInvalidArgumentException exception];
} @catch (id e) {
[self release];
@throw e;
}
return self;
}
|
︙ | | |
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
|
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
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
732
733
734
735
736
737
738
739
740
741
742
|
-
+
+
-
-
+
-
+
+
+
+
+
+
+
-
-
+
+
-
-
+
+
-
+
+
+
+
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
|
{
return _atEndOfStream;
}
- (size_t)lowlevelReadIntoBuffer: (void *)buffer
length: (size_t)length
{
size_t ret;
if (_socket == nil)
@throw [OFNotOpenException exceptionWithObject: self];
if (_toRead == 0) {
_atEndOfStream = true;
if (_atEndOfStream)
return 0;
}
if (_socket.atEndOfStream)
@throw [OFTruncatedDataException exception];
/* Content-Length */
if (!_chunked) {
size_t ret;
if (length > _toRead)
length = (size_t)_toRead;
if (length > (uintmax_t)_toRead)
length = (size_t)_toRead;
ret = [_socket readIntoBuffer: buffer
length: length];
ret = [_socket readIntoBuffer: buffer
length: length];
_toRead -= ret;
_toRead -= ret;
if (_toRead == 0)
_atEndOfStream = true;
return ret;
return ret;
}
/* Chunked */
if (_toRead == -2) {
char tmp[2];
switch ([_socket readIntoBuffer: tmp
length: 2]) {
case 2:
_toRead++;
if (tmp[1] != '\n')
@throw [OFInvalidFormatException exception];
case 1:
_toRead++;
if (tmp[0] != '\r')
@throw [OFInvalidFormatException exception];
}
if (_setAtEndOfStream && _toRead == 0)
_atEndOfStream = true;
return 0;
} else if (_toRead == -1) {
char tmp;
if ([_socket readIntoBuffer: &tmp
length: 1] == 1) {
_toRead++;
if (tmp != '\n')
@throw [OFInvalidFormatException exception];
}
if (_setAtEndOfStream && _toRead == 0)
_atEndOfStream = true;
return 0;
} else if (_toRead > 0) {
if (length > (uintmax_t)_toRead)
length = (size_t)_toRead;
length = [_socket readIntoBuffer: buffer
length: length];
_toRead -= length;
if (_toRead == 0)
_toRead = -2;
return length;
} else {
void *pool = objc_autoreleasePoolPush();
OFString *line;
of_range_t range;
@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:
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 &&
range.location == OF_NOT_FOUND)
@throw [OFTruncatedDataException exception];
else
@throw [OFInvalidFormatException exception];
}
if ((_toRead = line.hexadecimalValue) < 0)
@throw [OFOutOfRangeException exception];
if (_toRead == 0) {
_setAtEndOfStream = true;
_toRead = -2;
}
objc_autoreleasePoolPop(pool);
return 0;
}
}
- (bool)hasDataInReadBuffer
{
return (super.hasDataInReadBuffer || _socket.hasDataInReadBuffer);
}
|
︙ | | |
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
|
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
|
-
+
-
-
+
+
|
[thread stop];
[_threadPool release];
_threadPool = nil;
#endif
}
- (void)of_handleAcceptedSocket: (OFTCPSocket *)acceptedSocket
- (void)of_handleAcceptedSocket: (OFStreamSocket *)acceptedSocket
{
OFHTTPServerConnection *connection = [[[OFHTTPServerConnection alloc]
initWithSocket: acceptedSocket
server: self] autorelease];
acceptedSocket.delegate = connection;
[acceptedSocket asyncReadLine];
}
- (bool)socket: (OFTCPSocket *)sock
didAcceptSocket: (OFTCPSocket *)acceptedSocket
- (bool)socket: (OFStreamSocket *)sock
didAcceptSocket: (OFStreamSocket *)acceptedSocket
exception: (id)exception
{
if (exception != nil) {
if (![_delegate respondsToSelector:
@selector(server:didReceiveExceptionOnListeningSocket:)])
return false;
|
︙ | | |