<commit_before>var util = require('../util/util');
var hosts = require('./hosts');
var http = require('http');
var https = require('https');
var url = require('url');
var EventEmitter = require('events').EventEmitter;
var Transform = require('stream').Transform;
var transformHttps = require('../https/transform.js');
var config = require('../package.json');

function transform(chunk, encoding, callback) {
	var data = {
			encoding: encoding,
			data: chunk
	};
	var self = this;
	
	self.emit('data', data);
	
	function execCallback() {
		self._setResponseTimeout && self._setResponseTimeout();
		callback(data.error, data.data);
	}
	
	self._clearResponseTimeout && self._clearResponseTimeout();
	
	if (data.delay > 0) {
		setTimeout(execCallback, data.delay);
	} else {
		execCallback();
	}
}

function getTransform(eventEmitter) {
	var ts = new Transform();
	ts._transform = transform.bind(eventEmitter);
	return ts;
}

function getPort(options) {
	if (options.port) {
		return options.port;
	}
	
	if (options.protocol == 'https:') {
		return 443;
	}
	
	if (options.protocol == 'http:') {
		return 80;
	}
}

function getUIPort(options) {
	
	return options.protocol == 'https:' ? config.uisslport : config.uiport;
}

var RESET_COOKIE_RE = /secure;?|domain=([^;]+);?/ig;

function setWhistleHeaders(res, req, isHttp, isHttps) {
	var headers = res.headers;
	var options = req.options;
	var host = options.host.split(':')[0];
	options.host = host + (options.port ? ':' + options.port : '');
	
	headers['x-remote-ip'] = options.hosts[1] || '127.0.0.1';
	
	var matcher = options.rule && options.rule.matcher;
	if (matcher) {
		headers['x-' + config.name + '-rule'] = matcher;
	}
	
	if (matcher || isHttps) {
		headers['x-remote-url'] = isHttp ? url.format(options) : options.url;
	}
	
	if (!isHttps) {
		return;
	}
	
	var cookies = headers['set-cookie'];
	
	if (!Array.isArray(cookies)) {
		return;
	}
	
	var host = options.host.split('.');
	var len = host.length;
	if (host.length > 2) {
		host = host.slice(len - 2);
	}
	host = '.' + host.join('.');
	
	headers['set-cookie'] = cookies.map(function(cookie) {
		return cookie.replace(RESET_COOKIE_RE, '') + '; DOMAIN=' + host + ';';
	});
}

function getOptions(fullUrl) {
	var options = url.parse(fullUrl);
	options.hosts = ['127.0.0.1', '127.0.0.1'];
	options.url = fullUrl;
}

module.exports = function(req, res, next) {
	var self = this;
	var fullUrl = req.fullUrl = util.getFullUrl(req);
	var timeoutId, clientReq;
	
	function setResponseTimeout() {
		clearResponseTimeout();
		timeoutId = setTimeout(abort, config.timeout);
	}
	
	function clearResponseTimeout() {
		clearTimeout(timeoutId);
		timeoutId = null;
	}
	
	function abort() {
		clearResponseTimeout();
		if (clientReq) {
			clientReq.abort();
		}
		res.destroy();
	}
	
	setResponseTimeout();
	
	function handleResolve(err, options) {
		req.options = options;
		var request = new EventEmitter();
		var response = new EventEmitter();<commit_msg>options<commit_after>var util = require('../util/util');
var hosts = require('./hosts');
var http = require('http');
var https = require('https');
var url = require('url');
var EventEmitter = require('events').EventEmitter;
var Transform = require('stream').Transform;
var transformHttps = require('../https/transform.js');
var config = require('../package.json');

function transform(chunk, encoding, callback) {
	var data = {
			encoding: encoding,
			data: chunk
	};
	var self = this;
	
	self.emit('data', data);
	
	function execCallback() {
		self._setResponseTimeout && self._setResponseTimeout();
		callback(data.error, data.data);
	}
	
	self._clearResponseTimeout && self._clearResponseTimeout();
	
	if (data.delay > 0) {
		setTimeout(execCallback, data.delay);
	} else {
		execCallback();
	}
}

function getTransform(eventEmitter) {
	var ts = new Transform();
	ts._transform = transform.bind(eventEmitter);
	return ts;
}

function getPort(options) {
	if (options.port) {
		return options.port;
	}
	
	if (options.protocol == 'https:') {
		return 443;
	}
	
	if (options.protocol == 'http:') {
		return 80;
	}
}

function getUIPort(options) {
	
	return options.protocol == 'https:' ? config.uisslport : config.uiport;
}

var RESET_COOKIE_RE = /secure;?|domain=([^;]+);?/ig;

function setWhistleHeaders(res, req, isHttp, isHttps) {
	var headers = res.headers;
	var options = req.options;
	var host = options.host.split(':')[0];
	options.host = host + (options.port ? ':' + options.port : '');
	
	headers['x-remote-ip'] = options.hosts[1] || '127.0.0.1';
	
	var matcher = options.rule && options.rule.matcher;
	if (matcher) {
		headers['x-' + config.name + '-rule'] = matcher;
	}
	
	if (matcher || isHttps) {
		headers['x-remote-url'] = isHttp ? url.format(options) : options.url;
	}
	
	if (!isHttps) {
		return;
	}
	
	var cookies = headers['set-cookie'];
	
	if (!Array.isArray(cookies)) {
		return;
	}
	
	var host = options.host.split('.');
	var len = host.length;
	if (host.length > 2) {
		host = host.slice(len - 2);
	}
	host = '.' + host.join('.');
	
	headers['set-cookie'] = cookies.map(function(cookie) {
		return cookie.replace(RESET_COOKIE_RE, '') + '; DOMAIN=' + host + ';';
	});
}

function getOptions(fullUrl) {
	var options = url.parse(fullUrl);
	options.hosts = ['127.0.0.1', '127.0.0.1'];
	options.url = fullUrl;
	return options;
}

module.exports = function(req, res, next) {
	var self = this;
	var fullUrl = req.fullUrl = util.getFullUrl(req);
	var timeoutId, clientReq;
	
	function setResponseTimeout() {
		clearResponseTimeout();
		timeoutId = setTimeout(abort, config.timeout);
	}
	
	function clearResponseTimeout() {
		clearTimeout(timeoutId);
		timeoutId = null;
	}
	
	function abort() {
		clearResponseTimeout();
		if (clientReq) {
			clientReq.abort();
		}
		res.destroy();
	}
	
	setResponseTimeout();
	
	function handleResolve(err, options) {
		req.options = options;
		var request = new EventEmitter();
		var response = new EventEmitter();
{'fraction_non_alphanumeric': 0.1730541955090326, 'fraction_numerical': 0.009454668242444708, 'mean_word_length': 4.037414965986395, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 6, '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': '6761891', 'n_tokens_mistral': 2194, 'n_tokens_neox': 2037, 'n_words': 433}