Skip to content

Instantly share code, notes, and snippets.

@anazawa
Created November 22, 2012 13:45
Show Gist options
  • Select an option

  • Save anazawa/4131249 to your computer and use it in GitHub Desktop.

Select an option

Save anazawa/4131249 to your computer and use it in GitHub Desktop.
Blosxom plugin: conditional_get
package conditional_get;
use strict;
use warnings;
use CGI::Header;
our $VERSION = '0.02';
sub start { $ENV{REQUEST_METHOD} =~ /^GET|HEAD$/ }
sub last {
my $h = CGI::Header->new( $blosxom::header );
return unless etag_matches($h) or not_modified_since($h);
$h->set( 'Status' => '304 Not Modified' );
$h->delete( $_ ) for qw(Content-Type Content-Disposition Content-Length);
# Truncate output
$blosxom::output = q{};
return;
}
no warnings 'uninitialized';
# > RFC 2616 14.25 says it's OK and expected to use 'eq' :)
# >> Note: When handling an If-Modified-Since header field, some
# >> servers will use an exact date comparison function, rather than a
# >> less-than function, for deciding whether to send a 304 ...
sub etag_matches {
my $etag = $_[0]->get( 'ETag' );
$etag && $etag eq _trim( $ENV{HTTP_IF_NONE_MATCH} );
}
sub not_modified_since {
my $last_modified = $_[0]->get( 'Last-Modified' );
$last_modified && $last_modified eq _trim( $ENV{HTTP_IF_MODIFIED_SINCE} );
}
# > IE sends wrong formatted value
# > i.e. "Thu, 03 Dec 2009 01:46:32 GMT; length=17936"
sub _trim {
my $str = shift;
$str =~ s/;.*$//;
$str;
}
1;
__END__
=head1 NAME
Blosxom Plug-in: conditional_get
=head1 VERSION
0.02
=head1 SYNOPSIS
Enables condtional GET and HEAD using C<If-None-Match> and
C<If-Modified-Since> header.
The application should set either or both of C<Last-Modified>
or C<ETag> response headers according to RFC 2616.
When either of the conditions is met, the response body is set
to be zero length and the status is set to 304 Not Modified.
NOTE: This plugin adds neither C<Last-Modified> nor
C<ETag> response headers.
=head1 INSTALLATION
Drop the conditional_get plug-in into your Blosxom plugins folder.
=head1 DEPENDENCIES
L<CGI::Header>
=head1 SEE ALSO
L<Plack::Middleware::ConditionalGET>
=head1 AUTHOR
Ryo Anazawa <[email protected]>
=head1 LICENSE
This module is free software; you can redistribute it and/or
modify it under the same terms as Perl itself. See L<perlartistic>.
=cut
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment