AxKit2
view release on metacpan or search on metacpan
plugins/aio/serve_file view on Meta::CPAN
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
=head1 NAME
serve_file - Plugin for serving raw files
=head1 SYNOPSIS
Plugin aio/serve_file
=head1 DESCRIPTION
This plugin turns AxKit2 into a normal every-day httpd. Yay!
Most httpds need to serve plain files. Things like favicon.ico and robots.txt
that any sane web server would be lost without. So just load this plugin after
all the others, and if your other plugins DECLINE to deliver the content, this
kind little plugin will happily deliver your file without making any changes
to it whatsoever. Ain't that nice?
=head1 CONFIG
None.
=cut
use AxKit2::Utils qw(http_date);
sub register {
my $self = shift;
$self->register_hook('response' => 'hook_response1');
$self->register_hook('response' => 'hook_response2');
}
sub hook_response1 {
my ($self, $hd) = @_;
my $ct = $hd->mime_type;
# set default return value
$self->client->notes('serve_file_retcode', DECLINED);
my $client = $self->client;
if ($hd->request_method eq 'GET' || $hd->request_method eq 'HEAD') {
# and once we have it, start serving
$self->client->watch_read(0);
my $file = $hd->filename;
$self->log(LOGINFO, "Serving file: $file");
IO::AIO::aio_stat($file, sub {
#print "STAT returned\n";
if (!-e _) {
$client->notes('serve_file_retcode', NOT_FOUND);
return $client->finish_continuation;
}
# we only serve files here...
if (!-f _) {
$client->notes('serve_file_retcode', BAD_REQUEST);
return $client->finish_continuation;
}
my $mtime = http_date((stat(_))[9]);
my $ifmod = $client->headers_in->header('If-Modified-Since') || "";
my $ifmod_len = 0;
if ($ifmod =~ s/; length=(\d+)//) {
$ifmod_len = $1;
}
my $modified = $ifmod ? ($ifmod ne $mtime) : 1;
my $size = -s _;
$modified++ if $ifmod_len && $ifmod_len != $size;
if (!$modified) {
$client->notes('serve_file_retcode', NOT_MODIFIED);
return $client->finish_continuation;
}
$client->headers_out->header("Last-Modified", $mtime);
$client->headers_out->header("Content-Length", $size);
$client->headers_out->header("Content-Type", $ct);
$client->send_http_headers;
$client->notes('serve_file_retcode', OK);
if ($hd->request_method eq 'HEAD') {
return $client->finish_continuation;
}
IO::AIO::aio_open($file, 0, 0, sub {
#print "OPEN returned\n";
my $fh = shift;
if ($client->{closed}) {
return CORE::close($fh);
}
if (!$fh) {
$client->notes('serve_file_retcode', SERVER_ERROR);
return $client->close('aio_open_failure');
}
$client->notes('serve_file_bytes_remaining', $size);
$client->watch_write(1);
my $send_sub = sub {
my $remaining = $client->notes('serve_file_bytes_remaining');
# print "sending $remaining bytes...\n";
if ($remaining <= 0) {
CORE::close($fh);
#$client->watch_write(0);
return $client->finish_continuation;
}
# AIO version
# IO::AIO::aio_sendfile($client->sock, $fh,
# ($size - $remaining), $remaining,
# sub {
# my $sent = shift;
( run in 1.058 second using v1.01-cache-2.11-cpan-39bf76dae61 )