Last active
August 29, 2015 14:08
-
-
Save nlitsme/3ebae0d5c9ce236559ba to your computer and use it in GitHub Desktop.
decode url from stdin
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/perl | |
# (C) 2003-2007 Willem Jan Hengeveld <[email protected]> | |
# Web: http://www.xs4all.nl/~itsme/ | |
# http://wiki.xda-developers.com/ | |
# | |
# $Id$ | |
# | |
use strict; | |
# this script decodes urls listed on stdin. | |
use CGI::Util qw(unescape escape); | |
use Getopt::Long; | |
sub usage { | |
return <<__EOF__ | |
Usage: urldecode [-e] | |
__EOF__ | |
} | |
my $do_encode; | |
GetOptions( | |
"e" => \$do_encode, | |
) or die usage(); | |
if ($do_encode) { | |
encodeurl(); | |
} | |
else { | |
decodeurl(); | |
} | |
sub encodeurl { | |
my $base; | |
my @params; | |
while (<>) { | |
s/\s+$//; | |
if (/^\S/) { | |
$base= $_; | |
} | |
elsif (/(\S+?)\s*=\s*(.*)/) { | |
my ($key, $value)= ($1, $2); | |
push @params, escape($key).'='.escape($value); | |
} | |
elsif (/^\s*(.*)/) { | |
push @params, escape($1); | |
} | |
} | |
printf("%s?%s\n", $base, join("&", @params)); | |
} | |
sub decodeurl { | |
while (<>) | |
{ | |
chomp; | |
my ($url, $query)= /(?:(.*?)\?)?(.*)/; | |
print $url, "\n" if ($url); | |
for my $keyval (split("&", $query)) | |
{ | |
if (my ($key, $value) = ($keyval =~ /(.*?)=(.*)/)) { | |
print "\t", unescape($key), " = ", unescape($value), "\n"; | |
} | |
else { | |
print "\t", unescape($keyval), "\n"; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment