git ssb

30+

cel / git-ssb-web



Commit ac331ec120c1bf480ccc64ae36c3c27fbe380fc6

Consolidate serve buffer and content-type functions

Charles Lehner committed on 4/4/2016, 9:47:04 PM
Parent: d1e96510b7246fe9b0d685d132cfdfe322caa8f7

Files changed

index.jschanged
index.jsView
@@ -366,19 +366,17 @@
366366
367367 var hasOwnProp = Object.prototype.hasOwnProperty
368368
369369 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'
374372 }
375373
376374 var contentTypes = {
377375 css: 'text/css'
378376 }
379377
380-function readReqJSON(req, cb) {
378+function readReqForm(req, cb) {
381379 pull(
382380 toPull(req),
383381 pull.collect(function (err, bufs) {
384382 if (err) return cb(err)
@@ -504,11 +502,11 @@
504502 var dir = dirs[0]
505503
506504 if (req.method == 'POST') {
507505 if (isPublic)
508- return servePlainError(405, 'POST not allowed on public site')
506+ return serveBuffer(405, 'POST not allowed on public site')
509507 return readNext(function (cb) {
510- readReqJSON(req, function (err, data) {
508+ readReqForm(req, function (err, data) {
511509 if (err) return cb(null, serveError(err, 400))
512510 if (!data) return cb(null, serveError(new Error('No data'), 400))
513511
514512 switch (data.action) {
@@ -577,9 +575,9 @@
577575 case 'markdown':
578576 return cb(null, serveMarkdown(data.text, {id: data.repo}))
579577
580578 default:
581- cb(null, servePlainError(400, 'What are you trying to do?'))
579+ cb(null, serveBuffer(400, 'What are you trying to do?'))
582580 }
583581 })
584582 })
585583 }
@@ -603,20 +601,20 @@
603601 function serveFile(req, dirs, outside) {
604602 var filename = path.resolve.apply(path, [__dirname].concat(dirs))
605603 // prevent escaping base dir
606604 if (!outside && filename.indexOf('../') === 0)
607- return servePlainError(403, '403 Forbidden')
605+ return serveBuffer(403, '403 Forbidden')
608606
609607 return readNext(function (cb) {
610608 fs.stat(filename, function (err, stats) {
611609 cb(null, err ?
612610 err.code == 'ENOENT' ? serve404(req)
613- : servePlainError(500, err.message)
611+ : serveBuffer(500, err.message)
614612 : 'if-modified-since' in req.headers &&
615613 new Date(req.headers['if-modified-since']) >= stats.mtime ?
616614 pull.once([304])
617615 : stats.isDirectory() ?
618- servePlainError(403, 'Directory not listable')
616+ serveBuffer(403, 'Directory not listable')
619617 : cat([
620618 pull.once([200, {
621619 'Content-Type': getContentType(filename),
622620 'Content-Length': stats.size,
@@ -637,35 +635,32 @@
637635 msg
638636 ])
639637 }
640638
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+
641649 function serve404(req) {
642- return servePlainError(404, '404 Not Found')
650+ return serveBuffer(404, '404 Not Found')
643651 }
644652
645653 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})
657659 }
658660
659661 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')
668663 }
669664
670665 function renderTry(read) {
671666 var ended
@@ -1499,9 +1494,9 @@
14991494 var pathLinks = path.length === 0 ? '' :
15001495 ': ' + linkPath([repo.id, 'tree'], [rev].concat(path))
15011496 var rawFilePath = [repo.id, 'raw', rev].concat(path)
15021497 var filename = path[path.length-1]
1503- var extension = filename.split('.').pop()
1498+ var extension = getExtension(filename)
15041499 cb(null, renderRepoPage(repo, 'code', rev, cat([
15051500 pull.once('<section><form action="" method="get">' +
15061501 '<h3>' + type + ': ' + rev + ' '),
15071502 revMenu(repo, rev),
@@ -1535,10 +1530,10 @@
15351530
15361531 function serveRepoRaw(repo, branch, path) {
15371532 return readNext(function (cb) {
15381533 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])
15411536 var contentType = imgMimes[extension]
15421537 cb(null, pull(object.read, serveRaw(object.length, contentType)))
15431538 })
15441539 })

Built with git-ssb-web