/usr/share/fusioninventory/lib/FusionInventory/Agent/HTTP/Client/OCS.pm is in fusioninventory-agent 1:2.3.10.1-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 | package FusionInventory::Agent::HTTP::Client::OCS;
use strict;
use warnings;
use base 'FusionInventory::Agent::HTTP::Client';
use English qw(-no_match_vars);
use HTTP::Request;
use UNIVERSAL::require;
use URI;
use Encode;
use FusionInventory::Agent::Tools;
use FusionInventory::Agent::XML::Response;
my $log_prefix = "[http client] ";
sub new {
my ($class, %params) = @_;
my $self = $class->SUPER::new(%params);
$self->{ua}->default_header('Pragma' => 'no-cache');
# check compression mode
if (Compress::Zlib->require()) {
# RFC 1950
$self->{compression} = 'zlib';
$self->{ua}->default_header('Content-type' => 'application/x-compress-zlib');
$self->{logger}->debug(
$log_prefix .
'Using Compress::Zlib for compression'
);
} elsif (canRun('gzip')) {
# RFC 1952
$self->{compression} = 'gzip';
$self->{ua}->default_header('Content-type' => 'application/x-compress-gzip');
$self->{logger}->debug(
$log_prefix .
'Using gzip for compression'
);
} else {
$self->{compression} = 'none';
$self->{ua}->default_header('Content-type' => 'application/xml');
$self->{logger}->debug(
$log_prefix .
'Not using compression'
);
}
return $self;
}
sub send { ## no critic (ProhibitBuiltinHomonyms)
my ($self, %params) = @_;
my $url = ref $params{url} eq 'URI' ?
$params{url} : URI->new($params{url});
my $message = $params{message};
my $logger = $self->{logger};
my $request_content = $message->getContent();
$logger->debug2($log_prefix . "sending message:\n $request_content");
$request_content = $self->_compress(encode('UTF-8', $request_content));
if (!$request_content) {
$logger->error($log_prefix . 'inflating problem');
return;
}
my $request = HTTP::Request->new(POST => $url);
$request->content($request_content);
my $response = $self->request($request);
# no need to log anything specific here, it has already been done
# in parent class
return if !$response->is_success();
my $response_content = $response->content();
if (!$response_content) {
$logger->error($log_prefix . "unknown content format");
return;
}
my $uncompressed_response_content = $self->_uncompress($response_content);
if (!$uncompressed_response_content) {
$logger->error(
$log_prefix . "uncompressed content, starting with: ".substr($response_content, 0, 500)
);
return;
}
$logger->debug2($log_prefix . "receiving message:\n $uncompressed_response_content");
my $result;
eval {
$result = FusionInventory::Agent::XML::Response->new(
content => $uncompressed_response_content
);
};
if ($EVAL_ERROR) {
my @lines = split(/\n/, $uncompressed_response_content);
$logger->error(
$log_prefix . "unexpected content, starting with $lines[0]"
);
return;
}
return $result;
}
sub _compress {
my ($self, $data) = @_;
return
$self->{compression} eq 'zlib' ? $self->_compressZlib($data) :
$self->{compression} eq 'gzip' ? $self->_compressGzip($data) :
$data;
}
sub _uncompress {
my ($self, $data) = @_;
if ($data =~ /(\x78\x9C.*)/s) {
$self->{logger}->debug2("format: Zlib");
return $self->_uncompressZlib($1);
} elsif ($data =~ /(\x1F\x8B\x08.*)/s) {
$self->{logger}->debug2("format: Gzip");
return $self->_uncompressGzip($1);
} elsif ($data =~ /(<html><\/html>|)[^<]*(<.*>)\s*$/s) {
$self->{logger}->debug2("format: Plaintext");
return $2;
} else {
$self->{logger}->debug2("format: Unknown");
return;
}
}
sub _compressZlib {
my ($self, $data) = @_;
return Compress::Zlib::compress($data);
}
sub _compressGzip {
my ($self, $data) = @_;
File::Temp->require();
my $in = File::Temp->new();
print $in $data;
close $in;
my $out = getFileHandle(
command => 'gzip -c ' . $in->filename(),
logger => $self->{logger}
);
return unless $out;
local $INPUT_RECORD_SEPARATOR; # Set input to "slurp" mode.
my $result = <$out>;
close $out;
return $result;
}
sub _uncompressZlib {
my ($self, $data) = @_;
return Compress::Zlib::uncompress($data);
}
sub _uncompressGzip {
my ($self, $data) = @_;
my $in = File::Temp->new();
print $in $data;
close $in;
my $out = getFileHandle(
command => 'gzip -dc ' . $in->filename(),
logger => $self->{logger}
);
return unless $out;
local $INPUT_RECORD_SEPARATOR; # Set input to "slurp" mode.
my $result = <$out>;
close $out;
return $result;
}
1;
__END__
=head1 NAME
FusionInventory::Agent::HTTP::Client::OCS - An HTTP client using OCS protocol
=head1 DESCRIPTION
This is the object used by the agent to send messages to OCS or GLPI servers,
using original OCS protocol (XML messages sent through POST requests).
=head1 METHODS
=head2 send(%params)
Send an instance of C<FusionInventory::Agent::XML::Query> to the target (the
server).
The following parameters are allowed, as keys of the %params
hash:
=over
=item I<url>
the url to send the message to (mandatory)
=item I<message>
the message to send (mandatory)
=back
This method returns an C<FusionInventory::Agent::XML::Response> instance.
|