module.exports = (function () { var __obj__ = Object.create (null); ( (function (__global__) { var HttpServer = (function () { var __construct__ = require('http').Server; var HTTP_PORT_NUMBER = Type.synonym (INT); var directoryContentPage = function (rootDir) { return function (branchDir) { // the `localPath` is the combined `rootDir` and `branchDir` var localPath = System.Path.joinList ([rootDir, branchDir]); // first, verify that `localPath` is really a directory if (! (System.Path.isDirectory (localPath))) { throw Error ( "`HttpServer.dictoryContentPage(" + rootDir + ")(" + branchDir + ")` error, `" + localPath + "` is not a directory!" ); } // helper functions for `` tag generation function rootLink (href, text) { return '' + text + ''; } function dirLink (href, text) { return '' + text + ''; } function fileLink (href, text) { return '' + text + ''; } function noLink (text) { return '' + text + ''; } // now create the `fileList` with the `localPath` content var fileList = System.Dir.list (localPath); // `fileList` is say `['aaa.txt', 'bbb.js', 'ccc', ...]` // create `branchSteps`; which for say `"one/two/three"` is `['one', 'two', 'three']` var branchSteps = branchDir.split('/'); branchSteps = branchSteps.filter (function (name) { return (name !== ''); }); // create `multiPath`, the head line with links to each step of `branchDir` var href = '/'; // always a Path var multiPath = rootLink (href, System.Path.normalize(rootDir)); for (var i = 0; i < branchSteps.length; i++) { href += branchSteps[i] + '/'; multiPath += '/' + dirLink (href, branchSteps[i]); } // start `html` with `multiPath` wrapped in a `
' + recStr + '\n' + '\n' + '\n'; return html; }; var fileServer = function (port) { // reconstruct the involved directories and addresses var serverRoot = process.cwd(); var host = 'localhost'; var serverUrl = 'http://' + host + ":" + port; // create the server var server = new require('http').Server(); server.addListener ('request', function (req, res) { // reconstruct some request components var urlString = req.url; // the requested URL var urlRec = require ('url').parse(urlString); // the requested URL parsed as a record var method = req.method; // usually 'GET' var path = urlRec.pathname; // e.g. `'/one/two/three.txt'` var localPath = System.Path.joinList ([serverRoot, path]); // translate the `path` into the `localPath` on the server // print the log message var date = new Date(); console.log ( "`%s` request `%s` at %s:", method, urlString, date.toLocaleString() ); // // // BEGIN // // // (this block is the same in `fileServer` and `docProducerServer`) try { if (System.Path.exists (localPath)) { if (System.Path.isFile (localPath)) { var extension = System.Path.extname (path); // e.g. `'.html'` or `'.js'` or `'.txt'` or `''` console.log (" Responding with the `%s`-file `%s`.", extension, localPath); if (extension in ContentType) { res.writeHead (200, {"Content-Type": ContentType[extension]}); } else { res.writeHead (200, {"Content-Type": "application/octet-stream"}); } res.write (System.File.read (localPath)); res.end (); } else if (System.Path.isDirectory (localPath)) { console.log (" Responding with the listed directory at root `%s` and branch `%s`.", serverRoot, path); res.writeHead (200, {"Content-Type": "text/html; charset=UTF-8"}); res.write (directoryContentPage (serverRoot) (path)); res.end (); } else { console.log (" Responding with a 404 page, `%s` is neither a file nor directory.", localPath); res.writeHead (404, {"Content-Type": "text/html; charset=UTF-8"}); res.write ( '
' + localPath + '\n' ); res.end (); } } else { console.log (" Responding with a 404 page, `%s` does not exits on this system.", localPath); res.writeHead (404, {"Content-Type": "text/html; charset=UTF-8"}); res.write ( '
' + localPath + '\n' ); res.end (); } } catch (e) { console.log (" Responding with a 404 page due to error: " + e); res.writeHead (404, {"Content-Type": "text/html; charset=UTF-8"}); res.write ( '
' + e + '\n' ); res.end (); } // // // END // // // }); // add the `port` and `listening` properties to the `server` server.port = port; // a `$HttpPortnumber` property server.listening = false; // a `$Boolean` property, toggled on `start()` and `stop()` calls // return the `server` return server; }; var docProducerServer = function (port) { return function (docProducer) { // reconstruct the involved addresses var host = 'localhost'; var serverUrl = 'http://' + host + ":" + port; // create the server var server = new require('http').Server(); // the `request` event listener server.addListener ('request', function (req, res) { var urlRec = require ('url').parse (req.url); // the requested URL, parsed as a record var queryRec = require ('querystring').parse (urlRec.query); // the parsed query record of the URL var path = urlRec.pathname; // e.g. '/one/two/three.txt' var method = req.method; // usually, either 'GET' or 'POST' var dataString = ''; // the POST data as a string var dataRec; // the POST data as a record // action for the the next incoming POST `data` req.on ('data', function (data) { // add the next `data` chunk to `dataString` dataString += data; }); // action if the POST data stream has terminated req.on ('end', function () { // convert the dataRec = require('querystring').parse (dataString); // print the log message var date = new Date(); console.log ( "`%s` request for `%s` at %s:\n Path: `%s`\n Query record: `%j`\n Data record: `%s`", method, serverUrl, date.toLocaleString(), path, queryRec, Any.ellipsis (dataRec) ); // combine `queryRec` and `dataRec` into a single record `rec` var rec = {}; for (var k in queryRec) { rec[k] = queryRec[k]; } for (var k in dataRec) { rec[k] = dataRec[k]; } // generate the HTML document and return the result var htmlDoc = docProducer (rec); res.writeHead (200, {"Content-Type": "text/html"}); res.write (htmlDoc); res.end(); }); }); // add the `port` and `listening` properties to the `server` server.port = port; // a `$HttpPortnumber` property server.listening = false; // a `$Boolean` property, toggled on `start()` and `stop()` calls // return the `server` return server; } }; var fileAndDocProducerServer = function (port) { return function (docProducer) { // reconstruct the involved directories and addresses var serverRoot = process.cwd (); // current working directory/path of the server var host = 'localhost'; // host will always be `localhost` var serverUrl = 'http://' + host + ":" + port; // e.g. `http://localhost:55555` // create the `server` var server = new require('http').Server(); // implement the `request` event listener server.addListener ('request', function (req, res) { var urlRec = require ('url').parse (req.url); // the requested URL, parsed as a record var queryRec = require ('querystring').parse (urlRec.query); // the parsed query record of the URL var path = urlRec.pathname; // e.g. '/one/two/three.txt' var localPath = System.Path.joinList ([serverRoot, path]); // translate the `path` into the `localPath` on the server var method = req.method; // usually, either 'GET' or 'POST' var dataString = ''; // the POST data as a string var dataRec; // the POST data as a record // action for the the next incoming POST `data` req.on ('data', function (data) { // add the next `data` chunk to `dataString` dataString += data; }); // action if the POST data stream has terminated req.on ('end', function () { // convert the dataRec = require('querystring').parse (dataString); // print the log message var date = new Date(); console.log ( "`%s` request for `%s` at %s:\n Path: `%s`\n Query record: `%j`\n Data record: `%s`", method, serverUrl, date.toLocaleString(), path, queryRec, Any.ellipsis (dataRec) ); // combine `queryRec` and `dataRec` into a single record `rec` var rec = {}; for (var k in queryRec) { rec[k] = queryRec[k]; } for (var k in dataRec) { rec[k] = dataRec[k]; } // check if `rec` is empty; if so, return the file requested in `path`, otherwise return `docProducer(rec)` if (Object.keys(rec).length === 0) { // // // BEGIN // // // (this block is the same in `fileServer` and `docProducerServer`) try { if (System.Path.exists (localPath)) { if (System.Path.isFile (localPath)) { var extension = System.Path.extname (path); // e.g. `'.html'` or `'.js'` or `'.txt'` or `''` console.log (" Responding with the `%s`-file `%s`.", extension, localPath); if (extension in ContentType) { res.writeHead (200, {"Content-Type": ContentType[extension]}); } else { res.writeHead (200, {"Content-Type": "application/octet-stream"}); } res.write (System.File.read (localPath)); res.end (); } else if (System.Path.isDirectory (localPath)) { console.log (" Responding with the listed directory at root `%s` and branch `%s`.", serverRoot, path); res.writeHead (200, {"Content-Type": "text/html; charset=UTF-8"}); res.write (directoryContentPage (serverRoot) (path)); res.end (); } else { console.log (" Responding with a 404 page, `%s` is neither a file nor directory.", localPath); res.writeHead (404, {"Content-Type": "text/html; charset=UTF-8"}); res.write ( '
' + localPath + '\n' ); res.end (); } } else { console.log (" Responding with a 404 page, `%s` does not exits on this system.", localPath); res.writeHead (404, {"Content-Type": "text/html; charset=UTF-8"}); res.write ( '
' + localPath + '\n' ); res.end (); } } catch (e) { console.log (" Responding with a 404 page due to error: " + e); res.writeHead (404, {"Content-Type": "text/html; charset=UTF-8"}); res.write ( '
' + e + '\n' ); res.end (); } // // // END // // // } else { // `rec` is not empty, so return `docProducer(rec)` var htmlDoc = docProducer (rec); res.writeHead (200, {"Content-Type": "text/html"}); res.write (htmlDoc); res.end(); } }); }); // end of `request` event listener // add the `port` and `listening` properties to the `server` server.port = port; // a `$HttpPortnumber` property server.listening = false; // a `$Boolean` property, toggled on `start()` and `stop()` calls // return the `server` return server; } }; Object.defineProperty (__construct__, "HTTP_PORT_NUMBER", { writeable: false, configurable: false, enumerable: true, value: HTTP_PORT_NUMBER }); Object.defineProperty (__construct__, "directoryContentPage", { writeable: false, configurable: false, enumerable: true, value: directoryContentPage }); Object.defineProperty (__construct__, "ContentType", { writeable: false, configurable: false, enumerable: true, value: ContentType }); Object.defineProperty (__construct__, "defaultDocProducer", { writeable: false, configurable: false, enumerable: true, value: defaultDocProducer }); Object.defineProperty (__construct__, "fileServer", { writeable: false, configurable: false, enumerable: true, value: fileServer }); Object.defineProperty (__construct__, "docProducerServer", { writeable: false, configurable: false, enumerable: true, value: docProducerServer }); Object.defineProperty (__construct__, "fileAndDocProducerServer", { writeable: false, configurable: false, enumerable: true, value: fileAndDocProducerServer }); __construct__.prototype.listens = function () { return this.listening; }; __construct__.prototype.start = function () { try { var port = this.port; if (this.listening) { console.log ("The server is already listening on port `" + port + "`."); } else { this.listen (port); // the server now starts listening this.listening = true; // the `listening` property is set to `true` console.log ("Server started listening on port `" + port + "` ..." ); } return null; } catch (e) { throw Error ("`HttpServer::start(port)` error: " + e); } }; __construct__.prototype.stop = function () { try { var port = this.port; if (this.listening) { this.listening = false; this.close (); console.log ("Server stopped listening at port `" + port + "`."); } else { console.log ("Server was not listening, anyway."); } return null; } catch (e) { throw Error ("`HttpServer.stop()` error: " + e); } }; return __construct__; }) (); Object.defineProperty (__global__, "HttpServer", { writable: false, configurable: false, enumerable: true, value: HttpServer }); return null; }) ) (__obj__); return __obj__; }) ();