line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Serengeti::Backend::Native::Transport::Curl; |
2
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
66638
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
34
|
|
4
|
1
|
|
|
1
|
|
4
|
use warnings; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
35
|
|
5
|
|
|
|
|
|
|
|
6
|
1
|
|
|
1
|
|
799
|
use HTTP::Response; |
|
1
|
|
|
|
|
40106
|
|
|
1
|
|
|
|
|
43
|
|
7
|
1
|
|
|
1
|
|
14
|
use URI::Escape qw(uri_escape); |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
81
|
|
8
|
1
|
|
|
1
|
|
807
|
use WWW::Curl::Easy; |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
use Serengeti::Backend::Native qw($UserAgent %DefaultHeaders); |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
use accessors::ro qw(curl); |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
sub new { |
15
|
|
|
|
|
|
|
my ($pkg) = @_; |
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
my $curl = WWW::Curl::Easy->new(); |
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
$curl->setopt(CURLOPT_HEADER, 1); |
20
|
|
|
|
|
|
|
$curl->setopt(CURLOPT_FOLLOWLOCATION, 0); |
21
|
|
|
|
|
|
|
$curl->setopt(CURLOPT_SSL_VERIFYPEER, 0); |
22
|
|
|
|
|
|
|
$curl->setopt(CURLOPT_USERAGENT, $UserAgent); |
23
|
|
|
|
|
|
|
$curl->setopt(CURLOPT_COOKIEJAR, '/dev/null'); |
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
my $self = bless { curl => $curl }, $pkg; |
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
return $self; |
28
|
|
|
|
|
|
|
} |
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
sub _setup_curl { |
31
|
|
|
|
|
|
|
my ($curl, $options) = @_; |
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
my @headers = map { "$_: $DefaultHeaders{$_}" } keys %DefaultHeaders; |
34
|
|
|
|
|
|
|
$curl->setopt(CURLOPT_HTTPHEADER, \@headers); |
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
if ($options->{referrer}) { |
37
|
|
|
|
|
|
|
$curl->setopt(CURLOPT_REFERER, $options->{referrer}); |
38
|
|
|
|
|
|
|
} |
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
return $curl; |
41
|
|
|
|
|
|
|
} |
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
sub get { |
44
|
|
|
|
|
|
|
my ($self, $url, $query_params, $options) = @_; |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
my $curl = _setup_curl($self->curl, $options); |
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
$url = URI->new($url); |
49
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
$query_params = {} unless ref $query_params eq "HASH"; |
51
|
|
|
|
|
|
|
if ($query_params) { |
52
|
|
|
|
|
|
|
my %old_params = $url->query_form(); |
53
|
|
|
|
|
|
|
$old_params{$_} = $query_params->{$_} for keys %$query_params; |
54
|
|
|
|
|
|
|
$url->query_form(\%old_params); |
55
|
|
|
|
|
|
|
} |
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
$curl->setopt(CURLOPT_URL, $url->as_string); |
58
|
|
|
|
|
|
|
$curl->setopt(CURLOPT_HTTPGET, 1); |
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
# Target for request |
61
|
|
|
|
|
|
|
my $response_body; |
62
|
|
|
|
|
|
|
open my $fileb, ">", \$response_body or die "Can't open scalar: $!"; |
63
|
|
|
|
|
|
|
$curl->setopt(CURLOPT_WRITEDATA,$fileb); |
64
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
# Perform request |
66
|
|
|
|
|
|
|
my $retcode = $curl->perform(); |
67
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
# Successful request |
69
|
|
|
|
|
|
|
unless ($retcode == 0) { |
70
|
|
|
|
|
|
|
die $curl->strerror($retcode); |
71
|
|
|
|
|
|
|
} |
72
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
my $response = HTTP::Response->parse($response_body); |
74
|
|
|
|
|
|
|
return $response; |
75
|
|
|
|
|
|
|
} |
76
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
sub post { |
78
|
|
|
|
|
|
|
my ($self, $url, $form_data, $options) = @_; |
79
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
my $curl = _setup_curl($self->curl); |
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
$url = URI->new($url); |
83
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
$curl->setopt(CURLOPT_URL, $url->as_string); |
85
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
# Post data |
87
|
|
|
|
|
|
|
my $content = join "&", map { |
88
|
|
|
|
|
|
|
uri_escape($_) . "=" . uri_escape($form_data->{$_}) |
89
|
|
|
|
|
|
|
} keys %$form_data; |
90
|
|
|
|
|
|
|
$curl->setopt(CURLOPT_VERBOSE, 1); |
91
|
|
|
|
|
|
|
$curl->setopt(CURLOPT_POSTFIELDSIZE, bytes::length($content)); |
92
|
|
|
|
|
|
|
$curl->setopt(CURLOPT_POSTFIELDS, $content); |
93
|
|
|
|
|
|
|
# print STDERR "Content is: $content\n"; |
94
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
# Target for request |
96
|
|
|
|
|
|
|
my $response_body; |
97
|
|
|
|
|
|
|
open my $fileb, ">", \$response_body or die "Can't open scalar: $!"; |
98
|
|
|
|
|
|
|
$curl->setopt(CURLOPT_WRITEDATA,$fileb); |
99
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
# Perform request |
101
|
|
|
|
|
|
|
my $retcode = $curl->perform(); |
102
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
# Successful request |
104
|
|
|
|
|
|
|
unless ($retcode == 0) { |
105
|
|
|
|
|
|
|
die $curl->strerror($retcode); |
106
|
|
|
|
|
|
|
} |
107
|
|
|
|
|
|
|
# print STDERR "Got: \n", $response_body, "\n"; |
108
|
|
|
|
|
|
|
my $response = HTTP::Response->parse($response_body); |
109
|
|
|
|
|
|
|
return $response; |
110
|
|
|
|
|
|
|
} |
111
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
1; |
113
|
|
|
|
|
|
|
__END__ |