ObjWebServer  Check-in [7b4329bd1c]

Overview
Comment:Adjust to ObjFW changes
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | trunk
Files: files | file ages | folders
SHA3-256: 7b4329bd1cf5a76c93fb76e25bffd6068db688f342b1bb19bb1dbdfcd977dbc1
User & Date: js 2021-04-30 21:35:57
Context
2021-04-30
21:35
Adjust to ObjFW changes Leaf check-in: 7b4329bd1c user: js tags: trunk
2019-03-17
03:32
Use dot syntax check-in: 58aa5e718e user: js tags: trunk
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Changes to ConfigParser.m.

49
50
51
52
53
54
55




56
57
58
59
60
61
62


63
64
65
66
67
68
69
}

- (instancetype)initWithConfigPath: (OFString *)configPath
{
	self = [super init];

	@try {




		OFXMLElement *config = [[OFXMLElement alloc]
		    initWithFile: configPath];
		@try {
			[self _parseConfig: config];
		} @finally {
			[config release];
		}


	} @catch (id e) {
		[self release];
		@throw e;
	}

	return self;
}







>
>
>
>

|





>
>







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
}

- (instancetype)initWithConfigPath: (OFString *)configPath
{
	self = [super init];

	@try {
		void *pool = objc_autoreleasePoolPush();
		OFFile *configFile = [OFFile fileWithPath: configPath
						     mode: @"r"];

		OFXMLElement *config = [[OFXMLElement alloc]
		    initWithStream: configFile];
		@try {
			[self _parseConfig: config];
		} @finally {
			[config release];
		}

		objc_autoreleasePoolPop(pool);
	} @catch (id e) {
		[self release];
		@throw e;
	}

	return self;
}
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
		if (portString == nil)
			[self _invalidConfig:
			    @"<listen/> is missing port attribute"];

		listenConfig.host = host;

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

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







|







114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
		if (portString == nil)
			[self _invalidConfig:
			    @"<listen/> is missing port attribute"];

		listenConfig.host = host;

		@try {
			long long port = portString.longLongValue;
			if (port < 0 || port > 65535)
				@throw [OFInvalidFormatException exception];

			listenConfig.port = port;
		} @catch (OFInvalidFormatException *e) {
			[self _invalidConfig: @"<listen/> has invalid port"];
		}
165
166
167
168
169
170
171
172
173
174
175

	[modules makeImmutable];
	_modules = [modules copy];
}

- (void)_invalidConfig: (OFString *)message
{
	[of_stderr writeFormat: @"Error parsing config: %@", message];
	[OFApplication terminateWithStatus: 1];
}
@end







|



171
172
173
174
175
176
177
178
179
180
181

	[modules makeImmutable];
	_modules = [modules copy];
}

- (void)_invalidConfig: (OFString *)message
{
	[OFStdErr writeFormat: @"Error parsing config: %@", message];
	[OFApplication terminateWithStatus: 1];
}
@end

Changes to ObjWebServer.m.

66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
}

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







|

|







66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
}

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

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

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

	return module;
}

- (void)startWebserverWithListenConfig: (ListenConfig *)listenConfig
{
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
		server.certificateFile = listenConfig.TLSCertificateFile;
		server.privateKeyFile = listenConfig.TLSKeyFile;
	}

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

	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







|












|












|




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
		server.certificateFile = listenConfig.TLSCertificateFile;
		server.privateKeyFile = listenConfig.TLSKeyFile;
	}

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

	OFLog(@"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;

	OFLog(@"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
{
	OFLog(@"Exception on listening socket: %@", exception);

	return true;
}
@end

Changes to StaticModule.m.

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

- (void)parseConfig: (OFXMLElement *)config
{
	OFMutableDictionary OF_GENERIC(OFString *, OFString *) *MIMETypes;

	_root = [[config elementForName: @"root"].stringValue copy];
	if (_root == nil) {
		[of_stderr writeString:
		    @"Error parsing config: No <root/> element!"];
		[OFApplication terminateWithStatus: 1];
	}

	MIMETypes = [OFMutableDictionary dictionary];
	for (OFXMLElement *MIMEType in [config elementsForName: @"mime-type"]) {
		OFString *extension =
		    [MIMEType attributeForName: @"extension"].stringValue;
		OFString *type =
		    [MIMEType attributeForName: @"type"].stringValue;

		if (extension == nil) {
			[of_stderr writeString:
			    @"Error parsing config: "
			    @"<mime-type/> has no extension attribute!"];
			[OFApplication terminateWithStatus: 1];
		}
		if (type == nil) {
			[of_stderr writeString:
			    @"Error parsing config: "
			    @"<mime-type/> has no type attribute!"];
			[OFApplication terminateWithStatus: 1];
		}

		[MIMETypes setObject: type
			      forKey: extension];







|












|





|







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

- (void)parseConfig: (OFXMLElement *)config
{
	OFMutableDictionary OF_GENERIC(OFString *, OFString *) *MIMETypes;

	_root = [[config elementForName: @"root"].stringValue copy];
	if (_root == nil) {
		[OFStdErr writeString:
		    @"Error parsing config: No <root/> element!"];
		[OFApplication terminateWithStatus: 1];
	}

	MIMETypes = [OFMutableDictionary dictionary];
	for (OFXMLElement *MIMEType in [config elementsForName: @"mime-type"]) {
		OFString *extension =
		    [MIMEType attributeForName: @"extension"].stringValue;
		OFString *type =
		    [MIMEType attributeForName: @"type"].stringValue;

		if (extension == nil) {
			[OFStdErr writeString:
			    @"Error parsing config: "
			    @"<mime-type/> has no extension attribute!"];
			[OFApplication terminateWithStatus: 1];
		}
		if (type == nil) {
			[OFStdErr writeString:
			    @"Error parsing config: "
			    @"<mime-type/> has no type attribute!"];
			[OFApplication terminateWithStatus: 1];
		}

		[MIMETypes setObject: type
			      forKey: extension];
185
186
187
188
189
190
191
192
193
194
195
	[response asyncWriteData: readData(fileSender->_file)];

	return true;
}
@end

StaticModule *
init_plugin(void)
{
	return [[[StaticModule alloc] init] autorelease];
}







|



185
186
187
188
189
190
191
192
193
194
195
	[response asyncWriteData: readData(fileSender->_file)];

	return true;
}
@end

StaticModule *
OFPluginInit(void)
{
	return [[[StaticModule alloc] init] autorelease];
}