perl - Using HTTP::Server::Simple::CGI, how do I get the headers? -


basically, question similar how access http request headers in http::server::simple::cgi?

the answer use parse_headers(), there no example how use properly. tried use parse_headers() i'm not getting result, stops @ parse_headers() program stucked. couldn't add comment on question above since don't have enough rep so, created new question.

below sample code, example code cpan added parse_headers:

#!/usr/bin/perl { package mywebserver;  use http::server::simple::cgi; our @isa = qw(http::server::simple::cgi); use data::dumper;  %dispatch = (     '/hello.cgi' => \&resp_hello,     # ... );  sub handle_request {     $self = shift;     $cgi  = shift;      $path = $cgi->path_info();     $handler = $dispatch{$path};      $header = $self->parse_headers();     open f,qq{>>~/mywebserver.log};     $dump = data::dumper->dump([$header], [qw($header)]);     print f $dump;     close f;      if (ref($handler) eq "code") {         print "http/1.0 200 ok\r\n";         $handler->($cgi);      } else {         print "http/1.0 404 not found\r\n";         print $cgi->header,               $cgi->start_html('not found'),               $cgi->h1('not found'),               $cgi->end_html;     } }  sub resp_hello {     $cgi  = shift;   # cgi.pm object     return if !ref $cgi;      $who = $cgi->param('name');      print $cgi->header,           $cgi->start_html("hello"),           $cgi->h1("hello $who!"),           $cgi->end_html; }  } # end of package mywebserver  # start server on port 8080 $pid = mywebserver->new(8080)->background(); print "use 'kill $pid' stop server.\n"; 

only added part:

    $header = $self->parse_headers();     open f,qq{>>~/mywebserver.log};     $dump = data::dumper->dump([$header], [qw($header)]);     print f $dump;     close f; 

my objective headers , dump file.

add

sub headers {     my( $self, $headers ) = @_;     if( $headers ){         $self->{__last_headers} = { @$headers };     }     return $self->{__last_headers}; } 

then inside handle_request use my $header = $self->headers();

fwiw, i'm curious why you're using http::server::simple::cgi instead of mojolicious or dancer or http::server::simple::psgi. https://metacpan.org/pod/psgi portability.


Comments

Popular posts from this blog

python - TypeError: start must be a integer -

c# - DevExpress RepositoryItemComboBox BackColor property ignored -

django - Creating multiple model instances in DRF3 -