ObjWebServer  Diff

Differences From Artifact [05567321bf]:

To Artifact [a1c01b5487]:


20
21
22
23
24
25
26
27















28
29

30
31
32
33
34
35
36
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43

44
45
46
47
48
49
50
51








+
+
+
+
+
+
+
+
+
+
+
+
+
+
+

-
+








@interface ConfigParser ()
- (void)_parseConfig: (OFXMLElement *)config;
- (void)_parseListens: (OFArray OF_GENERIC(OFXMLElement *) *)elements;
- (void)_parseModules: (OFArray OF_GENERIC(OFXMLElement *) *)elements;
- (void)_invalidConfig: (OFString *)message;
@end

@implementation ListenConfig
@synthesize host = _host, port = _port;
@synthesize TLSCertificateFile = _TLSCertificateFile;
@synthesize TLSKeyFile = _TLSKeyFile;

- (void)dealloc
{
	[_host release];
	[_TLSCertificateFile release];
	[_TLSKeyFile release];

	[super dealloc];
}
@end

@implementation ConfigParser
@synthesize listenHosts = _listenHosts, modules = _modules;
@synthesize listenConfigs = _listenConfigs, modules = _modules;

- (instancetype)init
{
	OF_INVALID_INIT_METHOD
}

- (instancetype)initWithConfigPath: (OFString *)configPath
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
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







-
+




















-
-
+
+


+
+




-
+








+
+

-
-
+
+


-
+




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

+
+
+
-
-
+
+







	}

	return self;
}

- (void)dealloc
{
	[_listenHosts release];
	[_listenConfigs release];

	[super dealloc];
}

- (void)_parseConfig: (OFXMLElement *)config
{
	void *pool = objc_autoreleasePoolPush();

	if ([config namespace] != nil ||
	    ![[config name] isEqual: @"ObjWebServer"])
		[self _invalidConfig: @"Root element is not ObjWebServer"];

	[self _parseListens: [config elementsForName: @"listen"]];
	[self _parseModules: [config elementsForName: @"module"]];

	objc_autoreleasePoolPop(pool);
}

- (void)_parseListens: (OFArray OF_GENERIC(OFXMLElement *) *)elements
{
	OFMutableArray OF_GENERIC(OFPair OF_GENERIC(OFString *, OFNumber *) *)
	    *listenHosts = [OFMutableArray array];
	OFMutableArray OF_GENERIC(ListenConfig *) *listenConfigs =
	    [OFMutableArray array];

	for (OFXMLElement *element in elements) {
		ListenConfig *listenConfig =
		    [[[ListenConfig alloc] init] autorelease];
		OFString *host = [[element
		    attributeForName: @"host"] stringValue];
		OFString *portString = [[element
		    attributeForName: @"port"] stringValue];
		OFNumber *port;
		OFXMLElement *TLS = [element elementForName: @"tls"];

		if (host == nil)
			[self _invalidConfig:
			    @"<listen/> is missing host attribute"];
		if (portString == nil)
			[self _invalidConfig:
			    @"<listen/> is missing port attribute"];

		[listenConfig setHost: host];

		@try {
			intmax_t tmp = [portString decimalValue];
			if (tmp < 0 || tmp > 65535)
			intmax_t port = [portString decimalValue];
			if (port < 0 || port > 65535)
				@throw [OFInvalidFormatException exception];

			port = [OFNumber numberWithUInt16: (uint16_t)tmp];
			[listenConfig setPort: port];
		} @catch (OFInvalidFormatException *e) {
			[self _invalidConfig: @"<listen/> has invalid port"];
		}

		if (TLS != nil) {
			OFString *certificateFile =
			    [[TLS attributeForName: @"cert"] stringValue];
			OFString *keyFile =
			    [[TLS attributeForName: @"key"] stringValue];
		[listenHosts addObject: [OFPair pairWithFirstObject: host
						       secondObject: port]];
	}

			if (certificateFile == nil)
				[self _invalidConfig:
				    @"<tls/> has no cert attribute"];
			if (keyFile == nil)
				[self _invalidConfig:
				    @"<tls/> has no key attribute"];

			[listenConfig setTLSCertificateFile: certificateFile];
			[listenConfig setTLSKeyFile: keyFile];
		}

		[listenConfigs addObject: listenConfig];
	}

	[listenHosts makeImmutable];
	_listenHosts = [listenHosts copy];
	[listenConfigs makeImmutable];
	_listenConfigs = [listenConfigs copy];
}

- (void)_parseModules: (OFArray OF_GENERIC(OFXMLElement *) *)elements
{
	OFMutableArray OF_GENERIC(OFXMLElement *) *modules =
	    [OFMutableArray array];