File Coverage

blib/lib/AnyEvent/HTTP/Response.pm
Criterion Covered Total %
statement 41 41 100.0
branch 5 6 83.3
condition 3 4 75.0
subroutine 9 9 100.0
pod 5 5 100.0
total 63 65 96.9


line stmt bran cond sub pod time code
1             # vim: set ts=2 sts=2 sw=2 expandtab smarttab:
2             #
3             # This file is part of AnyEvent-HTTP-Message
4             #
5             # This software is copyright (c) 2012 by Randy Stauner.
6             #
7             # This is free software; you can redistribute it and/or modify it under
8             # the same terms as the Perl 5 programming language system itself.
9             #
10 2     2   881 use strict;
  2         3  
  2         89  
11 2     2   12 use warnings;
  2         3  
  2         151  
12              
13             package AnyEvent::HTTP::Response;
14             {
15             $AnyEvent::HTTP::Response::VERSION = '0.302';
16             }
17             BEGIN {
18 2     2   39 $AnyEvent::HTTP::Response::AUTHORITY = 'cpan:RWSTAUNER';
19             }
20             # ABSTRACT: HTTP Response object for AnyEvent::HTTP
21              
22 2     2   880 use parent 'AnyEvent::HTTP::Message';
  2         434  
  2         18  
23              
24              
25             sub args {
26 5     5 1 1001 my ($self) = @_;
27             return (
28 5         21 $self->body,
29             {
30 5         14 %{ $self->headers },
31 5         27 %{ $self->pseudo_headers },
32             },
33             );
34             }
35              
36              
37             sub parse_args {
38 5     5 1 625 my $self = shift;
39 5 100       33 $self->_error(
40             q[expects two arguments: ($content_body, \%headers)]
41             )
42             unless @_ == 2;
43              
44 2         8 my $args = {
45             body => $_[0],
46             };
47              
48 2         4 my %headers = %{ $_[1] };
  2         10  
49 2         7 my %pseudo;
50             {
51 2         5 my @pseudo = grep { /^[A-Z]/ } keys %headers;
  2         7  
  5         17  
52             # remove the ae-http pseudo-headers (init-capped)
53 2         11 @pseudo{ @pseudo } = delete @headers{ @pseudo };
54             }
55 2         11 @$args{qw(headers pseudo_headers)} = (\%headers, \%pseudo);
56              
57 2         9 return $args;
58             }
59              
60              
61             sub from_http_message {
62 3     3 1 11 my ($self, $res) = @_;
63 3   50     50 my $args = {
64 3         8 body => $res->${\ ($res->can('decoded_content') || 'content') },
65             pseudo_headers => {
66             Status => $res->code,
67             Reason => $res->message,
68             },
69             headers => $self->_hash_http_headers($res->headers),
70             };
71 3 100       17 if( my $proto = $res->protocol ){
72             # regexp taken straight from AnyEvent::HTTP 2.13
73 1         12 $args->{pseudo_headers}{HTTPVersion} = ($proto =~ /^HTTP\/0*([0-9\.]+)/)[0];
74             }
75              
76 3         29 return $args;
77             }
78              
79              
80 13   100 13 1 1131 sub pseudo_headers { $_[0]->{pseudo_headers} ||= {} }
81              
82              
83             sub to_http_message {
84 2     2 1 23348 my ($self) = @_;
85 2         829 require HTTP::Response;
86              
87 2         11 my $res = HTTP::Response->new(
88 2         16 @{ $self->pseudo_headers }{qw(Status Reason)},
89 2         6868 [ %{ $self->headers } ],
90             $self->body
91             );
92 2 50       245 if( my $v = $self->pseudo_headers->{HTTPVersion} ){
93 2         19 $res->protocol("HTTP/$v")
94             }
95 2         20 return $res;
96             }
97              
98             1;
99              
100             __END__