Any-Daemon-HTTP

 view release on metacpan or  search on metacpan

lib/Any/Daemon/FCGI/ClientConn.pm  view on Meta::CPAN


# Implementation heavily based on Net::Async::FastCGI::Request and
# Mojo::Server::FastCGI

my %server_role_name2id =
  ( RESPONDER          => 1
  , AUTHORIZER         => 2
  , FILTER             => 3
  );

my %frame_name2id =
  ( BEGIN_REQUEST      => 1
  , ABORT_REQUEST      => 2
  , END_REQUEST        => 3
  , PARAMS             => 4
  , STDIN              => 5
  , STDOUT             => 6
  , STDERR             => 7
  , DATA               => 8
  , GET_VALUES         => 9
  , GET_VALUES_RESULT  => 10

lib/Any/Daemon/FCGI/ClientConn.pm  view on Meta::CPAN

  );

my %end_status2id =
  ( REQUEST_COMPLETE   => 0
  , CANT_MPX_CONN      => 1
  , OVERLOADED         => 2
  , UNKNOWN_ROLE       => 3
  );

my %server_role_id2name = reverse %server_role_name2id;
my %frame_id2name       = reverse %frame_name2id;


sub new($%) { (bless {}, $_[0])->init($_[1]) }

sub init($)
{   my ($self, $args) = @_;
    $self->{ADFC_requests}  = {};
    $self->{ADFC_max_conns} = $args->{max_childs} or panic;
    $self->{ADFC_max_reqs}  = $args->{max_childs};

lib/Any/Daemon/FCGI/ClientConn.pm  view on Meta::CPAN

{   my $self = shift;
    my $leader = $self->_read_chunk(8);
    length $leader==8 or return;

    my ($version, $type_id, $req_id, $clen, $plen) = unpack 'CCnnC', $leader;
    my $body = $self->_read_chunk($clen + $plen);

    substr $body, -$plen, $plen, '' if $plen;   # remove padding bytes
    length $body==$clen or return;

    ($frame_id2name{$type_id} || 'UNKNOWN_TYPE', $req_id, \$body);
}

sub _reply_record($$$)
{   my ($self, $type, $req_id, $body) = @_;
    my $type_id = $frame_name2id{$type} or panic $type;
    my $empty   = ! length $body;  # write one empty frame

    while(length $body || $empty)
    {   my $chunk  = substr $body, 0, MAX_FRAME_SEND, '';
        my $size   = length $chunk;
        my $pad    = (-$size) % 8;    # advise to pad on 8 bytes
        my $frame  = pack "CCnnCxa${size}x${pad}"
          , FCGI_VERSION, $type_id, $req_id, $size, $pad, $chunk;

        while(length $frame)
        {   my $wrote = syswrite $self->socket, $frame;
            if(defined $wrote)
            {   substr $frame, 0, $wrote, '';
                next;
            }

            return unless $! == EAGAIN || $! == EINTR || $! == EWOULDBLOCK;
            usleep 1000;  # 1 ms
        }

        last if $empty;
    }
}

lib/Any/Daemon/HTTP/VirtualHost.pod  view on Meta::CPAN

create the object.  When %options are provided, they are passed to
L<Any::Daemon::HTTP::Proxy::new()|Any::Daemon::HTTP::Proxy/"Constructors"> to create the $object.

=back

=head1 DETAILS

=head2 Handlers

Handlers are called to dynamically generate responses, for instance
to fill-in templates.  In other frameworks, they are called 'routes'
or 'get'.

When a request for an URI is received, it is first checked whether
a static file can fulfil the request.  If not, a search is started
for the handler with the longest path.

  # /upload($|/*) goes to the upload_handler
  $vhost->addHandlers
    ( '/'       => \&default_handler
    , '/upload' => \&upload_handler

lib/Any/Daemon/HTTP/VirtualHost.pod  view on Meta::CPAN

     # whole directory trees
     $uri = URI->new_abs('/somewhere/else'.$1, $uri)
         if $uri->path =~ m!^/some/dir(/.*|$)!;
     
     $uri;
  }

=head2 Using Template Toolkit

Connecting this server to the popular Template Toolkit web-page
framework is quite simple:

  # Use TT only for pages under /status
  $vhost->addHandler('/status' => 'ttStatus');

  sub ttStatus($$$$)
  {   my ($self, $session, $request, $uri, $tree) = @_;;

      # Often, this object is global or an attribute
      my $template = Template->new(...);



( run in 0.756 second using v1.01-cache-2.11-cpan-d7f47b0818f )