(function (__global__){ var __o__ = (function () { var __o__ = Object.create(null); var HttpServer = (function () { var __o__ = 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 `

` tag var html = '

' + multiPath + '

\n'; // add the directory content to `html` for (var i = 0; i < fileList.length; i++) { var p0 = System.Path.joinList ([localPath, fileList [i]]); // local path on the machine (Windows or UNIX style) of the i-th entry var p1 = System.Path.joinList ([branchDir, fileList [i]]); // branch path of the i-th entry var p2 = p1.replace (/\\/g, '/'); // URL path, always UNIX/URL style with "/" (rather than "\\") if (System.Path.isFile (p0)) { html += fileLink (p2, fileList[i]) + '\n'; } else if (System.Path.isDirectory (p0)) { html += dirLink (p2, fileList[i]) + '\n'; } else { // i.e. `p0` is neither file nor directory html += noLink (fileList[i]) + '\n'; } } // wrap `html` in a `htmlDoc` and return that var title = "`" + localPath + "` content"; var css = '* { line-height: 200%; }\n' + 'a { text-decoration: none; }\n' + 'a, span { padding: 3px; margin: 2px }' ; var htmlDoc = Html.htmlDoc ({title: title, css: css}) (html); return htmlDoc; } }; var ContentType = { '.html' : 'text/html', '.htm' : 'text/html', '.text' : 'text/plain', '.txt' : 'text/plain', '.text' : 'text/plain', '.markdown' : 'text/plain', '.md' : 'text/plain', '.pod' : 'text/plain', '.tex' : 'text/plain', '.css' : 'text/css', '.xml' : 'text/xml', '.xhtml' : 'text/xhtml', '.flatcode' : 'text/plain', // only in `tof.js` '.tex' : 'text/plain', '.json' : 'application/json', '.js' : 'application/javascript; charset=UTF-8' }; var defaultDocProducer = function (rec) { var recStr = require('util').inspect (rec, {showHidden: true, depth: null}); var recStr = Html.entityify (recStr); var html = '\n' + '\n' + '\n' + ' \n' + '\n' + '\n' + '
' + 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 ( '

This path exists, but is neither a file nor a directory

\n' + '
' + 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 ( '

The path does not exist

\n' + '
' + 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 ( '

Error

\n' + '
' + 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 ( '

This path exists, but is neither a file nor a directory

\n' + '
' + 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 ( '

The path does not exist

\n' + '
' + 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 ( '

Error

\n' + '
' + 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; } }; var listens = function () { return this.listening; }; var 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); } }; var 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); } }; __o__.HTTP_PORT_NUMBER = HTTP_PORT_NUMBER; __o__.directoryContentPage = directoryContentPage; __o__.ContentType = ContentType; __o__.defaultDocProducer = defaultDocProducer; __o__.fileServer = fileServer; __o__.docProducerServer = docProducerServer; __o__.fileAndDocProducerServer = fileAndDocProducerServer; __o__.prototype.listens = listens; __o__.prototype.start = start; __o__.prototype.stop = stop; return __o__; })(); __o__.HttpServer = HttpServer; return __o__; })(); for (k in __o__) { __global__[k] = __o__[k]; } })(this);