<commit_before>## ----------------------------------------------------------------------------

package CGI::Conduit;

use Carp;
use Moose;
use Log::Log4perl qw(get_logger);
use URI::Escape;

# use the core roles
with qw(
    CGI::Conduit::Header
    CGI::Conduit::Status
    CGI::Conduit::Cfg
    CGI::Conduit::Cookie
    CGI::Conduit::Log
);

our $VERSION = '0.05';

## ----------------------------------------------------------------------------
# accessors

has 'cgi' => ( is => 'rw' );
has 'params_save' => ( is => 'rw' );
has 'res_content' => ( is => 'rw' );
has 'res_content_type' => ( is => 'rw' );
has 'rendered' => ( is => 'rw' );

## ----------------------------------------------------------------------------
# setup and initialisation

sub setup {
    my ($self, $cfg_filename, $log_filename) = @_;

    # check the config filename is given (log filename is optional)
    defined $cfg_filename
        or croak "Config filename not given";

    # check the config file exists
    -f $cfg_filename
        or croak "Config file doesn't exist: $!";

    # load up the config
    $self->cfg_load( $cfg_filename );

    # set the log file (whether it is defined or not)
    $self->log_filename( $log_filename );

    # finally, call the init function (which some roles hook to 'after')
    $self->init();
}

# this should eventually be moved to 'setup', though the roles then need
# changing so they hook into 'after setup' rather than 'after init'
sub init {
    my $self = shift;

    # 'setup_handlers' should be provided by the inheriting class
    $self->setup_handlers();
}

sub setup_handlers {
    die 'setup_handlers(): should be provided by the inheriting class';
}

sub clear {
    my $self = shift;
    $self->params_save(undef);
    delete $self->{res_content_type};
    $self->rendered(0);
}

## ----------------------------------------------------------------------------
# handle stuff

sub add_handler {
    my ($self, $match, $handler, $attr) = @_;

    # default to nothing
    $attr ||= {};

    push @{$self->{handler}}, { match => $match, name => $handler, attr => $attr };
}

sub handle {
    my ($self, $cgi) = @_;

    # clear everything we know then set the cgi object
    $self->clear();
    $self->cgi($cgi);

    eval {
        my ($handler, @captures) = $self->get_handler();

        if ( $handler ) {
            # we have something, but before we do anything, run the triggers
            my $done = 0;
            foreach my $before ( @{$handler->{attr}{before}} ) {
                next if $done;
                if ( $self->$before() ) {
                    $done = 1;
                }
            }

            # if we're already done, don't run the main handler
            unless ( $done ) {
                my $method = $handler->{name};
                $self->$method( @captures );
            }

            # run ALL of the after triggers
            foreach my $after ( @{$handler->{attr}{after}} ) {
                $self->$after();
            }
        }
        else {
            # if we are here, then we haven't been told what to do, 404 it
            $self->status_not_found();
        }
    };
    if ( $@ ) {
        # something went wrong, log it to both serverlog and ours and serve a 500
        my $msg = qq{Application died: $@};
        my $log = get_logger();
        $log->fatal( $msg );;
        warn $msg;
        $self->status_internal_server_error();
    }
}

sub get_handler {
    my ($self) = @_;

    my $path = $self->req_path;
    foreach my $handler ( @{$self->{handler}} ) {
        if ( ref $handler->{match} eq 'Regexp' ) {
            if ( my @matches = $path =~ $handler->{match} ) {
                @matches = ($path) unless @matches;
                $_ = uri_unescape($_)
                    for @matches;
                return ( $handler, @matches );
            }<commit_msg>Pass the captures to the before and after triggers<commit_after>## ----------------------------------------------------------------------------

package CGI::Conduit;

use Carp;
use Moose;
use Log::Log4perl qw(get_logger);
use URI::Escape;

# use the core roles
with qw(
    CGI::Conduit::Header
    CGI::Conduit::Status
    CGI::Conduit::Cfg
    CGI::Conduit::Cookie
    CGI::Conduit::Log
);

our $VERSION = '0.05';

## ----------------------------------------------------------------------------
# accessors

has 'cgi' => ( is => 'rw' );
has 'params_save' => ( is => 'rw' );
has 'res_content' => ( is => 'rw' );
has 'res_content_type' => ( is => 'rw' );
has 'rendered' => ( is => 'rw' );

## ----------------------------------------------------------------------------
# setup and initialisation

sub setup {
    my ($self, $cfg_filename, $log_filename) = @_;

    # check the config filename is given (log filename is optional)
    defined $cfg_filename
        or croak "Config filename not given";

    # check the config file exists
    -f $cfg_filename
        or croak "Config file doesn't exist: $!";

    # load up the config
    $self->cfg_load( $cfg_filename );

    # set the log file (whether it is defined or not)
    $self->log_filename( $log_filename );

    # finally, call the init function (which some roles hook to 'after')
    $self->init();
}

# this should eventually be moved to 'setup', though the roles then need
# changing so they hook into 'after setup' rather than 'after init'
sub init {
    my $self = shift;

    # 'setup_handlers' should be provided by the inheriting class
    $self->setup_handlers();
}

sub setup_handlers {
    die 'setup_handlers(): should be provided by the inheriting class';
}

sub clear {
    my $self = shift;
    $self->params_save(undef);
    delete $self->{res_content_type};
    $self->rendered(0);
}

## ----------------------------------------------------------------------------
# handle stuff

sub add_handler {
    my ($self, $match, $handler, $attr) = @_;

    # default to nothing
    $attr ||= {};

    push @{$self->{handler}}, { match => $match, name => $handler, attr => $attr };
}

sub handle {
    my ($self, $cgi) = @_;

    # clear everything we know then set the cgi object
    $self->clear();
    $self->cgi($cgi);

    eval {
        my ($handler, @captures) = $self->get_handler();

        if ( $handler ) {
            # we have something, but before we do anything, run the triggers
            my $done = 0;
            foreach my $before ( @{$handler->{attr}{before}} ) {
                next if $done;
                if ( $self->$before( @captures ) ) {
                    $done = 1;
                }
            }

            # if we're already done, don't run the main handler
            unless ( $done ) {
                my $method = $handler->{name};
                $self->$method( @captures );
            }

            # run ALL of the after triggers
            foreach my $after ( @{$handler->{attr}{after}} ) {
                $self->$after( @captures );
            }
        }
        else {
            # if we are here, then we haven't been told what to do, 404 it
            $self->status_not_found();
        }
    };
    if ( $@ ) {
        # something went wrong, log it to both serverlog and ours and serve a 500
        my $msg = qq{Application died: $@};
        my $log = get_logger();
        $log->fatal( $msg );;
        warn $msg;
        $self->status_internal_server_error();
    }
}

sub get_handler {
    my ($self) = @_;

    my $path = $self->req_path;
    foreach my $handler ( @{$self->{handler}} ) {
        if ( ref $handler->{match} eq 'Regexp' ) {
            if ( my @matches = $path =~ $handler->{match} ) {
                @matches = ($path) unless @matches;
                $_ = uri_unescape($_)
                    for @matches;
                return ( $handler, @matches );
            }
{'fraction_non_alphanumeric': 0.21053981279651238, 'fraction_numerical': 0.003333760738556225, 'mean_word_length': 1.6612077789150461, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 77, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '464757', 'n_tokens_mistral': 2327, 'n_tokens_neox': 2215, 'n_words': 698}