libanyevent-httpd-perl-0.93/ 0000755 0001750 0001750 00000000000 11616455751 015015 5 ustar dimka dimka libanyevent-httpd-perl-0.93/README 0000644 0001750 0001750 00000025402 11616455751 015700 0 ustar dimka dimka NAME
AnyEvent::HTTPD - A simple lightweight event based web (application)
server
VERSION
Version 0.93
SYNOPSIS
use AnyEvent::HTTPD;
my $httpd = AnyEvent::HTTPD->new (port => 9090);
$httpd->reg_cb (
'/' => sub {
my ($httpd, $req) = @_;
$req->respond ({ content => ['text/html',
"
Hello World!
"
. "another test page"
. ""
]});
},
'/test' => sub {
my ($httpd, $req) = @_;
$req->respond ({ content => ['text/html',
"Test page
"
. "Back to the main page"
. ""
]});
},
);
$httpd->run; # making a AnyEvent condition variable would also work
DESCRIPTION
This module provides a simple HTTPD for serving simple web application
interfaces. It's completly event based and independend from any event
loop by using the AnyEvent module.
It's HTTP implementation is a bit hacky, so before using this module
make sure it works for you and the expected deployment. Feel free to
improve the HTTP support and send in patches!
The documentation is currently only the source code, but next versions
of this module will be better documented hopefully. See also the
"samples/" directory in the AnyEvent::HTTPD distribution for basic
starting points.
FEATURES
* support for GET and POST requests.
* support for HTTP 1.0 keep-alive.
* processing of "x-www-form-urlencoded" and "multipart/form-data"
("multipart/mixed") encoded form parameters.
* support for streaming responses.
* with version 0.8 no more dependend on LWP for HTTP::Date.
* (limited) support for SSL
METHODS
The AnyEvent::HTTPD class inherits directly from
AnyEvent::HTTPD::HTTPServer which inherits the event callback interface
from Object::Event.
Event callbacks can be registered via the Object::Event API (see the
documentation of Object::Event for details).
For a list of available events see below in the *EVENTS* section.
new (%args)
This is the constructor for a AnyEvent::HTTPD object. The %args hash
may contain one of these key/value pairs:
host => $host
The TCP address of the HTTP server will listen on. Usually
0.0.0.0 (the default), for a public server, or 127.0.0.1 for a
local server.
port => $port
The TCP port the HTTP server will listen on. If undefined some
free port will be used. You can get it via the "port" method.
ssl => $tls_ctx
If this option is given the server will listen for a SSL/TLS
connection on the configured port. As $tls_ctx you can pass
anything that you can pass as "tls_ctx" to an AnyEvent::Handle
object.
Example:
my $httpd =
AnyEvent::HTTPD->new (
port => 443,
ssl => { cert_file => "/path/to/my/server_cert_and_key.pem" }
);
Or:
my $httpd =
AnyEvent::HTTPD->new (
port => 443,
ssl => AnyEvent::TLS->new (...),
);
request_timeout => $seconds
This will set the request timeout for connections. The default
value is 60 seconds.
backlog => $int
The backlog argument defines the maximum length the queue of
pending connections may grow to. The real maximum queue length
will be 1.5 times more than the value specified in the backlog
argument.
See also "man 2 listen".
By default will be set by AnyEvent::Socket"::tcp_server" to 128.
connection_class => $class
This is a special parameter that you can use to pass your own
connection class to AnyEvent::HTTPD::HTTPServer. This is only of
interest to you if you plan to subclass
AnyEvent::HTTPD::HTTPConnection.
request_class => $class
This is a special parameter that you can use to pass your own
request class to AnyEvent::HTTPD. This is only of interest to
you if you plan to subclass AnyEvent::HTTPD::Request.
allowed_methods => $arrayref
This parameter sets the allowed HTTP methods for requests,
defaulting to GET, HEAD and POST. Each request received is
matched against this list, and a '501 not implemented' is
returned if no match is found. Requests using disallowed
handlers will never trigger callbacks.
port
Returns the port number this server is bound to.
host
Returns the host/ip this server is bound to.
allowed_methods
Returns an arrayref of allowed HTTP methods, possibly as set by the
allowed_methods argument to the constructor.
stop_request
When the server walks the request URI path upwards you can stop the
walk by calling this method. You can even stop further handling
after the "request" event.
Example:
$httpd->reg_cb (
'/test' => sub {
my ($httpd, $req) = @_;
# ...
$httpd->stop_request; # will prevent that the callback below is called
},
'' => sub { # this one wont be called by a request to '/test'
my ($httpd, $req) = @_;
# ...
}
);
run This method is a simplification of the "AnyEvent" condition variable
idiom. You can use it instead of writing:
my $cvar = AnyEvent->condvar;
$cvar->wait;
stop
This will stop the HTTP server and return from the "run" method if
you started the server via that method!
EVENTS
Every request goes to a specific URL. After a (GET or POST) request is
received the URL's path segments are walked down and for each segment a
event is generated. An example:
If the URL '/test/bla.jpg' is requestes following events will be
generated:
'/test/bla.jpg' - the event for the last segment
'/test' - the event for the 'test' segment
'' - the root event of each request
To actually handle any request you just have to register a callback for
the event name with the empty string. To handle all requests in the
'/test' directory you have to register a callback for the event with the
name '/test'. Here is an example how to register an event for the
example URL above:
$httpd->reg_cb (
'/test/bla.jpg' => sub {
my ($httpd, $req) = @_;
$req->respond ([200, 'ok', { 'Content-Type' => 'text/html' }, 'Test
' }]);
}
);
See also "stop_request" about stopping the walk of the path segments.
The first argument to such a callback is always the AnyEvent::HTTPD
object itself. The second argument ($req) is the
AnyEvent::HTTPD::Request object for this request. It can be used to get
the (possible) form parameters for this request or the transmitted
content and respond to the request.
Along with the above mentioned events these events are also provided:
request => $req
Every request also emits the "request" event, with the same
arguments and semantics as the above mentioned path request events.
You can use this to implement your own request multiplexing. You can
use "stop_request" to stop any further processing of the request as
the "request" event is the first thing that is executed for an
incoming request.
An example of one of many possible uses:
$httpd->reg_cb (
request => sub {
my ($httpd, $req) = @_;
my $url = $req->url;
if ($url->path =~ /\/images\/img_(\d+).jpg$/) {
handle_image_request ($req, $1); # your task :)
# stop the request from emitting further events
# so that the '/images/img_001.jpg' and the
# '/images' and '' events are NOT emitted:
$httpd->stop_request;
}
}
);
client_connected => $host, $port
client_disconnected => $host, $port
These events are emitted whenever a client coming from "$host:$port"
connects to your server or is disconnected from it.
CACHING
Any response from the HTTP server will have "Cache-Control" set to
"max-age=0" and also the "Expires" header set to the "Date" header.
Meaning: Caching is disabled.
You can of course set those headers yourself in the response, or remove
them by setting them to undef, but keep in mind that the default for
those headers are like mentioned above.
If you need more support here you can send me a mail or even better: a
patch :)
AUTHOR
Robin Redeker, ""
BUGS
Please report any bugs or feature requests to "bug-bs-httpd at
rt.cpan.org", or through the web interface at
. I will
be notified, and then you'll automatically be notified of progress on
your bug as I make changes.
SUPPORT
You can find documentation for this module with the perldoc command.
perldoc AnyEvent::HTTPD
You can also look for information at:
* Git repository
* RT: CPAN's request tracker
* AnnoCPAN: Annotated CPAN documentation
* CPAN Ratings
* Search CPAN
ACKNOWLEDGEMENTS
Andrey Smirnov - for keep-alive patches.
Pedro Melo - for valuable input in general and patches.
Nicholas Harteau - patch for ';' pair separator support,
patch for allowed_methods support
Chris Kastorff - patch for making default headers removable
and more fault tolerant w.r.t. case.
Mons Anderson - Optimizing the regexes in L
and adding the C option to L.
COPYRIGHT & LICENSE
Copyright 2008-2011 Robin Redeker, all rights reserved.
This program is free software; you can redistribute it and/or modify it
under the same terms as Perl itself.
libanyevent-httpd-perl-0.93/Makefile.PL 0000644 0001750 0001750 00000001631 11521521042 016746 0 ustar dimka dimka use strict;
use warnings;
use ExtUtils::MakeMaker;
WriteMakefile(
NAME => 'AnyEvent::HTTPD',
AUTHOR => 'Robin Redeker ',
VERSION_FROM => 'lib/AnyEvent/HTTPD.pm',
ABSTRACT_FROM => 'lib/AnyEvent/HTTPD.pm',
LICENSE => 'perl',
PL_FILES => {},
PREREQ_PM => {
'Test::More' => 0,
'AnyEvent' => 0,
'Object::Event' => 0,
'URI' => 0,
'Time::Local' => 0,
'common::sense' => 0,
'AnyEvent::HTTP' => 0,
'bytes' => 0,
'Compress::Zlib' => 0,
},
dist => { COMPRESS => 'gzip -9f', SUFFIX => 'gz',
PREOP => 'pod2text lib/AnyEvent/HTTPD.pm | tee README >$(DISTVNAME)/README; chmod -R u=rwX,go=rX . ;',
},
clean => { FILES => 'AnyEvent-HTTPD-*' },
);
libanyevent-httpd-perl-0.93/META.yml 0000644 0001750 0001750 00000001375 11616455751 016274 0 ustar dimka dimka --- #YAML:1.0
name: AnyEvent-HTTPD
version: 0.93
abstract: A simple lightweight event based web (application) server
author:
- Robin Redeker
license: perl
distribution_type: module
configure_requires:
ExtUtils::MakeMaker: 0
build_requires:
ExtUtils::MakeMaker: 0
requires:
AnyEvent: 0
AnyEvent::HTTP: 0
bytes: 0
common::sense: 0
Compress::Zlib: 0
Object::Event: 0
Test::More: 0
Time::Local: 0
URI: 0
no_index:
directory:
- t
- inc
generated_by: ExtUtils::MakeMaker version 6.57_05
meta-spec:
url: http://module-build.sourceforge.net/META-spec-v1.4.html
version: 1.4
libanyevent-httpd-perl-0.93/t/ 0000755 0001750 0001750 00000000000 11616455751 015260 5 ustar dimka dimka libanyevent-httpd-perl-0.93/t/01_basic_request.t 0000644 0001750 0001750 00000002013 11537634345 020572 0 ustar dimka dimka #!perl
use common::sense;
use Test::More tests => 4;
use AnyEvent::Impl::Perl;
use AnyEvent;
use AnyEvent::Handle;
use AnyEvent::Socket;
use AnyEvent::HTTPD;
use AnyEvent::HTTPD::Util;
my $h = AnyEvent::HTTPD->new (port => 19090);
my $req_url;
my $req_url2;
my $req_method;
$h->reg_cb (
'' => sub {
my ($httpd, $req) = @_;
$req_url = $req->url->path;
},
'/test' => sub {
my ($httpd, $req) = @_;
$req_url2 = $req->url->path;
$req_method = $req->method;
$req->respond ({ content => ['text/plain', "Test response"] });
},
);
my $c = AnyEvent::HTTPD::Util::test_connect ('127.0.0.1', $h->port,
"GET\040http://localhost:19090/test\040HTTP/1.0\015\012\015\012");
my $buf = $c->recv;
my ($head, $body) = split /\015\012\015\012/, $buf, 2;
is ($req_url, "/test", "the path of the request URL was ok");
is ($req_url2, "/test", "the path of the second request URL was ok");
is ($req_method, 'GET', 'Correct method used');
is ($body, 'Test response', "the response text was ok");
libanyevent-httpd-perl-0.93/t/pod.t 0000644 0001750 0001750 00000000341 11444066620 016215 0 ustar dimka dimka #!perl -T
use common::sense;
use Test::More;
# Ensure a recent version of Test::Pod
my $min_tp = 1.22;
eval "use Test::Pod $min_tp";
plan skip_all => "Test::Pod $min_tp required for testing POD" if $@;
all_pod_files_ok();
libanyevent-httpd-perl-0.93/t/05_mp_param.t 0000644 0001750 0001750 00000004243 11444066620 017540 0 ustar dimka dimka #!perl
use common::sense;
use Test::More tests => 5;
use AnyEvent::Impl::Perl;
use AnyEvent;
use AnyEvent::HTTPD;
use AnyEvent::Socket;
my $c = AnyEvent->condvar;
my $h = AnyEvent::HTTPD->new;
my %params;
$h->reg_cb (
'/test' => sub {
my ($httpd, $req) = @_;
(%params) = $req->vars;
$req->respond ({
content => ['text/plain', "Test response"]
});
},
);
my $hdl;
my $buf;
tcp_connect '127.0.0.1', $h->port, sub {
my ($fh) = @_
or die "couldn't connect: $!";
$hdl =
AnyEvent::Handle->new (
fh => $fh,
on_read => sub { $hdl->rbuf = '' });
my $cont =
"--AaB03x\015\012Content-Disposition: form-data; name=\"submit-name\"\015\012"
. "\015\012Larry\015\012--AaB03x\015\012Content-Disposition: form-data; name=\"files\"; filename=\"file1.txt\"\015\012Content-Type: text/plain\015\012\015\012Test\015\012Test2\015\012"
. "--AaB03x\015\012Content-Disposition: form-data; name=\"files2\"; filename=\"file2.txt\"\015\012Content-Type: text/plain\015\012\015\012Test 2\015\012Test2\015\012"
. "--AaB03x\015\012Content-Disposition: form-data; name=\"files3\";\015\012Content-Type: multipart/mixed, boundary=BbC04y\015\012\015\012"
. "--BbC04y\015\012Content-disposition: attachment; filename=\"fileX1.txt\"\015\012Content-Type: text/plain\015\012\015\012"
. "BLABLABLA\015\012"
. "--BbC04y\015\012Content-disposition: attachment; filename=\"fileX2.xml\"\015\012Content-type: image/gif\015\012\015\012"
. "XXXXXXXXXXXXXXXXXXXX\015\012"
."--BbC04y--\015\012\015\012"
. "--AaB03x--\015\012";
$hdl->push_read (line => sub { $c->send });
$hdl->push_write (
"POST\040http://localhost:19090/test\040HTTP/1.0\015\012"
. "Content-Type: multipart/form-data; boundary=AaB03x\015\012"
. "Content-Length: " . length ($cont) . "\015\012\015\012$cont"
);
};
$c->recv;
is ($params{'submit-name'}, "Larry", "submit name");
is ($params{files}, "Test\015\012Test2", "files 1");
is ($params{files2}, "Test 2\015\012Test2", "files 2");
is ($params{files3}->[0], "BLABLABLA", "files 3.1");
is ($params{files3}->[1], "XXXXXXXXXXXXXXXXXXXX", "files 3.2");
libanyevent-httpd-perl-0.93/t/10_allowed_methods.t 0000644 0001750 0001750 00000003775 11537634345 021133 0 ustar dimka dimka #!perl
use common::sense;
use Test::More tests => 12;
use AnyEvent::Impl::Perl;
use AnyEvent;
use AnyEvent::HTTP;
use AnyEvent::HTTPD qw/http_request/;
my ($H, $P);
# make sure the default is GET HEAD POST
my $c = AnyEvent->condvar;
my $h = AnyEvent::HTTPD->new;
$h->reg_cb (
'' => sub {
my ($httpd, $req) = @_;
ok(scalar (grep { $req->method eq $_ } qw/GET HEAD POST/) == 1, "req " . $req->method );
if ($req->method eq 'POST')
{
ok($req->content eq 'hello world', "req POST body");
}
$req->respond({ content => ['text/plain', $req->method . " OK" ]});
},
client_connected => sub {
my ($httpd, $h, $p) = @_;
($H, $P) = ($h, $p);
},
);
is_deeply( $h->allowed_methods, [qw/GET HEAD POST/], 'allowed_methods()' );
http_request(
GET => sprintf("http://%s:%d/foo", '127.0.0.1', $h->port),
sub {
my ($body, $hdr) = @_;
ok($hdr->{'Status'} == 200, "resp GET 200 OK")
or diag explain $hdr;
ok($body eq 'GET OK', 'resp GET body OK')
or diag explain $body;
$c->send;
}
);
$c->recv;
$c = AnyEvent->condvar;
http_request(
POST => sprintf("http://%s:%d/foo", '127.0.0.1', $h->port),
body => 'hello world',
sub {
my ($body, $hdr) = @_;
ok($hdr->{'Status'} == 200, "resp POST 200 OK")
or diag explain $hdr;
ok($body eq 'POST OK', 'resp POST body OK')
or diag explain $body;
$c->send;
}
);
$c->recv;
$c = AnyEvent->condvar;
http_request(
HEAD => sprintf("http://%s:%d/foo", '127.0.0.1', $h->port),
sub {
my ($body, $hdr) = @_;
ok($hdr->{'Status'} == 200, "resp HEAD 200 OK")
or diag explain $hdr;
$c->send;
}
);
$c->recv;
$c = AnyEvent->condvar;
http_request(
OPTIONS => sprintf("http://%s:%d/foo", '127.0.0.1', $h->port),
sub {
my ($body, $hdr) = @_;
ok($hdr->{'Status'} == 501, "resp OPTIONS 501")
or diag explain $hdr;
ok($hdr->{'Reason'} == 'not implemented', 'resp OPTIONS reason')
or diag explain $hdr;
$c->send;
}
);
$c->recv;
done_testing();
libanyevent-httpd-perl-0.93/t/04_param.t 0000644 0001750 0001750 00000001221 11537634345 017044 0 ustar dimka dimka #!perl
use common::sense;
use Test::More tests => 2;
use AnyEvent::Impl::Perl;
use AnyEvent;
use AnyEvent::HTTPD;
my $h = AnyEvent::HTTPD->new (port => 19090);
my $req_q;
my $req_n;
$h->reg_cb (
'/test' => sub {
my ($httpd, $req) = @_;
$req_q = $req->parm ('q');
$req_n = $req->parm ('n');
$req->respond ({ content => ['text/plain', "Test response"] });
},
);
my $c = AnyEvent::HTTPD::Util::test_connect ('127.0.0.1', $h->port,
"GET\040http://localhost:19090/test?q=%3F%3F&n=%3F2%3F\040HTTP/1.0\015\012\015\012");
$c->recv;
is ($req_q, "??", "parameter q correct");
is ($req_n, "?2?", "parameter n correct");
libanyevent-httpd-perl-0.93/t/03_keep_alive.t 0000644 0001750 0001750 00000001753 11534654730 020056 0 ustar dimka dimka #!perl
use common::sense;
use Test::More tests => 1;
use AnyEvent::Impl::Perl;
use AnyEvent;
use AnyEvent::HTTPD;
use AnyEvent::Socket;
my $c = AnyEvent->condvar;
my $h = AnyEvent::HTTPD->new;
my $cnt = 0;
$h->reg_cb (
'/test' => sub {
my ($httpd, $req) = @_;
$cnt++;
$req->respond ({ content => ['text/plain', "Test response ($cnt)"] });
},
);
my $hdl;
my $buf;
tcp_connect '127.0.0.1', $h->port, sub {
my ($fh) = @_
or die "couldn't connect: $!";
$hdl =
AnyEvent::Handle->new (
fh => $fh, on_eof => sub { $c->send },
on_read => sub {
$buf .= $hdl->rbuf;
$hdl->rbuf = '';
if ($buf =~ /Test response \(2\)/) {
$c->send;
}
});
for (1..2) {
$hdl->push_write (
"GET\040http://localhost:19090/test\040HTTP/1.0\015\012"
. "Connection: Keep-Alive\015\012\015\012"
);
}
};
$c->recv;
is ($cnt, 2, 'two requests over one connection');
libanyevent-httpd-perl-0.93/t/07_param_semicolon.t 0000644 0001750 0001750 00000001252 11537634345 021123 0 ustar dimka dimka #!perl
use common::sense;
use Test::More tests => 2;
use AnyEvent::Impl::Perl;
use AnyEvent;
use AnyEvent::HTTPD;
use AnyEvent::HTTPD::Util;
my $h = AnyEvent::HTTPD->new (port => 19090);
my $req_q;
my $req_n;
$h->reg_cb (
'/test' => sub {
my ($httpd, $req) = @_;
$req_q = $req->parm ('q');
$req_n = $req->parm ('n');
$req->respond ({ content => ['text/plain', "Test response"] });
},
);
my $c = AnyEvent::HTTPD::Util::test_connect ('127.0.0.1', $h->port,
"GET\040http://localhost:19090/test?q=%3F%3F;n=%3F2%3F\040HTTP/1.0\015\012\015\012");
$c->recv;
is ($req_q, "??", "parameter q correct");
is ($req_n, "?2?", "parameter n correct");
libanyevent-httpd-perl-0.93/t/12_head_no_body.t 0000644 0001750 0001750 00000001002 11537634345 020352 0 ustar dimka dimka #!perl
use common::sense;
use Test::More tests => 1;
use AnyEvent::Impl::Perl;
use AnyEvent;
use AnyEvent::HTTPD;
use AnyEvent::Socket;
my $h = AnyEvent::HTTPD->new;
$h->reg_cb (
'/test' => sub {
my ($httpd, $req) = @_;
$req->respond ({ content => ['text/plain', "31337"] });
},
);
my $c = AnyEvent::HTTPD::Util::test_connect ('127.0.0.1', $h->port,
"HEAD\040http://localhost:19090/test\040HTTP/1.0\015\012\015\012");
my $buf = $c->recv;
ok ($buf !~ /31337/, "no body received");
libanyevent-httpd-perl-0.93/t/06_long_resp.t 0000644 0001750 0001750 00000001474 11537634345 017750 0 ustar dimka dimka #!perl
use common::sense;
use Test::More tests => 2;
use AnyEvent::Impl::Perl;
use AnyEvent;
use AnyEvent::HTTPD;
use AnyEvent::Socket;
my $h = AnyEvent::HTTPD->new;
my $SEND = "ELMEXBLABLA1235869302893095934";#"ABCDEF" x 1024;
my $SENT = $SEND;
$h->reg_cb (
'/test' => sub {
my ($httpd, $req) = @_;
$req->respond ({
content => ['text/plain', sub {
my ($data_cb) = @_;
return unless $data_cb;
$data_cb->(substr $SENT, 0, 10, '');
}]
});
},
);
my $c = AnyEvent::HTTPD::Util::test_connect ('127.0.0.1', $h->port,
"GET\040http://localhost:19090/test\040HTTP/1.0\015\012\015\012");
my $buf = $c->recv;
$buf =~ s/^.*?\015?\012\015?\012//s;
ok (length ($buf) == length ($SEND), 'sent all data');
ok (length ($SENT) == 0, 'send buf empty');
libanyevent-httpd-perl-0.93/t/14_header_unset.t 0000644 0001750 0001750 00000004061 11616454612 020413 0 ustar dimka dimka #!perl
use common::sense;
use Test::More tests => 8;
use AnyEvent::Impl::Perl;
use AnyEvent;
use AnyEvent::HTTPD;
my $h = AnyEvent::HTTPD->new (port => 19090);
$h->reg_cb (
'/header-unset' => sub {
my ($httpd, $req) = @_;
$req->respond (
[200, 'OK', {
'Cache-Control' => undef,
'Expires' => undef,
'Content-Length' => undef,
}, "Test response"]);
},
'/header-override-lowercase' => sub {
my ($httpd, $req) = @_;
$req->respond (
[200, 'OK', {
'cache-control' => "nonsensical",
}, "Test response"]);
},
'/header-override-uppercase' => sub {
my ($httpd, $req) = @_;
$req->respond (
[200, 'OK', {
'CACHE-CONTROL' => "nonsensical",
}, "Test response"]);
},
);
my $c1 = AnyEvent::HTTPD::Util::test_connect ('127.0.0.1', $h->port,
"GET\040/header-unset\040HTTP/1.0\015\012Connection: Keep-Alive\015\012\015\012");
my $c2 = AnyEvent::HTTPD::Util::test_connect ('127.0.0.1', $h->port,
"GET\040/header-override-lowercase\040HTTP/1.0\015\012\015\012");
my $c3 = AnyEvent::HTTPD::Util::test_connect ('127.0.0.1', $h->port,
"GET\040/header-override-uppercase\040HTTP/1.0\015\012\015\012");
my $r1 = $c1->recv;
my $r2 = $c2->recv;
my $r3 = $c3->recv;
unlike ($r1, qr/^expires:/im, "Can unset Expires header");
unlike ($r1, qr/^cache-control:/im, "Can unset Cache-Control header");
unlike ($r1, qr/^content-length:/im, "Can unset Content-Length header");
unlike ($r1, qr/^connection:\s*close$/im,
"Unsetting Content-Length implies no keep-alive");
like ($r2, qr/^cache-control:\s*nonsensical/im,
"Cache-Control set with lowercase gets through");
unlike ($r2, qr/^cache-control:\s*max-age/im,
"Cache-Control set with lowercase removes default header");
like ($r3, qr/^cache-control:\s*nonsensical/im,
"Cache-Control set with uppercase gets through");
unlike ($r3, qr/^cache-control:\s*max-age/im,
"Cache-Control set with uppercase removes default header");
libanyevent-httpd-perl-0.93/t/00-load.t 0000644 0001750 0001750 00000000233 11444066620 016567 0 ustar dimka dimka #!perl -T
use Test::More tests => 1;
BEGIN {
use_ok( 'AnyEvent::HTTPD' );
}
diag( "Testing AnyEvent::HTTPD $AnyEvent::HTTPD::VERSION, Perl $], $^X" );
libanyevent-httpd-perl-0.93/t/02_simple_requests.t 0000644 0001750 0001750 00000003211 11444066620 021157 0 ustar dimka dimka #!perl
use common::sense;
use Test::More tests => 8;
use AnyEvent::Impl::Perl;
use AnyEvent;
use AnyEvent::HTTPD;
use AnyEvent::Socket;
my $c = AnyEvent->condvar;
my $h = AnyEvent::HTTPD->new;
my $req_url;
my $req_hdr;
my ($H, $P);
$h->reg_cb (
'/test' => sub {
my ($httpd, $req) = @_;
$req_hdr = $req->headers->{'content-type'};
$req->respond ({
content => [
'text/plain',
"Test response\0"
. $req->client_host . "\0"
. $req->client_port
]
});
},
client_connected => sub {
my ($httpd, $h, $p) = @_;
ok ($h ne '', "got client host");
ok ($p ne '', "got client port");
($H, $P) = ($h, $p);
},
client_disconnected => sub {
my ($httpd, $h, $p) = @_;
is ($h, $H, "got client host disconnect");
is ($p, $P, "got client port disconnect");
}
);
my $hdl;
my $buf;
tcp_connect '127.0.0.1', $h->port, sub {
my ($fh) = @_
or die "couldn't connect: $!";
$hdl =
AnyEvent::Handle->new (
fh => $fh, on_eof => sub { $c->send ($buf) },
on_read => sub {
$buf .= $hdl->rbuf;
$hdl->rbuf = '';
});
$hdl->push_write (
"GET\040http://localhost:19090/test\040HTTP/1.0\015\012Content-Length:\015\012 10\015\012Content-Type: text/html;\015\012 charSet = \"ISO-8859-1\"; Foo=1\015\012\015\012ABC1234567"
);
};
my $r = $c->recv;
my ($tr, $host, $port) = split /\0/, $r;
ok ($tr =~ /Test response/m, 'test response ok');
ok ($req_hdr =~ /Foo/, 'test header ok');
ok ($host ne '', 'got a client host: ' . $host);
ok ($port ne '', 'got a client port: ' . $port);
libanyevent-httpd-perl-0.93/t/11_denied_methods.t 0000644 0001750 0001750 00000004120 11537634345 020716 0 ustar dimka dimka #!perl
use common::sense;
use Test::More tests => 13;
use AnyEvent::Impl::Perl;
use AnyEvent;
use AnyEvent::HTTP;
use AnyEvent::HTTPD qw/http_request/;
my ($H, $P);
# allow options, disallow POST
my $c = AnyEvent->condvar;
my $h = AnyEvent::HTTPD->new( allowed_methods => [qw/GET HEAD OPTIONS/] );
$h->reg_cb (
'' => sub {
my ($httpd, $req) = @_;
ok(scalar (grep { $req->method eq $_ } qw/GET HEAD OPTIONS/) == 1, "req " . $req->method );
if ($req->method eq 'POST')
{
ok(0, "got disallowed request");
$req->respond({ content => ['text/plain', $req->method . "NOT OK" ]});
}
else
{
ok(1, "got allowed request");
$req->respond({ content => ['text/plain', $req->method . " OK" ]});
}
},
client_connected => sub {
my ($httpd, $h, $p) = @_;
($H, $P) = ($h, $p);
},
);
is_deeply( $h->allowed_methods, [qw/GET HEAD OPTIONS/], 'allowed_methods()' );
http_request(
GET => sprintf("http://%s:%d/foo", '127.0.0.1', $h->port),
sub {
my ($body, $hdr) = @_;
ok($hdr->{'Status'} == 200, "resp GET 200 OK")
or diag explain $hdr;
ok($body eq 'GET OK', 'resp GET body OK')
or diag explain $body;
$c->send;
}
);
$c->recv;
$c = AnyEvent->condvar;
http_request(
POST => sprintf("http://%s:%d/foo", '127.0.0.1', $h->port),
body => 'hello world',
sub {
my ($body, $hdr) = @_;
ok($hdr->{'Status'} == 501, "resp POST 501")
or diag explain $hdr;
ok($hdr->{'Reason'} == 'not implemented', 'resp POST reason')
or diag explain $hdr;
$c->send;
}
);
$c->recv;
$c = AnyEvent->condvar;
http_request(
HEAD => sprintf("http://%s:%d/foo", '127.0.0.1', $h->port),
sub {
my ($body, $hdr) = @_;
ok($hdr->{'Status'} == 200, "resp HEAD 200 OK")
or diag explain $hdr;
$c->send;
}
);
$c->recv;
$c = AnyEvent->condvar;
http_request(
OPTIONS => sprintf("http://%s:%d/foo", '127.0.0.1', $h->port),
sub {
my ($body, $hdr) = @_;
ok($hdr->{'Status'} == 200, "resp OPTIONS OK")
or diag explain $hdr;
$c->send;
}
);
$c->recv;
done_testing();
libanyevent-httpd-perl-0.93/MANIFEST 0000644 0001750 0001750 00000001222 11616455751 016143 0 ustar dimka dimka Changes
MANIFEST
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
t/06_long_resp.t
t/07_param_semicolon.t
t/10_allowed_methods.t
t/11_denied_methods.t
t/12_head_no_body.t
t/14_header_unset.t
META.yml Module meta-data (added by MakeMaker)
libanyevent-httpd-perl-0.93/samples/ 0000755 0001750 0001750 00000000000 11616455751 016461 5 ustar dimka dimka libanyevent-httpd-perl-0.93/samples/second_example 0000755 0001750 0001750 00000001367 11444066620 021374 0 ustar dimka dimka #!/opt/perl/bin/perl
use common::sense;
use AnyEvent;
use AnyEvent::HTTPD;
my $cvar = AnyEvent->condvar;
my $httpd = AnyEvent::HTTPD->new (port => 19090);
$httpd->reg_cb (
'' => sub {
my ($httpd, $req) = @_;
$req->respond ({ content => [ 'text/html',
"Testing return types...
"
. "
"
. ""
]});
},
'/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 $/;
} ] });
},
);
$cvar->wait;
libanyevent-httpd-perl-0.93/samples/large_response_example 0000755 0001750 0001750 00000005172 11444066620 023127 0 ustar dimka dimka #!/opt/perl/bin/perl
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;
my $last_pos = 0;
print "going to open $SEND_FILE...\n";
# use IO::AIO to async open the file
aio_open $SEND_FILE, O_RDONLY, 0, sub {
$fh = shift;
unless ($fh) {
warn "couldn't open $SEND_FILE: $!\n";
$data_cb->(); # stop sending data...
return;
}
my $size = -s $fh;
print "opened $SEND_FILE, $size bytes big!\n";
# make a reader callback, that will be called
# whenever a chunk of data was written out to the kernel
my $get_chunk_cb = sub {
my ($data_cb) = @_;
if ($data_cb) {
print "get next chunk, $last_pos of $size!\n";
} else {
print "sent last chunk, no more required!\n";
}
return unless $data_cb; # in case the connection went away...
my $chunk = '';
# use IO::AIO again, to async read from disk
# you decide what chunks you want to send btw.
# here we send 4096 bytes on each chunk read.
aio_read $fh, $last_pos, 4096, $chunk, 0, sub {
if ($_[0] > 0) {
$last_pos += $_[0];
print "read $_[0] bytes, sending them...\n";
$data_cb->($chunk); # when we got another chunk, push it
# over the http connection;
$chunk = '';
# and here we just return, and wait for the next call to
# $get_chunk_cb when the data is in the kernel.
} else {
$data_cb->(); # stop sending data (in case of error or EOF)
return;
}
};
};
$req->respond (
[
200, 'ok', {
'Content-Type' => $mime,
# 'Content-Length' => $size
},
$get_chunk_cb
]
);
};
}
$httpd->reg_cb (
'' => sub {
my ($httpd, $req) = @_;
$req->respond ({ content => ['text/html', <<'CONT']});
Large Download Example!
download file
CONT
},
'/test' => sub {
my ($httpd, $req) = @_;
$httpd->stop_request;
print "sending file ...\n";
send_file ($req);
},
);
$cvar->wait;
libanyevent-httpd-perl-0.93/samples/delayed_example 0000755 0001750 0001750 00000001543 11444066620 021524 0 ustar dimka dimka #!/opt/perl/bin/perl
use common::sense;
use AnyEvent;
use AnyEvent::HTTPD;
my $cvar = AnyEvent->condvar;
my $httpd = AnyEvent::HTTPD->new (port => 19090);
my $timer;
$httpd->reg_cb (
'' => sub {
my ($httpd, $req) = @_;
$req->respond ({ content => [ 'text/html',
"Testing return types...
"
. "
"
. ""
]});
},
'/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 $/;
} ] });
});
},
);
$cvar->wait;
libanyevent-httpd-perl-0.93/samples/bshttp.png 0000644 0001750 0001750 00000041617 11444066620 020474 0 ustar dimka dimka PNG
IHDR , P pHYs tIME52`ԋ tEXtComment Created with The GIMPd%n IDATxy@g &@آAYD(*V-BEEZTZ[Պ*}+u)*EEAGJ{q2sf;yγ b b b b b b b b v v v v v v v v v b b b b b b b @@ ,++#Ғq\& 7 n"vqqq.]z7o:th@@93 ^A\\ܮ]rrrZ/^xOD`` w )H4lӷlrڵjff`jj+Ilnn322p84
+ meddt$I4556mZhhZ 4Wrss;34D"|>qFw V4+T,+r_xqٲepw PW1))믿 &]SS{MP B|v"HGFacc#333wh4H; b'3_V߿_RR" 7o;v,== $I]]][[ۏ>?5j<^ bױHÔE}}}VV|j>DD"
SRR7vX @s ]^^x@$d'NZvs||%O>ꫯ>| u@fffKK(Hd2K~G)=_߾}O 2=eeeXKS.FDrrrMM
²~ɇ~R.Ç ̸_~N<ƍˎz@ 8sLVVN˗YYY v XZZ26埕u-%ƾ477?{0 k366F!IR?-┛aFdmm-