bugfixes
[henge/kiak.git] / main.js
diff --git a/main.js b/main.js
index 6a631af..15bdde9 100644 (file)
--- 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('<!--STRAPP_SRC-->')
-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('<!--STRAPP_SRC-->')
+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]