1 package Catalyst::Context;
5 use B::Hooks::EndOfScope ();
6 use Catalyst::Exception;
7 use Catalyst::Exception::Detach;
8 use Catalyst::Exception::Go;
10 use Catalyst::Request::Upload;
11 use Catalyst::Response;
14 use Text::SimpleTable ();
15 use Path::Class::Dir ();
16 use Path::Class::File ();
20 use Tree::Simple::Visitor::FindByUID;
22 use Carp qw/croak carp shortmess/;
25 has action => (is => 'rw');
26 has counter => (is => 'rw', default => sub { {} });
27 has namespace => (is => 'rw');
28 has request_class => (is => 'ro', default => 'Catalyst::Request');
29 has request => (is => 'rw', default => sub { $_[0]->request_class->new({}) }, required => 1, lazy => 1);
30 has response_class => (is => 'ro', default => 'Catalyst::Response');
31 has response => (is => 'rw', default => sub { $_[0]->response_class->new({}) }, required => 1, lazy => 1);
32 has stack => (is => 'ro', default => sub { [] });
33 has stash => (is => 'rw', default => sub { {} });
34 has state => (is => 'rw', default => 0);
35 has stats => (is => 'rw');
37 has 'application' => (
82 sub depth { scalar @{ shift->stack || [] }; }
85 my $self = shift; return $self->request(@_);
88 my $self = shift; return $self->response(@_);
91 # For backwards compatibility
92 sub finalize_output { shift->finalize_body(@_) };
97 our $RECURSION = 1000;
98 our $DETACH = Catalyst::Exception::Detach->new;
99 our $GO = Catalyst::Exception::Go->new;
105 =head2 INFORMATION ABOUT THE CURRENT REQUEST
109 Returns a L<Catalyst::Action> object for the current action, which
110 stringifies to the action name. See L<Catalyst::Action>.
114 Returns the namespace of the current action, i.e., the URI prefix
115 corresponding to the controller of the current action. For example:
117 # in Controller::Foo::Bar
118 $c->namespace; # returns 'foo/bar';
124 Returns the current L<Catalyst::Request> object, giving access to
125 information about the current client request (including parameters,
126 cookies, HTTP headers, etc.). See L<Catalyst::Request>.
128 =head2 REQUEST FLOW HANDLING
130 =head2 $c->forward( $action [, \@arguments ] )
132 =head2 $c->forward( $class, $method, [, \@arguments ] )
134 Forwards processing to another action, by its private name. If you give a
135 class name but no method, C<process()> is called. You may also optionally
136 pass arguments in an arrayref. The action will receive the arguments in
137 C<@_> and C<< $c->req->args >>. Upon returning from the function,
138 C<< $c->req->args >> will be restored to the previous values.
140 Any data C<return>ed from the action forwarded to, will be returned by the
143 my $foodata = $c->forward('/foo');
144 $c->forward('index');
145 $c->forward(qw/MyApp::Model::DBIC::Foo do_stuff/);
146 $c->forward('MyApp::View::TT');
148 Note that L<< forward|/"$c->forward( $action [, \@arguments ] )" >> implies
149 an C<< eval { } >> around the call (actually
150 L<< execute|/"$c->execute( $class, $coderef )" >> does), thus de-fatalizing
151 all 'dies' within the called action. If you want C<die> to propagate you
152 need to do something like:
155 die $c->error if $c->error;
157 Or make sure to always return true values from your actions and write
160 $c->forward('foo') || return;
162 Another note is that C<< $c->forward >> always returns a scalar because it
163 actually returns $c->state which operates in a scalar context.
164 Thus, something like:
168 in an action that is forwarded to is going to return a scalar,
169 i.e. how many items are in that array, which is probably not what you want.
170 If you need to return an array then return a reference to it,
173 $c->stash->{array} = \@array;
175 and access it from the stash.
181 no warnings 'recursion';
182 my $dispatcher = $c->dispatcher;
183 $dispatcher->forward( $c, @_ );
186 =head2 $c->detach( $action [, \@arguments ] )
188 =head2 $c->detach( $class, $method, [, \@arguments ] )
192 The same as L<< forward|/"$c->forward( $action [, \@arguments ] )" >>, but
193 doesn't return to the previous action when processing is finished.
195 When called with no arguments it escapes the processing chain entirely.
199 sub detach { my $c = shift; $c->dispatcher->detach( $c, @_ ) }
201 =head2 $c->visit( $action [, \@captures, \@arguments ] )
203 =head2 $c->visit( $class, $method, [, \@captures, \@arguments ] )
205 Almost the same as L<< forward|/"$c->forward( $action [, \@arguments ] )" >>,
206 but does a full dispatch, instead of just calling the new C<$action> /
207 C<< $class->$method >>. This means that C<begin>, C<auto> and the method
208 you go to are called, just like a new request.
210 In addition both C<< $c->action >> and C<< $c->namespace >> are localized.
211 This means, for example, that C<< $c->action >> methods such as
212 L<name|Catalyst::Action/name>, L<class|Catalyst::Action/class> and
213 L<reverse|Catalyst::Action/reverse> return information for the visited action
214 when they are invoked within the visited action. This is different from the
215 behavior of L<< forward|/"$c->forward( $action [, \@arguments ] )" >>, which
216 continues to use the $c->action object from the caller action even when
217 invoked from the callee.
219 C<< $c->stash >> is kept unchanged.
221 In effect, L<< visit|/"$c->visit( $action [, \@captures, \@arguments ] )" >>
222 allows you to "wrap" another action, just as it would have been called by
223 dispatching from a URL, while the analogous
224 L<< go|/"$c->go( $action [, \@captures, \@arguments ] )" >> allows you to
225 transfer control to another action as if it had been reached directly from a URL.
229 sub visit { my $c = shift; $c->dispatcher->visit( $c, @_ ) }
231 =head2 $c->go( $action [, \@captures, \@arguments ] )
233 =head2 $c->go( $class, $method, [, \@captures, \@arguments ] )
235 The relationship between C<go> and
236 L<< visit|/"$c->visit( $action [, \@captures, \@arguments ] )" >> is the same as
237 the relationship between
238 L<< forward|/"$c->forward( $class, $method, [, \@arguments ] )" >> and
239 L<< detach|/"$c->detach( $action [, \@arguments ] )" >>. Like C<< $c->visit >>,
240 C<< $c->go >> will perform a full dispatch on the specified action or method,
241 with localized C<< $c->action >> and C<< $c->namespace >>. Like C<detach>,
242 C<go> escapes the processing of the current request chain on completion, and
243 does not return to its caller.
247 sub go { my $c = shift; $c->dispatcher->go( $c, @_ ) }
253 Returns the current L<Catalyst::Response> object, see there for details.
257 Returns a hashref to the stash, which may be used to store data and pass
258 it between components during a request. You can also set hash keys by
259 passing arguments. The stash is automatically sent to the view. The
260 stash is cleared at the end of a request; it cannot be used for
261 persistent storage (for this you must use a session; see
262 L<Catalyst::Plugin::Session> for a complete system integrated with
265 $c->stash->{foo} = $bar;
266 $c->stash( { moose => 'majestic', qux => 0 } );
267 $c->stash( bar => 1, gorch => 2 ); # equivalent to passing a hashref
269 # stash is automatically passed to the view for use in a template
270 $c->forward( 'MyApp::View::TT' );
274 around stash => sub {
277 my $stash = $orig->($c);
279 my $new_stash = @_ > 1 ? {@_} : $_[0];
280 croak('stash takes a hash or hashref') unless ref $new_stash;
281 foreach my $key ( keys %$new_stash ) {
282 $stash->{$key} = $new_stash->{$key};
292 =head2 $c->error($error, ...)
294 =head2 $c->error($arrayref)
296 Returns an arrayref containing error messages. If Catalyst encounters an
297 error while processing a request, it stores the error in $c->error. This
298 method should only be used to store fatal error messages.
300 my @error = @{ $c->error };
304 $c->error('Something bad happened');
311 my $error = ref $_[0] eq 'ARRAY' ? $_[0] : [@_];
312 croak @$error unless ref $c;
313 push @{ $c->{error} }, @$error;
315 elsif ( defined $_[0] ) { $c->{error} = undef }
316 return $c->{error} || [];
322 Contains the return value of the last executed action.
323 Note that << $c->state >> operates in a scalar context which means that all
324 values it returns are scalar.
326 =head2 $c->clear_errors
328 Clear errors. You probably don't want to clear the errors unless you are
329 implementing a custom error screen.
331 This is equivalent to running
342 =head2 COMPONENT ACCESSORS
344 =head2 $c->controller($name)
346 Gets a L<Catalyst::Controller> instance by name.
348 $c->controller('Foo')->do_stuff;
350 If the name is omitted, will return the controller for the dispatched
353 If you want to search for controllers, pass in a regexp as the argument.
355 # find all controllers that start with Foo
356 my @foo_controllers = $c->controller(qr{^Foo});
362 my ( $c, $name, @args ) = @_;
365 my @result = $c->_comp_search_prefixes( $name, qw/Controller C/ );
366 return map { $c->_filter_component( $_, @args ) } @result if ref $name;
367 return $c->_filter_component( $result[ 0 ], @args );
370 return $c->component( $c->action->class );
373 =head2 $c->model($name)
375 Gets a L<Catalyst::Model> instance by name.
377 $c->model('Foo')->do_stuff;
379 Any extra arguments are directly passed to ACCEPT_CONTEXT.
381 If the name is omitted, it will look for
382 - a model object in $c->stash->{current_model_instance}, then
383 - a model name in $c->stash->{current_model}, then
384 - a config setting 'default_model', or
385 - check if there is only one model, and return it if that's the case.
387 If you want to search for models, pass in a regexp as the argument.
389 # find all models that start with Foo
390 my @foo_models = $c->model(qr{^Foo});
395 my ( $c, $name, @args ) = @_;
396 my $appclass = ref($c) || $c;
398 my @result = $c->_comp_search_prefixes( $name, qw/Model M/ );
399 return map { $c->_filter_component( $_, @args ) } @result if ref $name;
400 return $c->_filter_component( $result[ 0 ], @args );
404 return $c->stash->{current_model_instance}
405 if $c->stash->{current_model_instance};
406 return $c->model( $c->stash->{current_model} )
407 if $c->stash->{current_model};
409 return $c->model( $c->config->{default_model} )
410 if $c->config->{default_model};
412 my( $comp, $rest ) = $c->_comp_search_prefixes( undef, qw/Model M/);
415 $c->log->warn( Carp::shortmess('Calling $c->model() will return a random model unless you specify one of:') );
416 $c->log->warn( '* $c->config(default_model => "the name of the default model to use")' );
417 $c->log->warn( '* $c->stash->{current_model} # the name of the model to use for this request' );
418 $c->log->warn( '* $c->stash->{current_model_instance} # the instance of the model to use for this request' );
419 $c->log->warn( 'NB: in version 5.81, the "random" behavior will not work at all.' );
422 return $c->_filter_component( $comp );
426 =head2 $c->view($name)
428 Gets a L<Catalyst::View> instance by name.
430 $c->view('Foo')->do_stuff;
432 Any extra arguments are directly passed to ACCEPT_CONTEXT.
434 If the name is omitted, it will look for
435 - a view object in $c->stash->{current_view_instance}, then
436 - a view name in $c->stash->{current_view}, then
437 - a config setting 'default_view', or
438 - check if there is only one view, and return it if that's the case.
440 If you want to search for views, pass in a regexp as the argument.
442 # find all views that start with Foo
443 my @foo_views = $c->view(qr{^Foo});
448 my ( $c, $name, @args ) = @_;
450 my $appclass = ref($c) || $c;
452 my @result = $c->_comp_search_prefixes( $name, qw/View V/ );
453 return map { $c->_filter_component( $_, @args ) } @result if ref $name;
454 return $c->_filter_component( $result[ 0 ], @args );
458 return $c->stash->{current_view_instance}
459 if $c->stash->{current_view_instance};
460 return $c->view( $c->stash->{current_view} )
461 if $c->stash->{current_view};
463 return $c->view( $c->config->{default_view} )
464 if $c->config->{default_view};
466 my( $comp, $rest ) = $c->_comp_search_prefixes( undef, qw/View V/);
469 $c->log->warn( 'Calling $c->view() will return a random view unless you specify one of:' );
470 $c->log->warn( '* $c->config(default_view => "the name of the default view to use")' );
471 $c->log->warn( '* $c->stash->{current_view} # the name of the view to use for this request' );
472 $c->log->warn( '* $c->stash->{current_view_instance} # the instance of the view to use for this request' );
473 $c->log->warn( 'NB: in version 5.81, the "random" behavior will not work at all.' );
476 return $c->_filter_component( $comp );
481 =head2 $c->uri_for( $path, @args?, \%query_values? )
483 =head2 $c->uri_for( $action, \@captures?, @args?, \%query_values? )
485 Constructs an absolute L<URI> object based on the application root, the
486 provided path, and the additional arguments and query parameters provided.
487 When used as a string, provides a textual URI.
489 If the first argument is a string, it is taken as a public URI path relative
490 to C<< $c->namespace >> (if it doesn't begin with a forward slash) or
491 relative to the application root (if it does). It is then merged with
492 C<< $c->request->base >>; any C<@args> are appended as additional path
493 components; and any C<%query_values> are appended as C<?foo=bar> parameters.
495 If the first argument is a L<Catalyst::Action> it represents an action which
496 will have its path resolved using C<< $c->dispatcher->uri_for_action >>. The
497 optional C<\@captures> argument (an arrayref) allows passing the captured
498 variables that are needed to fill in the paths of Chained and Regex actions;
499 once the path is resolved, C<uri_for> continues as though a path was
500 provided, appending any arguments or parameters and creating an absolute
503 The captures for the current request can be found in
504 C<< $c->request->captures >>, and actions can be resolved using
505 C<< Catalyst::Controller->action_for($name) >>. If you have a private action
506 path, use C<< $c->uri_for_action >> instead.
508 # Equivalent to $c->req->uri
509 $c->uri_for($c->action, $c->req->captures,
510 @{ $c->req->args }, $c->req->params);
512 # For the Foo action in the Bar controller
513 $c->uri_for($c->controller('Bar')->action_for('Foo'));
515 # Path to a static resource
516 $c->uri_for('/static/images/logo.png');
521 my ( $c, $path, @args ) = @_;
523 if (blessed($path) && $path->isa('Catalyst::Controller')) {
524 $path = $path->path_prefix;
529 if ( blessed($path) ) { # action object
530 my $captures = ( scalar @args && ref $args[0] eq 'ARRAY'
534 $path = $c->dispatcher->uri_for_action($action, $captures);
535 if (not defined $path) {
536 $c->log->debug(qq/Can't find uri_for action '$action' @$captures/)
540 $path = '/' if $path eq '';
543 undef($path) if (defined $path && $path eq '');
546 ( scalar @args && ref $args[$#args] eq 'HASH' ? pop @args : {} );
548 carp "uri_for called with undef argument" if grep { ! defined $_ } @args;
549 s/([^$URI::uric])/$URI::Escape::escapes{$1}/go for @args;
551 unshift(@args, $path);
553 unless (defined $path && $path =~ s!^/!!) { # in-place strip
554 my $namespace = $c->namespace;
555 if (defined $path) { # cheesy hack to handle path '../foo'
556 $namespace =~ s{(?:^|/)[^/]+$}{} while $args[0] =~ s{^\.\./}{};
558 unshift(@args, $namespace || '');
561 # join args with '/', or a blank string
562 my $args = join('/', grep { defined($_) } @args);
563 $args =~ s/\?/%3F/g; # STUPID STUPID SPECIAL CASE
565 my $base = $c->req->base;
566 my $class = ref($base);
567 $base =~ s{(?<!/)$}{/};
571 if (my @keys = keys %$params) {
572 # somewhat lifted from URI::_query's query_form
573 $query = '?'.join('&', map {
574 my $val = $params->{$_};
575 s/([;\/?:@&=+,\$\[\]%])/$URI::Escape::escapes{$1}/go;
578 $val = '' unless defined $val;
581 utf8::encode( $param ) if utf8::is_utf8($param);
582 # using the URI::Escape pattern here so utf8 chars survive
583 $param =~ s/([^A-Za-z0-9\-_.!~*'() ])/$URI::Escape::escapes{$1}/go;
585 "${key}=$param"; } ( ref $val eq 'ARRAY' ? @$val : $val ));
589 my $res = bless(\"${base}${args}${query}", $class);
593 =head2 $c->uri_for_action( $path, \@captures?, @args?, \%query_values? )
595 =head2 $c->uri_for_action( $action, \@captures?, @args?, \%query_values? )
601 A private path to the Catalyst action you want to create a URI for.
603 This is a shortcut for calling C<< $c->dispatcher->get_action_by_path($path)
604 >> and passing the resulting C<$action> and the remaining arguments to C<<
607 You can also pass in a Catalyst::Action object, in which case it is passed to
615 my ( $c, $path, @args ) = @_;
616 my $action = blessed($path)
618 : $c->dispatcher->get_action_by_path($path);
619 unless (defined $action) {
620 croak "Can't find action for path '$path'";
622 return $c->uri_for( $action, @args );
627 =head1 INTERNAL METHODS
631 Returns a hashref containing coderefs and execution counts (needed for
632 deep recursion detection).
636 Returns the number of actions on the current internal execution stack.
638 =head2 $c->dump_these
640 Returns a list of 2-element array references (name, structure) pairs
641 that will be dumped on the error page in debug mode.
647 [ Request => $c->req ],
648 [ Response => $c->res ],
649 [ Stash => $c->stash ],
650 [ Config => $c->config ];
654 =head2 $c->execute( $class, $coderef )
656 Execute a coderef in given class and catch exceptions. Errors are available
662 my ( $c, $class, $code ) = @_;
663 $class = $c->component($class) || $class;
666 if ( $c->depth >= $RECURSION ) {
667 my $action = $code->reverse();
668 $action = "/$action" unless $action =~ /->/;
669 my $error = qq/Deep recursion detected calling "${action}"/;
670 $c->log->error($error);
676 my $stats_info = $c->_stats_start_execute( $code ) if $c->use_stats;
678 push( @{ $c->stack }, $code );
680 no warnings 'recursion';
681 eval { $c->state( $code->execute( $class, $c, @{ $c->req->args } ) || 0 ) };
683 $c->_stats_finish_execute( $stats_info ) if $c->use_stats and $stats_info;
685 my $last = pop( @{ $c->stack } );
687 if ( my $error = $@ ) {
688 if ( blessed($error) and $error->isa('Catalyst::Exception::Detach') ) {
689 $error->rethrow if $c->depth > 1;
691 elsif ( blessed($error) and $error->isa('Catalyst::Exception::Go') ) {
692 $error->rethrow if $c->depth > 0;
695 unless ( ref $error ) {
696 no warnings 'uninitialized';
698 my $class = $last->class;
699 my $name = $last->name;
700 $error = qq/Caught exception in $class->$name "$error"/;
709 sub _stats_start_execute {
710 my ( $c, $code ) = @_;
711 my $appclass = ref($c) || $c;
712 return if ( ( $code->name =~ /^_.*/ )
713 && ( !$c->config->{show_internal_actions} ) );
715 my $action_name = $code->reverse();
716 $c->counter->{$action_name}++;
718 my $action = $action_name;
719 $action = "/$action" unless $action =~ /->/;
721 # determine if the call was the result of a forward
722 # this is done by walking up the call stack and looking for a calling
723 # sub of Catalyst::forward before the eval
725 for my $index ( 2 .. 11 ) {
727 if ( ( caller($index) )[0] eq 'Catalyst'
728 && ( caller($index) )[3] eq '(eval)' );
730 if ( ( caller($index) )[3] =~ /forward$/ ) {
731 $callsub = ( caller($index) )[3];
732 $action = "-> $action";
737 my $uid = $action_name . $c->counter->{$action_name};
739 # is this a root-level call or a forwarded call?
740 if ( $callsub =~ /forward$/ ) {
741 my $parent = $c->stack->[-1];
743 # forward, locate the caller
744 if ( exists $c->counter->{"$parent"} ) {
747 parent => "$parent" . $c->counter->{"$parent"},
753 # forward with no caller may come from a plugin
772 sub _stats_finish_execute {
773 my ( $c, $info ) = @_;
774 $c->stats->profile( end => $info );
779 Finalizes the request.
786 for my $error ( @{ $c->error } ) {
787 $c->log->error($error);
790 # Allow engine to handle finalize flow (for POE)
791 my $engine = $c->engine;
792 if ( my $code = $engine->can('finalize') ) {
797 $c->finalize_uploads;
800 if ( $#{ $c->error } >= 0 ) {
804 $c->finalize_headers;
807 if ( $c->request->method eq 'HEAD' ) {
808 $c->response->body('');
815 my $elapsed = sprintf '%f', $c->stats->elapsed;
816 my $av = $elapsed == 0 ? '??' : sprintf '%.3f', 1 / $elapsed;
818 "Request took ${elapsed}s ($av/s)\n" . $c->stats->report . "\n" );
821 return $c->response->status;
824 =head2 $c->finalize_body
830 sub finalize_body { my $c = shift; $c->engine->finalize_body( $c, @_ ) }
832 =head2 $c->finalize_cookies
838 sub finalize_cookies { my $c = shift; $c->engine->finalize_cookies( $c, @_ ) }
840 =head2 $c->finalize_error
846 sub finalize_error { my $c = shift; $c->engine->finalize_error( $c, @_ ) }
848 =head2 $c->finalize_headers
854 sub finalize_headers {
857 my $response = $c->response; #accessor calls can add up?
859 # Check if we already finalized headers
860 return if $response->finalized_headers;
863 if ( my $location = $response->redirect ) {
864 $c->log->debug(qq/Redirecting to "$location"/) if $c->debug;
865 $response->header( Location => $location );
867 if ( !$response->has_body ) {
868 # Add a default body if none is already present
870 qq{<html><body><p>This item has moved <a href="$location">here</a>.</p></body></html>}
876 if ( $response->body && !$response->content_length ) {
878 # get the length from a filehandle
879 if ( blessed( $response->body ) && $response->body->can('read') )
881 my $stat = stat $response->body;
882 if ( $stat && $stat->size > 0 ) {
883 $response->content_length( $stat->size );
886 $c->log->warn('Serving filehandle without a content-length');
890 # everything should be bytes at this point, but just in case
891 $response->content_length( bytes::length( $response->body ) );
896 if ( $response->status =~ /^(1\d\d|[23]04)$/ ) {
897 $response->headers->remove_header("Content-Length");
901 $c->finalize_cookies;
903 $c->engine->finalize_headers( $c, @_ );
906 $response->finalized_headers(1);
909 =head2 $c->finalize_output
911 An alias for finalize_body.
913 =head2 $c->finalize_read
915 Finalizes the input after reading is complete.
919 sub finalize_read { my $c = shift; $c->engine->finalize_read( $c, @_ ) }
921 =head2 $c->finalize_uploads
923 Finalizes uploads. Cleans up any temporary files.
927 sub finalize_uploads { my $c = shift; $c->engine->finalize_uploads( $c, @_ ) }
929 =head2 $c->get_action( $action, $namespace )
931 Gets an action in a given namespace.
935 sub get_action { my $c = shift; $c->dispatcher->get_action(@_) }
937 =head2 $c->get_actions( $action, $namespace )
939 Gets all actions of a given name in a namespace and all parent
944 sub get_actions { my $c = shift; $c->dispatcher->get_actions( $c, @_ ) }
948 Dispatches a request to actions.
954 my $dispatcher = $c->dispatcher;
955 $dispatcher->dispatch( $c, @_ )
958 =head2 $c->prepare_action
960 Prepares action. See L<Catalyst::Dispatcher>.
964 sub prepare_action { my $c = shift; $c->dispatcher->prepare_action( $c, @_ ) }
966 =head2 $c->prepare_body
968 Prepares message body.
975 return if $c->request->_has_body;
977 # Initialize on-demand data
978 $c->engine->prepare_body( $c, @_ );
979 $c->prepare_parameters;
982 if ( $c->debug && keys %{ $c->req->body_parameters } ) {
983 my $t = Text::SimpleTable->new( [ 35, 'Parameter' ], [ 36, 'Value' ] );
984 for my $key ( sort keys %{ $c->req->body_parameters } ) {
985 my $param = $c->req->body_parameters->{$key};
986 my $value = defined($param) ? $param : '';
988 ref $value eq 'ARRAY' ? ( join ', ', @$value ) : $value );
990 $c->log->debug( "Body Parameters are:\n" . $t->draw );
994 =head2 $c->prepare_body_chunk( $chunk )
996 Prepares a chunk of data before sending it to L<HTTP::Body>.
998 See L<Catalyst::Engine>.
1002 sub prepare_body_chunk {
1004 $c->engine->prepare_body_chunk( $c, @_ );
1007 =head2 $c->prepare_body_parameters
1009 Prepares body parameters.
1013 sub prepare_body_parameters {
1015 $c->engine->prepare_body_parameters( $c, @_ );
1018 =head2 $c->prepare_connection
1020 Prepares connection.
1024 sub prepare_connection {
1026 $c->engine->prepare_connection( $c, @_ );
1029 =head2 $c->prepare_cookies
1035 sub prepare_cookies { my $c = shift; $c->engine->prepare_cookies( $c, @_ ) }
1037 =head2 $c->prepare_headers
1043 sub prepare_headers { my $c = shift; $c->engine->prepare_headers( $c, @_ ) }
1045 =head2 $c->prepare_parameters
1047 Prepares parameters.
1051 sub prepare_parameters {
1053 $c->prepare_body_parameters;
1054 $c->engine->prepare_parameters( $c, @_ );
1057 =head2 $c->prepare_path
1059 Prepares path and base.
1063 sub prepare_path { my $c = shift; $c->engine->prepare_path( $c, @_ ) }
1065 =head2 $c->prepare_query_parameters
1067 Prepares query parameters.
1071 sub prepare_query_parameters {
1074 $c->engine->prepare_query_parameters( $c, @_ );
1076 if ( $c->debug && keys %{ $c->request->query_parameters } ) {
1077 my $t = Text::SimpleTable->new( [ 35, 'Parameter' ], [ 36, 'Value' ] );
1078 for my $key ( sort keys %{ $c->req->query_parameters } ) {
1079 my $param = $c->req->query_parameters->{$key};
1080 my $value = defined($param) ? $param : '';
1082 ref $value eq 'ARRAY' ? ( join ', ', @$value ) : $value );
1084 $c->log->debug( "Query Parameters are:\n" . $t->draw );
1088 =head2 $c->prepare_read
1090 Prepares the input for reading.
1094 sub prepare_read { my $c = shift; $c->engine->prepare_read( $c, @_ ) }
1096 =head2 $c->prepare_request
1098 Prepares the engine request.
1102 sub prepare_request { my $c = shift; $c->engine->prepare_request( $c, @_ ) }
1104 =head2 $c->prepare_uploads
1110 sub prepare_uploads {
1113 $c->engine->prepare_uploads( $c, @_ );
1115 if ( $c->debug && keys %{ $c->request->uploads } ) {
1116 my $t = Text::SimpleTable->new(
1117 [ 12, 'Parameter' ],
1122 for my $key ( sort keys %{ $c->request->uploads } ) {
1123 my $upload = $c->request->uploads->{$key};
1124 for my $u ( ref $upload eq 'ARRAY' ? @{$upload} : ($upload) ) {
1125 $t->row( $key, $u->filename, $u->type, $u->size );
1128 $c->log->debug( "File Uploads are:\n" . $t->draw );
1132 =head2 $c->prepare_write
1134 Prepares the output for writing.
1138 sub prepare_write { my $c = shift; $c->engine->prepare_write( $c, @_ ) }
1140 =head2 $c->request_class
1142 Returns or sets the request class.
1144 =head2 $c->response_class
1146 Returns or sets the response class.
1148 =head2 $c->read( [$maxlength] )
1150 Reads a chunk of data from the request body. This method is designed to
1151 be used in a while loop, reading C<$maxlength> bytes on every call.
1152 C<$maxlength> defaults to the size of the request if not specified.
1154 You have to set C<< MyApp->config(parse_on_demand => 1) >> to use this
1157 Warning: If you use read(), Catalyst will not process the body,
1158 so you will not be able to access POST parameters or file uploads via
1159 $c->request. You must handle all body parsing yourself.
1163 sub read { my $c = shift; return $c->engine->read( $c, @_ ) }
1171 sub run { my $c = shift; return $c->engine->run( $c, @_ ) }
1175 Returns an arrayref of the internal execution stack (actions that are
1176 currently executing).
1179 =head2 $c->write( $data )
1181 Writes $data to the output stream. When using this method directly, you
1182 will need to manually set the C<Content-Length> header to the length of
1183 your output data, if known.
1190 # Finalize headers if someone manually writes output
1191 $c->finalize_headers;
1193 return $c->engine->write( $c, @_ );
1198 __PACKAGE__->meta->make_immutable;
1206 Catalyst::Context - object for keeping request related state
1216 =head3 request_class
1220 =head3 response_class
1234 L<Catalyst>, L<Catalyst::Model>, L<Catalyst::View>, L<Catalyst::Controller>.
1238 Catalyst Contributors, see Catalyst.pm
1242 This library is free software. You can redistribute it and/or modify it under
1243 the same terms as Perl itself.