From 5ba2922524785654139fbbd2a63047e6b33de1ec Mon Sep 17 00:00:00 2001 From: dakkar Date: Tue, 5 Apr 2011 20:54:21 +0100 Subject: cgi --- cgi/shorten.pl | 44 ++++++++++++++++++++++++++++++-------------- confs/apache | 4 +++- 2 files changed, 33 insertions(+), 15 deletions(-) diff --git a/cgi/shorten.pl b/cgi/shorten.pl index 514dba2..b3b4ff8 100644 --- a/cgi/shorten.pl +++ b/cgi/shorten.pl @@ -1,12 +1,16 @@ #!/usr/bin/perl use strict; use warnings; +use FindBin; +use 5.12.0; use DBI; use open ':std',':utf8'; use Encode; -use 5.12.0; +use CGI '-debug'; -my $db=DBI->connect('dbi:SQLite:dbname=urls.db','','',{ +my $basedir=$FindBin::Bin.'/..'; + +my $db=DBI->connect("dbi:SQLite:dbname=${basedir}/urls.db",'','',{ PrintError => 0, RaiseError => 1, AutoCommit => 1, @@ -99,19 +103,31 @@ sub urldecode { return pack 'U0C*',@bytes; } -if (@ARGV>1) { - my $str=$ARGV[1]; - if ($str =~ m{^%}) { - $str=urldecode($str); +my $q=CGI->new(); +my $path=$q->path_info()||''; +my $url=$q->param('url'); + +if ($url) { + my $str=shorten($url); + say $q->header(-type=>'text/plain'), + 'http://',$q->virtual_host,'/',$str; +} +else { + if ($path =~ m{^%}) { + $path=urldecode($path); } else { - $str=decode('utf8',$str); + $path=decode('utf8',$path); + } + my ($short,$rest) = ($path =~ m{^/(\w+)(.*)$}); + my $url=lengthen($short); + if ($url eq '404') { + say $q->header(-type=>'text/plain'); + } + else { + say $q->redirect( + -uri=>$url.$rest, + -status=>301, + ); } - say lengthen($str); -} -else { - my $url=$ARGV[0]; - my $str=shorten($url); - say $str; - say urlencode($str); } diff --git a/confs/apache b/confs/apache index 956fd31..b3c3e4b 100644 --- a/confs/apache +++ b/confs/apache @@ -21,7 +21,9 @@ RewriteCond %{HTTPS} on RewriteRule ^/$ /usr/local/shorten/htdocs/form.html [L] - RewriteRule ^/(.*)$ /usr/local/shorten/cgi/shorten.pl/$1 [H=cgi-script,L] + RewriteRule ^/add /usr/local/shorten/htdocs/not_there [L] + + RewriteRule ^/(.*)$ /usr/local/shorten/cgi/shorten.pl/$1 [H=cgi-script,B,L] -- cgit v1.2.3