1 package Catalyst::Context;
5 use B::Hooks::EndOfScope ();
7 use Catalyst::Exception::Detach;
8 use Catalyst::Exception::Go;
10 use Catalyst::Request::Upload;
11 use Catalyst::Response;
14 use Text::SimpleTable ();
19 use Carp qw/croak carp shortmess/;
21 has stack => (is => 'ro', default => sub { [] });
22 has stash => (is => 'rw', default => sub { {} });
23 has state => (is => 'rw', default => 0);
24 has stats => (is => 'rw');
25 has action => (is => 'rw');
26 has counter => (is => 'rw', default => sub { {} });
27 has request => (is => 'rw', default => sub { $_[0]->request_class->new({}) }, required => 1, lazy => 1);
28 has response => (is => 'rw', default => sub { $_[0]->response_class->new({}) }, required => 1, lazy => 1);
29 has namespace => (is => 'rw');
32 has 'application' => (
79 sub depth { scalar @{ shift->stack || [] }; }
82 my $self = shift; return $self->request(@_);
85 my $self = shift; return $self->response(@_);
88 # For backwards compatibility
89 sub finalize_output { shift->finalize_body(@_) };
92 our $RECURSION = 1000;
96 =head2 INFORMATION ABOUT THE CURRENT REQUEST
100 Returns a L<Catalyst::Action> object for the current action, which
101 stringifies to the action name. See L<Catalyst::Action>.
105 Returns the namespace of the current action, i.e., the URI prefix
106 corresponding to the controller of the current action. For example:
108 # in Controller::Foo::Bar
109 $c->namespace; # returns 'foo/bar';
115 Returns the current L<Catalyst::Request> object, giving access to
116 information about the current client request (including parameters,
117 cookies, HTTP headers, etc.). See L<Catalyst::Request>.
119 =head2 REQUEST FLOW HANDLING
121 =head2 $c->forward( $action [, \@arguments ] )
123 =head2 $c->forward( $class, $method, [, \@arguments ] )
125 Forwards processing to another action, by its private name. If you give a
126 class name but no method, C<process()> is called. You may also optionally
127 pass arguments in an arrayref. The action will receive the arguments in
128 C<@_> and C<< $c->req->args >>. Upon returning from the function,
129 C<< $c->req->args >> will be restored to the previous values.
131 Any data C<return>ed from the action forwarded to, will be returned by the
134 my $foodata = $c->forward('/foo');
135 $c->forward('index');
136 $c->forward(qw/MyApp::Model::DBIC::Foo do_stuff/);
137 $c->forward('MyApp::View::TT');
139 Note that L<< forward|/"$c->forward( $action [, \@arguments ] )" >> implies
140 an C<< eval { } >> around the call (actually
141 L<< execute|/"$c->execute( $class, $coderef )" >> does), thus de-fatalizing
142 all 'dies' within the called action. If you want C<die> to propagate you
143 need to do something like:
146 die $c->error if $c->error;
148 Or make sure to always return true values from your actions and write
151 $c->forward('foo') || return;
153 Another note is that C<< $c->forward >> always returns a scalar because it
154 actually returns $c->state which operates in a scalar context.
155 Thus, something like:
159 in an action that is forwarded to is going to return a scalar,
160 i.e. how many items are in that array, which is probably not what you want.
161 If you need to return an array then return a reference to it,
164 $c->stash->{array} = \@array;
166 and access it from the stash.
170 sub forward { my $c = shift; no warnings 'recursion'; $c->dispatcher->forward( $c, @_ ) }
172 =head2 $c->detach( $action [, \@arguments ] )
174 =head2 $c->detach( $class, $method, [, \@arguments ] )
178 The same as L<< forward|/"$c->forward( $action [, \@arguments ] )" >>, but
179 doesn't return to the previous action when processing is finished.
181 When called with no arguments it escapes the processing chain entirely.
185 sub detach { my $c = shift; $c->dispatcher->detach( $c, @_ ) }
187 =head2 $c->visit( $action [, \@captures, \@arguments ] )
189 =head2 $c->visit( $class, $method, [, \@captures, \@arguments ] )
191 Almost the same as L<< forward|/"$c->forward( $action [, \@arguments ] )" >>,
192 but does a full dispatch, instead of just calling the new C<$action> /
193 C<< $class->$method >>. This means that C<begin>, C<auto> and the method
194 you go to are called, just like a new request.
196 In addition both C<< $c->action >> and C<< $c->namespace >> are localized.
197 This means, for example, that C<< $c->action >> methods such as
198 L<name|Catalyst::Action/name>, L<class|Catalyst::Action/class> and
199 L<reverse|Catalyst::Action/reverse> return information for the visited action
200 when they are invoked within the visited action. This is different from the
201 behavior of L<< forward|/"$c->forward( $action [, \@arguments ] )" >>, which
202 continues to use the $c->action object from the caller action even when
203 invoked from the callee.
205 C<< $c->stash >> is kept unchanged.
207 In effect, L<< visit|/"$c->visit( $action [, \@captures, \@arguments ] )" >>
208 allows you to "wrap" another action, just as it would have been called by
209 dispatching from a URL, while the analogous
210 L<< go|/"$c->go( $action [, \@captures, \@arguments ] )" >> allows you to
211 transfer control to another action as if it had been reached directly from a URL.
215 sub visit { my $c = shift; $c->dispatcher->visit( $c, @_ ) }
217 =head2 $c->go( $action [, \@captures, \@arguments ] )
219 =head2 $c->go( $class, $method, [, \@captures, \@arguments ] )
221 The relationship between C<go> and
222 L<< visit|/"$c->visit( $action [, \@captures, \@arguments ] )" >> is the same as
223 the relationship between
224 L<< forward|/"$c->forward( $class, $method, [, \@arguments ] )" >> and
225 L<< detach|/"$c->detach( $action [, \@arguments ] )" >>. Like C<< $c->visit >>,
226 C<< $c->go >> will perform a full dispatch on the specified action or method,
227 with localized C<< $c->action >> and C<< $c->namespace >>. Like C<detach>,
228 C<go> escapes the processing of the current request chain on completion, and
229 does not return to its caller.
233 sub go { my $c = shift; $c->dispatcher->go( $c, @_ ) }
239 Returns the current L<Catalyst::Response> object, see there for details.
243 Returns a hashref to the stash, which may be used to store data and pass
244 it between components during a request. You can also set hash keys by
245 passing arguments. The stash is automatically sent to the view. The
246 stash is cleared at the end of a request; it cannot be used for
247 persistent storage (for this you must use a session; see
248 L<Catalyst::Plugin::Session> for a complete system integrated with
251 $c->stash->{foo} = $bar;
252 $c->stash( { moose => 'majestic', qux => 0 } );
253 $c->stash( bar => 1, gorch => 2 ); # equivalent to passing a hashref
255 # stash is automatically passed to the view for use in a template
256 $c->forward( 'MyApp::View::TT' );
260 around stash => sub {
263 my $stash = $orig->($c);
265 my $new_stash = @_ > 1 ? {@_} : $_[0];
266 croak('stash takes a hash or hashref') unless ref $new_stash;
267 foreach my $key ( keys %$new_stash ) {
268 $stash->{$key} = $new_stash->{$key};
278 =head2 $c->error($error, ...)
280 =head2 $c->error($arrayref)
282 Returns an arrayref containing error messages. If Catalyst encounters an
283 error while processing a request, it stores the error in $c->error. This
284 method should only be used to store fatal error messages.
286 my @error = @{ $c->error };
290 $c->error('Something bad happened');
297 my $error = ref $_[0] eq 'ARRAY' ? $_[0] : [@_];
298 croak @$error unless ref $c;
299 push @{ $c->{error} }, @$error;
301 elsif ( defined $_[0] ) { $c->{error} = undef }
302 return $c->{error} || [];
308 Contains the return value of the last executed action.
309 Note that << $c->state >> operates in a scalar context which means that all
310 values it returns are scalar.
312 =head2 $c->clear_errors
314 Clear errors. You probably don't want to clear the errors unless you are
315 implementing a custom error screen.
317 This is equivalent to running
329 =head2 COMPONENT ACCESSORS
331 =head2 $c->controller($name)
333 Gets a L<Catalyst::Controller> instance by name.
335 $c->controller('Foo')->do_stuff;
337 If the name is omitted, will return the controller for the dispatched
340 If you want to search for controllers, pass in a regexp as the argument.
342 # find all controllers that start with Foo
343 my @foo_controllers = $c->controller(qr{^Foo});
349 my ( $c, $name, @args ) = @_;
352 my @result = $c->_comp_search_prefixes( $name, qw/Controller C/ );
353 return map { $c->_filter_component( $_, @args ) } @result if ref $name;
354 return $c->_filter_component( $result[ 0 ], @args );
357 return $c->component( $c->action->class );
360 =head2 $c->model($name)
362 Gets a L<Catalyst::Model> instance by name.
364 $c->model('Foo')->do_stuff;
366 Any extra arguments are directly passed to ACCEPT_CONTEXT.
368 If the name is omitted, it will look for
369 - a model object in $c->stash->{current_model_instance}, then
370 - a model name in $c->stash->{current_model}, then
371 - a config setting 'default_model', or
372 - check if there is only one model, and return it if that's the case.
374 If you want to search for models, pass in a regexp as the argument.
376 # find all models that start with Foo
377 my @foo_models = $c->model(qr{^Foo});
382 my ( $c, $name, @args ) = @_;
383 my $appclass = ref($c) || $c;
385 my @result = $c->_comp_search_prefixes( $name, qw/Model M/ );
386 return map { $c->_filter_component( $_, @args ) } @result if ref $name;
387 return $c->_filter_component( $result[ 0 ], @args );
391 return $c->stash->{current_model_instance}
392 if $c->stash->{current_model_instance};
393 return $c->model( $c->stash->{current_model} )
394 if $c->stash->{current_model};
396 return $c->model( $appclass->config->{default_model} )
397 if $appclass->config->{default_model};
399 my( $comp, $rest ) = $c->_comp_search_prefixes( undef, qw/Model M/);
402 $c->log->warn( Carp::shortmess('Calling $c->model() will return a random model unless you specify one of:') );
403 $c->log->warn( '* $c->config(default_model => "the name of the default model to use")' );
404 $c->log->warn( '* $c->stash->{current_model} # the name of the model to use for this request' );
405 $c->log->warn( '* $c->stash->{current_model_instance} # the instance of the model to use for this request' );
406 $c->log->warn( 'NB: in version 5.81, the "random" behavior will not work at all.' );
409 return $c->_filter_component( $comp );
413 =head2 $c->view($name)
415 Gets a L<Catalyst::View> instance by name.
417 $c->view('Foo')->do_stuff;
419 Any extra arguments are directly passed to ACCEPT_CONTEXT.
421 If the name is omitted, it will look for
422 - a view object in $c->stash->{current_view_instance}, then
423 - a view name in $c->stash->{current_view}, then
424 - a config setting 'default_view', or
425 - check if there is only one view, and return it if that's the case.
427 If you want to search for views, pass in a regexp as the argument.
429 # find all views that start with Foo
430 my @foo_views = $c->view(qr{^Foo});
435 my ( $c, $name, @args ) = @_;
438 my @result = $c->_comp_search_prefixes( $name, qw/View V/ );
439 return map { $c->_filter_component( $_, @args ) } @result if ref $name;
440 return $c->_filter_component( $result[ 0 ], @args );
444 return $c->stash->{current_view_instance}
445 if $c->stash->{current_view_instance};
446 return $c->view( $c->stash->{current_view} )
447 if $c->stash->{current_view};
449 return $c->view( $c->config->{default_view} )
450 if $c->config->{default_view};
451 my( $comp, $rest ) = $c->_comp_search_prefixes( undef, qw/View V/);
454 $c->log->warn( 'Calling $c->view() will return a random view unless you specify one of:' );
455 $c->log->warn( '* $c->config(default_view => "the name of the default view to use")' );
456 $c->log->warn( '* $c->stash->{current_view} # the name of the view to use for this request' );
457 $c->log->warn( '* $c->stash->{current_view_instance} # the instance of the view to use for this request' );
458 $c->log->warn( 'NB: in version 5.81, the "random" behavior will not work at all.' );
461 return $c->_filter_component( $comp );
464 =head2 UTILITY METHODS
466 =head2 $c->uri_for( $path?, @args?, \%query_values? )
468 =head2 $c->uri_for( $action, \@captures?, @args?, \%query_values? )
470 Constructs an absolute L<URI> object based on the application root, the
471 provided path, and the additional arguments and query parameters provided.
472 When used as a string, provides a textual URI.
474 If no arguments are provided, the URI for the current action is returned.
475 To return the current action and also provide @args, use
476 C<< $c->uri_for( $c->action, @args ) >>.
478 If the first argument is a string, it is taken as a public URI path relative
479 to C<< $c->namespace >> (if it doesn't begin with a forward slash) or
480 relative to the application root (if it does). It is then merged with
481 C<< $c->request->base >>; any C<@args> are appended as additional path
482 components; and any C<%query_values> are appended as C<?foo=bar> parameters.
484 If the first argument is a L<Catalyst::Action> it represents an action which
485 will have its path resolved using C<< $c->dispatcher->uri_for_action >>. The
486 optional C<\@captures> argument (an arrayref) allows passing the captured
487 variables that are needed to fill in the paths of Chained and Regex actions;
488 once the path is resolved, C<uri_for> continues as though a path was
489 provided, appending any arguments or parameters and creating an absolute
492 The captures for the current request can be found in
493 C<< $c->request->captures >>, and actions can be resolved using
494 C<< Catalyst::Controller->action_for($name) >>. If you have a private action
495 path, use C<< $c->uri_for_action >> instead.
497 # Equivalent to $c->req->uri
498 $c->uri_for($c->action, $c->req->captures,
499 @{ $c->req->args }, $c->req->params);
501 # For the Foo action in the Bar controller
502 $c->uri_for($c->controller('Bar')->action_for('Foo'));
504 # Path to a static resource
505 $c->uri_for('/static/images/logo.png');
510 my ( $c, $path, @args ) = @_;
512 if (blessed($path) && $path->isa('Catalyst::Controller')) {
513 $path = $path->path_prefix;
518 if ( blessed($path) ) { # action object
519 my $captures = [ map { s|/|%2F|; $_; }
520 ( scalar @args && ref $args[0] eq 'ARRAY'
524 $path = $c->dispatcher->uri_for_action($action, $captures);
525 if (not defined $path) {
526 $c->log->debug(qq/Can't find uri_for action '$action' @$captures/)
530 $path = '/' if $path eq '';
533 undef($path) if (defined $path && $path eq '');
536 ( scalar @args && ref $args[$#args] eq 'HASH' ? pop @args : {} );
538 carp "uri_for called with undef argument" if grep { ! defined $_ } @args;
539 s/([^$URI::uric])/$URI::Escape::escapes{$1}/go for @args;
542 unshift(@args, $path);
544 unless (defined $path && $path =~ s!^/!!) { # in-place strip
545 my $namespace = $c->namespace;
546 if (defined $path) { # cheesy hack to handle path '../foo'
547 $namespace =~ s{(?:^|/)[^/]+$}{} while $args[0] =~ s{^\.\./}{};
549 unshift(@args, $namespace || '');
552 # join args with '/', or a blank string
553 my $args = join('/', grep { defined($_) } @args);
554 $args =~ s/\?/%3F/g; # STUPID STUPID SPECIAL CASE
556 my $base = $c->req->base;
557 my $class = ref($base);
558 $base =~ s{(?<!/)$}{/};
562 if (my @keys = keys %$params) {
563 # somewhat lifted from URI::_query's query_form
564 $query = '?'.join('&', map {
565 my $val = $params->{$_};
566 s/([;\/?:@&=+,\$\[\]%])/$URI::Escape::escapes{$1}/go;
569 $val = '' unless defined $val;
572 utf8::encode( $param ) if utf8::is_utf8($param);
573 # using the URI::Escape pattern here so utf8 chars survive
574 $param =~ s/([^A-Za-z0-9\-_.!~*'() ])/$URI::Escape::escapes{$1}/go;
576 "${key}=$param"; } ( ref $val eq 'ARRAY' ? @$val : $val ));
580 my $res = bless(\"${base}${args}${query}", $class);
584 =head2 $c->uri_for_action( $path, \@captures?, @args?, \%query_values? )
586 =head2 $c->uri_for_action( $action, \@captures?, @args?, \%query_values? )
592 A private path to the Catalyst action you want to create a URI for.
594 This is a shortcut for calling C<< $c->dispatcher->get_action_by_path($path)
595 >> and passing the resulting C<$action> and the remaining arguments to C<<
598 You can also pass in a Catalyst::Action object, in which case it is passed to
606 my ( $c, $path, @args ) = @_;
607 my $action = blessed($path)
609 : $c->dispatcher->get_action_by_path($path);
610 unless (defined $action) {
611 croak "Can't find action for path '$path'";
613 return $c->uri_for( $action, @args );
616 =head1 INTERNAL METHODS
620 Returns a hashref containing coderefs and execution counts (needed for
621 deep recursion detection).
625 Returns the number of actions on the current internal execution stack.
629 Dispatches a request to actions.
633 sub dispatch { my $c = shift; $c->dispatcher->dispatch( $c, @_ ) }
635 =head2 $c->dump_these
637 Returns a list of 2-element array references (name, structure) pairs
638 that will be dumped on the error page in debug mode.
644 [ Request => $c->req ],
645 [ Response => $c->res ],
646 [ Stash => $c->stash ],
647 [ Config => $c->config ];
650 =head2 $c->execute( $class, $coderef )
652 Execute a coderef in given class and catch exceptions. Errors are available
658 my ( $c, $class, $code ) = @_;
659 $class = $c->component($class) || $class;
662 if ( $c->depth >= $RECURSION ) {
663 my $action = $code->reverse();
664 $action = "/$action" unless $action =~ /->/;
665 my $error = qq/Deep recursion detected calling "${action}"/;
666 $c->log->error($error);
672 my $stats_info = $c->_stats_start_execute( $code ) if $c->use_stats;
674 push( @{ $c->stack }, $code );
676 no warnings 'recursion';
677 eval { $c->state( $code->execute( $class, $c, @{ $c->req->args } ) || 0 ) };
679 $c->_stats_finish_execute( $stats_info ) if $c->use_stats and $stats_info;
681 my $last = pop( @{ $c->stack } );
683 if ( my $error = $@ ) {
684 if ( blessed($error) and $error->isa('Catalyst::Exception::Detach') ) {
685 $error->rethrow if $c->depth > 1;
687 elsif ( blessed($error) and $error->isa('Catalyst::Exception::Go') ) {
688 $error->rethrow if $c->depth > 0;
691 unless ( ref $error ) {
692 no warnings 'uninitialized';
694 my $class = $last->class;
695 my $name = $last->name;
696 $error = qq/Caught exception in $class->$name "$error"/;
705 sub _stats_start_execute {
706 my ( $c, $code ) = @_;
707 return if ( ( $code->name =~ /^_.*/ )
708 && ( !$c->config->{show_internal_actions} ) );
710 my $action_name = $code->reverse();
711 $c->counter->{$action_name}++;
713 my $action = $action_name;
714 $action = "/$action" unless $action =~ /->/;
716 # determine if the call was the result of a forward
717 # this is done by walking up the call stack and looking for a calling
718 # sub of Catalyst::forward before the eval
720 for my $index ( 2 .. 11 ) {
722 if ( ( caller($index) )[0] eq 'Catalyst'
723 && ( caller($index) )[3] eq '(eval)' );
725 if ( ( caller($index) )[3] =~ /forward$/ ) {
726 $callsub = ( caller($index) )[3];
727 $action = "-> $action";
732 my $uid = $action_name . $c->counter->{$action_name};
734 # is this a root-level call or a forwarded call?
735 if ( $callsub =~ /forward$/ ) {
736 my $parent = $c->stack->[-1];
738 # forward, locate the caller
739 if ( exists $c->counter->{"$parent"} ) {
742 parent => "$parent" . $c->counter->{"$parent"},
748 # forward with no caller may come from a plugin
767 sub _stats_finish_execute {
768 my ( $c, $info ) = @_;
769 $c->stats->profile( end => $info );
774 Finalizes the request.
781 for my $error ( @{ $c->error } ) {
782 $c->log->error($error);
785 # Allow engine to handle finalize flow (for POE)
786 my $engine = $c->engine;
787 if ( my $code = $engine->can('finalize') ) {
792 $c->finalize_uploads;
795 if ( $#{ $c->error } >= 0 ) {
799 $c->finalize_headers;
802 if ( $c->request->method eq 'HEAD' ) {
803 $c->response->body('');
810 my $elapsed = sprintf '%f', $c->stats->elapsed;
811 my $av = $elapsed == 0 ? '??' : sprintf '%.3f', 1 / $elapsed;
813 "Request took ${elapsed}s ($av/s)\n" . $c->stats->report . "\n" );
816 return $c->response->status;
819 =head2 $c->finalize_body
825 sub finalize_body { my $c = shift; $c->engine->finalize_body( $c, @_ ) }
827 =head2 $c->finalize_cookies
833 sub finalize_cookies { my $c = shift; $c->engine->finalize_cookies( $c, @_ ) }
835 =head2 $c->finalize_error
841 sub finalize_error { my $c = shift; $c->engine->finalize_error( $c, @_ ) }
843 =head2 $c->finalize_headers
849 sub finalize_headers {
852 my $response = $c->response; #accessor calls can add up?
854 # Check if we already finalized headers
855 return if $response->finalized_headers;
858 if ( my $location = $response->redirect ) {
859 $c->log->debug(qq/Redirecting to "$location"/) if $c->debug;
860 $response->header( Location => $location );
862 if ( !$response->has_body ) {
863 # Add a default body if none is already present
865 qq{<html><body><p>This item has moved <a href="$location">here</a>.</p></body></html>}
871 if ( $response->body && !$response->content_length ) {
873 # get the length from a filehandle
874 if ( blessed( $response->body ) && $response->body->can('read') )
876 my $stat = stat $response->body;
877 if ( $stat && $stat->size > 0 ) {
878 $response->content_length( $stat->size );
881 $c->log->warn('Serving filehandle without a content-length');
885 # everything should be bytes at this point, but just in case
886 $response->content_length( bytes::length( $response->body ) );
891 if ( $response->status =~ /^(1\d\d|[23]04)$/ ) {
892 $response->headers->remove_header("Content-Length");
896 $c->finalize_cookies;
898 $c->engine->finalize_headers( $c, @_ );
901 $response->finalized_headers(1);
904 =head2 $c->finalize_output
906 An alias for finalize_body.
908 =head2 $c->finalize_read
910 Finalizes the input after reading is complete.
914 sub finalize_read { my $c = shift; $c->engine->finalize_read( $c, @_ ) }
916 =head2 $c->finalize_uploads
918 Finalizes uploads. Cleans up any temporary files.
922 sub finalize_uploads { my $c = shift; $c->engine->finalize_uploads( $c, @_ ) }
924 =head2 $c->get_action( $action, $namespace )
926 Gets an action in a given namespace.
930 sub get_action { my $c = shift; $c->dispatcher->get_action(@_) }
932 =head2 $c->get_actions( $action, $namespace )
934 Gets all actions of a given name in a namespace and all parent
939 sub get_actions { my $c = shift; $c->dispatcher->get_actions( $c, @_ ) }
941 =head2 $c->prepare_action
943 Prepares action. See L<Catalyst::Dispatcher>.
947 sub prepare_action { my $c = shift; $c->dispatcher->prepare_action( $c, @_ ) }
949 =head2 $c->prepare_body
951 Prepares message body.
958 return if $c->request->_has_body;
960 # Initialize on-demand data
961 $c->engine->prepare_body( $c, @_ );
962 $c->prepare_parameters;
965 if ( $c->debug && keys %{ $c->req->body_parameters } ) {
966 my $t = Text::SimpleTable->new( [ 35, 'Parameter' ], [ 36, 'Value' ] );
967 for my $key ( sort keys %{ $c->req->body_parameters } ) {
968 my $param = $c->req->body_parameters->{$key};
969 my $value = defined($param) ? $param : '';
971 ref $value eq 'ARRAY' ? ( join ', ', @$value ) : $value );
973 $c->log->debug( "Body Parameters are:\n" . $t->draw );
977 =head2 $c->prepare_body_chunk( $chunk )
979 Prepares a chunk of data before sending it to L<HTTP::Body>.
981 See L<Catalyst::Engine>.
985 sub prepare_body_chunk {
987 $c->engine->prepare_body_chunk( $c, @_ );
990 =head2 $c->prepare_body_parameters
992 Prepares body parameters.
996 sub prepare_body_parameters {
998 $c->engine->prepare_body_parameters( $c, @_ );
1001 =head2 $c->prepare_connection
1003 Prepares connection.
1007 sub prepare_connection {
1009 $c->engine->prepare_connection( $c, @_ );
1012 =head2 $c->prepare_cookies
1018 sub prepare_cookies { my $c = shift; $c->engine->prepare_cookies( $c, @_ ) }
1020 =head2 $c->prepare_headers
1026 sub prepare_headers { my $c = shift; $c->engine->prepare_headers( $c, @_ ) }
1028 =head2 $c->prepare_parameters
1030 Prepares parameters.
1034 sub prepare_parameters {
1036 $c->prepare_body_parameters;
1037 $c->engine->prepare_parameters( $c, @_ );
1040 =head2 $c->prepare_path
1042 Prepares path and base.
1046 sub prepare_path { my $c = shift; $c->engine->prepare_path( $c, @_ ) }
1048 =head2 $c->prepare_query_parameters
1050 Prepares query parameters.
1054 sub prepare_query_parameters {
1057 $c->engine->prepare_query_parameters( $c, @_ );
1059 if ( $c->debug && keys %{ $c->request->query_parameters } ) {
1060 my $t = Text::SimpleTable->new( [ 35, 'Parameter' ], [ 36, 'Value' ] );
1061 for my $key ( sort keys %{ $c->req->query_parameters } ) {
1062 my $param = $c->req->query_parameters->{$key};
1063 my $value = defined($param) ? $param : '';
1065 ref $value eq 'ARRAY' ? ( join ', ', @$value ) : $value );
1067 $c->log->debug( "Query Parameters are:\n" . $t->draw );
1071 =head2 $c->prepare_read
1073 Prepares the input for reading.
1077 sub prepare_read { my $c = shift; $c->engine->prepare_read( $c, @_ ) }
1079 =head2 $c->prepare_request
1081 Prepares the engine request.
1085 sub prepare_request { my $c = shift; $c->engine->prepare_request( $c, @_ ) }
1087 =head2 $c->prepare_uploads
1093 sub prepare_uploads {
1096 $c->engine->prepare_uploads( $c, @_ );
1098 if ( $c->debug && keys %{ $c->request->uploads } ) {
1099 my $t = Text::SimpleTable->new(
1100 [ 12, 'Parameter' ],
1105 for my $key ( sort keys %{ $c->request->uploads } ) {
1106 my $upload = $c->request->uploads->{$key};
1107 for my $u ( ref $upload eq 'ARRAY' ? @{$upload} : ($upload) ) {
1108 $t->row( $key, $u->filename, $u->type, $u->size );
1111 $c->log->debug( "File Uploads are:\n" . $t->draw );
1115 =head2 $c->prepare_write
1117 Prepares the output for writing.
1121 sub prepare_write { my $c = shift; $c->engine->prepare_write( $c, @_ ) }
1123 =head2 $c->read( [$maxlength] )
1125 Reads a chunk of data from the request body. This method is designed to
1126 be used in a while loop, reading C<$maxlength> bytes on every call.
1127 C<$maxlength> defaults to the size of the request if not specified.
1129 You have to set C<< MyApp->config(parse_on_demand => 1) >> to use this
1132 Warning: If you use read(), Catalyst will not process the body,
1133 so you will not be able to access POST parameters or file uploads via
1134 $c->request. You must handle all body parsing yourself.
1138 sub read { my $c = shift; return $c->engine->read( $c, @_ ) }
1142 Returns an arrayref of the internal execution stack (actions that are
1143 currently executing).
1146 =head2 $c->write( $data )
1148 Writes $data to the output stream. When using this method directly, you
1149 will need to manually set the C<Content-Length> header to the length of
1150 your output data, if known.
1157 # Finalize headers if someone manually writes output
1158 $c->finalize_headers;
1160 return $c->engine->write( $c, @_ );
1164 __PACKAGE__->meta->make_immutable;
1172 Catalyst::Context - object for keeping request related state
1182 =head3 request_class
1186 =head3 response_class
1200 L<Catalyst>, L<Catalyst::Model>, L<Catalyst::View>, L<Catalyst::Controller>.
1204 Catalyst Contributors, see Catalyst.pm
1208 This library is free software. You can redistribute it and/or modify it under
1209 the same terms as Perl itself.