#!/usr/local/bin/perl -w =head1 uri.cgi Create short URL re-directs, similar to www.makeashorterlink.com =head1 Usage When passed a valid redirect uri it sends the user to the appropriate link. When passed an http uri it creates a new redirect uri or returns existing one. uri.cgi?1062698960 uri.cgi?http://www.sfu.ca/~ajdelore/ =cut use strict; use CGI qw (header redirect url); my $path_to_data = "./.uri_data"; my $uri = $ENV{QUERY_STRING} or &bad_query_string; if ($uri =~ qr(^\d+$) || $uri =~ qr(^http://)) { open (URI_DATA,"+>> $path_to_data"); my %uri_data; while () { chomp; my ($key,$value) = split; $uri_data{$key} = $value; } if ($uri_data{$uri}) { print redirect($uri_data{$uri}); } elsif ($uri =~ qr(^http://)) { my %data_uri; foreach (keys %uri_data) { $data_uri{$uri_data{$_}} = $_; } my $key = ($data_uri{$uri} || time); print URI_DATA "$key $uri\n"; print header (-type=>'text/plain'); print "The link is ", url, "?$key" } else { &bad_query_string } close URI_DATA; } else { &bad_query_string } sub bad_query_string { print header (-type=>'text/plain'); print "Bad query string supplied.\n"; exit; }