#!/usr/bin/env perl # # urlheadinfo - obtain information about http(s) URLs in input using HEAD # # $Id$ use warnings; use strict; use JSON qw(to_json); use Tie::IxHash; # for preserving input order use Getopt::Std; use AnyEvent::HTTP; my ( $ign, $me, $revnr ) = split( /\s+/, '$Id$' ); my %opt; getopts( 'hj', \%opt ) or die "Cannot parse command line: $@\n"; $opt{h} and HELP_MESSAGE(); sub HELP_MESSAGE { print STDERR <condvar; #my $json_opts = { map { $_ => 1 } qw(utf8 pretty canonical) }; my $json_opts = { map { $_ => 1 } qw(utf8 pretty) }; my $print_request = $opt{j} ? sub { my %header; tie %header, 'Tie::IxHash', @_; # preserves array order print to_json( \%header, $json_opts ); } : sub { while (@_) { my ( $h, $v ) = ( shift, shift ); print "$h: $v$/"; } }; while (<>) { foreach my $url (split) { $url =~ /^https?:/ or next; #warn "must process $url\n"; $cv->begin; http_head( $url, sub { my $header = $_[1]; &$print_request( 'URL-Originally', $url, map { ( s#\b\w#\U$&#gr, $header->{$_} ) } grep { $_ ne 'Redirect' } keys %$header ); $cv->end; } ); } } $cv->recv