| 1 | package Apache2::BalanceLogic; | ||
| 2 | |||
| 3 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | 1 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | 0 | ▶ | use Apache2::RequestRec (); |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 6 | use Apache2::RequestIO (); | ||
| 7 | use Apache2::Connection (); | ||
| 8 | use APR::Table (); | ||
| 9 | use Apache2::Const -compile => qw( OK DECLINED ); | ||
| 10 | use YAML qw 'LoadFile'; | ||
| 11 | use CGI::Cookie; | ||
| 12 | use Net::CIDR::Lite; | ||
| 13 | use UNIVERSAL::require; | ||
| 14 | |||
| 15 | our $VERSION = '0.0.1'; | ||
| 16 | |||
| 17 | our $conf; | ||
| 18 | our $plugin; | ||
| 19 | |||
| 20 | sub handler { | ||
| 21 | |||
| 22 | my $r = shift; | ||
| 23 | |||
| 24 | # load main config file. | ||
| 25 | $conf ||= LoadFile( $r->dir_config('CONFIG_PATH') ) or die; | ||
| 26 | |||
| 27 | # load plugin config file and generate plugin object. | ||
| 28 | $plugin ||= do { | ||
| 29 | my $p_conf = LoadFile( $conf->{Plugin}->{Config} ) or die; | ||
| 30 | my $class = __PACKAGE__ . '::Plugin::' . $conf->{Plugin}->{Name}; | ||
| 31 | $class->use or die $@; | ||
| 32 | $class->new($p_conf); | ||
| 33 | }; | ||
| 34 | |||
| 35 | # run! | ||
| 36 | my $route_id = $plugin->run($r); | ||
| 37 | return Apache2::Const::DECLINED unless $route_id; | ||
| 38 | |||
| 39 | # you can forward a request for a server apointed in '__force__' query string. | ||
| 40 | my $args = $r->args; | ||
| 41 | if ( $args =~ /__force__=(\d+)$/ ) { | ||
| 42 | my $force = $1; | ||
| 43 | my $ip = $r->connection->remote_ip; | ||
| 44 | my $cidr = Net::CIDR::Lite->new; | ||
| 45 | my $admin_ip = $conf->{ADMIN_IP} or die($!); | ||
| 46 | for (@$admin_ip) { | ||
| 47 | $cidr->add_any($_); | ||
| 48 | } | ||
| 49 | $route_id = $force if $cidr->find($ip); | ||
| 50 | } | ||
| 51 | |||
| 52 | # a inner cookie trick for "stickysession" in mod_proxy_balancer. | ||
| 53 | my $cookie_str = $r->headers_in->get('Cookie'); | ||
| 54 | $cookie_str =~ s/route_id=\d+\;?//; | ||
| 55 | $cookie_str = 'route_id=x.' . $route_id . '; ' . $cookie_str . ';'; | ||
| 56 | $r->headers_in->set( 'Cookie' => $cookie_str ); | ||
| 57 | |||
| 58 | # return OK | ||
| 59 | return Apache2::Const::OK; | ||
| 60 | } | ||
| 61 | |||
| 62 | 1; | ||
| 63 | |||
| 64 | __END__ | ||