Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

OK, I (after many hours of working with this) have this simple case down):
if ($url !~ /$regex/) { die "Can't find url of file in page.\n"; } $response = $ua->get($1, ':content_file' => $2);

I have two complications now.

First, I (in some cases) need to get the file name from the "Content-Disposition:" header. But I want to check it for sanity (a basic ^[-0-9A-Za-z_]) without putting the whole file into memory before I write it. Is there a way to do that?

Second, is there some way to print ALL of the headers (just to stdout is fine) from the syntax above?

I have looked at:

/usr/lib/perl5/vendor_perl/5.8.8/HTTP/Response.pm

But it seems to want to know the header names in advance (and I don't know them before the request - in fact, that is why I want to print them out :)).

Thank you in advance for your help.

Replies are listed 'Best First'.
Re: libwww-perl basics
by roboticus (Chancellor) on Aug 12, 2010 at 11:20 UTC

    In answer to your second question: You didn't read HTTP::Response very carefully, or you would have noticed the note to refer to the documentation on HTTP::Headers for more information. That document clearly describes:

    $h->header_field_names Returns the list of distinct names for the fields present in the header. The field names have case as suggested by HTTP spec, and the names are returned in the recommended "Good Practice" order.

    ...roboticus

      Actually, I did see that, but I thought (I really don't understand this at all) that I would have to make a second request to get just the headers.

      Can I get those after $response = $ua->get..., and if so, how?

        The docs for HTTP::Response don't give a method to access the HTTP::Headers object, but it mentions that it's a subclass of HTTP::Message. So we have to take a look at the documentation for HTTP::Message, and there we see that it provides the headers method to give you a reference to the HTTP::Headers object. So assuming $ua is an HTTP::Response object, you'd do something like this:

        for my $header (@{$ua->headers->header_field_names}) { print $header, ": ", $ua->header($header), "\n"; }

        Note: I don't write web apps, so I've never used the HTTP classes, so this is (of course!) untested. But hopefully it will lead you in the right direction.

        ...roboticus

Re: libwww-perl basics
by rowdog (Curate) on Aug 12, 2010 at 17:27 UTC

    Maybe you can get the Content-Disposition with a HEAD request.

    $ua->head( $url )

    In terms of dumping the headers you can do something like

    my @field_names = $response->headers->header_field_names; print "$_ => ", $response->header($_), "\n" for @field_names;

    or better yet

    $response->headers->scan(sub { my ($key, $value) = @_; print "$key => $value\n"; });
      I can't because the HEAD request (after going through an authorization sequence with time delays) destroys the ability for me to subsequently retrieve the content (one connection, and then the file is "gone").

        You could write the file to a temp file, parse the header, and then rename the file. As an alternative, lwpcook shows how to process a file as it arrives, so maybe you can read the header when the first chunk comes in.

      The second code snippet works perfectly for printing headers. Thank you.