Added SpeedyCGI, partly fixed HTTP::Daemon
[catagits/Catalyst-Runtime.git] / lib / Catalyst / Dispatcher.pm
CommitLineData
68a748b9 1package Catalyst::Dispatcher;
1abd6db7 2
3use strict;
4use base 'Class::Data::Inheritable';
f05af9ba 5use Catalyst::Utils;
1abd6db7 6use Text::ASCIITable;
1abd6db7 7use Tree::Simple;
8use Tree::Simple::Visitor::FindByPath;
9
10__PACKAGE__->mk_classdata($_) for qw/actions tree/;
11
99fe1710 12# These are the core structures
1abd6db7 13__PACKAGE__->actions(
14 { plain => {}, private => {}, regex => {}, compiled => [], reverse => {} }
15);
99fe1710 16
17# We use a tree
1abd6db7 18__PACKAGE__->tree( Tree::Simple->new( 0, Tree::Simple->ROOT ) );
19
1abd6db7 20=head1 NAME
21
9c053379 22Catalyst::Dispatcher - The Catalyst Dispatcher
1abd6db7 23
24=head1 SYNOPSIS
25
26See L<Catalyst>.
27
28=head1 DESCRIPTION
29
30=head1 METHODS
31
32=over 4
33
34=item $c->dispatch
35
36Dispatch request to actions.
37
38=cut
39
40sub dispatch {
41 my $c = shift;
42 my $action = $c->req->action;
43 my $namespace = '';
44 $namespace = ( join( '/', @{ $c->req->args } ) || '/' )
45 if $action eq 'default';
99fe1710 46
1abd6db7 47 unless ($namespace) {
48 if ( my $result = $c->get_action($action) ) {
f05af9ba 49 $namespace =
50 Catalyst::Utils::class2prefix( $result->[0]->[0]->[0] );
1abd6db7 51 }
52 }
99fe1710 53
1abd6db7 54 my $default = $action eq 'default' ? $namespace : undef;
55 my $results = $c->get_action( $action, $default );
56 $namespace ||= '/';
99fe1710 57
1abd6db7 58 if ( @{$results} ) {
59
60 # Execute last begin
61 $c->state(1);
62 if ( my $begin = @{ $c->get_action( 'begin', $namespace ) }[-1] ) {
63 $c->execute( @{ $begin->[0] } );
64 return if scalar @{ $c->error };
65 }
66
67 # Execute the auto chain
3e93ee3b 68 my $auto;
69 for $auto ( @{ $c->get_action( 'auto', $namespace ) } ) {
1abd6db7 70 $c->execute( @{ $auto->[0] } );
71 return if scalar @{ $c->error };
72 last unless $c->state;
73 }
74
75 # Execute the action or last default
3e93ee3b 76 my $mkay = defined $auto ? $c->state ? 1 : 0 : 1;
61cfdd57 77 if ( ( my $action = $c->req->action ) && $mkay ) {
1abd6db7 78 if ( my $result = @{ $c->get_action( $action, $default ) }[-1] ) {
79 $c->execute( @{ $result->[0] } );
80 }
81 }
82
83 # Execute last end
84 if ( my $end = @{ $c->get_action( 'end', $namespace ) }[-1] ) {
85 $c->execute( @{ $end->[0] } );
86 return if scalar @{ $c->error };
87 }
88 }
99fe1710 89
1abd6db7 90 else {
91 my $path = $c->req->path;
92 my $error = $path
93 ? qq/Unknown resource "$path"/
94 : "No default action defined";
95 $c->log->error($error) if $c->debug;
96 $c->error($error);
97 }
98}
99
100=item $c->forward($command)
101
102Forward processing to a private action or a method from a class.
103If you define a class without method it will default to process().
104
105 $c->forward('/foo');
106 $c->forward('index');
107 $c->forward(qw/MyApp::Model::CDBI::Foo do_stuff/);
108 $c->forward('MyApp::View::TT');
109
110=cut
111
112sub forward {
113 my $c = shift;
114 my $command = shift;
99fe1710 115
1abd6db7 116 unless ($command) {
117 $c->log->debug('Nothing to forward to') if $c->debug;
118 return 0;
119 }
99fe1710 120
1abd6db7 121 my $caller = caller(0);
122 my $namespace = '/';
99fe1710 123
1abd6db7 124 if ( $command =~ /^\// ) {
db0ca23c 125 $command =~ /^\/(.*)\/(\w+)$/;
1abd6db7 126 $namespace = $1 || '/';
db0ca23c 127 $command = $2 || $command;
128 $command =~ s/^\///;
1abd6db7 129 }
99fe1710 130
f05af9ba 131 else { $namespace = Catalyst::Utils::class2prefix($caller) || '/' }
99fe1710 132
1abd6db7 133 my $results = $c->get_action( $command, $namespace );
99fe1710 134
1abd6db7 135 unless ( @{$results} ) {
cd677e12 136 my $class = $command || '';
812a28c9 137 my $path = $class . '.pm';
fe14cf78 138 $path =~ s/::/\//g;
99fe1710 139
812a28c9 140 unless ( $INC{$path} ) {
141 my $error =
142 qq/Couldn't forward to "$class". Invalid or not loaded./;
3b2ed580 143 $c->error($error);
144 $c->log->debug($error) if $c->debug;
1abd6db7 145 return 0;
146 }
812a28c9 147
3245f607 148 unless ( UNIVERSAL::isa( $class, 'Catalyst::Base' ) ) {
812a28c9 149 my $error =
150 qq/Can't forward to "$class". Class is not a Catalyst component./;
3245f607 151 $c->error($error);
152 $c->log->debug($error) if $c->debug;
812a28c9 153 return 0;
3245f607 154 }
99fe1710 155
1abd6db7 156 my $method = shift || 'process';
99fe1710 157
1abd6db7 158 if ( my $code = $class->can($method) ) {
159 $c->actions->{reverse}->{"$code"} = "$class->$method";
160 $results = [ [ [ $class, $code ] ] ];
161 }
99fe1710 162
1abd6db7 163 else {
812a28c9 164 my $error =
165 qq/Couldn't forward to "$class". Does not implement "$method"/;
3b2ed580 166 $c->error($error);
167 $c->log->debug($error)
1abd6db7 168 if $c->debug;
169 return 0;
170 }
99fe1710 171
1abd6db7 172 }
99fe1710 173
1abd6db7 174 for my $result ( @{$results} ) {
175 $c->execute( @{ $result->[0] } );
176 return if scalar @{ $c->error };
177 last unless $c->state;
178 }
99fe1710 179
1abd6db7 180 return $c->state;
181}
182
183=item $c->get_action( $action, $namespace )
184
185Get an action in a given namespace.
186
187=cut
188
189sub get_action {
190 my ( $c, $action, $namespace ) = @_;
191 return [] unless $action;
192 $namespace ||= '';
99fe1710 193
1abd6db7 194 if ($namespace) {
195 $namespace = '' if $namespace eq '/';
196 my $parent = $c->tree;
197 my @results;
198 my %allowed = ( begin => 1, auto => 1, default => 1, end => 1 );
99fe1710 199
1abd6db7 200 if ( $allowed{$action} ) {
201 my $result = $c->actions->{private}->{ $parent->getUID }->{$action};
202 push @results, [$result] if $result;
203 my $visitor = Tree::Simple::Visitor::FindByPath->new;
99fe1710 204
1abd6db7 205 for my $part ( split '/', $namespace ) {
206 $visitor->setSearchPath($part);
207 $parent->accept($visitor);
208 my $child = $visitor->getResult;
209 my $uid = $child->getUID if $child;
210 my $match = $c->actions->{private}->{$uid}->{$action} if $uid;
211 push @results, [$match] if $match;
212 $parent = $child if $child;
213 }
99fe1710 214
1abd6db7 215 }
99fe1710 216
1abd6db7 217 else {
99fe1710 218
1abd6db7 219 if ($namespace) {
220 my $visitor = Tree::Simple::Visitor::FindByPath->new;
221 $visitor->setSearchPath( split '/', $namespace );
222 $parent->accept($visitor);
223 my $child = $visitor->getResult;
224 my $uid = $child->getUID if $child;
225 my $match = $c->actions->{private}->{$uid}->{$action}
226 if $uid;
227 push @results, [$match] if $match;
228 }
99fe1710 229
1abd6db7 230 else {
231 my $result =
232 $c->actions->{private}->{ $parent->getUID }->{$action};
233 push @results, [$result] if $result;
234 }
99fe1710 235
1abd6db7 236 }
237 return \@results;
238 }
99fe1710 239
1abd6db7 240 elsif ( my $p = $c->actions->{plain}->{$action} ) { return [ [$p] ] }
241 elsif ( my $r = $c->actions->{regex}->{$action} ) { return [ [$r] ] }
99fe1710 242
1abd6db7 243 else {
99fe1710 244
1abd6db7 245 for my $i ( 0 .. $#{ $c->actions->{compiled} } ) {
246 my $name = $c->actions->{compiled}->[$i]->[0];
247 my $regex = $c->actions->{compiled}->[$i]->[1];
99fe1710 248
2d752b2a 249 if ( my @snippets = ( $action =~ $regex ) ) {
1abd6db7 250 return [ [ $c->actions->{regex}->{$name}, $name, \@snippets ] ];
251 }
99fe1710 252
1abd6db7 253 }
254 }
255 return [];
256}
257
258=item $c->set_action( $action, $code, $namespace, $attrs )
259
260Set an action in a given namespace.
261
262=cut
263
264sub set_action {
265 my ( $c, $method, $code, $namespace, $attrs ) = @_;
266
f05af9ba 267 my $prefix = Catalyst::Utils::class2prefix($namespace) || '';
1abd6db7 268 my %flags;
269
270 for my $attr ( @{$attrs} ) {
271 if ( $attr =~ /^(Local|Relative)$/ ) { $flags{local}++ }
272 elsif ( $attr =~ /^(Global|Absolute)$/ ) { $flags{global}++ }
273 elsif ( $attr =~ /^Path\((.+)\)$/i ) { $flags{path} = $1 }
274 elsif ( $attr =~ /^Private$/i ) { $flags{private}++ }
275 elsif ( $attr =~ /^(Regex|Regexp)\((.+)\)$/i ) { $flags{regex} = $2 }
276 }
277
d0e524d2 278 if ( $flags{private} && ( keys %flags > 1 ) ) {
8d4e224b 279 $c->log->debug( 'Bad action definition "'
d0e524d2 280 . join( ' ', @{$attrs} )
8d4e224b 281 . qq/" for "$namespace->$method"/ )
282 if $c->debug;
d0e524d2 283 return;
284 }
1abd6db7 285 return unless keys %flags;
286
287 my $parent = $c->tree;
288 my $visitor = Tree::Simple::Visitor::FindByPath->new;
99fe1710 289
1abd6db7 290 for my $part ( split '/', $prefix ) {
291 $visitor->setSearchPath($part);
292 $parent->accept($visitor);
293 my $child = $visitor->getResult;
99fe1710 294
1abd6db7 295 unless ($child) {
296 $child = $parent->addChild( Tree::Simple->new($part) );
297 $visitor->setSearchPath($part);
298 $parent->accept($visitor);
299 $child = $visitor->getResult;
300 }
99fe1710 301
1abd6db7 302 $parent = $child;
303 }
99fe1710 304
1abd6db7 305 my $uid = $parent->getUID;
306 $c->actions->{private}->{$uid}->{$method} = [ $namespace, $code ];
307 my $forward = $prefix ? "$prefix/$method" : $method;
308
309 if ( $flags{path} ) {
310 $flags{path} =~ s/^\w+//;
311 $flags{path} =~ s/\w+$//;
312 if ( $flags{path} =~ /^'(.*)'$/ ) { $flags{path} = $1 }
313 if ( $flags{path} =~ /^"(.*)"$/ ) { $flags{path} = $1 }
314 }
99fe1710 315
1abd6db7 316 if ( $flags{regex} ) {
317 $flags{regex} =~ s/^\w+//;
318 $flags{regex} =~ s/\w+$//;
319 if ( $flags{regex} =~ /^'(.*)'$/ ) { $flags{regex} = $1 }
320 if ( $flags{regex} =~ /^"(.*)"$/ ) { $flags{regex} = $1 }
321 }
322
323 my $reverse = $prefix ? "$prefix/$method" : $method;
324
325 if ( $flags{local} || $flags{global} || $flags{path} ) {
99fe1710 326 my $path = $flags{path} || $method;
1abd6db7 327 my $absolute = 0;
99fe1710 328
1abd6db7 329 if ( $path =~ /^\/(.+)/ ) {
330 $path = $1;
331 $absolute = 1;
332 }
99fe1710 333
1abd6db7 334 $absolute = 1 if $flags{global};
335 my $name = $absolute ? $path : $prefix ? "$prefix/$path" : $path;
336 $c->actions->{plain}->{$name} = [ $namespace, $code ];
337 }
99fe1710 338
1abd6db7 339 if ( my $regex = $flags{regex} ) {
340 push @{ $c->actions->{compiled} }, [ $regex, qr#$regex# ];
341 $c->actions->{regex}->{$regex} = [ $namespace, $code ];
342 }
343
344 $c->actions->{reverse}->{"$code"} = $reverse;
345}
346
347=item $class->setup_actions($component)
348
349Setup actions for a component.
350
351=cut
352
353sub setup_actions {
354 my ( $self, $comps ) = @_;
99fe1710 355
1abd6db7 356 for my $comp (@$comps) {
357 $comp = ref $comp || $comp;
99fe1710 358
812a28c9 359 for my $action ( @{ Catalyst::Utils::reflect_actions($comp) } ) {
1abd6db7 360 my ( $code, $attrs ) = @{$action};
361 my $name = '';
362 no strict 'refs';
363 my @cache = ( $comp, @{"$comp\::ISA"} );
364 my %namespaces;
99fe1710 365
1abd6db7 366 while ( my $namespace = shift @cache ) {
367 $namespaces{$namespace}++;
368 for my $isa ( @{"$comp\::ISA"} ) {
369 next if $namespaces{$isa};
370 push @cache, $isa;
371 $namespaces{$isa}++;
372 }
373 }
99fe1710 374
1abd6db7 375 for my $namespace ( keys %namespaces ) {
99fe1710 376
1abd6db7 377 for my $sym ( values %{ $namespace . '::' } ) {
99fe1710 378
1abd6db7 379 if ( *{$sym}{CODE} && *{$sym}{CODE} == $code ) {
99fe1710 380
1abd6db7 381 $name = *{$sym}{NAME};
382 $self->set_action( $name, $code, $comp, $attrs );
383 last;
384 }
99fe1710 385
1abd6db7 386 }
99fe1710 387
1abd6db7 388 }
99fe1710 389
1abd6db7 390 }
99fe1710 391
1abd6db7 392 }
99fe1710 393
1abd6db7 394 my $actions = $self->actions;
395 my $privates = Text::ASCIITable->new;
5fbed090 396 $privates->setCols( 'Private', 'Class' );
397 $privates->setColWidth( 'Private', 36, 1 );
398 $privates->setColWidth( 'Class', 37, 1 );
99fe1710 399
1abd6db7 400 my $walker = sub {
401 my ( $walker, $parent, $prefix ) = @_;
402 $prefix .= $parent->getNodeValue || '';
403 $prefix .= '/' unless $prefix =~ /\/$/;
404 my $uid = $parent->getUID;
99fe1710 405
1abd6db7 406 for my $action ( keys %{ $actions->{private}->{$uid} } ) {
407 my ( $class, $code ) = @{ $actions->{private}->{$uid}->{$action} };
d36e81fd 408 $privates->addRow( "$prefix$action", $class );
1abd6db7 409 }
99fe1710 410
1abd6db7 411 $walker->( $walker, $_, $prefix ) for $parent->getAllChildren;
412 };
99fe1710 413
1abd6db7 414 $walker->( $walker, $self->tree, '' );
415 $self->log->debug( 'Loaded private actions', $privates->draw )
416 if ( @{ $privates->{tbl_rows} } && $self->debug );
99fe1710 417
1abd6db7 418 my $publics = Text::ASCIITable->new;
419 $publics->setCols( 'Public', 'Private' );
699e1247 420 $publics->setColWidth( 'Public', 36, 1 );
421 $publics->setColWidth( 'Private', 37, 1 );
99fe1710 422
1abd6db7 423 for my $plain ( sort keys %{ $actions->{plain} } ) {
424 my ( $class, $code ) = @{ $actions->{plain}->{$plain} };
7ace516e 425 my $reverse = $self->actions->{reverse}->{$code};
426 $reverse = $reverse ? "/$reverse" : $code;
cd677e12 427 $publics->addRow( "/$plain", $reverse );
1abd6db7 428 }
99fe1710 429
1abd6db7 430 $self->log->debug( 'Loaded public actions', $publics->draw )
431 if ( @{ $publics->{tbl_rows} } && $self->debug );
99fe1710 432
1abd6db7 433 my $regexes = Text::ASCIITable->new;
434 $regexes->setCols( 'Regex', 'Private' );
699e1247 435 $regexes->setColWidth( 'Regex', 36, 1 );
436 $regexes->setColWidth( 'Private', 37, 1 );
99fe1710 437
1abd6db7 438 for my $regex ( sort keys %{ $actions->{regex} } ) {
439 my ( $class, $code ) = @{ $actions->{regex}->{$regex} };
7ace516e 440 my $reverse = $self->actions->{reverse}->{$code};
441 $reverse = $reverse ? "/$reverse" : $code;
cd677e12 442 $regexes->addRow( $regex, $reverse );
1abd6db7 443 }
99fe1710 444
1abd6db7 445 $self->log->debug( 'Loaded regex actions', $regexes->draw )
446 if ( @{ $regexes->{tbl_rows} } && $self->debug );
447}
448
1abd6db7 449=back
450
451=head1 AUTHOR
452
453Sebastian Riedel, C<sri@cpan.org>
454
455=head1 COPYRIGHT
456
457This program is free software, you can redistribute it and/or modify it under
458the same terms as Perl itself.
459
460=cut
461
4621;