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
|
#!/usr/bin/perl
use strict;
use warnings;
use Data::Dumper;
use Getopt::Long;
use HTTP::Request;
use JSON;
use LWP::UserAgent;
my %command_line_options = (
'host:s' => \my $host,
'port:s' => \my $port,
'data:s' => \my $data,
);
GetOptions (%command_line_options);
if (!$host) { $host = 'localhost'; warn "using default: --host=$host\n"; };
if (!$port) { $port = '8080'; warn "using default: --port=$port\n"; };
if (!$data) { $data = get_json_request(); warn "using sample --data\n"; };
if ($data eq '-') {
$data = '';
while ($_ = <>) { chomp; $data .= $_; };
}
my $url = "http://$host:$port/dmarc/json/validate";
my $ua = LWP::UserAgent->new;
my $req = HTTP::Request->new(POST => $url);
$req->content_type('application/json');
$req->content($data);
my $response = $ua->request($req)->decoded_content;
#print Dumper($response); # raw JSON response
my $result;
eval { $result = JSON->new->utf8->decode($response) };
if ($result) {
print Dumper($result); # pretty formatted struct
exit;
};
die $response;
sub get_json_request {
return JSON->new->encode ({
source_ip => '192.0.1.1',
envelope_to => 'example.com',
envelope_from => 'cars4you.info',
header_from => 'yahoo.com',
dkim => [
{ domain => 'example.com',
selector => 'apr2013',
result => 'fail',
human_result => 'fail (body has been altered)',
}
],
spf => [
{ domain => 'example.com',
scope => 'mfrom',
result => 'pass',
}
],
});
};
__END__
=pod
=head1 NAME
dmarc_http_client - an HTTP client for submitting a DMARC validation request
=head1 SYNOPSIS
Send JSON encoded HTTP requests to the DMARC validation service provided by dmarc_httpd.
dmarc_http_client --host=localhost
--port=8080
--data='{"envelope_from":"cars4you.info"...}'
The data option accepts a special '-' value that will read the JSON encoded data from STDIN. Use it like this:
cat /path/to/data.json | dmarc_http_client --data=-
=head1 AUTHORS
=over 4
=item *
Matt Simerson <msimerson@cpan.org>
=item *
Davide Migliavacca <shari@cpan.org>
=item *
Marc Bradshaw <marc@marcbradshaw.net>
=back
=cut
|