AnyEvent-HTTPD
view release on metacpan or search on metacpan
lib/AnyEvent/HTTPD/Request.pm view on Meta::CPAN
=back
Here is an example:
$httpd->reg_cb (
'/image/elmex' => sub {
my ($httpd, $req) = @_;
open IMG, "$ENV{HOME}/media/images/elmex.png"
or $req->respond (
[404, 'not found', { 'Content-Type' => 'text/plain' }, 'not found']
);
$req->respond ({ content => ['image/png', do { local $/; <IMG> }] });
}
);
B<How to send large files:>
For longer responses you can give a callback instead of a string to
the response function for the value of the C<$content>.
lib/AnyEvent/HTTPD/Request.pm view on Meta::CPAN
];
}
}
$self->{responded} = 1;
my $no_body = $self->method eq 'HEAD';
if (not defined $res) {
$rescb->(404, "ok", { 'Content-Type' => 'text/html' }, "<h1>No content</h1>", $no_body);
} else {
$rescb->(@$res, $no_body);
}
}
=item B<responded>
Returns true if this request already has been responded to.
samples/delayed_example view on Meta::CPAN
. "<img src=\"/image/bshttp.png\" />"
. "</body></html>"
]});
},
'/image/bshttp.png' => sub {
my ($httpd, $req) = @_;
$httpd->stop_request;
$timer = AnyEvent->timer (after => 3, cb => sub {
open IMG, 'bshttp.png' or do { $req->respond; return }; # respond without output will
# generate a 404
$req->respond ({ content => [ 'image/png', do { local $/; <IMG> } ] });
});
},
);
$cvar->wait;
samples/second_example view on Meta::CPAN
"<html><body><h1>Testing return types...</h1>"
. "<img src=\"/image/bshttp.png\" />"
. "</body></html>"
]});
},
'/image/bshttp.png' => sub {
$_[0]->stop_request;
open IMG, 'bshttp.png'
or do { $_[1]->respond (
[404, 'not found', { 'Content-Type' => 'text/plain' }, 'Fail!']);
return };
$_[1]->respond ({ content => [ 'image/png', do { local $/; <IMG> } ] });
},
);
$cvar->wait;
( run in 1.671 second using v1.01-cache-2.11-cpan-39bf76dae61 )