perl - HTTP::Request->parse does not work as expected -
i'm trying parse http request string http::request->parse method,
get / http/1.1 host: www.google.com referer: www.google.com cookies: a=b
(there's "\r\n" @ end of it, can't append without breaking syntax highlighter ..)
the above string works when send nc www.google.com 80 < request
now try parse , send throught lwp:
use warnings; use strict; use file::slurp qw/read_file/; use http::request; use lwp::useragent; $ua = lwp::useragent->new; $request = http::request->parse (read_file ('/run/shm/request')); print dumper ($request); $r = $ua->request ($request); if ($r->is_success) { print $r->decoded_content; } else { print $r->status_line; }
and get:
$var1 = bless( { '_headers' => bless( {}, 'http::headers' ), '_content' => '', '_protocol' => 'http/1.1', '_method' => 'get', '_uri' => bless( do{\(my $o = '/')}, 'uri::_generic' ) }, 'http::request' ); 400 url must absolute
so parser not working, fails parse both uri , headers.
any ideas?
i don't file::slurp
. it's replacement idiom
my $contents = { open $fh, '<', 'myfile' or die $!; local $/; <$fh>; };
or, if pass filename on command line
my $contents = { local $/; <>; };
which far difficult use, , makes clear happening.
here, file::slurp
cause of problems because, when called in list context (and parameters of subroutine call apply list context) returns list of lines file instead of whole file in single scalar value.
because http::request->parse
looks @ first parameter passed, sees get
line, , produces request no headers.
you can fix writing
my $request = read_file ('/run/shm/request'); $request = http::request->parse($request);
or may prefer
my $request = http::request->parse(scalar read_file ('/run/shm/request'));
but write like
use autodie; $request = { open $fh, '<', '/run/shm/request'; local $/; $contents = <$fh>; http::request->parse($contents); };
update
by way, better way of viewing http message http::message
@ al. has built use print $message->as_string
. using data::dumper
shows lot of irrelevant data , context used internally object, , there no way of knowing parts of relevant.
in case of program above, corrected version uses
print $request->as_string
results in output
get / http/1.1 host: www.google.com referer: www.google.com cookies: a=b
which input file contains, , expect.
Comments
Post a Comment