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

Hello again Monks,

I want to thank everyone that responds to my questions. I sincerely appreciate the help.

In the past I have received answers for using HTTP::Proxy, and I have learned how to use it and like it a lot. Very fast. My question, now, is about how to use stored header files to recreate the headers for a stored file (URI). I can successfully send the content of the file back to the client via:

my $res = HTTP::Response->new($status_code); $res->content_type('text/html'); #The following $content contains data within saved file pulled from we +b $res->content($content); #Let's push response back to client $self->proxy()->response($res);

I have the raw headers stored in a file, too, and that is what I really want to use rather than only using a
$res->content_type('text/html');

I want to read in the entire file and use it to replicate what was originally received by the server the file was pulled from. Here is a sample of one of the files:

Connection: close Date: Sat, 24 Oct 2009 02:55:24 GMT Accept-Ranges: bytes ETag: "580d7-3e3e-45861df8" Server: Apache/1.3.29 (Unix) PHP/4.3.4 Content-Length: 15934 Content-Type: image/gif Last-Modified: Mon, 18 Dec 2006 17:12:40 GMT Client-Date: Sat, 24 Oct 2009 03:48:49 GMT Client-Response-Num: 1

Something worth noting is that the header file may contain mulitple lines with the same 'xyz:' field on the left, such as 'Links:'.

I want to read the file into a variable and just use something like the following to set up for returning to client.

# $header_file_content contains information from raw header dump file $res->headers($header_file_content);

Thank you in advance for your help

Replies are listed 'Best First'.
Re: Help building response headers for HTTP proxy request
by almut (Canon) on Feb 24, 2010 at 15:54 UTC

    A quick check of the docs makes me believe that HTTP::Headers (which is the type of object HTTP::Proxy is using together with the response) simply doesn't allow to init headers en bloc from a file.  So, I'm afraid you'll have to split them up and set them on a field by field basis...

      Almut,

      You are right. I ended up reading in the header file, line by line and splitting on the first colon ':'. Then, I use the take the info left of the colon and pair it with the info to the right. I build a HTTP::Headers object, and assign each left/right pair to the object via:

      if ( -f $file_hdr_raw ){ open(RAW, "<$file_hdr_raw") or plog("Can NOT open <$SHA1_file_hdr_r +aw> for reading\n"); my(@lines) = <RAW>; close(RAW); while ( @lines ){ my $line = shift(@lines); my ($key, $value) = split(/:/, $line, 2); $headers->header( $key => $value ); } } # create response my $res = HTTP::Response->new($status_code, $status, $headers, $co +ntent); # send back (short-circuit normal content fetching) $self->proxy()->response($res);

      At least I have tested the above, mixed with other code, and it works, well. I turned on the proxy use with Firefox and used a FF plugin to verify I was receiving the complete content of a past header saved off.

      Thank you to everyone for their help.

Re: Help building response headers for HTTP proxy request
by shmem (Chancellor) on Feb 24, 2010 at 16:41 UTC
    I want to read the file into a variable and just use something like the following to set up for returning to client.
    $res->headers($header_file_content);

    Then write the method, or use this one...

    # in whatever package... package HTTP::Response; sub headers { my $obj = shift; my $arg = shift || die "no file nor string, aborted"; my $fh; { no warnings 'io'; # stat might be unsuccessful open $fh, "<", -f $arg ? $arg: \$arg; } while( <$fh>) { chomp; my ($k,$v) = split/:\s+/,$_,2; $obj->header(lc $k, $v); } } package whatever; # back to whatever package

    ...which you can pass a file name or a string containing header lines.