libcurl via Mojolicious

40 views
Skip to first unread message

Felipe Gasper

unread,
Dec 10, 2019, 9:41:57 AM12/10/19
to Mojolicious
Hi all,

I’ve got a library called Net::Curl::Promiser that does the “dirty-work” of interfacing between different event loops and Net::Curl::Multi. I recently added a Mojo::IOLoop backend, so it should now be possible to integrate libcurl seamlessly into Mojolicious via Net::Curl::Promiser::Mojo.

Hope it’s useful … enjoy!

-FG

Stefan Adams

unread,
Dec 10, 2019, 1:19:20 PM12/10/19
to mojolicious
Hi, Felipe! Thanks for sharing this! What advantages does integrating with libcurl have over using Mojo::UserAgent?

--
You received this message because you are subscribed to the Google Groups "Mojolicious" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mojolicious...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/mojolicious/856af456-b0d4-4638-9eca-9661f02bb555%40googlegroups.com.

Felipe Gasper

unread,
Dec 10, 2019, 1:52:55 PM12/10/19
to mojol...@googlegroups.com
Hi Stefan,

The primary advantage I see is that whatever libcurl features that Net::Curl exposes are now readily usable within Mojo. So libcurl’s http/2 support, for example, should be usable, as would non-HTTP protocols that libcurl supports like FTP, IMAP, etc. (Possibly even the experimental http/3 support?)

Other potential advantages would be speed (XS/C versus pure Perl) and, I would think, wider use: as much as Mojo is used in Perl, curl is used practically everywhere, with bindings in Python, Ruby, Rust, etc.

The disadvantage is that you’ll need to use Net::Curl::Easy, which isn’t a very Perlish interface. HTTP::AnyUA’s backend for same demonstrates how to “tame the beast”, though, and Net::Curl’s own documentation is quite helpful (IMO), so there’s at least copious prior art to reference.

I should disclaim here: I’ve not benchmarked this nor used it very extensively beyond just verifying that it works for simple parallel GETs. That said, since Net::Curl::Promiser is fairly simple, and Net::Curl itself has been around for a while and remains actively maintained, whatever gremlins may still lurk should be easy to find and fix.

-FG
> To view this discussion on the web visit https://groups.google.com/d/msgid/mojolicious/CACyQ%2BFTQyKHHKe1OLaCkfxdN%2B-SgkKtbMq18q93w8FOFdKox0A%40mail.gmail.com.

sri

unread,
Dec 10, 2019, 1:57:07 PM12/10/19
to Mojolicious
        The disadvantage is that you’ll need to use Net::Curl::Easy, which isn’t a very Perlish interface. HTTP::AnyUA’s backend for same demonstrates how to “tame the beast”, though, and Net::Curl’s own documentation is quite helpful (IMO), so there’s at least copious prior art to reference.

Another big disadvantage is that it doesn't use Mojo::Promise. I don't think it will attract too many Mojo users without.

--
sebastian 

Felipe Gasper

unread,
Dec 10, 2019, 1:59:00 PM12/10/19
to mojol...@googlegroups.com


> Le 10 déc. 2019 à 13:57, sri <kra...@gmail.com> a écrit :
>
> The disadvantage is that you’ll need to use Net::Curl::Easy, which isn’t a very Perlish interface. HTTP::AnyUA’s backend for same demonstrates how to “tame the beast”, though, and Net::Curl’s own documentation is quite helpful (IMO), so there’s at least copious prior art to reference.
>
> Another big disadvantage is that it doesn't use Mojo::Promise. I don't think it will attract too many Mojo users without.

The latest version does, actually.

-FG

Sebastian Riedel

unread,
Dec 10, 2019, 2:09:14 PM12/10/19
to mojol...@googlegroups.com
> > Another big disadvantage is that it doesn't use Mojo::Promise. I don't think it will attract too many Mojo users without.
>
> The latest version does, actually.

Ah, nice. Then you can simplify your example too.

my $promiser = Net::Curl::Promiser::Mojo->new();

my $handle = Net::Curl::Easy->new();
$handle->setopt(CURLOPT_URL() => $url);

$promiser->add_handle($handle)->then(
sub { print "$url completed.$/" },
sub { warn "$url failed: " . shift },
)->wait;

And if you really want to conform to the Mojo style you rename
add_handle to add_handle_p, because we like to make promise
returning methods obvious. ;)

--
Sebastian

Felipe Gasper

unread,
Dec 10, 2019, 2:24:59 PM12/10/19
to mojol...@googlegroups.com
Ah, a nice convention!

I’ll add an “add_handle_p()” alias, update the example, and make another release.

-F
Reply all
Reply to author
Forward
0 new messages