/usr/share/perl5/HTTP/Headers/Fast.pm is in libhttp-headers-fast-perl 0.20-1.
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 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 | package HTTP::Headers::Fast;
use strict;
use warnings;
use 5.00800;
use Carp ();
our $VERSION = '0.20';
our $TRANSLATE_UNDERSCORE = 1;
# "Good Practice" order of HTTP message headers:
# - General-Headers
# - Request-Headers
# - Response-Headers
# - Entity-Headers
# yappo says "Readonly sucks".
my $OP_GET = 0;
my $OP_SET = 1;
my $OP_INIT = 2;
my $OP_PUSH = 3;
my @general_headers = qw(
Cache-Control Connection Date Pragma Trailer Transfer-Encoding Upgrade
Via Warning
);
my @request_headers = qw(
Accept Accept-Charset Accept-Encoding Accept-Language
Authorization Expect From Host
If-Match If-Modified-Since If-None-Match If-Range If-Unmodified-Since
Max-Forwards Proxy-Authorization Range Referer TE User-Agent
);
my @response_headers = qw(
Accept-Ranges Age ETag Location Proxy-Authenticate Retry-After Server
Vary WWW-Authenticate
);
my @entity_headers = qw(
Allow Content-Encoding Content-Language Content-Length Content-Location
Content-MD5 Content-Range Content-Type Expires Last-Modified
);
my %entity_header = map { lc($_) => 1 } @entity_headers;
my @header_order =
( @general_headers, @request_headers, @response_headers, @entity_headers, );
# Make alternative representations of @header_order. This is used
# for sorting and case matching.
my %header_order;
our %standard_case;
{
my $i = 0;
for (@header_order) {
my $lc = lc $_;
$header_order{$lc} = ++$i;
$standard_case{$lc} = $_;
}
}
sub new {
my ($class) = shift;
my $self = bless {}, $class;
$self->header(@_) if @_; # set up initial headers
$self;
}
sub isa {
my ($self, $klass) = @_;
my $proto = ref $self || $self;
return ($proto eq $klass || $klass eq 'HTTP::Headers') ? 1 : 0;
}
sub header {
my $self = shift;
Carp::croak('Usage: $h->header($field, ...)') unless @_;
my (@old);
if (@_ == 1) {
@old = $self->_header_get(@_);
} elsif( @_ == 2 ) {
@old = $self->_header_set(@_);
} else {
my %seen;
while (@_) {
my $field = shift;
if ( $seen{ lc $field }++ ) {
@old = $self->_header_push($field, shift);
} else {
@old = $self->_header_set($field, shift);
}
}
}
return @old if wantarray;
return $old[0] if @old <= 1;
join( ", ", @old );
}
sub clear {
my $self = shift;
%$self = ();
}
sub push_header {
my $self = shift;
if (@_ == 2) {
my ($field, $val) = @_;
$field = _standardize_field_name($field) unless $field =~ /^:/;
my $h = $self->{$field};
if (!defined $h) {
$h = [];
$self->{$field} = $h;
} elsif (ref $h ne 'ARRAY') {
$h = [ $h ];
$self->{$field} = $h;
}
push @$h, ref $val ne 'ARRAY' ? $val : @$val;
} else {
while ( my ($field, $val) = splice( @_, 0, 2 ) ) {
$field = _standardize_field_name($field) unless $field =~ /^:/;
my $h = $self->{$field};
if (!defined $h) {
$h = [];
$self->{$field} = $h;
} elsif (ref $h ne 'ARRAY') {
$h = [ $h ];
$self->{$field} = $h;
}
push @$h, ref $val ne 'ARRAY' ? $val : @$val;
}
}
return ();
}
sub init_header {
Carp::croak('Usage: $h->init_header($field, $val)') if @_ != 3;
shift->_header( @_, $OP_INIT );
}
sub remove_header {
my ( $self, @fields ) = @_;
my $field;
my @values;
for my $field (@fields) {
$field =~ tr/_/-/ if $field !~ /^:/ && $TRANSLATE_UNDERSCORE;
my $v = delete $self->{ lc $field };
push( @values, ref($v) eq 'ARRAY' ? @$v : $v ) if defined $v;
}
return @values;
}
sub remove_content_headers {
my $self = shift;
unless ( defined(wantarray) ) {
# fast branch that does not create return object
delete @$self{ grep $entity_header{$_} || /^content-/, keys %$self };
return;
}
my $c = ref($self)->new;
for my $f ( grep $entity_header{$_} || /^content-/, keys %$self ) {
$c->{$f} = delete $self->{$f};
}
$c;
}
my %field_name;
sub _standardize_field_name {
my $field = shift;
$field =~ tr/_/-/ if $TRANSLATE_UNDERSCORE;
if (my $cache = $field_name{$field}) {
return $cache;
}
my $old = $field;
$field = lc $field;
unless ( defined $standard_case{$field} ) {
# generate a %standard_case entry for this field
$old =~ s/\b(\w)/\u$1/g;
$standard_case{$field} = $old;
}
$field_name{$old} = $field;
return $field;
}
sub _header_get {
my ($self, $field, $skip_standardize) = @_;
$field = _standardize_field_name($field) unless $skip_standardize || $field =~ /^:/;
my $h = $self->{$field};
return (ref($h) eq 'ARRAY') ? @$h : ( defined($h) ? ($h) : () );
}
sub _header_set {
my ($self, $field, $val) = @_;
$field = _standardize_field_name($field) unless $field =~ /^:/;
my $h = $self->{$field};
my @old = ref($h) eq 'ARRAY' ? @$h : ( defined($h) ? ($h) : () );
if ( defined($val) ) {
if (ref $val eq 'ARRAY' && scalar(@$val) == 1) {
$val = $val->[0];
}
$self->{$field} = $val;
} else {
delete $self->{$field};
}
return @old;
}
sub _header_push {
my ($self, $field, $val) = @_;
$field = _standardize_field_name($field) unless $field =~ /^:/;
my $h = $self->{$field};
if (ref($h) eq 'ARRAY') {
my @old = @$h;
push @$h, ref $val ne 'ARRAY' ? $val : @$val;
return @old;
} elsif (defined $h) {
$self->{$field} = [$h, ref $val ne 'ARRAY' ? $val : @$val ];
return ($h);
} else {
$self->{$field} = ref $val ne 'ARRAY' ? $val : @$val;
return ();
}
}
sub _header {
my ($self, $field, $val, $op) = @_;
$field = _standardize_field_name($field) unless $field =~ /^:/;
$op ||= defined($val) ? $OP_SET : $OP_GET;
my $h = $self->{$field};
my @old = ref($h) eq 'ARRAY' ? @$h : ( defined($h) ? ($h) : () );
unless ( $op == $OP_GET || ( $op == $OP_INIT && @old ) ) {
if ( defined($val) ) {
my @new = ( $op == $OP_PUSH ) ? @old : ();
if ( ref($val) ne 'ARRAY' ) {
push( @new, $val );
}
else {
push( @new, @$val );
}
$self->{$field} = @new > 1 ? \@new : $new[0];
}
elsif ( $op != $OP_PUSH ) {
delete $self->{$field};
}
}
@old;
}
sub _sorted_field_names {
my $self = shift;
return [ sort {
( $header_order{$a} || 999 ) <=> ( $header_order{$b} || 999 )
|| $a cmp $b
} keys %$self ];
}
sub header_field_names {
my $self = shift;
return map $standard_case{$_} || $_, @{ $self->_sorted_field_names }
if wantarray;
return keys %$self;
}
sub scan {
my ( $self, $sub ) = @_;
for my $key (@{ $self->_sorted_field_names }) {
next if substr($key, 0, 1) eq '_';
my $vals = $self->{$key};
if ( ref($vals) eq 'ARRAY' ) {
for my $val (@$vals) {
$sub->( $standard_case{$key} || $key, $val );
}
}
else {
$sub->( $standard_case{$key} || $key, $vals );
}
}
}
sub _process_newline {
local $_ = shift;
my $endl = shift;
# must handle header values with embedded newlines with care
s/\s+$//; # trailing newlines and space must go
s/\n(\x0d?\n)+/\n/g; # no empty lines
s/\n([^\040\t])/\n $1/g; # intial space for continuation
s/\n/$endl/g; # substitute with requested line ending
$_;
}
sub _as_string {
my ($self, $endl, $fieldnames) = @_;
my @result;
for my $key ( @$fieldnames ) {
next if index($key, '_') == 0;
my $vals = $self->{$key};
if ( ref($vals) eq 'ARRAY' ) {
for my $val (@$vals) {
my $field = $standard_case{$key} || $key;
$field =~ s/^://;
if ( index($val, "\n") >= 0 ) {
$val = _process_newline($val, $endl);
}
push @result, $field . ': ' . $val;
}
} else {
my $field = $standard_case{$key} || $key;
$field =~ s/^://;
if ( index($vals, "\n") >= 0 ) {
$vals = _process_newline($vals, $endl);
}
push @result, $field . ': ' . $vals;
}
}
join( $endl, @result, '' );
}
sub as_string {
my ( $self, $endl ) = @_;
$endl = "\n" unless defined $endl;
$self->_as_string($endl, $self->_sorted_field_names);
}
sub as_string_without_sort {
my ( $self, $endl ) = @_;
$endl = "\n" unless defined $endl;
$self->_as_string($endl, [keys(%$self)]);
}
sub _flatten {
my ($self, $keys) = @_;
my @headers;
for my $key ( @{$keys} ) {
next if substr($key, 0, 1) eq '_';
my $vals = $self->{$key};
if ( ref($vals) eq 'ARRAY' ) {
for my $val (@$vals) {
$val =~ s/\015\012[\040|\011]+/chr(32)/ge; # replace LWS with a single SP
$val =~ s/\015|\012//g; # remove CR and LF since the char is invalid here
push @headers, $standard_case{$key} || $key, $val;
}
}
else {
$vals =~ s/\015\012[\040|\011]+/chr(32)/ge; # replace LWS with a single SP
$vals =~ s/\015|\012//g; # remove CR and LF since the char is invalid here
push @headers, $standard_case{$key} || $key, $vals;
}
}
return \@headers;
}
sub flatten {
$_[0]->_flatten($_[0]->_sorted_field_names);
}
sub flatten_without_sort {
$_[0]->_flatten([keys %{$_[0]}]);
}
{
my $storable_required;
sub clone {
unless ($storable_required) {
require Storable;
$storable_required++;
}
goto &Storable::dclone;
}
}
sub _date_header {
require HTTP::Date;
my ( $self, $header, $time ) = @_;
my $old;
if ( defined $time ) {
($old) = $self->_header_set( $header, HTTP::Date::time2str($time) );
} else {
($old) = $self->_header_get($header, 1);
}
$old =~ s/;.*// if defined($old);
HTTP::Date::str2time($old);
}
sub date { shift->_date_header( 'date', @_ ); }
sub expires { shift->_date_header( 'expires', @_ ); }
sub if_modified_since { shift->_date_header( 'if-modified-since', @_ ); }
sub if_unmodified_since { shift->_date_header( 'if-unmodified-since', @_ ); }
sub last_modified { shift->_date_header( 'last-modified', @_ ); }
# This is used as a private LWP extension. The Client-Date header is
# added as a timestamp to a response when it has been received.
sub client_date { shift->_date_header( 'client-date', @_ ); }
# The retry_after field is dual format (can also be a expressed as
# number of seconds from now), so we don't provide an easy way to
# access it until we have know how both these interfaces can be
# addressed. One possibility is to return a negative value for
# relative seconds and a positive value for epoch based time values.
#sub retry_after { shift->_date_header('Retry-After', @_); }
sub content_type {
my $self = shift;
my $ct = $self->{'content-type'};
$self->{'content-type'} = shift if @_;
$ct = $ct->[0] if ref($ct) eq 'ARRAY';
return '' unless defined($ct) && length($ct);
my @ct = split( /;\s*/, $ct, 2 );
for ( $ct[0] ) {
s/\s+//g;
$_ = lc($_);
}
wantarray ? @ct : $ct[0];
}
sub content_type_charset {
my $self = shift;
my $h = $self->{'content-type'};
$h = $h->[0] if ref($h);
$h = "" unless defined $h;
my @v = _split_header_words($h);
if (@v) {
my($ct, undef, %ct_param) = @{$v[0]};
my $charset = $ct_param{charset};
if ($ct) {
$ct = lc($ct);
$ct =~ s/\s+//;
}
if ($charset) {
$charset = uc($charset);
$charset =~ s/^\s+//; $charset =~ s/\s+\z//;
undef($charset) if $charset eq "";
}
return $ct, $charset if wantarray;
return $charset;
}
return undef, undef if wantarray;
return undef;
}
sub _split_header_words
{
my(@val) = @_;
my @res;
for (@val) {
my @cur;
while (length) {
if (s/^\s*(=*[^\s=;,]+)//) { # 'token' or parameter 'attribute'
push(@cur, $1);
# a quoted value
if (s/^\s*=\s*\"([^\"\\]*(?:\\.[^\"\\]*)*)\"//) {
my $val = $1;
$val =~ s/\\(.)/$1/g;
push(@cur, $val);
# some unquoted value
}
elsif (s/^\s*=\s*([^;,\s]*)//) {
my $val = $1;
$val =~ s/\s+$//;
push(@cur, $val);
# no value, a lone token
}
else {
push(@cur, undef);
}
}
elsif (s/^\s*,//) {
push(@res, [@cur]) if @cur;
@cur = ();
}
elsif (s/^\s*;// || s/^\s+//) {
# continue
}
else {
die "This should not happen: '$_'";
}
}
push(@res, \@cur) if @cur;
}
for my $arr (@res) {
for (my $i = @$arr - 2; $i >= 0; $i -= 2) {
$arr->[$i] = lc($arr->[$i]);
}
}
return @res;
}
sub content_is_html {
my $self = shift;
return $self->content_type eq 'text/html' || $self->content_is_xhtml;
}
sub content_is_xhtml {
my $ct = shift->content_type;
return $ct eq "application/xhtml+xml"
|| $ct eq "application/vnd.wap.xhtml+xml";
}
sub content_is_xml {
my $ct = shift->content_type;
return 1 if $ct eq "text/xml";
return 1 if $ct eq "application/xml";
return 1 if $ct =~ /\+xml$/;
return 0;
}
sub referer {
my $self = shift;
if ( @_ && $_[0] =~ /#/ ) {
# Strip fragment per RFC 2616, section 14.36.
my $uri = shift;
if ( ref($uri) ) {
$uri = $uri->clone;
$uri->fragment(undef);
}
else {
$uri =~ s/\#.*//;
}
unshift @_, $uri;
}
( $self->_header( 'Referer', @_ ) )[0];
}
*referrer = \&referer; # on tchrist's request
for my $key (qw/content-length content-language content-encoding title user-agent server from warnings www-authenticate authorization proxy-authenticate proxy-authorization/) {
no strict 'refs';
(my $meth = $key) =~ s/-/_/g;
*{$meth} = sub {
my $self = shift;
if (@_) {
( $self->_header_set( $key, @_ ) )[0]
} else {
my $h = $self->{$key};
(ref($h) eq 'ARRAY') ? $h->[0] : $h;
}
};
}
sub authorization_basic { shift->_basic_auth( "Authorization", @_ ) }
sub proxy_authorization_basic {
shift->_basic_auth( "Proxy-Authorization", @_ );
}
sub _basic_auth {
require MIME::Base64;
my ( $self, $h, $user, $passwd ) = @_;
my ($old) = $self->_header($h);
if ( defined $user ) {
Carp::croak("Basic authorization user name can't contain ':'")
if $user =~ /:/;
$passwd = '' unless defined $passwd;
$self->_header(
$h => 'Basic ' . MIME::Base64::encode( "$user:$passwd", '' ) );
}
if ( defined $old && $old =~ s/^\s*Basic\s+// ) {
my $val = MIME::Base64::decode($old);
return $val unless wantarray;
return split( /:/, $val, 2 );
}
return;
}
1;
__END__
=encoding utf8
=head1 NAME
HTTP::Headers::Fast - faster implementation of HTTP::Headers
=head1 SYNOPSIS
use HTTP::Headers::Fast;
# and, same as HTTP::Headers.
=head1 DESCRIPTION
HTTP::Headers::Fast is a perl class for parsing/writing HTTP headers.
The interface is same as HTTP::Headers.
=head1 WHY YET ANOTHER ONE?
HTTP::Headers is a very good. But I needed a faster implementation, fast =)
=head1 ADDITIONAL METHODS
=over 4
=item as_string_without_sort
as_string method sorts the header names.But, sorting is bit slow.
In this method, stringify the instance of HTTP::Headers::Fast without sorting.
=item flatten
returns PSGI compatible arrayref of header.
my $headers:ArrayRef = $header->flatten
=item flatten_without_sort
same as flatten but returns arrayref without sorting.
=back
=head1 @ISA HACK
If you want HTTP::Headers::Fast to pretend like it's really HTTP::Headers, you can try the following hack:
unshift @HTTP::Headers::Fast::ISA, 'HTTP::Headers';
=head1 BENCHMARK
HTTP::Headers 5.818, HTTP::Headers::Fast 0.01
-- push_header
Rate orig fast
orig 144928/s -- -20%
fast 181818/s 25% --
-- push_header_many
Rate orig fast
orig 74627/s -- -16%
fast 89286/s 20% --
-- get_date
Rate orig fast
orig 34884/s -- -14%
fast 40541/s 16% --
-- set_date
Rate orig fast
orig 21505/s -- -19%
fast 26525/s 23% --
-- scan
Rate orig fast
orig 57471/s -- -1%
fast 57803/s 1% --
-- get_header
Rate orig fast
orig 120337/s -- -24%
fast 157729/s 31% --
-- set_header
Rate orig fast
orig 79745/s -- -30%
fast 113766/s 43% --
-- get_content_length
Rate orig fast
orig 182482/s -- -77%
fast 793651/s 335% --
-- as_string
Rate orig fast
orig 23753/s -- -41%
fast 40161/s 69% --
=head1 AUTHOR
Tokuhiro Matsuno E<lt>tokuhirom@gmail.comE<gt>
Daisuke Maki
And HTTP::Headers' originally written by Gisle Aas.
=head1 THANKS TO
Markstos
Tatsuhiko Miyagawa
=head1 SEE ALSO
L<HTTP::Headers>
=head1 LICENSE
This library is free software; you can redistribute it and/or modify
it under the same terms as Perl itself.
=cut
|