<commit_before>'use strict';

'use strict';

var http = require('http'),
    https = require('https');

function faviconApp(req, res) {
    if (req.url === '/favicon.ico') {
        res.writeHead(204);
        res.end();
        return;
    }
    console.log('GET', req.url, req.headers.origin || '',
        req.connection.remoteAddress || '', req.headers['x-forwarded-for'] || '');
    var domain = req.url.substr(1);
    if (domain.indexOf('.') < 0 || domain.indexOf('/') >= 0) {
        res.writeHead(404, {'Content-Type': 'text/plain'});
        return res.end('Usage: GET /domain.com');
    }
    loadResource('http://' + domain + '/favicon.ico', res);
}

function loadResource(url, res, redirectNum) {
    var proto = url.lastIndexOf('https', 0) === 0 ? https : http;
    var serverReq = proto.get(url, function(srvRes) {
        if (srvRes.statusCode > 300 && srvRes.statusCode < 400 && srvRes.headers.location) {
            if (redirectNum > 3) {
                res.writeHead(404, {'Content-Type': 'text/plain'});
                return res.end('Too many redirects');
            }
            return loadResource(srvRes.headers.location, res, (redirectNum || 0) + 1);
        } else if (srvRes.statusCode === 200) {
            res.writeHead(200, {'Content-Type': 'image/x-icon', 'Access-Control-Allow-Origin': '*'});
            srvRes.pipe(res, {end: true});
        } else {
            res.writeHead(404, {'Content-Type': 'text/plain'});
            return res.end('Status ' + srvRes.statusCode);
        }
    });
    serverReq.on('error', function(e) {
        res.writeHead(404, {'Content-Type': 'text/plain'});
        return res.end(e.message);
    });
    serverReq.end();
}

module.exports = faviconApp;
<commit_msg>index<commit_after>'use strict';

'use strict';

var http = require('http'),
    https = require('https');

function faviconApp(req, res) {
    if (req.url === '/favicon.ico') {
        res.writeHead(204);
        res.end();
        return;
    }
    if (req.url === '/') {
        res.writeHead(200, {'Content-Type': 'text/plain'});
        res.end('This is a service for loading website favicon with CORS\n\n' +
            'Usage: GET /domain.com\n' +
            'Questions, source code: https://github.com/antelle/favicon-proxy');
        return;
    }
    console.log('GET', req.url, req.headers.origin || '',
        req.connection.remoteAddress || '', req.headers['x-forwarded-for'] || '');
    var domain = req.url.substr(1);
    if (domain.indexOf('.') < 0 || domain.indexOf('/') >= 0) {
        res.writeHead(404, {'Content-Type': 'text/plain'});
        return res.end('Usage: GET /domain.com');
    }
    loadResource('http://' + domain + '/favicon.ico', res);
}

function loadResource(url, res, redirectNum) {
    var proto = url.lastIndexOf('https', 0) === 0 ? https : http;
    var serverReq = proto.get(url, function(srvRes) {
        if (srvRes.statusCode > 300 && srvRes.statusCode < 400 && srvRes.headers.location) {
            if (redirectNum > 3) {
                res.writeHead(404, {'Content-Type': 'text/plain'});
                return res.end('Too many redirects');
            }
            return loadResource(srvRes.headers.location, res, (redirectNum || 0) + 1);
        } else if (srvRes.statusCode === 200) {
            res.writeHead(200, {'Content-Type': 'image/x-icon', 'Access-Control-Allow-Origin': '*'});
            srvRes.pipe(res, {end: true});
        } else {
            res.writeHead(404, {'Content-Type': 'text/plain'});
            return res.end('Status ' + srvRes.statusCode);
        }
    });
    serverReq.on('error', function(e) {
        res.writeHead(404, {'Content-Type': 'text/plain'});
        return res.end(e.message);
    });
    serverReq.end();
}

module.exports = faviconApp;

{'fraction_non_alphanumeric': 0.18751653001851362, 'fraction_numerical': 0.01930706162390902, 'mean_word_length': 2.453881278538813, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 9, 'https://': 1, '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': '611783', 'n_tokens_mistral': 1279, 'n_tokens_neox': 1210, 'n_words': 237}