|
Revision 11543, 1.8 kB
(checked in by tokuhirom, 5 years ago)
|
|
$c->env is not a useful, we should use %ENV directly.
|
| Line | |
|---|
| 1 | package HTTP::Engine::RequestProcessor; |
|---|
| 2 | use Moose; |
|---|
| 3 | use CGI::Simple::Cookie; |
|---|
| 4 | use HTTP::Body; |
|---|
| 5 | use HTTP::Headers; |
|---|
| 6 | use HTTP::Status (); |
|---|
| 7 | use Scalar::Util qw/blessed/; |
|---|
| 8 | use URI; |
|---|
| 9 | use URI::QueryParam; |
|---|
| 10 | use HTTP::Engine::RequestBuilder; |
|---|
| 11 | use HTTP::Engine::ResponseWriter; |
|---|
| 12 | |
|---|
| 13 | # modify plugin namespace to HTTP::Engine::Plugin::* |
|---|
| 14 | around 'new' => sub { |
|---|
| 15 | my ($next, @args) = @_; |
|---|
| 16 | my $self = $next->(@args); |
|---|
| 17 | $self->_plugin_app_ns(['HTTP::Engine']); |
|---|
| 18 | $self; |
|---|
| 19 | }; |
|---|
| 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 | has request_builder => ( |
|---|
| 46 | is => 'ro', |
|---|
| 47 | isa => 'HTTP::Engine::RequestBuilder', |
|---|
| 48 | lazy => 1, |
|---|
| 49 | default => sub { |
|---|
| 50 | HTTP::Engine::RequestBuilder->new(); |
|---|
| 51 | }, |
|---|
| 52 | ); |
|---|
| 53 | |
|---|
| 54 | has response_writer => ( |
|---|
| 55 | is => 'ro', |
|---|
| 56 | isa => 'HTTP::Engine::ResponseWriter', |
|---|
| 57 | required => 1, |
|---|
| 58 | ); |
|---|
| 59 | |
|---|
| 60 | has chunk_size => ( |
|---|
| 61 | is => 'ro', |
|---|
| 62 | isa => 'Int', |
|---|
| 63 | default => 4096, |
|---|
| 64 | ); |
|---|
| 65 | |
|---|
| 66 | no Moose; |
|---|
| 67 | |
|---|
| 68 | sub handle_request { |
|---|
| 69 | my $self = shift; |
|---|
| 70 | |
|---|
| 71 | my $context = $self->context_class->new( |
|---|
| 72 | req => $self->request_class->new(), |
|---|
| 73 | res => $self->response_class->new(), |
|---|
| 74 | ); |
|---|
| 75 | |
|---|
| 76 | $self->request_builder->prepare( $context ); |
|---|
| 77 | |
|---|
| 78 | my $ret = eval { |
|---|
| 79 | local *STDOUT; |
|---|
| 80 | local *STDIN; |
|---|
| 81 | $self->call_handler($context); |
|---|
| 82 | }; |
|---|
| 83 | if (my $e = $@) { |
|---|
| 84 | print STDERR $e; |
|---|
| 85 | } |
|---|
| 86 | $self->response_writer->finalize( $context ); |
|---|
| 87 | |
|---|
| 88 | $ret; |
|---|
| 89 | } |
|---|
| 90 | |
|---|
| 91 | # hooked by middlewares. |
|---|
| 92 | sub call_handler { |
|---|
| 93 | my ($self, $context) = @_; |
|---|
| 94 | $self->handler->($context); |
|---|
| 95 | } |
|---|
| 96 | |
|---|
| 97 | 1; |
|---|