Skip to content

Commit 599d987

Browse files
committed
Merge branch 'fix-edge-cases'
2 parents 5d46c66 + 82b1803 commit 599d987

File tree

2 files changed

+35
-32
lines changed

2 files changed

+35
-32
lines changed

lib/ecstatic/show-dir/index.js

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,7 @@ module.exports = (opts) => {
4242

4343
fs.stat(dir, (statErr, stat) => {
4444
if (statErr) {
45-
if (handleError) {
46-
status[500](res, next, { error: statErr });
47-
} else {
48-
next();
49-
}
45+
status[500](res, next, { error: statErr, handleError });
5046
return;
5147
}
5248

@@ -55,11 +51,7 @@ module.exports = (opts) => {
5551
let files = _files;
5652

5753
if (readErr) {
58-
if (handleError) {
59-
status[500](res, next, { error: readErr });
60-
} else {
61-
next();
62-
}
54+
status[500](res, next, { error: readErr, handleError });
6355
return;
6456
}
6557

@@ -152,11 +144,7 @@ module.exports = (opts) => {
152144
if (path.resolve(dir, '..').slice(0, root.length) === root) {
153145
fs.stat(path.join(dir, '..'), (err, s) => {
154146
if (err) {
155-
if (handleError) {
156-
status[500](res, next, { error: err });
157-
} else {
158-
next();
159-
}
147+
status[500](res, next, { error: err, handleError });
160148
return;
161149
}
162150
dirs.unshift(['..', s]);

lib/ecstatic/status-handlers.js

Lines changed: 32 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,17 @@ exports['416'] = (res, next) => {
5454
// flagrant error
5555
exports['500'] = (res, next, opts) => {
5656
res.statusCode = 500;
57+
58+
if (!opts.handleError && next) {
59+
next(opts.error);
60+
return;
61+
}
62+
63+
if (res.headersSent) {
64+
res.destroy();
65+
return;
66+
}
67+
5768
res.setHeader('content-type', 'text/html');
5869
const error = String(opts.error.stack || opts.error || 'No specified error');
5970
const html = `${[
@@ -76,21 +87,25 @@ exports['500'] = (res, next, opts) => {
7687
// bad request
7788
exports['400'] = (res, next, opts) => {
7889
res.statusCode = 400;
79-
res.setHeader('content-type', 'text/html');
80-
const error = opts && opts.error ? String(opts.error) : 'Malformed request.';
81-
const html = `${[
82-
'<!doctype html>',
83-
'<html>',
84-
' <head>',
85-
' <meta charset="utf-8">',
86-
' <title>400 Bad Request</title>',
87-
' </head>',
88-
' <body>',
89-
' <p>',
90-
` ${he.encode(error)}`,
91-
' </p>',
92-
' </body>',
93-
'</html>',
94-
].join('\n')}\n`;
95-
res.end(html);
90+
if (typeof next === 'function') {
91+
next();
92+
} else {
93+
res.setHeader('content-type', 'text/html');
94+
const error = opts && opts.error ? String(opts.error) : 'Malformed request.';
95+
const html = `${[
96+
'<!doctype html>',
97+
'<html>',
98+
' <head>',
99+
' <meta charset="utf-8">',
100+
' <title>400 Bad Request</title>',
101+
' </head>',
102+
' <body>',
103+
' <p>',
104+
` ${he.encode(error)}`,
105+
' </p>',
106+
' </body>',
107+
'</html>',
108+
].join('\n')}\n`;
109+
res.end(html);
110+
}
96111
};

0 commit comments

Comments
 (0)