Skip to content

Commit

Permalink
Item12620: added support for if-modified-since headers
Browse files Browse the repository at this point in the history
git-svn-id: http://svn.foswiki.org/trunk/XSendFileContrib@17023 0b4bb1d4-4e5a-0410-9cc4-b2b747904278
  • Loading branch information
MichaelDaum authored and MichaelDaum committed Nov 1, 2013
1 parent fd31b79 commit 7a52638
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 14 deletions.
1 change: 1 addition & 0 deletions data/System/XSendFileContrib.txt
Expand Up @@ -113,6 +113,7 @@ One line description, required for extensions repository catalog.
| Dependencies: | %$DEPENDENCIES% |
| Version: | %$VERSION% |
| Change History: | <!-- versions below in reverse order -->&nbsp; |
| 01 Nov 2013 | added support for if-modified-since http headers |
| 22 May 2013 | using mime-magic as a fallback in case file extensions don't unveil the mime-type |
| 28 Mar 2013 | implemented {<nop>AccessRules} to allow any kind of access control list for attachments |
| 1.00: | Initial version |
Expand Down
33 changes: 19 additions & 14 deletions lib/Foswiki/Contrib/XSendFileContrib.pm
Expand Up @@ -21,10 +21,11 @@ use warnings;
use Encode ();
use Foswiki::Sandbox ();
use Foswiki::Func ();
use Foswiki::Time ();
use File::MMagic ();

our $VERSION = '2.10';
our $RELEASE = '2.10';
our $VERSION = '3.00';
our $RELEASE = '3.00';
our $SHORTDESCRIPTION = 'A viewfile replacement to send static files efficiently';
our $mimeTypeInfo;
our $mmagic;
Expand Down Expand Up @@ -128,22 +129,28 @@ sub xsendfile {
# construct file path to protected location
my $location = $Foswiki::cfg{XSendFileContrib}{Location} || $Foswiki::cfg{PubDir};
my $filePath = $location.'/'.$web.'/'.$topic.'/'.$fileName;
my @stat = stat($filePath);
my $lastModified = Foswiki::Time::formatTime($stat[9] || $stat[10] || 0, '$http', 'gmtime');
my $ifModifiedSince = $request->header('If-Modified-Since') || '';

# ok
my $headerName = $Foswiki::cfg{XSendFileContrib}{Header} || 'X-LIGHTTPD-send-file';

$fileName = Encode::encode_utf8($fileName);
$filePath = Encode::encode_utf8($filePath);

#print STDERR "fileName=$fileName\n";
#print STDERR "filePath=$filePath\n";

$response->header(
-status => 200,
-type => mimeTypeOfFile($filePath),
-content_disposition => "$dispositionMode; filename=\"$fileName\"",
$headerName => $filePath,
);
if ($lastModified eq $ifModifiedSince) {
$response->header(
-status => 304,
);
} else {
$response->header(
-status => 200,
-type => mimeTypeOfFile($filePath),
-content_disposition => "$dispositionMode; filename=\"$fileName\"",
-last_modified => $lastModified,
$headerName => $filePath,
);
}

# $response->print("OK");

Expand All @@ -153,8 +160,6 @@ sub xsendfile {
sub checkAccess {
my ($topicObject, $fileName, $user) = @_;



if (defined $Foswiki::cfg{XSendFileContrib}{AccessRules}) {
my $web = $topicObject->web;
my $topic = $topicObject->topic;
Expand Down

0 comments on commit 7a52638

Please sign in to comment.