3 * Response handler for Ajax requests
10 * Handle responses for Ajax requests (send headers, print
11 * content, that sort of thing)
16 /** Number of seconds to get the response cached by a proxy */
17 private $mCacheDuration;
19 /** HTTP header Content-Type */
20 private $mContentType;
22 /** Disables output. Can be set by calling $AjaxResponse->disable() */
25 /** Date for the HTTP header Last-modified */
26 private $mLastModified;
28 /** HTTP response code */
29 private $mResponseCode;
31 /** HTTP Vary header */
34 /** Content of our HTTP response */
37 function __construct( $text = null ) {
38 $this->mCacheDuration
= null;
41 $this->mDisabled
= false;
43 $this->mResponseCode
= '200 OK';
44 $this->mLastModified
= false;
45 $this->mContentType
= 'application/x-wiki';
48 $this->addText( $text );
52 function setCacheDuration( $duration ) {
53 $this->mCacheDuration
= $duration;
56 function setVary( $vary ) {
60 function setResponseCode( $code ) {
61 $this->mResponseCode
= $code;
64 function setContentType( $type ) {
65 $this->mContentType
= $type;
69 $this->mDisabled
= true;
72 /** Add content to the response */
73 function addText( $text ) {
74 if ( ! $this->mDisabled
&& $text ) {
75 $this->mText
.= $text;
80 function printText() {
81 if ( ! $this->mDisabled
) {
86 /** Construct the header and output it */
87 function sendHeaders() {
88 global $wgUseSquid, $wgUseESI;
90 if ( $this->mResponseCode
) {
91 $n = preg_replace( '/^ *(\d+)/', '\1', $this->mResponseCode
);
92 header( "Status: " . $this->mResponseCode
, true, (int)$n );
95 header ( "Content-Type: " . $this->mContentType
);
97 if ( $this->mLastModified
) {
98 header ( "Last-Modified: " . $this->mLastModified
);
100 header ( "Last-Modified: " . gmdate( "D, d M Y H:i:s" ) . " GMT" );
103 if ( $this->mCacheDuration
) {
104 # If squid caches are configured, tell them to cache the response,
105 # and tell the client to always check with the squid. Otherwise,
106 # tell the client to use a cached copy, without a way to purge it.
109 # Expect explicite purge of the proxy cache, but require end user agents
110 # to revalidate against the proxy on each visit.
111 # Surrogate-Control controls our Squid, Cache-Control downstream caches
114 header( 'Surrogate-Control: max-age=' . $this->mCacheDuration
. ', content="ESI/1.0"' );
115 header( 'Cache-Control: s-maxage=0, must-revalidate, max-age=0' );
117 header( 'Cache-Control: s-maxage=' . $this->mCacheDuration
. ', must-revalidate, max-age=0' );
122 # Let the client do the caching. Cache is not purged.
123 header ( "Expires: " . gmdate( "D, d M Y H:i:s", time() +
$this->mCacheDuration
) . " GMT" );
124 header ( "Cache-Control: s-maxage={$this->mCacheDuration},public,max-age={$this->mCacheDuration}" );
128 # always expired, always modified
129 header ( "Expires: Mon, 26 Jul 1997 05:00:00 GMT" ); // Date in the past
130 header ( "Cache-Control: no-cache, must-revalidate" ); // HTTP/1.1
131 header ( "Pragma: no-cache" ); // HTTP/1.0
134 if ( $this->mVary
) {
135 header ( "Vary: " . $this->mVary
);
140 * checkLastModified tells the client to use the client-cached response if
141 * possible. If sucessful, the AjaxResponse is disabled so that
142 * any future call to AjaxResponse::printText() have no effect. The method
143 * returns true iff the response code was set to 304 Not Modified.
145 function checkLastModified ( $timestamp ) {
146 global $wgCachePages, $wgCacheEpoch, $wgUser;
147 $fname = 'AjaxResponse::checkLastModified';
149 if ( !$timestamp ||
$timestamp == '19700101000000' ) {
150 wfDebug( "$fname: CACHE DISABLED, NO TIMESTAMP\n" );
154 if ( !$wgCachePages ) {
155 wfDebug( "$fname: CACHE DISABLED\n", false );
159 if ( $wgUser->getOption( 'nocache' ) ) {
160 wfDebug( "$fname: USER DISABLED CACHE\n", false );
164 $timestamp = wfTimestamp( TS_MW
, $timestamp );
165 $lastmod = wfTimestamp( TS_RFC2822
, max( $timestamp, $wgUser->mTouched
, $wgCacheEpoch ) );
167 if ( !empty( $_SERVER['HTTP_IF_MODIFIED_SINCE'] ) ) {
168 # IE sends sizes after the date like this:
169 # Wed, 20 Aug 2003 06:51:19 GMT; length=5202
170 # this breaks strtotime().
171 $modsince = preg_replace( '/;.*$/', '', $_SERVER["HTTP_IF_MODIFIED_SINCE"] );
172 $modsinceTime = strtotime( $modsince );
173 $ismodsince = wfTimestamp( TS_MW
, $modsinceTime ?
$modsinceTime : 1 );
174 wfDebug( "$fname: -- client send If-Modified-Since: " . $modsince . "\n", false );
175 wfDebug( "$fname: -- we might send Last-Modified : $lastmod\n", false );
177 if ( ( $ismodsince >= $timestamp ) && $wgUser->validateCache( $ismodsince ) && $ismodsince >= $wgCacheEpoch ) {
178 ini_set( 'zlib.output_compression', 0 );
179 $this->setResponseCode( "304 Not Modified" );
181 $this->mLastModified
= $lastmod;
183 wfDebug( "$fname: CACHED client: $ismodsince ; user: $wgUser->mTouched ; page: $timestamp ; site $wgCacheEpoch\n", false );
187 wfDebug( "$fname: READY client: $ismodsince ; user: $wgUser->mTouched ; page: $timestamp ; site $wgCacheEpoch\n", false );
188 $this->mLastModified
= $lastmod;
191 wfDebug( "$fname: client did not send If-Modified-Since header\n", false );
192 $this->mLastModified
= $lastmod;
201 function loadFromMemcached( $mckey, $touched ) {
208 $mcvalue = $wgMemc->get( $mckey );
210 # Check to see if the value has been invalidated
211 if ( $touched <= $mcvalue['timestamp'] ) {
212 wfDebug( "Got $mckey from cache\n" );
213 $this->mText
= $mcvalue['value'];
217 wfDebug( "$mckey has expired\n" );
229 function storeInMemcached( $mckey, $expiry = 86400 ) {
232 $wgMemc->set( $mckey,
234 'timestamp' => wfTimestampNow(),
235 'value' => $this->mText