node-http-server – Diff between revs 18 and 19
?pathlinks?
Rev 18 | Rev 19 | |||
---|---|---|---|---|
Line 51... | Line 51... | |||
51 | // Serves a directory listing or the document index in case it exists. |
51 | // Serves a directory listing or the document index in case it exists. |
|
52 | function index(config, request, response, requestPath, requestURL, callback) { |
52 | function index(config, request, response, requestPath, requestURL, callback) { |
|
53 | const root = path.resolve(requestPath, config.site.index); |
53 | const root = path.resolve(requestPath, config.site.index); |
|
54 | fs.stat(root, (error, stats) => { |
54 | fs.stat(root, (error, stats) => { |
|
55 | if (error) { |
55 | if (error) { |
|
56 | if(config.site.indexing |
56 | if (config.site.indexing |
|
57 | .some((directory) => |
57 | .some((directory) => |
|
58 | directory.toUpperCase() === requestURL.toUpperCase())) { |
58 | directory.toUpperCase() === requestURL.toUpperCase())) { |
|
59 | fs.readdir(requestPath, (error, paths) => { |
59 | fs.readdir(requestPath, (error, paths) => { |
|
60 | if (error) { |
60 | if (error) { |
|
61 | process.nextTick(() => { |
61 | process.nextTick(() => { |
|
Line 79... | Line 79... | |||
79 | response.end(); |
79 | response.end(); |
|
80 | }); |
80 | }); |
|
Line 81... | Line 81... | |||
81 | |
81 | |
|
82 | return; |
82 | return; |
|
83 | } |
83 | } |
|
84 | |
84 | |
|
85 | // Could not access directory index file and directory listing not allowed. |
85 | // Could not access directory index file and directory listing not allowed. |
|
86 | response.statusCode = 404; |
86 | response.statusCode = 404; |
|
87 | response.end(); |
87 | response.end(); |
|
88 | return; |
88 | return; |
|
89 | |
89 | |
|
Line 90... | Line 90... | |||
90 | } |
90 | } |
|
91 | |
91 | |
|
92 | // Serve the document index. |
92 | // Serve the document index. |
|
Line 131... | Line 131... | |||
131 | response.statusCode = 404; |
131 | response.statusCode = 404; |
|
132 | response.end(); |
132 | response.end(); |
|
133 | return; |
133 | return; |
|
134 | } |
134 | } |
|
Line 135... | Line 135... | |||
135 | |
135 | |
|
136 | switch (stats.isDirectory()) { |
136 | if (stats.isDirectory()) { |
|
137 | case true: // Directory is requested so provide directory indexes. |
137 | // Directory is requested so provide directory indexes. |
|
138 | index(config, request, response, requestPath, requestURL, callback); |
138 | index(config, request, response, requestPath, requestURL, callback); |
|
139 | break; |
- | ||
140 | default: // Browser requesting file. |
- | ||
141 | files(config, request, response, requestPath, callback); |
- | ||
142 | break; |
139 | return; |
|
- | 140 | } |
||
- | 141 | // A file was requested so provide the file. |
||
143 | } |
142 | files(config, request, response, requestPath, callback); |
|
144 | }); |
143 | }); |
|
Line 145... | Line 144... | |||
145 | } |
144 | } |
|
146 | |
145 | |
|
Line 191... | Line 190... | |||
191 | response.statusCode = 404; |
190 | response.statusCode = 404; |
|
192 | response.end(); |
191 | response.end(); |
|
193 | return; |
192 | return; |
|
194 | } |
193 | } |
|
Line 195... | Line 194... | |||
195 | |
194 | |
|
196 | // Check if the requested path requires authentication. |
195 | // If authentication is required for this path then perform authentication. |
|
197 | switch (config.auth.locations.some( |
196 | if (config.auth.locations.some( |
|
198 | (authPath) => authPath.toUpperCase() === requestURL.pathname.toUpperCase())) { |
- | ||
199 | case true: |
197 | (authPath) => authPath.toUpperCase() === requestURL.pathname.toUpperCase())) { |
|
200 | // Requested location requires authentication. |
198 | // Requested location requires authentication. |
|
201 | authentication.check(request, response, (request, response) => { |
- | ||
202 | process.nextTick(() => { |
- | ||
203 | callback('Authenticated client: ' + |
- | ||
204 | requestAddress.address + ':' + |
- | ||
205 | requestAddress.port + |
- | ||
206 | ' accessing: ' + |
- | ||
207 | requestURL.pathname, |
- | ||
208 | module.exports.error.level.INFO |
- | ||
209 | ); |
- | ||
210 | }); |
- | ||
211 | serve(config, |
- | ||
212 | request, |
- | ||
213 | response, |
- | ||
214 | requestPath, |
- | ||
215 | requestURL.pathname, |
- | ||
216 | callback |
- | ||
217 | ); |
- | ||
218 | }); |
- | ||
219 | break; |
- | ||
220 | default: |
199 | authentication.check(request, response, (request, response) => { |
|
221 | process.nextTick(() => { |
200 | process.nextTick(() => { |
|
222 | callback('Client: ' + |
201 | callback('Authenticated client: ' + |
|
223 | requestAddress.address + ':' + |
202 | requestAddress.address + ':' + |
|
224 | requestAddress.port + |
203 | requestAddress.port + |
|
225 | ' accessing: ' + |
204 | ' accessing: ' + |
|
226 | requestURL.pathname, |
205 | requestURL.pathname, |
|
Line 232... | Line 211... | |||
232 | response, |
211 | response, |
|
233 | requestPath, |
212 | requestPath, |
|
234 | requestURL.pathname, |
213 | requestURL.pathname, |
|
235 | callback |
214 | callback |
|
236 | ); |
215 | ); |
|
- | 216 | }); |
||
237 | break; |
217 | return; |
|
238 | } |
218 | } |
|
- | 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 | ); |
||
239 | }); |
237 | }); |
|
240 | }); |
238 | }); |
|
241 | } |
239 | } |
|
242 | }; |
240 | }; |