Changeset 11792

Show
Ignore:
Timestamp:
05/17/08 23:09:46 (5 years ago)
Author:
yappo
Message:

add Interface::Standalone

Files:
1 copied

Legend:

Unmodified
Added
Removed
  • lang/perl/HTTP-Engine/trunk/lib/HTTP/Engine/Interface/Standalone.pm

    r11484 r11792  
    11package HTTP::Engine::Interface::Standalone; 
    2 use strict; 
    3 use warnings; 
    4 use base 'HTTP::Engine::Plugin'; 
    5 use HTTP::Engine::Role; 
     2use Moose; 
    63with 'HTTP::Engine::Role::Interface'; 
    74 
     
    1310use constant should_write_response_line => 1; 
    1411 
    15 sub read_chunk { 
     12has host => ( 
     13    is      => 'rw', 
     14    isa     => 'Str', 
     15    default => '127.0.0.1', 
     16); 
     17 
     18has port => ( 
     19    is      => 'rw', 
     20    isa     => 'Int', 
     21    default => 80, 
     22); 
     23 
     24has keepalive => ( 
     25    is      => 'ro', 
     26    isa     => 'Bool', 
     27    default => 0, 
     28); 
     29 
     30has fork => ( 
     31    is      => 'ro', 
     32    isa     => 'Bool', 
     33    default => 0, 
     34); 
     35 
     36has allowed => ( 
     37    is      => 'rw', 
     38    isa     => 'HashRef', 
     39    default => sub { { '127.0.0.1' => '255.255.255.255' } }, 
     40); 
     41 
     42has argv => ( 
     43    is      => 'ro', 
     44    isa     => 'ArrayRef', 
     45    default => sub { [] }, 
     46); 
     47 
     48 
     49use HTTP::Engine::ResponseWriter; 
     50HTTP::Engine::RequestBuilder->meta->add_method( _read_chunk  => sub { 
    1651    shift; 
    1752    # support for non-blocking IO 
     
    3065        } 
    3166    } 
    32 } 
    33  
    34 before prepare_read => sub { 
     67}); 
     68 
     69HTTP::Engine::RequestBuilder->meta->add_before_method_modifier( _prepare_read => sub { 
    3570    my $self = shift; 
    3671    # Set the input handle to non-blocking 
    3772    *STDIN->blocking(0); 
    38 }; 
    39  
    40 before write_headers => sub { 
    41     my($self, $res) = @_; 
    42  
    43     $res->headers->date(time); 
    44     $res->headers->header( 
    45         Connection => $self->_keep_alive ? 'keep-alive' : 'close' 
     73}); 
     74 
     75use HTTP::Engine::ResponseWriter; 
     76my $is_keepalive; 
     77HTTP::Engine::ResponseWriter->meta->add_before_method_modifier( finalize => sub { 
     78    my($self, $c) = @_; 
     79 
     80    $c->res->headers->date(time); 
     81    $c->res->headers->header( 
     82        Connection => $is_keepalive ? 'keep-alive' : 'close' 
    4683    ); 
    47 }; 
     84}); 
    4885 
    4986sub run { 
    50     my($self, $c) = @_; 
    51     my $host = $self->config->{host} || ''; 
    52     my $port = $self->config->{port} || 80; 
    53     $self->_keep_alive($self->config->{keepalive}); 
     87    my($self, ) = @_; 
     88 
     89    $is_keepalive = sub { $self->keepalive }; 
     90 
     91    my $host = $self->host; 
     92    my $port = $self->port; 
    5493 
    5594    # Setup address 
     
    75114    $url .= ":$port" unless $port == 80; 
    76115 
    77     $c->log( info => "You can connect to your server at $url\n"); 
    78  
    79116    my $restart = 0; 
    80     my $allowed = $self->config->{allowed} || { '127.0.0.1' => '255.255.255.255' }; 
     117    my $allowed = $self->allowed; 
    81118    my $parent = $$; 
    82119    my $pid    = undef; 
     
    93130        unless (uc $method eq 'RESTART') { 
    94131            # Fork 
    95             next if $self->config->{fork} && ($pid = fork); 
    96             $self->_handler($c, $port, $method, $uri, $protocol); 
     132            next if $self->fork && ($pid = fork); 
     133            $self->_handler($port, $method, $uri, $protocol); 
    97134            $daemon->close if defined $pid; 
    98135        } else { 
     
    119156        $SIG{CHLD} = 'DEFAULT'; 
    120157        wait; 
    121         exec $^X . ' "' . $0 . '" ' . join(' ', @{ $self->config->{argv} }); 
     158        exec $^X . ' "' . $0 . '" ' . join(' ', @{ $self->argv }); 
    122159    } 
    123160 
     
    126163 
    127164sub _handler { 
    128     my($self, $c, $port, $method, $uri, $protocol) = @_; 
     165    my($self, $port, $method, $uri, $protocol) = @_; 
    129166 
    130167    # Ignore broken pipes as an HTTP server should 
     
    178215        } 
    179216        # Pass flow control to HTTP::Engine 
    180         $c->handle_request; 
     217        $self->handle_request; 
    181218 
    182219        my $connection = lc $ENV{HTTP_CONNECTION}; 
    183220        last 
    184           unless $self->_keep_alive() 
     221          unless $self->keepalive 
    185222          && index($connection, 'keep-alive') > -1 
    186223          && index($connection, 'te') == -1          # opera stuff 
     
    192229    sysread(Remote, my $buf, 4096) if $sel->can_read(0); # IE bk 
    193230    close Remote; 
    194 } 
    195  
    196 sub _keep_alive { 
    197     my($self, $keepalive) = @_; 
    198  
    199     my $r = $self->{_keepalive} || 0; 
    200     $self->{_keepalive} = $keepalive if defined $keepalive; 
    201  
    202     $r; 
    203231} 
    204232