<commit_before>'use strict';

let proxy = require('http-proxy').createProxyServer({});

let APP_PTN = new RegExp('^(?:https?:\/\/)?([a-zA-Z0-9-]+)\.'
        + process.env.SERVER_NAME.replace('.', '\\.') + '$');

proxy.on('proxyRes', function(proxyRes, req, res) {
    if (req.headers.host && req.headers.origin
            && req.headers.host.match(APP_PTN) && req.headers.origin.match(APP_PTN)) {
        console.log('CORS: ' + req.headers.origin + ' -> ' + req.headers.host);
        res.setHeader('Access-Control-Allow-Credentials', 'true');
        res.setHeader('Access-Control-Allow-Origin', req.headers.origin);
    }
});

const getApp = function(req) {
    let host = req.headers.host;
    if (host && host.match(APP_PTN)) {
        return host.match(APP_PTN)[1];
    }
};

const getTarget = function(req, proto) {
    let app = getApp(req);
    if (app) {
        let addr = process.env[app.toUpperCase() + '_PORT_80_TCP_ADDR'];
        if (addr) {
            let target = proto + '://' + addr;
            console.log('PROXY: ' + proto + '://' + req.headers.host + req.url + ' -> ' + target);
            return {
                target: target,
            };
        }
    }
};

const response = function(res, code, message) {
    res.writeHead(code, { 'Content-Type': 'text/plain; charset=utf-8' });
    res.write(message);
    res.end();
};

const onError = function(req, res, sock) {
    return function(e) {
        if (res) {
            response(res, 500, 'Internal Server Error');
        } else if (sock) {
            sock.end();
        }

        if (req) {
            req.socket.end();
        }

        console.error(e);
    };
};

const forwardUser = function(req) {
    delete req.headers['username'];
    delete req.headers['Username'];
    delete req.headers['USERNAME'];
    delete req.headers['Remote-User'];
    delete req.headers['REDIRECT_REMOTE_USER'];
    delete req.headers['X-Forwarded-User'];

    if (!req.user) return;

    req.headers['WWW-Auth-Type'] = 'digest';
    req.headers['Username'] = 
        req.headers['Remote-User'] = 
        req.headers['Redirect-Remote-User'] = 
        req.headers['X-Forwarded-User'] = req.user.userid;
};

module.exports.web = function(req, res, next) {
    let target = getTarget(req, 'http');
    if (!target) {
        return next();
    }

    req.headers['X-Forwarded-From'] = req.socket.remoteAddress;

    let app = getApp(req);
    if (app) {
        let url = req.url;
        let ptn = process.env[app.toUpperCase() + '_PUBLIC_URL'];
        if (ptn && url && url.match(new RegExp(ptn))) {
            return proxy.web(req, res, target, onError(req, res));
        }
    }

    if (!req.user) {
        req.session.redirectTo = 'http://' + req.hostname + req.url;
        return res.redirect('http://' + (process.env.SERVER_NAME || 'localhost') + '/auth/twitter');
    }

    forwardUser(req);

    proxy.web(req, res, target, onError(req, res));
};

module.exports.ws = function(req, sock, head) {<commit_msg>Allow localhost<commit_after>'use strict';

let proxy = require('http-proxy').createProxyServer({});

let APP_PTN = new RegExp('^(?:https?:\/\/)?([a-zA-Z0-9-]+)\.'
        + process.env.SERVER_NAME.replace('.', '\\.') + '$');

proxy.on('proxyRes', function(proxyRes, req, res) {
    if (req.headers.host && req.headers.origin
            && req.headers.host.match(APP_PTN) && 
            (req.headers.origin.match(APP_PTN) || req.headers.origin.match(/^https?:\/\/localhost(:[0-9]+)?$/))) {
        console.log('CORS: ' + req.headers.origin + ' -> ' + req.headers.host);
        res.setHeader('Access-Control-Allow-Credentials', 'true');
        res.setHeader('Access-Control-Allow-Origin', req.headers.origin);
    }
});

const getApp = function(req) {
    let host = req.headers.host;
    if (host && host.match(APP_PTN)) {
        return host.match(APP_PTN)[1];
    }
};

const getTarget = function(req, proto) {
    let app = getApp(req);
    if (app) {
        let addr = process.env[app.toUpperCase() + '_PORT_80_TCP_ADDR'];
        if (addr) {
            let target = proto + '://' + addr;
            console.log('PROXY: ' + proto + '://' + req.headers.host + req.url + ' -> ' + target);
            return {
                target: target,
            };
        }
    }
};

const response = function(res, code, message) {
    res.writeHead(code, { 'Content-Type': 'text/plain; charset=utf-8' });
    res.write(message);
    res.end();
};

const onError = function(req, res, sock) {
    return function(e) {
        if (res) {
            response(res, 500, 'Internal Server Error');
        } else if (sock) {
            sock.end();
        }

        if (req) {
            req.socket.end();
        }

        console.error(e);
    };
};

const forwardUser = function(req) {
    delete req.headers['username'];
    delete req.headers['Username'];
    delete req.headers['USERNAME'];
    delete req.headers['Remote-User'];
    delete req.headers['REDIRECT_REMOTE_USER'];
    delete req.headers['X-Forwarded-User'];

    if (!req.user) return;

    req.headers['WWW-Auth-Type'] = 'digest';
    req.headers['Username'] = 
        req.headers['Remote-User'] = 
        req.headers['Redirect-Remote-User'] = 
        req.headers['X-Forwarded-User'] = req.user.userid;
};

module.exports.web = function(req, res, next) {
    let target = getTarget(req, 'http');
    if (!target) {
        return next();
    }

    req.headers['X-Forwarded-From'] = req.socket.remoteAddress;

    let app = getApp(req);
    if (app) {
        let url = req.url;
        let ptn = process.env[app.toUpperCase() + '_PUBLIC_URL'];
        if (ptn && url && url.match(new RegExp(ptn))) {
            return proxy.web(req, res, target, onError(req, res));
        }
    }

    if (!req.user) {
        req.session.redirectTo = 'http://' + req.hostname + req.url;
        console.log('AUTH: ' + req.session.redirectTo + ' -> /auth/twitter');
        return res.redirect('http://' + (process.env.SERVER_NAME || 'localhost') + '/auth/twitter');
    }

    forwardUser(req);

    proxy.web(req, res, target, onError(req, res));
};

module.exports.ws = function(req, sock, head) {
{'fraction_non_alphanumeric': 0.18686131386861313, 'fraction_numerical': 0.0032441200324412004, 'mean_word_length': 2.4160664819944597, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 8, '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': '3985235', 'n_tokens_mistral': 2073, 'n_tokens_neox': 2029, 'n_words': 365}