Skip to content

Commit

Permalink
Merge branch 'fix-edge-cases'
Browse files Browse the repository at this point in the history
  • Loading branch information
jfhbrook committed Apr 20, 2019
2 parents 5d46c66 + 82b1803 commit 599d987
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 32 deletions.
18 changes: 3 additions & 15 deletions lib/ecstatic/show-dir/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,7 @@ module.exports = (opts) => {

fs.stat(dir, (statErr, stat) => {
if (statErr) {
if (handleError) {
status[500](res, next, { error: statErr });
} else {
next();
}
status[500](res, next, { error: statErr, handleError });
return;
}

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

if (readErr) {
if (handleError) {
status[500](res, next, { error: readErr });
} else {
next();
}
status[500](res, next, { error: readErr, handleError });
return;
}

Expand Down Expand Up @@ -152,11 +144,7 @@ module.exports = (opts) => {
if (path.resolve(dir, '..').slice(0, root.length) === root) {
fs.stat(path.join(dir, '..'), (err, s) => {
if (err) {
if (handleError) {
status[500](res, next, { error: err });
} else {
next();
}
status[500](res, next, { error: err, handleError });
return;
}
dirs.unshift(['..', s]);
Expand Down
49 changes: 32 additions & 17 deletions lib/ecstatic/status-handlers.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,17 @@ exports['416'] = (res, next) => {
// flagrant error
exports['500'] = (res, next, opts) => {
res.statusCode = 500;

if (!opts.handleError && next) {
next(opts.error);
return;
}

if (res.headersSent) {
res.destroy();
return;
}

res.setHeader('content-type', 'text/html');
const error = String(opts.error.stack || opts.error || 'No specified error');
const html = `${[
Expand All @@ -76,21 +87,25 @@ exports['500'] = (res, next, opts) => {
// bad request
exports['400'] = (res, next, opts) => {
res.statusCode = 400;
res.setHeader('content-type', 'text/html');
const error = opts && opts.error ? String(opts.error) : 'Malformed request.';
const html = `${[
'<!doctype html>',
'<html>',
' <head>',
' <meta charset="utf-8">',
' <title>400 Bad Request</title>',
' </head>',
' <body>',
' <p>',
` ${he.encode(error)}`,
' </p>',
' </body>',
'</html>',
].join('\n')}\n`;
res.end(html);
if (typeof next === 'function') {
next();
} else {
res.setHeader('content-type', 'text/html');
const error = opts && opts.error ? String(opts.error) : 'Malformed request.';
const html = `${[
'<!doctype html>',
'<html>',
' <head>',
' <meta charset="utf-8">',
' <title>400 Bad Request</title>',
' </head>',
' <body>',
' <p>',
` ${he.encode(error)}`,
' </p>',
' </body>',
'</html>',
].join('\n')}\n`;
res.end(html);
}
};

0 comments on commit 599d987

Please sign in to comment.