<commit_before>/** Http.js
 *  Author: <NAME>
 *
 *  Wrappers for nodeJs Http objects
 */

module.exports = (function() {
	var $o = require("./core.js");
	var EventEmitter = require("events").EventEmitter;
	var errno = require("./ErrNo.js").errno
    var http = require("http");


	/* An http Error can be constructed with a nodeJs Error object as the first argument
	 	or an other HttpError as argument
	 	or an string as text 
	 	or an httpstatus code , (opt) text and (opt) extra text */
	var HttpError = $o.Object.extend({
		extra: "",
		code: 500, // http status code
		text: null,
		create: function(err, errtxt, extra) {
			if(err instanceof HttpError) {
				this.extra = err.extra;
				this.text = err.text;
				this.code = err.code;
            } else if(err instanceof Error) {
				var errDescr = HttpError.translateErrNo(err.errno);

				this.code = errDescr && errDescr.http ? errDescr.http : 500;
				
				this.extra = errDescr ? errDescr.description : err.toString();
			} else if(arguments.length === 2 && typeof(errtxt) === "string") {
				this.code = err;
				this.extra = errtxt;
			} else if(arguments.length === 3) {
				this.code = err;
				this.errtxt = errtxt;
				this.extra = extra;
			} else if(typeof(err) === "string") {
				this.extra = err;
			} else if(arguments.length === 1 && typeof(err) === "number") {
                this.code = err;
            }

			if(!this.text) this.text = HttpError.httpStatusText(this.code);
		},
        // Returns the Http status code, text and extra 
		httpStatus: function() {
			return { 
				code: this.code, 
				text: this.text,
				extra: this.extra
			};
		},
        // Sends the Error the the client bound by the given HttpContext
		send: function(ctx) {
			console.log("[Error:" + this.code + "]: " + this.extra);

			var st = this.httpStatus();
			ctx.response.status(st.code);

			ctx.response.data("<h1>" + st.code + " " + st.text + "</h1>");
			if(st.extra) ctx.response.append(st.extra);

			ctx.response.send();
		}
	});
	HttpError.translateErrNo = function(no) { return errno[no]; };
	HttpError.httpStatusText = function(no) {
        return http.STATUS_CODES[no]
	};

    // Wrapper class for the HTTP response
    var HttpResponse = $o.Object.extend({
        headers: null,
        statusCode: 200,
        eos: false, // stream has ended
        _res: null,
        _data: null,
        _isBinary: false, // Whenever data is a binary buffer
        create: function(res) {
            this._res = res;
            this.headers = { 
                "Content-Type": "text/html", 
                "Connection"  : "close",<commit_msg>Data within HttpResponse defaults to a empty string<commit_after>/** Http.js
 *  Author: <NAME>
 *
 *  Wrappers for nodeJs Http objects
 */

module.exports = (function() {
	var $o = require("./core.js");
	var EventEmitter = require("events").EventEmitter;
	var errno = require("./ErrNo.js").errno
    var http = require("http");


	/* An http Error can be constructed with a nodeJs Error object as the first argument
	 	or an other HttpError as argument
	 	or an string as text 
	 	or an httpstatus code , (opt) text and (opt) extra text */
	var HttpError = $o.Object.extend({
		extra: "",
		code: 500, // http status code
		text: null,
		create: function(err, errtxt, extra) {
			if(err instanceof HttpError) {
				this.extra = err.extra;
				this.text = err.text;
				this.code = err.code;
            } else if(err instanceof Error) {
				var errDescr = HttpError.translateErrNo(err.errno);

				this.code = errDescr && errDescr.http ? errDescr.http : 500;
				
				this.extra = errDescr ? errDescr.description : err.toString();
			} else if(arguments.length === 2 && typeof(errtxt) === "string") {
				this.code = err;
				this.extra = errtxt;
			} else if(arguments.length === 3) {
				this.code = err;
				this.errtxt = errtxt;
				this.extra = extra;
			} else if(typeof(err) === "string") {
				this.extra = err;
			} else if(arguments.length === 1 && typeof(err) === "number") {
                this.code = err;
            }

			if(!this.text) this.text = HttpError.httpStatusText(this.code);
		},
        // Returns the Http status code, text and extra 
		httpStatus: function() {
			return { 
				code: this.code, 
				text: this.text,
				extra: this.extra
			};
		},
        // Sends the Error the the client bound by the given HttpContext
		send: function(ctx) {
			console.log("[Error:" + this.code + "]: " + this.extra);

			var st = this.httpStatus();
			ctx.response.status(st.code);

			ctx.response.data("<h1>" + st.code + " " + st.text + "</h1>");
			if(st.extra) ctx.response.append(st.extra);

			ctx.response.send();
		}
	});
	HttpError.translateErrNo = function(no) { return errno[no]; };
	HttpError.httpStatusText = function(no) {
        return http.STATUS_CODES[no]
	};

    // Wrapper class for the HTTP response
    var HttpResponse = $o.Object.extend({
        headers: null,
        statusCode: 200,
        eos: false, // stream has ended
        _res: null,
        _data: "",
        _isBinary: false, // Whenever data is a binary buffer
        create: function(res) {
            this._res = res;
            this.headers = { 
                "Content-Type": "text/html", 
                "Connection"  : "close",
{'fraction_non_alphanumeric': 0.14356247626281807, 'fraction_numerical': 0.005317128750474744, 'mean_word_length': 2.4742744063324538, 'pattern_counts': {'":': 2, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, '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': '2504839', 'n_tokens_mistral': 1894, 'n_tokens_neox': 1663, 'n_words': 451}