1
votes

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?

1
Please show complete (runnable) scripts, see minimal reproducible example for more informationHåkon Hægland
On Windows, I find free program Fiddler useful for examining what is actually sent across.ikegami
This is a really interesting question. I don't think you can actually get the raw HTTP request out. Plack's parser is based on chunks. You can dump what's in $env or turn the Plack::Request into a string, but you'd have to hook into the request parser to get a dump out, I think. Or you could hook into your server.simbabque

1 Answers

3
votes

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.