⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 headers.pm

📁 美国CMU大学开发的操作系统健壮性评测软件
💻 PM
📖 第 1 页 / 共 2 页
字号:
## $Id: Headers.pm,v 1.1 1999/07/21 19:12:28 kraven Exp $package HTTP::Headers;=head1 NAMEHTTP::Headers - Class encapsulating HTTP Message headers=head1 SYNOPSIS require HTTP::Headers; $h = new HTTP::Headers;=head1 DESCRIPTIONThe C<HTTP::Headers> class encapsulates HTTP-style message headers.The headers consist of attribute-value pairs, which may be repeated,and which are printed in a particular order.Instances of this class are usually created as member variables of theC<HTTP::Request> and C<HTTP::Response> classes, internal to thelibrary.The following methods are available:=over 4=cutuse strict;use vars qw($VERSION $TRANSLATE_UNDERSCORE);$VERSION = sprintf("%d.%02d", q$Revision: 1.1 $ =~ /(\d+)\.(\d+)/);use Carp ();# Could not use the AutoLoader becase several of the method names are# not unique in the first 8 characters.#use SelfLoader;# "Good Practice" order of HTTP message headers:#    - General-Headers#    - Request-Headers#    - Response-Headers#    - Entity-Headers# (From draft-ietf-http-v11-spec-rev-01, Nov 21, 1997)my @header_order = qw(   Cache-Control Connection Date Pragma Transfer-Encoding Upgrade Trailer Via   Accept Accept-Charset Accept-Encoding Accept-Language   Authorization Expect From Host   If-Modified-Since If-Match If-None-Match If-Range If-Unmodified-Since   Max-Forwards Proxy-Authorization Range Referer TE User-Agent   Accept-Ranges Age Location Proxy-Authenticate Retry-After Server Vary   Warning WWW-Authenticate   Allow Content-Base Content-Encoding Content-Language Content-Length   Content-Location Content-MD5 Content-Range Content-Type   ETag Expires Last-Modified);# Make alternative representations of @header_order.  This is used# for sorting and case matching.my $i = 0;my %header_order;my %standard_case;for (@header_order) {    my $lc = lc $_;    $header_order{$lc} = $i++;    $standard_case{$lc} = $_;}$TRANSLATE_UNDERSCORE = 1 unless defined $TRANSLATE_UNDERSCORE;=item $h = new HTTP::HeadersConstructs a new C<HTTP::Headers> object.  You might pass some initialattribute-value pairs as parameters to the constructor.  I<E.g.>: $h = new HTTP::Headers     Date         => 'Thu, 03 Feb 1994 00:00:00 GMT',     Content_Type => 'text/html; version=3.2',     Content_Base => 'http://www.sn.no/';=cutsub new{    my($class) = shift;    my $self = bless {}, $class;    $self->header(@_); # set up initial headers    $self;}=item $h->header($field [=> $value],...)Get or set the value of a header.  The header field name is not casesensitive.  To make the life easier for perl users who wants to avoidquoting before the => operator, you can use '_' as a synonym for '-'in header names (this behaviour can be suppressed by setting$HTTP::Headers::TRANSLATE_UNDERSCORE to a FALSE value).The header() method accepts multiple ($field => $value) pairs, so youcan update several fields with a single invocation.The optional $value argument may be a scalar or a reference to a listof scalars. If the $value argument is undefined or not given, then theheader is not modified.The old value of the last of the $field values is returned.Multi-valued fields will be concatenated with "," as separator inscalar context. $header->header(MIME_Version => '1.0',		 User_Agent   => 'My-Web-Client/0.01'); $header->header(Accept => "text/html, text/plain, image/*"); $header->header(Accept => [qw(text/html text/plain image/*)]); @accepts = $header->header('Accept');=cutsub header{    my $self = shift;    my($field, $val, @old);    while (($field, $val) = splice(@_, 0, 2)) {	@old = $self->_header($field, $val);    }    return @old if wantarray;    return $old[0] if @old <= 1;    join(", ", @old);}sub _header{    my($self, $field, $val, $push) = @_;    $field =~ tr/_/-/ if $TRANSLATE_UNDERSCORE;    # $push is only used interally sub push_header    Carp::croak('Need a field name') unless length($field);    my $lc_field = lc $field;    unless(defined $standard_case{$lc_field}) {	# generate a %stadard_case entry for this field	$field =~ s/\b(\w)/\u$1/g;	$standard_case{$lc_field} = $field;    }    my $h = $self->{$lc_field};    my @old = ref($h) ? @$h : (defined($h) ? ($h) : ());    if (defined $val) {	my @new = $push ? @old : ();	if (!ref($val)) {	    push(@new, $val);	} elsif (ref($val) eq 'ARRAY') {	    push(@new, @$val);	} else {	    Carp::croak("Unexpected field value $val");	}	$self->{$lc_field} = @new > 1 ? \@new : $new[0];    }    @old;}# Compare function which makes it easy to sort headers in the# recommended "Good Practice" order.sub _header_cmp{    # Unknown headers are assign a large value so that they are    # sorted last.  This also helps avoiding a warning from -w    # about comparing undefined values.    $header_order{$a} = 999 unless defined $header_order{$a};    $header_order{$b} = 999 unless defined $header_order{$b};    $header_order{$a} <=> $header_order{$b} || $a cmp $b;}=item $h->scan(\&doit)Apply a subroutine to each header in turn.  The callback routine iscalled with two parameters; the name of the field and a single value.If the header has more than one value, then the routine is called oncefor each value.  The field name passed to the callback routine hascase as suggested by HTTP Spec, and the headers will be visited in therecommended "Good Practice" order.=cutsub scan{    my($self, $sub) = @_;    my $key;    foreach $key (sort _header_cmp keys %$self) {        next if $key =~ /^_/;	my $vals = $self->{$key};	if (ref($vals)) {	    my $val;	    for $val (@$vals) {		&$sub($standard_case{$key} || $key, $val);	    }	} else {	    &$sub($standard_case{$key} || $key, $vals);	}    }}=item $h->as_string([$endl])Return the header fields as a formatted MIME header.  Since itinternally uses the C<scan()> method to build the string, the resultwill use case as suggested by HTTP Spec, and it will followrecommended "Good Practice" of ordering the header fieds.  Long headervalues are not folded. The optional parameter specifies the line ending sequence to use.  Thedefault is C<"\n">.  Embedded "\n" characters in the header will besubstitued with this line ending sequence.=cutsub as_string{    my($self, $endl) = @_;    $endl = "\n" unless defined $endl;    my @result = ();    $self->scan(sub {	my($field, $val) = @_;	if ($val =~ /\n/) {	    # must handle header values with embedded newlines with care	    $val =~ s/\s+$//;          # trailing newlines and space must go	    $val =~ s/\n\n+/\n/g;      # no empty lines	    $val =~ s/\n([^\040\t])/\n $1/g;  # intial space for continuation	    $val =~ s/\n/$endl/g;      # substitute with requested line ending	}	push(@result, "$field: $val");    });    join($endl, @result, '');}# The remaining functions should autoloaded only when needed# A bug in 5.002gamma makes it risky to have POD text inside the# autoloaded section of the code, so we keep the documentation before# the __DATA__ token.=item $h->push_header($field, $val)Add a new field value of the specified header.  The header field nameis not case sensitive.  The field need not already have avalue. Previous values for the same field are retained.  The argumentmay be a scalar or a reference to a list of scalars. $header->push_header(Accept => 'image/jpeg');=item $h->remove_header($field,...)This function removes the headers with the specified names.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -