15
16
17
18
19
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
52
53
54
|
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#import <ObjFW/ObjFW.h>
#import "Module.h"
@interface StaticModule: OFPlugin <Module>
{
OFString *_root;
}
@end
@implementation StaticModule
- (void)dealloc
{
[_root release];
[super dealloc];
}
- (void)parseConfig: (OFXMLElement *)element
{
_root = [[[element elementForName: @"root"] stringValue] copy];
}
- (void)server: (OFHTTPServer *)server
didReceiveRequest: (OFHTTPRequest *)request
requestBody: (OFStream *)requestBody
response: (OFHTTPResponse *)response
{
}
@end
StaticModule *
init_plugin(void)
{
return [[[StaticModule alloc] init] autorelease];
}
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
>
>
|
>
>
>
>
|
>
>
>
>
>
>
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
|
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
15
16
17
18
19
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
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
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
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
|
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#import <ObjFW/ObjFW.h>
#import "Module.h"
#define BUFFER_SIZE 4096
@interface StaticModule: OFPlugin <Module>
{
OFString *_root;
OFDictionary OF_GENERIC(OFString *, OFString *) *_MIMETypes;
}
@end
@interface StaticModule_FileSender: OFObject <OFStreamDelegate>
{
@public
OFFile *_file;
OFHTTPResponse *_response;
}
@end
static OFData *
readData(OFStream *stream)
{
void *buffer;
OFData *ret;
if ((buffer = malloc(BUFFER_SIZE)) == NULL)
@throw [OFOutOfMemoryException
exceptionWithRequestedSize: BUFFER_SIZE];
@try {
size_t length = [stream readIntoBuffer: buffer
length: BUFFER_SIZE];
ret = [OFData dataWithItemsNoCopy: buffer
count: length
freeWhenDone: true];
} @catch (id e) {
free(buffer);
@throw e;
}
return ret;
}
@implementation StaticModule_FileSender
- (void)dealloc
{
[_file release];
[_response release];
[super dealloc];
}
- (OFData *)stream: (OF_KINDOF(OFStream *))stream
didWriteData: (OFData *)data
bytesWritten: (size_t)bytesWritten
exception: (id)exception
{
if (exception != nil || [_file isAtEndOfStream])
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];
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];
}
[MIMETypes makeImmutable];
_MIMETypes = [MIMETypes mutableCopy];
}
- (bool)handleRequest: (OFHTTPRequest *)request
requestBody: (OFStream *)requestBody
response: (OFHTTPResponse *)response
{
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)
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];
return false;
}
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 asyncWriteData: readData(fileSender->_file)];
return true;
}
@end
StaticModule *
init_plugin(void)
{
return [[[StaticModule alloc] init] autorelease];
}
|