Remove mime type stuff from Server object, tidy up for consistency with Site object...
authorNick Downing <nick@ndcode.org>
Tue, 4 Dec 2018 02:03:43 +0000 (13:03 +1100)
committerNick Downing <nick@ndcode.org>
Tue, 4 Dec 2018 02:03:55 +0000 (13:03 +1100)
Server.js

index 5ba15c4..142d4ae 100644 (file)
--- a/Server.js
+++ b/Server.js
@@ -15,13 +15,9 @@ let Server = function() {
 
   this.enable_caching = false
   this.listen = []
-  this.mime_types = {}
   this.hosts = {}
   this.roots = {}
 
-  this.mime_type_html = 'text/html; charset=utf-8'
-  this.mime_type_default = 'application/octet-stream'
-
   this.resources = new Resources()
   this.jst_cache = this.resources.ref(
     'jst_cache:.',
@@ -176,34 +172,25 @@ Server.prototype.refresh_config = async function() {
 
     this.listen = listen
 
-    this.mime_types = config.mime_types
-    this.mime_type_html =
-      Object.prototype.hasOwnProperty.call(this.mime_types, '.html') ?
-      this.mime_types['.html'] :
-      this.mime_type_default
-
     this.hosts = config.hosts
   }
 }
 
-Server.prototype.serve = function(response, status, mime_type, data, message) {
-  console.log(message)
+Server.prototype.serve_internal = function(response, status, mime_type, data) {
   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
-  // and won't change frequently (but we'll need cache busting eventually)
-  if (this.enable_caching && mime_type !== this.mime_type_html)
-    response.setHeader('Cache-Control', 'max-age=3600')
+  // no real need to cache errors and hostname redirects
+  // (pathname redirects on the other hand will be cached, see Site.js)
   response.setHeader('Content-Type', mime_type)
   response.setHeader('Content-Length', data.length)
   response.end(data)
 }
 
 Server.prototype.die = function(response, pathname, message) {
-  this.serve(
+  console.log(message)
+  this.serve_internal(
     response,
     404,
-    this.mime_type_html,
+    'text/html; charset=utf-8',
     Buffer.from(
       `<html>
   <head>
@@ -216,18 +203,18 @@ Server.prototype.die = function(response, pathname, message) {
   </body>
 </html>
 `
-    ),
-    message
+    )
   )
 }
 
 Server.prototype.redirect = function(response, location, message) {
+  console.log(message)
   response.statusCode = 301
   response.setHeader('Location', location)
-  this.serve(
+  this.serve_internal(
     response,
     301,
-    this.mime_type_html,
+    'text/html; charset=utf-8',
     Buffer.from(
       `<html>
   <head>
@@ -240,8 +227,7 @@ Server.prototype.redirect = function(response, location, message) {
   </body>
 </html>
 `
-    ),
-    message
+    )
   )
 }
 
@@ -272,7 +258,7 @@ Server.prototype.respond = async function(request, response, protocol) {
         new_host += ':' + parsed_url.port
       this.redirect(
         response,
-        `${parsed_url.protocol}//${redirect}${request.url}`,
+        `${parsed_url.protocol}//${new_host}${request.url}`,
         `redirecting ${parsed_url.host} to ${new_host}`
       )
       return
@@ -310,12 +296,12 @@ Server.prototype.respond = async function(request, response, protocol) {
   }     
   catch (err) {
     let message = (err.stack || err.message).toString()
-    this.serve(
+    console.log(message)
+    this.serve_internal(
       response,
       500,
-      this.mime_type_html,
-      Buffer.from(`<html><body><pre>${message}</pre></body></html>`),
-      message
+      'text/html; charset=utf-8',
+      Buffer.from(`<html><body><pre>${message}</pre></body></html>`)
     )
   }
 }