ObjWebServer  Diff

Differences From Artifact [b87ee149c4]:

To Artifact [f30751d900]:


28
29
30
31
32
33
34
35

36
37
38
39
40
41
42
43
28
29
30
31
32
33
34

35

36
37
38
39
40
41
42







-
+
-







	ConfigParser *_config;
	OFArray OF_GENERIC(OFPair OF_GENERIC(OFString *, OFPlugin <Module> *) *)
	    *_modules;
}

- (OFPlugin <Module> *)loadModuleAtPath: (OFString *)path
			     withConfig: (OFXMLElement *)config;
- (void)startWebserverOnHost: (OFString *)host
- (void)startWebserverWithListenConfig: (ListenConfig *)listenConfig;
			port: (uint16_t)port;
@end

OF_APPLICATION_DELEGATE(ObjWebServer)

@implementation ObjWebServer
- (void)applicationDidFinishLaunching
{
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
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







-
-
+
-
-
-
-
+
-
-















-
+
-


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

+

-
+
+




















+
+
+
+
+
+
+
+


		[modules addObject: [OFPair pairWithFirstObject: prefix
						   secondObject: module]];
	}
	[modules makeImmutable];
	_modules = [modules copy];

	for (OFPair OF_GENERIC(OFString *, OFNumber *) *listenHost in
	    [_config listenHosts]) {
	for (ListenConfig *listenConfig in [_config listenConfigs])
		OFString *host = [listenHost firstObject];
		OFNumber *port = [listenHost secondObject];

		[self startWebserverOnHost: host
		[self startWebserverWithListenConfig: listenConfig];
				      port: [port uInt16Value]];
	}
}

- (OFPlugin <Module> *)loadModuleAtPath: (OFString *)path
			     withConfig: (OFXMLElement *)config
{
	OFPlugin <Module> *module;

	of_log(@"Loading module at %@", path);

	module = [OFPlugin pluginFromFile: path];
	[module parseConfig: config];

	return module;
}

- (void)startWebserverOnHost: (OFString *)host
- (void)startWebserverWithListenConfig: (ListenConfig *)listenConfig
			port: (uint16_t)port
{
	OFHTTPServer *server = [OFHTTPServer server];
	[server setHost: host];
	[server setPort: port];
	[server setDelegate: self];
	[server setHost: [listenConfig host]];
	[server setPort: [listenConfig port]];

	if ([listenConfig TLSCertificateFile] != nil &&
	    [listenConfig TLSKeyFile] != nil) {
		[server setUsesTLS: true];
		[server setCertificateFile: [listenConfig TLSCertificateFile]];
		[server setPrivateKeyFile: [listenConfig TLSKeyFile]];
	}

	[server setNumberOfThreads: [OFSystemInfo numberOfCPUs] + 1];
	[server setDelegate: self];

	of_log(@"Starting server on host %@ port %" PRIu16, host, port);
	of_log(@"Starting server on host %@ port %" PRIu16,
	    [listenConfig host], [listenConfig port]);

	[server start];
}

-      (void)server: (OFHTTPServer *)server
  didReceiveRequest: (OFHTTPRequest *)request
	requestBody: (OFStream *)requestBody
	   response: (OFHTTPResponse *)response
{
	OFString *path = [[request URL] path];

	of_log(@"Request: %@", request);

	for (OFPair OF_GENERIC(OFString *, id <Module>) *module in _modules)
		if ([path hasPrefix: [module firstObject]])
			if ([[module secondObject] handleRequest: request
						     requestBody: requestBody
							response: response])
				return;
}

-			  (bool)server: (OFHTTPServer *)server
  didReceiveExceptionOnListeningSocket: (id)exception
{
	of_log(@"Exception on listening socket: %@", exception);

	return true;
}
@end