| 1 | |||||||||||
| 2 | package Clio::Server::HTTP::Client::Stream; | ||||||||||
| 3 | 2 | ▶ | BEGIN { | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 2 | $Clio::Server::HTTP::Client::Stream::AUTHORITY = 'cpan:AJGB'; | |||||||||
| 5 | } | ||||||||||
| 6 | { | ||||||||||
| 7 | 2 | $Clio::Server::HTTP::Client::Stream::VERSION = '0.02'; | |||||||||
| 8 | } | ||||||||||
| 9 | # ABSTRACT: Clio HTTP Client for streaming connections | ||||||||||
| 10 | |||||||||||
| 11 | 2 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 12 | 2 | ▶ | use Moo; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 13 | |||||||||||
| 14 | 2 | ▶ | use Scalar::Util qw( blessed ); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 15 | |||||||||||
| 16 | 2 | extends qw( Clio::Client ); | |||||||||
| 17 | |||||||||||
| 18 | |||||||||||
| 19 | 2 | has 'writer' => ( | |||||||||
| 20 | is => 'rw', | ||||||||||
| 21 | ); | ||||||||||
| 22 | |||||||||||
| 23 | |||||||||||
| 24 | 2 | has 'req' => ( | |||||||||
| 25 | is => 'rw', | ||||||||||
| 26 | ); | ||||||||||
| 27 | |||||||||||
| 28 | |||||||||||
| 29 | 0 | ▶ | sub write { | ||||||||
|
Subroutine
write: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||
| 30 | 0 | my $self = shift; | |||||||||
| 31 | |||||||||||
| 32 | 0 | $self->log->trace("Stream Client ", $self->id, " writing '@_'"); | |||||||||
| 33 | |||||||||||
| 34 | 0 | eval { | |||||||||
| 35 | 0 | $self->writer->write( @_ ); | |||||||||
| 36 | }; | ||||||||||
| 37 | 0 | ▶ | if ( my $e = $@ ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 38 | 0 | $self->_handle_client_error($e); | |||||||||
| 39 | } | ||||||||||
| 40 | } | ||||||||||
| 41 | |||||||||||
| 42 | |||||||||||
| 43 | |||||||||||
| 44 | 0 | ▶ | sub respond { | ||||||||
|
Subroutine
respond: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||
| 45 | 0 | my ($self, %args) = @_; | |||||||||
| 46 | |||||||||||
| 47 | 0 | ▶ | if ( my $input = $args{input} ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 48 | 0 | my $message = $input->{message}; | |||||||||
| 49 | 0 | $self->_process->write( $message ); | |||||||||
| 50 | |||||||||||
| 51 | 0 | return [ 200, [ | |||||||||
| 52 | 'Content-Type' => 'text/plain; charset=utf-8', | ||||||||||
| 53 | 'Access-Control-Allow-Origin' => '*', | ||||||||||
| 54 | ], [ "ACK" ] ]; | ||||||||||
| 55 | } else { | ||||||||||
| 56 | 0 | ▶ | return sub { | ||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||
| 57 | 0 | my $respond = shift; | |||||||||
| 58 | |||||||||||
| 59 | 0 | my $res_status = 200; | |||||||||
| 60 | 0 | my $res_headers = [ | |||||||||
| 61 | 'Content-Type' => 'text/plain; charset=utf-8', | ||||||||||
| 62 | 'Access-Control-Allow-Origin' => '*', | ||||||||||
| 63 | ]; | ||||||||||
| 64 | 0 | my $writer = $respond->([$res_status, $res_headers]); | |||||||||
| 65 | |||||||||||
| 66 | 0 | $self->writer( $writer ); | |||||||||
| 67 | |||||||||||
| 68 | # no middleware is in use, so let's get under the bonnet | ||||||||||
| 69 | 0 | ▶ | if ( blessed($writer) eq 'Twiggy::Writer' ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 70 | 0 | ▶ | $writer->{handle}->on_error(sub { | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 71 | 0 | my ($handle, $fatal, $message) = @_; | |||||||||
| 72 | 0 | $self->_handle_client_error($message); | |||||||||
| 73 | 0 | }); | |||||||||
| 74 | } | ||||||||||
| 75 | |||||||||||
| 76 | 0 | $self->handshake; | |||||||||
| 77 | |||||||||||
| 78 | 0 | $self->_process->add_client( $self ); | |||||||||
| 79 | } | ||||||||||
| 80 | 0 | } | |||||||||
| 81 | } | ||||||||||
| 82 | |||||||||||
| 83 | |||||||||||
| 84 | 0 | ▶ | sub close { | ||||||||
|
Subroutine
close: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 85 | 0 | my $self = shift; | |||||||||
| 86 | |||||||||||
| 87 | 0 | $self->writer->close; | |||||||||
| 88 | } | ||||||||||
| 89 | |||||||||||
| 90 | 0 | ▶ | sub _handle_client_error { | ||||||||
|
Subroutine
_handle_client_error: not called CC=1 SCAR=6.9
| |||||||||||
| 91 | 0 | my ($self, $err_msg) = @_; | |||||||||
| 92 | |||||||||||
| 93 | 0 | my $cid = $self->id; | |||||||||
| 94 | |||||||||||
| 95 | 0 | $self->log->error("Connection error for client $cid: $err_msg"); | |||||||||
| 96 | 0 | $self->manager->disconnect_client( $cid ); | |||||||||
| 97 | } | ||||||||||
| 98 | |||||||||||
| 99 | |||||||||||
| 100 | 2 | 1; | |||||||||
| 101 | |||||||||||
| 102 | |||||||||||
| 103 | __END__ | ||||||||||