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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
|
<?php
/**
* Response handler for Ajax requests.
*
* 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.
*
* 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. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
* http://www.gnu.org/copyleft/gpl.html
*
* @file
* @ingroup Ajax
*/
/**
* Handle responses for Ajax requests (send headers, print
* content, that sort of thing)
*
* @ingroup Ajax
*/
class AjaxResponse {
/**
* Number of seconds to get the response cached by a proxy
* @var int $mCacheDuration
*/
private $mCacheDuration;
/**
* HTTP header Content-Type
* @var string $mContentType
*/
private $mContentType;
/**
* Disables output. Can be set by calling $AjaxResponse->disable()
* @var bool $mDisabled
*/
private $mDisabled;
/**
* Date for the HTTP header Last-modified
* @var string|bool $mLastModified
*/
private $mLastModified;
/**
* HTTP response code
* @var string $mResponseCode
*/
private $mResponseCode;
/**
* HTTP Vary header
* @var string $mVary
*/
private $mVary;
/**
* Content of our HTTP response
* @var string $mText
*/
private $mText;
/**
* @var Config
*/
private $mConfig;
/**
* @param string|null $text
* @param Config|null $config
*/
function __construct( $text = null, Config $config = null ) {
$this->mCacheDuration = null;
$this->mVary = null;
$this->mConfig = $config ?: ConfigFactory::getDefaultInstance()->makeConfig( 'main' );
$this->mDisabled = false;
$this->mText = '';
$this->mResponseCode = '200 OK';
$this->mLastModified = false;
$this->mContentType = 'application/x-wiki';
if ( $text ) {
$this->addText( $text );
}
}
/**
* Set the number of seconds to get the response cached by a proxy
* @param int $duration
*/
function setCacheDuration( $duration ) {
$this->mCacheDuration = $duration;
}
/**
* Set the HTTP Vary header
* @param string $vary
*/
function setVary( $vary ) {
$this->mVary = $vary;
}
/**
* Set the HTTP response code
* @param string $code
*/
function setResponseCode( $code ) {
$this->mResponseCode = $code;
}
/**
* Set the HTTP header Content-Type
* @param string $type
*/
function setContentType( $type ) {
$this->mContentType = $type;
}
/**
* Disable output.
*/
function disable() {
$this->mDisabled = true;
}
/**
* Add content to the response
* @param string $text
*/
function addText( $text ) {
if ( !$this->mDisabled && $text ) {
$this->mText .= $text;
}
}
/**
* Output text
*/
function printText() {
if ( !$this->mDisabled ) {
print $this->mText;
}
}
/**
* Construct the header and output it
*/
function sendHeaders() {
if ( $this->mResponseCode ) {
$n = preg_replace( '/^ *(\d+)/', '\1', $this->mResponseCode );
header( "Status: " . $this->mResponseCode, true, (int)$n );
}
header ( "Content-Type: " . $this->mContentType );
if ( $this->mLastModified ) {
header ( "Last-Modified: " . $this->mLastModified );
} else {
header ( "Last-Modified: " . gmdate( "D, d M Y H:i:s" ) . " GMT" );
}
if ( $this->mCacheDuration ) {
# If squid caches are configured, tell them to cache the response,
# and tell the client to always check with the squid. Otherwise,
# tell the client to use a cached copy, without a way to purge it.
if ( $this->mConfig->get( 'UseSquid' ) ) {
# Expect explicit purge of the proxy cache, but require end user agents
# to revalidate against the proxy on each visit.
# Surrogate-Control controls our Squid, Cache-Control downstream caches
if ( $this->mConfig->get( 'UseESI' ) ) {
header( 'Surrogate-Control: max-age=' . $this->mCacheDuration . ', content="ESI/1.0"' );
header( 'Cache-Control: s-maxage=0, must-revalidate, max-age=0' );
} else {
header( 'Cache-Control: s-maxage=' . $this->mCacheDuration . ', must-revalidate, max-age=0' );
}
} else {
# Let the client do the caching. Cache is not purged.
header ( "Expires: " . gmdate( "D, d M Y H:i:s", time() + $this->mCacheDuration ) . " GMT" );
header ( "Cache-Control: s-maxage={$this->mCacheDuration}," .
"public,max-age={$this->mCacheDuration}" );
}
} else {
# always expired, always modified
header ( "Expires: Mon, 26 Jul 1997 05:00:00 GMT" ); // Date in the past
header ( "Cache-Control: no-cache, must-revalidate" ); // HTTP/1.1
header ( "Pragma: no-cache" ); // HTTP/1.0
}
if ( $this->mVary ) {
header ( "Vary: " . $this->mVary );
}
}
/**
* checkLastModified tells the client to use the client-cached response if
* possible. If successful, the AjaxResponse is disabled so that
* any future call to AjaxResponse::printText() have no effect.
*
* @param string $timestamp
* @return bool Returns true if the response code was set to 304 Not Modified.
*/
function checkLastModified( $timestamp ) {
global $wgCachePages, $wgCacheEpoch, $wgUser;
$fname = 'AjaxResponse::checkLastModified';
if ( !$timestamp || $timestamp == '19700101000000' ) {
wfDebug( "$fname: CACHE DISABLED, NO TIMESTAMP\n", 'log' );
return false;
}
if ( !$wgCachePages ) {
wfDebug( "$fname: CACHE DISABLED\n", 'log' );
return false;
}
$timestamp = wfTimestamp( TS_MW, $timestamp );
$lastmod = wfTimestamp( TS_RFC2822, max( $timestamp, $wgUser->getTouched(), $wgCacheEpoch ) );
if ( !empty( $_SERVER['HTTP_IF_MODIFIED_SINCE'] ) ) {
# IE sends sizes after the date like this:
# Wed, 20 Aug 2003 06:51:19 GMT; length=5202
# this breaks strtotime().
$modsince = preg_replace( '/;.*$/', '', $_SERVER["HTTP_IF_MODIFIED_SINCE"] );
$modsinceTime = strtotime( $modsince );
$ismodsince = wfTimestamp( TS_MW, $modsinceTime ? $modsinceTime : 1 );
wfDebug( "$fname: -- client send If-Modified-Since: " . $modsince . "\n", 'log' );
wfDebug( "$fname: -- we might send Last-Modified : $lastmod\n", 'log' );
if ( ( $ismodsince >= $timestamp )
&& $wgUser->validateCache( $ismodsince ) &&
$ismodsince >= $wgCacheEpoch
) {
ini_set( 'zlib.output_compression', 0 );
$this->setResponseCode( "304 Not Modified" );
$this->disable();
$this->mLastModified = $lastmod;
wfDebug( "$fname: CACHED client: $ismodsince ; user: {$wgUser->getTouched()} ; " .
"page: $timestamp ; site $wgCacheEpoch\n", 'log' );
return true;
} else {
wfDebug( "$fname: READY client: $ismodsince ; user: {$wgUser->getTouched()} ; " .
"page: $timestamp ; site $wgCacheEpoch\n", 'log' );
$this->mLastModified = $lastmod;
}
} else {
wfDebug( "$fname: client did not send If-Modified-Since header\n", 'log' );
$this->mLastModified = $lastmod;
}
return false;
}
/**
* @param string $mckey
* @param int $touched
* @return bool
*/
function loadFromMemcached( $mckey, $touched ) {
global $wgMemc;
if ( !$touched ) {
return false;
}
$mcvalue = $wgMemc->get( $mckey );
if ( $mcvalue ) {
# Check to see if the value has been invalidated
if ( $touched <= $mcvalue['timestamp'] ) {
wfDebug( "Got $mckey from cache\n" );
$this->mText = $mcvalue['value'];
return true;
} else {
wfDebug( "$mckey has expired\n" );
}
}
return false;
}
/**
* @param string $mckey
* @param int $expiry
* @return bool
*/
function storeInMemcached( $mckey, $expiry = 86400 ) {
global $wgMemc;
$wgMemc->set( $mckey,
array(
'timestamp' => wfTimestampNow(),
'value' => $this->mText
), $expiry
);
return true;
}
}
|