AnyEvent-HTTPD
view release on metacpan or search on metacpan
Makefile.PL
README
t/00-load.t
t/pod.t
lib/AnyEvent/HTTPD/HTTPServer.pm
lib/AnyEvent/HTTPD/HTTPConnection.pm
lib/AnyEvent/HTTPD/Request.pm
lib/AnyEvent/HTTPD/Util.pm
lib/AnyEvent/HTTPD.pm
samples/simple_example
samples/bshttp.png
samples/second_example
samples/delayed_example
samples/delayed_2_example
samples/large_response_example
t/00-load.t
t/01_basic_request.t
t/02_simple_requests.t
t/03_keep_alive.t
t/04_param.t
t/05_mp_param.t
lib/AnyEvent/HTTPD/Request.pm view on Meta::CPAN
value being the content type and the second the content.
=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>.
$req->respond ({ content => ['video/x-ms-asf', sub {
my ($data_cb) = @_;
samples/delayed_example view on Meta::CPAN
my $httpd = AnyEvent::HTTPD->new (port => 19090);
my $timer;
$httpd->reg_cb (
'' => sub {
my ($httpd, $req) = @_;
$req->respond ({ content => [ 'text/html',
"<html><body><h1>Testing return types...</h1>"
. "<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/large_response_example view on Meta::CPAN
use common::sense;
use AnyEvent;
use AnyEvent::HTTPD;
use AnyEvent::AIO;
use IO::AIO;
my $cvar = AnyEvent->condvar;
my $httpd = AnyEvent::HTTPD->new (port => 19090);
my $SEND_FILE = defined $ARGV[0] ? $ARGV[0] : 'bshttp.png';
my $mime = `file -i $SEND_FILE`;
$mime =~ s/^(.*?): //;
$mime =~ s/\r?\n$//;
print "going to send $SEND_FILE: $mime\n";
sub send_file {
my ($req) = @_;
my $fh;
samples/second_example view on Meta::CPAN
my $cvar = AnyEvent->condvar;
my $httpd = AnyEvent::HTTPD->new (port => 19090);
$httpd->reg_cb (
'' => sub {
my ($httpd, $req) = @_;
$req->respond ({ content => [ 'text/html',
"<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.517 second using v1.01-cache-2.11-cpan-df04353d9ac )