| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package Ark::Response; | 
| 2 | 60 |  |  | 60 |  | 326 | use Mouse; | 
|  | 60 |  |  |  |  | 113 |  | 
|  | 60 |  |  |  |  | 493 |  | 
| 3 | 60 |  |  | 60 |  | 17120 | use Scalar::Util (); | 
|  | 60 |  |  |  |  | 130 |  | 
|  | 60 |  |  |  |  | 9838 |  | 
| 4 |  |  |  |  |  |  |  | 
| 5 |  |  |  |  |  |  | extends 'Plack::Response'; | 
| 6 |  |  |  |  |  |  |  | 
| 7 |  |  |  |  |  |  | has body => ( | 
| 8 |  |  |  |  |  |  | is        => 'rw', | 
| 9 |  |  |  |  |  |  | predicate => 'has_body', | 
| 10 |  |  |  |  |  |  | ); | 
| 11 |  |  |  |  |  |  |  | 
| 12 |  |  |  |  |  |  | has status => ( | 
| 13 |  |  |  |  |  |  | is      => 'rw', | 
| 14 |  |  |  |  |  |  | default => 200, | 
| 15 |  |  |  |  |  |  | ); | 
| 16 |  |  |  |  |  |  |  | 
| 17 |  |  |  |  |  |  | has binary => ( | 
| 18 |  |  |  |  |  |  | is      => 'rw', | 
| 19 |  |  |  |  |  |  | default => 0, | 
| 20 |  |  |  |  |  |  | ); | 
| 21 |  |  |  |  |  |  |  | 
| 22 |  |  |  |  |  |  | has streaming => ( | 
| 23 |  |  |  |  |  |  | is        => 'rw', | 
| 24 |  |  |  |  |  |  | isa       => 'CodeRef', | 
| 25 |  |  |  |  |  |  | predicate => 'is_streaming', | 
| 26 |  |  |  |  |  |  | ); | 
| 27 |  |  |  |  |  |  |  | 
| 28 |  |  |  |  |  |  | has deferred => ( | 
| 29 |  |  |  |  |  |  | is        => 'rw', | 
| 30 |  |  |  |  |  |  | isa       => 'CodeRef', | 
| 31 |  |  |  |  |  |  | predicate => 'is_deferred', | 
| 32 |  |  |  |  |  |  | ); | 
| 33 |  |  |  |  |  |  |  | 
| 34 |  |  |  |  |  |  | has deferred_response => ( | 
| 35 |  |  |  |  |  |  | is  => 'rw', | 
| 36 |  |  |  |  |  |  | isa => 'CodeRef', | 
| 37 |  |  |  |  |  |  | ); | 
| 38 |  |  |  |  |  |  |  | 
| 39 | 60 |  |  | 60 |  | 372 | no Mouse; | 
|  | 60 |  |  |  |  | 165 |  | 
|  | 60 |  |  |  |  | 367 |  | 
| 40 |  |  |  |  |  |  |  | 
| 41 |  |  |  |  |  |  | sub finalize { | 
| 42 | 280 |  |  | 280 | 1 | 630 | my $self = shift; | 
| 43 |  |  |  |  |  |  |  | 
| 44 | 280 | 50 | 33 |  |  | 2139 | if (!$self->is_deferred && !$self->is_streaming) { | 
| 45 | 280 |  |  |  |  | 2087 | return $self->SUPER::finalize; | 
| 46 |  |  |  |  |  |  | } | 
| 47 |  |  |  |  |  |  |  | 
| 48 | 0 |  |  |  |  |  | my $headers = $self->headers->clone; | 
| 49 | 0 |  |  |  |  |  | $self->_finalize_cookies($headers); | 
| 50 |  |  |  |  |  |  |  | 
| 51 | 0 | 0 |  |  |  |  | if ($self->is_deferred) { | 
| 52 | 0 | 0 |  |  |  |  | if (my $cb = $self->deferred_response) { | 
| 53 | 0 |  |  |  |  |  | my $body = $self->_body; | 
| 54 |  |  |  |  |  |  |  | 
| 55 | 0 |  |  |  |  |  | my $response = [ | 
| 56 |  |  |  |  |  |  | $self->status, | 
| 57 |  |  |  |  |  |  | +[ | 
| 58 |  |  |  |  |  |  | map { | 
| 59 | 0 |  |  |  |  |  | my $k = $_; | 
| 60 | 0 |  |  |  |  |  | map { ( $k => $_ ) } $headers->header($_); | 
|  | 0 |  |  |  |  |  |  | 
| 61 |  |  |  |  |  |  | } $headers->header_field_names | 
| 62 |  |  |  |  |  |  | ], | 
| 63 |  |  |  |  |  |  | $body, | 
| 64 |  |  |  |  |  |  | ]; | 
| 65 |  |  |  |  |  |  |  | 
| 66 | 0 |  |  |  |  |  | $cb->($response); | 
| 67 |  |  |  |  |  |  | } | 
| 68 |  |  |  |  |  |  | else { | 
| 69 |  |  |  |  |  |  | my $r = sub { | 
| 70 | 0 |  |  | 0 |  |  | my $respond = shift; | 
| 71 | 0 |  |  |  |  |  | $self->deferred_response($respond); | 
| 72 | 0 |  |  |  |  |  | $self->deferred->($respond); | 
| 73 | 0 |  |  |  |  |  | }; | 
| 74 | 0 |  |  |  |  |  | Scalar::Util::weaken($self); | 
| 75 | 0 |  |  |  |  |  | return $r; | 
| 76 |  |  |  |  |  |  | } | 
| 77 |  |  |  |  |  |  | } | 
| 78 |  |  |  |  |  |  | else { # streaming | 
| 79 | 0 |  |  |  |  |  | my $response = [ | 
| 80 |  |  |  |  |  |  | $self->status, | 
| 81 |  |  |  |  |  |  | +[ | 
| 82 |  |  |  |  |  |  | map { | 
| 83 | 0 |  |  |  |  |  | my $k = $_; | 
| 84 |  |  |  |  |  |  | # steal from Plack::Response | 
| 85 | 0 |  |  |  |  |  | map { | 
| 86 | 0 |  |  |  |  |  | my $v = $_; | 
| 87 | 0 |  |  |  |  |  | $v =~ s/\015\012[\040|\011]+/chr(32)/ge; # replace LWS with a single SP | 
|  | 0 |  |  |  |  |  |  | 
| 88 | 0 |  |  |  |  |  | $v =~ s/\015|\012//g; # remove CR and LF since the char is invalid here | 
| 89 |  |  |  |  |  |  |  | 
| 90 | 0 |  |  |  |  |  | ( $k => $v ) | 
| 91 |  |  |  |  |  |  | } $headers->header($_); | 
| 92 |  |  |  |  |  |  |  | 
| 93 |  |  |  |  |  |  | } $headers->header_field_names | 
| 94 |  |  |  |  |  |  | ], | 
| 95 |  |  |  |  |  |  | ]; | 
| 96 |  |  |  |  |  |  |  | 
| 97 |  |  |  |  |  |  | return sub { | 
| 98 | 0 |  |  | 0 |  |  | my $respond = shift; | 
| 99 | 0 |  |  |  |  |  | my $writer  = $respond->($response); | 
| 100 | 0 |  |  |  |  |  | $self->streaming->($writer); | 
| 101 | 0 |  |  |  |  |  | }; | 
| 102 |  |  |  |  |  |  | } | 
| 103 |  |  |  |  |  |  | } | 
| 104 |  |  |  |  |  |  |  | 
| 105 |  |  |  |  |  |  | __PACKAGE__->meta->make_immutable; |