| 1 | package Zenoss::Connector; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 2 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use IO::Socket::INET; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 5 | 1 | ▶ | use Moose; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 6 | 1 | with 'Zenoss::Error'; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 8 | #************************************************************************** | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 9 | # Constants | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 10 | #************************************************************************** | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 11 | 1 | our $URL_REGEX; *URL_REGEX = | |||||||||||||||||||||||||||||||||||||||||||||||||
| 12 | \qr { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 13 | \b | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 14 | # Protocol | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 15 | (https?):// | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 17 | # URL | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 18 | ([-A-Z0-9.]+) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 20 | # Port | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 21 | (?::|)(\d+)? | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 22 | }ix; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 24 | #************************************************************************** | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 25 | # Attributes | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 26 | #************************************************************************** | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 27 | 1 | has 'username' => ( | |||||||||||||||||||||||||||||||||||||||||||||||||
| 28 | is => 'ro', | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 29 | isa => 'Str', | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 30 | required => 1, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 31 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 33 | 1 | has 'password' => ( | |||||||||||||||||||||||||||||||||||||||||||||||||
| 34 | is => 'ro', | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 35 | isa => 'Str', | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 36 | required => 1, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 37 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 39 | 1 | has 'url' => ( | |||||||||||||||||||||||||||||||||||||||||||||||||
| 40 | is => 'ro', | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 41 | isa => 'Str', | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 42 | required => 1, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 43 | trigger => \&_check_url, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 44 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 46 | 1 | has 'timeout' => ( | |||||||||||||||||||||||||||||||||||||||||||||||||
| 47 | is => 'ro', | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 48 | isa => 'Int', | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 49 | default => '120', | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 50 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 52 | 1 | has 'endpoint' => ( | |||||||||||||||||||||||||||||||||||||||||||||||||
| 53 | is => 'ro', | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 54 | isa => 'Str', | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 55 | writer => '_set_endpoint', | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 56 | init_arg => undef, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 57 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 59 | #************************************************************************** | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 60 | # _check_url | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 61 | #************************************************************************** | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 62 | 0 | ▶ | sub _check_url { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_check_url: not called CC=12 SCAR=50.5
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 63 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 64 | 0 | my $url = $self->url; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 65 | 0 | my $timeout = $self->timeout; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 67 | 0 | my $matched_protocol; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 68 | my $matched_host; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 69 | 0 | my $matched_port; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 70 | 0 | my $actual_port; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 72 | # Parse the URL to check correctness | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 73 | 0 | ▶ | if ($url =~ m/$URL_REGEX/) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 74 | 0 | $matched_protocol = $1; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 75 | 0 | $matched_host = $2; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 76 | 0 | $matched_port = $3; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 78 | # Check if we found a host | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 79 | 0 | ▶ | if (!defined($matched_host)) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 80 | 0 | $self->_croak("Unable to determine host from URL [$url]"); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 83 | # Store the port or default to 80 | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 84 | 0 | ▶ | if ((defined($matched_protocol) && $matched_protocol eq 'https') && !defined($matched_port)) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
Condition0%0/6
defined $matched_protocol and $matched_protocol eq "https" and not defined $matched_port
defined $matched_protocol and $matched_protocol eq "https"
MC/DC0%0/3
defined $matched_protocol and $matched_protocol eq "https" and not defined $matched_port
defined $matched_protocol
$matched_protocol eq "https"
not defined $matched_port
Truth table0%0/4
A and B and not(C)
A= defined $matched_protocolB= $matched_protocol eq "https"C= not defined $matched_port
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 85 | 0 | $actual_port = 443; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 86 | ▶ | } elsif ((defined($matched_protocol) && $matched_protocol eq 'http') && !defined($matched_port)) { | |||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/6
defined $matched_protocol and $matched_protocol eq "http" and not defined $matched_port
defined $matched_protocol and $matched_protocol eq "http"
MC/DC0%0/3
defined $matched_protocol and $matched_protocol eq "http" and not defined $matched_port
defined $matched_protocol
$matched_protocol eq "http"
not defined $matched_port
Truth table0%0/4
A and B and not(C)
A= defined $matched_protocolB= $matched_protocol eq "http"C= not defined $matched_port
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 87 | 0 | $actual_port = 80; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 88 | } elsif (defined($matched_port)) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 89 | 0 | $actual_port = $matched_port; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 90 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 91 | 0 | $self->_croak("Unable to determine the appropriate port from URL [$url]"); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 93 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 94 | 0 | $self->_croak("Unable to parse URL [$url]"); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 97 | # Test connection | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 98 | 0 | my $success = eval { | |||||||||||||||||||||||||||||||||||||||||||||||||
| 99 | 0 | my $socket = IO::Socket::INET->new( | |||||||||||||||||||||||||||||||||||||||||||||||||
| 100 | PeerAddr => $matched_host, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 101 | PeerPort => $actual_port, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 102 | Timeout => $timeout, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 103 | Proto => 'tcp', | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 104 | ) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 105 | }; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 107 | # If successful set the endpoint | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 108 | 0 | ▶ | if (!$success) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 109 | 0 | $self->_croak("Unable to establish connection to host [$matched_host] using port [$actual_port]"); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 110 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 111 | 0 | ▶ | if (defined($matched_port)) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 112 | 0 | $self->_set_endpoint(sprintf("%s://%s:%s\@%s:%s", $matched_protocol, $self->username, $self->password, $matched_host, $matched_port)); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 113 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 114 | 0 | $self->_set_endpoint(sprintf("%s://%s:%s\@%s", $matched_protocol, $self->username, $self->password, $matched_host)); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 116 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 117 | } # END _check_url | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 119 | #************************************************************************** | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 120 | # Package end | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 121 | #************************************************************************** | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 122 | 1 | __PACKAGE__->meta->make_immutable; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 123 | 1 | ▶ | no Moose; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 125 | 1 | 1; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 127 | __END__ | ||||||||||||||||||||||||||||||||||||||||||||||||||