summaryrefslogtreecommitdiff
path: root/cgi
diff options
context:
space:
mode:
Diffstat (limited to 'cgi')
-rwxr-xr-xcgi/shorten.pl56
1 files changed, 35 insertions, 21 deletions
diff --git a/cgi/shorten.pl b/cgi/shorten.pl
index 064d618..0177d0f 100755
--- a/cgi/shorten.pl
+++ b/cgi/shorten.pl
@@ -27,13 +27,16 @@ if (!defined $dbversion) {
$db->do(q{insert into metainfo(version) values(1)});
}
+my $shortpart_rx;
{
# these could be multiple ranges
my @ranges=(0x4e00,0x9fcb);
my $base=0;
for(my $i=0;$i<@ranges;$i+=2) {
$base+=$ranges[$i+1]-$ranges[$i];
+ $shortpart_rx.=sprintf("\\x{%04x}-\\x{%04x}",$ranges[$i],$ranges[$i+1]);
}
+$shortpart_rx=qr{[$shortpart_rx]+};
sub num2chars {
my ($number) = @_;
@@ -98,40 +101,51 @@ sub urlencode {
sub urldecode {
my ($str) = @_;
- my @pieces = ($str =~ m{%(..)}g);
- my @bytes = map { oct("0x$_") } @pieces;
- return pack 'U0C*',@bytes;
+ $str =~ s{%(..)}{chr(oct("0x$1"))}ge;
+ return $str;
}
my $q=CGI->new();
-my $path=$q->path_info()||'';
+my $path=$q->path_info;
+if ($ENV{QUERY_STRING}) { $path.='?'.$ENV{QUERY_STRING} }
$path=~s{^/}{};
my $url=$q->param('url');
+my $method=$q->request_method;
-if ($url) {
- my $str=shorten($url);
- say $q->header(
- -type => 'text/plain',
- -charset => 'utf-8',
- ),
- 'http://',$q->virtual_host,'/',$str;
+if ($method eq 'POST' && $path eq 'add' && $url) {
+
+ if ($url !~ m{^\w+://}) {
+ say $q->header(-type=>'text/plain');
+ }
+ else {
+ my $str=shorten($url);
+ say $q->header(
+ -type => 'text/plain',
+ -charset => 'utf-8',
+ ),
+ 'http://',$q->virtual_host,'/',$str;
+ }
}
else {
if ($path =~ m{^%}) {
$path=urldecode($path);
}
- else {
- $path=decode('utf8',$path);
- }
- my ($short,$rest) = ($path =~ m{^(\w+)(.*)$});
- my $url=lengthen($short);
- if ($url eq '404') {
+ $path=decode('utf8',$path);
+ my ($short,$rest) = ($path =~ m{^($shortpart_rx)(.*)$});
+
+ if (!$short) {
say $q->header(-type=>'text/plain');
}
else {
- say $q->redirect(
- -uri=>$url.$rest,
- -status=>301,
- );
+ my $url=lengthen($short);
+ if ($url eq '404') {
+ say $q->header(-type=>'text/plain');
+ }
+ else {
+ say $q->redirect(
+ -uri=>$url.($rest||''),
+ -status=>301,
+ );
+ }
}
}