/usr/share/perl5/Furl/Response.pm is in libfurl-perl 3.08-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 | package Furl::Response;
use strict;
use warnings;
use utf8;
use Furl::Headers;
sub new {
my ($class, $minor_version, $code, $message, $headers, $content) = @_;
bless {
minor_version => $minor_version,
code => $code,
message => $message,
headers => Furl::Headers->new($headers),
content => $content,
}, $class;
}
# DO NOT CALL this method DIRECTLY.
sub set_request_info {
my ($self, $request_src, $captured_req_headers, $captured_req_content) = @_;
$self->{request_src} = $request_src;
if (defined $captured_req_headers) {
$self->{captured_req_headers} = $captured_req_headers;
$self->{captured_req_content} = $captured_req_content;
} else {
$self->{captured_req_headers} = undef;
$self->{captured_req_content} = undef;
}
return;
}
sub captured_req_headers {
my $self = shift;
unless (exists $self->{captured_req_headers}) {
Carp::croak("You can't call cpatured_req_headers method without 'capture_request' options for Furl#new");
}
return $self->{captured_req_headers};
}
sub captured_req_content {
my $self = shift;
unless (exists $self->{captured_req_content}) {
Carp::croak("You can't call cpatured_req_content method without 'capture_request' options for Furl#new");
}
return $self->{captured_req_content};
}
# accessors
sub code { shift->{code} }
sub message { shift->{message} }
sub headers { shift->{headers} }
sub content { shift->{content} }
sub request {
my $self = shift;
if (!exists $self->{request}) {
if (!exists $self->{request_src}) {
Carp::croak("This request object does not have a request information");
}
# my ($method, $uri, $headers, $content) = @_;
$self->{request} = Furl::Request->new(
$self->{request_src}->{method},
$self->{request_src}->{url},
$self->{request_src}->{headers},
$self->{request_src}->{content},
);
}
return $self->{request};
}
# alias
sub status { shift->code() }
sub body { shift->content() }
# shorthand
sub content_length { shift->headers->content_length() }
sub content_type { shift->headers->content_type() }
sub content_encoding { shift->headers->content_encoding() }
sub header { shift->headers->header(@_) }
sub protocol { "HTTP/1." . $_[0]->{minor_version} }
sub decoded_content {
my $self = shift;
my $cloned = $self->headers->clone;
# 'HTTP::Message::decoded_content' tries to decompress content
# if response header contains 'Content-Encoding' field.
# However 'Furl' decompresses content by itself, 'Content-Encoding' field
# whose value is supported encoding type should be removed from response header.
my @removed = grep { ! m{\b(?:gzip|x-gzip|deflate)\b} } $cloned->header('content-encoding');
$cloned->header('content-encoding', \@removed);
$self->_as_http_response_internal([ $cloned->flatten ])->decoded_content(@_);
}
sub as_http_response {
my ($self) = @_;
return $self->_as_http_response_internal([ $self->headers->flatten ])
}
sub _as_http_response_internal {
my ($self, $flatten_headers) = @_;
require HTTP::Response;
my $res = HTTP::Response->new( $self->code, $self->message,
$flatten_headers,
$self->content );
$res->protocol($self->protocol);
if ($self->{request_src} || $self->{request}) {
if (my $req = $self->request) {
$res->request($req->as_http_request);
}
}
return $res;
}
sub to_psgi {
my ($self) = @_;
return [
$self->code,
[$self->headers->flatten],
[$self->content]
];
}
sub as_string {
my ($self) = @_;
return join("",
$self->status_line . "\015\012",
$self->headers->as_string,
"\015\012",
$self->content,
);
}
sub as_hashref {
my $self = shift;
return +{
code => $self->code,
message => $self->message,
protocol => $self->protocol,
headers => [$self->headers->flatten],
content => $self->content,
};
}
sub is_success { substr( $_[0]->code, 0, 1 ) eq '2' }
sub status_line { $_[0]->code . ' ' . $_[0]->message }
sub charset {
my $self = shift;
return $self->{__charset} if exists $self->{__charset};
if ($self->can('content_charset')){
# To suppress:
# Parsing of undecoded UTF-8 will give garbage when decoding entities
local $SIG{__WARN__} = sub {};
my $charset = $self->content_charset;
$self->{__charset} = $charset;
return $charset;
}
my $content_type = $self->headers->header('Content-Type');
return unless $content_type;
$content_type =~ /charset=([A-Za-z0-9_\-]+)/io;
$self->{__charset} = $1 || undef;
# Detect charset from HTML
unless (defined($self->{__charset}) && $self->content_type =~ m{text/html}) {
# I guess, this is not so perfect regexp. patches welcome.
#
# <meta http-equiv="Content-Type" content="text/html; charset=EUC-JP"/>
$self->content =~ m!<meta\s+http-equiv\s*=["']Content-Type["']\s+content\s*=\s*["']text/html;\s*charset=([^'">/]+)['"]\s*/?>!smi;
$self->{__charset} = $1;
}
$self->{__charset};
}
sub encoder {
require Encode;
my $self = shift;
return $self->{__encoder} if exists $self->{__encoder};
my $charset = $self->charset or return;
my $enc = Encode::find_encoding($charset);
$self->{__encoder} = $enc;
}
sub encoding {
my $enc = shift->encoder or return;
$enc->name;
}
1;
__END__
=encoding utf-8
=for stopwords charsets
=head1 NAME
Furl::Response - Response object for Furl
=head1 SYNOPSIS
my $res = Furl::Response->new($minor_version, $code, $message, $headers, $content);
print $res->status, "\n";
=head1 DESCRIPTION
This is a HTTP response object in Furl.
=head1 CONSTRUCTOR
my $res = Furl::Response->new($minor_version, $code, $msg, \%headers, $content);
=head1 INSTANCE METHODS
=over 4
=item $res->code
=item $res->status
Returns HTTP status code.
=item $res->message
Returns HTTP status message.
=item $res->headers
Returns instance of L<Furl::Headers>
=item $res->content
=item $res->body
Returns response body in scalar.
=item $res->decoded_content
This will return the content after any C<< Content-Encoding >> and charsets have been decoded. See L<< HTTP::Message >> for details
=item $res->request
Returns instance of L<Furl::Request> related this response.
=item $res->content_length
=item $res->content_type
=item $res->content_encoding
=item $res->header
Shorthand to access L<Furl::Headers>.
=item $res->protocol
$res->protocol(); # => "HTTP/1.1"
Returns HTTP protocol in string.
=item $res->as_http_response
Make instance of L<HTTP::Response> from L<Furl::Response>.
=item $res->to_psgi()
Convert object to L<PSGI> response. It's very useful to make proxy.
=item $res->as_hashref()
Convert response object to HashRef.
Format is following:
code: Int
message: Str
protocol: Str
headers: ArrayRef[Str]
content: Str
=item $res->is_success
Returns true if status code is 2xx.
=item $res->status_line
$res->status_line() # => "200 OK"
Returns status line.
=item my $headers = $res->captured_req_headers() : Str
Captured request headers in raw string.
This method is only for debugging.
You can use this method if you are using C<< capture_request >> parameter is true.
=item my $content = $res->captured_req_content() : Str
Captured request content in raw string.
This method is only for debugging.
You can use this method if you are using C<< capture_request >> parameter is true.
=back
|