line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package FusionInventory::Agent::HTTP::Client::OCS; |
2
|
|
|
|
|
|
|
|
3
|
20
|
|
|
20
|
|
22387470
|
use strict; |
|
20
|
|
|
|
|
39
|
|
|
20
|
|
|
|
|
802
|
|
4
|
20
|
|
|
20
|
|
288
|
use warnings; |
|
20
|
|
|
|
|
32
|
|
|
20
|
|
|
|
|
688
|
|
5
|
20
|
|
|
20
|
|
88
|
use base 'FusionInventory::Agent::HTTP::Client'; |
|
20
|
|
|
|
|
67
|
|
|
20
|
|
|
|
|
4801
|
|
6
|
|
|
|
|
|
|
|
7
|
20
|
|
|
20
|
|
119
|
use English qw(-no_match_vars); |
|
20
|
|
|
|
|
30
|
|
|
20
|
|
|
|
|
152
|
|
8
|
20
|
|
|
20
|
|
8496
|
use HTTP::Request; |
|
20
|
|
|
|
|
31
|
|
|
20
|
|
|
|
|
223
|
|
9
|
20
|
|
|
20
|
|
480
|
use UNIVERSAL::require; |
|
20
|
|
|
|
|
27
|
|
|
20
|
|
|
|
|
98
|
|
10
|
20
|
|
|
20
|
|
410
|
use URI; |
|
20
|
|
|
|
|
24
|
|
|
20
|
|
|
|
|
213
|
|
11
|
20
|
|
|
20
|
|
10228
|
use Encode; |
|
20
|
|
|
|
|
154186
|
|
|
20
|
|
|
|
|
1887
|
|
12
|
|
|
|
|
|
|
|
13
|
20
|
|
|
20
|
|
7194
|
use FusionInventory::Agent::Tools; |
|
20
|
|
|
|
|
61
|
|
|
20
|
|
|
|
|
3106
|
|
14
|
20
|
|
|
20
|
|
8480
|
use FusionInventory::Agent::XML::Response; |
|
20
|
|
|
|
|
53
|
|
|
20
|
|
|
|
|
231
|
|
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
my $log_prefix = "[http client] "; |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
sub new { |
19
|
2
|
|
|
2
|
1
|
175
|
my ($class, %params) = @_; |
20
|
|
|
|
|
|
|
|
21
|
2
|
|
|
|
|
21
|
my $self = $class->SUPER::new(%params); |
22
|
|
|
|
|
|
|
|
23
|
2
|
|
|
|
|
13
|
$self->{ua}->default_header('Pragma' => 'no-cache'); |
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
# check compression mode |
26
|
2
|
50
|
|
|
|
120
|
if (Compress::Zlib->require()) { |
|
|
0
|
|
|
|
|
|
27
|
|
|
|
|
|
|
# RFC 1950 |
28
|
2
|
|
|
|
|
88
|
$self->{compression} = 'zlib'; |
29
|
2
|
|
|
|
|
9
|
$self->{ua}->default_header('Content-type' => 'application/x-compress-zlib'); |
30
|
2
|
|
|
|
|
67
|
$self->{logger}->debug( |
31
|
|
|
|
|
|
|
$log_prefix . |
32
|
|
|
|
|
|
|
'Using Compress::Zlib for compression' |
33
|
|
|
|
|
|
|
); |
34
|
|
|
|
|
|
|
} elsif (canRun('gzip')) { |
35
|
|
|
|
|
|
|
# RFC 1952 |
36
|
0
|
|
|
|
|
0
|
$self->{compression} = 'gzip'; |
37
|
0
|
|
|
|
|
0
|
$self->{ua}->default_header('Content-type' => 'application/x-compress-gzip'); |
38
|
0
|
|
|
|
|
0
|
$self->{logger}->debug( |
39
|
|
|
|
|
|
|
$log_prefix . |
40
|
|
|
|
|
|
|
'Using gzip for compression' |
41
|
|
|
|
|
|
|
); |
42
|
|
|
|
|
|
|
} else { |
43
|
0
|
|
|
|
|
0
|
$self->{compression} = 'none'; |
44
|
0
|
|
|
|
|
0
|
$self->{ua}->default_header('Content-type' => 'application/xml'); |
45
|
0
|
|
|
|
|
0
|
$self->{logger}->debug( |
46
|
|
|
|
|
|
|
$log_prefix . |
47
|
|
|
|
|
|
|
'Not using compression' |
48
|
|
|
|
|
|
|
); |
49
|
|
|
|
|
|
|
} |
50
|
|
|
|
|
|
|
|
51
|
2
|
|
|
|
|
7
|
return $self; |
52
|
|
|
|
|
|
|
} |
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
sub send { ## no critic (ProhibitBuiltinHomonyms) |
55
|
7
|
|
|
7
|
1
|
1033385
|
my ($self, %params) = @_; |
56
|
|
|
|
|
|
|
|
57
|
7
|
50
|
|
|
|
121
|
my $url = ref $params{url} eq 'URI' ? |
58
|
|
|
|
|
|
|
$params{url} : URI->new($params{url}); |
59
|
7
|
|
|
|
|
13305
|
my $message = $params{message}; |
60
|
7
|
|
|
|
|
26
|
my $logger = $self->{logger}; |
61
|
|
|
|
|
|
|
|
62
|
7
|
|
|
|
|
67
|
my $request_content = $message->getContent(); |
63
|
7
|
|
|
|
|
4539
|
$logger->debug2($log_prefix . "sending message:\n $request_content"); |
64
|
|
|
|
|
|
|
|
65
|
7
|
|
|
|
|
32
|
$request_content = $self->_compress(encode('UTF-8', $request_content)); |
66
|
7
|
50
|
|
|
|
2868
|
if (!$request_content) { |
67
|
0
|
|
|
|
|
0
|
$logger->error($log_prefix . 'inflating problem'); |
68
|
0
|
|
|
|
|
0
|
return; |
69
|
|
|
|
|
|
|
} |
70
|
|
|
|
|
|
|
|
71
|
7
|
|
|
|
|
121
|
my $request = HTTP::Request->new(POST => $url); |
72
|
7
|
|
|
|
|
1109
|
$request->content($request_content); |
73
|
|
|
|
|
|
|
|
74
|
7
|
|
|
|
|
256
|
my $response = $self->request($request); |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
# no need to log anything specific here, it has already been done |
77
|
|
|
|
|
|
|
# in parent class |
78
|
7
|
100
|
|
|
|
26
|
return if !$response->is_success(); |
79
|
|
|
|
|
|
|
|
80
|
6
|
|
|
|
|
53
|
my $response_content = $response->content(); |
81
|
6
|
100
|
|
|
|
180
|
if (!$response_content) { |
82
|
1
|
|
|
|
|
208
|
$logger->error($log_prefix . "unknown content format"); |
83
|
1
|
|
|
|
|
30
|
return; |
84
|
|
|
|
|
|
|
} |
85
|
|
|
|
|
|
|
|
86
|
5
|
|
|
|
|
24
|
my $uncompressed_response_content = $self->_uncompress($response_content); |
87
|
5
|
50
|
|
|
|
262
|
if (!$uncompressed_response_content) { |
88
|
0
|
|
|
|
|
0
|
$logger->error( |
89
|
|
|
|
|
|
|
$log_prefix . "uncompressed content, starting with: ".substr($response_content, 0, 500) |
90
|
|
|
|
|
|
|
); |
91
|
0
|
|
|
|
|
0
|
return; |
92
|
|
|
|
|
|
|
} |
93
|
|
|
|
|
|
|
|
94
|
5
|
|
|
|
|
30
|
$logger->debug2($log_prefix . "receiving message:\n $uncompressed_response_content"); |
95
|
|
|
|
|
|
|
|
96
|
5
|
|
|
|
|
9
|
my $result; |
97
|
5
|
|
|
|
|
10
|
eval { |
98
|
5
|
|
|
|
|
68
|
$result = FusionInventory::Agent::XML::Response->new( |
99
|
|
|
|
|
|
|
content => $uncompressed_response_content |
100
|
|
|
|
|
|
|
); |
101
|
|
|
|
|
|
|
}; |
102
|
5
|
100
|
|
|
|
17
|
if ($EVAL_ERROR) { |
103
|
2
|
|
|
|
|
10
|
my @lines = split(/\n/, $uncompressed_response_content); |
104
|
2
|
|
|
|
|
13
|
$logger->error( |
105
|
|
|
|
|
|
|
$log_prefix . "unexpected content, starting with $lines[0]" |
106
|
|
|
|
|
|
|
); |
107
|
2
|
|
|
|
|
46
|
return; |
108
|
|
|
|
|
|
|
} |
109
|
|
|
|
|
|
|
|
110
|
3
|
|
|
|
|
45
|
return $result; |
111
|
|
|
|
|
|
|
} |
112
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
sub _compress { |
114
|
7
|
|
|
7
|
|
266
|
my ($self, $data) = @_; |
115
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
return |
117
|
7
|
0
|
|
|
|
71
|
$self->{compression} eq 'zlib' ? $self->_compressZlib($data) : |
|
|
50
|
|
|
|
|
|
118
|
|
|
|
|
|
|
$self->{compression} eq 'gzip' ? $self->_compressGzip($data) : |
119
|
|
|
|
|
|
|
$data; |
120
|
|
|
|
|
|
|
} |
121
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
sub _uncompress { |
123
|
5
|
|
|
5
|
|
13
|
my ($self, $data) = @_; |
124
|
|
|
|
|
|
|
|
125
|
5
|
100
|
|
|
|
62
|
if ($data =~ /(\x78\x9C.*)/s) { |
|
|
50
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
126
|
3
|
|
|
|
|
18
|
$self->{logger}->debug2("format: Zlib"); |
127
|
3
|
|
|
|
|
19
|
return $self->_uncompressZlib($1); |
128
|
|
|
|
|
|
|
} elsif ($data =~ /(\x1F\x8B\x08.*)/s) { |
129
|
0
|
|
|
|
|
0
|
$self->{logger}->debug2("format: Gzip"); |
130
|
0
|
|
|
|
|
0
|
return $self->_uncompressGzip($1); |
131
|
|
|
|
|
|
|
} elsif ($data =~ /(<\/html>|)[^<]*(<.*>)\s*$/s) { |
132
|
2
|
|
|
|
|
16
|
$self->{logger}->debug2("format: Plaintext"); |
133
|
2
|
|
|
|
|
8
|
return $2; |
134
|
|
|
|
|
|
|
} else { |
135
|
0
|
|
|
|
|
0
|
$self->{logger}->debug2("format: Unknown"); |
136
|
0
|
|
|
|
|
0
|
return; |
137
|
|
|
|
|
|
|
} |
138
|
|
|
|
|
|
|
} |
139
|
|
|
|
|
|
|
|
140
|
|
|
|
|
|
|
sub _compressZlib { |
141
|
8
|
|
|
8
|
|
25
|
my ($self, $data) = @_; |
142
|
|
|
|
|
|
|
|
143
|
8
|
|
|
|
|
58
|
return Compress::Zlib::compress($data); |
144
|
|
|
|
|
|
|
} |
145
|
|
|
|
|
|
|
|
146
|
|
|
|
|
|
|
sub _compressGzip { |
147
|
1
|
|
|
1
|
|
515
|
my ($self, $data) = @_; |
148
|
|
|
|
|
|
|
|
149
|
1
|
|
|
|
|
7
|
File::Temp->require(); |
150
|
1
|
|
|
|
|
7850
|
my $in = File::Temp->new(); |
151
|
1
|
|
|
|
|
636
|
print $in $data; |
152
|
1
|
|
|
|
|
49
|
close $in; |
153
|
|
|
|
|
|
|
|
154
|
1
|
|
|
|
|
5
|
my $out = getFileHandle( |
155
|
|
|
|
|
|
|
command => 'gzip -c ' . $in->filename(), |
156
|
|
|
|
|
|
|
logger => $self->{logger} |
157
|
|
|
|
|
|
|
); |
158
|
1
|
50
|
|
|
|
24
|
return unless $out; |
159
|
|
|
|
|
|
|
|
160
|
1
|
|
|
|
|
9
|
local $INPUT_RECORD_SEPARATOR; # Set input to "slurp" mode. |
161
|
1
|
|
|
|
|
1578
|
my $result = <$out>; |
162
|
1
|
|
|
|
|
33
|
close $out; |
163
|
|
|
|
|
|
|
|
164
|
1
|
|
|
|
|
27
|
return $result; |
165
|
|
|
|
|
|
|
} |
166
|
|
|
|
|
|
|
|
167
|
|
|
|
|
|
|
sub _uncompressZlib { |
168
|
4
|
|
|
4
|
|
367
|
my ($self, $data) = @_; |
169
|
|
|
|
|
|
|
|
170
|
4
|
|
|
|
|
21
|
return Compress::Zlib::uncompress($data); |
171
|
|
|
|
|
|
|
} |
172
|
|
|
|
|
|
|
|
173
|
|
|
|
|
|
|
sub _uncompressGzip { |
174
|
1
|
|
|
1
|
|
244
|
my ($self, $data) = @_; |
175
|
|
|
|
|
|
|
|
176
|
1
|
|
|
|
|
11
|
my $in = File::Temp->new(); |
177
|
1
|
|
|
|
|
391
|
print $in $data; |
178
|
1
|
|
|
|
|
32
|
close $in; |
179
|
|
|
|
|
|
|
|
180
|
1
|
|
|
|
|
5
|
my $out = getFileHandle( |
181
|
|
|
|
|
|
|
command => 'gzip -dc ' . $in->filename(), |
182
|
|
|
|
|
|
|
logger => $self->{logger} |
183
|
|
|
|
|
|
|
); |
184
|
1
|
50
|
|
|
|
12
|
return unless $out; |
185
|
|
|
|
|
|
|
|
186
|
1
|
|
|
|
|
6
|
local $INPUT_RECORD_SEPARATOR; # Set input to "slurp" mode. |
187
|
1
|
|
|
|
|
1376
|
my $result = <$out>; |
188
|
1
|
|
|
|
|
26
|
close $out; |
189
|
|
|
|
|
|
|
|
190
|
1
|
|
|
|
|
165
|
return $result; |
191
|
|
|
|
|
|
|
} |
192
|
|
|
|
|
|
|
|
193
|
|
|
|
|
|
|
1; |
194
|
|
|
|
|
|
|
__END__ |