File: delayed_example

package info (click to toggle)
libanyevent-httpd-perl 0.93-6
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 244 kB
  • sloc: perl: 1,247; makefile: 4
file content (33 lines) | stat: -rwxr-xr-x 867 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#!/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',
         "<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;