Subject: | Script sends headers before subroutine called |
Subj.
This is not good, because I can't work with cookies, or send other cookies.
My solution:
sub handle_request {
my ($self) = shift;
my $result; # $result takes the output of the function, if it's an
# array split on __pjx__
my @other = (); # array for catching extra parameters
# we need to access "fname" in the form from the web page, so make
# sure there is a CGI object defined
return undef unless defined $self->cgi();
my ($rv,$header) ;
# get the name of the function
my $func_name = $self->cgi()->param("fname")||$self->cgi->cookie('fname');
# check if the function name was created
if ( defined $self->coderef_list()->{$func_name} ) {
my $code = $self->coderef_list()->{$func_name};
# eval the code from the coderef, and append the output to $rv
if ( ref($code) eq "CODE" ) {
eval { ($result, @other) =
$code->($self,$self->cgi()->param("args") ) };
if ($@) {
# see if the eval caused and error and report it
# Should we be more severe and die?
# die. only die
return $@;
}
if( @other ) {
$rv .= join( "__pjx__", ($result, @other) );
warn "rv = $rv\n" if ( $self->DEBUG() );
} else {
if ( defined $result ) {
$rv .= $result;
}
}
} # end if ref = CODE
if ( $self->cgi()->can('header') ) {
$header = $self->cgi()->header( $self->cgi_header_extra() );
} else {
# don't have an object with a "header()" method, so just create
# a mimimal one
$header = "Content-Type: text/html;";
# TODO:
$header .= header $self->cgi_header_extra();
$header .= "\n\n";
}
} else {
# # problems with the URL, return a CGI rrror
print STDERR "POSSIBLE SECURITY INCIDENT! Browser from ",
$self->cgi()->remote_addr();
print STDERR "\trequested URL: ", $self->cgi()->url();
print STDERR "\tfname request: ", $self->cgi()->param('fname');
print STDERR " -- returning Bad Request status 400\n";
if ( $self->cgi()->can('header') ) {
return($self->cgi()->header( -status=>'400' ));
} else {
# don't have an object with a "header()" method, so just create
# a mimimal one with 400 error
$rv = "Status: 400\nContent-Type: text/html;\n\n";
}
}
return "$header$rv";
}