<commit_before>var express = require('express'),
    http = require('http'),
    path = require('path'),
    phantom = require('phantom'),
    config = require('./config.json');

var app = module.exports = express();

app.set('port', process.env.PORT || 80);
app.use(express.logger('dev'));
app.use(express.methodOverride());
app.use(app.router);

if (app.get('env') === 'development') {
    app.use(express.errorHandler());
};

app.get('/download', function (req, res) {

    var params = req.query;

    if (!params.targetUrl) {
        res.status(400);
        res.json({message: 'Missing targetUrl!'});
        return;
    }

    var secretIsValid = (params.secret == config.secret);
    var hostIsAllowed = (config.allowedHosts.indexOf("*") >= 0 || config.allowedHosts.indexOf(req.host) >= 0); //TODO: Validate using regex
    var targetIsAllowed = (config.allowedTarges.indexOf("*") >= 0 || config.alloedTargets.indexOf(params.targetUrl) >= 0); //TODO: Validate using regex

    if (!secretIsValid || !hostIsAllowed || !targetIsAllowed ) {
        res.status(403);
        res.json({message: 'Not allowed!'});
        return;
    }

    var filename = params.filename || new Date().toISOString().split('T')[0] + '.pdf';
    var targetUrl = params.targetUrl;

    phantom.create(function (phantomClient) {

        phantomClient.createPage(function (page) {
            page.open(targetUrl, function (status) {
                page.render(filename, function () {
                    res.download(filename);
                });
            });
        });

    });

});

http.createServer(app).listen(app.get('port'), function () {
    console.log('Express server listening on port ' + app.get('port'));
});<commit_msg>handle 404<commit_after>var express = require('express'),
    http = require('http'),
    path = require('path'),
    phantom = require('phantom'),
    config = require('./config.json');

var app = module.exports = express();

app.set('port', process.env.PORT || 80);
app.use(express.logger('dev'));
app.use(express.methodOverride());
app.use(app.router);

if (app.get('env') === 'development') {
    app.use(express.errorHandler());
}

app.get('/download', function (req, res) {

    var params = req.query;

    if (!params.targetUrl) {
        res.status(400);
        res.json({message: 'Missing targetUrl!'});
        return;
    }

    var secretIsValid = (params.secret == config.secret);
    var hostIsAllowed = (config.allowedHosts.indexOf("*") >= 0 || config.allowedHosts.indexOf(req.host) >= 0); //TODO: Validate using regex
    var targetIsAllowed = (config.allowedTarges.indexOf("*") >= 0 || config.alloedTargets.indexOf(params.targetUrl) >= 0); //TODO: Validate using regex

    if (!secretIsValid || !hostIsAllowed || !targetIsAllowed ) {
        res.status(403);
        res.json({message: 'Not allowed!'});
        return;
    }

    var filename = params.filename || new Date().toISOString().split('T')[0] + '.pdf';
    var targetUrl = params.targetUrl;

    phantom.create(function (phantomClient) {

        phantomClient.createPage(function (page) {
            page.open(targetUrl, function (status) {
                page.render(filename, function () {
                    res.download(filename);
                });
            });
        });

    });

});

app.get('*', function(req, res){
    res.send('Not found', 404);
});

http.createServer(app).listen(app.get('port'), function () {
    console.log('Express server listening on port ' + app.get('port'));
});
{'fraction_non_alphanumeric': 0.18080865603644647, 'fraction_numerical': 0.009111617312072893, 'mean_word_length': 3.028669724770642, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 11, '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': '4563223', 'n_tokens_mistral': 1097, 'n_tokens_neox': 1042, 'n_words': 205}