Your IP : 216.73.216.21


Current Path : /pkg/pilot/1.0/lib/perl5/Mojo/Server/
Upload File :
Current File : //pkg/pilot/1.0/lib/perl5/Mojo/Server/CGI.pm

package Mojo::Server::CGI;
use Mojo::Base 'Mojo::Server';

has 'nph';

sub run {
  my $self = shift;

  $self->app->server($self);
  my $tx  = $self->build_tx;
  my $req = $tx->req->parse(\%ENV);
  $tx->local_port($ENV{SERVER_PORT})->remote_address($ENV{REMOTE_ADDR});

  # Request body (may block if we try to read too much)
  binmode STDIN;
  my $len = $req->headers->content_length;
  until ($req->is_finished) {
    my $chunk = ($len && $len < 131072) ? $len : 131072;
    last unless my $read = STDIN->read(my $buffer, $chunk, 0);
    $req->parse($buffer);
    last if ($len -= $read) <= 0;
  }

  $self->emit(request => $tx);

  # Response start-line
  STDOUT->autoflush(1);
  binmode STDOUT;
  my $res = $tx->res->fix_headers;
  return undef if $self->nph && !_write($res, 'get_start_line_chunk');

  # Response headers
  my $code = $res->code    || 404;
  my $msg  = $res->message || $res->default_message;
  $res->headers->status("$code $msg") unless $self->nph;
  return undef                        unless _write($res, 'get_header_chunk');

  # Response body
  return undef unless $tx->is_empty || _write($res, 'get_body_chunk');

  # Finish transaction
  $tx->closed;

  return $res->code;
}

sub _write {
  my ($res, $method) = @_;

  my $offset = 0;
  while (1) {

    # No chunk yet, try again
    sleep 1 and next unless defined(my $chunk = $res->$method($offset));

    # End of part
    last unless my $len = length $chunk;

    # Make sure we can still write
    $offset += $len;
    return undef unless STDOUT->opened;
    print STDOUT $chunk;
  }

  return 1;
}

1;

=encoding utf8

=head1 NAME

Mojo::Server::CGI - CGI server

=head1 SYNOPSIS

  use Mojo::Server::CGI;

  my $cgi = Mojo::Server::CGI->new;
  $cgi->unsubscribe('request')->on(request => sub ($cgi, $tx) {

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

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

    # Resume transaction
    $tx->resume;
  });
  $cgi->run;

=head1 DESCRIPTION

L<Mojo::Server::CGI> is a simple and portable implementation of L<RFC 3875|https://tools.ietf.org/html/rfc3875>.

See L<Mojolicious::Guides::Cookbook/"DEPLOYMENT"> for more.

=head1 EVENTS

L<Mojo::Server::CGI> inherits all events from L<Mojo::Server>.

=head1 ATTRIBUTES

L<Mojo::Server::CGI> inherits all attributes from L<Mojo::Server> and implements the following new ones.

=head2 nph

  my $bool = $cgi->nph;
  $cgi     = $cgi->nph($bool);

Activate non-parsed header mode.

=head1 METHODS

L<Mojo::Server::CGI> inherits all methods from L<Mojo::Server> and implements the following new ones.

=head2 run

  my $status = $cgi->run;

Run CGI.

=head1 SEE ALSO

L<Mojolicious>, L<Mojolicious::Guides>, L<https://mojolicious.org>.

=cut

https://nalibali.org/post-sitemap.xml 2026-11-20T09:00:00+00:00 https://nalibali.org/page-sitemap.xml 2026-11-18T09:24:11+00:00 https://nalibali.org/stories-sitemap.xml 2026-05-05T06:48:38+00:00 https://nalibali.org/stories-sitemap2.xml 2026-05-05T06:48:38+00:00 https://nalibali.org/audio_stories-sitemap.xml 2026-03-27T06:53:13+00:00 https://nalibali.org/story_cards-sitemap.xml 2025-06-06T10:27:48+00:00 https://nalibali.org/story_seeds-sitemap.xml 2025-05-19T07:35:43+00:00 https://nalibali.org/rhymes-sitemap.xml 2025-06-17T07:43:10+00:00 https://nalibali.org/activity_sheets-sitemap.xml 2025-05-16T09:16:43+00:00 https://nalibali.org/printables-sitemap.xml 2025-05-16T09:28:40+00:00 https://nalibali.org/posters_bookmarks-sitemap.xml 2025-05-16T10:20:22+00:00 https://nalibali.org/team_member-sitemap.xml 2025-05-28T09:40:43+00:00 https://nalibali.org/video_stories-sitemap.xml 2025-06-23T08:48:28+00:00 https://nalibali.org/guides-sitemap.xml 2025-06-17T09:38:41+00:00 https://nalibali.org/story_supplements-sitemap.xml 2025-09-16T12:22:20+00:00 https://nalibali.org/vacancies-sitemap.xml 2026-09-17T07:05:01+00:00 https://nalibali.org/category-sitemap.xml 2026-11-20T09:00:00+00:00 https://nalibali.org/post_tag-sitemap.xml 2026-11-06T00:00:00+00:00 https://nalibali.org/test-sitemap.xml 2025-06-13T09:36:28+00:00 https://nalibali.org/story_categories-sitemap.xml 2026-05-05T06:48:38+00:00 https://nalibali.org/activity_sheets_categories-sitemap.xml 2025-05-16T09:16:43+00:00 https://nalibali.org/printables_categories-sitemap.xml 2025-05-16T09:28:40+00:00 https://nalibali.org/posters_bookmarks_categories-sitemap.xml 2025-05-16T10:20:22+00:00 https://nalibali.org/single_audio-sitemap.xml 2026-03-27T06:53:13+00:00 https://nalibali.org/rhyme_categories-sitemap.xml 2025-06-17T07:43:10+00:00 https://nalibali.org/guide_categories-sitemap.xml 2025-06-17T09:38:41+00:00 https://nalibali.org/author-sitemap.xml 2026-01-22T07:26:07+00:00