&remote_ip: Fixed for stable headers hitting.
[MyWeb.git] / Web.pm
diff --git a/Web.pm b/Web.pm
index f9d6d62..33cd6d9 100644 (file)
--- a/Web.pm
+++ b/Web.pm
@@ -31,7 +31,7 @@ our @EXPORT=qw(
                &Wrequire &Wuse
                &path_web &path_abs_disk
                &uri_escaped
-               &a_href &a_href_cz
+               &a_href &a_href_cc
                &vskip
                &img &centerimg &rightimg
                $W
@@ -121,6 +121,7 @@ require Data::Compare;
 use Data::Dumper;
 require Encode;
 use Apache2::Filter;
+use Apache2::Connection;
 
 
 #our $W;
@@ -237,7 +238,7 @@ my($class,%args)=@_;
        $W->{"headers_in"}=My::Hash::Merge->new(
                        $W->{"headers_in"},
                        My::Hash::Sub->new({
-                               "_get_remote_host"=>sub { return $W->{"r"}->get_remote_host(); },
+                               "_remote_ip"=>sub { return $W->{"r"}->connection()->remote_ip(); },
                                }),
                        );
        $W->{"headers_in"}=My::Hash::Readonly->new($W->{"headers_in"});
@@ -671,23 +672,28 @@ sub remote_ip ()
        # As 'Apache2::ForwardedFor' takes the first of $ENV{"HTTP_X_FORWARDED_FOR"}
        # while the contents is '127.0.0.1, 213.220.195.171' if client has its own proxy.
        # We must take the last item ourselves.
-       my $r=$W->{"headers_in"}{"X-Forwarded-For"} || $W->{"headers_in"}{"_get_remote_host"};
-       $r=~s/^.*,\s*//;
+       # Be VERY sure you always retrieve all the headers unconditionally to hit: My::Hash::RecordKeys
+       my $x_forwarded_for=$W->{"headers_in"}{"X-Forwarded-For"};
+       $x_forwarded_for=~s/^.*,\s*// if $x_forwarded_for;
+       my $remote_ip=$W->{"headers_in"}{"_remote_ip"};
+       my $r;
+       $r||=$x_forwarded_for;
+       $r||=$remote_ip;
        return $r;
 }
 
-sub is_cz ()
-{
-       return 0 if !$have_Geo_IP;
-       return "CZ" eq Geo::IP->new()->country_code_by_addr(remote_ip());
-}
-
-sub a_href_cz ($$;%)
+# $url={"JP"=>"http://specific",...};
+# $url={""=>"http://default",...};
+sub a_href_cc($$;%)
 {
 my($url,$contents,%args)=@_;
 
-       return a_href $url,$contents,%args if is_cz();
-       return $contents;
+       my $cc;
+       $cc||=Geo::IP->new()->country_code_by_addr(remote_ip()) if $have_Geo_IP;
+       $cc||="";
+       $url=$url->{$cc};
+       return $contents if !$url;
+       return a_href $url,$contents,%args;
 }
 
 sub make ($)