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
|
1
|
|
|
1
|
|
975
|
use strict; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
37
|
|
11
|
1
|
|
|
1
|
|
5
|
use warnings; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
59
|
|
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
package AnyEvent::HTTP::Request; |
14
|
|
|
|
|
|
|
{ |
15
|
|
|
|
|
|
|
$AnyEvent::HTTP::Request::VERSION = '0.302'; |
16
|
|
|
|
|
|
|
} |
17
|
|
|
|
|
|
|
BEGIN { |
18
|
1
|
|
|
1
|
|
18
|
$AnyEvent::HTTP::Request::AUTHORITY = 'cpan:RWSTAUNER'; |
19
|
|
|
|
|
|
|
} |
20
|
|
|
|
|
|
|
# ABSTRACT: HTTP Request object for AnyEvent::HTTP |
21
|
|
|
|
|
|
|
|
22
|
1
|
|
|
1
|
|
793
|
use parent 'AnyEvent::HTTP::Message'; |
|
1
|
|
|
|
|
322
|
|
|
1
|
|
|
|
|
7
|
|
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
sub new { |
26
|
7
|
|
|
7
|
1
|
15648
|
my $class = shift; |
27
|
7
|
|
|
|
|
42
|
my $self = $class->SUPER::new(@_); |
28
|
|
|
|
|
|
|
|
29
|
7
|
|
|
|
|
25
|
$self->{method} = uc $self->{method}; |
30
|
|
|
|
|
|
|
|
31
|
7
|
|
|
|
|
18
|
return $self; |
32
|
|
|
|
|
|
|
} |
33
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
sub parse_args { |
36
|
8
|
|
|
8
|
1
|
1150
|
my $self = shift; |
37
|
|
|
|
|
|
|
|
38
|
8
|
100
|
|
|
|
36
|
$self->_error( |
39
|
|
|
|
|
|
|
q[expects an odd number of arguments:], |
40
|
|
|
|
|
|
|
q[($method, $uri, (key => value, ...)*, \&callback)] |
41
|
|
|
|
|
|
|
) |
42
|
|
|
|
|
|
|
unless @_ & 1; ## no critic BitwiseOperators |
43
|
|
|
|
|
|
|
|
44
|
5
|
|
|
|
|
36
|
my $args = { |
45
|
|
|
|
|
|
|
method => shift, |
46
|
|
|
|
|
|
|
uri => shift, |
47
|
|
|
|
|
|
|
cb => pop, |
48
|
|
|
|
|
|
|
params => { @_ }, |
49
|
|
|
|
|
|
|
}; |
50
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
# remove these from params |
52
|
|
|
|
|
|
|
$args->{$_} = delete $args->{params}{$_} |
53
|
5
|
|
|
|
|
33
|
for qw( body headers ); |
54
|
|
|
|
|
|
|
|
55
|
5
|
|
|
|
|
19
|
return $args; |
56
|
|
|
|
|
|
|
} |
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
sub from_http_message { |
60
|
1
|
|
|
1
|
1
|
3
|
my ($self, $req, $extra) = @_; |
61
|
1
|
50
|
|
|
|
6
|
my $args = { |
62
|
|
|
|
|
|
|
method => $req->method, |
63
|
|
|
|
|
|
|
uri => $req->uri, |
64
|
|
|
|
|
|
|
headers => $self->_hash_http_headers($req->headers), |
65
|
|
|
|
|
|
|
body => $req->content, |
66
|
|
|
|
|
|
|
(ref($extra) eq 'HASH' ? %$extra : ()), |
67
|
|
|
|
|
|
|
}; |
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
# rt-85665: AE:H will provide it's own content-length. |
70
|
|
|
|
|
|
|
# If you provide your own it may persist incorrectly across redirects. |
71
|
|
|
|
|
|
|
delete $args->{headers}{$_} |
72
|
1
|
|
|
|
|
20
|
for qw( content-length ); |
73
|
|
|
|
|
|
|
|
74
|
1
|
|
|
|
|
3
|
return $args; |
75
|
|
|
|
|
|
|
} |
76
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
sub args { |
79
|
5
|
|
|
5
|
1
|
7212
|
my ($self) = @_; |
80
|
|
|
|
|
|
|
return ( |
81
|
5
|
|
|
|
|
13
|
$self->method => $self->uri, |
82
|
|
|
|
|
|
|
body => $self->body, |
83
|
|
|
|
|
|
|
headers => $self->headers, |
84
|
5
|
|
|
|
|
15
|
%{ $self->params }, |
85
|
|
|
|
|
|
|
$self->cb, |
86
|
|
|
|
|
|
|
); |
87
|
|
|
|
|
|
|
} |
88
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
|
90
|
11
|
|
|
11
|
1
|
1728
|
sub method { $_[0]->{method} } |
91
|
11
|
|
|
11
|
1
|
74
|
sub uri { $_[0]->{uri} } |
92
|
|
|
|
|
|
|
sub cb { |
93
|
14
|
|
|
14
|
1
|
3206
|
my $self = shift; |
94
|
14
|
100
|
|
|
|
55
|
$self->_error( |
95
|
|
|
|
|
|
|
q[cb() is a read-only accessor (for consistency and to avoid confusion).], |
96
|
|
|
|
|
|
|
q[To execute the callback dereference it or use respond_with().] |
97
|
|
|
|
|
|
|
) |
98
|
|
|
|
|
|
|
if @_; |
99
|
13
|
|
|
|
|
65
|
return $self->{cb}; |
100
|
|
|
|
|
|
|
} |
101
|
8
|
|
50
|
8
|
1
|
52
|
sub params { $_[0]->{params} ||= {} } |
102
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
sub respond_with { |
105
|
4
|
|
|
4
|
1
|
8522
|
my $self = shift; |
106
|
4
|
|
|
|
|
12
|
my ($res) = @_; # don't shift |
107
|
4
|
|
|
|
|
1079
|
require AnyEvent::HTTP::Response; |
108
|
|
|
|
|
|
|
$res = AnyEvent::HTTP::Response->new(@_) |
109
|
4
|
100
|
|
|
|
10
|
unless do { local $@; eval { $res->isa('AnyEvent::HTTP::Response') } }; |
|
4
|
|
|
|
|
6
|
|
|
4
|
|
|
|
|
9
|
|
|
4
|
|
|
|
|
70
|
|
110
|
3
|
|
|
|
|
18
|
return $self->cb->($res->args); |
111
|
|
|
|
|
|
|
} |
112
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
sub send { |
115
|
3
|
|
|
3
|
1
|
3192
|
my ($self) = @_; |
116
|
3
|
|
|
|
|
21
|
require AnyEvent::HTTP; |
117
|
|
|
|
|
|
|
# circumvent the sub's prototype |
118
|
3
|
|
|
|
|
16
|
&AnyEvent::HTTP::http_request( $self->args ); |
119
|
|
|
|
|
|
|
} |
120
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
sub to_http_message { |
123
|
3
|
|
|
3
|
1
|
52413
|
my ($self) = @_; |
124
|
3
|
|
|
|
|
1378
|
require HTTP::Request; |
125
|
|
|
|
|
|
|
|
126
|
3
|
|
|
|
|
21
|
my $res = HTTP::Request->new( |
127
|
|
|
|
|
|
|
$self->method, |
128
|
|
|
|
|
|
|
$self->uri, |
129
|
3
|
|
|
|
|
1121
|
[ %{ $self->headers } ], |
130
|
|
|
|
|
|
|
$self->body |
131
|
|
|
|
|
|
|
); |
132
|
3
|
|
|
|
|
17811
|
return $res; |
133
|
|
|
|
|
|
|
} |
134
|
|
|
|
|
|
|
|
135
|
|
|
|
|
|
|
1; |
136
|
|
|
|
|
|
|
|
137
|
|
|
|
|
|
|
__END__ |