| 1 | # ---------------------------------------------------------------------------------------------------------------------- | ||||||||||||||||||||
| 2 | # GX Framework (c) 2009-2011 Jörg A. Uzarek <uzarek@runlevelnull.de> | ||||||||||||||||||||
| 3 | # File: GX/HTTP/Message.pm | ||||||||||||||||||||
| 4 | # ---------------------------------------------------------------------------------------------------------------------- | ||||||||||||||||||||
| 5 | |||||||||||||||||||||
| 6 | package GX::HTTP::Message; | ||||||||||||||||||||
| 7 | |||||||||||||||||||||
| 8 | 22 | ▶ | use GX::Exception; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 9 | 22 | ▶ | use GX::HTTP::Body::Scalar; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 10 | 22 | ▶ | use GX::HTTP::Constants qw( CRLF ); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 11 | 22 | ▶ | use GX::HTTP::Headers; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 12 | |||||||||||||||||||||
| 13 | 22 | ▶ | use IO::File (); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 14 | |||||||||||||||||||||
| 15 | |||||||||||||||||||||
| 16 | # ---------------------------------------------------------------------------------------------------------------------- | ||||||||||||||||||||
| 17 | # Class setup | ||||||||||||||||||||
| 18 | # ---------------------------------------------------------------------------------------------------------------------- | ||||||||||||||||||||
| 19 | |||||||||||||||||||||
| 20 | 22 | ▶ | use GX::Class::Object; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 21 | |||||||||||||||||||||
| 22 | 22 | has 'body' => ( | |||||||||||||||||||
| 23 | isa => 'Scalar', | ||||||||||||||||||||
| 24 | initializer => '_initialize_body' | ||||||||||||||||||||
| 25 | ); | ||||||||||||||||||||
| 26 | |||||||||||||||||||||
| 27 | 22 | has 'headers' => ( | |||||||||||||||||||
| 28 | isa => 'Scalar', | ||||||||||||||||||||
| 29 | initializer => '_initialize_headers' | ||||||||||||||||||||
| 30 | ); | ||||||||||||||||||||
| 31 | |||||||||||||||||||||
| 32 | 22 | has 'protocol' => ( | |||||||||||||||||||
| 33 | isa => 'Scalar' | ||||||||||||||||||||
| 34 | ); | ||||||||||||||||||||
| 35 | |||||||||||||||||||||
| 36 | 22 | build; | |||||||||||||||||||
| 37 | |||||||||||||||||||||
| 38 | |||||||||||||||||||||
| 39 | # ---------------------------------------------------------------------------------------------------------------------- | ||||||||||||||||||||
| 40 | # Public methods | ||||||||||||||||||||
| 41 | # ---------------------------------------------------------------------------------------------------------------------- | ||||||||||||||||||||
| 42 | |||||||||||||||||||||
| 43 | 3 | ▶ | sub add { | ||||||||||||||||||
|
Subroutine
add: called CC=2 SCAR=8.1
Pod
documented
| |||||||||||||||||||||
| 44 | |||||||||||||||||||||
| 45 | 3 | my $self = shift; | |||||||||||||||||||
| 46 | |||||||||||||||||||||
| 47 | 3 | ▶ | return ( $self->body || complain "No body object" )->add( @_ ); | ||||||||||||||||||
|
Condition33%1/3
$self->body || &complain('No body object')
MC/DC0%0/2
$self->body || &complain('No body object')
$self->body
&complain('No body object')
Truth table33%1/3
A || B
A= $self->bodyB= &complain('No body object')
| |||||||||||||||||||||
| 48 | |||||||||||||||||||||
| 49 | } | ||||||||||||||||||||
| 50 | |||||||||||||||||||||
| 51 | 6 | ▶ | sub as_string { | ||||||||||||||||||
|
Subroutine
as_string: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||
| 52 | |||||||||||||||||||||
| 53 | 6 | my $self = shift; | |||||||||||||||||||
| 54 | |||||||||||||||||||||
| 55 | 6 | my $string = ''; | |||||||||||||||||||
| 56 | |||||||||||||||||||||
| 57 | 6 | eval { | |||||||||||||||||||
| 58 | 6 | $self->print_to( IO::File->new( \$string, '>' ) ); | |||||||||||||||||||
| 59 | }; | ||||||||||||||||||||
| 60 | |||||||||||||||||||||
| 61 | 6 | ▶ | complain $@ if $@; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 62 | |||||||||||||||||||||
| 63 | 6 | return $string; | |||||||||||||||||||
| 64 | |||||||||||||||||||||
| 65 | } | ||||||||||||||||||||
| 66 | |||||||||||||||||||||
| 67 | 0 | ▶ | sub header { | ||||||||||||||||||
|
Subroutine
header: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||||||||||||
| 68 | |||||||||||||||||||||
| 69 | 0 | my $self = shift; | |||||||||||||||||||
| 70 | |||||||||||||||||||||
| 71 | 0 | ▶ | if ( @_ > 1 ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 72 | 0 | ▶ | return ( $self->headers || complain "No headers object" )->set( $_[0], $_[1] ); | ||||||||||||||||||
|
Condition0%0/3
$self->headers || &complain('No headers object')
MC/DC0%0/2
$self->headers || &complain('No headers object')
$self->headers
&complain('No headers object')
Truth table0%0/3
A || B
A= $self->headersB= &complain('No headers object')
| |||||||||||||||||||||
| 73 | } | ||||||||||||||||||||
| 74 | else { | ||||||||||||||||||||
| 75 | 0 | ▶ | return ( $self->headers || return )->get( $_[0] ); | ||||||||||||||||||
|
Condition0%0/2
$self->headers || (return)
MC/DC0%0/1
$self->headers || (return)
$self->headers
Truth table0%0/2
A
A= $self->headers
| |||||||||||||||||||||
| 76 | } | ||||||||||||||||||||
| 77 | |||||||||||||||||||||
| 78 | } | ||||||||||||||||||||
| 79 | |||||||||||||||||||||
| 80 | 18 | ▶ | sub print_to { | ||||||||||||||||||
|
Subroutine
print_to: called CC=7 SCAR=22.0
Pod
documented
| |||||||||||||||||||||
| 81 | |||||||||||||||||||||
| 82 | 18 | my $self = shift; | |||||||||||||||||||
| 83 | 18 | my $handle = shift; | |||||||||||||||||||
| 84 | |||||||||||||||||||||
| 85 | 18 | my $result = eval { | |||||||||||||||||||
| 86 | |||||||||||||||||||||
| 87 | 18 | ▶ | if ( my $headers = $self->headers ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 88 | 18 | ▶ | $handle->print( $headers->as_string, CRLF ) or return; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 89 | } | ||||||||||||||||||||
| 90 | else { | ||||||||||||||||||||
| 91 | 0 | ▶ | $handle->print( CRLF ) or return; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 92 | } | ||||||||||||||||||||
| 93 | |||||||||||||||||||||
| 94 | 18 | ▶ | if ( my $body = $self->body ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 95 | 18 | ▶ | $body->print_to( $handle ) or return; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 96 | } | ||||||||||||||||||||
| 97 | |||||||||||||||||||||
| 98 | 18 | 1; | |||||||||||||||||||
| 99 | |||||||||||||||||||||
| 100 | }; | ||||||||||||||||||||
| 101 | |||||||||||||||||||||
| 102 | 18 | ▶ | complain $@ if $@; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 103 | |||||||||||||||||||||
| 104 | 18 | return $result; | |||||||||||||||||||
| 105 | |||||||||||||||||||||
| 106 | } | ||||||||||||||||||||
| 107 | |||||||||||||||||||||
| 108 | { | ||||||||||||||||||||
| 109 | |||||||||||||||||||||
| 110 | 22 | for ( qw( | |||||||||||||||||||
| 111 | Content-Encoding | ||||||||||||||||||||
| 112 | Content-Length | ||||||||||||||||||||
| 113 | Content-Type | ||||||||||||||||||||
| 114 | ) ) { | ||||||||||||||||||||
| 115 | 66 | __PACKAGE__->_install_header_accessor( $_ ); | |||||||||||||||||||
| 116 | } | ||||||||||||||||||||
| 117 | |||||||||||||||||||||
| 118 | } | ||||||||||||||||||||
| 119 | |||||||||||||||||||||
| 120 | |||||||||||||||||||||
| 121 | # ---------------------------------------------------------------------------------------------------------------------- | ||||||||||||||||||||
| 122 | # Private methods | ||||||||||||||||||||
| 123 | # ---------------------------------------------------------------------------------------------------------------------- | ||||||||||||||||||||
| 124 | |||||||||||||||||||||
| 125 | 22 | ▶ | sub _initialize_body { | ||||||||||||||||||
|
Subroutine
_initialize_body: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 126 | |||||||||||||||||||||
| 127 | 22 | return GX::HTTP::Body::Scalar->new; | |||||||||||||||||||
| 128 | |||||||||||||||||||||
| 129 | } | ||||||||||||||||||||
| 130 | |||||||||||||||||||||
| 131 | 0 | ▶ | sub _initialize_headers { | ||||||||||||||||||
|
Subroutine
_initialize_headers: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 132 | |||||||||||||||||||||
| 133 | 0 | return GX::HTTP::Headers->new; | |||||||||||||||||||
| 134 | |||||||||||||||||||||
| 135 | } | ||||||||||||||||||||
| 136 | |||||||||||||||||||||
| 137 | 101 | ▶ | sub _install_header_accessor { | ||||||||||||||||||
|
Subroutine
_install_header_accessor: called CC=1 SCAR=0.1
| |||||||||||||||||||||
| 138 | |||||||||||||||||||||
| 139 | 101 | my $class = shift; | |||||||||||||||||||
| 140 | 101 | my $field = shift; | |||||||||||||||||||
| 141 | |||||||||||||||||||||
| 142 | 101 | ( my $accessor = lc $field ) =~ tr/-/_/; | |||||||||||||||||||
| 143 | |||||||||||||||||||||
| 144 | $class->meta->add_method( | ||||||||||||||||||||
| 145 | |||||||||||||||||||||
| 146 | 8 | ▶ | $accessor => sub { | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=4 SCAR=14.6
| |||||||||||||||||||||
| 147 | |||||||||||||||||||||
| 148 | 8 | my $self = shift; | |||||||||||||||||||
| 149 | |||||||||||||||||||||
| 150 | 8 | ▶ | if ( @_ ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 151 | 4 | ▶ | return ( $self->headers || complain "No headers object" )->set( $field, $_[0] ); | ||||||||||||||||||
|
Condition33%1/3
$self->headers || &complain('No headers object')
MC/DC0%0/2
$self->headers || &complain('No headers object')
$self->headers
&complain('No headers object')
Truth table33%1/3
A || B
A= $self->headersB= &complain('No headers object')
| |||||||||||||||||||||
| 152 | } | ||||||||||||||||||||
| 153 | else { | ||||||||||||||||||||
| 154 | 4 | ▶ | return ( $self->headers || return )->get( $field ); | ||||||||||||||||||
|
Condition50%1/2
$self->headers || (return)
MC/DC0%0/1
$self->headers || (return)
$self->headers
Truth table50%1/2
A
A= $self->headers
| |||||||||||||||||||||
| 155 | } | ||||||||||||||||||||
| 156 | |||||||||||||||||||||
| 157 | } | ||||||||||||||||||||
| 158 | |||||||||||||||||||||
| 159 | 101 | ); | |||||||||||||||||||
| 160 | |||||||||||||||||||||
| 161 | 101 | return; | |||||||||||||||||||
| 162 | |||||||||||||||||||||
| 163 | } | ||||||||||||||||||||
| 164 | |||||||||||||||||||||
| 165 | |||||||||||||||||||||
| 166 | # ---------------------------------------------------------------------------------------------------------------------- | ||||||||||||||||||||
| 167 | # Aliases | ||||||||||||||||||||
| 168 | # ---------------------------------------------------------------------------------------------------------------------- | ||||||||||||||||||||
| 169 | |||||||||||||||||||||
| 170 | 22 | *print = \&add; | |||||||||||||||||||
| 171 | |||||||||||||||||||||
| 172 | |||||||||||||||||||||
| 173 | 22 | 1; | |||||||||||||||||||
| 174 | |||||||||||||||||||||
| 175 | __END__ | ||||||||||||||||||||