Improve serving, death and redirection messages, removing a lot of duplication
authorNick Downing <downing.nick@gmail.com>
Mon, 22 Oct 2018 00:33:07 +0000 (11:33 +1100)
committerNick Downing <downing.nick@gmail.com>
Mon, 22 Oct 2018 00:40:00 +0000 (11:40 +1100)
Server.js
Site.js

index 67f6335..3824c8c 100644 (file)
--- a/Server.js
+++ b/Server.js
@@ -231,7 +231,8 @@ Server.prototype.refresh_config = async function() {
     this.mime_type_default
 }
 
-Server.prototype.serve = function(response, status, mime_type, data) {
+Server.prototype.serve = function(response, status, mime_type, data, message) {
+  console.log(message)
   response.statusCode = status
   // html files will be direct recipient of links/bookmarks so can't have
   // a long lifetime, other files like css or images are often large files
@@ -243,7 +244,7 @@ Server.prototype.serve = function(response, status, mime_type, data) {
   response.end(data)
 }
 
-Server.prototype.die = function(response, pathname) {
+Server.prototype.die = function(response, pathname, message) {
   this.serve(
     response,
     404,
@@ -260,11 +261,12 @@ Server.prototype.die = function(response, pathname) {
   </body>
 </html>
 `
-    )
+    ),
+    message
   )
 }
 
-Server.prototype.redirect = function(response, location) {
+Server.prototype.redirect = function(response, location, message) {
   response.statusCode = 301
   response.setHeader('Location', location)
   this.serve(
@@ -275,7 +277,7 @@ Server.prototype.redirect = function(response, location) {
       `<html>
   <head>
     <meta http-equiv="content-type" content="text/html;charset=utf-8">
-    <title>301 Moved Permanetly</title>
+    <title>301 Moved Permanently</title>
   </head>
   <body style="font-family: sans-serif, serif, monospace; font-size: 16px;">
     <h2>301 Moved Permanently</h2>
@@ -283,7 +285,8 @@ Server.prototype.redirect = function(response, location) {
   </body>
 </html>
 `
-    )
+    ),
+    message
   )
 }
 
@@ -299,8 +302,11 @@ Server.prototype.respond = async function(request, response, protocol) {
     //console.log('parsed_url', parsed_url)
 
     if (!Object.prototype.hasOwnProperty.call(this.sites, parsed_url.hostname)) {
-      console.log('nonexistent site', parsed_url.hostname)
-      this.die(response, parsed_url.pathname)
+      this.die(
+        response,
+        parsed_url.pathname,
+        'nonexistent site', parsed_url.hostname
+      )
       return
     }
     let temp = this.sites[parsed_url.hostname]
@@ -309,8 +315,11 @@ Server.prototype.respond = async function(request, response, protocol) {
       let hostname = temp.domain
       if (parsed_url.port !== undefined)
         hostname += ':' + parsed_url.port
-      console.log('redirecting', parsed_url.host, 'to', hostname)
-      this.redirect(response, parsed_url.protocol + '//' + hostname + request.url)
+      this.redirect(
+        response,
+        `${parsed_url.protocol}//${hostname}${request.url}`,
+        `redirecting ${parsed_url.host} to ${hostname}`
+      )
       break
     case 'site':
       let site_factory
@@ -355,7 +364,6 @@ Server.prototype.respond = async function(request, response, protocol) {
           response: response,
           request: request,
           status: 200,
-          serve_from: undefined,
           server: this,
           site: site.object
         }
@@ -367,9 +375,13 @@ Server.prototype.respond = async function(request, response, protocol) {
   }
   catch (err) {
     let message = (err.stack || err.message).toString()
-    console.error(message)
-    let body = '<html><body><pre>' + message + '</pre></body></html>'
-    this.serve(response, 500, this.mime_type_html, Buffer.from(body, 'utf8'))
+    this.serve(
+      response,
+      500,
+      this.mime_type_html,
+      Buffer.from(`<html><body><pre>${message}</pre></body></html>`),
+      message
+    )
   }
 }
 
diff --git a/Site.js b/Site.js
index 53a228b..c09c7ba 100644 (file)
--- a/Site.js
+++ b/Site.js
@@ -14,6 +14,32 @@ let Site = function(server, root) {
   this.socket_io_connect_listeners = []
 }
 
+Site.prototype.serve = function(env, data, message_from) {
+  this.server.serve(
+    env.response,
+    env.status,
+    env.mime_type,
+    data,
+    `${env.parsed_url.host} serving ${env.pathname} size ${data.length} from ${message_from}`
+  )
+}
+
+Site.prototype.die = function(env, message) {
+  this.server.die(
+    env.response,
+    env.pathname,
+    `${env.parsed_url.host} ${message}`
+  )
+}
+
+Site.prototype.redirect = function(env, pathname) {
+  this.server.redirect(
+    env.response,
+    pathname + (env.parsed_url.search || ''),
+    `${env.parsed_url.host} redirecting ${env.pathname} to ${pathname}`
+  )
+}
+
 Site.prototype.get_email = function(path) {
   return /*await*/ this.server.get_email(this.root + path)
 }
@@ -72,14 +98,9 @@ Site.prototype.serve_jst = async function(env, pathname) {
       throw err
     return false
   }
-  env.serve_from = 'jst'
   let out = []
   await jst(env, out)
-  let data = Buffer.from(out.join(''))
-  console.log(
-    `${env.parsed_url.host} serving ${env.pathname} length ${data.length} from ${env.serve_from}`
-  )
-  this.server.serve(env.response, env.status, env.mime_type, data)
+  this.serve(env, Buffer.from(out.join('')), 'jst')
   return true
 }
 
@@ -96,10 +117,7 @@ Site.prototype.serve_less = async function(env, pathname) {
       throw err
     return false
   }
-  console.log(
-    `${env.parsed_url.host} serving ${env.pathname} length ${data.length} from less`
-  )
-  this.server.serve(env.response, env.status, env.mime_type, data)
+  this.serve(env, data, 'less')
   return true
 }
 
@@ -113,10 +131,7 @@ Site.prototype.serve_fs = async function(env, pathname) {
       throw err
     return false
   }
-  console.log(
-    `${env.parsed_url.host} serving ${env.pathname} length ${data.length} from fs`
-  )
-  this.server.serve(env.response, env.status, env.mime_type, data)
+  this.serve(env, data, 'fs')
   return true
 }
 
@@ -133,25 +148,14 @@ Site.prototype.serve_zip = async function(env, pathname) {
   }
   if (!Object.prototype.hasOwnProperty.call(zip, env.pathname))
     return false
-  let data = zip[env.pathname]
-  console.log(
-    `${env.parsed_url.host} serving ${env.pathname} length ${data.length} from zip`
-  )
-  this.server.serve(env.response, env.status, env.mime_type, data)
+  this.serve(env, zip[env.pathname], 'zip')
   return true
 }
 
 Site.prototype.respond = async function(env) {
   while (true) {
     if (env.pathname_pos >= env.pathname.length) {
-      let pathname = env.pathname + '/index.html'
-      console.log(
-        `${env.parsed_url.host} redirecting ${env.pathname} to ${pathname}`
-      )
-      this.server.redirect(
-        env.response,
-        pathname + (env.parsed_url.search || '')
-      )
+      this.redirect(env, env.pathname + '/index.html')
       return
     }
 
@@ -163,22 +167,10 @@ Site.prototype.respond = async function(env) {
     let filename = env.pathname.slice(i, j)
 
     if (filename.length === 0) {
-      if (j >= env.pathname.length) {
-        let pathname = env.pathname + 'index.html'
-        console.log(
-          `${env.parsed_url.host} redirecting ${env.pathname} to ${pathname}`
-        )
-        this.server.redirect(
-          env.response,
-          pathname + (env.parsed_url.search || '')
-        )
-      }
-      else {
-        console.log(
-          `${env.parsed_url.host} empty directory name in ${env.pathname}`
-        )
-        this.server.die(env.response, env.pathname)
-      }
+      if (j >= env.pathname.length)
+        this.redirect(env, env.pathname + 'index.html')
+      else
+        this.die(env, `empty directory name in ${env.pathname}`)
       return
     }
 
@@ -186,10 +178,7 @@ Site.prototype.respond = async function(env) {
       filename.charAt(0) === '.' ||
       filename.charAt(0) === '_'
     ) {
-      console.log(
-        `${env.parsed_url.host} bad component "${filename}" in ${env.pathname}`
-      )
-      this.server.die(env.response, env.pathname)
+      this.die(env, `bad component "${filename}" in ${env.pathname}`)
       return
     }
 
@@ -203,10 +192,7 @@ Site.prototype.respond = async function(env) {
       Object.prototype.hasOwnProperty.call(this.server.mime_types, filetype)
     ) {
       if (j < env.pathname.length) {
-        console.log(
-          `${env.parsed_url.host} non-directory filetype "${filetype}" in ${env.pathname}`
-        )
-        this.server.die(env.response, env.pathname)
+        this.die(env, `non-directory filetype "${filetype}" in ${env.pathname}`)
         return
       }
       env.mime_type = this.server.mime_types[filetype]
@@ -226,19 +212,16 @@ Site.prototype.respond = async function(env) {
     catch (err) {
       if (err.code !== 'ENOENT')
         throw err
-      console.log(
-        `${env.parsed_url.host} directory not found ${pathname}`
-      )
-      this.server.die(env.response, env.pathname)
+      this.die(env, `directory not found ${pathname}`)
       return
     }
     if (!stats.isDirectory()) {
-      console.log(
+      this.die(
+        env,
         j < env.pathname.length ?
-        `${env.parsed_url.host} not directory ${pathname}` :
-        `${env.parsed_url.host} unknown filetype "${filetype}" in ${pathname}`
+          `not directory ${pathname}` :
+          `unknown filetype "${filetype}" in ${pathname}`
       )
-      this.server.die(env.response, env.pathname)
       return
     }
   }    
@@ -249,10 +232,7 @@ Site.prototype.respond = async function(env) {
     !await this.serve_fs(env, env.pathname) &&
     !await this.serve_zip(env, '/favicons.zip')
   ) {
-    console.log(
-      `${env.parsed_url.host} file not found ${env.pathname}`
-    )
-    this.server.die(env.response, env.pathname)
+    this.die(env, `file not found ${env.pathname}`)
   }
 }