Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

getline method is inoperative and maybe outdated #183

Open
Openstreetmapler opened this issue Apr 20, 2023 · 17 comments
Open

getline method is inoperative and maybe outdated #183

Openstreetmapler opened this issue Apr 20, 2023 · 17 comments

Comments

@Openstreetmapler
Copy link

In my installation of Catalyst, I had to replace this line of code

$slurped = $fh->getline;

with a filehandle read operation (print and comments are not required)

            #! WORKAROUND: using the old and reliable filehandle method
            #- because $fh->getline doesn't return the JSON!
            open(FH, $fh);
            $slurped = <FH>;
            print("*DEBUG* slurped is $slurped\n");
            close(FH);
            #! END OF WORKAROUND

because the getline makes the $slurped variable not have any content, which causes a JSON malformed error when attempting to access the HTTP request body via $c->req->body_data:

[2023/04/20 10:31:12] [catalyst] [ERROR] Error Parsing POST 'undef', Error: malformed JSON string, neither tag, array, object, number, string or atom, at character offset 0 at /home/nasaruk/perl5/lib/perl5/Catalyst.pm line 4092, <$fh> chunk 1.
[2023/04/20 10:31:12] [catalyst] [DEBUG] Response Code: 500; Content-Type: unknown; Content-Length: unknown

Maybe that getline method is heavily outdated and no-one noticed that it ceased to work.

@jjn1056
Copy link
Member

jjn1056 commented Apr 20, 2023

I don't think get line is outdated; can I get a test case that shows this issue?

@Openstreetmapler
Copy link
Author

What should test case look like?

@jjn1056
Copy link
Member

jjn1056 commented Apr 21, 2023

Ideally it should be a failing test that shows the problem. We need to know how you are getting this error. There's a simple test case that has some JSON in it at https://github.com/perl-catalyst/catalyst-runtime/blob/master/t/content_negotiation.t that you might want to crib.

@Openstreetmapler
Copy link
Author

Openstreetmapler commented Apr 24, 2023

How do I run the test script? I don’t have the TestContentNegotiation module and it’s not a CPAN module.

EDIT: Oops, rookie mistake! I tried to install TestContentNegation from CPAN when the module is actually called TestContentNegotiation!

EDIT2: However, neither of these modules are found in CPAN! Can you please tell me where that module is to be found?

@haarg
Copy link
Member

haarg commented Apr 24, 2023

That modules is included in the t/lib directory.

@perl-catalyst-sync
Copy link

perl-catalyst-sync commented Apr 24, 2023 via email

@Openstreetmapler
Copy link
Author

Entering the command prove -lvr t/content_negotiation.t outputs these errors (probably because the files are missing or me using perlbrew):

Cannot detect source of 't/content_negotiation.t'! at /home/nasaruk/perl5/perlbrew/perls/perl-5.22.3/lib/5.22.3/TAP/Parser/IteratorFactory.pm line 261.
	TAP::Parser::IteratorFactory::detect_source(TAP::Parser::IteratorFactory=HASH(0x55b71da73840), TAP::Parser::Source=HASH(0x55b71da55a30)) called at /home/nasaruk/perl5/perlbrew/perls/perl-5.22.3/lib/5.22.3/TAP/Parser/IteratorFactory.pm line 211
	TAP::Parser::IteratorFactory::make_iterator(TAP::Parser::IteratorFactory=HASH(0x55b71da73840), TAP::Parser::Source=HASH(0x55b71da55a30)) called at /home/nasaruk/perl5/perlbrew/perls/perl-5.22.3/lib/5.22.3/TAP/Parser.pm line 472
	TAP::Parser::_initialize(TAP::Parser=HASH(0x55b71da59f10), HASH(0x55b71d828e48)) called at /home/nasaruk/perl5/perlbrew/perls/perl-5.22.3/lib/5.22.3/TAP/Object.pm line 55
	TAP::Object::new("TAP::Parser", HASH(0x55b71d828e48)) called at /home/nasaruk/perl5/perlbrew/perls/perl-5.22.3/lib/5.22.3/TAP/Object.pm line 130
	TAP::Object::_construct(TAP::Harness=HASH(0x55b71d308220), "TAP::Parser", HASH(0x55b71d828e48)) called at /home/nasaruk/perl5/perlbrew/perls/perl-5.22.3/lib/5.22.3/TAP/Harness.pm line 852
	TAP::Harness::make_parser(TAP::Harness=HASH(0x55b71d308220), TAP::Parser::Scheduler::Job=HASH(0x55b71d75ee90)) called at /home/nasaruk/perl5/perlbrew/perls/perl-5.22.3/lib/5.22.3/TAP/Harness.pm line 651
	TAP::Harness::_aggregate_single(TAP::Harness=HASH(0x55b71d308220), TAP::Parser::Aggregator=HASH(0x55b71d757948), TAP::Parser::Scheduler=HASH(0x55b71d75ee30)) called at /home/nasaruk/perl5/perlbrew/perls/perl-5.22.3/lib/5.22.3/TAP/Harness.pm line 743
	TAP::Harness::aggregate_tests(TAP::Harness=HASH(0x55b71d308220), TAP::Parser::Aggregator=HASH(0x55b71d757948), "t/content_negotiation.t") called at /home/nasaruk/perl5/perlbrew/perls/perl-5.22.3/lib/5.22.3/TAP/Harness.pm line 558
	TAP::Harness::__ANON__() called at /home/nasaruk/perl5/perlbrew/perls/perl-5.22.3/lib/5.22.3/TAP/Harness.pm line 571
	TAP::Harness::runtests(TAP::Harness=HASH(0x55b71d308220), "t/content_negotiation.t") called at /home/nasaruk/perl5/perlbrew/perls/perl-5.22.3/lib/5.22.3/App/Prove.pm line 546
	App::Prove::_runtests(App::Prove=HASH(0x55b71d2dfc98), HASH(0x55b71d71f460), "t/content_negotiation.t") called at /home/nasaruk/perl5/perlbrew/perls/perl-5.22.3/lib/5.22.3/App/Prove.pm line 504
	App::Prove::run(App::Prove=HASH(0x55b71d2dfc98)) called at /home/nasaruk/perl5/perlbrew/perls/perl-5.22.3/bin/prove line 13

@perl-catalyst-sync
Copy link

perl-catalyst-sync commented Apr 30, 2023 via email

@Openstreetmapler
Copy link
Author

I don’t have a GitHub checkout. I am running a Catalyst project in a local git repository.

@perl-catalyst-sync
Copy link

perl-catalyst-sync commented May 2, 2023 via email

@perl-catalyst-sync
Copy link

perl-catalyst-sync commented May 2, 2023 via email

@Openstreetmapler
Copy link
Author

Where is that directory?

@perl-catalyst-sync
Copy link

perl-catalyst-sync commented May 2, 2023 via email

@Openstreetmapler
Copy link
Author

No matter where I run the command (even in the Catalyst project’s root folder), it outputs the errors.

There is a t folder in my project folder, but only with these files:

  • 01app.t
  • 02pod.t
  • 03podcoverage.t

@Openstreetmapler
Copy link
Author

No matter where I run the command (even in the Catalyst project’s root folder), it outputs the errors.

There is a t folder in my project folder, but only with these files:

  • 01app.t
  • 02pod.t
  • 03podcoverage.t

I tried running the command with content_negotiation.t added to the t folder and ran the command – but the TestContentNegotiation module is nowhere to be found in @INC!

At this point, it feels like this issue is impossible to solve and I just have to re-install Catalyst. 😞

@haarg
Copy link
Member

haarg commented May 10, 2023

TestContentNegotiation is not installed with Catalyst. It is included with the Catalyst-Runtime git repository or with the CPAN release tarball. The test scripts and their related modules do not get installed.

git clone https://github.com/perl-catalyst/catalyst-runtime.git
cd catalyst-runtime
prove -lvr t/content_negotiation.t

@Openstreetmapler
Copy link
Author

Thanks a lot for the info! However, due to vacation, I won’t be able to try it out until May 31st since the issue is work-related in my case.

bigpresh added a commit to bigpresh/catalyst-runtime that referenced this issue Sep 14, 2023
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants