Skip to content

Commit b75d830

Browse files
CrawfordCurrieCrawfordCurrie
authored andcommitted
Item139: removed oops redirect for error reports. Fixed status header in HTTP responses.
1 parent a342147 commit b75d830

File tree

17 files changed

+118
-79
lines changed

17 files changed

+118
-79
lines changed

EditTablePlugin/lib/Foswiki/Plugins/EditTablePlugin/Core.pm

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1440,7 +1440,7 @@ sub doEnableEdit {
14401440

14411441
# user has no permission to change the topic
14421442
throw Foswiki::OopsException(
1443-
'accessdenied',
1443+
'accessdenied', status => 403,
14441444
def => 'topic_access',
14451445
web => $theWeb,
14461446
topic => $theTopic,

TWikiCompatibilityPlugin/lib/TWiki/OopsException.pm

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,9 @@
11
package TWiki::OopsException;
2-
use Error;
3-
use Assert;
4-
use base 'Error';
5-
6-
use Foswiki::OopsException;
2+
use base 'Foswiki::OopsException';
73

84
sub new {
9-
shift;
10-
return new Foswiki::OopsException(@_);
5+
my $class = shift;
6+
return $class->SUPER::new(@_);
117
}
128

139
1;

UnitTestContrib/test/unit/ExceptionTests.pm

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
package ExceptionTests;
2-
use base FoswikiTestCase;
2+
use base 'FoswikiTestCase';
33

44
use strict;
55

66
use Error qw( :try );
77
use Foswiki::OopsException;
8+
use Foswiki::AccessControlException;
89
use Foswiki::UI::Oops;
910

1011
# Check an OopsException with one non-array parameter
@@ -63,6 +64,7 @@ sub upchuck {
6364
$e->redirect($session);
6465
}
6566

67+
# Test for DEPRECATED redirect
6668
sub test_redirectOopsException {
6769
my $this = shift;
6870
my $t = new Foswiki();

WorkflowPlugin/data/Sandbox/ControlledDocument.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
%META:TOPICINFO{author="SimianApe" date="1228219811" format="1.1" version="1.3"}%
12
%WORKFLOWNOTICE%
23

34
Some controlled text.
@@ -11,3 +12,7 @@ Some controlled text.
1112
| Last version in APPROVED state | %WORKFLOWLASTVERSION_APPROVED% |
1213

1314
Workflow history: %WORKFLOWHISTORY%
15+
16+
%META:FORM{name="InProcessForm"}%
17+
%META:WORKFLOWHISTORY{value="<br>WAITINGFORQM -- 02 Dec 2008 - 12:10"}%
18+
%META:WORKFLOW{name="WAITINGFORQM" LASTTIME_WAITINGFORQM="02 Dec 2008 - 12:10" LASTVERSION_WAITINGFORQM=""}%

WorkflowPlugin/data/Sandbox/DocumentApprovalWorkflow.txt

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,10 @@ Workflow history: %<nop>WORKFLOWHISTORY%
2929
| WAITINGFORQM | nobody | This document is waiting for approval by the Quality Manager. |
3030
| WAITINGFORCTO | nobody | This document is waiting for approval by the CTO.|
3131
---++ Transitions
32-
| *State* | *Action* | *Next State* | *Allowed* | *Form* |
33-
| APPROVED | revise | UNDERREVISION | | ApprovedForm |
34-
| UNDERREVISION | ready for approval | WAITINGFORQM | | InProcessForm |
35-
| WAITINGFORQM | approve | WAITINGFORCTO | | |
36-
| WAITINGFORQM | reject | UNDERREVISION | | |
37-
| WAITINGFORCTO | approve | APPROVED | | ApprovedForm |
38-
| WAITINGFORCTO | reject | UNDERREVISION | | |
32+
| *State* | *Action* | *Next State* | *Allowed* | *Form* |
33+
| APPROVED | revise | UNDERREVISION | | ApprovedForm |
34+
| UNDERREVISION | ready for approval | WAITINGFORQM | | InProcessForm |
35+
| WAITINGFORQM | approve | WAITINGFORCTO | | |
36+
| WAITINGFORQM | reject | UNDERREVISION | | |
37+
| WAITINGFORCTO | approve | APPROVED | | ApprovedForm |
38+
| WAITINGFORCTO | reject | UNDERREVISION | | |

WorkflowPlugin/lib/TWiki/Plugins/WorkflowPlugin.pm

Lines changed: 31 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ sub beforeEditHandler {
240240
my $query = TWiki::Func::getCgiQuery();
241241
if ( !$query->param('INWORKFLOWSEQUENCE') && !$TOPIC->canEdit() ) {
242242
throw TWiki::OopsException(
243-
'accessdenied',
243+
'accessdenied', status => 403,
244244
def => 'topic_access',
245245
web => $_[2],
246246
topic => $_[1],
@@ -284,35 +284,40 @@ sub _changeState {
284284
my $newForm = $TOPIC->newForm($action);
285285

286286
try {
287-
$query->param('INWORKFLOWSEQUENCE' => 1);
288-
$TOPIC->changeState($action);
289-
if ($newForm) {
290-
291-
# If there is a form with the new state, and it's not the same
292-
# form as previously, we need to kick into edit mode to support
293-
# form field changes.
294-
$url = TWiki::Func::getScriptUrl(
295-
$web, $topic, 'edit',
296-
INWORKFLOWSEQUENCE => time());
287+
try {
288+
$query->param('INWORKFLOWSEQUENCE' => 1);
289+
if ($newForm) {
290+
# If there is a form with the new state, and it's not
291+
# the same form as previously, we need to kick into edit
292+
# mode to support form field changes.
293+
$url = TWiki::Func::getScriptUrl(
294+
$web, $topic, 'edit',
295+
INWORKFLOWSEQUENCE => time());
296+
}
297+
else {
298+
$url = TWiki::Func::getScriptUrl( $web, $topic, 'view' );
299+
}
300+
# SMELL: don't do this until the edit is over
301+
$TOPIC->changeState($action);
302+
TWiki::Func::redirectCgiQuery( undef, $url );
303+
} catch Error::Simple with {
304+
my $error = shift;
305+
throw TWiki::OopsException(
306+
'oopssaveerr',
307+
web => $web, topic => $topic,
308+
params => [ $error || '?' ]);
309+
};
310+
} catch TWiki::OopsException with {
311+
my $e = shift;
312+
if ($e->can('generate')) {
313+
$e->generate( $session );
314+
} else {
315+
# Deprecated, TWiki compatibility only
316+
$e->redirect( $session );
297317
}
298-
else {
299-
$url = TWiki::Func::getScriptUrl( $web, $topic, 'view' );
300-
}
301-
}
302-
catch Error::Simple with {
303-
my $error = shift;
304-
$url = TWiki::Func::getScriptUrl(
305-
$web, $topic, 'oops',
306-
template => "oopssaveerr",
307-
param1 => $error
308-
);
309-
}
310-
catch TWiki::OopsException with {
311-
shift->redirect( $session );
312318

313319
};
314320
}
315-
TWiki::Func::redirectCgiQuery( undef, $url );
316321
return undef;
317322
}
318323

core/lib/Foswiki.pm

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -722,7 +722,6 @@ sub generateHTTPHeaders {
722722

723723
# add cookie(s)
724724
$this->{users}->{loginManager}->modifyHeader($hopts);
725-
726725
$this->{response}->headers($hopts);
727726
}
728727

core/lib/Foswiki/Engine/CGI.pm

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,9 @@ sub finalizeHeaders {
176176
my ( $this, $res, $req ) = @_;
177177

178178
$this->SUPER::finalizeHeaders( $res, $req );
179+
# make sure we always generate a status for the response
180+
print 'Status: '.$res->status()
181+
if ($res->status() && !defined($res->headers->{status}));
179182
foreach my $header ( keys %{ $res->headers } ) {
180183
print $header . ': ' . $_ . $this->CRLF
181184
foreach $res->getHeader($header);

core/lib/Foswiki/OopsException.pm

Lines changed: 34 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ use Error;
2323
...
2424
2525
throw Foswiki::OopsException( 'bathplugin',
26+
status => 418,
2627
def => 'toestuck',
2728
web => $web,
2829
topic => $topic,
@@ -83,7 +84,7 @@ NOTE: parameter values are automatically and unconditionally entity-encoded
8384
sub new {
8485
my $class = shift;
8586
my $template = shift;
86-
my $this = bless( $class->SUPER::new(), $class );
87+
my $this = $class->SUPER::new();
8788
$this->{template} = $template;
8889
ASSERT( scalar(@_) % 2 == 0, join( ";", map { $_ || 'undef' } @_ ) )
8990
if DEBUG;
@@ -145,22 +146,44 @@ sub stringify {
145146
}
146147
}
147148

148-
=begin TML
149+
# Generate a redirect to an 'oops' script for this exception.
150+
#
151+
# If the 'keep' parameter is set in the
152+
# exception, it saves parameter values into the query as well. This is needed
153+
# if the query string might get lost during a passthrough, due to a POST
154+
# being redirected to a GET.
155+
# This redirect has been replaced by the generate function below and should
156+
# not be called in new code.
157+
sub redirect {
158+
my ( $this, $session ) = @_;
159+
my @p = $this->_prepareResponse($session);
160+
my $url =
161+
$session->getScriptUrl( 1, 'oops', $this->{web}, $this->{topic}, @p );
162+
$session->redirect( $url, 1 );
163+
}
149164

150-
---++ ObjectMethod redirect( $twiki )
165+
=begin TML
151166
152-
Generate a redirect to an 'oops' script for this exception.
167+
---++ ObjectMethod generate( $session )
153168
154-
If the 'keep' parameter is set in the
155-
exception, it saves parameter values into the query as well. This is needed
156-
if the query string might get lost during a passthrough, due to a POST
157-
being redirected to a GET.
169+
Generate an error page for the exception. This will output the error page
170+
to the browser. The default HTTP Status for an Oops exception is 500. This
171+
can be overridden using the 'status => ' parameter to the constructor.
158172
159173
=cut
160174

161-
sub redirect {
162-
my ( $this, $session ) = @_;
175+
sub generate {
176+
my ($this, $session ) = @_;
177+
178+
my @p = $this->_prepareResponse( $session );
179+
$session->{response}->status( $this->{status} || 500 );
180+
require Foswiki::UI::Oops;
181+
Foswiki::UI::Oops::oops($session, $this->{web}, $this->{topic},
182+
$session->{request}, 0);
183+
}
163184

185+
sub _prepareResponse {
186+
my ($this, $session ) = @_;
164187
my @p = ();
165188

166189
$this->{template} = "oops$this->{template}"
@@ -169,12 +192,10 @@ sub redirect {
169192
push( @p, def => $this->{def} ) if $this->{def};
170193
my $n = 1;
171194
push( @p, map { 'param' . ( $n++ ) => $_ } @{ $this->{params} } );
172-
my $url =
173-
$session->getScriptUrl( 1, 'oops', $this->{web}, $this->{topic}, @p );
174195
while ( my $p = shift(@p) ) {
175196
$session->{request}->param( -name => $p, -value => shift(@p) );
176197
}
177-
$session->redirect( $url, 1 );
198+
return @p;
178199
}
179200

180201
1;

core/lib/Foswiki/Store.pm

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1403,12 +1403,14 @@ sub topicExists {
14031403
my ( $this, $web, $topic ) = @_;
14041404
$web =~ s#\.#/#go;
14051405
ASSERT( defined($topic) ) if DEBUG;
1406-
if (DEBUG) {
1407-
my ( $webTest, $topicTest ) =
1408-
$this->{session}->normalizeWebTopicName( $web, $topic );
1409-
ASSERT( $topic eq $topicTest );
1410-
ASSERT( $web eq $webTest );
1411-
}
1406+
# This test is invalid. This intent is good, but this function may
1407+
# be called with a deliberately undef web or topic.
1408+
#if (DEBUG) {
1409+
# my ( $webTest, $topicTest ) =
1410+
# $this->{session}->normalizeWebTopicName( $web, $topic );
1411+
# ASSERT( $topic eq $topicTest );
1412+
# ASSERT( $web eq $webTest );
1413+
#}
14121414
return 0 unless $topic;
14131415

14141416
my $handler = _getHandler( $this, $web, $topic );

0 commit comments

Comments
 (0)