blib/lib/CGI/remote_addr.pm

filter: Statementstmt 100% 30 / 30 Branchbran 100% 6 / 6 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 7 / 7 Podpod 100% 1 / 1 totaltotal 100% 44 / 44 CC 4 SCAR 7.9
CC
4
Cov
100%
remote_addr
4.0
BEGIN
1.0
BEGIN
1.0
CRAP 2.2
↑ Summary
1 package CGI::remote_addr;
2  
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use Regexp::Common qw(net);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use List::MoreUtils qw(uniq);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 2 our $VERSION = '0.03';
10  
11 7 ▶sub remote_addr {
Subroutine remote_addr: called CC=4 SCAR=13.9
Pod documented
12 7 my @ips;
13  
14 # gather all available IP addresses
15 7 ▶ if ($ENV{'HTTP_X_FORWARDED_FOR'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ENV{"HTTP_X_FORWARDED_FOR"}) 5 2 7
16 5 push( @ips, split(/\s*,\s*/, $ENV{'HTTP_X_FORWARDED_FOR'}) );
17 }
18 7 ▶ if ($ENV{'REMOTE_ADDR'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ENV{"REMOTE_ADDR"}) 5 2 7
19 5 push( @ips, $ENV{'REMOTE_ADDR'} );
20 }
21  
22 # trim list to a unique list of valid IPs
23 7 @ips = uniq grep { /^$RE{net}{IPv4}$/ } @ips;
24  
25 # return IP back to caller
26 7 ▶ return wantarray ? @ips : $ips[0];
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 2 5 7
27 }
28  
29 # redefine CGI::remote_addr() so that it uses our version instead of the one
30 # that comes with CGI.pm
31 {
32 2 ▶ no warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
33 2 *CGI::remote_addr = \&remote_addr;
34 }
35  
36 2 1;
37  
38 =head1 NAME
39  
40 CGI::remote_addr - Enhanced version of CGI.pm's "remote_addr()"
41  
42 =head1 SYNOPSIS
43  
44 use CGI;
45 use CGI::remote_addr;
46  
47 my $cgi = CGI->new();
48 my $addr = $cgi->remote_addr();
49  
50 =head1 DESCRIPTION
51  
52 C<CGI::remote_addr> implements an enhanced version of the C<remote_addr()>
53 method provided by C<CGI.pm>, which attempts to return the original IP address
54 that the connection originated from (which is not necessarily the IP address
55 that we received the connection from).
56  
57 Simply loading C<CGI::remote_addr> causes it to over-ride the existing
58 C<remote_addr()> method. Do note, though, that this is a global over-ride; if
59 you're running under mod_perl you've just over-ridden it for B<all> of your
60 applications.
61  
62 =head2 Differences from CGI.pm
63  
64 =over
65  
66 =item *
67  
68 We check not only C<$ENV{REMOTE_ADDR}> to find the IP address, but also look in
69 C<$ENV{HTTP_X_FORWARDED_FOR}> to find the IP address. If
70 C<$ENV{HTTP_X_FORWARDED_FOR}> is defined, we try that first.
71  
72 =item *
73  
74 Only valid IP addresses are returned, regardless of whatever exists in
75 C<$ENV{REMOTE_ADDR}> or C<$ENV{HTTP_X_FORWARDED_FOR}>. I've seen lots of cases
76 where the values for C<$ENV{HTTP_X_FORWARDED_FOR}> were stuffed with garbage,
77 and we make sure that you only get a real IP back.
78  
79 =item *
80  
81 We return IPs in both a scalar and a list context. In scalar context you get
82 the first (originating) IP address. In list context you get a unique list of
83 all of the IPs that the connection was received through.
84  
85 =item *
86  
87 In the event that we cannot find a valid IP address, this method returns
88 C<undef>, B<NOT> 127.0.0.1 (like C<CGI.pm> does).
89  
90 =back
91  
92 =head1 METHODS
93  
94 =over
95  
96 =item remote_addr()
97  
98 Returns the IP address(es) of the remote host.
99  
100 =back
101  
102 =head1 AUTHOR
103  
104 Graham TerMarsch (cpan@howlingfrog.com)
105  
106 =head1 COPYRIGHT
107  
108 Copyright (C) 2008 Graham TerMarsch. All Rights Reserved.
109  
110 This library is free software; you can redistribute it and/or modify it under
111 the same terms as Perl itself.
112  
113 =head1 SEE ALSO
114  
115 =over
116  
117 =item L<CGI>
118  
119 =back
120  
121 =cut
↑ Summary