| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package WebService::SSLLabs; | 
| 2 |  |  |  |  |  |  |  | 
| 3 | 3 |  |  | 3 |  | 198422 | use strict; | 
|  | 3 |  |  |  |  | 22 |  | 
|  | 3 |  |  |  |  | 86 |  | 
| 4 | 3 |  |  | 3 |  | 15 | use warnings; | 
|  | 3 |  |  |  |  | 5 |  | 
|  | 3 |  |  |  |  | 69 |  | 
| 5 | 3 |  |  | 3 |  | 1874 | use JSON(); | 
|  | 3 |  |  |  |  | 36071 |  | 
|  | 3 |  |  |  |  | 72 |  | 
| 6 | 3 |  |  | 3 |  | 1255 | use URI::Escape(); | 
|  | 3 |  |  |  |  | 3882 |  | 
|  | 3 |  |  |  |  | 84 |  | 
| 7 | 3 |  |  | 3 |  | 1924 | use LWP::UserAgent(); | 
|  | 3 |  |  |  |  | 132086 |  | 
|  | 3 |  |  |  |  | 76 |  | 
| 8 | 3 |  |  | 3 |  | 1374 | use WebService::SSLLabs::Info(); | 
|  | 3 |  |  |  |  | 9 |  | 
|  | 3 |  |  |  |  | 57 |  | 
| 9 | 3 |  |  | 3 |  | 1284 | use WebService::SSLLabs::Host(); | 
|  | 3 |  |  |  |  | 7 |  | 
|  | 3 |  |  |  |  | 61 |  | 
| 10 | 3 |  |  | 3 |  | 21 | use WebService::SSLLabs::Endpoint(); | 
|  | 3 |  |  |  |  | 4 |  | 
|  | 3 |  |  |  |  | 43 |  | 
| 11 | 3 |  |  | 3 |  | 1271 | use WebService::SSLLabs::StatusCodes(); | 
|  | 3 |  |  |  |  | 7 |  | 
|  | 3 |  |  |  |  | 3533 |  | 
| 12 |  |  |  |  |  |  |  | 
| 13 |  |  |  |  |  |  | our $VERSION = '0.31'; | 
| 14 |  |  |  |  |  |  |  | 
| 15 | 0 |  |  | 0 |  | 0 | sub _MINIMUM_ETA_TIME { return 10; } | 
| 16 |  |  |  |  |  |  |  | 
| 17 |  |  |  |  |  |  | sub new { | 
| 18 | 2 |  |  | 2 | 1 | 163 | my ($class) = @_; | 
| 19 | 2 |  |  |  |  | 6 | my $self = {}; | 
| 20 | 2 |  |  |  |  | 5 | bless $self, $class; | 
| 21 | 2 |  |  |  |  | 10 | $self->{url} = 'https://api.ssllabs.com/api/v2/'; | 
| 22 | 2 |  |  |  |  | 13 | $self->{ua}  = LWP::UserAgent->new(); | 
| 23 | 2 |  |  |  |  | 5874 | $self->{ua}->env_proxy(); | 
| 24 | 2 |  |  |  |  | 28248 | return $self; | 
| 25 |  |  |  |  |  |  | } | 
| 26 |  |  |  |  |  |  |  | 
| 27 |  |  |  |  |  |  | sub _parse_success { | 
| 28 | 0 |  |  | 0 |  | 0 | my ( $self, $response ) = @_; | 
| 29 |  |  |  |  |  |  | $self->{max_assessments} = | 
| 30 | 0 |  |  |  |  | 0 | $response->headers()->header('X-Max-Assessments'); | 
| 31 |  |  |  |  |  |  | $self->{current_assessments} = | 
| 32 | 0 |  |  |  |  | 0 | $response->headers()->header('X-Current-Assessments'); | 
| 33 | 0 |  |  |  |  | 0 | return; | 
| 34 |  |  |  |  |  |  | } | 
| 35 |  |  |  |  |  |  |  | 
| 36 |  |  |  |  |  |  | sub max_assessments { | 
| 37 | 0 |  |  | 0 | 1 | 0 | my ($self) = @_; | 
| 38 | 0 |  |  |  |  | 0 | return $self->{max_assessments}; | 
| 39 |  |  |  |  |  |  | } | 
| 40 |  |  |  |  |  |  |  | 
| 41 |  |  |  |  |  |  | sub current_assessments { | 
| 42 | 0 |  |  | 0 | 1 | 0 | my ($self) = @_; | 
| 43 | 0 |  |  |  |  | 0 | return $self->{current_assessments}; | 
| 44 |  |  |  |  |  |  | } | 
| 45 |  |  |  |  |  |  |  | 
| 46 |  |  |  |  |  |  | sub info { | 
| 47 | 1 |  |  | 1 | 1 | 10 | my ($self)   = @_; | 
| 48 | 1 |  |  |  |  | 4 | my $url      = $self->{url} . 'info'; | 
| 49 | 1 |  |  |  |  | 5 | my $response = $self->{ua}->get($url); | 
| 50 | 1 | 50 |  |  |  | 12241 | if ( $response->is_success() ) { | 
| 51 | 0 |  |  |  |  | 0 | $self->_parse_success($response); | 
| 52 | 0 |  |  |  |  | 0 | return WebService::SSLLabs::Info->new( | 
| 53 |  |  |  |  |  |  | JSON::decode_json( $response->decoded_content() ) ); | 
| 54 |  |  |  |  |  |  | } | 
| 55 |  |  |  |  |  |  | else { | 
| 56 | 1 |  |  |  |  | 16 | Carp::croak( "Failed to retrieve $url:" . $response->status_line() ); | 
| 57 |  |  |  |  |  |  | } | 
| 58 |  |  |  |  |  |  | } | 
| 59 |  |  |  |  |  |  |  | 
| 60 |  |  |  |  |  |  | sub _translate_params { | 
| 61 | 17 |  |  | 17 |  | 47 | my ( $self, %params ) = @_; | 
| 62 | 17 |  |  |  |  | 29 | my %translated_params; | 
| 63 | 17 |  |  |  |  | 80 | foreach my $key ( sort { $a cmp $b } sort keys %params ) { | 
|  | 19 |  |  |  |  | 51 |  | 
| 64 | 34 | 50 |  |  |  | 80 | if ( defined $params{$key} ) { | 
| 65 | 34 |  |  |  |  | 51 | my $translated_key = $key; | 
| 66 | 34 |  |  |  |  | 104 | $translated_key =~ s/_([[:lower:]])/uc $1/egsmx; | 
|  | 11 |  |  |  |  | 47 |  | 
| 67 | 34 |  |  |  |  | 94 | $translated_params{$translated_key} = $params{$key}; | 
| 68 |  |  |  |  |  |  | } | 
| 69 |  |  |  |  |  |  | } | 
| 70 | 17 |  |  |  |  | 72 | return %translated_params; | 
| 71 |  |  |  |  |  |  | } | 
| 72 |  |  |  |  |  |  |  | 
| 73 |  |  |  |  |  |  | sub analyze { | 
| 74 | 15 |  |  | 15 | 1 | 9222 | my ( $self, %params ) = @_; | 
| 75 | 15 |  |  |  |  | 87 | my %translated_params = $self->_translate_params(%params); | 
| 76 |  |  |  |  |  |  | my $url               = $self->{url} . 'analyze?' . ( | 
| 77 |  |  |  |  |  |  | join q[&], | 
| 78 |  |  |  |  |  |  | map { | 
| 79 | 15 |  |  |  |  | 81 | URI::Escape::uri_escape_utf8($_) . q[=] | 
| 80 | 28 |  |  |  |  | 491 | . URI::Escape::uri_escape_utf8( $translated_params{$_} ) | 
| 81 |  |  |  |  |  |  | } sort _sort_ssllabs_params keys %translated_params | 
| 82 |  |  |  |  |  |  | ); | 
| 83 | 15 |  |  |  |  | 485 | my $response = $self->{ua}->get($url); | 
| 84 | 15 | 50 |  |  |  | 2037 | if ( $response->is_success() ) { | 
| 85 | 0 |  |  |  |  | 0 | $self->_parse_success($response); | 
| 86 | 0 |  |  |  |  | 0 | my $host = WebService::SSLLabs::Host->new( | 
| 87 |  |  |  |  |  |  | JSON::decode_json( $response->decoded_content() ) ); | 
| 88 | 0 |  |  |  |  | 0 | $self->{_previous_host} = $host; | 
| 89 | 0 |  |  |  |  | 0 | return $host; | 
| 90 |  |  |  |  |  |  | } | 
| 91 |  |  |  |  |  |  | else { | 
| 92 | 15 |  |  |  |  | 139 | Carp::croak( "Failed to retrieve $url:" . $response->status_line() ); | 
| 93 |  |  |  |  |  |  | } | 
| 94 | 0 |  |  |  |  | 0 | return; | 
| 95 |  |  |  |  |  |  | } | 
| 96 |  |  |  |  |  |  |  | 
| 97 |  |  |  |  |  |  | sub previous_eta { | 
| 98 | 0 |  |  | 0 | 1 | 0 | my ($self) = @_; | 
| 99 | 0 |  |  |  |  | 0 | my $eta = _MINIMUM_ETA_TIME(); | 
| 100 | 0 | 0 |  |  |  | 0 | if ( $self->{_previous_host} ) { | 
| 101 | 0 |  |  |  |  | 0 | my $host_eta = $self->{_previous_host}->eta(); | 
| 102 | 0 | 0 | 0 |  |  | 0 | if (   ( defined $host_eta ) | 
|  |  |  | 0 |  |  |  |  | 
| 103 |  |  |  |  |  |  | && ( $host_eta =~ /^\d+$/smx ) | 
| 104 |  |  |  |  |  |  | && ( $host_eta >= $eta ) ) | 
| 105 |  |  |  |  |  |  | { | 
| 106 | 0 |  |  |  |  | 0 | $eta = $host_eta; | 
| 107 |  |  |  |  |  |  | } | 
| 108 |  |  |  |  |  |  | } | 
| 109 | 0 |  |  |  |  | 0 | return $eta; | 
| 110 |  |  |  |  |  |  | } | 
| 111 |  |  |  |  |  |  |  | 
| 112 |  |  |  |  |  |  | sub _sort_ssllabs_params { | 
| 113 | 19 | 100 |  | 19 |  | 55 | if ( $a eq 'host' ) { | 
|  |  | 100 |  |  |  |  |  | 
| 114 | 10 |  |  |  |  | 23 | return -1; | 
| 115 |  |  |  |  |  |  | } | 
| 116 |  |  |  |  |  |  | elsif ( $b eq 'host' ) { | 
| 117 | 7 |  |  |  |  | 20 | return 1; | 
| 118 |  |  |  |  |  |  | } | 
| 119 | 2 | 50 |  |  |  | 7 | if ( $a eq 's' ) { | 
|  |  | 50 |  |  |  |  |  | 
| 120 | 0 |  |  |  |  | 0 | return -1; | 
| 121 |  |  |  |  |  |  | } | 
| 122 |  |  |  |  |  |  | elsif ( $b eq 's' ) { | 
| 123 | 2 |  |  |  |  | 5 | return 1; | 
| 124 |  |  |  |  |  |  | } | 
| 125 |  |  |  |  |  |  | else { | 
| 126 | 0 |  |  |  |  | 0 | return $a cmp $b; | 
| 127 |  |  |  |  |  |  | } | 
| 128 |  |  |  |  |  |  | } | 
| 129 |  |  |  |  |  |  |  | 
| 130 |  |  |  |  |  |  | sub get_endpoint_data { | 
| 131 | 2 |  |  | 2 | 1 | 937 | my ( $self, %params ) = @_; | 
| 132 | 2 |  |  |  |  | 10 | my %translated_params = $self->_translate_params(%params); | 
| 133 |  |  |  |  |  |  | my $url               = $self->{url} . 'getEndpointData?' . ( | 
| 134 |  |  |  |  |  |  | join q[&], | 
| 135 |  |  |  |  |  |  | map { | 
| 136 | 2 |  |  |  |  | 13 | URI::Escape::uri_escape_utf8($_) . q[=] | 
| 137 | 6 |  |  |  |  | 127 | . URI::Escape::uri_escape_utf8( $translated_params{$_} ) | 
| 138 |  |  |  |  |  |  | } sort _sort_ssllabs_params keys %translated_params | 
| 139 |  |  |  |  |  |  | ); | 
| 140 | 2 |  |  |  |  | 62 | my $response = $self->{ua}->get($url); | 
| 141 | 2 | 50 |  |  |  | 94 | if ( $response->is_success() ) { | 
| 142 | 0 |  |  |  |  | 0 | $self->_parse_success($response); | 
| 143 | 0 |  |  |  |  | 0 | return WebService::SSLLabs::Endpoint->new( | 
| 144 |  |  |  |  |  |  | JSON::decode_json( $response->decoded_content() ) ); | 
| 145 |  |  |  |  |  |  | } | 
| 146 |  |  |  |  |  |  | else { | 
| 147 | 2 |  |  |  |  | 17 | Carp::croak( "Failed to retrieve $url:" . $response->status_line() ); | 
| 148 |  |  |  |  |  |  | } | 
| 149 |  |  |  |  |  |  | } | 
| 150 |  |  |  |  |  |  |  | 
| 151 |  |  |  |  |  |  | sub get_status_codes { | 
| 152 | 1 |  |  | 1 | 1 | 11868 | my ($self)   = @_; | 
| 153 | 1 |  |  |  |  | 4 | my $url      = $self->{url} . 'getStatusCodes'; | 
| 154 | 1 |  |  |  |  | 4 | my $response = $self->{ua}->get($url); | 
| 155 | 1 | 50 |  |  |  | 1397 | if ( $response->is_success() ) { | 
| 156 | 0 |  |  |  |  | 0 | $self->_parse_success($response); | 
| 157 | 0 |  |  |  |  | 0 | return WebService::SSLLabs::StatusCodes->new( | 
| 158 |  |  |  |  |  |  | JSON::decode_json( $response->decoded_content() ) ); | 
| 159 |  |  |  |  |  |  | } | 
| 160 |  |  |  |  |  |  | else { | 
| 161 | 1 |  |  |  |  | 11 | Carp::croak( "Failed to retrieve $url:" . $response->status_line() ); | 
| 162 |  |  |  |  |  |  | } | 
| 163 |  |  |  |  |  |  | } | 
| 164 |  |  |  |  |  |  |  | 
| 165 |  |  |  |  |  |  | sub get_root_certs_raw { | 
| 166 | 0 |  |  | 0 | 1 |  | my ($self)   = @_; | 
| 167 | 0 |  |  |  |  |  | my $url      = $self->{url} . 'getRootCertsRaw'; | 
| 168 | 0 |  |  |  |  |  | my $response = $self->{ua}->get($url); | 
| 169 | 0 | 0 |  |  |  |  | if ( $response->is_success() ) { | 
| 170 | 0 |  |  |  |  |  | $self->_parse_success($response); | 
| 171 | 0 |  |  |  |  |  | return $response->decoded_content(); | 
| 172 |  |  |  |  |  |  | } | 
| 173 |  |  |  |  |  |  | else { | 
| 174 | 0 |  |  |  |  |  | Carp::croak( "Failed to retrieve $url:" . $response->status_line() ); | 
| 175 |  |  |  |  |  |  | } | 
| 176 |  |  |  |  |  |  | } | 
| 177 |  |  |  |  |  |  |  | 
| 178 |  |  |  |  |  |  | 1;    # End of WebService::SSLLabs | 
| 179 |  |  |  |  |  |  | __END__ |