Engine/Interface/POE.pm untested

filter: Statementstmt 0.0% 0 / 53 Branchbran 0.0% 0 / 8 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 7 Podpod 0.0% 0 / 3 totaltotal 0.0% 0 / 71 CC 5 SCAR 18.2
CC
5
Cov
0%
_make_request
12.0
run
6.0
_client_input
6.0
CRAP 6.2
1 package HTTP::Engine::Interface::POE;
2 our $CLIENT; ## no critic
3 use Any::Moose;
4 use HTTP::Engine::Interface
5 builder => 'NoEnv',
6 writer => {
7 response_line => 1,
8 'write' => sub {
9 my ($self, $buffer) = @_;
10 $CLIENT->put($buffer);
11 return 1;
12 }
13 }
14 ;
15  
16 use POE qw/
17 Component::Server::TCP
18 Filter::HTTPD
19 /;
20 use URI::WithBase;
21  
22 has host => (
23 is => 'ro',
24 isa => 'Str',
25 default => '127.0.0.1',
26 trigger => sub {
27 my $self = shift;
28 utf8::downgrade($self->{host});
29 },
30 );
31  
32 has port => (
33 is => 'ro',
34 isa => 'Int',
35 default => 1978,
36 trigger => sub {
37 my $self = shift;
38 utf8::downgrade($self->{port});
39 },
40 );
41  
42 has alias => (
43 is => 'ro',
44 isa => 'Str | Undef',
45 );
46  
47 my $filter = any_moose('::Meta::Class')->create(
48 'HTTP::Engine::Interface::POE::Filter',
49 superclasses => ['POE::Filter::HTTPD'],
50 methods => {
51 put => sub { # omit output filter
52 shift; # class name
53 return @_;
54 }
55 },
56 )->name;
57  
58 sub run {
59 my ($self) = @_;
60  
61 # setup poe session
62 POE::Component::Server::TCP->new(
63 Port => $self->port,
64 Address => $self->host,
65 ClientFilter => $filter->new,
66 ( $self->alias ? ( Alias => $self->alias ) : () ),
67 ClientInput => _client_input($self),
68 );
69 }
70  
71 sub _client_input {
72 my $self = shift;
73  
74 sub {
75 my ( $kernel, $heap, $request ) = @_[ KERNEL, HEAP, ARG0 ];
76  
77 # Filter::HTTPD sometimes generates HTTP::Response objects.
78 # They indicate (and contain the response for) errors that occur
79 # while parsing the client's HTTP request. It's easiest to send
80 # the responses as they are and finish up.
81 if ( $request->isa('HTTP::Response') ) {
82 $heap->{client}->put($request->as_string);
83 } else {
84 local $CLIENT = $heap->{client};
85 $self->handle_request(%{ $self->_make_request($request, $heap) });
86 }
87 $kernel->yield('shutdown');
88 }
89 }
90  
91 sub _make_request {
92 my ($self, $request, $heap) = @_;
93  
94 my ($host, $port) = $request->headers->header('Host') ?
95 split(':', $request->headers->header('Host')) : ($self->host, $self->port);
96  
97 {
98 headers => $request->headers,
99 uri => URI::WithBase->new(do {
100 my $uri = $request->uri->clone;
101 $uri->scheme('http');
102 $uri->host($host);
103 $uri->port($port);
104 $uri->path('/') if $request->uri =~ m!^https?://!i;
105  
106 my $b = $uri->clone;
107 $b->path_query('/');
108  
109 ($uri, $b);
110 }),
111 connection_info => {
112 address => $heap->{remote_ip},
113 method => $request->method,
114 port => $port,
115 user => undef,
116 _https_info => 'OFF',
117 protocol => $request->protocol(),
118 request_uri => "".$request->uri,
119 },
120 _connection => {
121 input_handle => do {
122 my $buf = $request->content;
123 open my $fh, '<', \$buf;
124 $fh;
125 },
126 output_handle => undef,
127 env => \%ENV,
128 },
129 };
130 }
131  
132 __INTERFACE__
133  
134 __END__