| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | # $Id: $ | 
| 2 |  |  |  |  |  |  |  | 
| 3 |  |  |  |  |  |  | package Apache2::Mogile::Dispatch; | 
| 4 |  |  |  |  |  |  |  | 
| 5 | 1 |  |  | 1 |  | 24948 | use strict; | 
|  | 1 |  |  |  |  | 3 |  | 
|  | 1 |  |  |  |  | 41 |  | 
| 6 | 1 |  |  | 1 |  | 5 | use warnings; | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 29 |  | 
| 7 |  |  |  |  |  |  |  | 
| 8 | 1 |  |  | 1 |  | 1048 | use English; | 
|  | 1 |  |  |  |  | 5115 |  | 
|  | 1 |  |  |  |  | 6 |  | 
| 9 |  |  |  |  |  |  |  | 
| 10 | 1 |  |  | 1 |  | 944 | use APR::Table (); | 
|  | 0 |  |  |  |  |  |  | 
|  | 0 |  |  |  |  |  |  | 
| 11 |  |  |  |  |  |  | use APR::SockAddr (); | 
| 12 |  |  |  |  |  |  | use Apache2::RequestRec (); | 
| 13 |  |  |  |  |  |  | use Apache2::RequestUtil (); | 
| 14 |  |  |  |  |  |  | use Apache2::Connection (); | 
| 15 |  |  |  |  |  |  | use Apache2::Filter (); | 
| 16 |  |  |  |  |  |  | use Apache2::RequestRec (); | 
| 17 |  |  |  |  |  |  | use Apache2::Module; | 
| 18 |  |  |  |  |  |  | use Apache2::CmdParms (); | 
| 19 |  |  |  |  |  |  | use Apache2::Directive (); | 
| 20 |  |  |  |  |  |  | use Apache2::Log (); | 
| 21 |  |  |  |  |  |  | use Apache2::URI (); | 
| 22 |  |  |  |  |  |  | use Apache2::Const -compile => qw(DECLINED OK DONE NOT_FOUND); | 
| 23 |  |  |  |  |  |  |  | 
| 24 |  |  |  |  |  |  | use MogileFS; | 
| 25 |  |  |  |  |  |  |  | 
| 26 |  |  |  |  |  |  | our $VERSION = '0.2'; | 
| 27 |  |  |  |  |  |  |  | 
| 28 |  |  |  |  |  |  | sub handler { | 
| 29 |  |  |  |  |  |  | my ($r) = @_; | 
| 30 |  |  |  |  |  |  | my $cf = get_config($r); | 
| 31 |  |  |  |  |  |  | my $host_info = get_direction($r, $cf); | 
| 32 |  |  |  |  |  |  | if ($host_info && $host_info->{'reproxy'})  { | 
| 33 |  |  |  |  |  |  | $r->err_headers_out->add('X-REPROXY-URL', $host_info->{'reproxy'} ); | 
| 34 |  |  |  |  |  |  | return Apache2::Const::DONE; | 
| 35 |  |  |  |  |  |  | } | 
| 36 |  |  |  |  |  |  | if (exists $host_info->{'mogile'} && $host_info->{'mogile'} eq '0') { | 
| 37 |  |  |  |  |  |  | if ($cf->{'MogReproxyToken'}) { | 
| 38 |  |  |  |  |  |  | $r->err_headers_out->add('X-REPROXY-SERVICE' => $cf->{'MogReproxyToken'}); | 
| 39 |  |  |  |  |  |  | } else { | 
| 40 |  |  |  |  |  |  | my $good_path = get_working_path(@{ $cf->{'MogStaticServers'} || '' }); | 
| 41 |  |  |  |  |  |  | if (! $good_path) { | 
| 42 |  |  |  |  |  |  | return Apache2::Const::NOT_FOUND; | 
| 43 |  |  |  |  |  |  | } | 
| 44 |  |  |  |  |  |  | $r->err_headers_out->add('X-REPROXY-URL', $good_path ); | 
| 45 |  |  |  |  |  |  | } | 
| 46 |  |  |  |  |  |  | return Apache2::Const::DONE; | 
| 47 |  |  |  |  |  |  | } | 
| 48 |  |  |  |  |  |  | if ($host_info && $host_info->{'mogile'}) { | 
| 49 |  |  |  |  |  |  | my $filekey = uri2key($r, $cf, $host_info); | 
| 50 |  |  |  |  |  |  | my $mogfs = get_mogile_object([ @{$cf->{'MogTrackers'}} ], $cf->{'MogDomain'}); | 
| 51 |  |  |  |  |  |  | my @paths; | 
| 52 |  |  |  |  |  |  | eval { | 
| 53 |  |  |  |  |  |  | @paths = $mogfs->get_paths($filekey, 1); | 
| 54 |  |  |  |  |  |  | }; | 
| 55 |  |  |  |  |  |  | if ($EVAL_ERROR) { | 
| 56 |  |  |  |  |  |  | return Apache2::Const::NOT_FOUND; | 
| 57 |  |  |  |  |  |  | } | 
| 58 |  |  |  |  |  |  | my $working_path = get_working_path(@paths); | 
| 59 |  |  |  |  |  |  | if (! $working_path) { | 
| 60 |  |  |  |  |  |  | return Apache2::Const::NOT_FOUND; | 
| 61 |  |  |  |  |  |  | } | 
| 62 |  |  |  |  |  |  | if (usessi($r, $cf, $host_info)) { | 
| 63 |  |  |  |  |  |  | $r->err_headers_out->add('X-REPROXY-URL', $working_path ); | 
| 64 |  |  |  |  |  |  | return Apache2::Const::DONE; | 
| 65 |  |  |  |  |  |  | } | 
| 66 |  |  |  |  |  |  | my $ua = LWP::UserAgent->new; | 
| 67 |  |  |  |  |  |  | my $response = $ua->get($working_path); | 
| 68 |  |  |  |  |  |  | if ($response->is_success) { | 
| 69 |  |  |  |  |  |  | $r->print($response->content); | 
| 70 |  |  |  |  |  |  | } | 
| 71 |  |  |  |  |  |  | return Apache2::Const::DONE; | 
| 72 |  |  |  |  |  |  | } | 
| 73 |  |  |  |  |  |  | return Apache2::Const::DONE; | 
| 74 |  |  |  |  |  |  | } | 
| 75 |  |  |  |  |  |  |  | 
| 76 |  |  |  |  |  |  | sub get_mogile_object { | 
| 77 |  |  |  |  |  |  | my ($hosts, $domain) = @_; | 
| 78 |  |  |  |  |  |  | my $mog = MogileFS->new( | 
| 79 |  |  |  |  |  |  | hosts => $hosts, | 
| 80 |  |  |  |  |  |  | domain => $domain, | 
| 81 |  |  |  |  |  |  | ); | 
| 82 |  |  |  |  |  |  | return $mog; | 
| 83 |  |  |  |  |  |  | } | 
| 84 |  |  |  |  |  |  |  | 
| 85 |  |  |  |  |  |  | sub get_working_path { | 
| 86 |  |  |  |  |  |  | my (@uris) = @_; | 
| 87 |  |  |  |  |  |  | my $ua = LWP::UserAgent->new; | 
| 88 |  |  |  |  |  |  | for my $uri (@uris) { | 
| 89 |  |  |  |  |  |  | my $response = $ua->head($uri); | 
| 90 |  |  |  |  |  |  | if ($response->is_success) { return $uri; } | 
| 91 |  |  |  |  |  |  | } | 
| 92 |  |  |  |  |  |  | return 0; | 
| 93 |  |  |  |  |  |  | } | 
| 94 |  |  |  |  |  |  |  | 
| 95 |  |  |  |  |  |  | # XXX To be subclassed | 
| 96 |  |  |  |  |  |  | sub mogile_key { | 
| 97 |  |  |  |  |  |  | my ($r) = @_; | 
| 98 |  |  |  |  |  |  | return $r->uri; | 
| 99 |  |  |  |  |  |  | } | 
| 100 |  |  |  |  |  |  |  | 
| 101 |  |  |  |  |  |  | # XXX To be subclassed | 
| 102 |  |  |  |  |  |  | sub get_direction { | 
| 103 |  |  |  |  |  |  | return ( 'mogile' => 1 ); | 
| 104 |  |  |  |  |  |  | } | 
| 105 |  |  |  |  |  |  |  | 
| 106 |  |  |  |  |  |  | # XXX To be subclassed | 
| 107 |  |  |  |  |  |  | sub get_config { | 
| 108 |  |  |  |  |  |  | return { | 
| 109 |  |  |  |  |  |  | 'MogTrackers' => [ 'localhost:11211'], | 
| 110 |  |  |  |  |  |  | 'MogStaticServers' => ['localhost:80'], | 
| 111 |  |  |  |  |  |  | 'MogDomain' => 'localhost', | 
| 112 |  |  |  |  |  |  | 'MogReproxyToken' => 'legacy_web', | 
| 113 |  |  |  |  |  |  | }; | 
| 114 |  |  |  |  |  |  | } | 
| 115 |  |  |  |  |  |  |  | 
| 116 |  |  |  |  |  |  | # XXX To be subclassed | 
| 117 |  |  |  |  |  |  | sub reproxy_request { | 
| 118 |  |  |  |  |  |  | return 1; | 
| 119 |  |  |  |  |  |  | } | 
| 120 |  |  |  |  |  |  |  | 
| 121 |  |  |  |  |  |  | 1; | 
| 122 |  |  |  |  |  |  | __END__ |