Apache-GeoIP

 view release on metacpan or  search on metacpan

Apache/Geo/IP.pm  view on Meta::CPAN

Multiple values of I<GeoIPFlag> can be set by specifying them
using I<PerlAddVar>. If no values are specified, I<STANDARD> is used.

=item PerlSetVar GeoIPType CITY_EDITION_REV1

This specifies the type of database file to be used. See the L<Geo::IP> documentation
for the various types that are supported.

=item PerlSetVar GeoIPXForwardedFor 1

If this directive is set to something true, the I<X-Forwarded-For> header will
be used to try to identify the originating IP address; this is useful for clients 
connecting to a web server through an HTTP proxy or load balancer. If this header
is not present, C<$r-E<gt>connection-E<gt>remote_ip> will be used.

=back

=head1 METHODS

The available methods are as follows.

=over 4

=item $code = $r->country_code_by_addr( [$ipaddr] );

Returns the ISO 3166 country code for an IP address.
If I<$ipaddr> is not given, the value obtained by
examining the I<X-Forwarded-For> header will be used, if
I<GeoIPXForwardedFor> is used, or else
C<$r-E<gt>connection-E<gt>remote_ip> is used

=item $code = $r->country_code_by_name( [$ipname] );

Returns the ISO 3166 country code for a hostname.
If I<$ipname> is not given, the value obtained by
C<$r-E<gt>get_remote_host(REMOTE_HOST)> is used.

=item $code = $r->country_code3_by_addr( [$ipaddr] );

Returns the 3 letter country code for an IP address.
If I<$ipaddr> is not given, the value obtained by
examining the I<X-Forwarded-For> header will be used, if
I<GeoIPXForwardedFor> is used, or else
C<$r-E<gt>connection-E<gt>remote_ip> is used.

=item $code = $r->country_code3_by_name( [$ipname] );

Returns the 3 letter country code for a hostname.
If I<$ipname> is not given, the value obtained by
C<$r-E<gt>get_remote_host(REMOTE_HOST)> is used.

=item $org = $r->org_by_addr( [$ipaddr] );

Returns the Organization, ISP name or Domain Name for an IP address.
If I<$ipaddr> is not given, the value obtained by
examining the I<X-Forwarded-For> header will be used, if
I<GeoIPXForwardedFor> is used, or else
C<$r-E<gt>connection-E<gt>remote_ip> is used.

=item $org = $r->org_by_name( [$ipname] );

Returns the Organization, ISP name or Domain Name for a hostname.
If I<$ipname> is not given, the value obtained by
C<$r-E<gt>get_remote_host(REMOTE_HOST)> is used.

=item ( $country, $region ) = $r->region_by_addr( [$ipaddr] );

Returns a list containing country and region for an IP address. If the
region and/or country is unknown, I<undef> is returned. This works only 
for region databases. If I<$ipaddr> is not given, the value obtained by
examining the I<X-Forwarded-For> header will be used, if
I<GeoIPXForwardedFor> is used, or else
C<$r-E<gt>connection-E<gt>remote_ip> is used.

=item ( $country, $region ) = $r->region_by_name( [$ipname] );

Returns a list containing country and region for a hostname. If the
region and/or country is unknown, I<undef> is returned. This works only 
for region databases. If I<$ipname> is not given, the value obtained by
examining the I<X-Forwarded-For> header will be used, if
I<GeoIPXForwardedFor> is used, or else
C<$r-E<gt>get_remote_host(REMOTE_HOST)> is used.

=item $gi = $r->gi

Returns the L<Geo::IP> object.

=back

=head1 Geo::IP::Record

A L<Geo::IP::Record> object can be created by two ways:

=over 4

=item $record = $r->record_by_addr( [$ipaddr] );

Returns a L<Geo::IP::Record> object containing city location an IP address.
If I<$ipaddr> is not given, the value obtained by
examining the I<X-Forwarded-For> header will be used, if
I<GeoIPXForwardedFor> is used, or else
C<$r-E<gt>connection-E<gt>remote_ip> is used.

=item $record = $r->record_by_name( [$ipname] );

Returns a L<Geo::IP::Record> object containing city location for a hostname.
If I<$ipname> is not given, the value obtained by
C<$r-E<gt>get_remote_host(REMOTE_HOST)> is used.

=back

Apache/Geo/Mirror.pm  view on Meta::CPAN

with a freshness equal to or above this value may be chosen.

=item PerlSetVar GeoIPDefault "http://some.where.org/"

This specifies the default url to be used if no nearby mirror is found.
Multiple values may be specified using I<PerlAddVar>; if more than one
default is given, a random one will be chosen.

=item PerlSetVar GeoIPXForwardedFor 1

If this directive is set to something true, the I<X-Forwarded-For> header will
be used to try to identify the originating IP address; this is useful for clients 
connecting to a web server through an HTTP proxy or load balancer. If this header
is not present, C<$r-E<gt>connection-E<gt>remote_ip> will be used.

=back

=head1 METHODS

The available methods are as follows.

Apache/Geo/Mirror.pm  view on Meta::CPAN

=item $mirror = $r->find_mirror_by_country( [$country] );

Finds the nearest mirror by country code. If I<$country> is not
given, this defaults to the country as specified by a lookup
of C<$r-E<gt>connection-E<gt>remote_ip>.

=item $mirror = $r->find_mirror_by_addr( [$ipaddr] );

Finds the nearest mirror by IP address. If I<$ipaddr> is not
given, the value obtained by
examining the I<X-Forwarded-For> header will be used, if
I<GeoIPXForwardedFor> is used, or else
C<$r-E<gt>connection-E<gt>remote_ip> is used.

=item $gm = $r->gm;

Returns the L<Geo::IP::Mirror> object.

=back
=head1 AUTOMATIC REDIRECTION

Apache/Geo/Mirror.pm  view on Meta::CPAN


can be used to handle robots that honor a I<robots.txt> file. This can be
a physical file that exists on the system or, if it is set to the special
value I<default>, the string

    User-agent: *
    Disallow: /

will be used, which disallows robot access to anything.

Within automatic redirection, the I<X-Forwarded-For> header wil be
used to try to infer the IP address of the client.

=head1 SEE ALSO

L<Geo::IP>, L<Geo::Mirror>, and L<Apache>.

=head1 AUTHOR

The look-up code for associating a country with an IP address 
is based on the GeoIP library and the Geo::IP Perl module, and is 

Apache/GeoIP.pm  view on Meta::CPAN


$VERSION = '1.99';
@EXPORT_OK = qw(find_addr);

sub find_addr {
  my ($r, $xforwardedfor) = @_;
  my $host;
  if (defined $xforwardedfor) {
    my $ReIpNum = qr{([01]?\d\d?|2[0-4]\d|25[0-5])};
    my $ReIpAddr = qr{^$ReIpNum\.$ReIpNum\.$ReIpNum\.$ReIpNum$};
    $host =  $r->header_in('X-Forwarded-For') || 
      $r->connection->remote_ip;
    if ($host =~ /,/) {
      my @a = split /\s*,\s*/, $host;
      for my $i (0 .. $#a) {
          if ($a[$i] =~ /$ReIpAddr/ and $a[$i] ne '127.0.0.1') {
              $host = $a[$i];
              last;
          }
      }
      $host = '127.0.0.1' if $host =~ /,/;

Apache2/Geo/IP.pm  view on Meta::CPAN

Multiple values of I<GeoIPFlag> can be set by specifying them
using I<PerlAddVar>. If no values are specified, I<STANDARD> is used.

=item PerlSetVar GeoIPType CITY_EDITION_REV1

This specifies the type of database file to be used. See the L<Geo::IP> documentation
for the various types that are supported.

=item PerlSetVar GeoIPXForwardedFor 1

If this directive is set to something true, the I<X-Forwarded-For> header will
be used to try to identify the originating IP address; this is useful for clients 
connecting to a web server through an HTTP proxy or load balancer. If this header
is not present, C<$r-E<gt>connection-E<gt>remote_ip> will be used.

=back

=head1 METHODS

The available methods are as follows.

=over 4

=item $code = $r->country_code_by_addr( [$ipaddr] );

Returns the ISO 3166 country code for an IP address.
If I<$ipaddr> is not given, the value obtained by
examining the I<X-Forwarded-For> header will be used, if
I<GeoIPXForwardedFor> is used, or else
C<$r-E<gt>connection-E<gt>remote_ip> is used

=item $code = $r->country_code_by_name( [$ipname] );

Returns the ISO 3166 country code for a hostname.
If I<$ipname> is not given, the value obtained by
C<$r-E<gt>get_remote_host(Apache2::Const::REMOTE_HOST)> is used.

=item $code = $r->country_code3_by_addr( [$ipaddr] );

Returns the 3 letter country code for an IP address.
If I<$ipaddr> is not given, the value obtained by
examining the I<X-Forwarded-For> header will be used, if
I<GeoIPXForwardedFor> is used, or else
C<$r-E<gt>connection-E<gt>remote_ip> is used.

=item $code = $r->country_code3_by_name( [$ipname] );

Returns the 3 letter country code for a hostname.
If I<$ipname> is not given, the value obtained by
C<$r-E<gt>get_remote_host(Apache2::Const::REMOTE_HOST)> is used.

=item $org = $r->org_by_addr( [$ipaddr] );

Returns the Organization, ISP name or Domain Name for an IP address.
If I<$ipaddr> is not given, the value obtained by
examining the I<X-Forwarded-For> header will be used, if
I<GeoIPXForwardedFor> is used, or else
C<$r-E<gt>connection-E<gt>remote_ip> is used.

=item $org = $r->org_by_name( [$ipname] );

Returns the Organization, ISP name or Domain Name for a hostname.
If I<$ipname> is not given, the value obtained by
C<$r-E<gt>get_remote_host(Apache2::Const::REMOTE_HOST)> is used.

=item ( $country, $region ) = $r->region_by_addr( [$ipaddr] );

Returns a list containing country and region for an IP address. If the
region and/or country is unknown, I<undef> is returned. This works only 
for region databases. If I<$ipaddr> is not given, the value obtained by
examining the I<X-Forwarded-For> header will be used, if
I<GeoIPXForwardedFor> is used, or else
C<$r-E<gt>connection-E<gt>remote_ip> is used.

=item ( $country, $region ) = $r->region_by_name( [$ipname] );

Returns a list containing country and region for a hostname. If the
region and/or country is unknown, I<undef> is returned. This works only 
for region databases. If I<$ipname> is not given, the value obtained by
examining the I<X-Forwarded-For> header will be used, if
I<GeoIPXForwardedFor> is used, or else
C<$r-E<gt>get_remote_host(Apache2::Const::REMOTE_HOST)> is used.

=item $gi = $r->gi

Returns the L<Geo::IP> object.

=back

=head1 Geo::IP::Record

A L<Geo::IP::Record> object can be created by two ways:

=over 4

=item $record = $r->record_by_addr( [$ipaddr] );

Returns a L<Geo::IP::Record> object containing city location an IP address.
If I<$ipaddr> is not given, the value obtained by
examining the I<X-Forwarded-For> header will be used, if
I<GeoIPXForwardedFor> is used, or else
C<$r-E<gt>connection-E<gt>remote_ip> is used.

=item $record = $r->record_by_name( [$ipname] );

Returns a L<Geo::IP::Record> object containing city location for a hostname.
If I<$ipname> is not given, the value obtained by
C<$r-E<gt>get_remote_host(Apache2::Const::REMOTE_HOST)> is used.

=back

Apache2/Geo/Mirror.pm  view on Meta::CPAN

with a freshness equal to or above this value may be chosen.

=item PerlSetVar GeoIPDefault "http://some.where.org/"

This specifies the default url to be used if no nearby mirror is found.
Multiple values may be specified using I<PerlAddVar>; if more than one
default is given, a random one will be chosen.

=item PerlSetVar GeoIPXForwardedFor 1

If this directive is set to something true, the I<X-Forwarded-For> header will
be used to try to identify the originating IP address; this is useful for clients 
connecting to a web server through an HTTP proxy or load balancer. If this header
is not present, C<$r-E<gt>connection-E<gt>remote_ip> will be used.

=back

=head1 METHODS

The available methods are as follows.

Apache2/Geo/Mirror.pm  view on Meta::CPAN

=item $mirror = $r->find_mirror_by_country( [$country] );

Finds the nearest mirror by country code. If I<$country> is not
given, this defaults to the country as specified by a lookup
of C<$r-E<gt>connection-E<gt>remote_ip>.

=item $mirror = $r->find_mirror_by_addr( [$ipaddr] );

Finds the nearest mirror by IP address. If I<$ipaddr> is not
given, the value obtained by
examining the I<X-Forwarded-For> header will be used, if
I<GeoIPXForwardedFor> is used, or else
C<$r-E<gt>connection-E<gt>remote_ip> is used.

=item $gm = $r->gm;

Returns the L<Geo::IP::Mirror> object.

=back

=head1 AUTOMATIC REDIRECTION

Apache2/Geo/Mirror.pm  view on Meta::CPAN


can be used to handle robots that honor a I<robots.txt> file. This can be
a physical file that exists on the system or, if it is set to the special
value I<default>, the string

    User-agent: *
    Disallow: /

will be used, which disallows robot access to anything.

Within automatic redirection, the I<X-Forwarded-For> header wil be
used to try to infer the IP address of the client.

=head1 SEE ALSO

L<Geo::IP>, L<Geo::Mirror>, and L<Apache2::RequestRec>.

=head1 AUTHOR

The look-up code for associating a country with an IP address 
is based on the GeoIP library and the Geo::IP Perl module, and is 

Apache2/GeoIP.pm  view on Meta::CPAN


$VERSION = '1.99';
@EXPORT_OK = qw(find_addr);

sub find_addr {
  my ($r, $xforwardedfor) = @_;
  my $host;
  if (defined $xforwardedfor) {
    my $ReIpNum = qr{([01]?\d\d?|2[0-4]\d|25[0-5])};
    my $ReIpAddr = qr{^$ReIpNum\.$ReIpNum\.$ReIpNum\.$ReIpNum$};
    $host =  $r->headers_in->get('X-Forwarded-For') || 
      $r->connection->remote_ip;
    if ($host =~ /,/) {
      my @a = split /\s*,\s*/, $host;
      for my $i (0 .. $#a) {
          if ($a[$i] =~ /$ReIpAddr/ and $a[$i] ne '127.0.0.1') {
              $host = $a[$i];
              last;
          }
      }
      $host = '127.0.0.1' if $host =~ /,/;

Changes  view on Meta::CPAN

Revision history for Perl extension Apache::Geo::IP.

1.99 April 23, 2009
  - Require Geo::IP
  - remove Apache::Geo::IP::Record and Apache2::Geo::IP::Record, as they're
    now supplied through Geo::IP
  - exit Makefile.PL if mod_perl isn't found, to try to make CPAN testers happy
  - add Apache directive to handle a robots.txt in automatic redirection
  - add Apache directive to use the X-Forwarded-For header, if present, to
    try to infer originating IP address; this is useful for clients using
    a proxy or load balancer
  - *incompatible change*: the GeoIPDefault Apache directive now specifies
    the default url to use in automatic redirection, not the default country

1.63 March 26, 2007
  - update C library files to GeoIP-1.4.2
  - use mod_perl2, rather than mod_perl, in Apache2 test

1.62 Thu, May 12 2005
  - update C library files to GeoIP-1.3.8
  - rename the mod_perl 2 modules from Apache::* to Apache2::*,
    and adjust to use the new namespace of mod_perl-2

1.52 Mon Sep 13 16:36:00 2004
  - in mp2 Apache::Geo::Mirror, fix bug when host mirror chosen
    didn't have a path after the hostname
  - in Mirror.pm, use the left-most ip address in the
    X-Forwarded-For header for the host name, if defined,
    rather than the right one, in cases more than one
    is present. Also, ignore cases when 127.0.0.1 is used.

1.51 Thu Sep 8 16:36:00 2004
  - fix bug in mp2 auto-redirect handler in Mirror.pm to
    remove the port number before redirecting

1.5 Wed Sep 08 22:13:00 2004
  - upgrade to GeoIP-1.3.6
  - get rid of warning in Mirror.pm when a country's



( run in 0.296 second using v1.01-cache-2.11-cpan-26ccb49234f )