<commit_before>'use strict';

var path = process.cwd();
var ClickHandler = require(path + '/app/controllers/clickHandler.server.js');
//var timeHandler = require(path + '/app/controllers/timekHandler.server.js');

var baseURL = "https://url-shortener-richardsa.c9users.io/";
var isObj = true;
var urls = {};
var id = 0; 
var answer;
var notIn = {"error":"No short url found for given input"};
module.exports = function(app, passport) {

  /*	function isLoggedIn (req, res, next) {
  		if (req.isAuthenticated()) {
  			return next();
  		} else {
  			res.redirect('/timestamp');
  		}
  	}*/


  var clickHandler = new ClickHandler();

  app.route('/')
    .get(function(req, res) {
    	res.sendFile(path + '/public/index.html');
    });
  app.route('/:id')
    .get(function(req, res) {
      var short = req.params.id;
      console.log(short);
      returnFull(short);
      console.log(answer);
      if (isObj){
        res.send(notIn); 
      } else {
        
        answer = hasHTTP(answer);
        console.log("redirecting to ..." + answer);
        res.redirect(answer);
      // req.url = "www.reddit.com";
        //res.send(answer);
        return res.end();
      }
    	
    });

app.route('/new/:url*')
  .get(function(req, res) {
    var query = req.params.url;
    console.log(query);
    // valid url reg ex taken from http://stackoverflow.com/questions/161738/what-is-the-best-regular-expression-to-check-if-a-string-is-a-valid-url
    if (/((([A-Za-z]{3,9}:(?:\/\/)?)(?:[\-;:&=\+\$,\w]+@)?[A-Za-z0-9\.\-]+|(?:www\.|[\-;:&=\+\$,\w]+@)[A-Za-z0-9\.\-]+)((?:\/[\+~%\/\.\w\-_]*)?\??(?:[\-\+=&;%@\.\w_]*)#?(?:[\.\!\/\\\w]*))?)/.test(query)){
      query = inSystem(query);
      var solution = {};
      solution["original_url"] = req.params.url;
      console.log(urls[query]);
      solution["short_url"] = urls[req.params.url];
      res.send(solution);
    } else {
      
     res.send({"error":"URL invalid"}); 
    }
      
    
  });
function returnFull (urlID){
 
  for (var key in urls) {
    console.log(urls[key]);
    console.log(urlID);
  if (urls[key] == baseURL + urlID) {
    console.log("urls key " + urls[key]);
    isObj = false;
    answer = key;
    console.log("answer" + answer);
    return answer
  }
  
  console.log("nope");
} return answer;
}

function hasHTTP(x){
  var http = "http://";
  var https = "https://";
  if (x.substring(0, 7) != http || x.substring(0, 8) != https){
    
    x = http + x;
  }
  console.log("final x " + x);
  return x; 
}
function inSystem (url){<commit_msg>allow forward slash in routes<commit_after>'use strict';

var path = process.cwd();
var ClickHandler = require(path + '/app/controllers/clickHandler.server.js');
//var timeHandler = require(path + '/app/controllers/timekHandler.server.js');

var baseURL = "https://url-shortener-richardsa.c9users.io/";
var isObj = true;
var urls = {};
var id = 0; 
var answer;
var notIn = {"error":"No short url found for given input"};
module.exports = function(app, passport) {

  /*	function isLoggedIn (req, res, next) {
  		if (req.isAuthenticated()) {
  			return next();
  		} else {
  			res.redirect('/timestamp');
  		}
  	}*/


  var clickHandler = new ClickHandler();

  app.route('/')
    .get(function(req, res) {
    	res.sendFile(path + '/public/index.html');
    });
  app.route('/:id') // '/:href(.+)'
    .get(function(req, res) {
      var short = req.params.id;
      console.log(short);
      returnFull(short);
      console.log(answer);
      if (isObj){
        res.send(notIn); 
      } else {
        
        answer = hasHTTP(answer);
        console.log("redirecting to ..." + answer);
        res.redirect(answer);
      // req.url = "www.reddit.com";
        //res.send(answer);
        return res.end();
      }
    	
    });

app.route('/new/:url(*)') // allow forward slashes in route: http://stackoverflow.com/a/24366031
  .get(function(req, res) {
    var query = req.params.url;
    console.log(query);
    // valid url reg ex taken from http://stackoverflow.com/questions/161738/what-is-the-best-regular-expression-to-check-if-a-string-is-a-valid-url
    if (/((([A-Za-z]{3,9}:(?:\/\/)?)(?:[\-;:&=\+\$,\w]+@)?[A-Za-z0-9\.\-]+|(?:www\.|[\-;:&=\+\$,\w]+@)[A-Za-z0-9\.\-]+)((?:\/[\+~%\/\.\w\-_]*)?\??(?:[\-\+=&;%@\.\w_]*)#?(?:[\.\!\/\\\w]*))?)/.test(query)){
      query = inSystem(query);
      var solution = {};
      solution["original_url"] = req.params.url;
      console.log(urls[query]);
      solution["short_url"] = urls[req.params.url];
      res.send(solution);
    } else {
      
     res.send({"error":"URL invalid"}); 
    }
      
    
  });
function returnFull (urlID){
 
  for (var key in urls) {
    console.log(urls[key]);
    console.log(urlID);
  if (urls[key] == baseURL + urlID) {
    console.log("urls key " + urls[key]);
    isObj = false;
    answer = key;
    console.log("answer" + answer);
    return answer
  }
  
  console.log("nope");
} return answer;
}

function hasHTTP(x){
  var http = "http://";
  var https = "https://";
  console.log(x.substring(0, 7))
  console.log(x.substring(0, 8))
  if (x.substring(0, 7) != http && x.substring(0, 8) != https){
    
    x = http + x;
  }
  console.log("final x " + x);
  return x; 
}
function inSystem (url){
{'fraction_non_alphanumeric': 0.22495706926159129, 'fraction_numerical': 0.009158557527189467, 'mean_word_length': 2.5299663299663298, 'pattern_counts': {'":': 4, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 4, 'lorem ipsum': 0, 'www.': 4, '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': '1733503', 'n_tokens_mistral': 1990, 'n_tokens_neox': 1933, 'n_words': 344}