lib/Rex/Interface/Connection/HTTP.pm

filter: Statementstmt 21.9% 18 / 82 Branchbran 0.0% 0 / 16 Conditioncond 0.0% 0 / 12 MC/DCmcdc 0.0% 0 / 7 Subroutinesub 27.2% 6 / 22 Podpod 0.0% 0 / 14 totaltotal 15.6% 24 / 153 CC 14 SCAR 23.9
CC
14
Cov
16%
connect
30.0
post
30.0
new
6.0
CRAP 10.9
« lib/Rex/Commands/Upload.pm ↑ Summary lib/Rex/Virtualization/Lxc/destroy.pm »
1 #
2 # (c) Jan Gehring <jan.gehring@gmail.com>
3 #
4  
5 package Rex::Interface::Connection::HTTP;
6  
7 1 ▶use v5.14.4;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 1 our $VERSION = '1.16.1'; # VERSION
11  
12 1 ▶use Rex::Interface::Connection::Base;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 1 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 LWP::UserAgent->use;
16 1 JSON::MaybeXS->use;
17 }
18  
19 1 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 1 ▶use base qw(Rex::Interface::Connection::Base);
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 0 ▶sub new {
Subroutine new: not called CC=2 SCAR=17.9
Pod undocumented
24 0 my $that = shift;
25 0 ▶ my $proto = ref($that) || $that;
Condition0%0/3
ref $that || $that
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $that || $that
ref $that $that
Truth table0%0/3
A || B
A= ref $thatB= $that
ABresult
1X1
011
000
26 0 my $self = $that->SUPER::new(@_);
27  
28 0 bless( $self, $proto );
29  
30 # activate caching
31 0 Rex::Config->set_use_cache(1);
32  
33 0 return $self;
34 }
35  
36 0 ▶sub error { }
Subroutine error: not called CC=1 SCAR=0.0
Pod undocumented
37  
38 0 ▶sub connect {
Subroutine connect: not called CC=5 SCAR=34.0
Pod undocumented
39 0 my ( $self, %option ) = @_;
40 0 my ( $user, $pass, $server, $port, $timeout );
41  
42 0 $user = $option{user};
43 0 $pass = $option{password};
44 0 $server = $option{server};
45 0 $port = $self->_get_port( $option{port} );
46 0 $timeout = $option{timeout};
47  
48 0 $self->{server} = $server;
49 0 $self->{port} = $port;
50  
51 0 ▶ if ( $server =~ m/([^:]+):(\d+)/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($server =~ /([^:]+):(\d+)/u) 0 0 0
52 0 $server = $self->{server} = $1;
53 0 $port = $self->{port} = $2;
54 }
55  
56 0 $self->{__user} = $user;
57 0 $self->{__pass} = $pass;
58  
59 0 ▶ if (!Rex::Config->has_user
Branch: 1 branch
BranchTrueFalseTotal
if (not "Rex::Config"->has_user and "Rex::Config"->get_ssh_config_username("server", $server)) 0 0 0
Condition0%0/3
not "Rex::Config"->has_user and "Rex::Config"->get_ssh_config_username("server", $server)
!ll&&!rl&&r
000
MC/DC0%0/2
not "Rex::Config"->has_user and "Rex::Config"->get_ssh_config_username("server", $server)
not "Rex::Config"->has_user "Rex::Config"->get_ssh_config_username("server", $server)
Truth table0%0/3
not(A) and B
A= not "Rex::Config"->has_userB= "Rex::Config"->get_ssh_config_username("server", $server)
ABresult
0X0
100
111
60 && Rex::Config->get_ssh_config_username( server => $server ) )
61 {
62 0 $user = Rex::Config->get_ssh_config_username( server => $server );
63 }
64  
65 0 $self->ua->credentials( "$server:$port", "Rex::Endpoint::HTTP",
66 $user => $pass, );
67  
68 0 my $resp = $self->post("/login");
69 0 ▶ if ( $resp->{ok} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($resp->{'ok'}) { } 0 0 0
70 0 Rex::Logger::info("Connected to $server, trying to authenticate.");
71 }
72 else {
73 0 Rex::Logger::info( "Can't connect to $server", "warn" );
74 0 $self->{connected} = 0;
75 0 return;
76 }
77  
78 0 Rex::Logger::info( "Connecting to $server:$port (" . $user . ")" );
79  
80 }
81  
82 0 ▶sub disconnect { }
Subroutine disconnect: not called CC=1 SCAR=0.0
Pod undocumented
83 0 ▶sub get_connection_object { my ($self) = @_; return $self; }
Subroutine get_connection_object: not called CC=1 SCAR=6.9
Pod undocumented
84 0 ▶sub get_fs_connection_object { my ($self) = @_; return $self; }
Subroutine get_fs_connection_object: not called CC=1 SCAR=6.9
Pod undocumented
85 0 ▶sub is_connected { return 1; }
Subroutine is_connected: not called CC=1 SCAR=6.9
Pod undocumented
86 0 ▶sub is_authenticated { return 1; }
Subroutine is_authenticated: not called CC=1 SCAR=6.9
Pod undocumented
87  
88 0 ▶sub exec {
Subroutine exec: not called CC=1 SCAR=6.9
Pod undocumented
89 0 my ( $self, $cmd ) = @_;
90 }
91  
92 0 ▶sub ua {
Subroutine ua: not called CC=2 SCAR=17.9
Pod undocumented
93 0 my ($self) = @_;
94 0 ▶ return $self->{ua} if $self->{ua};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"ua"} 0 0 0
95  
96 0 $self->{ua} = LWP::UserAgent->new;
97 }
98  
99 0 ▶sub upload {
Subroutine upload: not called CC=2 SCAR=17.9
Pod undocumented
100 0 my ( $self, $data ) = @_;
101  
102 my $res = $self->ua->post(
103 $self->_get_proto . "://"
104 . $self->{server} . ":"
105 . $self->{port}
106 0 . "/fs/upload",
107 Content_Type => "multipart/form-data",
108 Content => $data
109 );
110  
111 0 ▶ if ( $res->is_success ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->is_success) { } 0 0 0
112 0 return decode_json( $res->decoded_content );
113 }
114 else {
115 0 die("Error requesting /fs/upload.");
116 }
117 }
118  
119 0 ▶sub post {
Subroutine post: not called CC=5 SCAR=34.0
Pod undocumented
120 0 my ( $self, $service, $data, $header ) = @_;
121  
122 0 ▶ $header ||= {};
Condition0%0/2
$header ||= {}
l!l
00
MC/DC0%0/1
$header ||= {}
$header
Truth table0%0/2
A
A= $header
Aresult
11
00
123 0 ▶ $data ||= {};
Condition0%0/2
$data ||= {}
l!l
00
MC/DC0%0/1
$data ||= {}
$data
Truth table0%0/2
A
A= $data
Aresult
11
00
124  
125 0 ▶ if ( !ref($data) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $data) 0 0 0
126 0 die(
127 "Invalid 2nd argument. must be arrayRef or hashRef!\npost(\$service, \$ref)"
128 );
129 }
130  
131 my $res = $self->ua->post(
132 $self->_get_proto . "://"
133 . $self->{server} . ":"
134 . $self->{port}
135 . "$service",
136 0 %{$header},
137 Content => encode_json($data)
138 );
139  
140 0 ▶ if ( $res->is_success ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->is_success) { } 0 0 0
141 0 return decode_json( $res->decoded_content );
142 }
143 else {
144 0 die( "Error requesting $service.\n\nError: " . $res->{_content} );
145 }
146  
147 }
148  
149 0 ▶sub get {
Subroutine get: not called CC=2 SCAR=17.9
Pod undocumented
150 0 my ( $self, $service ) = @_;
151  
152 my $res =
153 $self->ua->get( $self->_get_proto . "://"
154 . $self->{server} . ":"
155 . $self->{port}
156 0 . "$service" );
157  
158 0 ▶ if ( $res->is_success ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->is_success) { } 0 0 0
159 0 return decode_json( $res->decoded_content );
160 }
161 else {
162 0 die("Error requesting $service.");
163 }
164  
165 }
166  
167 0 ▶sub get_connection_type { return "HTTP"; }
Subroutine get_connection_type: not called CC=1 SCAR=6.9
Pod undocumented
168  
169 0 ▶sub _get_proto {
Subroutine _get_proto: not called CC=1 SCAR=6.9
170 0 return "http";
171 }
172  
173 0 ▶sub _get_port {
Subroutine _get_port: not called CC=2 SCAR=17.9
174 0 my ( $self, $port ) = @_;
175 0 ▶ return $port || 8080;
Condition0%0/2
$port || 8080
l!l
00
MC/DC0%0/1
$port || 8080
$port
Truth table0%0/2
A
A= $port
Aresult
11
00
176 }
177  
178 1 1;
« lib/Rex/Commands/Upload.pm ↑ Summary lib/Rex/Virtualization/Lxc/destroy.pm »