#!/usr/local/bin/perl -pl

# URL simplifier http://www.cs.tu-berlin.de/~czyborra/~/bin/rurl
# see http://www.leo.org/mlists/www-de/199606/19960606.html
# and http://www.cs.tu-berlin.de/standards/rurl

$_ = $file if ($server, $file, $query) = m=^(.+://[^/?\#;]+)([^?\#;]*)(.*)=;

# URL decoding is dangerous because /A/B and /A%2EB may
# mean two different things (C:\A\B != C:\A/B)

s=%([0-9A-F]{2})=pack("H2",$1)=gie;
s=([^/A-Z0-9$-_.+!*''(),])=sprintf("%%%02X",ord($1))=gie;

# 1. Ensure slashes for the following substitutions to work:

s=^=/=; s=/\.$=/=; s=/\.\.$=/../=;

# 2. Eliminate all no-ops:

1 while s=/\.?/=/=;

# 3. Process directory ups:

1 while s=(^|/[^/]+)/\.\./=/=;

# $server and $query are not canonicalized:

$_ = "$server$_$query";
