/
Curl.pm
447 lines (297 loc) · 11.2 KB
/
Curl.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
package LWP::Curl;
use warnings;
use strict;
use WWW::Curl::Easy;
use Carp qw(croak);
use Data::Dumper;
use URI::Escape;
=head1 NAME
LWP::Curl - LWP methods implementation with Curl engine
=head1 VERSION
Version 0.11
=cut
our $VERSION = '0.13';
=head1 SYNOPSIS
use LWP::Curl;
my $lwpcurl = LWP::Curl->new();
my $content = $lwpcurl->get('http://search.cpan.org','http://www.cpan.org');
# get the page http://search.cpan.org passing with referer http://www.cpan.org
=head1 DESCRIPTION
LWP::Curl provides an interface similar to the LWP library, but is built on top of the Curl library.
The simple LWP-style interface means you don't have to know anything about the underlying library.
=head1 Constructor
=head2 new()
Creates and returns a new LWP::Curl object, hereafter referred to as
the "lwpcurl".
my $lwpcurl = LWP::Curl->new()
=over 4
=item * C<< timeout => sec >>
Set the timeout value in seconds. The default timeout value is
180 seconds, i.e. 3 minutes.
=item * C<< headers => [0|1] >>
Show HTTP headers when return a content. The default is false '0'
=item * C<< user_agent => 'agent86' >>
Set the user agent string. The default is 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)'
=item * C<< followlocation => [0|1] >>
If the spider receive a HTTP 301 ( Redirect ) they will follow?. The default is 1.
=item * C<< auto_encode => [0|1] >>
Turn on/off auto encode urls, for get/post.
=item * C<< maxredirs => number >>
Set how deep the spider will follow when receive HTTP 301 ( Redirect ). The default is 3.
=item * C<< proxy => $proxyurl >>
Set the proxy in the constructor, $proxyurl will be like:
http://myproxy.com:3128/
http://username:password@proxy.com:3128/
libcurl respects the environment variables http_proxy, ftp_proxy,
all_proxy etc, if any of those are set. The $lwpcurl->proxy option does
however override any possibly set environment variables.
=back
=cut
sub new {
# Check for common user mistake
croak("Options to LWP::Curl should be key/value pairs, not hash reference")
if ref( $_[1] ) eq 'HASH';
my ( $class, %args ) = @_;
my $self = {};
my $log = delete $args{log};
my $timeout = delete $args{timeout};
$timeout = 3 * 60 unless defined $timeout;
my $headers = delete $args{headers};
$headers = 0 unless defined $headers;
my $user_agent = delete $args{user_agent};
$user_agent = 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)'
unless defined $user_agent;
my $maxredirs = delete $args{max_redirs};
$maxredirs = 3 unless defined $maxredirs;
my $followlocation = delete $args{followlocation};
$followlocation = 1 unless defined $followlocation;
$self->{auto_encode} = delete $args{auto_encode};
$self->{auto_encode} = 1 unless defined $self->{auto_encode};
$self->{timeout} = $timeout;
my $proxy = delete $args{proxy};
$self->{proxy} = undef unless defined $proxy;
$self->{retcode} = undef;
my $debug = delete $args{debug};
$self->{debug} = 0 unless defined $debug;
print STDERR "\n Hash Debug: \n" . Dumper($self) . "\n" if $debug;
$self->{agent} = WWW::Curl::Easy->new();
$self->{agent}->setopt( CURLOPT_TIMEOUT, $timeout );
$self->{agent}->setopt( CURLOPT_USERAGENT, $user_agent );
$self->{agent}->setopt( CURLOPT_HEADER, $headers );
$self->{agent}->setopt( CURLOPT_AUTOREFERER, 1 ); # always true
$self->{agent}->setopt( CURLOPT_MAXREDIRS, $maxredirs );
$self->{agent}->setopt( CURLOPT_FOLLOWLOCATION, $followlocation );
$self->{agent}->setopt( CURLOPT_SSL_VERIFYPEER, 0 );
$self->{agent}->setopt( CURLOPT_VERBOSE, 0 ); #ubuntu bug
$self->{agent}->setopt( CURLOPT_PROXY, $proxy ) if $proxy;
return bless $self, $class;
}
=head1 METHODS
=head2 $lwpcurl->get($url,$referer)
Get content of $url, passing $referer if defined.
use LWP::Curl;
my $referer = 'http://www.example.com';
my $get_url = 'http://www.example.com/foo';
my $lwpcurl = LWP::Curl->new();
my $content = $lwpcurl->get($get_url, $referer);
The C<get> method croak()'s if the request fails, so wrap an C<eval> around it if you want to
handle failure more elegantly.
=cut
sub get {
my ( $self, $url, $referer ) = @_;
my $agent = $self->{agent};
if ( !$referer ) {
$referer = "";
}
$url = uri_escape($url,"[^:./]") if $self->{auto_encode};
$agent->setopt( CURLOPT_REFERER, $referer );
$agent->setopt( CURLOPT_URL, $url );
$agent->setopt( CURLOPT_HTTPGET, 1 );
my $content = "";
open( my $fileb, ">", \$content );
$agent->setopt( CURLOPT_WRITEDATA, $fileb );
$self->{retcode} = $agent->perform;
if ( $self->{retcode} == 0 ) {
my $response_code = $agent->getinfo(CURLINFO_HTTP_CODE);
if ($response_code == 200 || ($response_code == 0 && $url =~ m!^file:!)) {
print("\nTransfer went ok\n") if $self->{debug};
return $content;
}
}
croak( "An error happened: Host $url "
. $self->{agent}->strerror( $self->{retcode} )
. " ($self->{retcode})\n" );
return undef;
}
=head2 $lwpcurl->post($url,$hash_form,$referer)
POST the $hash_form fields in $url, passing $referer if defined:
use LWP::Curl;
my $lwpcurl = LWP::Curl->new();
my $referer = 'http://www.examplesite.com/';
my $post_url = 'http://www.examplesite.com/post/';
my $hash_form = {
'field1' => 'value1',
'field2' => 'value2',
}
my $content = $lwpcurl->post($post_url, $hash_form, $referer);
=cut
sub post {
my ( $self, $url, $hash_form, $referer ) = @_;
if ( !$referer ) {
$referer = "";
}
if ( !$hash_form ) {
warn(qq{POST Data not defined});
}
else {
#print STDERR Dumper $hash_form;
}
my $post_string = "";
foreach my $var ( keys %{$hash_form} ) {
$post_string = $post_string . "$var=$hash_form->{$var}";
$post_string = $post_string . "&";
#print STDERR "var: $var - $hash_form->{$var}\n";
}
$url = uri_escape($url,"[^:./]") if $self->{auto_encode};
$post_string = uri_escape($post_string,"[^:./]") if $self->{auto_encode};
$self->{agent}->setopt( CURLOPT_POSTFIELDS, $post_string );
$self->{agent}->setopt( CURLOPT_POST, 1 );
$self->{agent}->setopt( CURLOPT_HTTPGET, 0 );
$self->{agent}->setopt( CURLOPT_REFERER, $referer );
$self->{agent}->setopt( CURLOPT_URL, $url );
my $content = "";
open( my $fileb, ">", \$content );
$self->{agent}->setopt( CURLOPT_WRITEDATA, $fileb );
$self->{retcode} = $self->{agent}->perform;
if ( $self->{retcode} == 0 ) {
my $code;
$code = $self->{agent}->getinfo(CURLINFO_HTTP_CODE);
if ($code =~ /^2/) {
return $content;
}
croak "$code request not successful\n";
} else {
croak( "An error happened: Host $url "
. $self->{agent}->strerror( $self->{retcode} )
. " ($self->{retcode})\n" );
}
}
=head2 $lwpcurl->timeout($sec)
Set the timeout to use for all subsequent requests, in seconds.
Defaults to 180 seconds.
=cut
sub timeout {
my ( $self, $timeout ) = @_;
if ( !$timeout ) {
return $self->{timeout};
}
$self->{timeout} = $timeout;
$self->{agent}->setopt( CURLOPT_TIMEOUT, $self->timeout );
}
=head2 $lwpcurl->auto_encode($value)
Turn on/off auto_encode.
=cut
sub auto_encode {
my ( $self, $value ) = @_;
if ( !$value ) {
return $self->{auto_encode};
}
$self->{auto_encode} = $value;
}
=head2 $lwpcurl->agent_alias($alias)
Sets the user agent string to the expanded version from a table
of actual user strings.
I<$alias> can be one of the following:
=over 4
=item * Windows IE 6
=item * Windows Mozilla
=item * Mac Safari
=item * Mac Mozilla
=item * Linux Mozilla
=item * Linux Konqueror
=back
then it will be replaced with a more interesting one. For instance,
$lwpcurl->agent_alias( 'Windows IE 6' );
sets your User-Agent to
Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)
=cut
sub agent_alias {
my ( $self, $alias ) = @_;
# CTRL+C from WWW::Mechanize, thanks for petdance
# ------------
my %known_agents = (
'Windows IE 6' => 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)',
'Windows Mozilla' =>
'Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.4b) Gecko/20030516 Mozilla Firebird/0.6',
'Mac Safari' =>
'Mozilla/5.0 (Macintosh; U; PPC Mac OS X; en-us) AppleWebKit/85 (KHTML, like Gecko) Safari/85',
'Mac Mozilla' =>
'Mozilla/5.0 (Macintosh; U; PPC Mac OS X Mach-O; en-US; rv:1.4a) Gecko/20030401',
'Linux Mozilla' =>
'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.4) Gecko/20030624',
'Linux Konqueror' => 'Mozilla/5.0 (compatible; Konqueror/3; Linux)',
);
if ( defined $known_agents{$alias} ) {
$self->{agent}->setopt( CURLOPT_USERAGENT, $known_agents{$alias} );
}
else {
warn(qq{Unknown agent alias "$alias"});
}
}
=head2 $lwpcurl->proxy($proxyurl)
Set the proxy in the constructor, $proxyurl will be like:
http://myproxy.com:3128/
http://username:password@proxy.com:3128/
libcurl respects the environment variables http_proxy, ftp_proxy,
all_proxy etc, if any of those are set. The $lwpcurl->proxy option does
however override any possibly set environment variables.
To disable proxy set $lwpcurl->proxy('');
$lwpcurl->proxy without argument, return the current proxy
=cut
sub proxy {
my ( $self, $proxy ) = @_;
if ( !defined $proxy ) {
return $self->{proxy};
}
$self->{proxy} = $proxy;
$self->{agent}->setopt( CURLOPT_PROXY, $self->proxy );
}
=head1 TODO
This is a small list of features I'm plan to add. Feel free to contribute with your wishlist and comentaries!
=over 4
=item * Test for the upload method
=item * Improve the Documentation and tests
=item * Support Cookies
=item * PASS in all tests of LWP
=item * Make a patch to L<WWW::Mechanize>, todo change engine, like "new(engine => 'LWP::Curl')"
=back
=head1 AUTHOR
Lindolfo Rodrigues de Oliveira Neto, C<< <lorn at cpan.org> >>
=head1 BUGS
Please report any bugs or feature requests to C<bug-lwp-curl at rt.cpan.org>, or through
the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=LWP-Curl>. I will be notified, and then you'll
automatically be notified of progress on your bug as I make changes.
=head1 SUPPORT
You can find documentation for this module with the perldoc command.
perldoc LWP::Curl
You can also look for information at:
=over 4
=item * RT: CPAN's request tracker
L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=LWP-Curl>
=item * AnnoCPAN: Annotated CPAN documentation
L<http://annocpan.org/dist/LWP-Curl>
=item * CPAN Ratings
L<http://cpanratings.perl.org/d/LWP-Curl>
=item * Search CPAN
L<http://search.cpan.org/dist/LWP-Curl>
=back
=head1 ACKNOWLEDGEMENTS
Thanks to Breno G. Oliveira for the great tips.
Thanks for the LWP and WWW::Mechanize for the inspiration.
Thanks for Neil Bowers for patches
=head1 COPYRIGHT & LICENSE
Copyright 2009 Lindolfo Rodrigues de Oliveira Neto, all rights reserved.
This program is free software; you can redistribute it and/or modify it
under the same terms as Perl itself.
=cut
1; # End of LWP::Curl