/
HTTPResponse.pm
177 lines (117 loc) · 3.47 KB
/
HTTPResponse.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# See bottom of file for license and copyright information
=begin TML
---+!! Class Foswiki::Exception::HTTPResponse
Used to send HTTP status responses to the user.
The exception is %PERLDOC{"Foswiki::Exception::Harmless" text="harmless"}%.
Attributes:
$ =status= : HTTP status code, integer; response status code used if omitted.
$ =response= : a Foswiki::Response object. If not supplied then the default
from =$Foswiki::app->response= is used.
$ =text= : read-only, generated using the exception attributes.
=cut
package Foswiki::Exception::HTTPResponse;
use Foswiki::Class;
extends qw<Foswiki::Exception>;
with qw<Foswiki::Exception::Harmless>;
=begin TML
---++ ATTRIBUTES
=cut
=begin TML
---+++ ObjectAttribute status
HTTP response status code. By default is taken from =response= object.
=cut
has status => (
is => 'ro',
lazy => 1,
builder => 'prepareStatus',
);
=begin TML
---+++ ObjectAttribute response
A %PERLDOC{"Foswiki::Response"}% object. If =$Foswiki::app= is defined then
is taken from its =response= attribute. Otherwise a new one is generated.
=cut
has response => (
is => 'ro',
lazy => 1,
builder => 'prepareResponse',
);
=begin TML
---+++ObjectAttribute text
Overrides the base class attribute, makes it read-only.
=cut
has '+text' => (
is => 'ro',
lazy => 1,
);
=begin TML
---++ METHODS
=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($app)
&& $app->has_engine
&& defined( $app->engine )
&& $app->engine->HTTPCompliant;
}
# Simplified version of stringify() method.
around stringify => sub {
my $orig = shift;
my $this = shift;
my $str = '';
if ( $this->_useHTTP ) {
$str .= $this->response->printHeaders;
}
$str .= $this->response->body;
return $str;
};
=begin TML
---+++ ObjectMethod prepareText
Overrides base class method.
=cut
around prepareText => sub {
my $orig = shift;
my $this = shift;
return 'HTTP status code "' . $this->status;
};
=begin TML
---+++ ObjectMethod prepareStatus
Initializer for =status= attribute.
=cut
sub prepareStatus {
return $_[0]->response->status;
}
=begin TML
---+++ ObjectMethod prepareResponse
Initializer for =response= attribute.
=cut
sub prepareResponse {
my $this = shift;
my $app = $this->guessApp;
return defined($app) ? $app->response : Foswiki::Response->new;
}
1;
__END__
Foswiki - The Free and Open Source Wiki, http://foswiki.org/
Copyright (C) 2017 Foswiki Contributors. Foswiki Contributors
are listed in the AUTHORS file in the root of this distribution.
NOTE: Please extend that file, not this notice.
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version. For
more details read LICENSE in the root of this distribution.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
As per the GPL, removal of this notice is prohibited.