The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

Mojo::IOLoop::Stream::HTTPServer - Non-blocking I/O HTTP server stream

SYNOPSIS

  use Mojo::IOLoop::Server;
  use Mojo::IOLoop::Stream::HTTPServer;
  
  # Create listen socket
  my $server = Mojo::IOLoop::Server->new;
  $server->on(
    accept => sub {
      my $stream = Mojo::IOLoop::Stream::HTTPServer->new(pop);
  
      $stream->on(
        request => sub {
          my ($stream, $tx) = @_;
          $tx->res->code(200);
          $tx->res->headers->content_type('text/plain');
          $tx->res->body('Hello World!');
          $tx->resume;
        }
      );
      $stream->start;
    }
  );
  $server->listen(port => 3000);
  
  # Start reactor if necessary
  $stream->reactor->start unless $stream->reactor->is_running;

DESCRIPTION

Mojo::IOLoop::Stream::HTTPServer is a container for I/O streams used by Mojo::IOLoop to support the HTTP protocol server-side.

EVENTS

Mojo::IOLoop::Stream::HTTPServer inherits all events from Mojo::IOLoop::Stream and can emit the following new ones.

request

  $stream->on(request => sub {
    my ($sream, $tx) = @_;
    ...
  });

Emitted when a request is ready and needs to be handled.

  $stream->on(request => sub {
    my ($stream, $tx) = @_;
    $tx->res->code(200);
    $tx->res->headers->content_type('text/plain');
    $tx->res->body('Hello World!');
    $tx->resume;
  });

start

  $stream->on(start => sub {
    my ($stream, $tx) = @_;
    ...
  });

Emitted whenever a transaction for a new request is about to start.

upgrade

  $stream->on(upgrade => sub {
    my ($stream, $ws) = @_;
    ...
  });

Emitted when the connection should be upgraded to the WebSocket protocol.

ATTRIBUTES

Mojo::IOLoop::Stream::HTTPServer inherits all attributes from Mojo::IOLoop::Stream and implements the following ones.

app

  my $app = $stream->app;
  $stream = $stream->app(Mojolicious->new);

Application responsible for building transactions, defaults to a Mojo::HelloWorld object.

max_requests

  my $max = $stream->max_requests;
  $stream = $stream->max_requests(250);

Maximum number of keep-alive requests per connection, defaults to 100.

METHODS

Mojo::IOLoop::Stream::HTTPServer inherits all methods from Mojo::IOLoop::Stream and implements the following new ones.

new

  my $stream = Mojo::IOLoop::Stream::HTTPServer->new($handle);

Construct a new Mojo::IOLoop::Stream::HTTPServer object.

DEBUGGING

You can set the MOJO_SERVER_DEBUG environment variable to get some advanced diagnostics information printed to STDERR.

  MOJO_SERVER_DEBUG=1

SEE ALSO

Mojolicious, Mojolicious::Guides, http://mojolicious.org.