-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathfilename-v3.pl
More file actions
executable file
·44 lines (38 loc) · 990 Bytes
/
filename-v3.pl
File metadata and controls
executable file
·44 lines (38 loc) · 990 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#!/usr/bin/perl
# I've attached a new debugging version that will spit out the redirect info
# so you can get an idea of what's going on. If the chain ends in an ftp address,
# it'll also spit out some ftp errors (because you can't do a HEAD request over ftp).
# -greg
use strict;
use warnings;
use URI;
use Data::Dumper;
use LWP::UserAgent;
unless (@ARGV) {
warn "Usage: $0 URL\n\n";
exit 1;
}
my $url = shift;
my $ua = LWP::UserAgent->new( max_redirect => 0 );
$ua->env_proxy; # use the proxy specified by $http_proxy
my %seen;
while (1) {
if ($seen{ $url }++) {
warn "Found a redirect loop. Exiting.\n";
exit -1;
}
my $resp = $ua->head($url);
unless ($resp) {
warn "Failed to HEAD $url\n";
exit;
}
my $loc = $resp->header( 'Location' );
my $redir = URI->new_abs( $loc, $url )->as_string;
warn $resp->status_line . (defined($redir) ? " (location: " . $redir . ")" : '') . "\n";
if ($resp->is_redirect) {
$url = $redir;
next;
}
last;
}
print "$url\n";