| 1 | package Net::Curl::Promiser; | ||
| 2 | |||
| 3 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | 1 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | |||
| 6 | our $VERSION = '0.21'; | ||
| 7 | |||
| 8 | =encoding utf-8 | ||
| 9 | |||
| 10 | =head1 NAME | ||
| 11 | |||
| 12 | Net::Curl::Promiser - Asynchronous L<libcurl|https://curl.haxx.se/libcurl/>, the easy way! | ||
| 13 | |||
| 14 | =head1 DESCRIPTION | ||
| 15 | |||
| 16 | =begin html | ||
| 17 | |||
| 18 | <a href='https://coveralls.io/github/FGasper/p5-Net-Curl-Promiser?branch=master'><img src='https://coveralls.io/repos/github/FGasper/p5-Net-Curl-Promiser/badge.svg?branch=master' alt='Coverage Status' /></a> | ||
| 19 | |||
| 20 | =end html | ||
| 21 | |||
| 22 | L<Net::Curl::Multi> is powerful but tricky to use: polling, callbacks, | ||
| 23 | timers, etc. This module does all of that for you and puts a Promise | ||
| 24 | interface on top of it, so asynchronous I/O becomes almost as simple as | ||
| 25 | synchronous I/O. | ||
| 26 | |||
| 27 | L<Net::Curl::Promiser> itself is a base class; you’ll need to use | ||
| 28 | a subclass that works with your chosen event interface. | ||
| 29 | |||
| 30 | This distribution provides the following usable subclasses: | ||
| 31 | |||
| 32 | =over | ||
| 33 | |||
| 34 | =item * L<Net::Curl::Promiser::Mojo> (for L<Mojolicious>) | ||
| 35 | |||
| 36 | =item * L<Net::Curl::Promiser::AnyEvent> (for L<AnyEvent>) | ||
| 37 | |||
| 38 | =item * L<Net::Curl::Promiser::IOAsync> (for L<IO::Async>) | ||
| 39 | |||
| 40 | =item * L<Net::Curl::Promiser::Select> (for manually-written | ||
| 41 | C<select()> loops) | ||
| 42 | |||
| 43 | =back | ||
| 44 | |||
| 45 | If the event interface you want to use isn’t compatible with one of the | ||
| 46 | above, you’ll need to create your own L<Net::Curl::Promiser> subclass. | ||
| 47 | This is undocumented but pretty simple; have a look at the ones above as | ||
| 48 | well as another based on Linux’s L<epoll(7)> in the distribution’s | ||
| 49 | F</examples>. | ||
| 50 | |||
| 51 | =head1 MEMORY LEAK DETECTION | ||
| 52 | |||
| 53 | This module will, by default, C<warn()> if its objects are C<DESTROY()>ed | ||
| 54 | during Perl’s global destruction phase. To suppress this behavior, set | ||
| 55 | C<$Net::Curl::Promiser::IGNORE_MEMORY_LEAKS> to a truthy value. | ||
| 56 | |||
| 57 | =head1 PROMISE IMPLEMENTATION | ||
| 58 | |||
| 59 | This class’s default Promise implementation is L<Promise::ES6>. | ||
| 60 | You can use a different one by overriding the C<PROMISE_CLASS()> method in | ||
| 61 | a subclass, as long as the substitute class’s C<new()> method works the | ||
| 62 | same way as Promise::ES6’s (which itself follows the ECMAScript standard). | ||
| 63 | |||
| 64 | (NB: L<Net::Curl::Promiser::Mojo> uses L<Mojo::Promise> instead of | ||
| 65 | Promise::ES6.) | ||
| 66 | |||
| 67 | =head2 B<Experimental> L<Promise::XS> support | ||
| 68 | |||
| 69 | Try out experimental Promise::XS support by running with | ||
| 70 | C<NET_CURL_PROMISER_PROMISE_ENGINE=Promise::XS> in your environment. | ||
| 71 | This will override C<PROMISE_CLASS()>. | ||
| 72 | |||
| 73 | =head1 DESIGN NOTES | ||
| 74 | |||
| 75 | Internally each instance of this class uses an instance of | ||
| 76 | L<Net::Curl::Multi> and an instance of L<Net::Curl::Promiser::Backend>. | ||
| 77 | (The latter, in turn, is subclassed to provide logic specific to | ||
| 78 | each event interface.) These are kept separate to avoid circular references. | ||
| 79 | |||
| 80 | =cut | ||
| 81 | |||
| 82 | #---------------------------------------------------------------------- | ||
| 83 | |||
| 84 | 1 | ▶ | use parent 'Net::Curl::Promiser::LeakDetector'; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 85 | |||
| 86 | # So that Net::Curl::Easy::Code’s overloading gets set up: | ||
| 87 | 0 | ▶ | use Net::Curl::Easy (); |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 88 | |||
| 89 | use Net::Curl::Multi (); | ||
| 90 | |||
| 91 | use constant _DEBUG => 0; | ||
| 92 | |||
| 93 | use constant _DEFAULT_TIMEOUT => 1000; | ||
| 94 | |||
| 95 | our $IGNORE_MEMORY_LEAKS; | ||
| 96 | |||
| 97 | #---------------------------------------------------------------------- | ||
| 98 | |||
| 99 | =head1 GENERAL-USE METHODS | ||
| 100 | |||
| 101 | The following are of interest to any code that uses this module: | ||
| 102 | |||
| 103 | =head2 I<CLASS>->new(@ARGS) | ||
| 104 | |||
| 105 | Instantiates this class, including creation of an underlying | ||
| 106 | L<Net::Curl::Multi> object. | ||
| 107 | |||
| 108 | =cut | ||
| 109 | |||
| 110 | sub new { | ||
| 111 | my ($class, @args) = @_; | ||
| 112 | |||
| 113 | my %props = ( | ||
| 114 | callbacks => {}, | ||
| 115 | to_fail => {}, | ||
| 116 | ignore_leaks => $IGNORE_MEMORY_LEAKS, | ||
| 117 | ); | ||
| 118 | |||
| 119 | my $self = bless \%props, $class; | ||
| 120 | |||
| 121 | my $multi = Net::Curl::Multi->new(); | ||
| 122 | $self->{'multi'} = $multi; | ||
| 123 | |||
| 124 | my $backend = $self->_INIT(\@args); | ||
| 125 | $self->{'backend'} = $backend; | ||
| 126 | |||
| 127 | $multi->setopt( | ||
| 128 | Net::Curl::Multi::CURLMOPT_SOCKETDATA, | ||
| 129 | $backend, | ||
| 130 | ); | ||
| 131 | |||
| 132 | $multi->setopt( | ||
| 133 | Net::Curl::Multi::CURLMOPT_SOCKETFUNCTION, | ||
| 134 | \&_socket_fn, | ||
| 135 | ); | ||
| 136 | |||
| 137 | return $self; | ||
| 138 | } | ||
| 139 | |||
| 140 | #---------------------------------------------------------------------- | ||
| 141 | |||
| 142 | =head2 promise($EASY) = I<OBJ>->add_handle( $EASY ) | ||
| 143 | |||
| 144 | A passthrough to the underlying L<Net::Curl::Multi> object’s | ||
| 145 | method of the same name, but the return is given as a Promise object. | ||
| 146 | |||
| 147 | That promise resolves with the passed-in $EASY object. | ||
| 148 | It rejects with either the error given to C<fail_handle()> or the | ||
| 149 | error that L<Net::Curl::Multi> object’s C<info_read()> returns. | ||
| 150 | |||
| 151 | B<IMPORTANT:> As with libcurl itself, HTTP-level failures | ||
| 152 | (e.g., 4xx and 5xx responses) are B<NOT> considered failures at this level. | ||
| 153 | |||
| 154 | =cut | ||
| 155 | |||
| 156 | sub add_handle { | ||
| 157 | my ($self, $easy) = @_; | ||
| 158 | |||
| 159 | return $self->{'backend'}->add_handle($easy, $self->{'multi'}); | ||
| 160 | } | ||
| 161 | |||
| 162 | =head2 $obj = I<OBJ>->cancel_handle( $EASY ) | ||
| 163 | |||
| 164 | Prematurely cancels $EASY. The associated promise will be abandoned | ||
| 165 | in pending state, never to resolve nor reject. | ||
| 166 | |||
| 167 | Returns I<OBJ>. | ||
| 168 | |||
| 169 | =cut | ||
| 170 | |||
| 171 | sub cancel_handle { | ||
| 172 | my ($self, $easy) = @_; | ||
| 173 | |||
| 174 | $self->{'backend'}->cancel_handle($easy, $self->{'multi'}); | ||
| 175 | |||
| 176 | return $self; | ||
| 177 | } | ||
| 178 | |||
| 179 | =head2 $obj = I<OBJ>->fail_handle( $EASY, $REASON ) | ||
| 180 | |||
| 181 | Like C<cancel_handle()> but rejects $EASY’s associated promise | ||
| 182 | with the given $REASON. | ||
| 183 | |||
| 184 | Returns I<OBJ>. | ||
| 185 | |||
| 186 | =cut | ||
| 187 | |||
| 188 | sub fail_handle { | ||
| 189 | my ($self, $easy, $reason) = @_; | ||
| 190 | |||
| 191 | if (!defined $reason || !length $reason) { | ||
| 192 | require Carp; | ||
| 193 | Carp::carp("fail_handle(): no reason given"); | ||
| 194 | } | ||
| 195 | |||
| 196 | $self->{'backend'}->fail_handle($easy, $reason, $self->{'multi'}); | ||
| 197 | |||
| 198 | return $self; | ||
| 199 | } | ||
| 200 | |||
| 201 | #---------------------------------------------------------------------- | ||
| 202 | |||
| 203 | =head2 $obj = I<OBJ>->setopt( … ) | ||
| 204 | |||
| 205 | A passthrough to the underlying L<Net::Curl::Multi> object’s | ||
| 206 | method of the same name. Returns I<OBJ> to facilitate chaining. | ||
| 207 | |||
| 208 | This class requires control of certain L<Net::Curl::Multi> options; | ||
| 209 | if you attempt to set one of these here you’ll get an exception. | ||
| 210 | |||
| 211 | =cut | ||
| 212 | |||
| 213 | sub _SETOPT_FORBIDDEN { qw( SOCKETFUNCTION SOCKETDATA ) }; | ||
| 214 | |||
| 215 | sub setopt { | ||
| 216 | my $self = shift; | ||
| 217 | |||
| 218 | for my $opt ( $self->_SETOPT_FORBIDDEN() ) { | ||
| 219 | my $fullopt = "CURLMOPT_$opt"; | ||
| 220 | |||
| 221 | if ($_[0] == Net::Curl::Multi->can($fullopt)->()) { | ||
| 222 | my $ref = ref $self; | ||
| 223 | die "Don’t set $fullopt via $ref!"; | ||
| 224 | } | ||
| 225 | } | ||
| 226 | |||
| 227 | $self->{'multi'}->setopt(@_); | ||
| 228 | return $self; | ||
| 229 | } | ||
| 230 | |||
| 231 | =head2 $obj = I<OBJ>->handles( … ) | ||
| 232 | |||
| 233 | A passthrough to the underlying L<Net::Curl::Multi> object’s | ||
| 234 | method of the same name. | ||
| 235 | |||
| 236 | =cut | ||
| 237 | |||
| 238 | sub handles { | ||
| 239 | return shift()->{'multi'}->handles(); | ||
| 240 | } | ||
| 241 | |||
| 242 | #---------------------------------------------------------------------- | ||
| 243 | |||
| 244 | sub _socket_fn { | ||
| 245 | my ( $multi, $fd, $action, $backend ) = @_[0, 2, 3, 5]; | ||
| 246 | |||
| 247 | # IMPORTANT: Removing handles within this function is likely to | ||
| 248 | # corrupt libcurl. | ||
| 249 | |||
| 250 | if ($action == Net::Curl::Multi::CURL_POLL_IN) { | ||
| 251 | print STDERR "FD $fd, IN\n" if _DEBUG; | ||
| 252 | |||
| 253 | $backend->SET_POLL_IN($fd, $multi); | ||
| 254 | } | ||
| 255 | elsif ($action == Net::Curl::Multi::CURL_POLL_OUT) { | ||
| 256 | print STDERR "FD $fd, OUT\n" if _DEBUG; | ||
| 257 | |||
| 258 | $backend->SET_POLL_OUT($fd, $multi); | ||
| 259 | } | ||
| 260 | elsif ($action == Net::Curl::Multi::CURL_POLL_INOUT) { | ||
| 261 | print STDERR "FD $fd, INOUT\n" if _DEBUG; | ||
| 262 | |||
| 263 | $backend->SET_POLL_INOUT($fd, $multi); | ||
| 264 | } | ||
| 265 | elsif ($action == Net::Curl::Multi::CURL_POLL_REMOVE) { | ||
| 266 | print STDERR "FD $fd, STOP\n" if _DEBUG; | ||
| 267 | |||
| 268 | $backend->STOP_POLL($fd, $multi); | ||
| 269 | } | ||
| 270 | else { | ||
| 271 | warn( __PACKAGE__ . ": Unrecognized action $action on FD $fd\n" ); | ||
| 272 | } | ||
| 273 | |||
| 274 | return 0; | ||
| 275 | } | ||
| 276 | |||
| 277 | #---------------------------------------------------------------------- | ||
| 278 | |||
| 279 | =head1 EXAMPLES | ||
| 280 | |||
| 281 | See the distribution’s F</examples> directory. | ||
| 282 | |||
| 283 | =head1 SEE ALSO | ||
| 284 | |||
| 285 | Try L<Net::Curl::Easier> for a more polished variant of Net::Curl::Easy. | ||
| 286 | |||
| 287 | L<Net::Curl::Simple> implements a similar idea to this module but | ||
| 288 | doesn’t return promises. It has a more extensive interface that provides | ||
| 289 | a more “perlish” experience than L<Net::Curl::Easy>. | ||
| 290 | |||
| 291 | If you use L<AnyEvent>, then L<AnyEvent::XSPromises> with | ||
| 292 | L<AnyEvent::YACurl> may be a nicer fit for you. | ||
| 293 | |||
| 294 | =head1 REPOSITORY | ||
| 295 | |||
| 296 | L<https://github.com/FGasper/p5-Net-Curl-Promiser> | ||
| 297 | |||
| 298 | =head1 LICENSE & COPYRIGHT | ||
| 299 | |||
| 300 | Copyright 2019-2020 Gasper Software Consulting. | ||
| 301 | |||
| 302 | This library is licensed under the same terms as Perl itself. | ||
| 303 | |||
| 304 | =cut | ||
| 305 | |||
| 306 | 1; | ||