Net-CVE
view release on metacpan - search on metacpan
view release on metacpan or search on metacpan
lib/Net/CVE.pm view on Meta::CPAN
#!/usr/bin/perl
package Net::CVE;
use 5.014002;
use warnings;
our $VERSION = "0.009";
use Carp;
use HTTP::Tiny;
use JSON::MaybeXS;
use List::Util qw( first );
# https://cveawg.mitre.org/api/cve/CVE-2022-26928
# But that is likely to change to cve.org
sub Version { $VERSION }
sub new {
my $class = shift;
my %r = (
url => "https://cveawg.mitre.org/api/cve",
ua => undef,
lang => "en",
data => {},
diag => undef,
);
if (@_) {
if (@_ == 1 && ref $_[0] eq "HASH") {
$r{$_} = $_[0]{$_} for keys %{$_[0]};
}
elsif (@_ == 2) {
my %p = @_;
$r{$_} = $p{$_} for keys %p;
}
}
bless \%r => $class;
} # new
sub diag {
my $self = shift or return;
ref $self or return;
my $d = $self->{diag} or return;
unless (defined wantarray) { # void context
my $act = $d->{action};
warn "$act: ",
(join " " => grep { length } $d->{status}, $d->{reason}), "\n";
$act =~ s/./ /g;
warn "$act source = $d->{source}\n" if $d->{source};
warn "$act usage: $d->{usage}\n" if $d->{usage};
}
return $d;
} # diag
sub get {
my ($self, $cve) = @_;
ref $self or $self = __PACKAGE__->new ();
$self->{data} = {};
$self->{diag} = undef;
$cve or return $self;
$cve =~ s/^(?=[0-9])/CVE-/;
if ($cve =~ m/^CVE-[0-9]{4}-([0-9]+)$/) {
$self->{ua} //= HTTP::Tiny->new ();
my $url = join "/" => $self->{url}, $cve;
my $r = $self->{ua}->get ($url);
unless ($r->{success}) {
# if pseudo-HTTP status code 599 and reason "Internal Exception"
# the content field will contain the text of the error
my $status = $r->{status};
my $reason = join ": " => grep { length }
$r->{reason}, $status =~ m/^5[0-9][0-9]$/ ? $r->{content} : "";
$self->{diag} = {
status => $status,
reason => $reason,
action => "get",
source => $url,
usage => undef,
};
return $self;
}
$self->{data} = decode_json ($r->{content});
}
elsif (-s $cve) {
my $fh;
unless (open $fh, "<:encoding(utf-8)", $cve) {
$self->{diag} = {
status => 0 + $!,
reason => "$!",
action => "get",
source => $cve,
usage => 'get ("cve-2022-26928.json")',
};
return $self;
}
unless (eval { $self->{data} = decode_json (do { local $/; <$fh> }); 1 }) {
$self->{diag} = {
status => -2,
view all matches for this distributionview release on metacpan - search on metacpan
( run in 1.269 second using v1.00-cache-2.02-grep-82fe00e-cpan-cec75d87357c )