<commit_before>var padManager = require('../../db/PadManager');
var url = require('url');

exports.expressCreateServer = function (hook_name, args, cb) {

  // redirects browser to the pad's sanitized url if needed. otherwise, renders the html
  args.app.param('pad', function (req, res, next, padId) {
    // ensure the padname is valid and the url doesn't end with a /
    if (!padManager.isValidPadId(padId) || /\/$/.test(req.url)) {
      res.status(404).send('Such a padname is forbidden');
      return;
    }

    padManager.sanitizePadId(padId, function(sanitizedPadId) {
      if (sanitizedPadId === padId) {
        // the pad id was fine, so just render it
        next();
      } else {
        // the pad id was sanitized, so we redirect to the sanitized version
        var real_url = sanitizedPadId;
        real_url = encodeURIComponent(real_url);
        var query = url.parse(req.url).query;
        if ( query ) real_url += '?' + query;
        res.header('Location', real_url);
        res.status(302).send('You should be redirected to <a href="' + real_url + '">' + real_url + '</a>');
      }
    });
  });
}<commit_msg>padurlsanitize.js: rewritten to consume promises<commit_after>var padManager = require('../../db/PadManager');
var url = require('url');

exports.expressCreateServer = function (hook_name, args, cb) {

  // redirects browser to the pad's sanitized url if needed. otherwise, renders the html
  args.app.param('pad', async function (req, res, next, padId) {
    // ensure the padname is valid and the url doesn't end with a /
    if (!padManager.isValidPadId(padId) || /\/$/.test(req.url)) {
      res.status(404).send('Such a padname is forbidden');
      return;
    }

    let sanitizedPadId = await padManager.sanitizePadId(padId);

    if (sanitizedPadId === padId) {
      // the pad id was fine, so just render it
      next();
    } else {
      // the pad id was sanitized, so we redirect to the sanitized version
      var real_url = sanitizedPadId;
      real_url = encodeURIComponent(real_url);
      var query = url.parse(req.url).query;
      if ( query ) real_url += '?' + query;
      res.header('Location', real_url);
      res.status(302).send('You should be redirected to <a href="' + real_url + '">' + real_url + '</a>');
    }
  });
}
{'fraction_non_alphanumeric': 0.1420479302832244, 'fraction_numerical': 0.00522875816993464, 'mean_word_length': 2.8849407783417935, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, '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': '5551018', 'n_tokens_mistral': 740, 'n_tokens_neox': 715, 'n_words': 213}