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