| 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 | 6 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 11 | 6 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 12 | |||||||||||||||||||||
| 13 | package AnyEvent::HTTP::Message; | ||||||||||||||||||||
| 14 | { | ||||||||||||||||||||
| 15 | 6 | $AnyEvent::HTTP::Message::VERSION = '0.302'; | |||||||||||||||||||
| 16 | } | ||||||||||||||||||||
| 17 | # git description: v0.301-3-ge92f3a7 | ||||||||||||||||||||
| 18 | |||||||||||||||||||||
| 19 | 6 | ▶ | BEGIN { | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 20 | 6 | $AnyEvent::HTTP::Message::AUTHORITY = 'cpan:RWSTAUNER'; | |||||||||||||||||||
| 21 | } | ||||||||||||||||||||
| 22 | # ABSTRACT: Lightweight objects for AnyEvent::HTTP Request/Response | ||||||||||||||||||||
| 23 | |||||||||||||||||||||
| 24 | 6 | ▶ | use Carp (); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 25 | 6 | ▶ | use Scalar::Util (); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 26 | |||||||||||||||||||||
| 27 | |||||||||||||||||||||
| 28 | 21 | ▶ | sub new { | ||||||||||||||||||
|
Subroutine
new: called CC=7 SCAR=19.5
Pod
documented
| |||||||||||||||||||||
| 29 | 21 | my $class = shift; | |||||||||||||||||||
| 30 | |||||||||||||||||||||
| 31 | 21 | my $self; | |||||||||||||||||||
| 32 | 21 | ▶ | if( ref($_[0]) eq 'HASH' ){ | ||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||
| 33 | # if passed a single hashref take a shallow copy | ||||||||||||||||||||
| 34 | 5 | $self = { %{ $_[0] } }; | |||||||||||||||||||
| 35 | } | ||||||||||||||||||||
| 36 | ▶ | elsif( Scalar::Util::blessed($_[0]) && $_[0]->isa('HTTP::Message') ){ | |||||||||||||||||||
|
Condition66%2/3
builtin::blessed($_[0]) and $_[0]->isa("HTTP::Message")
MC/DC50%1/2
builtin::blessed($_[0]) and $_[0]->isa("HTTP::Message")
builtin::blessed($_[0])
$_[0]->isa("HTTP::Message")
Truth table66%2/3
A and B
A= builtin::blessed($_[0])B= $_[0]->isa("HTTP::Message")
| |||||||||||||||||||||
| 37 | # allow an optional second hashref for extra params | ||||||||||||||||||||
| 38 | 6 | $self = $class->from_http_message(@_); | |||||||||||||||||||
| 39 | } | ||||||||||||||||||||
| 40 | else { | ||||||||||||||||||||
| 41 | # otherwise it's the argument list for http_request() | ||||||||||||||||||||
| 42 | 10 | $self = $class->parse_args(@_); | |||||||||||||||||||
| 43 | } | ||||||||||||||||||||
| 44 | |||||||||||||||||||||
| 45 | # accept 'content' as an alias for 'body', but store as 'body' | ||||||||||||||||||||
| 46 | $self->{body} = delete $self->{content} | ||||||||||||||||||||
| 47 | 18 | ▶ | if exists $self->{content}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 48 | |||||||||||||||||||||
| 49 | $self->{body} = '' | ||||||||||||||||||||
| 50 | 18 | ▶ | if !defined $self->{body}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 51 | |||||||||||||||||||||
| 52 | $self->{headers} = $self->{headers} | ||||||||||||||||||||
| 53 | ? $class->_normalize_headers($self->{headers}) | ||||||||||||||||||||
| 54 | 18 | ▶ | : {}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 55 | |||||||||||||||||||||
| 56 | 18 | bless $self, $class; | |||||||||||||||||||
| 57 | } | ||||||||||||||||||||
| 58 | |||||||||||||||||||||
| 59 | 10 | ▶ | sub _error { | ||||||||||||||||||
|
Subroutine
_error: called CC=2 SCAR=7.0
| |||||||||||||||||||||
| 60 | 10 | my $self = shift; | |||||||||||||||||||
| 61 | 10 | ▶ | @_ = join ' ', (ref($self) || $self), 'error:', @_; | ||||||||||||||||||
|
Condition66%2/3
ref $self || $self
MC/DC0%0/2
ref $self || $self
ref $self
$self
Truth table66%2/3
A || B
A= ref $selfB= $self
| |||||||||||||||||||||
| 62 | 10 | goto &Carp::croak; | |||||||||||||||||||
| 63 | } | ||||||||||||||||||||
| 64 | |||||||||||||||||||||
| 65 | |||||||||||||||||||||
| 66 | 1 | ▶ | sub parse_args { | ||||||||||||||||||
|
Subroutine
parse_args: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 67 | 1 | $_[0]->_error('parse_args() is not defined'); | |||||||||||||||||||
| 68 | } | ||||||||||||||||||||
| 69 | |||||||||||||||||||||
| 70 | |||||||||||||||||||||
| 71 | 1 | ▶ | sub from_http_message { | ||||||||||||||||||
|
Subroutine
from_http_message: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 72 | 1 | $_[0]->_error('from_http_message() is not defined'); | |||||||||||||||||||
| 73 | } | ||||||||||||||||||||
| 74 | |||||||||||||||||||||
| 75 | # turn HTTP::Headers into a hashref | ||||||||||||||||||||
| 76 | 4 | ▶ | sub _hash_http_headers { | ||||||||||||||||||
|
Subroutine
_hash_http_headers: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 77 | 4 | my ($self, $headers) = @_; | |||||||||||||||||||
| 78 | 4 | my $aeh = {}; | |||||||||||||||||||
| 79 | 9 | ▶ | $headers->scan(sub { | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||
| 80 | 9 | my ($k, $v) = @_; | |||||||||||||||||||
| 81 | 9 | my $l = lc $k; | |||||||||||||||||||
| 82 | 9 | ▶ | $aeh->{$l} = exists($aeh->{$l}) ? $aeh->{$l} . ',' . $v : $v; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 83 | 4 | }); | |||||||||||||||||||
| 84 | 4 | return $aeh; | |||||||||||||||||||
| 85 | } | ||||||||||||||||||||
| 86 | |||||||||||||||||||||
| 87 | |||||||||||||||||||||
| 88 | # stubs for read-only accessors | ||||||||||||||||||||
| 89 | 26 | ▶ | sub body { $_[0]->{body} } | ||||||||||||||||||
|
Subroutine
body: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 90 | 34 | ▶ | sub headers { $_[0]->{headers} ||= {} } | ||||||||||||||||||
|
Subroutine
headers: called CC=2 SCAR=7.6
Pod
documented
Condition50%1/2
$_[0]{"headers"} ||= {}
MC/DC0%0/1
$_[0]{"headers"} ||= {}
$_[0]{"headers"}
Truth table50%1/2
A
A= $_[0]{"headers"}
| |||||||||||||||||||||
| 91 | |||||||||||||||||||||
| 92 | # alias | ||||||||||||||||||||
| 93 | 8 | ▶ | sub content { $_[0]->body } | ||||||||||||||||||
|
Subroutine
content: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 94 | |||||||||||||||||||||
| 95 | |||||||||||||||||||||
| 96 | 12 | ▶ | sub header { | ||||||||||||||||||
|
Subroutine
header: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 97 | 12 | my ($self, $h) = @_; | |||||||||||||||||||
| 98 | 12 | $h =~ tr/_/-/; | |||||||||||||||||||
| 99 | 12 | return $self->headers->{ lc $h }; | |||||||||||||||||||
| 100 | } | ||||||||||||||||||||
| 101 | |||||||||||||||||||||
| 102 | # ensure keys are stored with dashes (not underscores) and lower-cased | ||||||||||||||||||||
| 103 | 16 | ▶ | sub _normalize_headers { | ||||||||||||||||||
|
Subroutine
_normalize_headers: called CC=2 SCAR=6.9
| |||||||||||||||||||||
| 104 | 16 | my ($self, $headers) = @_; | |||||||||||||||||||
| 105 | 16 | my $norm = {}; | |||||||||||||||||||
| 106 | 16 | ▶ | while( my ($k, $v) = each %$headers ){ | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 107 | 19 | my $n = $k; | |||||||||||||||||||
| 108 | 19 | $n =~ tr/_/-/; | |||||||||||||||||||
| 109 | 19 | $norm->{ lc $n } = $v; | |||||||||||||||||||
| 110 | } | ||||||||||||||||||||
| 111 | 16 | return $norm; | |||||||||||||||||||
| 112 | } | ||||||||||||||||||||
| 113 | |||||||||||||||||||||
| 114 | 6 | 1; | |||||||||||||||||||
| 115 | |||||||||||||||||||||
| 116 | __END__ | ||||||||||||||||||||