Changeset 10867 for lang/perl/HTTP-Engine
- Timestamp:
- 05/01/08 13:03:37 (5 years ago)
- Location:
- lang/perl/HTTP-Engine/branches/moose
- Files:
-
- 9 modified
-
Makefile.PL (modified) (2 diffs)
-
examples/moose-dumper.pl (modified) (2 diffs)
-
lib/HTTP/Engine.pm (modified) (1 diff)
-
lib/HTTP/Engine/Context.pm (modified) (1 diff)
-
lib/HTTP/Engine/Interface/CGI.pm (modified) (5 diffs)
-
lib/HTTP/Engine/Interface/ServerSimple.pm (modified) (3 diffs)
-
lib/HTTP/Engine/Plugin/DebugScreen.pm (modified) (1 diff)
-
lib/HTTP/Engine/Request.pm (modified) (2 diffs)
-
lib/HTTP/Engine/Role/Interface.pm (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
-
lang/perl/HTTP-Engine/branches/moose/Makefile.PL
r10866 r10867 3 3 all_from 'lib/HTTP/Engine.pm'; 4 4 5 { 6 no warnings 'redefine'; 7 if ($Module::Install::VERSION > 0.70) { 8 *recommends_hack = sub { $_[0] } 9 } else { 10 *recommends_hack = \&recommends; 11 } 12 } 13 14 15 requires 'Class::Accessor::Fast'; 16 requires 'Class::Component'; 5 requires 'Moose' => 0.40; 6 requires 'MooseX::Object::Pluggable'; 17 7 requires 'Scalar::Util'; 18 8 … … 25 15 requires 'CGI::Simple::Cookie'; 26 16 requires 'HTTP::Body'; 17 requires 'HTTP::Headers'; 27 18 requires 'URI'; 28 19 -
lang/perl/HTTP-Engine/branches/moose/examples/moose-dumper.pl
r10841 r10867 5 5 use HTTP::Engine; 6 6 use HTTP::Engine::Interface::ServerSimple; 7 use Moose::Util 'apply_all_roles';8 7 use HTTP::Engine::Plugin::DebugScreen; 9 8 10 11 apply_all_roles('HTTP::Engine', 'HTTP::Engine::Plugin::DebugScreen'); 12 13 HTTP::Engine->new( 14 interface => HTTP::Engine::Interface::ServerSimple->new(port => 9999), 9 my $engine = HTTP::Engine::Interface::ServerSimple->new( 10 port => 9999, 15 11 handler => sub { 16 my $c = shift;17 my $req_dump = Dumper( $c->req);18 my $raw = $c->req->raw_body;19 my $body = <<"...";12 my $c = shift; 13 my $req_dump = Dumper( $c->req ); 14 my $raw = $c->req->raw_body; 15 my $body = <<"..."; 20 16 <form method="post"> 21 17 <input type="text" name="foo" /> … … 28 24 $c->res->body($body); 29 25 }, 30 )->run; 26 ); 27 $engine->load_plugins(qw/DebugScreen/); 28 $engine->run; 29 -
lang/perl/HTTP-Engine/branches/moose/lib/HTTP/Engine.pm
r10864 r10867 4 4 use base 'HTTPEx'; 5 5 our $VERSION = '0.0.3'; 6 7 use HTTP::Engine::Context;8 use HTTP::Engine::Request;9 use HTTP::Engine::Request::Upload;10 use HTTP::Engine::Response;11 12 has handler => (13 is => 'rw',14 isa => 'CodeRef',15 required => 1,16 );17 18 has interface => (19 is => 'rw',20 does => 'HTTP::Engine::Role::Interface',21 required => 1,22 handles => [ qw(prepare finalize) ],23 );24 25 has context_class => (26 is => 'rw',27 isa => 'Str',28 default => 'HTTP::Engine::Context',29 );30 31 has request_class => (32 is => 'rw',33 isa => 'Str',34 default => 'HTTP::Engine::Request',35 );36 37 has response_class => (38 is => 'rw',39 isa => 'Str',40 default => 'HTTP::Engine::Response',41 );42 43 sub handle_request {44 my $self = shift;45 46 $self->interface->initialize();47 48 my %env = @_;49 %env = %ENV unless %env;50 51 my $context = $self->context_class->new(52 engine => $self,53 req => $self->request_class->new(),54 res => $self->response_class->new(),55 env => \%env,56 );57 58 $self->prepare( $context );59 60 my $ret = eval {61 $self->call_handler($context);62 };63 if (my $e = $@) {64 $self->handle_error( $context, $e);65 }66 $self->finalize( $context );67 68 $ret;69 }70 71 sub run {72 my $self = shift;73 $self->interface->run($self);74 }75 76 sub finalize_headers {77 my($self, $c) = @_;78 return if $c->res->{_finalized_headers};79 80 # Handle redirects81 if (my $location = $c->res->redirect ) {82 $self->log( debug => qq/Redirecting to "$location"/ );83 $c->res->header( Location => $self->absolute_url($c, $location) );84 $c->res->body($c->res->status . ': Redirect') unless $c->res->body;85 }86 87 # Content-Length88 $c->res->content_length(0);89 if ($c->res->body && !$c->res->content_length) {90 # get the length from a filehandle91 if (Scalar::Util::blessed($c->res->body) && $c->res->body->can('read')) {92 if (my $stat = stat $c->res->body) {93 $c->res->content_length($stat->size);94 } else {95 $self->log( warn => 'Serving filehandle without a content-length' );96 }97 } else {98 $c->res->content_length(bytes::length($c->res->body));99 }100 }101 102 $c->res->content_type('text/html') unless $c->res->content_type;103 104 # Errors105 if ($c->res->status =~ /^(1\d\d|[23]04)$/) {106 $c->res->headers->remove_header("Content-Length");107 $c->res->body('');108 }109 110 $self->finalize_cookies($c);111 $self->finalize_output_headers($c);112 113 # Done114 $c->res->{_finalized_headers} = 1;115 }116 117 # hook me!118 sub call_handler {119 my ($self, $context) = @_;120 $self->handler->($context);121 }122 6 123 7 1; -
lang/perl/HTTP-Engine/branches/moose/lib/HTTP/Engine/Context.pm
r10825 r10867 12 12 has engine => ( 13 13 is => 'rw', 14 isa => 'HTTP::Engine',14 does => 'HTTP::Engine::Role::Interface', 15 15 required => 1, 16 16 weakref => 1, -
lang/perl/HTTP-Engine/branches/moose/lib/HTTP/Engine/Interface/CGI.pm
r10808 r10867 2 2 use Moose; 3 3 with 'HTTP::Engine::Role::Interface'; 4 use Scalar::Util qw/blessed/; 4 5 5 6 has read_position => ( … … 38 39 39 40 sub run { 40 my ($self, $engine) = @_; 41 $engine->handle_request(); 42 } 43 44 sub prepare { 45 my ($self, $context) = @_; 46 47 for my $method (qw/ request connection query_parameters headers cookie path body body_parameters parameters uploads /) { 48 my $method = "prepare_$method"; 49 $self->$method($context); 50 } 41 my ($self, ) = @_; 42 $self->handle_request(); 51 43 } 52 44 … … 280 272 # private methods 281 273 282 sub read_chunk { shift; *STDIN->sysread(@_) } 274 sub read_chunk { 275 my $self = shift; 276 if (blessed(*STDIN)) { 277 *STDIN->sysread(@_); 278 } else { 279 STDIN->sysread(@_); 280 } 281 } 283 282 #Apache sub read_chunk { shift->apache->read(@_) } 284 283 … … 297 296 298 297 my $remaining = $self->read_length - $self->read_position; 299 $maxlength ||= $self->chunk size;298 $maxlength ||= $self->chunk_size; 300 299 301 300 # Are we done reading? … … 321 320 322 321 # Set the output handle to autoflush 323 *STDOUT->autoflush(1); 322 if (blessed *STDOUT) { 323 *STDOUT->autoflush(1); 324 } 324 325 } 325 326 -
lang/perl/HTTP-Engine/branches/moose/lib/HTTP/Engine/Interface/ServerSimple.pm
r10808 r10867 13 13 14 14 sub run { 15 my ($self, $engine) = @_;15 my ($self, ) = @_; 16 16 17 17 my $simple_meta = Class::MOP::Class->create_anon_class( … … 19 19 methods => { 20 20 handler => sub { 21 $ engine->handle_request;21 $self->handle_request; 22 22 } 23 23 }, … … 27 27 } 28 28 29 sub finalize_output_headers { 30 my ( $self, $engine ) = @_; 31 32 $self->write_response_line($engine); 33 $self->SUPER::finalize_output_headers($engine); 34 } 35 36 sub prepare_write { 37 # nop. do not *STDOUT->autoflush(1); 38 } 29 before 'finalize_output_headers' => sub { 30 my ($self, $c) = @_; 31 $self->write_response_line($c); 32 }; 39 33 40 34 1; -
lang/perl/HTTP-Engine/branches/moose/lib/HTTP/Engine/Plugin/DebugScreen.pm
r10827 r10867 1 1 package HTTP::Engine::Plugin::DebugScreen; 2 2 use Moose::Role; 3 use Carp::Always; 3 use Carp; 4 5 around call_handler => sub { 6 my ($next, @args) = @_; 7 local $SIG{__DIE__} = \&Carp::confess; 8 $next->(@args); 9 }; 4 10 5 11 around handle_error => sub { -
lang/perl/HTTP-Engine/branches/moose/lib/HTTP/Engine/Request.pm
r10859 r10867 1 1 package HTTP::Engine::Request; 2 2 use Moose; 3 with 'MooseX::Object::Pluggable'; 3 4 use Carp; 4 5 use IO::Socket qw[AF_INET inet_aton]; … … 146 147 sub parameters { 147 148 my ($self, $params) = @_; 149 $self->{parameters} ||= {}; 150 148 151 if ($params) { 149 152 if (ref $params) { 150 153 $self->{parameters} = $params; 151 154 } else { 152 $self->context->log->warn(155 warn( 153 156 "Attempt to retrieve '$params' with req->params(), " . 154 157 "you probably meant to call req->param('$params')" ); -
lang/perl/HTTP-Engine/branches/moose/lib/HTTP/Engine/Role/Interface.pm
r10860 r10867 2 2 use strict; 3 3 use Moose::Role; 4 with 'MooseX::Object::Pluggable'; 4 5 5 # PUBLIC INTERFACES: 6 # ->run($engine) 7 # ->prepare($context) 8 # ->finalize($context) 6 requires 'run', 'finalize_cookies', 'finalize_output_body', 'finalize_output_headers', 'prepare_body'; 7 requires map { "prepare_$_" } qw/request connection query_parameters headers cookie path body body_parameters parameters uploads/; 9 8 10 requires 'run', 'prepare', 'finalize_cookies', 'finalize_output_body', 'finalize_output_headers'; 9 around 'new' => sub { 10 my ($next, @args) = @_; 11 my $self = $next->(@args); 12 $self->_plugin_app_ns(['HTTP::Engine']); 13 $self; 14 }; 15 16 use HTTP::Engine::Context; 17 use HTTP::Engine::Request; 18 use HTTP::Engine::Request::Upload; 19 use HTTP::Engine::Response; 20 21 has handler => ( 22 is => 'rw', 23 isa => 'CodeRef', 24 required => 1, 25 ); 26 27 has context_class => ( 28 is => 'rw', 29 isa => 'Str', 30 default => 'HTTP::Engine::Context', 31 ); 32 33 has request_class => ( 34 is => 'rw', 35 isa => 'Str', 36 default => 'HTTP::Engine::Request', 37 ); 38 39 has response_class => ( 40 is => 'rw', 41 isa => 'Str', 42 default => 'HTTP::Engine::Response', 43 ); 44 45 sub handle_request { 46 my $self = shift; 47 48 $self->initialize(); 49 50 my %env = @_; 51 %env = %ENV unless %env; 52 53 my $context = $self->context_class->new( 54 engine => $self, 55 req => $self->request_class->new(), 56 res => $self->response_class->new(), 57 env => \%env, 58 ); 59 60 $self->prepare( $context ); 61 62 my $ret = eval { 63 $self->call_handler($context); 64 }; 65 if (my $e = $@) { 66 $self->handle_error( $context, $e); 67 } 68 $self->finalize( $context ); 69 70 $ret; 71 } 72 73 # hook me! 74 sub handle_error { 75 my ($self, $context, $error) = @_; 76 print STDERR $error; 77 } 78 79 # hook me! 80 sub call_handler { 81 my ($self, $context) = @_; 82 $self->handler->($context); 83 } 84 85 sub prepare { 86 my ($self, $context) = @_; 87 88 for my $method (qw/ request connection query_parameters headers cookie path body body_parameters parameters uploads /) { 89 my $method = "prepare_$method"; 90 $self->$method($context); 91 } 92 } 11 93 12 94 sub finalize {
![(please configure the [header_logo] section in trac.ini)](/share/chrome/site/your_project_logo.png)