Skip to content
This repository
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 695 lines (533 sloc) 16.115 kb
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
package CGI::Header;
use 5.008_009;
use strict;
use warnings;
use overload q{""} => 'as_string', fallback => 1;
use Carp qw/carp croak/;
use CGI::Util qw//;
use HTTP::Date qw//;
use List::Util qw/first/;
use Scalar::Util qw/refaddr/;
use Storable qw//;

our $VERSION = '0.01';

my %header_of;

sub new {
    my $class = shift;
    my $header = ref $_[0] eq 'HASH' ? shift : { @_ };
    my $self = bless \do { my $anon_scalar }, $class;
    my $this = refaddr $self;

    $header_of{ $this } = $header;

    $self;
}

sub header {
    my $self = shift;
    my $this = refaddr $self;
    $header_of{ $this };
}

sub DESTROY {
    my $self = shift;
    my $this = refaddr $self;
    delete $header_of{ $this };
    return;
}

sub get {
    my $self = shift;
    my $norm = $self->_normalize( shift );
    my $this = refaddr $self;
    my $header = $header_of{ $this };

    return unless $norm;

    if ( $norm eq '-content_type' ) {
        my $type = $header->{-type};
        my $charset = $header->{-charset};

        if ( defined $type and $type eq q{} ) {
            undef $charset;
            undef $type;
        }
        else {
            $type ||= 'text/html';

            if ( $type =~ /\bcharset\b/ ) {
                undef $charset;
            }
            elsif ( !defined $charset ) {
                $charset = 'ISO-8859-1';
            }
        }

        return $charset ? "$type; charset=$charset" : $type;
    }
    elsif ( $norm eq '-expires' ) {
        my $expires = $header->{-expires};
        return $expires && CGI::Util::expires( $expires );
    }
    elsif ( $norm eq '-p3p' ) {
        my $p3p = $header->{-p3p};
        my $tags = ref $p3p eq 'ARRAY' ? join ' ', @{ $p3p } : $p3p;
        return $tags && qq{policyref="/w3c/p3p.xml", CP="$tags"};
    }

    if ( $norm eq '-content_disposition' ) {
        if ( my $filename = $header->{-attachment} ) {
            return qq{attachment; filename="$filename"};
        }
    }
    elsif ( $norm eq '-date' ) {
        if ( first { $header->{$_} } qw(-nph -expires -cookie) ) {
            return HTTP::Date::time2str( time );
        }
    }

    $header->{ $norm };
}

sub set {
    my $self = shift;
    my $norm = $self->_normalize( shift );
    my $value = shift;
    my $this = refaddr $self;
    my $header = $header_of{ $this };

    return unless $norm;

    if ( $norm eq '-content_type' ) {
        $header->{-type} = $value;
        $header->{-charset} = q{};
        return;
    }
    elsif ( $norm eq '-p3p' or $norm eq '-expires' ) {
        carp "Can't assign to '$norm' directly, use accessors instead";
        return;
    }

    if ( $norm eq '-date' ) {
        if ( first { $header->{$_} } qw(-nph -expires -cookie) ) {
            return carp 'The Date header is fixed';
        }
    }
    elsif ( $norm eq '-content_disposition' ) {
        delete $header->{-attachment};
    }
    elsif ( $norm eq '-cookie' ) {
        delete $header->{-date};
    }

    $header->{ $norm } = $value;

    return;
}

sub delete {
    my $self = shift;
    my $field = shift;
    my $norm = $self->_normalize( $field );
    my $value = defined wantarray && $self->get( $field );
    my $this = refaddr $self;
    my $header = $header_of{ $this };

    return unless $norm;

    if ( $norm eq '-date' ) {
        if ( first { $header->{$_} } qw(-nph -expires -cookie) ) {
            return carp 'The Date header is fixed';
        }
    }
    elsif ( $norm eq '-content_type' ) {
        delete $header->{-charset};
        $header->{-type} = q{};
    }
    elsif ( $norm eq '-content_disposition' ) {
        delete $header->{-attachment};
    }

    delete $header->{ $norm };

    $value;
}

sub clear {
    my $self = shift;
    my $this = refaddr $self;
    %{ $header_of{$this} } = ( -type => q{} );
    return;
}

sub exists {
    my $self = shift;
    my $norm = $self->_normalize( shift );
    my $this = refaddr $self;
    my $header = $header_of{ $this };

    return unless $norm;

    if ( $norm eq '-content_type' ) {
        return !defined $header->{-type} || $header->{-type} ne q{};
    }
    
    if ( $norm eq '-content_disposition' ) {
        return 1 if $header->{-attachment};
    }
    elsif ( $norm eq '-date' ) {
        return 1 if first { $header->{$_} } qw(-nph -expires -cookie);
    }

    $header->{ $norm };
}

BEGIN {
    *TIEHASH = \&new; *STORE = \&set; *FETCH = \&get;
    *CLEAR = \&clear; *EXISTS = \&exists; *DELETE = \&delete;
}

sub SCALAR {
    my $self = shift;
    my $this = refaddr $self;
    my $header = $header_of{ $this };
    !defined $header->{-type} || first { $_ } values %{ $header };
}

sub is_empty { !shift->SCALAR }

sub field_names {
    my $self = shift;
    my $this = refaddr $self;
    my %header = %{ $header_of{$this} }; # copy

    my @fields;

    push @fields, 'Status' if delete $header{-status};
    push @fields, 'Window-Target' if delete $header{-target};
    push @fields, 'P3P' if delete $header{-p3p};

    push @fields, 'Set-Cookie' if my $cookie = delete $header{-cookie};
    push @fields, 'Expires' if my $expires = delete $header{-expires};
    push @fields, 'Date' if delete $header{-nph} or $cookie or $expires;

    push @fields, 'Content-Disposition' if delete $header{-attachment};

    my $type = delete @header{ '-charset', '-type' };

    # not ordered
    while ( my ($norm, $value) = CORE::each %header ) {
        push @fields, $self->_denormalize( $norm ) if $value;
    }

    push @fields, 'Content-Type' if !defined $type or $type ne q{};

    @fields;
}

sub flatten {
    my $self = shift;
    map { $_, $self->get($_) } $self->field_names;
}

sub each {
    my ( $self, $callback ) = @_;

    if ( ref $callback eq 'CODE' ) {
        for my $field ( $self->field_names ) {
            $callback->( $field, $self->get($field) );
        }
    }
    else {
        croak 'Must provide a code reference to each()';
    }

    return;
}

sub attachment {
    my $self = shift;
    my $this = refaddr $self;
    my $header = $header_of{ $this };

    if ( @_ ) {
        my $filename = shift;
        delete $header->{-content_disposition};
        $header->{-attachment} = $filename;
        return;
    }

    $header->{-attachment};
}

sub nph {
    my $self = shift;
    my $this = refaddr $self;
    my $header = $header_of{ $this };
    
    if ( @_ ) {
        my $nph = shift;
        delete $header->{-date} if $nph;
        $header->{-nph} = $nph;
        return;
    }

    $header->{-nph};
}

sub p3p_tags {
    my $self = shift;
    my $this = refaddr $self;
    my $header = $header_of{ $this };

    if ( @_ ) {
        $header->{-p3p} = @_ > 1 ? [ @_ ] : shift;
    }
    elsif ( my $tags = $header->{-p3p} ) {
        my @tags = ref $tags eq 'ARRAY' ? @{ $tags } : split ' ', $tags;
        return wantarray ? @tags : $tags[0];
    }

    return;
}

sub target {
    my $self = shift;
    my $this = refaddr $self;
    my $header = $header_of{ $this };

    if ( @_ ) {
        $header->{-target} = shift;
        return;
    }

    $header->{-target};
}

sub dump {
    my $self = shift;
    my $this = refaddr $self;

    require Data::Dumper;

    local $Data::Dumper::Indent = 1;

    my %dump = (
        __PACKAGE__, {
            header => $header_of{ $this },
        },
        @_,
    );

    Data::Dumper::Dumper( \%dump );
}

sub content_type {
    my $self = shift;

    return $self->set( 'Content-Type' => shift ) if @_;

    my ( $media_type, $rest ) = do {
        my $content_type = $self->get( 'Content-Type' );
        return q{} unless defined $content_type;
        split /;\s*/, $content_type, 2;
    };

    $media_type =~ s/\s+//g;
    $media_type = lc $media_type;

    wantarray ? ($media_type, $rest) : $media_type;
}

sub expires {
    my $self = shift;
    my $this = refaddr $self;
    my $header = $header_of{ $this };

    if ( @_ ) {
        my $expires = shift;
        delete $header->{-date} if $expires;
        $header->{-expires} = $expires;
    }
    elsif ( my $expires = $self->get('Expires') ) {
        return HTTP::Date::str2time( $expires );
    }

    return;
}

sub date {
    my $self = shift;
    my $time = shift;
    my $this = refaddr $self;
    my $header = $header_of{ $this };
    my $is_fixed = first { $header->{$_} } qw(-nph -expires -cookie);

    if ( defined $time ) {
        return carp 'The Date header is fixed' if $is_fixed;
        $header->{-date} = HTTP::Date::time2str( $time );
    }
    elsif ( $is_fixed ) {
        return time;
    }
    elsif ( my $date = $header->{-date} ) {
        return HTTP::Date::str2time( $date );
    }

    return;
}

sub get_cookie {
    my $self = shift;
    my $name = shift;
    my $this = refaddr $self;
    my $header = $header_of{ $this };

    my @cookies = do {
        my $cookies = $header->{-cookie};
        return unless $cookies;
        ref $cookies eq 'ARRAY' ? @{ $cookies } : $cookies;
    };

    my @values;
    for my $cookie ( @cookies ) {
        next unless ref $cookie eq 'CGI::Cookie';
        next unless $cookie->name eq $name;
        push @values, $cookie;
    }

    wantarray ? @values : $values[0];
}

sub set_cookie {
    my ( $self, $name, $value ) = @_;

    require CGI::Cookie;

    my $new_cookie = CGI::Cookie->new(do {
        my %args = ref $value eq 'HASH' ? %{ $value } : ( value => $value );
        $args{name} = $name;
        \%args;
    });

    my $cookies = $self->get( 'Set-Cookie' );

    if ( !$cookies ) {
        $self->set( 'Set-Cookie' => [ $new_cookie ] );
        return;
    }
    elsif ( ref $cookies ne 'ARRAY' ) {
        $cookies = [ $cookies ];
        $self->set( 'Set-Cookie' => $cookies );
    }

    my $set;
    for my $cookie ( @{$cookies} ) {
        next unless ref $cookie eq 'CGI::Cookie';
        next unless $cookie->name eq $name;
        $cookie = $new_cookie;
        $set++;
        last;
    }

    push @{ $cookies }, $new_cookie unless $set;

    return;
}

sub status {
    my $self = shift;
    my $this = refaddr $self;
    my $header = $header_of{ $this };

    require HTTP::Status;

    if ( @_ ) {
        my $code = shift;
        my $message = HTTP::Status::status_message( $code );
        return $header->{-status} = "$code $message" if $message;
        carp "Unknown status code '$code' passed to status()";
    }
    elsif ( my $status = $header->{-status} ) {
        return substr( $status, 0, 3 );
    }
    else {
        return '200';
    }

    return;
}

sub as_string {
    my $self = shift;
    my $eol = defined $_[0] ? shift : "\n";
    my $this = refaddr $self;
    my $header = $header_of{ $this };

    my @lines;

    if ( $header->{-nph} ) {
        my $protocol = $ENV{SERVER_PROTOCOL} || 'HTTP/1.0';
        my $software = $ENV{SERVER_SOFTWARE} || 'cmdline';
        my $status = $header->{-status} || '200 OK';
        push @lines, "$protocol $status";
        push @lines, "Server: $software";
    }

    $self->each(sub {
        my ( $field, $value ) = @_;
        my @values = ref $value eq 'ARRAY' ? @{ $value } : $value;
        push @lines, "$field: $_" for @values;
    });

    # CR escaping for values, per RFC 822
    for my $line ( @lines ) {
        $line =~ s/$eol(\s)/$1/g;
        next unless $line =~ m/$eol|\015|\012/;
        $line = substr $line, 0, 72 if length $line > 72;
        croak "Invalid header value contains a new line ",
              "not followed by whitespace: $line";
    }

    join $eol, @lines, q{};
}

BEGIN { *clone = \&Storable::dclone }

sub STORABLE_freeze {
    my ( $self, $cloning ) = @_;
    my $this = refaddr $self;
    ( q{}, $header_of{$this} );
}

sub STORABLE_thaw {
    my ( $self, $serialized, $cloning, $header ) = @_;
    my $this = refaddr $self;
    $header_of{ $this } = $header;
    $self;
}

my %norm_of = (
    -attachment => q{}, -charset => q{},
    -cookie => q{}, -nph => q{},
    -set_cookie => q{-cookie}, -target => q{},
    -type => q{}, -window_target => q{-target},
);

sub _normalize {
    my $class = shift;
    my $field = lc shift;

    # transliterate dashes into underscores
    $field =~ tr{-}{_};

    # add an initial dash
    $field = "-$field";

    exists $norm_of{$field} ? $norm_of{ $field } : $field;
}

my %field_name_of = (
    -attachment => 'Content-Disposition', -cookie => 'Set-Cookie',
    -p3p => 'P3P', -target => 'Window-Target',
    -type => 'Content-Type',
);

sub _denormalize {
    my ( $class, $norm ) = @_;

    unless ( exists $field_name_of{$norm} ) {
        ( my $field = $norm ) =~ s/^-//;
        $field =~ tr/_/-/;
        $field_name_of{ $norm } = ucfirst $field;
    }

    $field_name_of{ $norm };
}

1;

__END__

=head1 NAME

CGI::Header - Emulates CGI::header()

=head1 SYNOPSIS

use CGI::Header;

my $header = CGI::Header->new(
-attachment => 'genome.jpg',
-charset => 'utf-8',
-cookie => 'ID=123456; path=/',
-expires => '+3M',
-nph => 1,
-p3p => [qw/CAO DSP LAW CURa/],
-target => 'ResultsWindow',
-type => 'text/plain',
);

$header->set( 'Content-Length' => 12345 );
$header->delete( 'Content-Disposition' );
my $value = $header->get( 'Status' );
my $bool = $header->exists( 'ETag' );

$header->attachment( 'genome.jpg' );
$header->expires( '+3M' );
$header->nph( 1 );
$header->p3p_tags(qw/CAO DSP LAW CURa/);
$header->target( 'ResultsWindow' );

=head1 DESCRIPTION

Accepts the same arguments as CGI::header() does.
Generates the same HTTP response headers as the subroutine does.

=head2 METHODS

=over 4

=item $header = CGI::Header->new( -type => 'text/plain', ... )

Construct a new CGI::Header object.
You might pass some initial attribute-value pairs as parameters to
the constructor:

my $header = CGI::Header->new(
-attachment => 'genome.jpg',
-charset => 'utf-8',
-cookie => 'ID=123456; path=/',
-expires => '+3M',
-nph => 1,
-p3p => [qw/CAO DSP LAW CURa/],
-target => 'ResultsWindow',
-type => 'text/plain',
);

=item $value = $eader->get( $field )

=item $header->set( $field => $value )

=item $bool = $header->exists( $field )

Returns a Boolean value telling whether the specified field exists.

if ( $header->exists('ETag') ) {
...
}

=item $value = $header->delete( $field )

Deletes the specified field.

$header->delete( 'Content-Disposition' );
my $value = $header->delete( 'Content-Disposition' ); # inline

=item @fields = $header->field_names

Returns the list of field names present in the header.

my @fields = $header->field_names;
# => ( 'Set-Cookie', 'Content-length', 'Content-Type' )

=item $header->each( \&callback )

Apply a subroutine to each header field in turn.
The callback routine is called with two parameters;
the name of the field and a value.
Any return values of the callback routine are ignored.

my @lines;

$self->each(sub {
my ( $field, $value ) = @_;
push @lines, "$field: $value";
});

=item @headers = $header->flatten

Returns pairs of fields and values.

my @headers = $header->flatten;
# => ( 'Status', '304 Nod Modified', 'Content-Type', 'text/plain' )

=item $header->clear

This will remove all header fields.

=item $bool = $header->is_empty

Returns true if the header contains no key-value pairs.

$header->clear;

if ( $header->is_empty ) { # true
...
}

=item $clone = $header->clone

Returns a copy of this CGI::Header object.

=item $header->as_string

=item $header->as_string( $eol )

Returns the header fields as a formatted MIME header.
The optional C<$eol> parameter specifies the line ending sequence to use.
The default is C<\n>.

=back

=head1 AUTHOR

Ryo Anazawa (anazawa@cpan.org)

=head1 LICENSE

This module is free software; you can redistibute it and/or
modify it under the same terms as Perl itself. See L<perlartistic>.

=cut
Something went wrong with that request. Please try again.