1

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?

brian d foy
  • 129,424
  • 31
  • 207
  • 592
dave
  • 867
  • 1
  • 5
  • 11
  • 2
    Please show complete (runnable) scripts, see [mcve] for more information – Håkon Hægland Apr 05 '21 at 20:20
  • 1
    On Windows, I find free program Fiddler useful for examining what is actually sent across. – ikegami Apr 06 '21 at 03:02
  • 1
    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 Apr 06 '21 at 13:36

1 Answers1

3

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.

simbabque
  • 53,749
  • 8
  • 73
  • 136