| 1 | package Net::Curl::Easier; | ||
| 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 | =encoding utf-8 | ||
| 7 | |||
| 8 | =head1 NAME | ||
| 9 | |||
| 10 | Net::Curl::Easier - Convenience wrapper around L<Net::Curl::Easy> | ||
| 11 | |||
| 12 | =head1 SYNOPSIS | ||
| 13 | |||
| 14 | my $easy = Net::Curl::Easier->new( url => 'http://perl.org' )->perform(); | ||
| 15 | |||
| 16 | print $easy->body(); | ||
| 17 | |||
| 18 | # … or, to dig in to the response: | ||
| 19 | my $response = HTTP::Response->parse( $easy->head() . $easy->body() ); | ||
| 20 | |||
| 21 | =head1 DESCRIPTION | ||
| 22 | |||
| 23 | L<Net::Curl> is wonderful, but L<Net::Curl::Easy> is a bit clunky for | ||
| 24 | day-to-day use. This library attempts to make that, well, “easier”. :-) | ||
| 25 | |||
| 26 | This module extends Net::Curl::Easy, with differences as noted here: | ||
| 27 | |||
| 28 | =head1 DIFFERENCES FROM Net::Curl::Easy | ||
| 29 | |||
| 30 | =over | ||
| 31 | |||
| 32 | =item * The response headers and body go to an internal buffer by default. | ||
| 33 | Net::Curl::Easy simply adopts libcurl’s defaults, which is understandable | ||
| 34 | but frequently unhelpful. | ||
| 35 | |||
| 36 | =item * Character encoding. As of this writing Net::Curl::Easy uses | ||
| 37 | L<SvPV|perlapi/SvPV>, which means that what libcurl receives depends on | ||
| 38 | how Perl internally stores your string. Thus, two identical strings given | ||
| 39 | to Net::Curl::Easy can yield different input to libcurl. | ||
| 40 | |||
| 41 | This library fixes that by requiring all strings to be B<byte> B<strings> | ||
| 42 | and normalizing Perl’s internal storage before calling into Net::Curl::Easy. | ||
| 43 | |||
| 44 | =item * Several methods are wrapped, as described below. | ||
| 45 | |||
| 46 | =back | ||
| 47 | |||
| 48 | =head1 SEE ALSO | ||
| 49 | |||
| 50 | =over | ||
| 51 | |||
| 52 | =item * L<Net::Curl::Promiser> wraps L<Net::Curl::Multi> with promises. | ||
| 53 | Recommended for concurrent queries! | ||
| 54 | |||
| 55 | =item * L<Net::Curl::Simple> takes a similar approach to this module but | ||
| 56 | presents a substantially different interface. | ||
| 57 | |||
| 58 | =back | ||
| 59 | |||
| 60 | =cut | ||
| 61 | |||
| 62 | #---------------------------------------------------------------------- | ||
| 63 | |||
| 64 | 1 | ▶ | use parent 'Net::Curl::Easy'; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 65 | |||
| 66 | our $VERSION = '0.01_01'; | ||
| 67 | |||
| 68 | #---------------------------------------------------------------------- | ||
| 69 | |||
| 70 | =head1 METHODS | ||
| 71 | |||
| 72 | Besides those inherited from Net::Curl::Easy, this class defines: | ||
| 73 | |||
| 74 | =head2 $obj = I<OBJ>->set( $NAME1 => $VALUE1, $NAME2 => $VALUE2, .. ) | ||
| 75 | |||
| 76 | C<setopt()>s multiple values in a single call. Instead of: | ||
| 77 | |||
| 78 | $easy->setopt( Net::Curl::Easy::CURLOPT_URL, 'http://perl.org' ); | ||
| 79 | $easy->setopt( Net::Curl::Easy::CURLOPT_VERBOSE, 1 ); | ||
| 80 | |||
| 81 | … you can do: | ||
| 82 | |||
| 83 | $easy->set( url => 'http://perl.org', verbose => 1 ); | ||
| 84 | |||
| 85 | See L<curl_easy_setopt(3)> for the full set of options you can give here. | ||
| 86 | |||
| 87 | Note that, since I<OBJ> is returned, you can chain calls to this with | ||
| 88 | calls to other methods like C<perform()>. | ||
| 89 | |||
| 90 | You may not need to call this method since C<new()> calls it for you. | ||
| 91 | |||
| 92 | =cut | ||
| 93 | |||
| 94 | sub set { | ||
| 95 | splice @_, 1, 0, 'setopt'; | ||
| 96 | &_set_or_push; | ||
| 97 | } | ||
| 98 | |||
| 99 | =head2 $obj = I<OBJ>->push( $NAME1 => \@VALUES1, $NAME2 => \@VALUES2, .. ) | ||
| 100 | |||
| 101 | Like C<set()>, but for C<pushopt()>. | ||
| 102 | |||
| 103 | =cut | ||
| 104 | |||
| 105 | sub push { | ||
| 106 | splice @_, 1, 0, 'pushopt'; | ||
| 107 | &_set_or_push; | ||
| 108 | } | ||
| 109 | |||
| 110 | sub _set_or_push { | ||
| 111 | my $self = shift; | ||
| 112 | my $method = shift; | ||
| 113 | |||
| 114 | my @to_set; | ||
| 115 | while (my ($k, $v) = splice @_, 0, 2) { | ||
| 116 | $k =~ tr<a-z><A-Z>; | ||
| 117 | |||
| 118 | $k = __PACKAGE__->can("CURLOPT_$k") || do { | ||
| 119 | require Carp; | ||
| 120 | Carp::croak("CURLOPT_$k doesn’t exist!"); | ||
| 121 | }; | ||
| 122 | |||
| 123 | $k = $k->(); | ||
| 124 | |||
| 125 | CORE::push @to_set, [$k, $v]; | ||
| 126 | } | ||
| 127 | |||
| 128 | $self->$method(@$_) for @to_set; | ||
| 129 | |||
| 130 | return $self; | ||
| 131 | } | ||
| 132 | |||
| 133 | =head2 $value = I<OBJ>->get($NAME) | ||
| 134 | |||
| 135 | Like C<set()>, but for C<getinfo()>. This, of course, doesn’t return | ||
| 136 | I<OBJ>, so it can’t be chained. | ||
| 137 | |||
| 138 | =cut | ||
| 139 | |||
| 140 | sub get { | ||
| 141 | my ($self, $name) = @_; | ||
| 142 | |||
| 143 | $name =~ tr<a-z><A-Z>; | ||
| 144 | |||
| 145 | my $nameval = __PACKAGE__->can("CURLINFO_$name") || do { | ||
| 146 | require Carp; | ||
| 147 | Carp::croak("CURLINFO_$name doesn’t exist!"); | ||
| 148 | }; | ||
| 149 | |||
| 150 | return $self->getinfo($nameval->()); | ||
| 151 | } | ||
| 152 | |||
| 153 | =head2 $str = I<OBJ>->head() | ||
| 154 | |||
| 155 | Returns I<OBJ>’s internal HTTP response header buffer, as a byte string. | ||
| 156 | |||
| 157 | =cut | ||
| 158 | |||
| 159 | sub head { $_[0]{'head'} } | ||
| 160 | |||
| 161 | =head2 $str = I<OBJ>->body() | ||
| 162 | |||
| 163 | Returns the HTTP response body, as a byte string. | ||
| 164 | |||
| 165 | =cut | ||
| 166 | |||
| 167 | sub body { $_[0]{'body'} } | ||
| 168 | |||
| 169 | #---------------------------------------------------------------------- | ||
| 170 | |||
| 171 | =head1 WRAPPED METHODS | ||
| 172 | |||
| 173 | =over | ||
| 174 | |||
| 175 | =item * C<new()> takes a list of key/value pairs and passes it to | ||
| 176 | an internal call to C<set()>. The returned object will always be a | ||
| 177 | (newly-created) hash reference. | ||
| 178 | |||
| 179 | =item * C<escape()> and C<send()> apply the character encoding fix described | ||
| 180 | above. | ||
| 181 | |||
| 182 | =item * C<setopt()> and C<pushopt()> fix character encoding and return | ||
| 183 | the instance object. | ||
| 184 | |||
| 185 | =item * C<perform()> returns the instance object. | ||
| 186 | |||
| 187 | =back | ||
| 188 | |||
| 189 | =cut | ||
| 190 | |||
| 191 | sub escape { | ||
| 192 | utf8::downgrade($_[1]); | ||
| 193 | |||
| 194 | return $_[0]->SUPER::escape($_[1]); | ||
| 195 | } | ||
| 196 | |||
| 197 | sub send { | ||
| 198 | utf8::downgrade($_[1]); | ||
| 199 | |||
| 200 | return $_[0]->SUPER::send($_[1]); | ||
| 201 | } | ||
| 202 | |||
| 203 | sub setopt { | ||
| 204 | my ($self, $key, $value) = @_; | ||
| 205 | |||
| 206 | utf8::downgrade($value) if !ref $value; | ||
| 207 | |||
| 208 | $self->SUPER::setopt($key, $value); | ||
| 209 | |||
| 210 | return $self; | ||
| 211 | } | ||
| 212 | |||
| 213 | sub pushopt { | ||
| 214 | my ($self, $key, $values_ar) = @_; | ||
| 215 | |||
| 216 | ref || utf8::downgrade($_) for @$values_ar; | ||
| 217 | |||
| 218 | $self->SUPER::pushopt($key, $values_ar); | ||
| 219 | |||
| 220 | return $self; | ||
| 221 | } | ||
| 222 | |||
| 223 | sub perform { | ||
| 224 | my $self = shift; | ||
| 225 | |||
| 226 | $self->SUPER::perform(@_); | ||
| 227 | |||
| 228 | return $self; | ||
| 229 | } | ||
| 230 | |||
| 231 | sub new { | ||
| 232 | my $class = shift; | ||
| 233 | my $self = $class->SUPER::new( { head => '', body => ''} ); | ||
| 234 | |||
| 235 | return $self->set( | ||
| 236 | file => \$self->{'body'}, | ||
| 237 | headerdata => \$self->{'head'}, | ||
| 238 | @_, | ||
| 239 | ); | ||
| 240 | } | ||
| 241 | |||
| 242 | =head1 STATIC FUNCTIONS | ||
| 243 | |||
| 244 | For convenience, C<Net::Curl::Easy::strerror()> is aliased in this module. | ||
| 245 | |||
| 246 | =cut | ||
| 247 | |||
| 248 | *strerror = *Net::Curl::Easy::strerror; | ||
| 249 | |||
| 250 | #---------------------------------------------------------------------- | ||
| 251 | |||
| 252 | =head1 LICENSE & COPYRIGHT | ||
| 253 | |||
| 254 | Copyright 2021 by Gasper Software Consulting. All rights reserved. | ||
| 255 | |||
| 256 | This library is licensed under the same terms as Perl itself. | ||
| 257 | See L<perlartistic> for details. | ||
| 258 | |||
| 259 | =cut | ||
| 260 | |||
| 261 | 1; | ||