Catalyst-Runtime

 view release on metacpan or  search on metacpan

lib/Catalyst.pm  view on Meta::CPAN

                push @middleware, $mw;
            } else {
              die "I can't handle middleware definition ${\ref $next}";
            }
        } else {
          my $mw = $class->Catalyst::Utils::build_middleware($next);
          push @middleware, $mw;
        }
    }

    my @existing = @{$class->_psgi_middleware || []};
    $class->_psgi_middleware([@middleware,@existing,]);
}

=head2 registered_data_handlers

A read only copy of registered Data Handlers returned as a Hash, where each key
is a content type and each value is a subref that attempts to decode that content
type.

=head2 setup_data_handlers (?@data_handler)

Read configuration information stored in configuration key C<data_handlers> or
from passed @args.

See under L</CONFIGURATION> information regarding C<data_handlers>.

This method is automatically called during 'setup' of your application, so
you really don't need to invoke it.

=head2 default_data_handlers

Default Data Handlers that come bundled with L<Catalyst>.  Currently there are
only two default data handlers, for 'application/json' and an alternative to
'application/x-www-form-urlencoded' which supposed nested form parameters via
L<CGI::Struct> or via L<CGI::Struct::XS> IF you've installed it.

The 'application/json' data handler is used to parse incoming JSON into a Perl
data structure.  It uses L<JSON::MaybeXS>.  This allows you to fail back to
L<JSON::PP>, which is a Pure Perl JSON decoder, and has the smallest dependency
impact.

Because we don't wish to add more dependencies to L<Catalyst>, if you wish to
use this new feature we recommend installing L<Cpanel::JSON::XS> in order to get
the best performance.  You should add either to your dependency list
(Makefile.PL, dist.ini, cpanfile, etc.)

=cut

sub registered_data_handlers {
    my $class = shift;
    if(my $data_handlers = $class->_data_handlers) {
        return %$data_handlers;
    } else {
        $class->setup_data_handlers;
        return $class->registered_data_handlers;
    }
}

sub setup_data_handlers {
    my ($class, %data_handler_callbacks) = @_;
    %data_handler_callbacks = (
      %{$class->default_data_handlers},
      %{$class->config->{'data_handlers'}||+{}},
      %data_handler_callbacks);

    $class->_data_handlers(\%data_handler_callbacks);
}

sub default_data_handlers {
    my ($class) = @_;
    return +{
      'application/x-www-form-urlencoded' => sub {
          my ($fh, $req) = @_;
          my $params = $req->_use_hash_multivalue ? $req->body_parameters->mixed : $req->body_parameters;
          Class::Load::load_first_existing_class('CGI::Struct::XS', 'CGI::Struct')
            ->can('build_cgi_struct')->($params);
      },
      'application/json' => sub {
          my ($fh, $req) = @_;
          require JSON::MaybeXS;
          my $slurped;
          return eval {
            local $/;
            $slurped = $fh->getline;
            JSON::MaybeXS::decode_json($slurped); # decode_json does utf8 decoding for us
          } || Catalyst::Exception->throw(sprintf "Error Parsing POST '%s', Error: %s", (defined($slurped) ? $slurped : 'undef') ,$@);
        },
    };
}

sub _handle_http_exception {
    my ( $self, $error ) = @_;
    if (
           !$self->config->{always_catch_http_exceptions}
        && blessed $error
        && (
            $error->can('as_psgi')
            || (   $error->can('code')
                && $error->code =~ m/^[1-5][0-9][0-9]$/ )
        )
      )
    {
        return 1;
    }
}

=head2 $c->stack

Returns an arrayref of the internal execution stack (actions that are
currently executing).

=head2 $c->stats

Returns the current timing statistics object. By default Catalyst uses
L<Catalyst::Stats|Catalyst::Stats>, but can be set otherwise with
L<< stats_class|/"$c->stats_class" >>.

Even if L<< -Stats|/"-Stats" >> is not enabled, the stats object is still
available. By enabling it with C<< $c->stats->enabled(1) >>, it can be used to
profile explicitly, although MyApp.pm still won't profile nor output anything
by itself.

=head2 $c->stats_class

Returns or sets the stats (timing statistics) class. L<Catalyst::Stats|Catalyst::Stats> is used by default.



( run in 0.677 second using v1.01-cache-2.11-cpan-39bf76dae61 )