| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
package FusionInventory::Agent::HTTP::Client::Fusion; |
|
2
|
|
|
|
|
|
|
|
|
3
|
9
|
|
|
9
|
|
25156400
|
use strict; |
|
|
9
|
|
|
|
|
15
|
|
|
|
9
|
|
|
|
|
235
|
|
|
4
|
9
|
|
|
9
|
|
28
|
use warnings; |
|
|
9
|
|
|
|
|
10
|
|
|
|
9
|
|
|
|
|
239
|
|
|
5
|
9
|
|
|
9
|
|
34
|
use base 'FusionInventory::Agent::HTTP::Client'; |
|
|
9
|
|
|
|
|
64
|
|
|
|
9
|
|
|
|
|
2503
|
|
|
6
|
|
|
|
|
|
|
|
|
7
|
9
|
|
|
9
|
|
37
|
use English qw(-no_match_vars); |
|
|
9
|
|
|
|
|
8
|
|
|
|
9
|
|
|
|
|
52
|
|
|
8
|
|
|
|
|
|
|
|
|
9
|
9
|
|
|
9
|
|
7718
|
use JSON::PP; |
|
|
9
|
|
|
|
|
73457
|
|
|
|
9
|
|
|
|
|
518
|
|
|
10
|
9
|
|
|
9
|
|
49
|
use HTTP::Request; |
|
|
9
|
|
|
|
|
10
|
|
|
|
9
|
|
|
|
|
56
|
|
|
11
|
9
|
|
|
9
|
|
142
|
use HTTP::Headers; |
|
|
9
|
|
|
|
|
2875
|
|
|
|
9
|
|
|
|
|
82
|
|
|
12
|
9
|
|
|
9
|
|
4015
|
use HTTP::Cookies; |
|
|
9
|
|
|
|
|
44609
|
|
|
|
9
|
|
|
|
|
69
|
|
|
13
|
9
|
|
|
9
|
|
204
|
use URI::Escape; |
|
|
9
|
|
|
|
|
11
|
|
|
|
9
|
|
|
|
|
5156
|
|
|
14
|
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
my $log_prefix = "[http client] "; |
|
16
|
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
sub new { |
|
18
|
21
|
|
|
21
|
1
|
99
|
my ($class, %params) = @_; |
|
19
|
|
|
|
|
|
|
|
|
20
|
21
|
|
|
|
|
104
|
my $self = $class->SUPER::new(%params); |
|
21
|
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
# Stack the messages sent in order to be able to check the |
|
23
|
|
|
|
|
|
|
# correctness of the behavior with the test-suite |
|
24
|
21
|
50
|
|
|
|
53
|
if ($params{debug}) { |
|
25
|
0
|
|
|
|
|
0
|
$self->{debug} = 1; |
|
26
|
0
|
|
|
|
|
0
|
$self->{msgStack} = [] |
|
27
|
|
|
|
|
|
|
} |
|
28
|
|
|
|
|
|
|
|
|
29
|
21
|
|
|
|
|
77
|
$self->{_cookies} = HTTP::Cookies->new ; |
|
30
|
|
|
|
|
|
|
|
|
31
|
21
|
|
|
|
|
266
|
return $self; |
|
32
|
|
|
|
|
|
|
} |
|
33
|
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
sub _prepareVal { |
|
35
|
6
|
|
|
6
|
|
10
|
my ($self, $val) = @_; |
|
36
|
|
|
|
|
|
|
|
|
37
|
6
|
50
|
|
|
|
22
|
return '' unless length($val); |
|
38
|
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
# forbid to long argument. |
|
40
|
6
|
|
|
|
|
18
|
while (length(URI::Escape::uri_escape_utf8($val)) > 1500) { |
|
41
|
0
|
|
|
|
|
0
|
$val =~ s/^.{5}/…/; |
|
42
|
|
|
|
|
|
|
} |
|
43
|
|
|
|
|
|
|
|
|
44
|
6
|
|
|
|
|
78
|
return URI::Escape::uri_escape_utf8($val); |
|
45
|
|
|
|
|
|
|
} |
|
46
|
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
sub send { ## no critic (ProhibitBuiltinHomonyms) |
|
48
|
6
|
|
|
6
|
1
|
1020592
|
my ($self, %params) = @_; |
|
49
|
|
|
|
|
|
|
|
|
50
|
6
|
50
|
|
|
|
35
|
push @{$self->{msgStack}}, $params{args} if $self->{debug}; |
|
|
0
|
|
|
|
|
0
|
|
|
51
|
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
my $url = ref $params{url} eq 'URI' ? |
|
53
|
6
|
50
|
|
|
|
89
|
$params{url} : URI->new($params{url}); |
|
54
|
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
my $method = (exists($params{method}) && $params{method} =~ /^GET|POST$/) ? |
|
56
|
6
|
50
|
33
|
|
|
10257
|
$params{method} : 'GET' ; |
|
57
|
|
|
|
|
|
|
|
|
58
|
6
|
|
|
|
|
36
|
my $urlparams = 'action='.uri_escape($params{args}->{action}); |
|
59
|
6
|
|
|
|
|
100
|
my $referer = ''; |
|
60
|
6
|
50
|
|
|
|
17
|
if ($method eq 'POST') { |
|
61
|
0
|
|
|
|
|
0
|
$referer = $url; |
|
62
|
0
|
|
|
|
|
0
|
$url .= '?'.$urlparams ; |
|
63
|
0
|
0
|
|
|
|
0
|
$url .= '&uuid='.uri_escape($params{args}->{uuid}) if (exists($params{args}->{uuid})); |
|
64
|
0
|
|
|
|
|
0
|
$url .= '&method=POST' ; |
|
65
|
|
|
|
|
|
|
} |
|
66
|
|
|
|
|
|
|
|
|
67
|
6
|
|
|
|
|
15
|
foreach my $k (keys %{$params{args}}) { |
|
|
6
|
|
|
|
|
28
|
|
|
68
|
18
|
50
|
66
|
|
|
166
|
if (ref($params{args}->{$k}) eq 'ARRAY') { |
|
|
|
100
|
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
69
|
0
|
|
|
|
|
0
|
foreach (@{$params{args}->{$k}}) { |
|
|
0
|
|
|
|
|
0
|
|
|
70
|
0
|
|
0
|
|
|
0
|
$urlparams .= '&'.$k.'[]='.$self->_prepareVal($_ || ''); |
|
71
|
|
|
|
|
|
|
} |
|
72
|
|
|
|
|
|
|
} elsif (ref($params{args}->{$k}) eq 'HASH') { |
|
73
|
6
|
|
|
|
|
14
|
foreach (keys %{$params{args}->{$k}}) { |
|
|
6
|
|
|
|
|
23
|
|
|
74
|
0
|
|
|
|
|
0
|
$urlparams .= '&'.$k.'['.$_.']='.$self->_prepareVal($params{args}->{$k}{$_}); |
|
75
|
|
|
|
|
|
|
} |
|
76
|
|
|
|
|
|
|
} elsif ($k ne 'action' && length($params{args}->{$k})) { |
|
77
|
6
|
|
|
|
|
35
|
$urlparams .= '&'.$k.'='.$self->_prepareVal($params{args}->{$k}); |
|
78
|
|
|
|
|
|
|
} |
|
79
|
|
|
|
|
|
|
} |
|
80
|
|
|
|
|
|
|
|
|
81
|
6
|
50
|
|
|
|
139
|
$url .= '?'.$urlparams if ($method eq 'GET'); |
|
82
|
|
|
|
|
|
|
|
|
83
|
6
|
50
|
|
|
|
95
|
$self->{logger}->debug2($url) if $self->{logger}; |
|
84
|
|
|
|
|
|
|
|
|
85
|
6
|
|
|
|
|
14
|
my $request ; |
|
86
|
6
|
50
|
|
|
|
21
|
if ($method eq 'GET') { |
|
87
|
6
|
|
|
|
|
68
|
$request = HTTP::Request->new($method => $url); |
|
88
|
|
|
|
|
|
|
} else { |
|
89
|
0
|
0
|
|
|
|
0
|
$self->{logger}->debug2($log_prefix."POST: ".$urlparams) if $self->{logger}; |
|
90
|
0
|
|
|
|
|
0
|
my $headers = HTTP::Headers->new( |
|
91
|
|
|
|
|
|
|
'Content-Type' => 'application/x-www-form-urlencoded', |
|
92
|
|
|
|
|
|
|
'Referer' => $referer |
|
93
|
|
|
|
|
|
|
); |
|
94
|
0
|
|
|
|
|
0
|
$request = HTTP::Request->new( |
|
95
|
|
|
|
|
|
|
$method => $url, |
|
96
|
|
|
|
|
|
|
$headers, |
|
97
|
|
|
|
|
|
|
$urlparams |
|
98
|
|
|
|
|
|
|
); |
|
99
|
0
|
|
|
|
|
0
|
$self->{_cookies}->add_cookie_header( $request ); |
|
100
|
|
|
|
|
|
|
} |
|
101
|
|
|
|
|
|
|
|
|
102
|
6
|
|
|
|
|
685
|
my $response = $self->request($request); |
|
103
|
|
|
|
|
|
|
|
|
104
|
6
|
100
|
|
|
|
14
|
return unless $response->is_success(); |
|
105
|
|
|
|
|
|
|
|
|
106
|
5
|
|
|
|
|
66
|
$self->{_cookies}->extract_cookies($response); |
|
107
|
|
|
|
|
|
|
|
|
108
|
5
|
|
|
|
|
354
|
my $content = $response->content(); |
|
109
|
5
|
100
|
|
|
|
53
|
unless ($content) { |
|
110
|
|
|
|
|
|
|
$self->{logger}->error( $log_prefix . "Got empty response" ) |
|
111
|
1
|
50
|
|
|
|
9
|
if $self->{logger}; |
|
112
|
1
|
|
|
|
|
21
|
return; |
|
113
|
|
|
|
|
|
|
} |
|
114
|
|
|
|
|
|
|
|
|
115
|
4
|
|
|
|
|
5
|
my $answer; |
|
116
|
4
|
|
|
|
|
7
|
eval { |
|
117
|
4
|
50
|
|
|
|
45
|
my $decoder = JSON::PP->new |
|
118
|
|
|
|
|
|
|
or die "Can't use JSON::PP decoder: $!"; |
|
119
|
|
|
|
|
|
|
|
|
120
|
4
|
|
|
|
|
80
|
$answer = $decoder->decode($content); |
|
121
|
|
|
|
|
|
|
}; |
|
122
|
|
|
|
|
|
|
|
|
123
|
4
|
100
|
|
|
|
3420
|
if ($EVAL_ERROR) { |
|
124
|
1
|
|
|
|
|
4
|
my @lines = split(/\n/, $content); |
|
125
|
|
|
|
|
|
|
$self->{logger}->error( |
|
126
|
|
|
|
|
|
|
$log_prefix . "Can't decode JSON content, starting with $lines[0]" |
|
127
|
1
|
50
|
|
|
|
9
|
) if $self->{logger}; |
|
128
|
1
|
|
|
|
|
22
|
return; |
|
129
|
|
|
|
|
|
|
} |
|
130
|
|
|
|
|
|
|
|
|
131
|
3
|
|
|
|
|
31
|
return $answer; |
|
132
|
|
|
|
|
|
|
} |
|
133
|
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
1; |
|
135
|
|
|
|
|
|
|
__END__ |