Commit ac331ec120c1bf480ccc64ae36c3c27fbe380fc6
Consolidate serve buffer and content-type functions
Charles Lehner committed on 4/4/2016, 9:47:04 PMParent: d1e96510b7246fe9b0d685d132cfdfe322caa8f7
Files changed
index.js | changed |
index.js | ||
---|---|---|
@@ -366,19 +366,17 @@ | ||
366 | 366 | |
367 | 367 | var hasOwnProp = Object.prototype.hasOwnProperty |
368 | 368 | |
369 | 369 | function getContentType(filename) { |
370 | - var ext = filename.split('.').pop() | |
371 | - return hasOwnProp.call(contentTypes, ext) | |
372 | - ? contentTypes[ext] | |
373 | - : 'text/plain; charset=utf-8' | |
370 | + var ext = getExtension(filename) | |
371 | + return contentTypes[ext] || imgMimes[ext] || 'text/plain; charset=utf-8' | |
374 | 372 | } |
375 | 373 | |
376 | 374 | var contentTypes = { |
377 | 375 | css: 'text/css' |
378 | 376 | } |
379 | 377 | |
380 | -function readReqJSON(req, cb) { | |
378 | +function readReqForm(req, cb) { | |
381 | 379 | pull( |
382 | 380 | toPull(req), |
383 | 381 | pull.collect(function (err, bufs) { |
384 | 382 | if (err) return cb(err) |
@@ -504,11 +502,11 @@ | ||
504 | 502 | var dir = dirs[0] |
505 | 503 | |
506 | 504 | if (req.method == 'POST') { |
507 | 505 | if (isPublic) |
508 | - return servePlainError(405, 'POST not allowed on public site') | |
506 | + return serveBuffer(405, 'POST not allowed on public site') | |
509 | 507 | return readNext(function (cb) { |
510 | - readReqJSON(req, function (err, data) { | |
508 | + readReqForm(req, function (err, data) { | |
511 | 509 | if (err) return cb(null, serveError(err, 400)) |
512 | 510 | if (!data) return cb(null, serveError(new Error('No data'), 400)) |
513 | 511 | |
514 | 512 | switch (data.action) { |
@@ -577,9 +575,9 @@ | ||
577 | 575 | case 'markdown': |
578 | 576 | return cb(null, serveMarkdown(data.text, {id: data.repo})) |
579 | 577 | |
580 | 578 | default: |
581 | - cb(null, servePlainError(400, 'What are you trying to do?')) | |
579 | + cb(null, serveBuffer(400, 'What are you trying to do?')) | |
582 | 580 | } |
583 | 581 | }) |
584 | 582 | }) |
585 | 583 | } |
@@ -603,20 +601,20 @@ | ||
603 | 601 | function serveFile(req, dirs, outside) { |
604 | 602 | var filename = path.resolve.apply(path, [__dirname].concat(dirs)) |
605 | 603 | // prevent escaping base dir |
606 | 604 | if (!outside && filename.indexOf('../') === 0) |
607 | - return servePlainError(403, '403 Forbidden') | |
605 | + return serveBuffer(403, '403 Forbidden') | |
608 | 606 | |
609 | 607 | return readNext(function (cb) { |
610 | 608 | fs.stat(filename, function (err, stats) { |
611 | 609 | cb(null, err ? |
612 | 610 | err.code == 'ENOENT' ? serve404(req) |
613 | - : servePlainError(500, err.message) | |
611 | + : serveBuffer(500, err.message) | |
614 | 612 | : 'if-modified-since' in req.headers && |
615 | 613 | new Date(req.headers['if-modified-since']) >= stats.mtime ? |
616 | 614 | pull.once([304]) |
617 | 615 | : stats.isDirectory() ? |
618 | - servePlainError(403, 'Directory not listable') | |
616 | + serveBuffer(403, 'Directory not listable') | |
619 | 617 | : cat([ |
620 | 618 | pull.once([200, { |
621 | 619 | 'Content-Type': getContentType(filename), |
622 | 620 | 'Content-Length': stats.size, |
@@ -637,35 +635,32 @@ | ||
637 | 635 | msg |
638 | 636 | ]) |
639 | 637 | } |
640 | 638 | |
639 | + function serveBuffer(code, buf, contentType, headers) { | |
640 | + headers = headers || {} | |
641 | + headers['Content-Type'] = contentType || 'text/plain; charset=utf-8' | |
642 | + headers['Content-Length'] = Buffer.byteLength(buf) | |
643 | + return pull.values([ | |
644 | + [code, headers], | |
645 | + buf | |
646 | + ]) | |
647 | + } | |
648 | + | |
641 | 649 | function serve404(req) { |
642 | - return servePlainError(404, '404 Not Found') | |
650 | + return serveBuffer(404, '404 Not Found') | |
643 | 651 | } |
644 | 652 | |
645 | 653 | function serveRedirect(path) { |
646 | - var msg = '<!doctype><html><head><meta charset=utf-8>' + | |
647 | - '<title>Redirect</title></head>' + | |
648 | - '<body><p><a href="' + path + '">Continue</a></p></body></html>' | |
649 | - return pull.values([ | |
650 | - [302, { | |
651 | - 'Content-Length': Buffer.byteLength(msg), | |
652 | - 'Content-Type': 'text/html', | |
653 | - Location: path | |
654 | - }], | |
655 | - msg | |
656 | - ]) | |
654 | + return serveBuffer(302, | |
655 | + '<!doctype><html><head>' + | |
656 | + '<title>Redirect</title></head><body>' + | |
657 | + '<p><a href="' + escapeHTML(path) + '">Continue</a></p>' + | |
658 | + '</body></html>', 'text/html; charset=utf-8', {Location: path}) | |
657 | 659 | } |
658 | 660 | |
659 | 661 | function serveMarkdown(text, repo) { |
660 | - var html = markdown(text, repo) | |
661 | - return pull.values([ | |
662 | - [200, { | |
663 | - 'Content-Length': Buffer.byteLength(html), | |
664 | - 'Content-Type': 'text/html; charset=utf-8' | |
665 | - }], | |
666 | - html | |
667 | - ]) | |
662 | + return serveBuffer(200, markdown(text, repo), 'text/html; charset=utf-8') | |
668 | 663 | } |
669 | 664 | |
670 | 665 | function renderTry(read) { |
671 | 666 | var ended |
@@ -1499,9 +1494,9 @@ | ||
1499 | 1494 | var pathLinks = path.length === 0 ? '' : |
1500 | 1495 | ': ' + linkPath([repo.id, 'tree'], [rev].concat(path)) |
1501 | 1496 | var rawFilePath = [repo.id, 'raw', rev].concat(path) |
1502 | 1497 | var filename = path[path.length-1] |
1503 | - var extension = filename.split('.').pop() | |
1498 | + var extension = getExtension(filename) | |
1504 | 1499 | cb(null, renderRepoPage(repo, 'code', rev, cat([ |
1505 | 1500 | pull.once('<section><form action="" method="get">' + |
1506 | 1501 | '<h3>' + type + ': ' + rev + ' '), |
1507 | 1502 | revMenu(repo, rev), |
@@ -1535,10 +1530,10 @@ | ||
1535 | 1530 | |
1536 | 1531 | function serveRepoRaw(repo, branch, path) { |
1537 | 1532 | return readNext(function (cb) { |
1538 | 1533 | repo.getFile(branch, path, function (err, object) { |
1539 | - if (err) return cb(null, servePlainError(404, 'Blob not found')) | |
1540 | - var extension = path[path.length-1].split('.').pop() | |
1534 | + if (err) return cb(null, serveBuffer(404, 'Blob not found')) | |
1535 | + var extension = getExtension(path[path.length-1]) | |
1541 | 1536 | var contentType = imgMimes[extension] |
1542 | 1537 | cb(null, pull(object.read, serveRaw(object.length, contentType))) |
1543 | 1538 | }) |
1544 | 1539 | }) |
Built with git-ssb-web