| 1 | package PAGI::App::Storage::Abstract; | ||
| 2 | $PAGI::App::Storage::Abstract::VERSION = '0.001'; | ||
| 3 | 1 | ▶ | use v5.18; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | 1 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | |||
| 6 | 0 | ▶ | use Storage::Abstract; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 7 | use Future::AsyncAwait; | ||
| 8 | use Feature::Compat::Try; | ||
| 9 | use Scalar::Util qw(blessed); | ||
| 10 | use HTTP::Date qw(time2str); | ||
| 11 | use MIME::Types; | ||
| 12 | |||
| 13 | use constant ERROR_MESSAGES => { | ||
| 14 | 400 => 'Bad Request', | ||
| 15 | 403 => 'Forbidden', | ||
| 16 | 404 => 'Not Found', | ||
| 17 | 405 => 'Method Not Allowed', | ||
| 18 | }; | ||
| 19 | |||
| 20 | sub new | ||
| 21 | { | ||
| 22 | my ($class, %args) = @_; | ||
| 23 | |||
| 24 | # either storage or storage_config is required | ||
| 25 | my $storage = $args{storage} | ||
| 26 | // Storage::Abstract->new(%{$args{storage_config}}); | ||
| 27 | |||
| 28 | return bless { | ||
| 29 | storage => $storage, | ||
| 30 | mime => MIME::Types->new, | ||
| 31 | }, $class; | ||
| 32 | } | ||
| 33 | |||
| 34 | sub storage { shift->{storage} } | ||
| 35 | sub mime { shift->{mime} } | ||
| 36 | |||
| 37 | async sub call | ||
| 38 | { | ||
| 39 | my ($self, $scope, $receive, $send) = @_; | ||
| 40 | |||
| 41 | die "Unsupported scope type: $scope->{type}" if $scope->{type} ne 'http'; | ||
| 42 | |||
| 43 | try { | ||
| 44 | my $method = uc($scope->{method} // ''); | ||
| 45 | |||
| 46 | die {status => 405} | ||
| 47 | unless $method eq 'GET' || $method eq 'HEAD'; | ||
| 48 | |||
| 49 | my $path = $scope->{path}; | ||
| 50 | my $fh = $self->storage->retrieve($path, \my %info); | ||
| 51 | my ($ext) = $path =~ m{\.([^.]+)$}; | ||
| 52 | my $content_type = $ext ? $self->mime->mimeTypeOf($ext) : 'application/octet-stream'; | ||
| 53 | |||
| 54 | await $send->( | ||
| 55 | { | ||
| 56 | type => 'http.response.start', | ||
| 57 | status => 200, | ||
| 58 | headers => [ | ||
| 59 | ['content-type', $content_type], | ||
| 60 | ['content-length', $info{size}], | ||
| 61 | ['last-modified', time2str($info{mtime})], | ||
| 62 | ], | ||
| 63 | } | ||
| 64 | ); | ||
| 65 | |||
| 66 | if ($method eq 'HEAD') { | ||
| 67 | await $send->({type => 'http.response.body', body => '', more => 0}); | ||
| 68 | } | ||
| 69 | else { | ||
| 70 | await $send->( | ||
| 71 | { | ||
| 72 | type => 'http.response.body', | ||
| 73 | fh => $fh, | ||
| 74 | } | ||
| 75 | ); | ||
| 76 | } | ||
| 77 | } | ||
| 78 | catch ($e) { | ||
| 79 | if (blessed $e && $e->isa('Storage::Abstract::X::NotFound')) { | ||
| 80 | $self->_send_error($send, 404); | ||
| 81 | } | ||
| 82 | elsif (blessed $e && $e->isa('Storage::Abstract::X::PathError')) { | ||
| 83 | $self->_send_error($send, 403); | ||
| 84 | } | ||
| 85 | elsif (ref $e eq 'HASH' && $e->{status}) { | ||
| 86 | $self->_send_error($send, $e->{status}); | ||
| 87 | } | ||
| 88 | else { | ||
| 89 | # other error - let server handle it as 500 | ||
| 90 | die $e; | ||
| 91 | } | ||
| 92 | } | ||
| 93 | } | ||
| 94 | |||
| 95 | async sub _send_error | ||
| 96 | { | ||
| 97 | my ($self, $send, $status) = @_; | ||
| 98 | |||
| 99 | my $message = ERROR_MESSAGES->{$status}; | ||
| 100 | |||
| 101 | await $send->( | ||
| 102 | { | ||
| 103 | type => 'http.response.start', | ||
| 104 | status => $status, | ||
| 105 | headers => [['content-type', 'text/plain'], ['content-length', length($message)]], | ||
| 106 | } | ||
| 107 | ); | ||
| 108 | |||
| 109 | await $send->({type => 'http.response.body', body => $message, more => 0}); | ||
| 110 | } | ||
| 111 | |||
| 112 | sub to_app | ||
| 113 | { | ||
| 114 | my ($self) = @_; | ||
| 115 | |||
| 116 | return sub { $self->call(@_) }; | ||
| 117 | } | ||
| 118 | |||
| 119 | 1; | ||
| 120 | |||
| 121 | __END__ | ||