Minor change
[catagits/Catalyst-Controller-WrapCGI.git] / lib / Catalyst / Controller / CGIBin.pm
1 package Catalyst::Controller::CGIBin;
2
3 use strict;
4 use warnings;
5
6 use Class::C3;
7 use URI::Escape;
8 use File::Slurp 'slurp';
9 use File::Find::Rule ();
10 use Cwd;
11 use Catalyst::Exception ();
12 use File::Spec::Functions 'splitdir';
13
14 use parent 'Catalyst::Controller::WrapCGI';
15
16 =head1 NAME
17
18 Catalyst::Controller::CGIBin - Serve CGIs from root/cgi-bin
19
20 =head1 VERSION
21
22 Version 0.001
23
24 =cut
25
26 our $VERSION = '0.001';
27
28 =head1 SYNOPSIS
29
30 In your controller:
31
32     package MyApp::Controller::Foo;
33
34     use parent qw/Catalyst::Controller::CGIBin/;
35
36     # example of a forward to /cgi-bin/hlagh/mtfnpy.cgi
37     sub dongs : Local Args(0) {
38         my ($self, $c) = @_;
39         $c->forward($self->cgi_action('hlagh/mtfnpy.cgi'));
40     }
41
42 In your .conf:
43
44     <Controller::Foo>
45         <CGI>
46             username_field username # used for REMOTE_USER env var
47             pass_env PERL5LIB
48             pass_env PATH
49             pass_env /^MYAPP_/
50         </CGI>
51     </Controller::Foo>
52
53 =head1 DESCRIPTION
54
55 Dispatches to executable CGI files in root/cgi-bin for /cgi-bin/ paths.
56
57 CGI paths are converted into action names using cgi_action (below.)
58
59 A path such as C<root/cgi-bin/hlagh/bar.cgi> will get the private path
60 C<foo/CGI_hlagh_bar_cgi>, for controller Foo, with the C</>s converted to C<_>s
61 and prepended with C<CGI_>, as well as all non-word characters converted to
62 C<_>s. This is because L<Catalyst> action names can't have non-word characters
63 in them.
64
65 Inherits from L<Catalyst::Controller::WrapCGI>, see the documentation for that
66 module for configuration information.
67
68 =cut
69
70 sub register_actions {
71     my ($self, $app) = @_;
72
73     my $cwd = getcwd;
74
75     my $cgi_bin = $app->path_to('root', 'cgi-bin');
76
77     chdir $cgi_bin ||
78         Catalyst::Exception->throw(
79             message => 'You have no root/cgi-bin directory'
80         );
81
82     my $namespace = $self->action_namespace($app);
83
84     my $class = ref $self || $self;
85
86     for my $file (File::Find::Rule->executable->file->in(".")) {
87         my ($cgi, $type);
88         my $code = do { no warnings; eval 'sub { '.slurp($file).' }' };
89
90         if (!$@) {
91             $cgi = $code;
92             $type = 'Perl';
93         } else {
94             $cgi = sub { system "$cgi_bin/$file" };
95             $type = 'Non-Perl';
96             undef $@;
97         }
98
99         $app->log->info("Registering root/cgi_bin/$file as a $type CGI.")
100             if $app->debug;
101
102         my $action_name = $self->cgi_action($file);
103         my $path        = join '/' => splitdir($file);
104         my $reverse     = $namespace ? "$namespace/$action_name" : $action_name;
105         my $attrs       = { Path => [ "cgi-bin/$path" ], Args => [ 0 ] };
106
107         $code = sub {
108             my ($controller, $context) = @_;
109             $controller->cgi_to_response($context, $cgi)
110         };
111
112         my $action = $self->create_action(
113             name       => $action_name,
114             code       => $code,
115             reverse    => $reverse,
116             namespace  => $namespace,
117             class      => $class,
118             attributes => $attrs
119         );
120
121         $app->dispatcher->register($app, $action);
122     }
123
124     chdir $cwd;
125
126     $self->next::method($app, @_);
127 }
128
129 =head1 METHODS
130
131 =head2 $self->cgi_action($cgi_path)
132
133 Takes a path to a CGI from C<root/cgi-bin> such as C<foo/bar.cgi> and returns
134 the action name it is registered as.
135
136 =cut
137
138 sub cgi_action {
139     my ($self, $cgi) = @_;
140
141     my $action_name = 'CGI_' . join '_' => splitdir($cgi);
142     $action_name    =~ s/\W/_/g;
143
144     $action_name
145 }
146
147 =head1 AUTHOR
148
149 Rafael Kitover, C<< <rkitover at cpan.org> >>
150
151 =head1 BUGS
152
153 Please report any bugs or feature requests to C<bug-catalyst-controller-wrapcgi at
154 rt.cpan.org>, or through the web interface at
155 L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Catalyst-Controller-WrapCGI>.
156 I will be notified, and then you'll automatically be notified of progress on
157 your bug as I make changes.
158
159 =head1 SUPPORT
160
161 More information at:
162
163 =over 4
164
165 =item * RT: CPAN's request tracker
166
167 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Catalyst-Controller-WrapCGI>
168
169 =item * AnnoCPAN: Annotated CPAN documentation
170
171 L<http://annocpan.org/dist/Catalyst-Controller-WrapCGI>
172
173 =item * CPAN Ratings
174
175 L<http://cpanratings.perl.org/d/Catalyst-Controller-WrapCGI>
176
177 =item * Search CPAN
178
179 L<http://search.cpan.org/dist/Catalyst-Controller-WrapCGI>
180
181 =back
182
183 =head1 COPYRIGHT & LICENSE
184
185 Copyright (c) 2008 Rafael Kitover
186
187 This program is free software; you can redistribute it and/or modify it
188 under the same terms as Perl itself.
189
190 =cut
191
192 1; # End of Catalyst::Controller::CGIBin
193
194 # vim: expandtab shiftwidth=4 ts=4 tw=80: