AnyEvent-HTTPD

 view release on metacpan or  search on metacpan

lib/AnyEvent/HTTPD.pm  view on Meta::CPAN

package AnyEvent::HTTPD;
use common::sense;
use Scalar::Util qw/weaken/;
use URI;
use AnyEvent::HTTPD::Request;
use AnyEvent::HTTPD::Util;

use base qw/AnyEvent::HTTPD::HTTPServer/;

=head1 NAME

AnyEvent::HTTPD - A simple lightweight event based web (application) server

lib/AnyEvent/HTTPD.pm  view on Meta::CPAN

   my $class = ref($this) || $this;
   my $self  = $class->SUPER::new (
      request_class => "AnyEvent::HTTPD::Request",
      @_
   );

   $self->reg_cb (
      connect => sub {
         my ($self, $con) = @_;

         weaken $self;

         $self->{conns}->{$con} = $con->reg_cb (
            request => sub {
               my ($con, $meth, $url, $hdr, $cont) = @_;
               #d# warn "REQUEST: $meth, $url, [$cont] " . join (',', %$hdr) . "\n";

               $url = URI->new ($url);

               if ($meth eq 'GET') {
                  $cont = parse_urlencoded ($url->query);
               }

               if ( scalar grep { $meth eq $_ } @{ $self->{allowed_methods} } ) {

                  weaken $con;

                  $self->handle_app_req (
                     $meth, $url, $hdr, $cont, $con->{host}, $con->{port},
                     sub {
                        $con->response (@_) if $con;
                     });
               } else {
                  $con->response (200, "ok");
               }
            }

lib/AnyEvent/HTTPD/HTTPConnection.pm  view on Meta::CPAN

package AnyEvent::HTTPD::HTTPConnection;
use common::sense;
use IO::Handle;
use AnyEvent::Handle;
use Object::Event;
use Time::Local;

use AnyEvent::HTTPD::Util;

use Scalar::Util qw/weaken/;
our @ISA = qw/Object::Event/;

=head1 NAME

AnyEvent::HTTPD::HTTPConnection - A simple HTTP connection for request and response handling

=head1 DESCRIPTION

This class is a helper class for L<AnyEvent:HTTPD::HTTPServer> and L<AnyEvent::HTTPD>,
it handles TCP reading and writing as well as parsing and serializing

lib/AnyEvent/HTTPD/HTTPConnection.pm  view on Meta::CPAN


   $res .= "\015\012";

   if ($no_body) { # for HEAD requests!
      $self->{hdl}->push_write ($res);
      $self->response_done;
      return;
   }

   if (ref ($content) eq 'CODE') {
      weaken $self;

      my $chunk_cb = sub {
         my ($chunk) = @_;

         return 0 unless defined ($self) && defined ($self->{hdl}) && !$self->{disconnected};

         delete $self->{transport_polled};

         if (defined ($chunk) && length ($chunk) > 0) {
            $self->{hdl}->push_write ($chunk);

lib/AnyEvent/HTTPD/HTTPConnection.pm  view on Meta::CPAN

         }
      }
   );
}

sub push_header_line {
   my ($self) = @_;

   return if $self->{disconnected};

   weaken $self;

   $self->{req_timeout} =
      AnyEvent->timer (after => $self->{request_timeout}, cb => sub {
         return unless defined $self;

         $self->do_disconnect ("request timeout ($self->{request_timeout})");
      });

   $self->{hdl}->push_read (line => sub {
      my ($hdl, $line) = @_;

lib/AnyEvent/HTTPD/HTTPServer.pm  view on Meta::CPAN

package AnyEvent::HTTPD::HTTPServer;
use common::sense;
use Scalar::Util qw/weaken/;
use Object::Event;
use AnyEvent::Handle;
use AnyEvent::Socket;

use AnyEvent::HTTPD::HTTPConnection;

our @ISA = qw/Object::Event/;

=head1 NAME

lib/AnyEvent/HTTPD/HTTPServer.pm  view on Meta::CPAN

   my $class = ref($this) || $this;
   my $self  = {
      connection_class => "AnyEvent::HTTPD::HTTPConnection",
      allowed_methods  => [ qw/GET HEAD POST/ ],
      @_,
   };
   bless $self, $class;

   my $rself = $self;

   weaken $self;

   $self->{srv} =
      tcp_server $self->{host}, $self->{port}, sub {
         my ($fh, $host, $port) = @_;

         unless ($fh) {
            $self->event (error => "couldn't accept client: $!");
            return;
         }

lib/AnyEvent/HTTPD/HTTPServer.pm  view on Meta::CPAN

      $self->{connection_class}->new (
         fh => $fh,
         request_timeout => $self->{request_timeout},
         allowed_methods => $self->{allowed_methods},
         ssl => $self->{ssl},
         host => $h,
         port => $p);

   $self->{handles}->{$htc} = $htc;

   weaken $self;

   $htc->reg_cb (disconnect => sub {
      if (defined $self) {
         delete $self->{handles}->{$_[0]};
         $self->event (disconnect => $_[0], $_[1]);
      }
   });

   $self->event (connect => $htc);
}



( run in 1.891 second using v1.01-cache-2.11-cpan-65fba6d93b7 )