<commit_before>/*jslint forin:true sub:true anon:true, sloppy:true, stupid:true nomen:true, node:true continue:true*/

/*
 * Copyright (c) 2012, Yahoo! Inc.  All rights reserved.
 * Copyrights licensed under the New BSD License.
 * See the accompanying LICENSE file for terms.
 */

var http = require('http'),
    log4js = require("log4js"),
    fs = require('fs'),
    path = require('path'),
    portchecker = require("../../ext-lib/portchecker"),
    url = require('url');

var coverage = require("../util/coverage");
var zlib = require('zlib');

function ProxyManager(proxyConfig, config) {
    this.config = config || {};
    this.proxyConfig = proxyConfig || {};
    this.logger = log4js.getLogger("ProxyManager");
    if (global.workingDirectory) {
        this.fileName = path.resolve(global.workingDirectory, "proxy.log");
    } else {
        this.fileName = "proxy.log";
    }
    global.proxyManager = {};
    global.proxyManager.record = [];
}

/**
 *
 * @param router - proxy configuration defined in json file
 * @param request - request
 * @return {Object} - Options
 */
ProxyManager.prototype.getOptions = function (router, request) {
    var reqHost = request.headers['host'],
        onlyhost,
        reqUrl = request.url,
        reqHeaders = request.headers,
        port = 80,
        options,
        objUrl,
        hd,
        i,
        self = this;

    /*
     host could be www.yahoo.com or localhost:8080,check both and
     remove port info for non-80 host,this would support route localhost:8080 -> localhost:8081
     */
    onlyhost = (reqHost && reqHost.match(/:/g) ) ? reqHost.slice(0, reqHost.indexOf(":")) : reqHost;

    // reqest host is localhost:8080,then define "localhost:8080" or "localhost" would be all OK
    if (router[reqHost] || router[onlyhost]) {
        router[reqHost] = router[reqHost] || router[onlyhost];
        hd = router[reqHost].headers;
        if (hd) {
            for (i = 0; i < hd.length; i += 1) {
                reqHeaders[hd[i].param] = hd[i].value;
            }
        }

        if (router[reqHost].record) {

            var obj = [];

            if (global.proxyManager.record[self.proxyConfig]) {
                obj = global.proxyManager.record[self.proxyConfig];
                obj.push({"url": reqUrl, "headers": request.headers, "method": request.method});
                global.proxyManager.record[self.proxyConfig] = obj;
            } else {
                obj.push({"url": reqUrl, "headers": request.headers, "method": request.method});
                global.proxyManager.record[self.proxyConfig] = obj;
            }

        }

        if (router[reqHost].newHost) {
            reqUrl = reqUrl.replace(reqHost, router[reqHost].newHost);
            reqHost = router[reqHost].newHost;
        }
    }

    reqHeaders['reqUrl'] = reqUrl; // not standard in headers

    objUrl = url.parse(reqUrl);

    port = (reqHost && reqHost.match(/:/g) ) ? reqHost.slice(reqHost.indexOf(":") + 1, reqHost.length) : port;
    reqHost = (reqHost && reqHost.match(/:/g) ) ? reqHost.slice(0, reqHost.indexOf(":")) : reqHost;

    reqHeaders['protocol'] = objUrl.protocol || 'http'; // not standard in headers

    options = {
        host: reqHost,
        port: objUrl.port || port,
        path: objUrl.path || "/",
        method: request.method,
        protocol: objUrl.protocol || 'http',
        headers: reqHeaders
    };
    return options;

};
<commit_msg>fix ut<commit_after>/*jslint forin:true sub:true anon:true, sloppy:true, stupid:true nomen:true, node:true continue:true*/

/*
 * Copyright (c) 2012, Yahoo! Inc.  All rights reserved.
 * Copyrights licensed under the New BSD License.
 * See the accompanying LICENSE file for terms.
 */

var http = require('http'),
    log4js = require("log4js"),
    fs = require('fs'),
    path = require('path'),
    portchecker = require("../../ext-lib/portchecker"),
    url = require('url');

var coverage = require("../util/coverage");
var zlib = require('zlib');

function ProxyManager(proxyConfig, config) {
    this.config = config || {};
    this.proxyConfig = proxyConfig || {};
    this.logger = log4js.getLogger("ProxyManager");
    if (global.workingDirectory) {
        this.fileName = path.resolve(global.workingDirectory, "proxy.log");
    } else {
        this.fileName = "proxy.log";
    }
    global.proxyManager = {};
    global.proxyManager.record = [];
}

/**
 *
 * @param router - proxy configuration defined in json file
 * @param request - request
 * @return {Object} - Options
 */
ProxyManager.prototype.getOptions = function (router, request) {
    var reqHost = request.headers['host'],
        onlyhost,
        reqUrl = request.url,
        reqHeaders = request.headers,
        port = 80,
        options,
        objUrl,
        hd,
        i,
        self = this;

    /*
     host could be www.yahoo.com or localhost:8080,check both and
     remove port info for non-80 host,this would support route localhost:8080 -> localhost:8081
     */
    onlyhost = (reqHost && reqHost.match(/:/g) ) ? reqHost.slice(0, reqHost.indexOf(":")) : reqHost;

    // reqest host is localhost:8080,then define "localhost:8080" or "localhost" would be all OK
    if (router[reqHost] || router[onlyhost]) {
        router[reqHost] = router[reqHost] || router[onlyhost];
        hd = router[reqHost].headers;
        if (hd) {
            for (i = 0; i < hd.length; i += 1) {
                reqHeaders[hd[i].param] = hd[i].value;
            }
        }

        if (router[reqHost].record) {

            var obj = [];

            if (global.proxyManager.record[self.proxyConfig]) {
                obj = global.proxyManager.record[self.proxyConfig];
                obj.push({"url": reqUrl, "headers": request.headers, "method": request.method});
                global.proxyManager.record[self.proxyConfig] = obj;
            } else {
                obj.push({"url": reqUrl, "headers": request.headers, "method": request.method});
                global.proxyManager.record[self.proxyConfig] = obj;
            }

        }

        if (router[reqHost].newHost) {
            reqUrl = reqUrl.replace(reqHost, router[reqHost].newHost);
            reqHost = router[reqHost].newHost;
        }
    }

    reqHeaders['reqUrl'] = reqUrl; // not standard in headers

    objUrl = url.parse(reqUrl);

    port = (reqHost && reqHost.match(/:/g) ) ? reqHost.slice(reqHost.indexOf(":") + 1, reqHost.length) : port;
    reqHost = (reqHost && reqHost.match(/:/g) ) ? reqHost.slice(0, reqHost.indexOf(":")) : reqHost;

    reqHeaders['protocol'] = objUrl.protocol || 'http:'; // not standard in headers

    options = {
        host: reqHost,
        port: objUrl.port || port,
        path: objUrl.path || "/",
        method: request.method,
        headers: reqHeaders
    };
    return options;

};

{'fraction_non_alphanumeric': 0.14793158894898406, 'fraction_numerical': 0.01052477707937436, 'mean_word_length': 2.453811206461383, 'pattern_counts': {'":': 18, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '6782681', 'n_tokens_mistral': 2127, 'n_tokens_neox': 2022, 'n_words': 470}