<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");

function ProxyManager(routerConfig) {
    this.routerJsonPath = routerConfig;
    this.logger = log4js.getLogger("ProxyManager");
    this.fileName = path.resolve(global.workingDirectory, "proxy.log");
}

ProxyManager.prototype.runRouterProxy = function(minPort, maxPort, host, callback) {
    var options, router, proxy, proxy_request,
        self = this;
    this.logger.info("Starting Proxy");
    try {

        if (self.routerJsonPath) {
            router = JSON.parse(fs.readFileSync(self.routerJsonPath, "utf-8"));
        } else {
            router = {};
        }

        this.logger.info("Proxy Router Config :" + JSON.stringify(router));

        portchecker.getFirstAvailable(minPort, maxPort, host, function(p, host) {
            if (p === -1) {
               // console.log('No free ports found for GhostDriver on ' + host + ' between ' + minPort + ' and ' + minPort);
                callback("Error : No free ports found for Proxy Server on " + host + " between " + minPort + " and " + minPort);
            } else {

                self.proxyServer = http.createServer(function (request, response) {
                    var host = request.headers['host'],
                        url;
                    try {
                        url = request.yiv_get_url('url');
                    } catch (e) {
                        url = request.url;
                    }

                    if (router[host]) {
                        url = url.replace(host, router[host]);
                        host = router[host];
                    }

                    proxy = http.createClient(80, host);
                    proxy_request = proxy.request(request.method, url, request.headers);

                    proxy_request.addListener('response', function (proxy_response) {
                        proxy_response.addListener('data', function(chunk) {
                            response.write(chunk, 'binary');
                        });
                        proxy_response.addListener('end', function() {
                            response.end();
                        });

                        self.writeLog(url + ":" + proxy_response.statusCode);
                        response.writeHead(proxy_response.statusCode, proxy_response.headers);
                    });

                    request.addListener('data', function(chunk) {
                        proxy_request.write(chunk, 'binary');
                    });

                    request.addListener('end', function() {
                        proxy_request.end();
                    });

                    proxy.on('error', function (err, req, res) {
                        self.logger.warn("Proxy Error, Request URL : " + request.url);
                        response.writeHead(500, {
                            'Content-Type': 'text/plain'
                        });
                        response.end('Something went wrong: ' + JSON.stringify(err));
                    });

                    proxy.on('proxyError', function (err, req, res) {
                        self.logger.warn("Proxy proxyError, Request URL : " + request.url);
                        response.writeHead(500, {
                            'Content-Type': 'text/plain'
                        });
                        response.end('Something went wrong: ' + JSON.stringify(err));
                    });
                }).listen(p);<commit_msg>http.createClient to http.request<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");

function ProxyManager(routerConfig) {
    this.routerJsonPath = routerConfig;
    this.logger = log4js.getLogger("ProxyManager");
    this.fileName = path.resolve(global.workingDirectory, "proxy.log");
}

ProxyManager.prototype.runRouterProxy = function(minPort, maxPort, host, callback) {
    var options, router, proxy, proxy_request,
        self = this;
    this.logger.info("Starting Proxy");
    try {

        if (self.routerJsonPath) {
            router = JSON.parse(fs.readFileSync(self.routerJsonPath, "utf-8"));
        } else {
            router = {};
        }

        this.logger.info("Proxy Router Config :" + JSON.stringify(router));

        portchecker.getFirstAvailable(minPort, maxPort, host, function(p, host) {
            if (p === -1) {
                callback("Error : No free ports found for Proxy Server on " + host + " between " + minPort + " and " + minPort);
            } else {

                self.proxyServer = http.createServer(function (request, response) {
                    var reqHost = request.headers['host'],
                        reqUrl = request.url,
                        port = 80;

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

                    var objUrl = url.parse(reqUrl);
                    if(objUrl.port) {
                        try {
                            if ( port != undefined ) {
                                port = objUrl.port;
                            }
                        } catch (e) {
                            port = 80;
                        }
                    }

                    var options = {
                        host: reqHost,
                        port: port,
                        path: reqUrl,
                        method: request.method,
                        headers: request.headers
                    };

                    proxy_request = http.request(options, function(proxy_response) {

                        proxy_response.addListener('data', function(chunk) {
                            response.write(chunk, 'binary');
                        });

                        proxy_response.addListener('end', function() {
                            response.end();
                        });

                        self.writeLog(port + ":" + reqUrl + ":" + proxy_response.statusCode);
                        response.writeHead(proxy_response.statusCode, proxy_response.headers);

                    });

                    request.addListener('data', function(chunk) {
                        proxy_request.write(chunk, 'binary');
                    });

                    request.addListener('end', function() {
                        proxy_request.end();
                    });

                    proxy_request.on('error', function (err, req, res) {
                        self.logger.warn("Proxy Error, Request URL : " + request.url);
                        response.writeHead(500, {
                            'Content-Type': 'text/plain'
                        });
                        response.end('Something went wrong: ' + JSON.stringify(err));
                    });


                }).listen(p);
{'fraction_non_alphanumeric': 0.11621551275377408, 'fraction_numerical': 0.004294638209266007, 'mean_word_length': 1.3741118319431573, 'pattern_counts': {'":': 3, '<': 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': '1650026', 'n_tokens_mistral': 1942, 'n_tokens_neox': 1803, 'n_words': 382}