File: microhttpd.pl

package info (click to toggle)
libmojolicious-perl 7.21%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 3,432 kB
  • ctags: 1,253
  • sloc: perl: 11,603; makefile: 10
file content (38 lines) | stat: -rw-r--r-- 1,017 bytes parent folder | download | duplicates (2)
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
34
35
36
37
38
use Mojo::Base -strict;
use Mojo::IOLoop;

# Minimal event loop example demonstrating how to cheat at HTTP benchmarks :)
my %buffer;
Mojo::IOLoop->server(
  {port => 8080} => sub {
    my ($loop, $stream, $id) = @_;
    $stream->on(
      read => sub {
        my ($stream, $chunk) = @_;

        # Check if we got start-line and headers (no body support)
        $buffer{$id} .= $chunk;
        if (index($buffer{$id}, "\x0d\x0a\x0d\x0a") >= 0) {
          delete $buffer{$id};

          # Write a minimal HTTP response
          # (the "Hello World!" message has been optimized away!)
          $stream->write(
            "HTTP/1.1 200 OK\x0d\x0aContent-Length: 0\x0d\x0a\x0d\x0a");
        }
      }
    );
  }
);

print <<'EOF';
Starting server on port 8080.
For testing use something like "wrk -c 100 -d 10s http://127.0.0.1:8080/".
EOF

# Stop gracefully to make profiling easier
Mojo::IOLoop->recurring(1 => sub { });
local $SIG{INT} = local $SIG{TERM} = sub { Mojo::IOLoop->stop };
Mojo::IOLoop->start;

1;