<commit_before>package ShortyApp::Controller;
use Mojo::Base 'Mojolicious::Controller';
use ShortyApp::Couch;
use Mojo::Util qw(url_escape);

sub welcome {
  my $self = shift;

  $self->render();
}

sub generate {
	my $self = shift;

	my $url 	= $self->param('url')  || '';
	my $name 	= $self->param('name') || '';
	my $db 		= _create_dsn($self);

	# sanitise name
	$name =~ s![^-0-9a-zA-Z]!!g;

	# generate random string if name is empty or looks like an important file
	if ($name eq '') {
    	$name = _random_string(4);
	} elsif ($name =~ m{^(?:robots|htaccess|index|home)$}i) {
		$name = _random_string(4);
	}

	# return if URL is blank or doesn't look like a valid URL
	if (($url eq '') || ($url !~ m{^\w\w+\.\w\w[^<>]*$}i)) {
		$self->render( db_response => 'fail', url => $url, name => $name );
	}

	# ensure URL starts with http/s or ftp
	if ($url !~ m{^[hf]t?tps?://}i) {
		$url = 'http://' . $url;
	}

	my $result = couch_insert($db, $url, $name);

	if ($result->{'ok'}) {
		$self->render( db_response => 'ok', url => $url, name => $name );
	} else {
		$self->render( db_response => 'fail', url => $url, name => $name );
	}
	
}

# ajax function to check if shortcode is available
sub checkname {
	my $self = shift;
	my $name = $self->param('name');
	my $db = _create_dsn($self);
	my $status;
	my $result = couch_exists($db, $name);
	if ($result == 404) {
		$status = 'ok';
	} elsif ($result == 200) {
		$status = 'taken';
	}
	$self->render( text => $status );
}

# route shortcodes
sub route {
	my $self = shift;
	my $db = _create_dsn($self);
	my $router = couch_fetch($db, $self->stash('name'));
	my $redirect = $router->{url};
	if (defined $redirect) {
		$self->res->code(301);
		$self->redirect_to($redirect);<commit_msg>Fixed error in basic URL validation regex making full URLs entered appear to fail<commit_after>package ShortyApp::Controller;
use Mojo::Base 'Mojolicious::Controller';
use ShortyApp::Couch;
use Mojo::Util qw(url_escape);

sub welcome {
  my $self = shift;

  $self->render();
}

sub generate {
	my $self = shift;

	my $url 	= $self->param('url')  || '';
	my $name 	= $self->param('name') || '';
	my $db 		= _create_dsn($self);

	# sanitise name
	$name =~ s![^-0-9a-zA-Z]!!g;

	# generate random string if name is empty or looks like an important file
	if ($name eq '') {
    	$name = _random_string(4);
	} elsif ($name =~ m{^(?:robots|htaccess|index|home)$}i) {
		$name = _random_string(4);
	}

	# return if URL is blank or doesn't look like a valid URL
	if (($url eq '') || ($url !~ m{^(?:[hf]t?tps?://)?\w+\.\w[^<>]*$}i)) {
		$self->render( db_response => 'fail', url => $url, name => $name );
	}

	# ensure URL starts with http/s or ftp
	if ($url !~ m{^[hf]t?tps?://}i) {
		$url = 'http://' . $url;
	}

	my $result = couch_insert($db, $url, $name);

	if (defined($result->{'ok'})) {
		$self->render( db_response => 'ok', url => $url, name => $name );
	} else {
		$self->render( db_response => 'fail', url => $url, name => $name );
	}
	
}

# ajax function to check if shortcode is available
sub checkname {
	my $self = shift;
	my $name = $self->param('name');
	my $db = _create_dsn($self);
	my $status;
	my $result = couch_exists($db, $name);
	if ($result == 404) {
		$status = 'ok';
	} elsif ($result == 200) {
		$status = 'taken';
	}
	$self->render( text => $status );
}

# route shortcodes
sub route {
	my $self = shift;
	my $db = _create_dsn($self);
	my $router = couch_fetch($db, $self->stash('name'));
	my $redirect = $router->{url};
	if (defined $redirect) {
		$self->res->code(301);
		$self->redirect_to($redirect);
{'fraction_non_alphanumeric': 0.21691486372576566, 'fraction_numerical': 0.007305422871593144, 'mean_word_length': 3.823848238482385, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 53, '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': '1976185', 'n_tokens_mistral': 1447, 'n_tokens_neox': 1381, 'n_words': 364}