package Splitref::response;

use strict;
use warnings;

use Apache2::RequestRec(); # for $r->content_type
use Apache2::SubProcess();
#use Apache2::Const -compile => 'OK';

sub handler
{
  my $r = shift;

#  my $response = `/home/websites/splitreflection.com/perl_modules/suid-exec.pl`;
  $r->subprocess_env;
  my $command;
  if($r->dir_config("SUID") eq 'yes')
  {
    $command = "/home/websites/splitreflection.com/perl_modules/suid-exec.pl";
  }
  else
  {
    $command = "/home/websites/splitreflection.com/perl_modules/exec.pl";
  }
  my ($in_fh, $out_fh, $err_fh) = $r->spawn_proc_prog($command);
    while(my $line = <STDIN>)
    {print $in_fh $line;}
    my $head = 1;
    while(my $line = <$out_fh>)
    {
      if($head > 0)
      {
        if($line =~ /^[\r\n]+$/)
        { $head -= 1; }
        else
        {
          chomp $line;
          my ($key, $value) = split ": ", $line, 2;
          if(lc($key) eq "content-type")
          {
            $r->content_type($value);
          }
          $r->headers_out->add($key => $value);
        }
      }
      else
      {
        print $line;
      }
    }
#  print $response;
#  while(my ($key, $value) = each %ENV)
#  { print "$key = $value\n"; }

  return;# \&Apache2::OK;
}

1;
