From 8d6ac4afcc3d8aae1a18e30d05847115dd8a9b91 Mon Sep 17 00:00:00 2001 From: Flummi Date: Mon, 28 Nov 2016 08:25:23 +0100 Subject: [PATCH] +404 --- src/websrv.js | 51 ++++++++++++++++++++++----------------------------- 1 file changed, 22 insertions(+), 29 deletions(-) diff --git a/src/websrv.js b/src/websrv.js index 8494b77..884a4bc 100644 --- a/src/websrv.js +++ b/src/websrv.js @@ -181,22 +181,29 @@ function Websrv(tlib) { var start = 0; var end = 0; var range = req.headers['range']; - var stat = fs.statSync(filePath); - if(range != null) { - start = parseInt(range.slice(range.indexOf('bytes=')+6, range.indexOf('-'))); - end = parseInt(range.slice(range.indexOf('-')+1, range.length)); - } - if(isNaN(end) || end == 0) end = stat.size-1; - if(start > end) return; - res.writeHead(206, { - 'Connection':'close', - 'Content-Type':contentType, - 'Content-Length':end - start, - 'Content-Range':'bytes '+start+'-'+end+'/'+stat.size, - 'Transfer-Encoding':'chunked' + fs.stat(filePath, (err, stat) => { + if(!err) { + if(range != null) { + start = parseInt(range.slice(range.indexOf('bytes=')+6, range.indexOf('-'))); + end = parseInt(range.slice(range.indexOf('-')+1, range.length)); + } + if(isNaN(end) || end == 0) end = stat.size-1; + if(start > end) return; + res.writeHead(206, { + 'Connection':'close', + 'Content-Type':contentType, + 'Content-Length':end - start, + 'Content-Range':'bytes '+start+'-'+end+'/'+stat.size, + 'Transfer-Encoding':'chunked' + }); + var stream = fs.createReadStream(filePath, { flags: 'r', start: start, end: end}); + stream.pipe(res); + } + else { + res.writeHead(200, { 'Content-Type': contentType }); + res.end('404 - f0ck you', 'utf-8'); + } }); - var stream = fs.createReadStream(filePath, { flags: 'r', start: start, end: end}); - stream.pipe(res); } else { fs.readFile(filePath, (error, content) => { @@ -218,20 +225,6 @@ function Websrv(tlib) { }); } } - /*fs.readFile(filePath, (error, content) => { - if(error) { - if(error.code == 'ENOENT') { - res.writeHead(200, { 'Content-Type': contentType }); - res.end('404 - f0ck you', 'utf-8'); - } - else { - res.writeHead(500); - res.end('Sorry, check with the site admin for error: '+error.code+' ..\n'); - res.end(); - } - } - });*/ - //} else if(filePath.match(/^\.\/api/i)) { // api var url = filePath.split('/'); if(url[2] === undefined) { // Mainpage