Skip to content

Commit

Permalink
added experimental etag method to Mojo::Headers
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Nov 28, 2011
1 parent 7ab4592 commit 6533277
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 133 deletions.
3 changes: 3 additions & 0 deletions Changes
@@ -1,5 +1,8 @@
This file documents the revision history for Perl extension Mojolicious.

2.35 2011-11-29 00:00:00
- Added EXPERIMENTAL etag method to Mojo::Headers.

2.34 2011-11-28 00:00:00
- Added "websocket.pl" to example scripts.
- Improved documentation.
Expand Down
158 changes: 27 additions & 131 deletions lib/Mojo/Headers.pm
Expand Up @@ -5,74 +5,25 @@ use Mojo::Util 'get_line';

has max_line_size => sub { $ENV{MOJO_MAX_LINE_SIZE} || 10240 };

# Headers
my @GENERAL_HEADERS = qw/
Connection
Cache-Control
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 @WEBSOCKET_HEADERS = qw/
Sec-WebSocket-Accept
Sec-WebSocket-Key
Sec-WebSocket-Origin
Sec-WebSocket-Protocol
Sec-WebSocket-Version
/;
my @MISC_HEADERS = qw/DNT/;
my @HEADERS = (
@GENERAL_HEADERS, @REQUEST_HEADERS, @RESPONSE_HEADERS,
@ENTITY_HEADERS, @WEBSOCKET_HEADERS, @MISC_HEADERS
# Common headers
my @HEADERS = (
qw/Accept Accept-Language Accept-Ranges Authorization Connection/,
qw/Cache-Control Content-Disposition Content-Length Content-Range/,
qw/Content-Transfer-Encoding Content-Type Cookie DNT Date ETag Expect/,
qw/Expires Host If-Modified-Since Last-Modified Location/,
qw/Proxy-Authenticate Proxy-Authorization Range Sec-WebSocket-Accept/,
qw/Sec-WebSocket-Key Sec-WebSocket-Origin Sec-WebSocket-Protocol/,
qw/Sec-WebSocket-Version Server Set-Cookie Set-Cookie2 Trailer/,
qw/Transfer-Encoding Upgrade User-Agent WWW-Authenticate X-Forwarded-For/,
);

This comment has been minimized.

Copy link
@ssinyagin

ssinyagin Dec 15, 2011

this list is missing the Status header, and thus $res->headers->status(...) produces an error:
Can't locate object method "status" via package "Mojo::Headers"

{
no strict 'refs';
for my $header (@HEADERS) {
my $name = lc $header;
$name =~ s/-/_/g;
*{__PACKAGE__ . "::$name"} = sub { scalar shift->header($header => @_) };
}
}

# Lower case headers
my %NORMALCASE_HEADERS;
Expand All @@ -81,11 +32,6 @@ for my $name (@HEADERS) {
$NORMALCASE_HEADERS{$lowercase} = $name;
}

sub accept { scalar shift->header(Accept => @_) }
sub accept_language { scalar shift->header('Accept-Language' => @_) }
sub accept_ranges { scalar shift->header('Accept-Ranges' => @_) }
sub authorization { scalar shift->header(Authorization => @_) }

sub add {
my ($self, $name) = (shift, shift);

Expand All @@ -102,8 +48,6 @@ sub add {
return $self;
}

sub cache_control { scalar shift->header('Cache-Control' => @_) }

sub clone {
my $self = shift;
my $clone = $self->new;
Expand All @@ -112,22 +56,6 @@ sub clone {
return $clone;
}

sub connection { scalar shift->header(Connection => @_) }
sub content_disposition { scalar shift->header('Content-Disposition' => @_) }
sub content_length { scalar shift->header('Content-Length' => @_) }
sub content_range { scalar shift->header('Content-Range' => @_) }

sub content_transfer_encoding {
scalar shift->header('Content-Transfer-Encoding' => @_);
}

sub content_type { scalar shift->header('Content-Type' => @_) }
sub cookie { scalar shift->header(Cookie => @_) }
sub date { scalar shift->header(Date => @_) }
sub dnt { scalar shift->header(DNT => @_) }
sub expect { scalar shift->header(Expect => @_) }
sub expires { scalar shift->header(Expires => @_) }

sub from_hash {
my ($self, $hash) = (shift, shift);

Expand Down Expand Up @@ -160,9 +88,6 @@ sub header {
return @$headers;
}

sub host { scalar shift->header(Host => @_) }
sub if_modified_since { scalar shift->header('If-Modified-Since' => @_) }

# DEPRECATED in Leaf Fluttering In Wind!
sub is_done {
warn <<EOF;
Expand All @@ -175,12 +100,8 @@ sub is_finished { (shift->{state} || '') eq 'finished' }

sub is_limit_exceeded { shift->{limit} }

sub last_modified { scalar shift->header('Last-Modified' => @_) }

sub leftovers { delete shift->{buffer} }

sub location { scalar shift->header(Location => @_) }

sub names {
my @headers;
push @headers, $NORMALCASE_HEADERS{$_} || $_ for keys %{shift->{headers}};
Expand Down Expand Up @@ -228,40 +149,14 @@ sub parse {
return $self;
}

sub proxy_authenticate { scalar shift->header('Proxy-Authenticate' => @_) }
sub proxy_authorization { scalar shift->header('Proxy-Authorization' => @_) }
sub range { scalar shift->header(Range => @_) }
sub referrer { scalar shift->header(Referer => @_) }
sub referrer { scalar shift->header(Referer => @_) }

sub remove {
my ($self, $name) = @_;
delete $self->{headers}->{lc $name};
return $self;
}

sub sec_websocket_accept {
scalar shift->header('Sec-WebSocket-Accept' => @_);
}

sub sec_websocket_key { scalar shift->header('Sec-WebSocket-Key' => @_) }

sub sec_websocket_origin {
scalar shift->header('Sec-WebSocket-Origin' => @_);
}

sub sec_websocket_protocol {
scalar shift->header('Sec-WebSocket-Protocol' => @_);
}

sub sec_websocket_version {
scalar shift->header('Sec-WebSocket-Version' => @_);
}

sub server { scalar shift->header(Server => @_) }
sub set_cookie { scalar shift->header('Set-Cookie' => @_) }
sub set_cookie2 { scalar shift->header('Set-Cookie2' => @_) }
sub status { scalar shift->header(Status => @_) }

sub to_hash {
my $self = shift;
my %params = @_;
Expand Down Expand Up @@ -300,13 +195,6 @@ sub to_string {
return join "\x0d\x0a", @headers;
}

sub trailer { scalar shift->header(Trailer => @_) }
sub transfer_encoding { scalar shift->header('Transfer-Encoding' => @_) }
sub upgrade { scalar shift->header(Upgrade => @_) }
sub user_agent { scalar shift->header('User-Agent' => @_) }
sub www_authenticate { scalar shift->header('WWW-Authenticate' => @_) }
sub x_forwarded_for { scalar shift->header('X-Forwarded-For' => @_) }

1;
__END__
Expand Down Expand Up @@ -456,6 +344,14 @@ Shortcut for the C<Date> header.
Shortcut for the C<DNT> (Do Not Track) header.
Note that this method is EXPERIMENTAL and might change without warning!
=head2 C<etag>
my $etag = $headers->etag;
$headers = $headers->etag('abc321');
Shortcut for the C<ETag> header.
Note that this method is EXPERIMENTAL and might change without warning!
=head2 C<expect>
my $expect = $headers->expect;
Expand Down
2 changes: 1 addition & 1 deletion lib/Mojolicious.pm
Expand Up @@ -32,7 +32,7 @@ has static => sub { Mojolicious::Static->new };
has types => sub { Mojolicious::Types->new };

our $CODENAME = 'Leaf Fluttering In Wind';
our $VERSION = '2.34';
our $VERSION = '2.35';

# "These old doomsday devices are dangerously unstable.
# I'll rest easier not knowing where they are."
Expand Down
4 changes: 3 additions & 1 deletion t/mojo/headers.t
Expand Up @@ -2,7 +2,7 @@
use Mojo::Base -strict;

# "Remember, you can always find East by staring directly at the sun."
use Test::More tests => 47;
use Test::More tests => 48;

# "So, have a merry Christmas, a happy Hanukkah, a kwaazy Kwanza,
# a tip-top Tet, and a solemn, dignified, Ramadan.
Expand Down Expand Up @@ -36,6 +36,8 @@ $headers->expires('Thu, 01 Dec 1994 16:00:00 GMT');
$headers->cache_control('public');
is $headers->expires, 'Thu, 01 Dec 1994 16:00:00 GMT', 'right value';
is $headers->cache_control, 'public', 'right value';
$headers->etag('abc321');
is $headers->etag, 'abc321', 'right value';

# Clone
$headers = Mojo::Headers->new;
Expand Down

0 comments on commit 6533277

Please sign in to comment.