C::C::CGIBin - new release
[catagits/Catalyst-Controller-WrapCGI.git] / lib / Catalyst / Controller / CGIBin.pm
index 30f44ee..b31b0da 100644 (file)
@@ -1,10 +1,10 @@
 package Catalyst::Controller::CGIBin;
 
-use strict;
-use warnings;
-
-use MRO::Compat;
+use Moose;
 use mro 'c3';
+
+extends 'Catalyst::Controller::WrapCGI';
+
 use File::Slurp 'slurp';
 use File::Find::Rule ();
 use Catalyst::Exception ();
@@ -16,19 +16,17 @@ use IO::File ();
 use Carp;
 use namespace::clean -except => 'meta';
 
-use parent 'Catalyst::Controller::WrapCGI';
-
 =head1 NAME
 
 Catalyst::Controller::CGIBin - Serve CGIs from root/cgi-bin
 
 =head1 VERSION
 
-Version 0.006
+Version 0.007
 
 =cut
 
-our $VERSION = '0.006';
+our $VERSION = '0.007';
 
 =head1 SYNOPSIS
 
@@ -39,7 +37,7 @@ In your controller:
     use parent qw/Catalyst::Controller::CGIBin/;
 
     # example of a forward to /cgi-bin/hlagh/mtfnpy.cgi
-    sub dongs : Local Args(0) {
+    sub serve_cgi : Local Args(0) {
         my ($self, $c) = @_;
         $c->forward($self->cgi_action('hlagh/mtfnpy.cgi'));
     }
@@ -47,6 +45,7 @@ In your controller:
 In your .conf:
 
     <Controller::Foo>
+        cgi_root_path cgi-bin
         <CGI>
             username_field username # used for REMOTE_USER env var
             pass_env PERL5LIB
@@ -64,17 +63,13 @@ for every invocation. If this is something you need, let me know.
 
 CGI paths are converted into action names using cgi_action (below.)
 
-A path such as C<root/cgi-bin/hlagh/bar.cgi> will get the private path
-C<foo/CGI_hlagh_bar_cgi>, for controller Foo, with the C</>s converted to C<_>s
-and prepended with C<CGI_>, as well as all non-word characters converted to
-C<_>s. This is because L<Catalyst> action names can't have non-word characters
-in them.
-
 Inherits from L<Catalyst::Controller::WrapCGI>, see the documentation for that
 module for configuration information.
 
 =cut
 
+has cgi_root_path => (is => 'ro', isa => 'Str', default => 'cgi-bin');
+
 sub register_actions {
     my ($self, $app) = @_;
 
@@ -141,12 +136,21 @@ Takes a path to a CGI from C<root/cgi-bin> such as C<foo/bar.cgi> and returns
 the action name it is registered as. See L</DESCRIPTION> for a discussion on how
 CGI actions are named.
 
+A path such as C<root/cgi-bin/hlagh/bar.cgi> will get the private path
+C<foo/CGI_hlagh__bar_cgi>, for controller Foo, with the C</>s converted to C<__>
+and prepended with C<CGI_>, as well as all non-word characters converted to
+C<_>s. This is because L<Catalyst> action names can't have non-word characters
+in them.
+
+This means that C<foo/bar.cgi> and C<foo__bar.cgi> for example will both map to
+the action C<CGI_foo__bar_cgi> so B<DON'T DO THAT>.
+
 =cut
 
 sub cgi_action {
     my ($self, $cgi) = @_;
 
-    my $action_name = 'CGI_' . join '_' => split '/' => $cgi;
+    my $action_name = 'CGI_' . join '__' => split '/' => $cgi;
     $action_name    =~ s/\W/_/g;
 
     $action_name
@@ -157,13 +161,17 @@ sub cgi_action {
 Takes a path to a CGI from C<root/cgi-bin> such as C<foo/bar.cgi> and returns
 the public path it should be registered under.
 
-The default is C<cgi-bin/$cgi>.
+The default is to prefix with the C<cgi_root_path> config setting, or if not set
+uses C<cgi-bin/$cgi>.
 
 =cut
 
 sub cgi_path {
     my ($self, $cgi) = @_;
-    return "cgi-bin/$cgi";
+
+    my $root = $self->cgi_root_path;
+    $root =~ s{/*$}{};
+    return "$root/$cgi";
 }
 
 =head2 $self->is_perl_cgi($path)
@@ -200,12 +208,15 @@ sub is_perl_cgi {
 Takes the path to a Perl CGI and returns a coderef suitable for passing to
 cgi_to_response (from L<Catalyst::Controller::WrapCGI>.)
 
-C<$action_name> is the generated name for the action representing the CGI file.
+C<$action_name> is the generated name for the action representing the CGI file
+from C<cgi_action>.
 
 This is similar to how L<ModPerl::Registry> works, but will only work for
 well-written CGIs. Otherwise, you may have to override this method to do
 something more involved (see L<ModPerl::PerlRun>.)
 
+Scripts with C<__DATA__> sections now work too.
+
 =cut
 
 sub wrap_perl_cgi {
@@ -213,7 +224,7 @@ sub wrap_perl_cgi {
 
     my $code = slurp $cgi;
 
-    $code =~ s/^__DATA__\r?\n(.*)//ms;
+    $code =~ s/^__DATA__(?:\r?\n|\r\n?)(.*)//ms;
     my $data = $1;
 
     my $coderef = do {