<commit_before>var express = require('express');
var expressWs = require('express-ws');
var net = require('net');
var crypto = require('crypto');

function generateToken() {
	return crypto.randomBytes(32).toString('hex');
}

function checkTo(allowed, requested) {
	if (!(allowed instanceof Array)) {
		allowed = [allowed];
	}

	// For each rule
	for (var i = 0; i < allowed.length; i++) {
		var to = allowed[i];

		if ((to.host == requested.host || !to.host) && (to.port == requested.port || !to.port)) {
			if (to.blacklist) { // This item is blacklisted
				return false;
			} else { // Otheriwse, it's whitelisted
				return true;
			}
		}
	}

	// No rule found, access denied
	return false;
}

module.exports = function (server, options) {
	options = options || {};

	var app = express();

	var sockets = {};

	if (options.allowOrigin) {
		app.use(function (req, res, next) {
			var allowOrigin = options.allowOrigin;
			if (typeof options.allowOrigin != 'string') {
				allowOrigin = (options.allowOrigin) ? '*' : '';
			}
			if (allowOrigin) {
				res.header('Access-Control-Allow-Origin', allowOrigin);
			}
			next();
		});
	}<commit_msg>Adds a better Access-Control headers (CORS) handling (adds OPTIONS request support)<commit_after>var express = require('express');
var expressWs = require('express-ws');
var net = require('net');
var crypto = require('crypto');

function generateToken() {
	return crypto.randomBytes(32).toString('hex');
}

function checkTo(allowed, requested) {
	if (!(allowed instanceof Array)) {
		allowed = [allowed];
	}

	// For each rule
	for (var i = 0; i < allowed.length; i++) {
		var to = allowed[i];

		if ((to.host == requested.host || !to.host) && (to.port == requested.port || !to.port)) {
			if (to.blacklist) { // This item is blacklisted
				return false;
			} else { // Otheriwse, it's whitelisted
				return true;
			}
		}
	}

	// No rule found, access denied
	return false;
}

module.exports = function (server, options) {
	options = options || {};

	var app = express();

	var sockets = {};

	if (options.allowOrigin) {
		var allowOrigin = options.allowOrigin;
		if (typeof options.allowOrigin != 'string') {
			allowOrigin = (options.allowOrigin === true) ? '*' : '';
		}

		if (allowOrigin) {
			// Set Access-Control headers (CORS)
			app.use(function (req, res, next) {
				if (req.path.indexOf('/api/vm/net/') !== 0) {
					next();
					return;
				}

				res.header('Access-Control-Allow-Origin', allowOrigin);

				if (req.method.toUpperCase() == 'OPTIONS') { // Preflighted requests
					res.header('Access-Control-Allow-Methods', 'GET, POST, OPTIONS');
					res.header('Access-Control-Allow-Headers', 'Content-Type');

					res.header('Access-Control-Max-Age', 1728000); // Access-Control headers cached for 20 days
				}
				next();
			});
		}
	}
{'fraction_non_alphanumeric': 0.17034925160370634, 'fraction_numerical': 0.005702066999287242, 'mean_word_length': 3.4065934065934065, 'pattern_counts': {'":': 0, '<': 5, '<?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': '6727110', 'n_tokens_mistral': 1039, 'n_tokens_neox': 896, 'n_words': 233}