| 1 | use strict; | ||
| 2 | use warnings; | ||
| 3 | |||
| 4 | package Jifty::CAS::Store; | ||
| 5 | use Any::Moose; | ||
| 6 | |||
| 7 | =head1 NAME | ||
| 8 | |||
| 9 | Jifty::CAS::Store - Abstract class for Jifty's Content-Addressed Storage | ||
| 10 | |||
| 11 | =head1 DESCRIPTION | ||
| 12 | |||
| 13 | This is the abstract base class for a backend store for L<Jifty::CAS>. | ||
| 14 | For more information, see L<Jifty::CAS/DESCRIPTION>. | ||
| 15 | |||
| 16 | =cut | ||
| 17 | |||
| 18 | use Jifty::CAS::Blob; | ||
| 19 | |||
| 20 | =head2 publish DOMAIN NAME CONTENT METADATA | ||
| 21 | |||
| 22 | Publishes the given C<CONTENT> at the address C<DOMAIN> and C<NAME>. | ||
| 23 | C<METADATA> is an arbitrary hash; see L<Jifty::CAS::Blob> for more. | ||
| 24 | Returns the key. | ||
| 25 | |||
| 26 | =cut | ||
| 27 | |||
| 28 | sub publish { | ||
| 29 | my ($self, $domain, $name, $content, $opt) = @_; | ||
| 30 | $opt ||= {}; | ||
| 31 | |||
| 32 | my $blob = Jifty::CAS::Blob->new( | ||
| 33 | { content => $content, | ||
| 34 | metadata => $opt, | ||
| 35 | } | ||
| 36 | ); | ||
| 37 | return $self->_store( $domain, $name, $blob ); | ||
| 38 | } | ||
| 39 | |||
| 40 | =head2 _store DOMAIN NAME BLOB | ||
| 41 | |||
| 42 | Stores the BLOB (a L<Jifty::CAS::Blob>) in the backend. Returns the | ||
| 43 | key. Subclasses should override this, but it should not be called | ||
| 44 | directly -- use L</publish> instead. | ||
| 45 | |||
| 46 | =cut | ||
| 47 | |||
| 48 | sub _store { | ||
| 49 | die "This is an abstract base class; use one of the provided subclasses instead\n"; | ||
| 50 | } | ||
| 51 | |||
| 52 | =head2 key DOMAIN NAME | ||
| 53 | |||
| 54 | Returns the most recent key for the given pair of C<DOMAIN> and C<NAME>, | ||
| 55 | or undef if none such exists. Subclasses should override this. | ||
| 56 | |||
| 57 | =cut | ||
| 58 | |||
| 59 | sub key { | ||
| 60 | die "This is an abstract base class; use one of the provided subclasses instead\n"; | ||
| 61 | } | ||
| 62 | |||
| 63 | =head2 retrieve DOMAIN KEY | ||
| 64 | |||
| 65 | Returns a L<Jifty::CAS::Blob> for the given pair of C<DOMAIN> and | ||
| 66 | C<KEY>, or undef if none such exists. Subclasses should override this. | ||
| 67 | |||
| 68 | =cut | ||
| 69 | |||
| 70 | sub retrieve { | ||
| 71 | die "This is an abstract base class; use one of the provided subclasses instead\n"; | ||
| 72 | } | ||
| 73 | |||
| 74 | =head2 uri DOMAIN NAME | ||
| 75 | |||
| 76 | Returns a URL where the given C<DOMAIN> and C<NAME> can be accessed. | ||
| 77 | |||
| 78 | =cut | ||
| 79 | |||
| 80 | sub uri { | ||
| 81 | my $self = shift; | ||
| 82 | my ($domain, $name) = @_; | ||
| 83 | my $CDN = Jifty->config->framework('Web')->{'CDN'} || ""; | ||
| 84 | return "$CDN/__jifty/cas/$domain/" . $self->key($domain, $name); | ||
| 85 | } | ||
| 86 | |||
| 87 | =head2 serve DOMAIN ARGUMENT ENV | ||
| 88 | |||
| 89 | Serves a plack request in C<ENV>, given a C<DOMAIN> and an C<ARGUMENT>, | ||
| 90 | which may wither be a key or a name. | ||
| 91 | |||
| 92 | This correctly uses the C<If-None-Match> and C<Etag> headers to send | ||
| 93 | HTTP 304 responses to unchanged content. Additionally, the | ||
| 94 | C<content_type> key in the requested object's metadata is expected to be | ||
| 95 | set and is used for the HTTP response. This method is usually only | ||
| 96 | called by L<JiftY::CAS/wrap>, which calls this method as appropriate for | ||
| 97 | requests under C</__jifty/cas/>. | ||
| 98 | |||
| 99 | =cut | ||
| 100 | |||
| 101 | sub serve { | ||
| 102 | my ($self, $domain, $arg, $env) = @_; | ||
| 103 | |||
| 104 | my $key; | ||
| 105 | if ($arg =~ /^[a-f0-9]{32}$/) { | ||
| 106 | $key = $arg; | ||
| 107 | } else { | ||
| 108 | $key = $self->key($domain, $arg); | ||
| 109 | return $self->_serve_404( $domain, $arg, "Unable to lookup key." ) | ||
| 110 | if not defined $key; | ||
| 111 | } | ||
| 112 | |||
| 113 | my $req = Plack::Request->new($env); | ||
| 114 | my $etag = $req->header('If-None-Match'); | ||
| 115 | if ( defined $etag and $etag eq qq["$key"] ) { | ||
| 116 | Jifty->log->info("Returning 304 for CAS cached $domain:$key"); | ||
| 117 | return Plack::Response->new(304)->finalize; | ||
| 118 | } | ||
| 119 | |||
| 120 | my $obj = Jifty::CAS->retrieve($domain, $key); | ||
| 121 | return $self->_serve_404( $domain, $key, "Unable to retrieve blob." ) | ||
| 122 | if not defined $obj; | ||
| 123 | |||
| 124 | my $res = Plack::Response->new(200); | ||
| 125 | my $length = length($obj->content); | ||
| 126 | $res->content_type($obj->metadata->{content_type}); | ||
| 127 | $res->header( 'Cache-Control' => 'max-age=31536000, public' ); | ||
| 128 | $res->header( 'Expires' => HTTP::Date::time2str( time() + 31536000 ) ); | ||
| 129 | $res->header( 'ETag' => '"'.$obj->key.'"' ); | ||
| 130 | $res->header( 'Content-Length' => $length ); | ||
| 131 | $res->header( | ||
| 132 | 'Last-Modified' => HTTP::Date::time2str( $obj->metadata->{time} ) ); | ||
| 133 | |||
| 134 | Jifty->log->info("Sending $domain:$key from CAS ($length bytes)"); | ||
| 135 | $res->body($obj->content); | ||
| 136 | |||
| 137 | return $res->finalize; | ||
| 138 | } | ||
| 139 | |||
| 140 | sub _serve_404 { | ||
| 141 | my ($self, $domain, $name, $msg) = @_; | ||
| 142 | $msg ||= ''; | ||
| 143 | Jifty->log->error("Returning 404 for CAS cached $domain:$name. $msg"); | ||
| 144 | return Plack::Response->new(404)->finalize; | ||
| 145 | } | ||
| 146 | |||
| 147 | =head2 durable | ||
| 148 | |||
| 149 | Returns true if the backing store is durable -- that is, if there is a | ||
| 150 | guarantee that data placed there will be accessible from all processes at | ||
| 151 | all later times. | ||
| 152 | |||
| 153 | =cut | ||
| 154 | |||
| 155 | sub durable { 0 } | ||
| 156 | |||
| 157 | no Any::Moose; | ||
| 158 | __PACKAGE__->meta->make_immutable(inline_constructor => 0); | ||
| 159 | 1; |