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
|
#
# (c) Jan Gehring <jan.gehring@gmail.com>
#
package Rex::Interface::Connection::HTTP;
use v5.12.5;
use warnings;
our $VERSION = '1.14.1'; # VERSION
use Rex::Interface::Connection::Base;
BEGIN {
LWP::UserAgent->use;
JSON::MaybeXS->use;
}
use Data::Dumper;
use base qw(Rex::Interface::Connection::Base);
sub new {
my $that = shift;
my $proto = ref($that) || $that;
my $self = $that->SUPER::new(@_);
bless( $self, $proto );
# activate caching
Rex::Config->set_use_cache(1);
return $self;
}
sub error { }
sub connect {
my ( $self, %option ) = @_;
my ( $user, $pass, $server, $port, $timeout );
$user = $option{user};
$pass = $option{password};
$server = $option{server};
$port = $self->_get_port( $option{port} );
$timeout = $option{timeout};
$self->{server} = $server;
$self->{port} = $port;
if ( $server =~ m/([^:]+):(\d+)/ ) {
$server = $self->{server} = $1;
$port = $self->{port} = $2;
}
$self->{__user} = $user;
$self->{__pass} = $pass;
if (!Rex::Config->has_user
&& Rex::Config->get_ssh_config_username( server => $server ) )
{
$user = Rex::Config->get_ssh_config_username( server => $server );
}
$self->ua->credentials( "$server:$port", "Rex::Endpoint::HTTP",
$user => $pass, );
my $resp = $self->post("/login");
if ( $resp->{ok} ) {
Rex::Logger::info("Connected to $server, trying to authenticate.");
}
else {
Rex::Logger::info( "Can't connect to $server", "warn" );
$self->{connected} = 0;
return;
}
Rex::Logger::info( "Connecting to $server:$port (" . $user . ")" );
}
sub disconnect { }
sub get_connection_object { my ($self) = @_; return $self; }
sub get_fs_connection_object { my ($self) = @_; return $self; }
sub is_connected { return 1; }
sub is_authenticated { return 1; }
sub exec {
my ( $self, $cmd ) = @_;
}
sub ua {
my ($self) = @_;
return $self->{ua} if $self->{ua};
$self->{ua} = LWP::UserAgent->new;
}
sub upload {
my ( $self, $data ) = @_;
my $res = $self->ua->post(
$self->_get_proto . "://"
. $self->{server} . ":"
. $self->{port}
. "/fs/upload",
Content_Type => "multipart/form-data",
Content => $data
);
if ( $res->is_success ) {
return decode_json( $res->decoded_content );
}
else {
die("Error requesting /fs/upload.");
}
}
sub post {
my ( $self, $service, $data, $header ) = @_;
$header ||= {};
$data ||= {};
if ( !ref($data) ) {
die(
"Invalid 2nd argument. must be arrayRef or hashRef!\npost(\$service, \$ref)"
);
}
my $res = $self->ua->post(
$self->_get_proto . "://"
. $self->{server} . ":"
. $self->{port}
. "$service",
%{$header},
Content => encode_json($data)
);
if ( $res->is_success ) {
return decode_json( $res->decoded_content );
}
else {
die( "Error requesting $service.\n\nError: " . $res->{_content} );
}
}
sub get {
my ( $self, $service ) = @_;
my $res =
$self->ua->get( $self->_get_proto . "://"
. $self->{server} . ":"
. $self->{port}
. "$service" );
if ( $res->is_success ) {
return decode_json( $res->decoded_content );
}
else {
die("Error requesting $service.");
}
}
sub get_connection_type { return "HTTP"; }
sub _get_proto {
return "http";
}
sub _get_port {
my ( $self, $port ) = @_;
return $port || 8080;
}
1;
|