croservices / cro-http

HTTP (including HTTPS and HTTP/2) support for the Cro library for building distributed systems in Raku.
https://cro.services/
Artistic License 2.0
49 stars 26 forks source link

cro::uri generating internal Nil warning #197

Open coke opened 1 month ago

coke commented 1 month ago
raku -MCro::HTTP::Client -e 'my $a = Cro::HTTP::Client.new; my $b = await $a.get: "https://www.reddit.com/r/perl6/comments/42dkme/perl6_not_being_an_interpreted_language/"'
Use of Nil in string context

Something about this particular URL (which returns a 302 if it matters) triggers it.

patrickbkr commented 1 month ago

As you already diagnosed, it's in this line.

method path-absolute($/) {
    my $result = '/';
    $result ~= $_<pchars>.ast with $<segment-nz>;
    $result ~= '/' ~ $_<pchars>.ast for @$<segment>; # <-- here
    make $result; 
}

That's an action method for a grammar.

A quick debug output gives:

Match.new(:orig("/r/perl6/comments/42dkme/perl6_not_being_an_interpreted_language/?rdt=61905"), :from(3), :pos(8), :hash(Map.new((:pchars(Match.new(:orig("/r/perl6/comments/42dkme/perl6_not_being_an_interpreted_language/?rdt=61905"), :from(3), :pos(8), :list(([Match.new(:orig("/r/perl6/comments/42dkme/perl6_not_being_an_interpreted_language/?rdt=61905"), :from(3), :pos(8))],)), :made("perl6")))))), :made("perl6"))
Match.new(:orig("/r/perl6/comments/42dkme/perl6_not_being_an_interpreted_language/?rdt=61905"), :from(9), :pos(17), :hash(Map.new((:pchars(Match.new(:orig("/r/perl6/comments/42dkme/perl6_not_being_an_interpreted_language/?rdt=61905"), :from(9), :pos(17), :list(([Match.new(:orig("/r/perl6/comments/42dkme/perl6_not_being_an_interpreted_language/?rdt=61905"), :from(9), :pos(17))],)), :made("comments")))))), :made("comments"))
Match.new(:orig("/r/perl6/comments/42dkme/perl6_not_being_an_interpreted_language/?rdt=61905"), :from(18), :pos(24), :hash(Map.new((:pchars(Match.new(:orig("/r/perl6/comments/42dkme/perl6_not_being_an_interpreted_language/?rdt=61905"), :from(18), :pos(24), :list(([Match.new(:orig("/r/perl6/comments/42dkme/perl6_not_being_an_interpreted_language/?rdt=61905"), :from(18), :pos(24))],)), :made("42dkme")))))), :made("42dkme"))
Match.new(:orig("/r/perl6/comments/42dkme/perl6_not_being_an_interpreted_language/?rdt=61905"), :from(25), :pos(64), :hash(Map.new((:pchars(Match.new(:orig("/r/perl6/comments/42dkme/perl6_not_being_an_interpreted_language/?rdt=61905"), :from(25), :pos(64), :list(([Match.new(:orig("/r/perl6/comments/42dkme/perl6_not_being_an_interpreted_language/?rdt=61905"), :from(25), :pos(64))],)), :made("perl6_not_being_an_interpreted_language")))))), :made("perl6_not_being_an_interpreted_language"))
Match.new(:orig("/r/perl6/comments/42dkme/perl6_not_being_an_interpreted_language/?rdt=61905"), :from(65), :pos(65))

Notice that the last match is empty. That's the emptyness after the last "/" and before the "?" in the URI.

In similar action methods a few lines down I see guards for this. Replicating them gives:

$result ~= '/' ~ ($_<pchars> ?? $_<pchars>.ast !! ~$_) for @$<segment>;

This fixes the issue.

But I have no idea what's happening. Can I summon a regex expert? Maybe @lizmat?

patrickbkr commented 1 month ago

I think I understand a bit. ~$_ is the unprocessed input. @$ is a list of matched input. If the element in that list happens to be a pchars, then take the processed output (pchars percent-encodes it's content), if not, just take the input unprocessed.