line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Net::HTTP::Spore::Response; |
2
|
|
|
|
|
|
|
$Net::HTTP::Spore::Response::VERSION = '0.09'; |
3
|
|
|
|
|
|
|
# ABSTRACT: Portable HTTP Response object for SPORE response |
4
|
|
|
|
|
|
|
|
5
|
34
|
|
|
34
|
|
274134
|
use strict; |
|
34
|
|
|
|
|
114
|
|
|
34
|
|
|
|
|
1207
|
|
6
|
34
|
|
|
34
|
|
187
|
use warnings; |
|
34
|
|
|
|
|
57
|
|
|
34
|
|
|
|
|
1695
|
|
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
use overload |
9
|
34
|
|
|
|
|
340
|
'@{}' => \&finalize, |
10
|
|
|
|
|
|
|
'""' => \&to_string, |
11
|
34
|
|
|
34
|
|
4622
|
fallback => 1; |
|
34
|
|
|
|
|
4302
|
|
12
|
|
|
|
|
|
|
|
13
|
34
|
|
|
34
|
|
12321
|
use HTTP::Headers; |
|
34
|
|
|
|
|
163013
|
|
|
34
|
|
|
|
|
19600
|
|
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
sub new { |
16
|
41
|
|
|
41
|
1
|
2859
|
my ( $class, $rc, $headers, $body ) = @_; |
17
|
|
|
|
|
|
|
|
18
|
41
|
|
|
|
|
117
|
my $self = bless {}, $class; |
19
|
|
|
|
|
|
|
|
20
|
41
|
100
|
|
|
|
245
|
$self->status($rc) if defined $rc; |
21
|
41
|
100
|
|
|
|
197
|
$self->body($body) if defined $body; |
22
|
41
|
|
100
|
|
|
239
|
$self->headers( $headers || [] ); |
23
|
41
|
|
|
|
|
136
|
$self; |
24
|
|
|
|
|
|
|
} |
25
|
|
|
|
|
|
|
|
26
|
8
|
|
|
8
|
1
|
32
|
sub code { shift->status(@_) } |
27
|
2
|
|
|
2
|
1
|
9
|
sub content { shift->body(@_) } |
28
|
14
|
|
|
14
|
1
|
42
|
sub env { shift->request->env } |
29
|
5
|
|
|
5
|
1
|
29
|
sub content_type { shift->headers->content_type(@_) } |
30
|
1
|
|
|
1
|
1
|
660
|
sub content_length { shift->headers->content_length(@_) } |
31
|
0
|
|
|
0
|
1
|
0
|
sub location { shift->header->header( 'Location' => @_ ) } |
32
|
21
|
|
|
21
|
0
|
74
|
sub is_success { shift->status =~ /^2\d\d$/ } |
33
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
sub status { |
35
|
145
|
|
|
145
|
1
|
928
|
my $self = shift; |
36
|
145
|
100
|
|
|
|
331
|
if (@_) { |
37
|
41
|
|
|
|
|
650
|
$self->{status} = shift; |
38
|
|
|
|
|
|
|
} |
39
|
|
|
|
|
|
|
else { |
40
|
104
|
|
|
|
|
450
|
return $self->{status}; |
41
|
|
|
|
|
|
|
} |
42
|
|
|
|
|
|
|
} |
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
sub body { |
45
|
87
|
|
|
87
|
1
|
702
|
my $self = shift; |
46
|
87
|
100
|
|
|
|
206
|
if (@_) { |
47
|
41
|
|
|
|
|
103
|
$self->{body} = shift; |
48
|
41
|
100
|
|
|
|
200
|
if ( !defined $self->{raw_body} ) { |
49
|
34
|
|
|
|
|
120
|
$self->{raw_body} = $self->{body}; |
50
|
|
|
|
|
|
|
} |
51
|
|
|
|
|
|
|
} |
52
|
|
|
|
|
|
|
else { |
53
|
46
|
|
|
|
|
317
|
return $self->{body}; |
54
|
|
|
|
|
|
|
} |
55
|
|
|
|
|
|
|
} |
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
sub raw_body { |
58
|
4
|
|
|
4
|
1
|
17
|
my $self = shift; |
59
|
4
|
100
|
|
|
|
11
|
if (@_) { |
60
|
2
|
|
|
|
|
6
|
$self->{raw_body} = shift; |
61
|
|
|
|
|
|
|
}else{ |
62
|
2
|
|
|
|
|
14
|
return $self->{raw_body}; |
63
|
|
|
|
|
|
|
} |
64
|
|
|
|
|
|
|
} |
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
sub headers { |
67
|
93
|
|
|
93
|
1
|
154
|
my $self = shift; |
68
|
93
|
100
|
|
|
|
344
|
if (@_) { |
69
|
41
|
|
|
|
|
73
|
my $headers = shift; |
70
|
41
|
100
|
|
|
|
173
|
if ( ref $headers eq 'ARRAY' ) { |
|
|
50
|
|
|
|
|
|
71
|
40
|
|
|
|
|
179
|
$headers = HTTP::Headers->new(@$headers); |
72
|
|
|
|
|
|
|
} |
73
|
|
|
|
|
|
|
elsif ( ref $headers eq 'HASH' ) { |
74
|
1
|
|
|
|
|
5
|
$headers = HTTP::Headers->new(%$headers); |
75
|
|
|
|
|
|
|
} |
76
|
41
|
|
|
|
|
1630
|
$self->{headers} = $headers; |
77
|
|
|
|
|
|
|
} |
78
|
|
|
|
|
|
|
else { |
79
|
52
|
|
33
|
|
|
336
|
return $self->{headers} ||= HTTP::Headers->new(); |
80
|
|
|
|
|
|
|
} |
81
|
|
|
|
|
|
|
} |
82
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
sub request { |
84
|
43
|
|
|
43
|
1
|
2610
|
my $self = shift; |
85
|
43
|
100
|
|
|
|
136
|
if (@_) { |
86
|
24
|
|
|
|
|
81
|
$self->{request} = shift; |
87
|
|
|
|
|
|
|
}else{ |
88
|
19
|
|
|
|
|
515
|
return $self->{request}; |
89
|
|
|
|
|
|
|
} |
90
|
|
|
|
|
|
|
} |
91
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
sub header { |
93
|
11
|
|
|
11
|
1
|
86
|
my $self = shift; |
94
|
11
|
|
|
|
|
44
|
$self->headers->header(@_); |
95
|
|
|
|
|
|
|
} |
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
sub to_string { |
98
|
26
|
|
|
26
|
0
|
488
|
my $self = shift; |
99
|
26
|
|
|
|
|
108
|
my $status = "HTTP status: ".$self->{status}; |
100
|
26
|
100
|
|
|
|
110
|
if ($self->{body} =~ /read timeout/){ |
101
|
1
|
|
|
|
|
19
|
$status .= " - read timeout"; |
102
|
|
|
|
|
|
|
} |
103
|
26
|
|
|
|
|
155
|
return $status; |
104
|
|
|
|
|
|
|
} |
105
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
sub finalize { |
107
|
20
|
|
|
20
|
0
|
392
|
my $self = shift; |
108
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
return [ |
110
|
|
|
|
|
|
|
$self->status, |
111
|
|
|
|
|
|
|
+[ |
112
|
|
|
|
|
|
|
map { |
113
|
20
|
|
|
|
|
56
|
my $k = $_; |
|
15
|
|
|
|
|
270
|
|
114
|
15
|
|
|
|
|
42
|
map { ( $k => $_ ) } $self->headers->header($_); |
|
15
|
|
|
|
|
587
|
|
115
|
|
|
|
|
|
|
} $self->headers->header_field_names |
116
|
|
|
|
|
|
|
], |
117
|
|
|
|
|
|
|
$self->body, |
118
|
|
|
|
|
|
|
]; |
119
|
|
|
|
|
|
|
} |
120
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
1; |
122
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
__END__ |
124
|
|
|
|
|
|
|
|
125
|
|
|
|
|
|
|
=pod |
126
|
|
|
|
|
|
|
|
127
|
|
|
|
|
|
|
=encoding UTF-8 |
128
|
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
=head1 NAME |
130
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
Net::HTTP::Spore::Response - Portable HTTP Response object for SPORE response |
132
|
|
|
|
|
|
|
|
133
|
|
|
|
|
|
|
=head1 VERSION |
134
|
|
|
|
|
|
|
|
135
|
|
|
|
|
|
|
version 0.09 |
136
|
|
|
|
|
|
|
|
137
|
|
|
|
|
|
|
=head1 SYNOPSIS |
138
|
|
|
|
|
|
|
|
139
|
|
|
|
|
|
|
use Net:HTTP::Spore::Response; |
140
|
|
|
|
|
|
|
|
141
|
|
|
|
|
|
|
my $response = Net::HTTP::Spore::Response->new( |
142
|
|
|
|
|
|
|
200, ['Content-Type', 'application/json'], '{"foo":1}'; |
143
|
|
|
|
|
|
|
); |
144
|
|
|
|
|
|
|
$response->request($request); |
145
|
|
|
|
|
|
|
|
146
|
|
|
|
|
|
|
=head1 DESCRIPTION |
147
|
|
|
|
|
|
|
|
148
|
|
|
|
|
|
|
Net::HTTP::Spore::Response create a HTTP response |
149
|
|
|
|
|
|
|
|
150
|
|
|
|
|
|
|
=head1 METHODS |
151
|
|
|
|
|
|
|
|
152
|
|
|
|
|
|
|
=over 4 |
153
|
|
|
|
|
|
|
|
154
|
|
|
|
|
|
|
=item new |
155
|
|
|
|
|
|
|
|
156
|
|
|
|
|
|
|
my $res = Net::HTTP::Spore::Response->new; |
157
|
|
|
|
|
|
|
my $res = Net::HTTP::Spore::Response->new($status); |
158
|
|
|
|
|
|
|
my $res = Net::HTTP::Spore::Response->new($status, $headers); |
159
|
|
|
|
|
|
|
my $res = Net::HTTP::Spore::Response->new($status, $headers, $body); |
160
|
|
|
|
|
|
|
|
161
|
|
|
|
|
|
|
Creates a new Net::HTTP::Spore::Response object. |
162
|
|
|
|
|
|
|
|
163
|
|
|
|
|
|
|
=item code |
164
|
|
|
|
|
|
|
|
165
|
|
|
|
|
|
|
=item status |
166
|
|
|
|
|
|
|
|
167
|
|
|
|
|
|
|
$res->status(200); |
168
|
|
|
|
|
|
|
my $status = $res->status; |
169
|
|
|
|
|
|
|
|
170
|
|
|
|
|
|
|
Gets or sets the HTTP status of the response |
171
|
|
|
|
|
|
|
|
172
|
|
|
|
|
|
|
=item env |
173
|
|
|
|
|
|
|
|
174
|
|
|
|
|
|
|
$res->env($env); |
175
|
|
|
|
|
|
|
my $env = $res->env; |
176
|
|
|
|
|
|
|
|
177
|
|
|
|
|
|
|
Gets or sets the environment for the response. Shortcut to C<< $res->request->env >> |
178
|
|
|
|
|
|
|
|
179
|
|
|
|
|
|
|
=item content |
180
|
|
|
|
|
|
|
|
181
|
|
|
|
|
|
|
=item body |
182
|
|
|
|
|
|
|
|
183
|
|
|
|
|
|
|
$res->body($body); |
184
|
|
|
|
|
|
|
my $body = $res->body; |
185
|
|
|
|
|
|
|
|
186
|
|
|
|
|
|
|
Gets or sets the body for the response |
187
|
|
|
|
|
|
|
|
188
|
|
|
|
|
|
|
=item raw_body |
189
|
|
|
|
|
|
|
|
190
|
|
|
|
|
|
|
my $raw_body = $res->raw_body |
191
|
|
|
|
|
|
|
|
192
|
|
|
|
|
|
|
The raw_body value is the same as body when the body is sets for the first time. |
193
|
|
|
|
|
|
|
|
194
|
|
|
|
|
|
|
=item content_type |
195
|
|
|
|
|
|
|
|
196
|
|
|
|
|
|
|
$res->content_type('application/json'); |
197
|
|
|
|
|
|
|
my $ct = $res->content_type; |
198
|
|
|
|
|
|
|
|
199
|
|
|
|
|
|
|
Gets or sets the content type of the response body |
200
|
|
|
|
|
|
|
|
201
|
|
|
|
|
|
|
=item content_length |
202
|
|
|
|
|
|
|
|
203
|
|
|
|
|
|
|
$res->content_length(length($body)); |
204
|
|
|
|
|
|
|
my $cl = $res->content_length; |
205
|
|
|
|
|
|
|
|
206
|
|
|
|
|
|
|
Gets or sets the content type of the response body |
207
|
|
|
|
|
|
|
|
208
|
|
|
|
|
|
|
=item location |
209
|
|
|
|
|
|
|
|
210
|
|
|
|
|
|
|
$res->location('http://example.com'); |
211
|
|
|
|
|
|
|
my $location = $res->location; |
212
|
|
|
|
|
|
|
|
213
|
|
|
|
|
|
|
Gets or sets the location header for the response |
214
|
|
|
|
|
|
|
|
215
|
|
|
|
|
|
|
=item request |
216
|
|
|
|
|
|
|
|
217
|
|
|
|
|
|
|
$res->request($request); |
218
|
|
|
|
|
|
|
$request = $res->request; |
219
|
|
|
|
|
|
|
|
220
|
|
|
|
|
|
|
Gets or sets the HTTP request that created the current HTTP response. |
221
|
|
|
|
|
|
|
|
222
|
|
|
|
|
|
|
=item headers |
223
|
|
|
|
|
|
|
|
224
|
|
|
|
|
|
|
$headers = $res->headers; |
225
|
|
|
|
|
|
|
$res->headers(['Content-Type' => 'application/json']); |
226
|
|
|
|
|
|
|
|
227
|
|
|
|
|
|
|
Gets or sets HTTP response headers. |
228
|
|
|
|
|
|
|
|
229
|
|
|
|
|
|
|
=item header |
230
|
|
|
|
|
|
|
|
231
|
|
|
|
|
|
|
my $cl = $res->header('Content-Length'); |
232
|
|
|
|
|
|
|
$res->header('Content-Type' => 'application/json'); |
233
|
|
|
|
|
|
|
|
234
|
|
|
|
|
|
|
Shortcut for C<< $res->headers->header >>. |
235
|
|
|
|
|
|
|
|
236
|
|
|
|
|
|
|
=item finalise |
237
|
|
|
|
|
|
|
|
238
|
|
|
|
|
|
|
my $res = Net::HTTP::Response->new($status, $headers, $body); |
239
|
|
|
|
|
|
|
say "http status is ".$res->[0]; |
240
|
|
|
|
|
|
|
|
241
|
|
|
|
|
|
|
Return an arrayref: |
242
|
|
|
|
|
|
|
|
243
|
|
|
|
|
|
|
=over 2 |
244
|
|
|
|
|
|
|
|
245
|
|
|
|
|
|
|
=item status |
246
|
|
|
|
|
|
|
|
247
|
|
|
|
|
|
|
The first element of the array ref is the HTTP status |
248
|
|
|
|
|
|
|
|
249
|
|
|
|
|
|
|
=item headers |
250
|
|
|
|
|
|
|
|
251
|
|
|
|
|
|
|
The second element is an arrayref containing the list of HTTP headers |
252
|
|
|
|
|
|
|
|
253
|
|
|
|
|
|
|
=item body |
254
|
|
|
|
|
|
|
|
255
|
|
|
|
|
|
|
The third and final element is the body |
256
|
|
|
|
|
|
|
|
257
|
|
|
|
|
|
|
=back |
258
|
|
|
|
|
|
|
|
259
|
|
|
|
|
|
|
=back |
260
|
|
|
|
|
|
|
|
261
|
|
|
|
|
|
|
=head1 AUTHORS |
262
|
|
|
|
|
|
|
|
263
|
|
|
|
|
|
|
=over 4 |
264
|
|
|
|
|
|
|
|
265
|
|
|
|
|
|
|
=item * |
266
|
|
|
|
|
|
|
|
267
|
|
|
|
|
|
|
Franck Cuny <franck.cuny@gmail.com> |
268
|
|
|
|
|
|
|
|
269
|
|
|
|
|
|
|
=item * |
270
|
|
|
|
|
|
|
|
271
|
|
|
|
|
|
|
Ash Berlin <ash@cpan.org> |
272
|
|
|
|
|
|
|
|
273
|
|
|
|
|
|
|
=item * |
274
|
|
|
|
|
|
|
|
275
|
|
|
|
|
|
|
Ahmad Fatoum <athreef@cpan.org> |
276
|
|
|
|
|
|
|
|
277
|
|
|
|
|
|
|
=back |
278
|
|
|
|
|
|
|
|
279
|
|
|
|
|
|
|
=head1 COPYRIGHT AND LICENSE |
280
|
|
|
|
|
|
|
|
281
|
|
|
|
|
|
|
This software is copyright (c) 2012 by Linkfluence. |
282
|
|
|
|
|
|
|
|
283
|
|
|
|
|
|
|
This is free software; you can redistribute it and/or modify it under |
284
|
|
|
|
|
|
|
the same terms as the Perl 5 programming language system itself. |
285
|
|
|
|
|
|
|
|
286
|
|
|
|
|
|
|
=cut |