<commit_before>var
    Promise = require('bluebird'),
    fs = require('fs'),
    http = require('http'),
    https = require('https'),
	path = require('path'),
    express = require('express'),
    session = require('express-session'),
    morgan = require('morgan'),
    bodyParser = require('body-parser'),
    lessMiddleware = require('less-middleware'),
    request = require('request'),
    uuid = require('node-uuid'),

    console = require('clim')(),

    config = (require('../../../config').cn || { }),
    httpConfig = (config ? config.http : { });

if (config.auth) {
    var maybeAddSlashTo = (s) => s.slice(-1) == '/' ? s : s + '/';
    config.auth.path = maybeAddSlashTo(config.auth.path);

    if (!config.auth.secret) config.auth.secret = uuid.v4();

    if (typeof config.auth.cacheMinutes == 'number') {
        config.auth.cacheMinutes = {
            good: config.auth.cacheMinutes,
            bad: config.auth.cacheMinutes
        };
    }

    if (!config.auth.cacheMinutes.good) config.auth.cacheMinutes.good = 1;
    if (!config.auth.cacheMinutes.bad)  config.auth.cacheMinutes.bad  = 1;
}

http.globalAgent.maxSockets = 1024;

(function() {
    'use strict';

    var HttpHandler = function(controller, port, securePort, creds) {
        this.controller = controller;
        this.port = port;
        this.securePort = securePort;
        this.creds = creds;
    }

    HttpHandler.prototype.start = function(creds) {
        var app = express();

        app.use(morgan('dev'));
        app.use(bodyParser.json());
        app.use(bodyParser.urlencoded({ extended: true }));

        app.use(function(req, res, next) {
            if (config.auth) {
                res.header(
                    'Access-Control-Allow-Headers',
                    'Content-Type, Authorization');
                res.header(
                    'Access-Control-Allow-Methods',
                    'GET, OPTIONS');
            }

            Object.keys(httpConfig.headers).map(function(key) {
                res.header(key, httpConfig.headers[key]);
            });
            res.header('X-powered-by', 'Hobu, Inc.');
            next();
        });

        if (httpConfig.enableStaticServe) registerStatic(app);
        registerCommands(this.controller, app);

        if (this.port) {
            if (!config.auth) {
                http.createServer(app).listen(this.port);
                console.log('HTTP server running on port', this.port);
            }
            else {
                console.log('HTTP server disabled due to auth specification');
            }
        }

        if (this.securePort) {
            https.createServer(this.creds, app).listen(this.securePort);
            console.log('HTTPS server running on port', this.securePort);
        }
    }

    var registerStatic = function(app) {
        app.set('views', __dirname + '/static/views');
        app.set('view engine', 'jade');

        var publicDir = '/static/public';
        app.use(lessMiddleware(path.join(__dirname, publicDir)));
        app.use(express.static(__dirname + publicDir));

        app.get('/ws/:resourceId', function(req, res) {
            res.render('wsView');
        });

        app.get('/http/:resourceId', function(req, res) {
            res.render('httpView');
        });
    };

    var registerCommands = function(controller, app) {
        if (config.auth) {
            console.log('Proxying auth requests to', config.auth.path);
            var auths = { };

            app.use(session({
                cookie: {
                    path: '/',
                    domain: config.auth.domain,
                    httpOnly: true,
                    secure: true,
                    maxAge: 1000 * 60 * 60 * 24 * 365,
                },
                genid: () => uuid.v4(),
                name: 'greyhound-user',
                resave: false,
                rolling: false,
                saveUninitialized: false,
                secret: config.auth.secret,
                unset: 'keep'
            }));

            app.use(function(req, res, next) {
                if (!req.session.greyhoundId) {
                    req.session.greyhoundId = uuid.v4();
                }

                next();
            });

            app.options('*', function(req, res) {
                res.status(200).end();
            });

            app.use('/resource/:resource(*)/:call(info|read)',
                    function(req, res, next)
            {
                var id = req.session.greyhoundId;
                var resource = req.params.resource;

                if (!auths[id]) auths[id] = { };

                if (!auths[id][resource]) {
                    console.log('Authing', id);
                    var start = new Date();

                    auths[id][resource] = new Promise((resolve, reject) => {
                        var authPath = config.auth.path + resource;
                        req.pipe(request(authPath, (err, authResponse) => {
                            if (err) return reject(500);

                            var code = authResponse.statusCode;
                            var ok = Math.floor(code / 100) == 2;

                            var time = (new Date() - start) / 1000;

                            console.log('Authed', id, 'in', time, 's');

                            var timeoutMinutes = ok ?
                                config.auth.cacheMinutes.good :
                                config.auth.cacheMinutes.bad;

                            setTimeout(() => {
                                delete auths[id][resource];
                                if (!Object.keys(auths[id]).length) {
                                    delete auths[id];<commit_msg>Disable rejectUnauthorized for backend HTTPS calls.<commit_after>var
    Promise = require('bluebird'),
    fs = require('fs'),
    http = require('http'),
    https = require('https'),
	path = require('path'),
    express = require('express'),
    session = require('express-session'),
    morgan = require('morgan'),
    bodyParser = require('body-parser'),
    lessMiddleware = require('less-middleware'),
    request = require('request'),
    uuid = require('node-uuid'),

    console = require('clim')(),

    config = (require('../../../config').cn || { }),
    httpConfig = (config ? config.http : { });

if (config.auth) {
    var maybeAddSlashTo = (s) => s.slice(-1) == '/' ? s : s + '/';
    config.auth.path = maybeAddSlashTo(config.auth.path);

    if (!config.auth.secret) config.auth.secret = uuid.v4();

    if (typeof config.auth.cacheMinutes == 'number') {
        config.auth.cacheMinutes = {
            good: config.auth.cacheMinutes,
            bad: config.auth.cacheMinutes
        };
    }

    if (!config.auth.cacheMinutes.good) config.auth.cacheMinutes.good = 1;
    if (!config.auth.cacheMinutes.bad)  config.auth.cacheMinutes.bad  = 1;
}

http.globalAgent.maxSockets = 1024;

(function() {
    'use strict';

    var HttpHandler = function(controller, port, securePort, creds) {
        this.controller = controller;
        this.port = port;
        this.securePort = securePort;
        this.creds = creds;
    }

    HttpHandler.prototype.start = function(creds) {
        var app = express();

        app.use(morgan('dev'));
        app.use(bodyParser.json());
        app.use(bodyParser.urlencoded({ extended: true }));

        app.use(function(req, res, next) {
            if (config.auth) {
                res.header(
                    'Access-Control-Allow-Headers',
                    'Content-Type, Authorization');
                res.header(
                    'Access-Control-Allow-Methods',
                    'GET, OPTIONS');
            }

            Object.keys(httpConfig.headers).map(function(key) {
                res.header(key, httpConfig.headers[key]);
            });
            res.header('X-powered-by', 'Hobu, Inc.');
            next();
        });

        if (httpConfig.enableStaticServe) registerStatic(app);
        registerCommands(this.controller, app);

        if (this.port) {
            if (!config.auth) {
                http.createServer(app).listen(this.port);
                console.log('HTTP server running on port', this.port);
            }
            else {
                console.log('HTTP server disabled due to auth specification');
            }
        }

        if (this.securePort) {
            https.createServer(this.creds, app).listen(this.securePort);
            console.log('HTTPS server running on port', this.securePort);
        }
    }

    var registerStatic = function(app) {
        app.set('views', __dirname + '/static/views');
        app.set('view engine', 'jade');

        var publicDir = '/static/public';
        app.use(lessMiddleware(path.join(__dirname, publicDir)));
        app.use(express.static(__dirname + publicDir));

        app.get('/ws/:resourceId', function(req, res) {
            res.render('wsView');
        });

        app.get('/http/:resourceId', function(req, res) {
            res.render('httpView');
        });
    };

    var registerCommands = function(controller, app) {
        if (config.auth) {
            console.log('Proxying auth requests to', config.auth.path);
            var auths = { };

            app.use(session({
                cookie: {
                    path: '/',
                    domain: config.auth.domain,
                    httpOnly: true,
                    secure: true,
                    maxAge: 1000 * 60 * 60 * 24 * 365,
                },
                genid: () => uuid.v4(),
                name: 'greyhound-user',
                resave: false,
                rolling: false,
                saveUninitialized: false,
                secret: config.auth.secret,
                unset: 'keep'
            }));

            app.use(function(req, res, next) {
                if (!req.session.greyhoundId) {
                    req.session.greyhoundId = uuid.v4();
                }

                next();
            });

            app.options('*', function(req, res) {
                res.status(200).end();
            });

            app.use('/resource/:resource(*)/:call(info|read)',
                    function(req, res, next)
            {
                var id = req.session.greyhoundId;
                var resource = req.params.resource;

                if (!auths[id]) auths[id] = { };

                if (!auths[id][resource]) {
                    console.log('Authing', id);
                    var start = new Date();

                    auths[id][resource] = new Promise((resolve, reject) => {
                        var options = {
                            url: config.auth.path + resource,
                            rejectUnauthorized: false
                        };

                        req.pipe(request(options, (err, authResponse) => {
                            if (err) {
                                console.log('Auth proxy err:', err);
                                return reject(500);
                            }

                            var code = authResponse.statusCode;
                            var ok = Math.floor(code / 100) == 2;

                            var time = (new Date() - start) / 1000;

                            console.log('Authed', id, 'in', time, 's');

                            var timeoutMinutes = ok ?
                                config.auth.cacheMinutes.good :
                                config.auth.cacheMinutes.bad;

                            setTimeout(() => {
                                delete auths[id][resource];
                                if (!Object.keys(auths[id]).length) {
                                    delete auths[id];
{'fraction_non_alphanumeric': 0.13697934304097528, 'fraction_numerical': 0.006264815441923467, 'mean_word_length': 1.4346661170651278, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2961402', 'n_tokens_mistral': 3294, 'n_tokens_neox': 3099, 'n_words': 593}