17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
|
#include "config.h"
#include <stdlib.h>
#include <string.h>
#import "OFHTTPServer.h"
#import "OFData.h"
#import "OFDate.h"
#import "OFDictionary.h"
#import "OFHTTPRequest.h"
#import "OFHTTPResponse.h"
#import "OFNumber.h"
#import "OFTCPSocket.h"
#import "OFTLSSocket.h"
#import "OFTimer.h"
#import "OFURL.h"
#import "OFAlreadyConnectedException.h"
#import "OFInvalidArgumentException.h"
#import "OFInvalidFormatException.h"
#import "OFNotOpenException.h"
|
>
>
|
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
|
#include "config.h"
#include <stdlib.h>
#include <string.h>
#import "OFHTTPServer.h"
#import "OFArray.h"
#import "OFData.h"
#import "OFDate.h"
#import "OFDictionary.h"
#import "OFHTTPRequest.h"
#import "OFHTTPResponse.h"
#import "OFNumber.h"
#import "OFTCPSocket.h"
#import "OFTLSSocket.h"
#import "OFThread.h"
#import "OFTimer.h"
#import "OFURL.h"
#import "OFAlreadyConnectedException.h"
#import "OFInvalidArgumentException.h"
#import "OFInvalidFormatException.h"
#import "OFNotOpenException.h"
|
100
101
102
103
104
105
106
107
108
109
110
111
112
113
|
uintmax_t _toRead;
bool _atEndOfStream;
}
- (instancetype)initWithSocket: (OF_KINDOF(OFTCPSocket *))sock
contentLength: (uintmax_t)contentLength;
@end
static const char *
statusCodeToString(short code)
{
switch (code) {
case 100:
return "Continue";
|
>
>
>
>
>
>
|
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
|
uintmax_t _toRead;
bool _atEndOfStream;
}
- (instancetype)initWithSocket: (OF_KINDOF(OFTCPSocket *))sock
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";
|
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
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
|
- (void)close
{
[_socket release];
_socket = nil;
}
@end
@implementation OFHTTPServer
@synthesize host = _host, port = _port, usesTLS = _usesTLS;
@synthesize certificateFile = _certificateFile;
@synthesize privateKeyFile = _privateKeyFile;
@synthesize privateKeyPassphrase = _privateKeyPassphrase, delegate = _delegate;
@synthesize name = _name;
+ (instancetype)server
{
return [[[self alloc] init] autorelease];
}
- (instancetype)init
{
self = [super init];
_name = @"OFHTTPServer (ObjFW's HTTP server class "
@"<https://heap.zone/objfw/>)";
return self;
}
- (void)dealloc
{
[_host release];
[_listeningSocket release];
[_name release];
[super dealloc];
}
- (void)start
{
if (_host == nil)
@throw [OFInvalidArgumentException exception];
if (_listeningSocket != nil)
@throw [OFAlreadyConnectedException exception];
if (_usesTLS) {
|
>
|
<
|
>
>
>
>
>
>
|
>
|
<
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
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
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
|
- (void)close
{
[_socket release];
_socket = nil;
}
@end
#ifdef OF_HAVE_THREADS
@implementation OFHTTPServerThread
- (void)stop
{
[[OFRunLoop currentRunLoop] stop];
[self join];
}
@end
#endif
@implementation OFHTTPServer
@synthesize delegate = _delegate, name = _name;
+ (instancetype)server
{
return [[[self alloc] init] autorelease];
}
- (instancetype)init
{
self = [super init];
_name = @"OFHTTPServer (ObjFW's HTTP server class "
@"<https://heap.zone/objfw/>)";
#ifdef OF_HAVE_THREADS
_numberOfThreads = 1;
#endif
return self;
}
- (void)dealloc
{
[self stop];
[_host release];
[_listeningSocket release];
[_name release];
[super dealloc];
}
- (void)setHost: (OFString *)host
{
OFString *old;
if (_listeningSocket != nil)
@throw [OFAlreadyConnectedException exception];
old = _host;
_host = [host copy];
[old release];
}
- (OFString *)host
{
return _host;
}
- (void)setPort: (uint16_t)port
{
if (_listeningSocket != nil)
@throw [OFAlreadyConnectedException exception];
_port = port;
}
- (uint16_t)port
{
return _port;
}
- (void)setUsesTLS: (bool)usesTLS
{
if (_listeningSocket != nil)
@throw [OFAlreadyConnectedException exception];
_usesTLS = usesTLS;
}
- (bool)usesTLS
{
return _usesTLS;
}
- (void)setCertificateFile: (OFString *)certificateFile
{
OFString *old;
if (_listeningSocket != nil)
@throw [OFAlreadyConnectedException exception];
old = _certificateFile;
_certificateFile = [certificateFile copy];
[old release];
}
- (OFString *)certificateFile
{
return _certificateFile;
}
- (void)setPrivateKeyFile: (OFString *)privateKeyFile
{
OFString *old;
if (_listeningSocket != nil)
@throw [OFAlreadyConnectedException exception];
old = _privateKeyFile;
_privateKeyFile = [privateKeyFile copy];
[old release];
}
- (OFString *)privateKeyFile
{
return _privateKeyFile;
}
- (void)setPrivateKeyPassphrase: (const char *)privateKeyPassphrase
{
if (_listeningSocket != nil)
@throw [OFAlreadyConnectedException exception];
_privateKeyPassphrase = privateKeyPassphrase;
}
- (const char *)privateKeyPassphrase
{
return _privateKeyPassphrase;
}
#ifdef OF_HAVE_THREADS
- (void)setNumberOfThreads: (size_t)numberOfThreadas
{
if (numberOfThreadas == 0)
@throw [OFInvalidArgumentException exception];
if (_listeningSocket != nil)
@throw [OFAlreadyConnectedException exception];
_numberOfThreads = numberOfThreadas;
}
- (size_t)numberOfThreads
{
return _numberOfThreads;
}
#endif
- (void)start
{
void *pool = objc_autoreleasePoolPush();
if (_host == nil)
@throw [OFInvalidArgumentException exception];
if (_listeningSocket != nil)
@throw [OFAlreadyConnectedException exception];
if (_usesTLS) {
|
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
|
setPrivateKeyPassphrase: _privateKeyPassphrase];
} else
_listeningSocket = [[OFTCPSocket alloc] init];
_port = [_listeningSocket bindToHost: _host
port: _port];
[_listeningSocket listen];
[(OFTCPSocket *)_listeningSocket setDelegate: self];
[_listeningSocket asyncAccept];
}
- (void)stop
{
[_listeningSocket cancelAsyncRequests];
[_listeningSocket release];
_listeningSocket = nil;
}
- (bool)socket: (OF_KINDOF(OFTCPSocket *))sock
didAcceptSocket: (OF_KINDOF(OFTCPSocket *))acceptedSocket
exception: (id)exception
{
OFHTTPServer_Connection *connection;
if (exception != nil) {
if (![_delegate respondsToSelector:
@selector(server:didReceiveExceptionOnListeningSocket:)])
return false;
return [_delegate server: self
didReceiveExceptionOnListeningSocket: exception];
}
connection = [[[OFHTTPServer_Connection alloc]
initWithSocket: acceptedSocket
server: self] autorelease];
[(OFTCPSocket *)acceptedSocket setDelegate: connection];
[acceptedSocket asyncReadLine];
return true;
}
@end
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
<
<
>
>
|
>
|
<
>
>
>
>
|
>
>
>
|
|
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
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
|
setPrivateKeyPassphrase: _privateKeyPassphrase];
} else
_listeningSocket = [[OFTCPSocket alloc] init];
_port = [_listeningSocket bindToHost: _host
port: _port];
[_listeningSocket listen];
#ifdef OF_HAVE_THREADS
if (_numberOfThreads > 1) {
OFMutableArray *threads =
[OFMutableArray arrayWithCapacity: _numberOfThreads - 1];
for (size_t i = 1; i < _numberOfThreads; i++) {
OFHTTPServerThread *thread =
[OFHTTPServerThread thread];
[thread start];
[threads addObject: thread];
}
[threads makeImmutable];
_threadPool = [threads copy];
}
#endif
[(OFTCPSocket *)_listeningSocket setDelegate: self];
[_listeningSocket asyncAccept];
objc_autoreleasePoolPop(pool);
}
- (void)stop
{
[_listeningSocket cancelAsyncRequests];
[_listeningSocket release];
_listeningSocket = nil;
#ifdef OF_HAVE_THREADS
for (OFHTTPServerThread *thread in _threadPool)
[thread stop];
[_threadPool release];
_threadPool = nil;
#endif
}
- (void)of_handleAcceptedSocket: (OF_KINDOF(OFTCPSocket *))acceptedSocket
{
OFHTTPServer_Connection *connection = [[[OFHTTPServer_Connection alloc]
initWithSocket: acceptedSocket
server: self] autorelease];
[(OFTCPSocket *)acceptedSocket setDelegate: connection];
[acceptedSocket asyncReadLine];
}
- (bool)socket: (OF_KINDOF(OFTCPSocket *))sock
didAcceptSocket: (OF_KINDOF(OFTCPSocket *))acceptedSocket
exception: (id)exception
{
if (exception != nil) {
if (![_delegate respondsToSelector:
@selector(server:didReceiveExceptionOnListeningSocket:)])
return false;
return [_delegate server: self
didReceiveExceptionOnListeningSocket: exception];
}
#ifdef OF_HAVE_THREADS
if (_numberOfThreads > 1) {
OFHTTPServerThread *thread =
[_threadPool objectAtIndex: _nextThreadIndex];
if (++_nextThreadIndex >= _numberOfThreads - 1)
_nextThreadIndex = 0;
[self performSelector: @selector(of_handleAcceptedSocket:)
onThread: thread
withObject: acceptedSocket
waitUntilDone: false];
} else
#endif
[self of_handleAcceptedSocket: acceptedSocket];
return true;
}
@end
|