| 1 | package CPAN::MirrorMerger::ProxyServer; | ||
| 2 | use strict; | ||
| 3 | use warnings; | ||
| 4 | |||
| 5 | use parent qw/Plack::Component/; | ||
| 6 | |||
| 7 | use Plack::Util::Accessor qw/storage logger mirror_cache/; | ||
| 8 | |||
| 9 | use Plack::Request; | ||
| 10 | use Plack::Response; | ||
| 11 | use Plack::MIME; | ||
| 12 | use Path::Tiny; | ||
| 13 | |||
| 14 | use CPAN::MirrorMerger; | ||
| 15 | use CPAN::MirrorMerger::Mirror; | ||
| 16 | use CPAN::MirrorMerger::MirrorCache; | ||
| 17 | use CPAN::MirrorMerger::RetryPolicy::NoRetry; | ||
| 18 | use CPAN::MirrorMerger::PackageInfo; | ||
| 19 | use CPAN::MirrorMerger::Logger::Stderr; | ||
| 20 | |||
| 21 | our $DEFAULT_LOGGER = CPAN::MirrorMerger::Logger::Stderr->new(level => $ENV{CPAN_MIRROR_MERGER_PROXY_LOG_LEVEL} || 'warn'); | ||
| 22 | our $DEFAULT_REQUEST_TIMEOUT = 10; | ||
| 23 | our $DEFAULT_RETRY_POLICY = CPAN::MirrorMerger::RetryPolicy::NoRetry->instance(); | ||
| 24 | |||
| 25 | sub new { | ||
| 26 | my $class = shift; | ||
| 27 | my $self = $class->SUPER::new(@_); | ||
| 28 | $self->{mirrors} = []; | ||
| 29 | |||
| 30 | # set default | ||
| 31 | $self->{logger} ||= $DEFAULT_LOGGER; | ||
| 32 | $self->{mirror_cache} ||= _get_default_mirror_cache($self->{logger}); | ||
| 33 | |||
| 34 | return $self; | ||
| 35 | } | ||
| 36 | |||
| 37 | sub prepare_app { | ||
| 38 | my $self = shift; | ||
| 39 | unless ($self->storage) { | ||
| 40 | die "storage is required"; | ||
| 41 | } | ||
| 42 | |||
| 43 | unless (@{ $self->{mirrors} }) { | ||
| 44 | die "mirrors are required"; | ||
| 45 | } | ||
| 46 | } | ||
| 47 | |||
| 48 | sub add_mirror { | ||
| 49 | my ($self, $mirror_url) = @_; | ||
| 50 | my $mirror = CPAN::MirrorMerger::Mirror->new($mirror_url); | ||
| 51 | $self->logger->debug("add @{[ $mirror->name ]} as mirror backend"); | ||
| 52 | push @{ $self->{mirrors} } => $mirror; | ||
| 53 | } | ||
| 54 | |||
| 55 | sub call { | ||
| 56 | my ($self, $env) = @_; | ||
| 57 | my $req = Plack::Request->new($env); | ||
| 58 | |||
| 59 | # GET/HEAD request only | ||
| 60 | unless ($req->method eq 'GET' || $req->method eq 'HEAD') { | ||
| 61 | return $self->_res_405()->finalize(); | ||
| 62 | } | ||
| 63 | |||
| 64 | # path routing | ||
| 65 | if ($req->path =~ m!^/authors/id/([A-Z])/(\1[A-Z])/\2[-A-Z0-9]*/.+$!o) { | ||
| 66 | return $self->proxt_to_storage_or_mirrors($req)->finalize(); | ||
| 67 | } elsif ($req->path eq '/modules/02packages.details.txt.gz') { | ||
| 68 | return $self->proxy_to_storage($req)->finalize(); | ||
| 69 | } | ||
| 70 | |||
| 71 | return $self->_res_404()->finalize(); | ||
| 72 | } | ||
| 73 | |||
| 74 | sub proxt_to_storage_or_mirrors { | ||
| 75 | my ($self, $req) = @_; | ||
| 76 | |||
| 77 | # try proxy to storage | ||
| 78 | my $res = $self->proxy_to_storage($req); | ||
| 79 | return $res if $res->status == 200; | ||
| 80 | |||
| 81 | # remove "/authors/id/" (e.g. D/DU/DUMMY/Foo.tar.gz) | ||
| 82 | my $path = substr $req->path, length "/authors/id/"; | ||
| 83 | my $package_info = CPAN::MirrorMerger::PackageInfo->new(path => $path); | ||
| 84 | my $content_type = Plack::MIME->mime_type($path); | ||
| 85 | for my $mirror (@{ $self->{mirrors} }) { | ||
| 86 | # fetch from mirror | ||
| 87 | my $package_path = eval { | ||
| 88 | $self->mirror_cache->get_or_fetch_package($mirror, $package_info) | ||
| 89 | }; | ||
| 90 | if (my $e = $@) { | ||
| 91 | if (CPAN::MirrorMerger::Agent::Exception::NotFound->caught($e)) { | ||
| 92 | $self->logger->debug("skip package $path on @{[ $mirror->name ]}"); | ||
| 93 | next; # skip it | ||
| 94 | } | ||
| 95 | $self->logger->error("failed to fetch package $path on @{[ $mirror->name ]}"); | ||
| 96 | die $e; | ||
| 97 | } | ||
| 98 | |||
| 99 | $self->logger->debug("found @{[ $req->path ]} from @{[ $mirror->name ]}"); | ||
| 100 | |||
| 101 | # save to storage | ||
| 102 | my $save_key = $mirror->package_path($package_info->canonicalized_path); | ||
| 103 | $self->storage->copy($package_path, $save_key); | ||
| 104 | |||
| 105 | # create response | ||
| 106 | my $fh = $package_path->openr_raw(); | ||
| 107 | my $res = Plack::Response->new(200, [ | ||
| 108 | 'Content-Type' => $content_type, | ||
| 109 | 'Content-Length' => $package_path->stat->size, | ||
| 110 | 'Cache-Control' => 'private', | ||
| 111 | ], $fh); | ||
| 112 | return $res; | ||
| 113 | } | ||
| 114 | |||
| 115 | return $self->_res_404(); | ||
| 116 | } | ||
| 117 | |||
| 118 | sub proxy_to_storage { | ||
| 119 | my ($self, $req) = @_; | ||
| 120 | my $storage_key = substr $req->path, 1; # remove first slash | ||
| 121 | my $content_type = Plack::MIME->mime_type($storage_key); | ||
| 122 | |||
| 123 | # check from storage | ||
| 124 | my $storage_path = $self->storage->fetch($storage_key); | ||
| 125 | return $self->_res_404() unless $storage_path; | ||
| 126 | |||
| 127 | # create response | ||
| 128 | $self->logger->debug("found $storage_key from storage"); | ||
| 129 | my $fh = $storage_path->openr_raw(); | ||
| 130 | my $res = Plack::Response->new(200, [ | ||
| 131 | 'Content-Type' => $content_type, | ||
| 132 | 'Content-Length' => $storage_path->stat->size, | ||
| 133 | 'Cache-Control' => 'private', | ||
| 134 | ], $fh); | ||
| 135 | return $res; | ||
| 136 | } | ||
| 137 | |||
| 138 | sub _res_404 { shift->_res_simple(404, 'Not Found') } | ||
| 139 | sub _res_405 { shift->_res_simple(405, 'Method Not Allowed') } | ||
| 140 | |||
| 141 | sub _res_simple { | ||
| 142 | my ($self, $status, $content) = @_; | ||
| 143 | return Plack::Response->new($status, [ | ||
| 144 | 'Content-Type' => 'text/plain', | ||
| 145 | 'Content-Length' => length $content, | ||
| 146 | 'Cache-Control' => 'no-cache,no-store', | ||
| 147 | ], $content); | ||
| 148 | } | ||
| 149 | |||
| 150 | sub _get_default_mirror_cache { | ||
| 151 | my $logger = shift; | ||
| 152 | return CPAN::MirrorMerger::MirrorCache->new( | ||
| 153 | cache_dir => $CPAN::MirrorMerger::DEFAULT_MIRROR_CACHE_DIR, | ||
| 154 | index_cache_timeout => $CPAN::MirrorMerger::DEFAULT_MIRROR_INDEX_CACHE_TIMEOUT, | ||
| 155 | agent => _get_default_agent($logger), | ||
| 156 | logger => $logger, | ||
| 157 | ); | ||
| 158 | } | ||
| 159 | |||
| 160 | sub _get_default_agent { | ||
| 161 | my $logger = shift; | ||
| 162 | return CPAN::MirrorMerger::Agent->new( | ||
| 163 | agent => __PACKAGE__."/$CPAN::MirrorMerger::VERSION", | ||
| 164 | timeout => $DEFAULT_REQUEST_TIMEOUT, | ||
| 165 | logger => $logger, | ||
| 166 | retry_policy => $DEFAULT_RETRY_POLICY, | ||
| 167 | ); | ||
| 168 | } | ||
| 169 | |||
| 170 | 1; | ||
| 171 | __END__ |