GeoIP.pm

filter: Statementstmt 26.3% 19 / 72 Branchbran 0.0% 0 / 40 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 66.6% 6 / 9 Podpod 66.6% 2 / 3 totaltotal 20.1% 27 / 134 CC 26 SCAR 48.5
CC
26
Cov
16%
call
240.0
prepare_app
72.0
geoip_flag
30.0
CRAP 127.4
↑ Summary GeoIP2.pm »
1 package Plack::Middleware::GeoIP;
2 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use 5.008;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 our $VERSION = 0.05;
6 1 ▶use parent qw/Plack::Middleware/;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Geo::IP;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 1 ▶use Plack::Util::Accessor qw( GeoIPDBFile GeoIPEnableUTF8 );
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 0 ▶sub geoip_flag {
Subroutine geoip_flag: not called CC=5 SCAR=34.0
Pod undocumented
12 0 my $self = shift;
13 0 my $flag = shift;
14  
15 0 ▶ return GEOIP_MEMORY_CACHE if $flag eq 'MemoryCache';
Branch: 1 branch
BranchTrueFalseTotal
if $flag eq "MemoryCache" 0 0 0
16 0 ▶ return GEOIP_CHECK_CACHE if $flag eq 'CheckCache';
Branch: 1 branch
BranchTrueFalseTotal
if $flag eq "CheckCache" 0 0 0
17 0 ▶ return GEOIP_INDEX_CACHE if $flag eq 'IndexCache';
Branch: 1 branch
BranchTrueFalseTotal
if $flag eq "IndexCache" 0 0 0
18 0 ▶ return GEOIP_MMAP_CACHE if $flag eq 'MMapCache';
Branch: 1 branch
BranchTrueFalseTotal
if $flag eq "MMapCache" 0 0 0
19 0 return GEOIP_STANDARD;
20 }
21  
22 0 ▶sub prepare_app {
Subroutine prepare_app: not called CC=8 SCAR=42.8
Pod documented
23 0 my $self = shift;
24  
25 0 ▶ if (my $dbfiles = $self->GeoIPDBFile) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $dbfiles = $self->GeoIPDBFile) { } 0 0 0
26 0 ▶ my @dbfiles = ref $dbfiles ? @{ $dbfiles } : ($dbfiles);
Branch: 1 branch
BranchTrueFalseTotal
ref $dbfiles ? : 0 0 0
27 0 foreach my $dbfile (@dbfiles) {
28 0 ▶ my ($filename, $flag) = ref $dbfile ? @{ $dbfile } : ($dbfile, 'Standard');
Branch: 1 branch
BranchTrueFalseTotal
ref $dbfile ? : 0 0 0
29  
30 # combine flags
31 0 ▶ my @flags = ref $flag ? @{ $flag } : ($flag);
Branch: 1 branch
BranchTrueFalseTotal
ref $flag ? : 0 0 0
32 0 my $flags = GEOIP_STANDARD;
33 0 foreach my $f (@flags) {
34 0 $flags |= $self->geoip_flag($f);
35 }
36  
37 0 my $gi = Geo::IP->open($filename, $flags);
38 0 ▶ $gi->set_charset(GEOIP_CHARSET_UTF8) if $self->GeoIPEnableUTF8;
Branch: 1 branch
BranchTrueFalseTotal
if $self->GeoIPEnableUTF8 0 0 0
39 0 push @{ $self->{gips} }, $gi;
40 }
41 } else {
42 0 my $gi = Geo::IP->new(GEOIP_STANDARD);
43 0 push @{ $self->{gips} }, $gi;
44 }
45 }
46  
47 0 ▶sub call {
Subroutine call: not called CC=15 SCAR=54.8
Pod documented
48 0 my $self = shift;
49 0 my $env = shift;
50  
51 0 my $ipaddr = $env->{REMOTE_ADDR};
52  
53 0 foreach my $gi (@{ $self->{gips} }) {
54 0 my $type = $gi->database_edition;
55 0 ▶ if (GEOIP_COUNTRY_EDITION == $type) {
Branch: 2 branches
BranchTrueFalseTotal
if (1 == $type) { } 0 0 0
elsif (6 == $type or 2 == $type) { } 0 0 0
56 0 ▶ if (my $code = $gi->country_code_by_addr($ipaddr)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $code = $gi->country_code_by_addr($ipaddr)) 0 0 0
57 0 $env->{GEOIP_COUNTRY_CODE} = $code;
58 0 $env->{GEOIP_COUNTRY_CODE3} = $gi->country_code3_by_addr($ipaddr);
59 0 $env->{GEOIP_COUNTRY_NAME} = $gi->country_name_by_addr($ipaddr);
60 0 $env->{GEOIP_CONTINENT_CODE} = $gi->continent_code_by_country_code($code);
61 }
62 ▶ } elsif (GEOIP_CITY_EDITION_REV0 == $type or GEOIP_CITY_EDITION_REV1 == $type) {
Condition0%0/3
6 == $type or 2 == $type
l!l&&r!l&&!r
000
MC/DC0%0/2
6 == $type or 2 == $type
6 == $type 2 == $type
Truth table0%0/3
A or B
A= 6 == $typeB= 2 == $type
ABresult
1X1
011
000
63 0 ▶ if (my $record = $gi->record_by_addr($ipaddr)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $record = $gi->record_by_addr($ipaddr)) 0 0 0
64 0 $env->{GEOIP_COUNTRY_CODE} = $record->country_code;
65 0 $env->{GEOIP_COUNTRY_CODE3} = $record->country_code3;
66 0 $env->{GEOIP_COUNTRY_NAME} = $record->country_name;
67 0 $env->{GEOIP_LATITUDE} = $record->latitude;
68 0 $env->{GEOIP_LONGITUDE} = $record->longitude;
69 0 $env->{GEOIP_CONTINENT_CODE} = $record->continent_code;
70 0 ▶ $env->{GEOIP_TIME_ZONE} = $record->time_zone if $record->time_zone;
Branch: 1 branch
BranchTrueFalseTotal
if $record->time_zone 0 0 0
71 0 ▶ $env->{GEOIP_REGION} = $record->region if $record->region;
Branch: 1 branch
BranchTrueFalseTotal
if $record->region 0 0 0
72 0 ▶ $env->{GEOIP_REGION_NAME} = $record->region_name if $record->region and $record->region_name;
Branch: 1 branch
BranchTrueFalseTotal
if $record->region and $record->region_name 0 0 0
Condition0%0/3
$record->region and $record->region_name
!ll&&!rl&&r
000
MC/DC0%0/2
$record->region and $record->region_name
$record->region $record->region_name
Truth table0%0/3
A and B
A= $record->regionB= $record->region_name
ABresult
0X0
100
111
73 0 ▶ $env->{GEOIP_CITY} = $record->city if $record->city;
Branch: 1 branch
BranchTrueFalseTotal
if $record->city 0 0 0
74 0 ▶ $env->{GEOIP_POSTAL_CODE} = $record->postal_code if $record->postal_code;
Branch: 1 branch
BranchTrueFalseTotal
if $record->postal_code 0 0 0
75 0 ▶ $env->{GEOIP_METRO_CODE} = $record->metro_code if $record->metro_code;
Branch: 1 branch
BranchTrueFalseTotal
if $record->metro_code 0 0 0
76 0 ▶ $env->{GEOIP_AREA_CODE} = $record->area_code if $record->area_code;
Branch: 1 branch
BranchTrueFalseTotal
if $record->area_code 0 0 0
77 }
78 }
79 }
80  
81 0 return $self->app->($env);
82 }
83  
84 1 1;
85  
86 =head1 NAME
87  
88 Plack::Middleware::GeoIP - Find country and city of origin of a web request
89  
90 =head1 SYNOPSIS
91  
92 # with Plack::Middleware::RealIP
93 enable 'Plack::Middleware::RealIP',
94 header => 'X-Forwarded-For',
95 trusted_proxy => [ qw(192.168.1.0/24 192.168.2.1) ];
96 enable 'Plack::Middleware::GeoIP',
97 GeoIPDBFile => [ '/path/to/GeoIP.dat', '/path/to/GeoIPCity.dat' ],
98 GeoIPEnableUTF8 => 1;
99  
100 =head1 DESCRIPTION
101  
102 Plack::Middleware::GeoIP is a loose port of the Apache module
103 mod_geoip. It uses Geo::IP to lookup the country and city that a web
104 request originated from.
105  
106 All requests are looked up and GEOIP_* variables are added to PSGI
107 environment hash. For improved performance, you may want to only enable
108 this middleware for specific URL's.
109  
110 The following PSGI environment variables are set by this middleware:
111  
112 GeoIP Country Edition:
113  
114 GEOIP_COUNTRY_CODE, GEOIP_COUNTRY_CODE3, GEOIP_COUNTRY_NAME,
115 GEOIP_CONTINENT_CODE
116  
117 GeoIP City Edition:
118  
119 GEOIP_COUNTRY_CODE, GEOIP_COUNTRY_CODE3, GEOIP_COUNTRY_NAME,
120 GEOIP_CONTINENT_CODE, GEOIP_LATITUDE, GEOIP_LONGITUDE, GEOIP_TIME_ZONE,
121 GEOIP_REGION, GEOIP_REGION_NAME, GEOIP_CITY, GEOIP_POSTAL_CODE,
122 GEOIP_METRO_CODE, GEOIP_AREA_CODE
123  
124 =head1 CONFIGURATION
125  
126 =over 4
127  
128 =item GeoIPDBFile
129  
130 GeoIPDBFile => '/path/to/GeoIP.dat'
131 GeoIPDBFile => [ '/path/to/GeoIP.dat', '/path/to/GeoIPCity.dat' ]
132 GeoIPDBFile => [ '/path/to/GeoIP.dat', [ '/path/to/GeoIPCity.dat', 'MemoryCache' ] ]
133 GeoIPDBFile => [ '/path/to/GeoIP.dat', [ '/path/to/GeoIPCity.dat', [ qw(MemoryCache CheckCache) ] ] ]
134  
135 Path to GeoIP data file. GeoIP flags may also be specified. Accepted
136 flags are Standard, MemoryCache, CheckCache, IndexCache, and MMapCache.
137  
138 =item GeoIPEnableUTF8
139  
140 GeoIPEnableUTF8 => 1
141  
142 Turn on utf8 characters for city names.
143  
144 =back
145  
146 =head1 AUTHOR
147  
148 Sherwin Daganato E<lt>sherwin@daganato.comE<gt>
149  
150 =head1 LICENSE
151  
152 This library is free software; you can redistribute it and/or modify
153 it under the same terms as Perl itself.
154  
155 =head1 SEE ALSO
156  
157 L<mod_geoip|http://www.maxmind.com/app/mod_geoip>
158  
159 L<Geo::IP>
160  
161 =cut
↑ Summary GeoIP2.pm »