The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

Apache::Geo::Mirror - Find closest Mirror

SYNOPSIS

 # in httpd.conf
 # PerlModule Apache::HelloMirror
 #<Location /mirror>
 #   SetHandler perl-script
 #   PerlHandler Apache::HelloMirror
 #   PerlSetVar GeoIPDBFile "/usr/local/share/geoip/GeoIP.dat"
 #   PerlSetVar GeoIPFlag Standard
 #   PerlSetVar GeoIPMirror "/usr/local/share/data/mirror.txt"
 #   PerlSetVar GeoIPDefault it
 #</Location>
 
 # file Apache::HelloMirror
 
 use Apache::Geo::Mirror;
 use strict;
  
 use Apache::Constants qw(OK);
 
 sub handler {
   my $r = Apache::Geo::Mirror->new(shift);
   $r->content_type('text/plain');
   my $mirror = $r->find_mirror_by_addr();
   $r->print($mirror);
  
   OK;
 }
 1;

DESCRIPTION

This module provides a mod_perl (version 1) interface to the Geo::Mirror module, which finds the closest mirror for an IP address. It uses Geo::IP to identify the country that the IP address originated from. If the country is not represented in the mirror list, then it finds the closest country using a latitude/longitude table.

CONFIGURATION

This module subclasses Apache, and can be used as follows in an Apache module.

  # file Apache::HelloMirror
  
  use Apache::Geo::Mirror;
  use strict;
 
  sub handler {
     my $r = Apache::Geo::Mirror->new(shift);
     # continue along
  }
 

The directives in httpd.conf are as follows:

  <Location /mirror>
    PerlSetVar GeoIPDBFile "/usr/local/share/GeoIP/GeoIP.dat"
    PerlSetVar GeoIPFlag Standard
    PerlSetVar GeoIPMirror "/usr/local/share/data/mirror.txt"
    PerlSetVar GeoIPDefault us
    # other directives
  </Location>
 

The PerlSetVar directives available are

PerlSetVar GeoIPDBFile "/path/to/GeoIP.dat"

This specifies the location of the GeoIP.dat file. If not given, it defaults to the location specified upon installing the module.

PerlSetVar GeoIPFlag Standard

This can be set to STANDARD, or for faster performance but at a cost of using more memory, MEMORY_CACHE. When using memory cache you can force a reload if the file is updated by using CHECK_CACHE. If not specified, STANDARD is used.

PerlSetVar GeoIPMirror "/path/to/mirror.txt"

This specifies the location of a file containing the list of available mirrors. This file contains a list of mirror sites and the corresponding country code in the format

  http://some.server.com/some/path         us
  ftp://some.other.server.fr/somewhere     fr

No default location for this file is assumed.

PerlSetVar GeoIPDefault country

This specifies the country code to be used if a wanted country is not available in the mirror file. This defaults to us.

METHODS

The available methods are as follows.

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

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

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

Finds the nearest mirror by IP address. If $ipaddr is not given, this defaults $r->connection->remote_ip.

$mirror = $r->find_mirror_by_name( [$ipname] );

Finds the nearest mirror by country code. If $ipname is not given, this defaults to $r->get_remote_host(Apache::REMOTE_HOST).

AUTOMATIC REDIRECTION

If Apache::Geo::Mirror is used as

  PerlModule Apache::Geo::Mirror
  <Location /CPAN>
    PerlSetVar GeoIPDBFile "/usr/local/share/geoip/GeoIP.dat"
    PerlSetVar GeoIPFlag Standard
    PerlSetVar GeoIPMirror "/usr/local/share/data/mirror.txt"
    PerlSetVar GeoIPDefault us
    PerlHandler Apache::Geo::Mirror->auto_redirect
  </Location>

then an automatic redirection is made.

VERSION

0.10

SEE ALSO

Geo::IP, Geo::Mirror, and Apache.

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 Copyright (c) 2002, T.J. Mather, tjmather@tjmather.com, New York, NY, USA. See http://www.maxmind.com/ for details. The mod_perl interface is Copyright (c) 2002, Randy Kobes <randy@theoryx5.uwinnipeg.ca>.

All rights reserved. This package is free software; you can redistribute it and/or modify it under the same terms as Perl itself.