<commit_before>package Combust::Control;
use strict;
use Exception::Class ('ControllerException');
use Apache::Request;
use Apache::Cookie;
use Apache::Constants qw(:common :response);
use Apache::File;
use Carp qw(confess cluck carp);
use Digest::SHA1 qw(sha1_hex);
use Encode qw(encode_utf8);
use base qw(Combust::Redirect);

# TODO: figure out why we use this; remove it if possible
require bytes;

use Exception::Class ('Ex_ServerError');

use Apache::Util qw();

use Combust::Cache;
use Combust::Template;
use Combust::Cookies;
use Combust::Secret qw(get_secret);

use Combust::Config;

my $config = Combust::Config->new();

sub config { $config }

my $root = $config->root;

sub r {
  my $self = shift;
  # some day we'll deprecate this - it only works for Apache13 and Apache2
  return $self->request->_r;
}

sub req_param {
  my $self = shift;
  $self->request->req_param(@_);
}

sub param  { cluck "param() deprecated; use tpl_param()"; tpl_param(@_) }
sub params { cluck "params() deprecated; use tpl_params()"; tpl_params(@_) }

sub tpl_param {
  my ($self, $key) = (shift, shift);
  return unless $key;
  #Carp::cluck "param('$key' ...) called" if $key eq "user";
  $self->{params}->{$key} = shift if @_;
  return $self->{params}->{$key};
}

sub tpl_params {
  my $self = shift;
  cluck("tpl_params called with [$self] as self.  Did you configure the handler to call ->handler instead of ->super?")
    unless ref $self;
  cluck('Combust::Control->tpl_params called with parameters, did you mean to call "param"?') if @_;
  $self->{params} || {};
}

sub new {
  my ($class, $r) = @_;

  # return if we are already blessed
  return $class if ref $class;

  my $self = bless( { } , $class);
  
  $self;
}

sub super ($$) {
  my $class   = shift;
  my $r = shift;

  confess(__PACKAGE__ . '->super got called without $r') unless $r;
  return unless $r;

  my $self = $class->new($r);
  $self->tt->set_include_path(sub { $self->get_include_path });

  my $status;
  
  eval {
    $status = OK;
    $status = $self->init if $self->can('init');
  };
  if ($@) {
    cluck "$self->init died: $@";
    return SERVER_ERROR;
  }
  return $status unless $status == OK;

  $self->redirect_check;

  eval {
      ($status) = $self->handler($self->r);
  };
  cluck "Combust::Control: oops, class handler died with: $@" if $@;
  return SERVER_ERROR if $@;

  # should we do this to make it harder for people to shoot themselves in the foot?
  # $self->_cleanup_params;

  return $status;
}

sub handler {
  my $self = shift;
  unless ($self->can('render')) {
    my $msg = "$self doesn't have a render method; you probably got the inheritance order messed up somewhere.";
    warn $msg;
    die $msg;
  }
  my ($status, $output, $content_type) = $self->do_request();
  # have to return 'OK' and fake it with r->status or some such to make a custom 404 easily
  return $status unless $status == OK;
  my @r = $self->send_output($output, $content_type);
  if ($self->can('cleanup')) {
      eval { $self->cleanup };
      warn "CLEANUP method failed: $@" if $@; 
  }
  return @r;
}

sub do_request {
  my $self = shift;

  my $cache_info = $self->cache_info || {};

  my ($status, $output, $cache);<commit_msg>avoid storing $self between requests in $ctemplate<commit_after>package Combust::Control;
use strict;
use Exception::Class ('ControllerException');
use Apache::Request;
use Apache::Cookie;
use Apache::Constants qw(:common :response);
use Apache::File;
use Carp qw(confess cluck carp);
use Digest::SHA1 qw(sha1_hex);
use Encode qw(encode_utf8);
use base qw(Combust::Redirect);

# TODO: figure out why we use this; remove it if possible
require bytes;

use Exception::Class ('Ex_ServerError');

use Apache::Util qw();

use Combust::Cache;
use Combust::Template;
use Combust::Cookies;
use Combust::Secret qw(get_secret);

use Combust::Config;

my $config = Combust::Config->new();

sub config { $config }

my $root = $config->root;

sub r {
  my $self = shift;
  # some day we'll deprecate this - it only works for Apache13 and Apache2
  return $self->request->_r;
}

sub req_param {
  my $self = shift;
  $self->request->req_param(@_);
}

sub param  { cluck "param() deprecated; use tpl_param()"; tpl_param(@_) }
sub params { cluck "params() deprecated; use tpl_params()"; tpl_params(@_) }

sub tpl_param {
  my ($self, $key) = (shift, shift);
  return unless $key;
  #Carp::cluck "param('$key' ...) called" if $key eq "user";
  $self->{params}->{$key} = shift if @_;
  return $self->{params}->{$key};
}

sub tpl_params {
  my $self = shift;
  cluck("tpl_params called with [$self] as self.  Did you configure the handler to call ->handler instead of ->super?")
    unless ref $self;
  cluck('Combust::Control->tpl_params called with parameters, did you mean to call "param"?') if @_;
  $self->{params} || {};
}

sub new {
  my ($class, $r) = @_;

  # return if we are already blessed
  return $class if ref $class;

  my $self = bless( { } , $class);
  
  $self;
}

sub super ($$) {
  my $class   = shift;
  my $r = shift;

  confess(__PACKAGE__ . '->super got called without $r') unless $r;
  return unless $r;

  my $self = $class->new($r);
  $self->tt->set_include_path($self->get_include_path);

  my $status;

  eval {
    $status = OK;
    $status = $self->init if $self->can('init');
  };
  if ($@) {
    cluck "$self->init died: $@";
    return SERVER_ERROR;
  }
  return $status unless $status == OK;

  eval {
      ($status) = $self->handler($self->r);
  };
  cluck "Combust::Control: oops, class handler died with: $@" if $@;
  return SERVER_ERROR if $@;

  # should we do this to make it harder for people to shoot themselves in the foot?
  # $self->_cleanup_params;

  return $status;
}

sub handler {
  my $self = shift;
  unless ($self->can('render')) {
    my $msg = "$self doesn't have a render method; you probably got the inheritance order messed up somewhere.";
    warn $msg;
    die $msg;
  }

  my $redir_status = $self->redirect_check;
  return $redir_status unless $redir_status == DECLINED;

  my ($status, $output, $content_type) = $self->do_request();
  # have to return 'OK' and fake it with r->status or some such to make a custom 404 easily
  return $status unless $status == OK;
  my @r = $self->send_output($output, $content_type);
  if ($self->can('cleanup')) {
      eval { $self->cleanup };
      warn "CLEANUP method failed: $@" if $@; 
  }
  return @r;
}

sub do_request {
  my $self = shift;

  my $cache_info = $self->cache_info || {};

  my ($status, $output, $cache);
{'fraction_non_alphanumeric': 0.1607307338041142, 'fraction_numerical': 0.00276327909118821, 'mean_word_length': 3.8187869822485205, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 69, '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': '1320807', 'n_tokens_mistral': 2290, 'n_tokens_neox': 2226, 'n_words': 715}