| 1 |
package HTTP::Headers; |
| 2 |
|
| 3 |
use strict; |
| 4 |
use warnings; |
| 5 |
|
| 6 |
our $VERSION = '7.02'; |
| 7 |
|
| 8 |
use Clone qw(clone); |
| 9 |
use Carp (); |
| 10 |
|
| 11 |
# The $TRANSLATE_UNDERSCORE variable controls whether '_' can be used |
| 12 |
# as a replacement for '-' in header field names. |
| 13 |
our $TRANSLATE_UNDERSCORE = 1 unless defined $TRANSLATE_UNDERSCORE; |
| 14 |
|
| 15 |
# "Good Practice" order of HTTP message headers: |
| 16 |
# - General-Headers |
| 17 |
# - Request-Headers |
| 18 |
# - Response-Headers |
| 19 |
# - Entity-Headers |
| 20 |
|
| 21 |
my @general_headers = qw( |
| 22 |
Cache-Control Connection Date Pragma Trailer Transfer-Encoding Upgrade |
| 23 |
Via Warning |
| 24 |
); |
| 25 |
|
| 26 |
my @request_headers = qw( |
| 27 |
Accept Accept-Charset Accept-Encoding Accept-Language |
| 28 |
Authorization Expect From Host |
| 29 |
If-Match If-Modified-Since If-None-Match If-Range If-Unmodified-Since |
| 30 |
Max-Forwards Proxy-Authorization Range Referer TE User-Agent |
| 31 |
); |
| 32 |
|
| 33 |
my @response_headers = qw( |
| 34 |
Accept-Ranges Age ETag Location Proxy-Authenticate Retry-After Server |
| 35 |
Vary WWW-Authenticate |
| 36 |
); |
| 37 |
|
| 38 |
my @entity_headers = qw( |
| 39 |
Allow Content-Encoding Content-Language Content-Length Content-Location |
| 40 |
Content-MD5 Content-Range Content-Type Expires Last-Modified |
| 41 |
); |
| 42 |
|
| 43 |
my %entity_header = map { lc($_) => 1 } @entity_headers; |
| 44 |
|
| 45 |
my @header_order = ( |
| 46 |
@general_headers, |
| 47 |
@request_headers, |
| 48 |
@response_headers, |
| 49 |
@entity_headers, |
| 50 |
); |
| 51 |
|
| 52 |
# Make alternative representations of @header_order. This is used |
| 53 |
# for sorting and case matching. |
| 54 |
my %header_order; |
| 55 |
my %standard_case; |
| 56 |
|
| 57 |
{ |
| 58 |
my $i = 0; |
| 59 |
for (@header_order) { |
| 60 |
my $lc = lc $_; |
| 61 |
$header_order{$lc} = ++$i; |
| 62 |
$standard_case{$lc} = $_; |
| 63 |
} |
| 64 |
} |
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
sub new |
| 69 |
{ |
| 70 |
my($class) = shift; |
| 71 |
my $self = bless {}, $class; |
| 72 |
$self->header(@_) if @_; # set up initial headers |
| 73 |
$self; |
| 74 |
} |
| 75 |
|
| 76 |
|
| 77 |
sub header |
| 78 |
{ |
| 79 |
my $self = shift; |
| 80 |
Carp::croak('Usage: $h->header($field, ...)') unless @_; |
| 81 |
my(@old); |
| 82 |
my %seen; |
| 83 |
while (@_) { |
| 84 |
my $field = shift; |
| 85 |
my $op = @_ ? ($seen{lc($field)}++ ? 'PUSH' : 'SET') : 'GET'; |
| 86 |
@old = $self->_header($field, shift, $op); |
| 87 |
} |
| 88 |
return @old if wantarray; |
| 89 |
return $old[0] if @old <= 1; |
| 90 |
join(", ", @old); |
| 91 |
} |
| 92 |
|
| 93 |
sub clear |
| 94 |
{ |
| 95 |
my $self = shift; |
| 96 |
%$self = (); |
| 97 |
} |
| 98 |
|
| 99 |
|
| 100 |
sub push_header |
| 101 |
{ |
| 102 |
my $self = shift; |
| 103 |
return $self->_header(@_, 'PUSH_H') if @_ == 2; |
| 104 |
while (@_) { |
| 105 |
$self->_header(splice(@_, 0, 2), 'PUSH_H'); |
| 106 |
} |
| 107 |
} |
| 108 |
|
| 109 |
|
| 110 |
sub init_header |
| 111 |
{ |
| 112 |
Carp::croak('Usage: $h->init_header($field, $val)') if @_ != 3; |
| 113 |
shift->_header(@_, 'INIT'); |
| 114 |
} |
| 115 |
|
| 116 |
|
| 117 |
sub remove_header |
| 118 |
{ |
| 119 |
my($self, @fields) = @_; |
| 120 |
my $field; |
| 121 |
my @values; |
| 122 |
foreach $field (@fields) { |
| 123 |
$field =~ tr/_/-/ if $field !~ /^:/ && $TRANSLATE_UNDERSCORE; |
| 124 |
my $v = delete $self->{lc $field}; |
| 125 |
push(@values, ref($v) eq 'ARRAY' ? @$v : $v) if defined $v; |
| 126 |
} |
| 127 |
return @values; |
| 128 |
} |
| 129 |
|
| 130 |
sub remove_content_headers |
| 131 |
{ |
| 132 |
my $self = shift; |
| 133 |
unless (defined(wantarray)) { |
| 134 |
# fast branch that does not create return object |
| 135 |
delete @$self{grep $entity_header{$_} || /^content-/, keys %$self}; |
| 136 |
return; |
| 137 |
} |
| 138 |
|
| 139 |
my $c = ref($self)->new; |
| 140 |
for my $f (grep $entity_header{$_} || /^content-/, keys %$self) { |
| 141 |
$c->{$f} = delete $self->{$f}; |
| 142 |
} |
| 143 |
if (exists $self->{'::std_case'}) { |
| 144 |
$c->{'::std_case'} = $self->{'::std_case'}; |
| 145 |
} |
| 146 |
$c; |
| 147 |
} |
| 148 |
|
| 149 |
|
| 150 |
sub _header |
| 151 |
{ |
| 152 |
my($self, $field, $val, $op) = @_; |
| 153 |
|
| 154 |
Carp::croak("Illegal field name '$field'") |
| 155 |
if rindex($field, ':') > 1 || !length($field); |
| 156 |
|
| 157 |
unless ($field =~ /^:/) { |
| 158 |
$field =~ tr/_/-/ if $TRANSLATE_UNDERSCORE; |
| 159 |
my $old = $field; |
| 160 |
$field = lc $field; |
| 161 |
unless($standard_case{$field} || $self->{'::std_case'}{$field}) { |
| 162 |
# generate a %std_case entry for this field |
| 163 |
$old =~ s/\b(\w)/\u$1/g; |
| 164 |
$self->{'::std_case'}{$field} = $old; |
| 165 |
} |
| 166 |
} |
| 167 |
|
| 168 |
$op ||= defined($val) ? 'SET' : 'GET'; |
| 169 |
if ($op eq 'PUSH_H') { |
| 170 |
# Like PUSH but where we don't care about the return value |
| 171 |
if (exists $self->{$field}) { |
| 172 |
my $h = $self->{$field}; |
| 173 |
if (ref($h) eq 'ARRAY') { |
| 174 |
push(@$h, ref($val) eq "ARRAY" ? @$val : $val); |
| 175 |
} |
| 176 |
else { |
| 177 |
$self->{$field} = [$h, ref($val) eq "ARRAY" ? @$val : $val] |
| 178 |
} |
| 179 |
return; |
| 180 |
} |
| 181 |
$self->{$field} = $val; |
| 182 |
return; |
| 183 |
} |
| 184 |
|
| 185 |
my $h = $self->{$field}; |
| 186 |
my @old = ref($h) eq 'ARRAY' ? @$h : (defined($h) ? ($h) : ()); |
| 187 |
|
| 188 |
unless ($op eq 'GET' || ($op eq 'INIT' && @old)) { |
| 189 |
if (defined($val)) { |
| 190 |
my @new = ($op eq 'PUSH') ? @old : (); |
| 191 |
if (ref($val) ne 'ARRAY') { |
| 192 |
push(@new, $val); |
| 193 |
} |
| 194 |
else { |
| 195 |
push(@new, @$val); |
| 196 |
} |
| 197 |
$self->{$field} = @new > 1 ? \@new : $new[0]; |
| 198 |
} |
| 199 |
elsif ($op ne 'PUSH') { |
| 200 |
delete $self->{$field}; |
| 201 |
} |
| 202 |
} |
| 203 |
@old; |
| 204 |
} |
| 205 |
|
| 206 |
|
| 207 |
sub _sorted_field_names |
| 208 |
{ |
| 209 |
my $self = shift; |
| 210 |
return [ sort { |
| 211 |
($header_order{$a} || 999) <=> ($header_order{$b} || 999) || |
| 212 |
$a cmp $b |
| 213 |
} grep !/^::/, keys %$self ]; |
| 214 |
} |
| 215 |
|
| 216 |
|
| 217 |
sub header_field_names { |
| 218 |
my $self = shift; |
| 219 |
return map $standard_case{$_} || $self->{'::std_case'}{$_} || $_, @{ $self->_sorted_field_names }, |
| 220 |
if wantarray; |
| 221 |
return grep !/^::/, keys %$self; |
| 222 |
} |
| 223 |
|
| 224 |
|
| 225 |
sub scan |
| 226 |
{ |
| 227 |
my($self, $sub) = @_; |
| 228 |
my $key; |
| 229 |
for $key (@{ $self->_sorted_field_names }) { |
| 230 |
my $vals = $self->{$key}; |
| 231 |
if (ref($vals) eq 'ARRAY') { |
| 232 |
my $val; |
| 233 |
for $val (@$vals) { |
| 234 |
$sub->($standard_case{$key} || $self->{'::std_case'}{$key} || $key, $val); |
| 235 |
} |
| 236 |
} |
| 237 |
else { |
| 238 |
$sub->($standard_case{$key} || $self->{'::std_case'}{$key} || $key, $vals); |
| 239 |
} |
| 240 |
} |
| 241 |
} |
| 242 |
|
| 243 |
sub flatten { |
| 244 |
my($self)=@_; |
| 245 |
|
| 246 |
( |
| 247 |
map { |
| 248 |
my $k = $_; |
| 249 |
map { |
| 250 |
( $k => $_ ) |
| 251 |
} $self->header($_); |
| 252 |
} $self->header_field_names |
| 253 |
); |
| 254 |
} |
| 255 |
|
| 256 |
sub as_string |
| 257 |
{ |
| 258 |
my($self, $endl) = @_; |
| 259 |
$endl = "\n" unless defined $endl; |
| 260 |
|
| 261 |
my @result = (); |
| 262 |
for my $key (@{ $self->_sorted_field_names }) { |
| 263 |
next if index($key, '_') == 0; |
| 264 |
my $vals = $self->{$key}; |
| 265 |
if ( ref($vals) eq 'ARRAY' ) { |
| 266 |
for my $val (@$vals) { |
| 267 |
$val = '' if not defined $val; |
| 268 |
my $field = $standard_case{$key} || $self->{'::std_case'}{$key} || $key; |
| 269 |
$field =~ s/^://; |
| 270 |
if ( index($val, "\n") >= 0 ) { |
| 271 |
$val = _process_newline($val, $endl); |
| 272 |
} |
| 273 |
push @result, $field . ': ' . $val; |
| 274 |
} |
| 275 |
} |
| 276 |
else { |
| 277 |
$vals = '' if not defined $vals; |
| 278 |
my $field = $standard_case{$key} || $self->{'::std_case'}{$key} || $key; |
| 279 |
$field =~ s/^://; |
| 280 |
if ( index($vals, "\n") >= 0 ) { |
| 281 |
$vals = _process_newline($vals, $endl); |
| 282 |
} |
| 283 |
push @result, $field . ': ' . $vals; |
| 284 |
} |
| 285 |
} |
| 286 |
|
| 287 |
join($endl, @result, ''); |
| 288 |
} |
| 289 |
|
| 290 |
sub _process_newline { |
| 291 |
local $_ = shift; |
| 292 |
my $endl = shift; |
| 293 |
# must handle header values with embedded newlines with care |
| 294 |
s/\s+$//; # trailing newlines and space must go |
| 295 |
s/\n(\x0d?\n)+/\n/g; # no empty lines |
| 296 |
s/\n([^\040\t])/\n $1/g; # initial space for continuation |
| 297 |
s/\n/$endl/g; # substitute with requested line ending |
| 298 |
$_; |
| 299 |
} |
| 300 |
|
| 301 |
|
| 302 |
sub _date_header |
| 303 |
{ |
| 304 |
require HTTP::Date; |
| 305 |
my($self, $header, $time) = @_; |
| 306 |
my($old) = $self->_header($header); |
| 307 |
if (defined $time) { |
| 308 |
$self->_header($header, HTTP::Date::time2str($time)); |
| 309 |
} |
| 310 |
$old =~ s/;.*// if defined($old); |
| 311 |
HTTP::Date::str2time($old); |
| 312 |
} |
| 313 |
|
| 314 |
|
| 315 |
sub date { shift->_date_header('Date', @_); } |
| 316 |
sub expires { shift->_date_header('Expires', @_); } |
| 317 |
sub if_modified_since { shift->_date_header('If-Modified-Since', @_); } |
| 318 |
sub if_unmodified_since { shift->_date_header('If-Unmodified-Since', @_); } |
| 319 |
sub last_modified { shift->_date_header('Last-Modified', @_); } |
| 320 |
|
| 321 |
# This is used as a private LWP extension. The Client-Date header is |
| 322 |
# added as a timestamp to a response when it has been received. |
| 323 |
sub client_date { shift->_date_header('Client-Date', @_); } |
| 324 |
|
| 325 |
# The retry_after field is dual format (can also be a expressed as |
| 326 |
# number of seconds from now), so we don't provide an easy way to |
| 327 |
# access it until we have know how both these interfaces can be |
| 328 |
# addressed. One possibility is to return a negative value for |
| 329 |
# relative seconds and a positive value for epoch based time values. |
| 330 |
#sub retry_after { shift->_date_header('Retry-After', @_); } |
| 331 |
|
| 332 |
sub content_type { |
| 333 |
my $self = shift; |
| 334 |
my $ct = $self->{'content-type'}; |
| 335 |
$self->{'content-type'} = shift if @_; |
| 336 |
$ct = $ct->[0] if ref($ct) eq 'ARRAY'; |
| 337 |
return '' unless defined($ct) && length($ct); |
| 338 |
my @ct = split(/;\s*/, $ct, 2); |
| 339 |
for ($ct[0]) { |
| 340 |
s/\s+//g; |
| 341 |
$_ = lc($_); |
| 342 |
} |
| 343 |
wantarray ? @ct : $ct[0]; |
| 344 |
} |
| 345 |
|
| 346 |
sub content_type_charset { |
| 347 |
my $self = shift; |
| 348 |
require HTTP::Headers::Util; |
| 349 |
my $h = $self->{'content-type'}; |
| 350 |
$h = $h->[0] if ref($h); |
| 351 |
$h = "" unless defined $h; |
| 352 |
my @v = HTTP::Headers::Util::split_header_words($h); |
| 353 |
if (@v) { |
| 354 |
my($ct, undef, %ct_param) = @{$v[0]}; |
| 355 |
my $charset = $ct_param{charset}; |
| 356 |
if ($ct) { |
| 357 |
$ct = lc($ct); |
| 358 |
$ct =~ s/\s+//; |
| 359 |
} |
| 360 |
if ($charset) { |
| 361 |
$charset = uc($charset); |
| 362 |
$charset =~ s/^\s+//; $charset =~ s/\s+\z//; |
| 363 |
undef($charset) if $charset eq ""; |
| 364 |
} |
| 365 |
return $ct, $charset if wantarray; |
| 366 |
return $charset; |
| 367 |
} |
| 368 |
return undef, undef if wantarray; |
| 369 |
return undef; |
| 370 |
} |
| 371 |
|
| 372 |
sub content_is_text { |
| 373 |
my $self = shift; |
| 374 |
return $self->content_type =~ m,^text/,; |
| 375 |
} |
| 376 |
|
| 377 |
sub content_is_html { |
| 378 |
my $self = shift; |
| 379 |
return $self->content_type eq 'text/html' || $self->content_is_xhtml; |
| 380 |
} |
| 381 |
|
| 382 |
sub content_is_xhtml { |
| 383 |
my $ct = shift->content_type; |
| 384 |
return $ct eq "application/xhtml+xml" || |
| 385 |
$ct eq "application/vnd.wap.xhtml+xml"; |
| 386 |
} |
| 387 |
|
| 388 |
sub content_is_xml { |
| 389 |
my $ct = shift->content_type; |
| 390 |
return 1 if $ct eq "text/xml"; |
| 391 |
return 1 if $ct eq "application/xml"; |
| 392 |
return 1 if $ct =~ /\+xml$/; |
| 393 |
return 0; |
| 394 |
} |
| 395 |
|
| 396 |
sub referer { |
| 397 |
my $self = shift; |
| 398 |
if (@_ && $_[0] =~ /#/) { |
| 399 |
# Strip fragment per RFC 2616, section 14.36. |
| 400 |
my $uri = shift; |
| 401 |
if (ref($uri)) { |
| 402 |
$uri = $uri->clone; |
| 403 |
$uri->fragment(undef); |
| 404 |
} |
| 405 |
else { |
| 406 |
$uri =~ s/\#.*//; |
| 407 |
} |
| 408 |
unshift @_, $uri; |
| 409 |
} |
| 410 |
($self->_header('Referer', @_))[0]; |
| 411 |
} |
| 412 |
*referrer = \&referer; # on tchrist's request |
| 413 |
|
| 414 |
sub title { (shift->_header('Title', @_))[0] } |
| 415 |
sub content_encoding { (shift->_header('Content-Encoding', @_))[0] } |
| 416 |
sub content_language { (shift->_header('Content-Language', @_))[0] } |
| 417 |
sub content_length { (shift->_header('Content-Length', @_))[0] } |
| 418 |
|
| 419 |
sub user_agent { (shift->_header('User-Agent', @_))[0] } |
| 420 |
sub server { (shift->_header('Server', @_))[0] } |
| 421 |
|
| 422 |
sub from { (shift->_header('From', @_))[0] } |
| 423 |
sub warning { (shift->_header('Warning', @_))[0] } |
| 424 |
|
| 425 |
sub www_authenticate { (shift->_header('WWW-Authenticate', @_))[0] } |
| 426 |
sub authorization { (shift->_header('Authorization', @_))[0] } |
| 427 |
|
| 428 |
sub proxy_authenticate { (shift->_header('Proxy-Authenticate', @_))[0] } |
| 429 |
sub proxy_authorization { (shift->_header('Proxy-Authorization', @_))[0] } |
| 430 |
|
| 431 |
sub authorization_basic { shift->_basic_auth("Authorization", @_) } |
| 432 |
sub proxy_authorization_basic { shift->_basic_auth("Proxy-Authorization", @_) } |
| 433 |
|
| 434 |
sub _basic_auth { |
| 435 |
require MIME::Base64; |
| 436 |
my($self, $h, $user, $passwd) = @_; |
| 437 |
my($old) = $self->_header($h); |
| 438 |
if (defined $user) { |
| 439 |
Carp::croak("Basic authorization user name can't contain ':'") |
| 440 |
if $user =~ /:/; |
| 441 |
$passwd = '' unless defined $passwd; |
| 442 |
$self->_header($h => 'Basic ' . |
| 443 |
MIME::Base64::encode("$user:$passwd", '')); |
| 444 |
} |
| 445 |
if (defined $old && $old =~ s/^\s*Basic\s+//) { |
| 446 |
my $val = MIME::Base64::decode($old); |
| 447 |
return $val unless wantarray; |
| 448 |
return split(/:/, $val, 2); |
| 449 |
} |
| 450 |
return; |
| 451 |
} |
| 452 |
|
| 453 |
|
| 454 |
1; |
| 455 |
|
| 456 |
=pod |
| 457 |
|
| 458 |
=encoding UTF-8 |
| 459 |
|
| 460 |
=head1 NAME |
| 461 |
|
| 462 |
HTTP::Headers - Class encapsulating HTTP Message headers |
| 463 |
|
| 464 |
=head1 VERSION |
| 465 |
|
| 466 |
version 7.02 |
| 467 |
|
| 468 |
=head1 SYNOPSIS |
| 469 |
|
| 470 |
require HTTP::Headers; |
| 471 |
$h = HTTP::Headers->new; |
| 472 |
|
| 473 |
$h->header('Content-Type' => 'text/plain'); # set |
| 474 |
$ct = $h->header('Content-Type'); # get |
| 475 |
$h->remove_header('Content-Type'); # delete |
| 476 |
|
| 477 |
=head1 DESCRIPTION |
| 478 |
|
| 479 |
The C<HTTP::Headers> class encapsulates HTTP-style message headers. |
| 480 |
The headers consist of attribute-value pairs also called fields, which |
| 481 |
may be repeated, and which are printed in a particular order. The |
| 482 |
field names are cases insensitive. |
| 483 |
|
| 484 |
Instances of this class are usually created as member variables of the |
| 485 |
C<HTTP::Request> and C<HTTP::Response> classes, internal to the |
| 486 |
library. |
| 487 |
|
| 488 |
The following methods are available: |
| 489 |
|
| 490 |
=over 4 |
| 491 |
|
| 492 |
=item $h = HTTP::Headers->new |
| 493 |
|
| 494 |
Constructs a new C<HTTP::Headers> object. You might pass some initial |
| 495 |
attribute-value pairs as parameters to the constructor. I<E.g.>: |
| 496 |
|
| 497 |
$h = HTTP::Headers->new( |
| 498 |
Date => 'Thu, 03 Feb 1994 00:00:00 GMT', |
| 499 |
Content_Type => 'text/html; version=3.2', |
| 500 |
Content_Base => 'http://www.perl.org/'); |
| 501 |
|
| 502 |
The constructor arguments are passed to the C<header> method which is |
| 503 |
described below. |
| 504 |
|
| 505 |
=item $h->clone |
| 506 |
|
| 507 |
Returns a copy of this C<HTTP::Headers> object. |
| 508 |
|
| 509 |
=item $h->header( $field ) |
| 510 |
|
| 511 |
=item $h->header( $field => $value ) |
| 512 |
|
| 513 |
=item $h->header( $f1 => $v1, $f2 => $v2, ... ) |
| 514 |
|
| 515 |
Get or set the value of one or more header fields. The header field |
| 516 |
name ($field) is not case sensitive. To make the life easier for perl |
| 517 |
users who wants to avoid quoting before the => operator, you can use |
| 518 |
'_' as a replacement for '-' in header names. |
| 519 |
|
| 520 |
The header() method accepts multiple ($field => $value) pairs, which |
| 521 |
means that you can update several fields with a single invocation. |
| 522 |
|
| 523 |
The $value argument may be a plain string or a reference to an array |
| 524 |
of strings for a multi-valued field. If the $value is provided as |
| 525 |
C<undef> then the field is removed. If the $value is not given, then |
| 526 |
that header field will remain unchanged. In addition to being a string, |
| 527 |
$value may be something that stringifies. |
| 528 |
|
| 529 |
The old value (or values) of the last of the header fields is returned. |
| 530 |
If no such field exists C<undef> will be returned. |
| 531 |
|
| 532 |
A multi-valued field will be returned as separate values in list |
| 533 |
context and will be concatenated with ", " as separator in scalar |
| 534 |
context. The HTTP spec (RFC 2616) promises that joining multiple |
| 535 |
values in this way will not change the semantic of a header field, but |
| 536 |
in practice there are cases like old-style Netscape cookies (see |
| 537 |
L<HTTP::Cookies>) where "," is used as part of the syntax of a single |
| 538 |
field value. |
| 539 |
|
| 540 |
Examples: |
| 541 |
|
| 542 |
$header->header(MIME_Version => '1.0', |
| 543 |
User_Agent => 'My-Web-Client/0.01'); |
| 544 |
$header->header(Accept => "text/html, text/plain, image/*"); |
| 545 |
$header->header(Accept => [qw(text/html text/plain image/*)]); |
| 546 |
@accepts = $header->header('Accept'); # get multiple values |
| 547 |
$accepts = $header->header('Accept'); # get values as a single string |
| 548 |
|
| 549 |
=item $h->push_header( $field => $value ) |
| 550 |
|
| 551 |
=item $h->push_header( $f1 => $v1, $f2 => $v2, ... ) |
| 552 |
|
| 553 |
Add a new field value for the specified header field. Previous values |
| 554 |
for the same field are retained. |
| 555 |
|
| 556 |
As for the header() method, the field name ($field) is not case |
| 557 |
sensitive and '_' can be used as a replacement for '-'. |
| 558 |
|
| 559 |
The $value argument may be a scalar or a reference to a list of |
| 560 |
scalars. |
| 561 |
|
| 562 |
$header->push_header(Accept => 'image/jpeg'); |
| 563 |
$header->push_header(Accept => [map "image/$_", qw(gif png tiff)]); |
| 564 |
|
| 565 |
=item $h->init_header( $field => $value ) |
| 566 |
|
| 567 |
Set the specified header to the given value, but only if no previous |
| 568 |
value for that field is set. |
| 569 |
|
| 570 |
The header field name ($field) is not case sensitive and '_' |
| 571 |
can be used as a replacement for '-'. |
| 572 |
|
| 573 |
The $value argument may be a scalar or a reference to a list of |
| 574 |
scalars. |
| 575 |
|
| 576 |
=item $h->remove_header( $field, ... ) |
| 577 |
|
| 578 |
This function removes the header fields with the specified names. |
| 579 |
|
| 580 |
The header field names ($field) are not case sensitive and '_' |
| 581 |
can be used as a replacement for '-'. |
| 582 |
|
| 583 |
The return value is the values of the fields removed. In scalar |
| 584 |
context the number of fields removed is returned. |
| 585 |
|
| 586 |
Note that if you pass in multiple field names then it is generally not |
| 587 |
possible to tell which of the returned values belonged to which field. |
| 588 |
|
| 589 |
=item $h->remove_content_headers |
| 590 |
|
| 591 |
This will remove all the header fields used to describe the content of |
| 592 |
a message. All header field names prefixed with C<Content-> fall |
| 593 |
into this category, as well as C<Allow>, C<Expires> and |
| 594 |
C<Last-Modified>. RFC 2616 denotes these fields as I<Entity Header |
| 595 |
Fields>. |
| 596 |
|
| 597 |
The return value is a new C<HTTP::Headers> object that contains the |
| 598 |
removed headers only. |
| 599 |
|
| 600 |
=item $h->clear |
| 601 |
|
| 602 |
This will remove all header fields. |
| 603 |
|
| 604 |
=item $h->header_field_names |
| 605 |
|
| 606 |
Returns the list of distinct names for the fields present in the |
| 607 |
header. The field names have case as suggested by HTTP spec, and the |
| 608 |
names are returned in the recommended "Good Practice" order. |
| 609 |
|
| 610 |
In scalar context return the number of distinct field names. |
| 611 |
|
| 612 |
=item $h->scan( \&process_header_field ) |
| 613 |
|
| 614 |
Apply a subroutine to each header field in turn. The callback routine |
| 615 |
is called with two parameters; the name of the field and a single |
| 616 |
value (a string). If a header field is multi-valued, then the |
| 617 |
routine is called once for each value. The field name passed to the |
| 618 |
callback routine has case as suggested by HTTP spec, and the headers |
| 619 |
will be visited in the recommended "Good Practice" order. |
| 620 |
|
| 621 |
Any return values of the callback routine are ignored. The loop can |
| 622 |
be broken by raising an exception (C<die>), but the caller of scan() |
| 623 |
would have to trap the exception itself. |
| 624 |
|
| 625 |
=item $h->flatten() |
| 626 |
|
| 627 |
Returns the list of pairs of keys and values. |
| 628 |
|
| 629 |
=item $h->as_string |
| 630 |
|
| 631 |
=item $h->as_string( $eol ) |
| 632 |
|
| 633 |
Return the header fields as a formatted MIME header. Since it |
| 634 |
internally uses the C<scan> method to build the string, the result |
| 635 |
will use case as suggested by HTTP spec, and it will follow |
| 636 |
recommended "Good Practice" of ordering the header fields. Long header |
| 637 |
values are not folded. |
| 638 |
|
| 639 |
The optional $eol parameter specifies the line ending sequence to |
| 640 |
use. The default is "\n". Embedded "\n" characters in header field |
| 641 |
values will be substituted with this line ending sequence. |
| 642 |
|
| 643 |
=back |
| 644 |
|
| 645 |
=head1 CONVENIENCE METHODS |
| 646 |
|
| 647 |
The most frequently used headers can also be accessed through the |
| 648 |
following convenience methods. Most of these methods can both be used to read |
| 649 |
and to set the value of a header. The header value is set if you pass |
| 650 |
an argument to the method. The old header value is always returned. |
| 651 |
If the given header did not exist then C<undef> is returned. |
| 652 |
|
| 653 |
Methods that deal with dates/times always convert their value to system |
| 654 |
time (seconds since Jan 1, 1970) and they also expect this kind of |
| 655 |
value when the header value is set. |
| 656 |
|
| 657 |
=over 4 |
| 658 |
|
| 659 |
=item $h->date |
| 660 |
|
| 661 |
This header represents the date and time at which the message was |
| 662 |
originated. I<E.g.>: |
| 663 |
|
| 664 |
$h->date(time); # set current date |
| 665 |
|
| 666 |
=item $h->expires |
| 667 |
|
| 668 |
This header gives the date and time after which the entity should be |
| 669 |
considered stale. |
| 670 |
|
| 671 |
=item $h->if_modified_since |
| 672 |
|
| 673 |
=item $h->if_unmodified_since |
| 674 |
|
| 675 |
These header fields are used to make a request conditional. If the requested |
| 676 |
resource has (or has not) been modified since the time specified in this field, |
| 677 |
then the server will return a C<304 Not Modified> response instead of |
| 678 |
the document itself. |
| 679 |
|
| 680 |
=item $h->last_modified |
| 681 |
|
| 682 |
This header indicates the date and time at which the resource was last |
| 683 |
modified. I<E.g.>: |
| 684 |
|
| 685 |
# check if document is more than 1 hour old |
| 686 |
if (my $last_mod = $h->last_modified) { |
| 687 |
if ($last_mod < time - 60*60) { |
| 688 |
... |
| 689 |
} |
| 690 |
} |
| 691 |
|
| 692 |
=item $h->content_type |
| 693 |
|
| 694 |
The Content-Type header field indicates the media type of the message |
| 695 |
content. I<E.g.>: |
| 696 |
|
| 697 |
$h->content_type('text/html'); |
| 698 |
|
| 699 |
The value returned will be converted to lower case, and potential |
| 700 |
parameters will be chopped off and returned as a separate value if in |
| 701 |
an array context. If there is no such header field, then the empty |
| 702 |
string is returned. This makes it safe to do the following: |
| 703 |
|
| 704 |
if ($h->content_type eq 'text/html') { |
| 705 |
# we enter this place even if the real header value happens to |
| 706 |
# be 'TEXT/HTML; version=3.0' |
| 707 |
... |
| 708 |
} |
| 709 |
|
| 710 |
=item $h->content_type_charset |
| 711 |
|
| 712 |
Returns the upper-cased charset specified in the Content-Type header. In list |
| 713 |
context return the lower-cased bare content type followed by the upper-cased |
| 714 |
charset. Both values will be C<undef> if not specified in the header. |
| 715 |
|
| 716 |
=item $h->content_is_text |
| 717 |
|
| 718 |
Returns TRUE if the Content-Type header field indicate that the |
| 719 |
content is textual. |
| 720 |
|
| 721 |
=item $h->content_is_html |
| 722 |
|
| 723 |
Returns TRUE if the Content-Type header field indicate that the |
| 724 |
content is some kind of HTML (including XHTML). This method can't be |
| 725 |
used to set Content-Type. |
| 726 |
|
| 727 |
=item $h->content_is_xhtml |
| 728 |
|
| 729 |
Returns TRUE if the Content-Type header field indicate that the |
| 730 |
content is XHTML. This method can't be used to set Content-Type. |
| 731 |
|
| 732 |
=item $h->content_is_xml |
| 733 |
|
| 734 |
Returns TRUE if the Content-Type header field indicate that the |
| 735 |
content is XML. This method can't be used to set Content-Type. |
| 736 |
|
| 737 |
=item $h->content_encoding |
| 738 |
|
| 739 |
The Content-Encoding header field is used as a modifier to the |
| 740 |
media type. When present, its value indicates what additional |
| 741 |
encoding mechanism has been applied to the resource. |
| 742 |
|
| 743 |
=item $h->content_length |
| 744 |
|
| 745 |
A decimal number indicating the size in bytes of the message content. |
| 746 |
|
| 747 |
=item $h->content_language |
| 748 |
|
| 749 |
The natural language(s) of the intended audience for the message |
| 750 |
content. The value is one or more language tags as defined by RFC |
| 751 |
1766. Eg. "no" for some kind of Norwegian and "en-US" for English the |
| 752 |
way it is written in the US. |
| 753 |
|
| 754 |
=item $h->title |
| 755 |
|
| 756 |
The title of the document. In libwww-perl this header will be |
| 757 |
initialized automatically from the E<lt>TITLE>...E<lt>/TITLE> element |
| 758 |
of HTML documents. I<This header is no longer part of the HTTP |
| 759 |
standard.> |
| 760 |
|
| 761 |
=item $h->user_agent |
| 762 |
|
| 763 |
This header field is used in request messages and contains information |
| 764 |
about the user agent originating the request. I<E.g.>: |
| 765 |
|
| 766 |
$h->user_agent('Mozilla/5.0 (compatible; MSIE 7.0; Windows NT 6.0)'); |
| 767 |
|
| 768 |
=item $h->server |
| 769 |
|
| 770 |
The server header field contains information about the software being |
| 771 |
used by the originating server program handling the request. |
| 772 |
|
| 773 |
=item $h->from |
| 774 |
|
| 775 |
This header should contain an Internet e-mail address for the human |
| 776 |
user who controls the requesting user agent. The address should be |
| 777 |
machine-usable, as defined by RFC822. E.g.: |
| 778 |
|
| 779 |
$h->from('King Kong <[email protected]>'); |
| 780 |
|
| 781 |
I<This header is no longer part of the HTTP standard.> |
| 782 |
|
| 783 |
=item $h->referer |
| 784 |
|
| 785 |
Used to specify the address (URI) of the document from which the |
| 786 |
requested resource address was obtained. |
| 787 |
|
| 788 |
The "Free On-line Dictionary of Computing" as this to say about the |
| 789 |
word I<referer>: |
| 790 |
|
| 791 |
<World-Wide Web> A misspelling of "referrer" which |
| 792 |
somehow made it into the {HTTP} standard. A given {web |
| 793 |
page}'s referer (sic) is the {URL} of whatever web page |
| 794 |
contains the link that the user followed to the current |
| 795 |
page. Most browsers pass this information as part of a |
| 796 |
request. |
| 797 |
|
| 798 |
(1998-10-19) |
| 799 |
|
| 800 |
By popular demand C<referrer> exists as an alias for this method so you |
| 801 |
can avoid this misspelling in your programs and still send the right |
| 802 |
thing on the wire. |
| 803 |
|
| 804 |
When setting the referrer, this method removes the fragment from the |
| 805 |
given URI if it is present, as mandated by RFC2616. Note that |
| 806 |
the removal does I<not> happen automatically if using the header(), |
| 807 |
push_header() or init_header() methods to set the referrer. |
| 808 |
|
| 809 |
=item $h->www_authenticate |
| 810 |
|
| 811 |
This header must be included as part of a C<401 Unauthorized> response. |
| 812 |
The field value consist of a challenge that indicates the |
| 813 |
authentication scheme and parameters applicable to the requested URI. |
| 814 |
|
| 815 |
=item $h->proxy_authenticate |
| 816 |
|
| 817 |
This header must be included in a C<407 Proxy Authentication Required> |
| 818 |
response. |
| 819 |
|
| 820 |
=item $h->authorization |
| 821 |
|
| 822 |
=item $h->proxy_authorization |
| 823 |
|
| 824 |
A user agent that wishes to authenticate itself with a server or a |
| 825 |
proxy, may do so by including these headers. |
| 826 |
|
| 827 |
=item $h->authorization_basic |
| 828 |
|
| 829 |
This method is used to get or set an authorization header that use the |
| 830 |
"Basic Authentication Scheme". In array context it will return two |
| 831 |
values; the user name and the password. In scalar context it will |
| 832 |
return I<"uname:password"> as a single string value. |
| 833 |
|
| 834 |
When used to set the header value, it expects two arguments. I<E.g.>: |
| 835 |
|
| 836 |
$h->authorization_basic($uname, $password); |
| 837 |
|
| 838 |
The method will croak if the $uname contains a colon ':'. |
| 839 |
|
| 840 |
=item $h->proxy_authorization_basic |
| 841 |
|
| 842 |
Same as authorization_basic() but will set the "Proxy-Authorization" |
| 843 |
header instead. |
| 844 |
|
| 845 |
=back |
| 846 |
|
| 847 |
=head1 NON-CANONICALIZED FIELD NAMES |
| 848 |
|
| 849 |
The header field name spelling is normally canonicalized including the |
| 850 |
'_' to '-' translation. There are some application where this is not |
| 851 |
appropriate. Prefixing field names with ':' allow you to force a |
| 852 |
specific spelling. For example if you really want a header field name |
| 853 |
to show up as C<foo_bar> instead of "Foo-Bar", you might set it like |
| 854 |
this: |
| 855 |
|
| 856 |
$h->header(":foo_bar" => 1); |
| 857 |
|
| 858 |
These field names are returned with the ':' intact for |
| 859 |
$h->header_field_names and the $h->scan callback, but the colons do |
| 860 |
not show in $h->as_string. |
| 861 |
|
| 862 |
=head1 AUTHOR |
| 863 |
|
| 864 |
Gisle Aas <[email protected]> |
| 865 |
|
| 866 |
=head1 COPYRIGHT AND LICENSE |
| 867 |
|
| 868 |
This software is copyright (c) 1994 by Gisle Aas. |
| 869 |
|
| 870 |
This is free software; you can redistribute it and/or modify it under |
| 871 |
the same terms as the Perl 5 programming language system itself. |
| 872 |
|
| 873 |
=cut |
| 874 |
|
| 875 |
__END__ |
| 876 |
|
| 877 |
|
| 878 |
#ABSTRACT: Class encapsulating HTTP Message headers |
| 879 |
|