File assets should always give IO::File::WithPath objects to PSGI, instead of the current redirecting or streaming behavior. (#11688)

New API method:  WebGUI::Response::sendFile;  it, as appropriate, calls
setRedirect or setStreamedFile, depending on enableStreamingUploads config var.
setStreamedFile now kicks off the XSendfile process.
File.pm now uses this instead of trying to set both a redirect and a stream.
IO::File::WithPath blows up if a file doesn't exist so this raises an exception
now.
The http now no longer insist that '0' is not a valid filename to stream.
site.psgi, depending on enableStreamingUploads, enables either the Static
or XSendfile middleware.
This commit is contained in:
Scott Walters 2011-05-12 20:09:04 -04:00
parent 96bb194402
commit 7a994b59ce
6 changed files with 184 additions and 25 deletions

68
t/PSGI/Http.t Normal file
View file

@ -0,0 +1,68 @@
use strict;
use warnings;
use Test::More tests => 7;
use Plack::Test;
use Plack::Util;
use HTTP::Request::Common;
use WebGUI::Paths;
use WebGUI::Test;
# test things about responses
# this is like t/Session/Http.t but Plack specific
SKIP: {
skip 'set WEBGUI_LIVE to enable these tests', 7 unless $ENV{WEBGUI_LIVE};
my $session = WebGUI::Test->session;
my $prev_streaming_uploads = $session->config->get('enableStreamingUploads');
local $ENV{WEBGUI_CONFIG} = WebGUI::Test->file; # tell the share/site.psgi which site to load
#
# fire up a Plack to test streaming
#
$session->config->set('enableStreamingUploads', 1);
my $app = Plack::Util::load_psgi( WebGUI::Paths->defaultPSGI );
ok( $app, "created a PSGI app from app.psgi" );
test_psgi $app, sub {
my $cb = shift;
my $res = $cb->( GET "/root/import/gallery-templates/images/previous.gif" );
is $res->code, 200, 'enableStreamingUploads: 200 response';
is $res->header('Content-Type'), 'image/gif', '... content type is image/gif';
ok substr($res->content, 0, 100) =~ m/GIF89/, '... data contains the string GIF89';
};
#
# fire up another Plack to test non-streaming
#
$session->config->set('enableStreamingUploads', 0);
$app = Plack::Util::load_psgi( WebGUI::Paths->defaultPSGI );
my $redirect_url;
test_psgi $app, sub {
my $cb = shift;
my $res = $cb->( GET "/root/import/gallery-templates/images/previous.gif" );
is $res->code, 302, 'enableStreamingUploads: 302 response';
ok $res->header('Location'), '... Location header in response';
$res = $cb->(GET $res->header('Location') );
is $res->code, 200, '... following location, we get a 200 response code';
};
#
# put things back how they were
#
$session->config->set('enableStreamingUploads', $prev_streaming_uploads);
};

View file

@ -66,11 +66,22 @@ $response->status('200');
$http->setStreamedFile('');
is($http->getStreamedFile, undef, 'set/get StreamedFile: false values return undef, empty string');
$http->setStreamedFile(0);
$http->setStreamedFile(undef);
is($http->getStreamedFile, undef, 'set/get StreamedFile: false values return undef, empty string');
$http->setStreamedFile('/home/streaming');
is($http->getStreamedFile, '/home/streaming', 'set/get StreamedFile: set specific location and get it');
my $actual_file = $session->config->get('uploadsPath') . '/9e/a3/9ea37e148e517d4ae3d6326f691d848f/previous.gif'; # arbitrary file that exactually exists and hopefully will continue for a while
$http->setStreamedFile( $actual_file );
is($http->getStreamedFile, $actual_file, 'set/get StreamedFile: set specific location and get it');
do {
eval {
$http->setStreamedFile( $actual_file . '_but_actually_not_an_actual_file_because_someone_appended_a_bunch_of_bloody_garbage_to_it' );
};
my $e = WebGUI::Error->caught("WebGUI::Error::InvalidFile");
my $errorMessage = $e->error;
ok($errorMessage =~ m/No such file or directory/, "set/get StreamedFile: setting a non-existant file blows stuff up but that's okay because it's handled gracefully" );
};
$http->setStreamedFile('');
####################################################