<commit_before>var Link = require('mongoose').model('Link');
var url = require('url');
var Linkstats = require('mongoose').model('LinkStatistic');
var statistic = require('../../app/controllers/statistic.server.controller');

exports.create = function(req, res, next) {
    var link = new Link(req.body);
    var LinkSuccess = {
        shortLink: String,
        shortURL: String,
        shortQR: String,
        longLink: String,
        longURL: String
    };
    console.log(link);

    LinkSuccess.shortLink = '/' + link.shortlink;
    LinkSuccess.shortURL = GLOBAL_SERVER + '/' + link.shortlink;
    LinkSuccess.shortQR = GLOBAL_SERVER + '/qr/' + link.shortlink;
    LinkSuccess.longLink = url.parse(link.longlink).hostname;
    LinkSuccess.longURL = link.longlink;
    //Check if cookie is set
    //Check if url is valide url

    link.save(function(err) {
        if (err) {
            return next(err);
        } else {
            res.json(LinkSuccess);
            res.end();
            //init statistic
            statistic.initStatistics(link.shortlink);
            statistic.initQRStatistics(link.shortlink);

        }
    });
};

exports.list = function(req, res, next) {
    Link.find({}, function(err, links) {
        if (err) {
            return next(err);
        } else {
            res.json(links);
        }
    });
};

exports.text = function(req, res) {
    console.log(req.body);
}

exports.redirect = function(req, res) {
    //Longlink has to save in standardformat to make this redirect correct
    statistic.updateStatistic(req.link.shortlink, false);
    res.redirect(req.link.longlink)
};

exports.redirectQR = function(req, res) {
    //Longlink has to save in standardformat to make this redirect correct
    statistic.updateStatistic(req.link.shortlink, true);
    res.redirect("http://" + req.link.longlink);
    //console.log("test");
};

exports.linkByShort = function(req, res, next, slink) {
    Link.findOne({
            shortlink: slink
        },
        function(err, link) {
            if (err) {
                return next(err);
            } else {
                req.link = link;
                next();
            }
        }
    );
};



exports.update = function(req, res, next) {
    Link.findByIdAndUpdate(req.link.id, req.body, function(err, link) {
        if (err) {
            return next(err);
        } else {
            res.json(link);
        }
    });
};

exports.validateURL = function(req, res, next) {
    var link = new Link(req.body);
    var LinkError = {
        shortlink: String,
        longlink: String,
        error: String
    };
    if (validateUrl(link.longlink) == false) {
        if (validateUrl("http://" + link.longlink) == true) {
            link.longlink = "http://" + link.longlink;
            req.body.longlink = link.longlink;

            next();
        } else {
            //Throw Error
            LinkError.shortlink = link.shortlink;
            LinkError.longlink = link.longlink;
            LinkError.error = 'URL malformatted';

            res.json(LinkError);
            res.end();
        }
    }
};

function randomText() {
    var text = "";
    var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";

    for (var i = 0; i < 5; i++)
        text += possible.charAt(Math.floor(Math.random() * possible.length));

    return text;
}

function validateUrl(value) {
    return /^(https?|ftp):\/\/(((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:)*@)?(((\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5]))|((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?)(:\d*)?)(\/((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)+(\/(([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)*)*)?)?(\?((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)|[\uE000-\uF8FF]|\/|\?)*)?(\#((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)|\/|\?)*)?$/i.test(value);
}
<commit_msg>Change error message for invalid URL on server side to be the same as client side<commit_after>var Link = require('mongoose').model('Link');
var url = require('url');
var Linkstats = require('mongoose').model('LinkStatistic');
var statistic = require('../../app/controllers/statistic.server.controller');

exports.create = function(req, res, next) {
    var link = new Link(req.body);
    var LinkSuccess = {
        shortLink: String,
        shortURL: String,
        shortQR: String,
        longLink: String,
        longURL: String
    };
    console.log(link);

    LinkSuccess.shortLink = '/' + link.shortlink;
    LinkSuccess.shortURL = GLOBAL_SERVER + '/' + link.shortlink;
    LinkSuccess.shortQR = GLOBAL_SERVER + '/qr/' + link.shortlink;
    LinkSuccess.longLink = url.parse(link.longlink).hostname;
    LinkSuccess.longURL = link.longlink;
    //Check if cookie is set
    //Check if url is valide url

    link.save(function(err) {
        if (err) {
            return next(err);
        } else {
            res.json(LinkSuccess);
            res.end();
            //init statistic
            statistic.initStatistics(link.shortlink);
            statistic.initQRStatistics(link.shortlink);

        }
    });
};

exports.list = function(req, res, next) {
    Link.find({}, function(err, links) {
        if (err) {
            return next(err);
        } else {
            res.json(links);
        }
    });
};

exports.text = function(req, res) {
    console.log(req.body);
}

exports.redirect = function(req, res) {
    //Longlink has to save in standardformat to make this redirect correct
    statistic.updateStatistic(req.link.shortlink, false);
    res.redirect(req.link.longlink)
};

exports.redirectQR = function(req, res) {
    //Longlink has to save in standardformat to make this redirect correct
    statistic.updateStatistic(req.link.shortlink, true);
    res.redirect("http://" + req.link.longlink);
    //console.log("test");
};

exports.linkByShort = function(req, res, next, slink) {
    Link.findOne({
            shortlink: slink
        },
        function(err, link) {
            if (err) {
                return next(err);
            } else {
                req.link = link;
                next();
            }
        }
    );
};



exports.update = function(req, res, next) {
    Link.findByIdAndUpdate(req.link.id, req.body, function(err, link) {
        if (err) {
            return next(err);
        } else {
            res.json(link);
        }
    });
};

exports.validateURL = function(req, res, next) {
    var link = new Link(req.body);
    var LinkError = {
        shortlink: String,
        longlink: String,
        error: String
    };
    if (validateUrl(link.longlink) == false) {
        if (validateUrl("http://" + link.longlink) == true) {
            link.longlink = "http://" + link.longlink;
            req.body.longlink = link.longlink;

            next();
        } else {
            //Throw Error
            LinkError.shortlink = link.shortlink;
            LinkError.longlink = link.longlink;
            LinkError.error = 'Unable to shorten that link. It is not a valid url.';

            res.json(LinkError);
            res.end();
        }
    }
};

function randomText() {
    var text = "";
    var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";

    for (var i = 0; i < 5; i++)
        text += possible.charAt(Math.floor(Math.random() * possible.length));

    return text;
}

function validateUrl(value) {
    return /^(https?|ftp):\/\/(((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:)*@)?(((\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5]))|((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?)(:\d*)?)(\/((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)+(\/(([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)*)*)?)?(\?((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)|[\uE000-\uF8FF]|\/|\?)*)?(\#((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)|\/|\?)*)?$/i.test(value);
}

{'fraction_non_alphanumeric': 0.24022167750186507, 'fraction_numerical': 0.035169988276670575, 'mean_word_length': 2.3865030674846626, 'pattern_counts': {'":': 0, '<': 5, '<?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': '2642540', 'n_tokens_mistral': 4004, 'n_tokens_neox': 3782, 'n_words': 460}