node-http-server – Blame information for rev 19
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
8 | office | 1 | #!/usr/bin/env node |
2 | |||
9 | office | 3 | /*************************************************************************/ |
4 | /* Copyright (C) 2017 Wizardry and Steamworks - License: GNU GPLv3 */ |
||
5 | /*************************************************************************/ |
||
6 | |||
8 | office | 7 | const url = require('url'); |
8 | const path = require('path'); |
||
9 | const fs = require('fs'); |
||
10 | const mime = require('mime'); |
||
14 | office | 11 | const auth = require("http-auth"); |
8 | office | 12 | |
14 | office | 13 | // Checks whether userPath is a child of rootPath. |
15 | office | 14 | function isRooted(userPath, rootPath, separator, callback) { |
15 | process.nextTick(() => { |
||
16 | userPath = userPath.split(separator).filter(Boolean); |
||
17 | rootPath = rootPath.split(separator).filter(Boolean); |
||
18 | callback(userPath.length >= rootPath.length && |
||
19 | rootPath.every((e, i) => e === userPath[i])); |
||
20 | }); |
||
8 | office | 21 | } |
22 | |||
14 | office | 23 | // Serves files. |
17 | office | 24 | function files(config, request, response, requestPath, callback) { |
14 | office | 25 | // Check if the file is accessible. |
17 | office | 26 | fs.access(requestPath, fs.constants.R_OK, (error) => { |
15 | office | 27 | if (error) { |
28 | response.statusCode = 403; |
||
29 | response.end(); |
||
30 | return; |
||
31 | } |
||
32 | |||
33 | response.setHeader( |
||
34 | 'Content-Type', |
||
17 | office | 35 | mime.lookup(requestPath) |
15 | office | 36 | ); |
37 | |||
17 | office | 38 | var readStream = fs.createReadStream(requestPath) |
15 | office | 39 | .on('open', () => { |
40 | response.statusCode = 200; |
||
41 | readStream.pipe(response); |
||
42 | }) |
||
43 | .on('error', () => { |
||
44 | response.statusCode = 500; |
||
45 | response.end(); |
||
46 | }); |
||
47 | |||
48 | }); |
||
49 | } |
||
50 | |||
51 | // Serves a directory listing or the document index in case it exists. |
||
17 | office | 52 | function index(config, request, response, requestPath, requestURL, callback) { |
53 | const root = path.resolve(requestPath, config.site.index); |
||
15 | office | 54 | fs.stat(root, (error, stats) => { |
17 | office | 55 | if (error) { |
19 | office | 56 | if (config.site.indexing |
17 | office | 57 | .some((directory) => |
58 | directory.toUpperCase() === requestURL.toUpperCase())) { |
||
59 | fs.readdir(requestPath, (error, paths) => { |
||
60 | if (error) { |
||
61 | process.nextTick(() => { |
||
62 | callback('Could not list directory: ' + |
||
63 | requestPath, |
||
64 | module.exports.error.level.ERROR |
||
65 | ); |
||
66 | }); |
||
67 | response.statusCode = 500; |
||
68 | response.end(); |
||
69 | return; |
||
70 | } |
||
15 | office | 71 | process.nextTick(() => { |
17 | office | 72 | callback('Directory listing requested for: ' + |
73 | requestPath, |
||
74 | module.exports.error.level.INFO |
||
15 | office | 75 | ); |
76 | }); |
||
17 | office | 77 | response.statusCode = 200; |
78 | response.write(JSON.stringify(paths)); |
||
15 | office | 79 | response.end(); |
80 | }); |
||
81 | |||
17 | office | 82 | return; |
83 | } |
||
19 | office | 84 | |
18 | office | 85 | // Could not access directory index file and directory listing not allowed. |
86 | response.statusCode = 404; |
||
87 | response.end(); |
||
88 | return; |
||
19 | office | 89 | |
15 | office | 90 | } |
91 | |||
92 | // Serve the document index. |
||
17 | office | 93 | fs.access(requestPath, fs.constants.R_OK, (error) => { |
14 | office | 94 | if (error) { |
15 | office | 95 | process.nextTick(() => { |
96 | callback('The server was unable to access the filesystem path: ' + |
||
17 | office | 97 | requestPath, |
15 | office | 98 | module.exports.error.level.WARN |
99 | ); |
||
100 | }); |
||
14 | office | 101 | response.statusCode = 403; |
102 | response.end(); |
||
103 | return; |
||
104 | } |
||
105 | |||
15 | office | 106 | // Set MIME content type. |
14 | office | 107 | response.setHeader( |
108 | 'Content-Type', |
||
15 | office | 109 | mime.lookup(root) |
14 | office | 110 | ); |
111 | |||
15 | office | 112 | var readStream = fs.createReadStream(root) |
14 | office | 113 | .on('open', () => { |
114 | response.statusCode = 200; |
||
115 | readStream.pipe(response); |
||
116 | }) |
||
117 | .on('error', () => { |
||
118 | response.statusCode = 500; |
||
119 | response.end(); |
||
120 | }); |
||
121 | |||
122 | }); |
||
123 | }); |
||
124 | } |
||
125 | |||
17 | office | 126 | // Determines whether the requested filesystem request path is a directory or a file. |
127 | function serve(config, request, response, requestPath, requestURL, callback) { |
||
128 | fs.stat(requestPath, (error, stats) => { |
||
15 | office | 129 | // Document does not exist. |
130 | if (error) { |
||
131 | response.statusCode = 404; |
||
132 | response.end(); |
||
133 | return; |
||
134 | } |
||
14 | office | 135 | |
19 | office | 136 | if (stats.isDirectory()) { |
137 | // Directory is requested so provide directory indexes. |
||
138 | index(config, request, response, requestPath, requestURL, callback); |
||
139 | return; |
||
15 | office | 140 | } |
19 | office | 141 | // A file was requested so provide the file. |
142 | files(config, request, response, requestPath, callback); |
||
14 | office | 143 | }); |
144 | } |
||
145 | |||
8 | office | 146 | module.exports = { |
147 | error: { |
||
148 | level: { |
||
149 | INFO: 1, |
||
150 | WARN: 2, |
||
151 | ERROR: 3 |
||
152 | } |
||
153 | }, |
||
11 | office | 154 | process: (config, request, response, root, callback) => { |
8 | office | 155 | process.nextTick(() => { |
14 | office | 156 | // Create digest authentication. |
157 | const authentication = auth.digest({ |
||
158 | realm: config.auth.realm, |
||
159 | file: path.resolve( |
||
160 | path.dirname(require.main.filename), |
||
161 | config.auth.digest |
||
162 | ) |
||
163 | }); |
||
164 | |||
8 | office | 165 | const requestAddress = request.socket.address(); |
17 | office | 166 | const requestURL = url.parse( |
14 | office | 167 | request.url, true |
168 | ); |
||
8 | office | 169 | |
17 | office | 170 | const trimmedPath = requestURL |
8 | office | 171 | .pathname |
172 | .split('/') |
||
173 | .filter(Boolean) |
||
174 | .join('/'); |
||
17 | office | 175 | const requestPath = trimmedPath === '/' ? |
8 | office | 176 | path.join(root, trimmedPath) : |
177 | path.resolve(root, trimmedPath); |
||
178 | |||
17 | office | 179 | isRooted(requestPath, root, path.sep, (rooted) => { |
15 | office | 180 | if (!rooted) { |
181 | process.nextTick(() => { |
||
182 | callback('Attempted path traversal: ' + |
||
183 | requestAddress.address + ':' + |
||
184 | requestAddress.port + |
||
185 | ' requesting: ' + |
||
17 | office | 186 | requestURL.pathname, |
15 | office | 187 | module.exports.error.level.WARN |
188 | ); |
||
189 | }); |
||
190 | response.statusCode = 404; |
||
191 | response.end(); |
||
192 | return; |
||
193 | } |
||
8 | office | 194 | |
19 | office | 195 | // If authentication is required for this path then perform authentication. |
196 | if (config.auth.locations.some( |
||
197 | (authPath) => authPath.toUpperCase() === requestURL.pathname.toUpperCase())) { |
||
198 | // Requested location requires authentication. |
||
199 | authentication.check(request, response, (request, response) => { |
||
14 | office | 200 | process.nextTick(() => { |
19 | office | 201 | callback('Authenticated client: ' + |
14 | office | 202 | requestAddress.address + ':' + |
203 | requestAddress.port + |
||
204 | ' accessing: ' + |
||
17 | office | 205 | requestURL.pathname, |
14 | office | 206 | module.exports.error.level.INFO |
207 | ); |
||
8 | office | 208 | }); |
17 | office | 209 | serve(config, |
210 | request, |
||
211 | response, |
||
212 | requestPath, |
||
213 | requestURL.pathname, |
||
214 | callback |
||
215 | ); |
||
19 | office | 216 | }); |
217 | return; |
||
15 | office | 218 | } |
19 | office | 219 | |
220 | // If no authentication is required then serve the request. |
||
221 | process.nextTick(() => { |
||
222 | callback('Client: ' + |
||
223 | requestAddress.address + ':' + |
||
224 | requestAddress.port + |
||
225 | ' accessing: ' + |
||
226 | requestURL.pathname, |
||
227 | module.exports.error.level.INFO |
||
228 | ); |
||
229 | }); |
||
230 | serve(config, |
||
231 | request, |
||
232 | response, |
||
233 | requestPath, |
||
234 | requestURL.pathname, |
||
235 | callback |
||
236 | ); |
||
15 | office | 237 | }); |
8 | office | 238 | }); |
239 | } |
||
240 | }; |