ObjWebServer  Diff

Differences From Artifact [f30751d900]:

To Artifact [2c2a999a6a]:


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
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







-
+

-
+









-
+



















-
-
+
+

-
-
-
-
-
+
+
+
+
+


-
-
+
+


-
+









-
+




-
-
-
-
+
+
+
+












	_config = [[ConfigParser alloc]
	    initWithConfigPath: @"ObjWebServer.xml"];

	modules = [OFMutableArray array];
	for (OFXMLElement *config in [_config modules]) {
		OFString *path =
		    [[config attributeForName: @"path"] stringValue];
		    [config attributeForName: @"path"].stringValue;
		OFString *prefix =
		    [[config attributeForName: @"prefix"] stringValue];
		    [config attributeForName: @"prefix"].stringValue;
		OFPlugin <Module> *module = [self loadModuleAtPath: path
							withConfig: config];

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

	for (ListenConfig *listenConfig in [_config listenConfigs])
	for (ListenConfig *listenConfig in _config.listenConfigs)
		[self startWebserverWithListenConfig: listenConfig];
}

- (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)startWebserverWithListenConfig: (ListenConfig *)listenConfig
{
	OFHTTPServer *server = [OFHTTPServer server];
	[server setHost: [listenConfig host]];
	[server setPort: [listenConfig port]];
	server.host = listenConfig.host;
	server.port = listenConfig.port;

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

	[server setNumberOfThreads: [OFSystemInfo numberOfCPUs] + 1];
	[server setDelegate: self];
	server.numberOfThreads = [OFSystemInfo numberOfCPUs] + 1;
	server.delegate = self;

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

	[server start];
}

-      (void)server: (OFHTTPServer *)server
  didReceiveRequest: (OFHTTPRequest *)request
	requestBody: (OFStream *)requestBody
	   response: (OFHTTPResponse *)response
{
	OFString *path = [[request URL] path];
	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])
		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