| 1 | package Business::MaxMind::HTTPBase; | ||||||||||||
| 2 | |||||||||||||
| 3 | 1 | ▶ | use 5.006; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 4 | |||||||||||||
| 5 | 1 | ▶ | use strict; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 6 | |||||||||||||
| 7 | 1 | ▶ | use LWP::Protocol::https; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 8 | 1 | ▶ | use LWP::UserAgent; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 9 | 1 | ▶ | use URI::Escape; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 10 | |||||||||||||
| 11 | 1 | our $VERSION = '1.60'; | |||||||||||
| 12 | |||||||||||||
| 13 | 1 | my $API_VERSION = join( '/', 'Perl', $VERSION ); | |||||||||||
| 14 | |||||||||||||
| 15 | # default minfraud servers | ||||||||||||
| 16 | 1 | my @defaultservers = ( | |||||||||||
| 17 | 'minfraud.maxmind.com', 'minfraud-us-east.maxmind.com', | ||||||||||||
| 18 | 'minfraud-us-west.maxmind.com' | ||||||||||||
| 19 | ); | ||||||||||||
| 20 | |||||||||||||
| 21 | 2 | ▶ | sub new { | ||||||||||
|
Subroutine
new: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||
| 22 | 2 | my $i = 0; | |||||||||||
| 23 | 2 | my ($class) = shift; | |||||||||||
| 24 | 2 | ▶ | if ( $class eq 'Business::MaxMind::HTTPBase' ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 25 | 0 | die | |||||||||||
| 26 | "Business::MaxMind::HTTPBase is an abstract class - use a subclass instead"; | ||||||||||||
| 27 | } | ||||||||||||
| 28 | 2 | my $self = {@_}; | |||||||||||
| 29 | 2 | bless $self, $class; | |||||||||||
| 30 | 2 | ▶ | $self->{isSecure} = 1 unless exists $self->{isSecure}; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 31 | 2 | for my $server (@defaultservers) { | |||||||||||
| 32 | 6 | $self->{servers}->[$i] = $server; | |||||||||||
| 33 | 6 | $i++; | |||||||||||
| 34 | } | ||||||||||||
| 35 | 2 | $self->{ua} = LWP::UserAgent->new( ssl_opts => { verify_hostname => 0 } ); | |||||||||||
| 36 | 2 | $self->_init; | |||||||||||
| 37 | 2 | return $self; | |||||||||||
| 38 | } | ||||||||||||
| 39 | |||||||||||||
| 40 | 0 | ▶ | sub getServers { | ||||||||||
|
Subroutine
getServers: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||
| 41 | 0 | ▶ | return [ @{ $_[0]->{servers} || [] } ]; | ||||||||||
|
Condition0%0/2
$_[0]{'servers'} || []
MC/DC0%0/1
$_[0]{'servers'} || []
$_[0]{'servers'}
Truth table0%0/2
A
A= $_[0]{'servers'}
| |||||||||||||
| 42 | } | ||||||||||||
| 43 | |||||||||||||
| 44 | 0 | ▶ | sub setServers { | ||||||||||
|
Subroutine
setServers: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||
| 45 | 0 | my ( $self, $serverarrayref ) = @_; | |||||||||||
| 46 | 0 | $self->{servers} = [@$serverarrayref]; | |||||||||||
| 47 | } | ||||||||||||
| 48 | |||||||||||||
| 49 | 0 | ▶ | sub query { | ||||||||||
|
Subroutine
query: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||
| 50 | 0 | my ($self) = @_; | |||||||||||
| 51 | 0 | my $s = $self->{servers}; | |||||||||||
| 52 | 0 | my $datetime; | |||||||||||
| 53 | |||||||||||||
| 54 | 0 | for my $server (@$s) { | |||||||||||
| 55 | 0 | my $result = $self->querySingleServer($server); | |||||||||||
| 56 | 0 | ▶ | return $result if $result; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 57 | } | ||||||||||||
| 58 | 0 | return 0; | |||||||||||
| 59 | } | ||||||||||||
| 60 | |||||||||||||
| 61 | 0 | ▶ | sub input { | ||||||||||
|
Subroutine
input: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||
| 62 | 0 | my $self = shift; | |||||||||||
| 63 | 0 | my %vars = @_; | |||||||||||
| 64 | 0 | ▶ | while ( my ( $k, $v ) = each %vars ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 65 | 0 | ▶ | unless ( exists $self->{allowed_fields}->{$k} ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 66 | 0 | die "invalid input $k - perhaps misspelled field?"; | |||||||||||
| 67 | } | ||||||||||||
| 68 | 0 | $self->{queries}->{$k} = $self->filter_field( $k, $v ); | |||||||||||
| 69 | } | ||||||||||||
| 70 | } | ||||||||||||
| 71 | |||||||||||||
| 72 | # sub-class should override this if it needs to filter inputs | ||||||||||||
| 73 | 0 | ▶ | sub filter_field { | ||||||||||
|
Subroutine
filter_field: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||
| 74 | 0 | my ( $self, $name, $value ) = @_; | |||||||||||
| 75 | 0 | return $value; | |||||||||||
| 76 | } | ||||||||||||
| 77 | |||||||||||||
| 78 | 0 | ▶ | sub output { | ||||||||||
|
Subroutine
output: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||
| 79 | 0 | my $self = shift; | |||||||||||
| 80 | 0 | return $self->{output}; | |||||||||||
| 81 | } | ||||||||||||
| 82 | |||||||||||||
| 83 | # if possible send the escaped string as latin1 for backward compatibility. | ||||||||||||
| 84 | # That makes a difference for chars 128..255 | ||||||||||||
| 85 | # otherwise use utf8 encoding. | ||||||||||||
| 86 | # | ||||||||||||
| 87 | 0 | ▶ | sub _mm_uri_escape { | ||||||||||
|
Subroutine
_mm_uri_escape: not called CC=3 SCAR=24.8
| |||||||||||||
| 88 | 0 | ▶ | return uri_escape( $_[0] ) if $] < 5.007; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 89 | 0 | ▶ | return utf8::downgrade( my $t = $_[0], 1 ) | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 90 | ? uri_escape( $_[0] ) | ||||||||||||
| 91 | : uri_escape_utf8( $_[0] ); | ||||||||||||
| 92 | } | ||||||||||||
| 93 | |||||||||||||
| 94 | 0 | ▶ | sub querySingleServer { | ||||||||||
|
Subroutine
querySingleServer: not called CC=8 SCAR=42.8
Pod
undocumented
| |||||||||||||
| 95 | 0 | my ( $self, $server ) = @_; | |||||||||||
| 96 | my $url | ||||||||||||
| 97 | = ( $self->{isSecure} ? 'https' : 'http' ) . '://' | ||||||||||||
| 98 | . $server . '/' | ||||||||||||
| 99 | 0 | ▶ | . $self->{url}; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 100 | 0 | my $check_field = $self->{check_field}; | |||||||||||
| 101 | 0 | my $queries = $self->{queries}; | |||||||||||
| 102 | my $query_string = join( | ||||||||||||
| 103 | '&', | ||||||||||||
| 104 | 0 | map { "$_=" . _mm_uri_escape( $queries->{$_} ) } keys %$queries | |||||||||||
| 105 | ); | ||||||||||||
| 106 | 0 | $query_string .= "&clientAPI=$API_VERSION"; | |||||||||||
| 107 | 0 | ▶ | if ( $self->{"timeout"} > 0 ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 108 | 0 | $self->{ua}->timeout( $self->{"timeout"} ); | |||||||||||
| 109 | } | ||||||||||||
| 110 | 0 | my $request = HTTP::Request->new( 'POST', $url ); | |||||||||||
| 111 | 0 | $request->content_type('application/x-www-form-urlencoded'); | |||||||||||
| 112 | 0 | $request->content($query_string); | |||||||||||
| 113 | 0 | ▶ | if ( $self->{debug} ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 114 | 0 | print STDERR "sending HTTP::Request: " . $request->as_string; | |||||||||||
| 115 | } | ||||||||||||
| 116 | 0 | my $response = $self->{ua}->request($request); | |||||||||||
| 117 | 0 | ▶ | if ( $response->is_success ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 118 | 0 | my $content = $response->content; | |||||||||||
| 119 | 0 | my @kvpair = split( ';', $content ); | |||||||||||
| 120 | 0 | my %output; | |||||||||||
| 121 | 0 | for my $kvp (@kvpair) { | |||||||||||
| 122 | 0 | my ( $key, $value ) = split( '=', $kvp, 2 ); | |||||||||||
| 123 | 0 | $output{$key} = $value; | |||||||||||
| 124 | } | ||||||||||||
| 125 | 0 | ▶ | unless ( exists $output{$check_field} ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 126 | 0 | return 0; | |||||||||||
| 127 | } | ||||||||||||
| 128 | 0 | $self->{output} = \%output; | |||||||||||
| 129 | 0 | return 1; | |||||||||||
| 130 | } | ||||||||||||
| 131 | else { | ||||||||||||
| 132 | 0 | ▶ | if ( $self->{debug} ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 133 | 0 | print STDERR "Error querying $server code: " . $response->code; | |||||||||||
| 134 | } | ||||||||||||
| 135 | 0 | return 0; | |||||||||||
| 136 | } | ||||||||||||
| 137 | } | ||||||||||||
| 138 | |||||||||||||
| 139 | 1 | 1; | |||||||||||
| 140 | |||||||||||||
| 141 | =pod | ||||||||||||
| 142 | |||||||||||||
| 143 | =head1 NAME | ||||||||||||
| 144 | |||||||||||||
| 145 | Business::MaxMind::HTTPBase - Base class for accessing HTTP web services | ||||||||||||
| 146 | |||||||||||||
| 147 | =head1 VERSION | ||||||||||||
| 148 | |||||||||||||
| 149 | version 1.60 | ||||||||||||
| 150 | |||||||||||||
| 151 | =head1 DESCRIPTION | ||||||||||||
| 152 | |||||||||||||
| 153 | This is an abstract base class for accessing MaxMind web services. | ||||||||||||
| 154 | |||||||||||||
| 155 | =head1 METHODS | ||||||||||||
| 156 | |||||||||||||
| 157 | =over 4 | ||||||||||||
| 158 | |||||||||||||
| 159 | =item new | ||||||||||||
| 160 | |||||||||||||
| 161 | Class method that returns a new object that is a subclass of Business::MaxMind::HTTPBase. | ||||||||||||
| 162 | Will die if you attempt to call this for the Business::MaxMind::HTTPBase class, instead | ||||||||||||
| 163 | you should call it on one of its subclasses. | ||||||||||||
| 164 | |||||||||||||
| 165 | =item input | ||||||||||||
| 166 | |||||||||||||
| 167 | Sets input fields. See subclass for details on fields that should be set. | ||||||||||||
| 168 | Returns 1 on success, 0 on failure. | ||||||||||||
| 169 | |||||||||||||
| 170 | =item query | ||||||||||||
| 171 | |||||||||||||
| 172 | Sends out query to MaxMind server and waits for response. If the primary | ||||||||||||
| 173 | server fails to respond, it sends out a request to the secondary server. | ||||||||||||
| 174 | Returns 1 on success, 0 on failure. | ||||||||||||
| 175 | |||||||||||||
| 176 | =item output | ||||||||||||
| 177 | |||||||||||||
| 178 | Returns the output returned by the MaxMind server as a hash reference. | ||||||||||||
| 179 | |||||||||||||
| 180 | =back | ||||||||||||
| 181 | |||||||||||||
| 182 | =head1 SEE ALSO | ||||||||||||
| 183 | |||||||||||||
| 184 | L<Business::MaxMind::CreditCardFraudDetection> | ||||||||||||
| 185 | |||||||||||||
| 186 | L<https://www.maxmind.com/en/minfraud-services> | ||||||||||||
| 187 | |||||||||||||
| 188 | =head1 AUTHORS | ||||||||||||
| 189 | |||||||||||||
| 190 | =over 4 | ||||||||||||
| 191 | |||||||||||||
| 192 | =item * | ||||||||||||
| 193 | |||||||||||||
| 194 | TJ Mather <tjmather@maxmind.com> | ||||||||||||
| 195 | |||||||||||||
| 196 | =item * | ||||||||||||
| 197 | |||||||||||||
| 198 | Frank Mather <frank@maxmind.com> | ||||||||||||
| 199 | |||||||||||||
| 200 | =back | ||||||||||||
| 201 | |||||||||||||
| 202 | =head1 COPYRIGHT AND LICENSE | ||||||||||||
| 203 | |||||||||||||
| 204 | This software is Copyright (c) 2015 by MaxMind, Inc.. | ||||||||||||
| 205 | |||||||||||||
| 206 | This is free software, licensed under: | ||||||||||||
| 207 | |||||||||||||
| 208 | The GNU General Public License, Version 2, June 1991 | ||||||||||||
| 209 | |||||||||||||
| 210 | =cut | ||||||||||||
| 211 | |||||||||||||
| 212 | __END__ | ||||||||||||