perlplack

How can Plack debug a request?


I am debugging an HTTP Client with Plack, where the plack server listens on port 80 and the client sends request. How can I see the client request using Plack? I am trying to do something like this:

my $app = sub {
  my $self = shift;

  [200, ['Content-Type' => 'text/html'], [ $self ]];
};

How can I debug the request?


Solution

  • PSGI/Plack is an abstraction around HTTP. Its goal is not to have to bother about the implementation details (as much as without it). At the time your app sees the request, it's already been parsed into the $env and is in Plack's representation.

    You could monkey-patch something into Plack::HTTPParser::PP to dump the $chunks out to see what's coming in. You would have to set PLACK_HTTP_PARSER_PP=1 in your environment to make sure it loads the pure Perl version.

    However that seems really tedious. If you're on Linux, you can use netcat (nc). Listen on your port, and send requests there with the client you are testing.

    $ nc -l 3000
    GET / HTTP/1.1
    Host: localhost:3000
    User-Agent: curl/7.68.0
    Accept: */*
    

    And on another terminal...

    $ curl localhost:3000
    

    If you don't care about the exact representation, but only about whether it's got the right stuff in it after parsing, start by dumping out $env in your Plack app instead.