Skip to content

Commit

Permalink
Item14237: Fixed HTTPResponse exceptions handling
Browse files Browse the repository at this point in the history
  • Loading branch information
vrurg committed May 14, 2018
1 parent 77e9d0f commit b29c8eb
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 15 deletions.
4 changes: 3 additions & 1 deletion core/lib/Foswiki/App.pm
Expand Up @@ -984,7 +984,9 @@ sub handleRequest {
$exception->generate;
}
}
elsif ( $e->isa('Foswiki::OopsException') ) {
elsif ($e->isa('Foswiki::OopsException')
|| $e->isa("Foswiki::Exception::HTTPResponse") )
{
$e->generate;
}
elsif ( $e->isa('Foswiki::EngineException') ) {
Expand Down
24 changes: 17 additions & 7 deletions core/lib/Foswiki/Exception/HTTPError.pm
Expand Up @@ -20,20 +20,30 @@ extends qw<Foswiki::Exception::HTTPResponse>;

has header => ( is => 'rw', default => '' );

around generate => sub {
my $orig = shift;
my $this = shift;

$orig->( $this, @_ );

my $res = $this->response;
$res->header( -type => 'text/html' );
my $html = CGI::start_html( $this->status . ' ' . $this->header );
$html .= CGI::h1( {}, $this->header );
$html .= CGI::p( {}, $this->text );
$html .= CGI::p( {}, CGI::pre( $this->stacktrace ) ) if DEBUG;
$html .= CGI::end_html();
$res->print( Foswiki::encode_utf8($html) );
};

around stringify => sub {
my $orig = shift;
my $this = shift;

my $res = $this->response;
$res->body('');
if ( $this->_useHTTP ) {
$res->header( -type => 'text/html', -status => $this->status );
my $html = CGI::start_html( $this->status . ' ' . $this->header );
$html .= CGI::h1( {}, $this->header );
$html .= CGI::p( {}, $this->text );
$html .= CGI::p( {}, CGI::pre( $this->stacktrace ) ) if DEBUG;
$html .= CGI::end_html();
$res->print($html);
$this->generate;
}
else {
$res->print( $this->status . " "
Expand Down
30 changes: 23 additions & 7 deletions core/lib/Foswiki/Exception/HTTPResponse.pm
Expand Up @@ -76,12 +76,28 @@ has '+text' => (
=cut

=begin TML
---+++ ObjectMethod generate()
This method is called by application allowing the exception to generate
necessary response headers/body/etc. prior to reporting back to the user.
=cut

sub generate {
my $this = shift;
$this->response->status( $this->status );
}

sub _useHTTP {
my $this = shift;
my $app = $this->guessApp;
return
defined($Foswiki::app)
&& defined( $Foswiki::app->engine )
&& $Foswiki::app->engine->HTTPCompliant;
defined($app)
&& $app->has_engine
&& defined( $app->engine )
&& $app->engine->HTTPCompliant;
}

# Simplified version of stringify() method.
Expand Down Expand Up @@ -111,7 +127,7 @@ around prepareText => sub {
my $orig = shift;
my $this = shift;

return 'HTTP status code "' . $_[0]->status;
return 'HTTP status code "' . $this->status;
};

=begin TML
Expand All @@ -135,9 +151,9 @@ Initializer for =response= attribute.
=cut

sub prepareResponse {
return defined($Foswiki::app)
? $Foswiki::app->response
: Foswiki::Response->new;
my $this = shift;
my $app = $this->guessApp;
return defined($app) ? $app->response : Foswiki::Response->new;
}

1;
Expand Down

0 comments on commit b29c8eb

Please sign in to comment.