3 * Response handler for Ajax requests.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
25 * Handle responses for Ajax requests (send headers, print
26 * content, that sort of thing)
31 /** Number of seconds to get the response cached by a proxy */
32 private $mCacheDuration;
34 /** HTTP header Content-Type */
35 private $mContentType;
37 /** Disables output. Can be set by calling $AjaxResponse->disable() */
40 /** Date for the HTTP header Last-modified */
41 private $mLastModified;
43 /** HTTP response code */
44 private $mResponseCode;
46 /** HTTP Vary header */
49 /** Content of our HTTP response */
53 * @param $text string|null
55 function __construct( $text = null ) {
56 $this->mCacheDuration
= null;
59 $this->mDisabled
= false;
61 $this->mResponseCode
= '200 OK';
62 $this->mLastModified
= false;
63 $this->mContentType
= 'application/x-wiki';
66 $this->addText( $text );
70 function setCacheDuration( $duration ) {
71 $this->mCacheDuration
= $duration;
74 function setVary( $vary ) {
78 function setResponseCode( $code ) {
79 $this->mResponseCode
= $code;
82 function setContentType( $type ) {
83 $this->mContentType
= $type;
87 $this->mDisabled
= true;
91 * Add content to the response
94 function addText( $text ) {
95 if ( ! $this->mDisabled
&& $text ) {
96 $this->mText
.= $text;
103 function printText() {
104 if ( ! $this->mDisabled
) {
110 * Construct the header and output it
112 function sendHeaders() {
113 global $wgUseSquid, $wgUseESI;
115 if ( $this->mResponseCode
) {
116 $n = preg_replace( '/^ *(\d+)/', '\1', $this->mResponseCode
);
117 header( "Status: " . $this->mResponseCode
, true, (int)$n );
120 header ( "Content-Type: " . $this->mContentType
);
122 if ( $this->mLastModified
) {
123 header ( "Last-Modified: " . $this->mLastModified
);
125 header ( "Last-Modified: " . gmdate( "D, d M Y H:i:s" ) . " GMT" );
128 if ( $this->mCacheDuration
) {
129 # If squid caches are configured, tell them to cache the response,
130 # and tell the client to always check with the squid. Otherwise,
131 # tell the client to use a cached copy, without a way to purge it.
134 # Expect explicite purge of the proxy cache, but require end user agents
135 # to revalidate against the proxy on each visit.
136 # Surrogate-Control controls our Squid, Cache-Control downstream caches
139 header( 'Surrogate-Control: max-age=' . $this->mCacheDuration
. ', content="ESI/1.0"' );
140 header( 'Cache-Control: s-maxage=0, must-revalidate, max-age=0' );
142 header( 'Cache-Control: s-maxage=' . $this->mCacheDuration
. ', must-revalidate, max-age=0' );
147 # Let the client do the caching. Cache is not purged.
148 header ( "Expires: " . gmdate( "D, d M Y H:i:s", time() +
$this->mCacheDuration
) . " GMT" );
149 header ( "Cache-Control: s-maxage={$this->mCacheDuration},public,max-age={$this->mCacheDuration}" );
153 # always expired, always modified
154 header ( "Expires: Mon, 26 Jul 1997 05:00:00 GMT" ); // Date in the past
155 header ( "Cache-Control: no-cache, must-revalidate" ); // HTTP/1.1
156 header ( "Pragma: no-cache" ); // HTTP/1.0
159 if ( $this->mVary
) {
160 header ( "Vary: " . $this->mVary
);
165 * checkLastModified tells the client to use the client-cached response if
166 * possible. If sucessful, the AjaxResponse is disabled so that
167 * any future call to AjaxResponse::printText() have no effect.
169 * @param $timestamp string
170 * @return bool Returns true if the response code was set to 304 Not Modified.
172 function checkLastModified ( $timestamp ) {
173 global $wgCachePages, $wgCacheEpoch, $wgUser;
174 $fname = 'AjaxResponse::checkLastModified';
176 if ( !$timestamp ||
$timestamp == '19700101000000' ) {
177 wfDebug( "$fname: CACHE DISABLED, NO TIMESTAMP\n" );
181 if ( !$wgCachePages ) {
182 wfDebug( "$fname: CACHE DISABLED\n", false );
186 if ( $wgUser->getOption( 'nocache' ) ) {
187 wfDebug( "$fname: USER DISABLED CACHE\n", false );
191 $timestamp = wfTimestamp( TS_MW
, $timestamp );
192 $lastmod = wfTimestamp( TS_RFC2822
, max( $timestamp, $wgUser->getTouched(), $wgCacheEpoch ) );
194 if ( !empty( $_SERVER['HTTP_IF_MODIFIED_SINCE'] ) ) {
195 # IE sends sizes after the date like this:
196 # Wed, 20 Aug 2003 06:51:19 GMT; length=5202
197 # this breaks strtotime().
198 $modsince = preg_replace( '/;.*$/', '', $_SERVER["HTTP_IF_MODIFIED_SINCE"] );
199 $modsinceTime = strtotime( $modsince );
200 $ismodsince = wfTimestamp( TS_MW
, $modsinceTime ?
$modsinceTime : 1 );
201 wfDebug( "$fname: -- client send If-Modified-Since: " . $modsince . "\n", false );
202 wfDebug( "$fname: -- we might send Last-Modified : $lastmod\n", false );
204 if ( ( $ismodsince >= $timestamp ) && $wgUser->validateCache( $ismodsince ) && $ismodsince >= $wgCacheEpoch ) {
205 ini_set( 'zlib.output_compression', 0 );
206 $this->setResponseCode( "304 Not Modified" );
208 $this->mLastModified
= $lastmod;
210 wfDebug( "$fname: CACHED client: $ismodsince ; user: {$wgUser->getTouched()} ; page: $timestamp ; site $wgCacheEpoch\n", false );
214 wfDebug( "$fname: READY client: $ismodsince ; user: {$wgUser->getTouched()} ; page: $timestamp ; site $wgCacheEpoch\n", false );
215 $this->mLastModified
= $lastmod;
218 wfDebug( "$fname: client did not send If-Modified-Since header\n", false );
219 $this->mLastModified
= $lastmod;
229 function loadFromMemcached( $mckey, $touched ) {
236 $mcvalue = $wgMemc->get( $mckey );
238 # Check to see if the value has been invalidated
239 if ( $touched <= $mcvalue['timestamp'] ) {
240 wfDebug( "Got $mckey from cache\n" );
241 $this->mText
= $mcvalue['value'];
245 wfDebug( "$mckey has expired\n" );
257 function storeInMemcached( $mckey, $expiry = 86400 ) {
260 $wgMemc->set( $mckey,
262 'timestamp' => wfTimestampNow(),
263 'value' => $this->mText