<commit_before>var EventEmitter = require('events').EventEmitter;
var util = require('util');
var ltx = require('ltx');
var BOSH = require('./bosh');

function BOSHServer() {
    this.sessions = {};
}

util.inherits(BOSHServer, EventEmitter);
exports.BOSHServer = BOSHServer;

/**
 * *YOU* need to check the path before passing to this function.
 */
BOSHServer.prototype.handleHTTP = function(req, res) {
    var that = this;

    if (req.method === 'POST') {
	BOSH.parseBody(req, function(error, bodyEl) {
	    if (error || !bodyEl) {
		res.writeHead(400, { 'Content-Type': "text/plain" });
		res.end(error.message || error.stack || "Error");
		return;
	    }

	    var session;
	    if (bodyEl.attrs.sid) {
		session = that.sessions[bodyEl.attrs.sid];
		if (session) {
		    session.handleHTTP({ req: req, res: res, bodyEl: bodyEl });
		} else {
		    res.writeHead(404, { 'Content-Type': "text/plain" });
		    res.end("BOSH session not found");
		}
	    } else {
		/* No sid: create session */
		do {
		    session = new BOSHServerSession({ req: req, res: res, bodyEl: bodyEl });
		} while(that.sessions.hasOwnProperty(session.sid));
		that.sessions[session.sid] = session;
		/* Hook for destruction */
		session.on('close', function() {
		    delete that.sessions[session.sid];
		});
		that.emit('connect', session);
	    }
	});
    } else if (false && req.method === 'PROPFIND') {
	/* TODO */
    } else {
	res.writeHead(400);
	res.end();
    }<commit_msg>bosh_server: more safety<commit_after>var EventEmitter = require('events').EventEmitter;
var util = require('util');
var ltx = require('ltx');
var BOSH = require('./bosh');

function BOSHServer() {
    this.sessions = {};
}

util.inherits(BOSHServer, EventEmitter);
exports.BOSHServer = BOSHServer;

/**
 * *YOU* need to check the path before passing to this function.
 */
BOSHServer.prototype.handleHTTP = function(req, res) {
    var that = this;

    if (req.method === 'POST') {
	BOSH.parseBody(req, function(error, bodyEl) {
	    if (error || !bodyEl || !bodyEl.attrs) {
		res.writeHead(400, { 'Content-Type': "text/plain" });
		res.end(error.message || error.stack || "Error");
		return;
	    }

	    var session, sid = bodyEl.attrs.sid;
	    if (sid) {
		session = that.sessions[sid];
		if (session) {
		    session.handleHTTP({ req: req, res: res, bodyEl: bodyEl });
		} else {
		    res.writeHead(404, { 'Content-Type': "text/plain" });
		    res.end("BOSH session not found");
		}
	    } else {
		/* No sid: create session */
		do {
		    session = new BOSHServerSession({ req: req, res: res, bodyEl: bodyEl });
		} while(that.sessions.hasOwnProperty(session.sid));
		that.sessions[session.sid] = session;
		/* Hook for destruction */
		session.on('close', function() {
		    delete that.sessions[session.sid];
		});
		that.emit('connect', session);
	    }
	});
    } else if (false && req.method === 'PROPFIND') {
	/* TODO */
    } else {
	res.writeHead(400);
	res.end();
    }
{'fraction_non_alphanumeric': 0.1759322033898305, 'fraction_numerical': 0.006101694915254237, 'mean_word_length': 3.2830188679245285, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '1271751', 'n_tokens_mistral': 1094, 'n_tokens_neox': 1018, 'n_words': 227}