Skip to content

Commit

Permalink
Rewind body filehandle before reading JSON
Browse files Browse the repository at this point in the history
If something else had already read from the filehandle, we'd get
nothing, and end up throwing an error like:

```
  [error] Caught exception in engine "Error Parsing POST 'undef',
    Error: malformed JSON string, neither tag, array, object, number,
    string or atom, at character offset 0 (before "(end of string)")
    at /home/davidp/perl5/lib/perl5/Catalyst.pm line 4092, <$fh> chunk 11."
```

... that sounds like we got an empty POST or something, but in fact the
problem was that a plugin had caused the request body to have already
been read, so the filehandle wasn't at the beginning.

This seek means that we will read and parse the whole body content as
intended, even if something had already read from the filehandle.

I think this will also likely solve perl-catalyst#183.
  • Loading branch information
bigpresh committed Sep 14, 2023
1 parent d9a9286 commit 635b273
Showing 1 changed file with 1 addition and 0 deletions.
1 change: 1 addition & 0 deletions lib/Catalyst.pm
Original file line number Diff line number Diff line change
Expand Up @@ -4088,6 +4088,7 @@ sub default_data_handlers {
my $slurped;
return eval {
local $/;
$fh->seek(0,0); # in case it's already been read
$slurped = $fh->getline;
JSON::MaybeXS::decode_json($slurped); # decode_json does utf8 decoding for us
} || Catalyst::Exception->throw(sprintf "Error Parsing POST '%s', Error: %s", (defined($slurped) ? $slurped : 'undef') ,$@);
Expand Down

0 comments on commit 635b273

Please sign in to comment.