ObjWebServer  Diff

Differences From Artifact [6e7b57cfd5]:

To Artifact [1eb8b41163]:


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







-
+




-
+


















-
+









-
+

-
+







{
	[_file release];
	[_response release];

	[super dealloc];
}

- (OFData *)stream: (OF_KINDOF(OFStream *))stream
- (OFData *)stream: (OFStream *)stream
      didWriteData: (OFData *)data
      bytesWritten: (size_t)bytesWritten
	 exception: (id)exception
{
	if (exception != nil || [_file isAtEndOfStream])
	if (exception != nil || _file.atEndOfStream)
		return nil;

	return readData(_file);
}
@end

@implementation StaticModule
- (void)dealloc
{
	[_root release];

	[super dealloc];
}

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

	_root = [[[config elementForName: @"root"] stringValue] copy];
	_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];
		    [MIMEType attributeForName: @"extension"].stringValue;
		OFString *type =
		    [[MIMEType attributeForName: @"type"] stringValue];
		    [MIMEType attributeForName: @"type"].stringValue;

		if (extension == nil) {
			[of_stderr writeString:
			    @"Error parsing config: "
			    @"<mime-type/> has no extension attribute!"];
			[OFApplication terminateWithStatus: 1];
		}
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
154
155
156
157
158
159
160
161
162
163
164
165

166
167
168
169

170
171
172
173
174
175
176
177
178
179
180
181
182
183
184



185
186
187
188
189
190
191
192
193
194
195
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
154
155
156
157
158
159
160
161
162
163
164

165
166
167
168

169
170
171
172
173
174
175
176
177
178
179
180
181



182
183
184
185
186
187
188
189
190
191
192
193
194
195







-
-
+
+






-
-
+
+



















-
+



-
+












-
-
-
+
+
+











	_MIMETypes = [MIMETypes mutableCopy];
}

- (bool)handleRequest: (OFHTTPRequest *)request
	  requestBody: (OFStream *)requestBody
	     response: (OFHTTPResponse *)response
{
	OFURL *URL = [[request URL] URLByStandardizingPath];
	OFString *path = [URL path];
	OFURL *URL = request.URL.URLByStandardizingPath;
	OFString *path = URL.path;
	OFMutableDictionary *headers = [OFMutableDictionary dictionary];
	OFFileManager *fileManager;
	bool firstComponent = true;
	OFString *MIMEType;
	StaticModule_FileSender *fileSender;

	for (OFString *component in [URL pathComponents]) {
		if (firstComponent && [component length] != 0)
	for (OFString *component in URL.pathComponents) {
		if (firstComponent && component.length != 0)
			return false;

		if ([component isEqual: @"."] || [component isEqual: @".."])
			return false;

		firstComponent = false;
	}

	/* TODO: Properly handle for OSes that do not use UNIX paths */
	if (![path hasPrefix: @"/"])
		return false;

	path = [_root stringByAppendingString: path];

	fileManager = [OFFileManager defaultManager];
	if ([fileManager directoryExistsAtPath: path])
		path = [path stringByAppendingPathComponent: @"index.html"];

	if (![fileManager fileExistsAtPath: path]) {
		[response setStatusCode: 404];
		response.statusCode = 404;
		return false;
	}

	MIMEType = [_MIMETypes objectForKey: [path pathExtension]];
	MIMEType = [_MIMETypes objectForKey: path.pathExtension];
	if (MIMEType == nil)
		MIMEType = [_MIMETypes objectForKey: @""];

	if (MIMEType != nil)
		[headers setObject: MIMEType
			    forKey: @"Content-Type"];

	fileSender = [[[StaticModule_FileSender alloc] init] autorelease];
	fileSender->_file = [[OFFile alloc] initWithPath: path
						    mode: @"r"];
	fileSender->_response = [response retain];

	[response setStatusCode: 200];
	[response setHeaders: headers];
	[response setDelegate: fileSender];
	response.statusCode = 200;
	response.headers = headers;
	response.delegate = fileSender;
	[response asyncWriteData: readData(fileSender->_file)];

	return true;
}
@end

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