X-Git-Url: https://git.kengrimes.com/?p=henge%2Fkiak.git;a=blobdiff_plain;f=main.js;h=15bdde9acf4be8d22939b0daacc3b8cd8d5f3c57;hp=6a631af946d81c40b4935c7b06daaeb8b51b9d3a;hb=981f7c49567699addfa54664745b71fab2531b21;hpb=0e842dfeac9f6727b04dd242144e7c4a3a76f41e diff --git a/main.js b/main.js index 6a631af..15bdde9 100644 --- a/main.js +++ b/main.js @@ -16,11 +16,10 @@ const getport = require('get-port') const mime = require('mime') const opts = require('./opts.js') -const skelPage = fs.readFileSync('./skel.html', { encoding: 'utf8' }).split('') -const clientJS = fs.readFileSync(opts['client-js']) -const hostJS = fs.readFileSync(opts['host-js']) -const routes = {} - +const skelPage = fs.readFileSync('./skel.html', { encoding: 'utf8' }).split('') +const clientJS = fs.readFileSync(opts['client-js']) +const hostJS = fs.readFileSync(opts['host-js']) +const routes = {} const httpsOpts = (opts['no-tls'] ? undefined : { @@ -32,45 +31,35 @@ const routeConnection = (request,response) => { const serveFile = (fPath) => { fs.readFile(fPath, { encoding: 'utf8' }, (err, data) => { if (err || data == undefined) { - response.write(404) + response.writeHead(404) response.end() } else { - response.write(200, mime.lookup(fPath)) + response.writeHead(200, { 'Content-Type': mime.lookup(fPath) }) response.write(data) response.end() } }) } const htArgv = request.url.slice(1).split("?") - console.log(htArgv) - let routePath = htArgv[0].split(path.sep) + let routePath = htArgv[0].split('/') let routeName = routePath[0] - if (routeName === '') - routeName = opts['index'] - if (routeName in opts['bindings']) { - const followBind = (dir, fPaths) => { - fs.readdir(dir, (err, file) => { + if (routeName === '' || routeName === 'index.html') + serveFile(opts['index']) + else if (routeName in opts['bindings']) { + let localPath = path.normalize(opts['bindings'][routeName].concat(path.sep + routePath.slice(1).join(path.sep))) + console.log(localPath) + if (localPath.includes(opts['bindings'][routeName])) { + fs.readdir(localPath, (err, files) => { if (err) - serveFile(dir) - else if (fPaths.length == 0) - serveFile(`${dir}/index.html`) - else if (fPaths[0] !== '..' || path.normalize(`${dir}${path.sep}..`).includes(path.normalize(opts['bindings'][routeName]))) { - let nextFPath = fPaths.shift() - if (nextFPath in fPaths) - followBind(`${dir}${path.sep}${nextFPath}`, fPaths) - else { - response.writeHead(404) - response.end() - } - } - else { - console.log(`SEC: Ignored '..' in URL ${request.url}`) - console.log(request) - } + serveFile(localPath) + else + serveFile(`${localPath}/index.html`) }) } - followBind(opts['bindings'][routeName], htArgv[0].split(path.sep).slice(1)) + else { + console.log(`SEC: ${localPath} references files not in route`) + } } else if (routeName in routes) { const route = routes[routeName]