[Rt-commit] r16050 - in rt/3.8/trunk: lib/RT/Interface/Web lib/RT/Interface/Web/Mason share/html/NoAuth/images

ruz at bestpractical.com ruz at bestpractical.com
Thu Sep 25 09:04:12 EDT 2008


Author: ruz
Date: Thu Sep 25 09:04:10 2008
New Revision: 16050

Added:
   rt/3.8/trunk/lib/RT/Interface/Web/Mason/
   rt/3.8/trunk/lib/RT/Interface/Web/Mason/ApacheHandler.pm
   rt/3.8/trunk/lib/RT/Interface/Web/Mason/CGIHandler.pm
   rt/3.8/trunk/lib/RT/Interface/Web/Mason/HandlerMixin.pm
   rt/3.8/trunk/share/html/NoAuth/images/test.png
Modified:
   rt/3.8/trunk/lib/RT/Interface/Web/Handler.pm
   rt/3.8/trunk/t/web/basic.t

Log:
* don't try to compile images with mason
  Thanks to Hans Dieter Pearcey

Modified: rt/3.8/trunk/lib/RT/Interface/Web/Handler.pm
==============================================================================
--- rt/3.8/trunk/lib/RT/Interface/Web/Handler.pm	(original)
+++ rt/3.8/trunk/lib/RT/Interface/Web/Handler.pm	Thu Sep 25 09:04:10 2008
@@ -137,8 +137,12 @@
 =cut
 
 sub NewApacheHandler {
-    require HTML::Mason::ApacheHandler;
-    return NewHandler('HTML::Mason::ApacheHandler', args_method => "CGI", @_);
+    require RT::Interface::Web::Mason::ApacheHandler;
+    return NewHandler(
+        'RT::Interface::Web::Mason::ApacheHandler',
+        args_method => "CGI",
+        @_
+    );
 }
 
 # }}}
@@ -152,8 +156,8 @@
 =cut
 
 sub NewCGIHandler {
-    require HTML::Mason::CGIHandler;
-    return NewHandler('HTML::Mason::CGIHandler', @_);
+    require RT::Interface::Web::Mason::CGIHandler;
+    return NewHandler('RT::Interface::Web::Mason::CGIHandler', @_);
 }
 
 sub NewHandler {

Added: rt/3.8/trunk/lib/RT/Interface/Web/Mason/ApacheHandler.pm
==============================================================================
--- (empty file)
+++ rt/3.8/trunk/lib/RT/Interface/Web/Mason/ApacheHandler.pm	Thu Sep 25 09:04:10 2008
@@ -0,0 +1,26 @@
+package RT::Interface::Web::Mason::ApacheHandler;
+
+use strict;
+use warnings;
+
+use base qw(HTML::Mason::ApacheHandler);
+use RT::Interface::Web::Mason::HandlerMixin;
+
+sub handle_request {
+    my ($self, $r) = @_;
+
+    if (my ($file, $type) = $self->image_file_request($r->uri)) {
+        # is there a better way, like changing $r->filename and letting Apache
+        # send the file?
+        $r->content_type($type);
+        open my $fh, "<$file" or die "Can't open $file: $!";
+        binmode($fh);
+        local $/ = \16384;
+        $r->print($_) while <$fh>;
+        return;
+    }
+
+    $self->SUPER::handle_request($r);
+}
+
+1;

Added: rt/3.8/trunk/lib/RT/Interface/Web/Mason/CGIHandler.pm
==============================================================================
--- (empty file)
+++ rt/3.8/trunk/lib/RT/Interface/Web/Mason/CGIHandler.pm	Thu Sep 25 09:04:10 2008
@@ -0,0 +1,29 @@
+package RT::Interface::Web::Mason::CGIHandler;
+
+use strict;
+use warnings;
+
+use base qw(HTML::Mason::CGIHandler);
+use RT::Interface::Web::Mason::HandlerMixin;
+
+sub handle_cgi_object {
+    my ($self, $cgi) = @_;
+
+    if (my ($file, $type) = $self->image_file_request($cgi->path_info)) {
+        print "HTTP/1.0 200 OK\x0d\x0a";
+        print "Content-Type: $type\x0d\x0a";
+        print "Content-Length: " . (-s $file) . "\x0d\x0a\x0d\x0a";
+        open my $fh, "<$file" or die "Can't open $file: $!";
+        binmode($fh);
+        {
+            local $/ = \16384;
+            print $_ while <$fh>;
+        }
+        close $fh;
+        return;
+    }
+
+    $self->SUPER::handle_cgi_object($cgi);
+}
+
+1;

Added: rt/3.8/trunk/lib/RT/Interface/Web/Mason/HandlerMixin.pm
==============================================================================
--- (empty file)
+++ rt/3.8/trunk/lib/RT/Interface/Web/Mason/HandlerMixin.pm	Thu Sep 25 09:04:10 2008
@@ -0,0 +1,31 @@
+package RT::Interface::Web::Mason::HandlerMixin;
+
+use strict;
+use warnings;
+
+use Exporter ();
+our @ISA = qw(Exporter);
+
+our (@EXPORT_OK, @EXPORT);
+ at EXPORT_OK = @EXPORT = qw(image_file_request);
+
+sub image_file_request {
+    my ($self, $path) = @_;
+
+    return unless $path =~ /\.(gif|png|jpe?g)$/i;
+    my $type = "image/$1";
+    $type =~ s/jpg/jpeg/gi;
+
+    my $file;
+    for my $comp_root (map { $_->[1] } @{ $self->interp->comp_root }) {
+        my $tmp = File::Spec->catfile($comp_root, $path);
+        next unless -f $tmp;
+        $file = $tmp;
+        last;
+    }
+    return unless $file;
+
+    return ($file, $type);
+}
+
+1;

Added: rt/3.8/trunk/share/html/NoAuth/images/test.png
==============================================================================
--- (empty file)
+++ rt/3.8/trunk/share/html/NoAuth/images/test.png	Thu Sep 25 09:04:10 2008
@@ -0,0 +1,2 @@
+This file exists to support t/web/basic.t's image handling test.
+<& SYNTAX ERROR

Modified: rt/3.8/trunk/t/web/basic.t
==============================================================================
--- rt/3.8/trunk/t/web/basic.t	(original)
+++ rt/3.8/trunk/t/web/basic.t	Thu Sep 25 09:04:10 2008
@@ -1,7 +1,7 @@
 #!/usr/bin/perl
 
 use strict;
-use Test::More tests => 20;
+use Test::More tests => 21;
 use HTTP::Request::Common;
 use HTTP::Cookies;
 use LWP;
@@ -71,6 +71,21 @@
 
 # }}}
 
+# {{{ test an image
+
+$agent->get( $url."NoAuth/images/test.png" );
+my $file = RT::Test::get_relocatable_file(
+  File::Spec->catfile(
+    qw(.. .. share html NoAuth images test.png)
+  )
+);
+is(
+    length($agent->content),
+    -s $file,
+    "got a file of the correct size ($file)",
+);
+# }}}
+
 # {{{ Query Builder tests
 #
 # XXX: hey-ho, we have these tests in t/web/query-builder


More information about the Rt-commit mailing list