<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">package Mojo::Server::PSGI;
use Mojo::Base 'Mojo::Server';

sub run {
  my ($self, $env) = @_;

  my $tx  = $self-&gt;build_tx;
  my $req = $tx-&gt;req-&gt;parse($env);
  $tx-&gt;local_port($env-&gt;{SERVER_PORT})-&gt;remote_address($env-&gt;{REMOTE_ADDR});

  # Request body (may block if we try to read too much)
  my $len = $env-&gt;{CONTENT_LENGTH};
  until ($req-&gt;is_finished) {
    my $chunk = ($len &amp;&amp; $len &lt; 131072) ? $len : 131072;
    last unless my $read = $env-&gt;{'psgi.input'}-&gt;read(my $buffer, $chunk, 0);
    $req-&gt;parse($buffer);
    last if ($len -= $read) &lt;= 0;
  }

  # Handle request
  $self-&gt;emit(request =&gt; $tx);

  # Response headers
  my $res     = $tx-&gt;res-&gt;fix_headers;
  my $headers = $res-&gt;content-&gt;headers;
  my @headers;
  for my $name (@{$headers-&gt;names}) {
    push @headers, $name =&gt; $_ for map {@$_} $headers-&gt;header($name);
  }

  # PSGI response
  my $io = Mojo::Server::PSGI::_IO-&gt;new(tx =&gt; $tx, empty =&gt; $tx-&gt;is_empty);
  return [$res-&gt;code || 404, \@headers, $io];
}

sub to_psgi_app {
  my $self = shift;

  # Preload application and wrap it
  $self-&gt;app;
  return sub { $self-&gt;run(@_) }
}

package Mojo::Server::PSGI::_IO;
use Mojo::Base -base;

# Finish transaction
sub close { shift-&gt;{tx}-&gt;server_close }

sub getline {
  my $self = shift;

  # Empty
  return undef if $self-&gt;{empty};

  # No content yet, try again later
  my $chunk = $self-&gt;{tx}-&gt;res-&gt;get_body_chunk($self-&gt;{offset} = defined $self-&gt;{offset} ? $self-&gt;{offset} : 0);
  return '' unless defined $chunk;

  # End of content
  return undef unless length $chunk;

  $self-&gt;{offset} += length $chunk;
  return $chunk;
}

1;

=encoding utf8

=head1 NAME

Mojo::Server::PSGI - PSGI server

=head1 SYNOPSIS

  use Mojo::Server::PSGI;

  my $psgi = Mojo::Server::PSGI-&gt;new;
  $psgi-&gt;unsubscribe('request');
  $psgi-&gt;on(request =&gt; sub {
    my ($psgi, $tx) = @_;

    # Request
    my $method = $tx-&gt;req-&gt;method;
    my $path   = $tx-&gt;req-&gt;url-&gt;path;

    # Response
    $tx-&gt;res-&gt;code(200);
    $tx-&gt;res-&gt;headers-&gt;content_type('text/plain');
    $tx-&gt;res-&gt;body("$method request for $path!");

    # Resume transaction
    $tx-&gt;resume;
  });
  my $app = $psgi-&gt;to_psgi_app;

=head1 DESCRIPTION

L&lt;Mojo::Server::PSGI&gt; allows L&lt;Mojo&gt; applications to run on all PSGI
compatible servers.

See L&lt;Mojolicious::Guides::Cookbook&gt; for more.

=head1 EVENTS

L&lt;Mojo::Server::PSGI&gt; inherits all events from L&lt;Mojo::Server&gt;.

=head1 METHODS

L&lt;Mojo::Server::PSGI&gt; inherits all methods from L&lt;Mojo::Server&gt; and implements
the following new ones.

=head2 run

  my $res = $psgi-&gt;run($env);

Run L&lt;PSGI&gt;.

=head2 to_psgi_app

  my $app = $psgi-&gt;to_psgi_app;

Turn L&lt;Mojo&gt; application into L&lt;PSGI&gt; application.

=head1 SEE ALSO

L&lt;Mojolicious&gt;, L&lt;Mojolicious::Guides&gt;, L&lt;http://mojolicio.us&gt;.

=cut
</pre></body></html>