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
response.end(data)
}
-Server.prototype.die = function(response, pathname) {
+Server.prototype.die = function(response, pathname, message) {
this.serve(
response,
404,
</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(
`<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>
</body>
</html>
`
- )
+ ),
+ message
)
}
//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]
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
response: response,
request: request,
status: 200,
- serve_from: undefined,
server: this,
site: site.object
}
}
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
+ )
}
}
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)
}
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
}
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
}
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
}
}
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
}
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
}
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
}
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]
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
}
}
!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}`)
}
}