Changeset 11792
- Timestamp:
- 05/17/08 23:09:46 (5 years ago)
- Files:
-
- 1 copied
Legend:
- Unmodified
- Added
- Removed
-
lang/perl/HTTP-Engine/trunk/lib/HTTP/Engine/Interface/Standalone.pm
r11484 r11792 1 1 package HTTP::Engine::Interface::Standalone; 2 use strict; 3 use warnings; 4 use base 'HTTP::Engine::Plugin'; 5 use HTTP::Engine::Role; 2 use Moose; 6 3 with 'HTTP::Engine::Role::Interface'; 7 4 … … 13 10 use constant should_write_response_line => 1; 14 11 15 sub read_chunk { 12 has host => ( 13 is => 'rw', 14 isa => 'Str', 15 default => '127.0.0.1', 16 ); 17 18 has port => ( 19 is => 'rw', 20 isa => 'Int', 21 default => 80, 22 ); 23 24 has keepalive => ( 25 is => 'ro', 26 isa => 'Bool', 27 default => 0, 28 ); 29 30 has fork => ( 31 is => 'ro', 32 isa => 'Bool', 33 default => 0, 34 ); 35 36 has allowed => ( 37 is => 'rw', 38 isa => 'HashRef', 39 default => sub { { '127.0.0.1' => '255.255.255.255' } }, 40 ); 41 42 has argv => ( 43 is => 'ro', 44 isa => 'ArrayRef', 45 default => sub { [] }, 46 ); 47 48 49 use HTTP::Engine::ResponseWriter; 50 HTTP::Engine::RequestBuilder->meta->add_method( _read_chunk => sub { 16 51 shift; 17 52 # support for non-blocking IO … … 30 65 } 31 66 } 32 } 33 34 beforeprepare_read => sub {67 }); 68 69 HTTP::Engine::RequestBuilder->meta->add_before_method_modifier( _prepare_read => sub { 35 70 my $self = shift; 36 71 # Set the input handle to non-blocking 37 72 *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 75 use HTTP::Engine::ResponseWriter; 76 my $is_keepalive; 77 HTTP::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' 46 83 ); 47 } ;84 }); 48 85 49 86 sub 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; 54 93 55 94 # Setup address … … 75 114 $url .= ":$port" unless $port == 80; 76 115 77 $c->log( info => "You can connect to your server at $url\n");78 79 116 my $restart = 0; 80 my $allowed = $self-> config->{allowed} || { '127.0.0.1' => '255.255.255.255' };117 my $allowed = $self->allowed; 81 118 my $parent = $$; 82 119 my $pid = undef; … … 93 130 unless (uc $method eq 'RESTART') { 94 131 # 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); 97 134 $daemon->close if defined $pid; 98 135 } else { … … 119 156 $SIG{CHLD} = 'DEFAULT'; 120 157 wait; 121 exec $^X . ' "' . $0 . '" ' . join(' ', @{ $self-> config->{argv}});158 exec $^X . ' "' . $0 . '" ' . join(' ', @{ $self->argv }); 122 159 } 123 160 … … 126 163 127 164 sub _handler { 128 my($self, $ c, $port, $method, $uri, $protocol) = @_;165 my($self, $port, $method, $uri, $protocol) = @_; 129 166 130 167 # Ignore broken pipes as an HTTP server should … … 178 215 } 179 216 # Pass flow control to HTTP::Engine 180 $ c->handle_request;217 $self->handle_request; 181 218 182 219 my $connection = lc $ENV{HTTP_CONNECTION}; 183 220 last 184 unless $self-> _keep_alive()221 unless $self->keepalive 185 222 && index($connection, 'keep-alive') > -1 186 223 && index($connection, 'te') == -1 # opera stuff … … 192 229 sysread(Remote, my $buf, 4096) if $sel->can_read(0); # IE bk 193 230 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;203 231 } 204 232
![(please configure the [header_logo] section in trac.ini)](/share/chrome/site/your_project_logo.png)