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