<commit_before>#!/usr/bin/env node
// 
// Based on rpflorence's Gist @ https://gist.github.com/rpflorence/701407
// 

var http = require("http"),
	url  = require("url"),
	path = require("path"),
	fs   = require("fs"),
	conf = require("./conf/server.conf.js"),
	di   = require("./extensions/directory-indexing.js"),
	mime = require("./lib/node-mime/mime.js"),
	port = process.argv[2] || 8888;

// The base level of the server, nothing allowed below this.
// Do not end this in a "/". If you want the server's root, set this to empty-string: ""
// http.DOCUMENT_ROOT = "/Users/blake/Source";

http.createServer(function(request, response) {
	var objUrl = url.parse(request.url, true),
		uri = path.normalize(objUrl.pathname),
		filename = path.join(conf.DocumentRoot, uri);
	// process.cwd()

	// console.log("process.cwd()", process.cwd(), "request.url", request.url, "uri", uri);
	
	var writeEntireResponse = function(intStatus, strContent, objOptions) {
		if (arguments.length == 1) {
			strContent = arguments[0];
			intStatus = 200;
		}
		if (!objOptions) objOptions = {"Content-Type": conf.DefaultContentType};
		if (strContent === undefined) strContent = "";
		else if ( typeof strContent !== "string") {
			objOptions["Content-Type"] = "application/json";
			strContent = JSON.stringify(strContent);
		}
		response.writeHead(intStatus, objOptions);
		response.write(strContent, "binary");
		response.end();		
	};

	var serveFile = function(filename) {
		// console.log("accessing:", filename);
		var strMime = mime.lookup(filename) || conf.DefaultContentType;
		fs.readFile(filename, "binary", function(err, file) {
			if (err) {
				writeEntireResponse(500, err + "\n", {"Content-Type": "text/plain"});
				return;
			}

			writeEntireResponse(200, file, {"Content-Type": strMime});
		});
		return true;
	};

	fs.exists(filename, function(exists) {
		if (!exists) {
			writeEntireResponse(404, "404 Not Found\n"+uri, {"Content-Type": "text/plain"});<commit_msg>Adding “callback” support.<commit_after>#!/usr/bin/env node
// 
// Based on rpflorence's Gist @ https://gist.github.com/rpflorence/701407
// 

var http = require("http"),
	url  = require("url"),
	path = require("path"),
	fs   = require("fs"),
	conf = require("./conf/server.conf.js"),
	di   = require("./extensions/directory-indexing.js"),
	mime = require("./lib/node-mime/mime.js"),
	port = process.argv[2] || 8888;

// The base level of the server, nothing allowed below this.
// Do not end this in a "/". If you want the server's root, set this to empty-string: ""
// http.DOCUMENT_ROOT = "/Users/blake/Source";

http.createServer(function(request, response) {
	var objUrl = url.parse(request.url, true),
		uri = path.normalize(objUrl.pathname),
		filename = path.join(conf.DocumentRoot, uri);
	// process.cwd()

	// console.log("process.cwd()", process.cwd(), "request.url", request.url, "uri", uri);
	
	var writeEntireResponse = function(intStatus, strContent, objOptions) {
		if (arguments.length == 1) {
			strContent = arguments[0];
			intStatus = 200;
		}
		if (!objOptions) objOptions = {"Content-Type": conf.DefaultContentType};
		if (strContent === undefined) strContent = "";
		else if ( typeof strContent !== "string") {
			objOptions["Content-Type"] = "application/json";
			strContent = JSON.stringify(strContent);
			if (objUrl.query["callback"]) {
				objOptions["Content-Type"] = "application/javascript";
				strContent = objUrl.query["callback"] + "(" + strContent + ");";
			}
		}
		response.writeHead(intStatus, objOptions);
		response.write(strContent, "binary");
		response.end();		
	};

	var serveFile = function(filename) {
		// console.log("accessing:", filename);
		var strMime = mime.lookup(filename) || conf.DefaultContentType;
		fs.readFile(filename, "binary", function(err, file) {
			if (err) {
				writeEntireResponse(500, err + "\n", {"Content-Type": "text/plain"});
				return;
			}

			writeEntireResponse(200, file, {"Content-Type": strMime});
		});
		return true;
	};

	fs.exists(filename, function(exists) {
		if (!exists) {
			writeEntireResponse(404, "404 Not Found\n"+uri, {"Content-Type": "text/plain"});
{'fraction_non_alphanumeric': 0.18047121690551374, 'fraction_numerical': 0.01360213747874666, 'mean_word_length': 4.121890547263682, 'pattern_counts': {'":': 8, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1391050', 'n_tokens_mistral': 1513, 'n_tokens_neox': 1347, 'n_words': 294}