︙ | | | ︙ | |
45
46
47
48
49
50
51
52
53
54
55
56
57
58
|
* 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;
@end
static const char *
statusCodeToString(short code)
{
switch (code) {
|
>
|
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
|
* 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
context: (id)context
exception: (OFException *)exception;
@end
static const char *
statusCodeToString(short code)
{
switch (code) {
|
︙ | | | ︙ | |
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
|
OFMutableData *_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
length: (size_t)length
exception: (OFException *)exception;
- (bool)sendErrorAndClose: (short)statusCode;
- (void)createResponse;
@end
@implementation OFHTTPServer_Connection
- initWithSocket: (OFTCPSocket *)socket
|
>
>
|
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
|
OFMutableData *_body;
}
- initWithSocket: (OFTCPSocket *)socket
server: (OFHTTPServer *)server;
- (bool)socket: (OFTCPSocket *)socket
didReadLine: (OFString *)line
context: (id)context
exception: (OFException *)exception;
- (bool)parseProlog: (OFString *)line;
- (bool)parseHeaders: (OFString *)line;
- (bool)socket: (OFTCPSocket *)socket
didReadIntoBuffer: (char *)buffer
length: (size_t)length
context: (id)context
exception: (OFException *)exception;
- (bool)sendErrorAndClose: (short)statusCode;
- (void)createResponse;
@end
@implementation OFHTTPServer_Connection
- initWithSocket: (OFTCPSocket *)socket
|
︙ | | | ︙ | |
391
392
393
394
395
396
397
398
399
400
401
402
403
404
|
[_body release];
[super dealloc];
}
- (bool)socket: (OFTCPSocket *)socket
didReadLine: (OFString *)line
exception: (OFException *)exception
{
if (line == nil || exception != nil)
return false;
@try {
switch (_state) {
|
>
|
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
|
[_body release];
[super dealloc];
}
- (bool)socket: (OFTCPSocket *)socket
didReadLine: (OFString *)line
context: (id)context
exception: (OFException *)exception
{
if (line == nil || exception != nil)
return false;
@try {
switch (_state) {
|
︙ | | | ︙ | |
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
|
_body = [[OFMutableData alloc] init];
[_socket asyncReadIntoBuffer: buffer
length: BUFFER_SIZE
target: self
selector: @selector(socket:
didReadIntoBuffer:
length:exception:)];
[_timer setFireDate:
[OFDate dateWithTimeIntervalSinceNow: 5]];
return false;
}
_state = SEND_RESPONSE;
|
>
|
>
|
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
|
_body = [[OFMutableData alloc] init];
[_socket asyncReadIntoBuffer: buffer
length: BUFFER_SIZE
target: self
selector: @selector(socket:
didReadIntoBuffer:
length:context:
exception:)
context: nil];
[_timer setFireDate:
[OFDate dateWithTimeIntervalSinceNow: 5]];
return false;
}
_state = SEND_RESPONSE;
|
︙ | | | ︙ | |
568
569
570
571
572
573
574
575
576
577
578
579
580
581
|
return true;
}
- (bool)socket: (OFTCPSocket *)socket
didReadIntoBuffer: (char *)buffer
length: (size_t)length
exception: (OFException *)exception
{
if ([socket isAtEndOfStream] || exception != nil)
return false;
[_body addItems: buffer
count: length];
|
>
|
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
|
return true;
}
- (bool)socket: (OFTCPSocket *)socket
didReadIntoBuffer: (char *)buffer
length: (size_t)length
context: (id)context
exception: (OFException *)exception
{
if ([socket isAtEndOfStream] || exception != nil)
return false;
[_body addItems: buffer
count: length];
|
︙ | | | ︙ | |
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
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
|
_listeningSocket = [[OFTCPSocket alloc] init];
_port = [_listeningSocket bindToHost: _host
port: _port];
[_listeningSocket listen];
[_listeningSocket asyncAcceptWithTarget: self
selector: @selector(of_socket:
didAcceptSocket:
exception:)];
}
- (void)stop
{
[_listeningSocket cancelAsyncRequests];
[_listeningSocket release];
_listeningSocket = nil;
}
- (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
didReceiveExceptionOnListeningSocket: exception];
return false;
}
connection = [[[OFHTTPServer_Connection alloc]
initWithSocket: clientSocket
server: self] autorelease];
[clientSocket asyncReadLineWithTarget: connection
selector: @selector(socket:didReadLine:
exception:)];
return true;
}
@end
|
|
|
>
>
|
>
|
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
|
_listeningSocket = [[OFTCPSocket alloc] init];
_port = [_listeningSocket bindToHost: _host
port: _port];
[_listeningSocket listen];
[_listeningSocket asyncAcceptWithTarget: self
selector: @selector(of_socket:
didAcceptSocket:context:
exception:)
context: nil];
}
- (void)stop
{
[_listeningSocket cancelAsyncRequests];
[_listeningSocket release];
_listeningSocket = nil;
}
- (bool)of_socket: (OFTCPSocket *)socket
didAcceptSocket: (OFTCPSocket *)clientSocket
context: (id)context
exception: (OFException *)exception
{
OFHTTPServer_Connection *connection;
if (exception != nil) {
if ([_delegate respondsToSelector:
@selector(server:didReceiveExceptionOnListeningSocket:)])
return [_delegate server: self
didReceiveExceptionOnListeningSocket: exception];
return false;
}
connection = [[[OFHTTPServer_Connection alloc]
initWithSocket: clientSocket
server: self] autorelease];
[clientSocket asyncReadLineWithTarget: connection
selector: @selector(socket:didReadLine:
context:exception:)
context: nil];
return true;
}
@end
|