list( $host, $lag ) = wfGetLB()->getMaxLag();
if ( $lag > $maxLag ) {
$resp = $this->context->getRequest()->response();
- $resp->header( 'HTTP/1.1 503 Service Unavailable' );
+ $resp->statusHeader( 503 );
$resp->header( 'Retry-After: ' . max( intval( $maxLag ), 5 ) );
$resp->header( 'X-Database-Lag: ' . intval( $lag ) );
$resp->header( 'Content-Type: text/plain' );
}
# Not modified
- # Give a 304 response code and disable body output
+ # Give a 304 Not Modified response code and disable body output
wfDebug( __METHOD__ . ": NOT MODIFIED, $info\n", 'log' );
ini_set( 'zlib.output_compression', 0 );
- $this->getRequest()->response()->header( "HTTP/1.1 304 Not Modified" );
+ $this->getRequest()->response()->statusHeader( 304 );
$this->sendCacheControl();
$this->disable();
if ( Hooks::run( "BeforePageRedirect", array( $this, &$redirect, &$code ) ) ) {
if ( $code == '301' || $code == '303' ) {
if ( !$config->get( 'DebugRedirects' ) ) {
- $message = HttpStatus::getMessage( $code );
- $response->header( "HTTP/1.1 $code $message" );
+ $response->statusHeader( $code );
}
$this->mLastModified = wfTimestamp( TS_RFC2822 );
}
return;
} elseif ( $this->mStatusCode ) {
- $message = HttpStatus::getMessage( $this->mStatusCode );
- if ( $message ) {
- $response->header( 'HTTP/1.1 ' . $this->mStatusCode . ' ' . $message );
- }
+ $response->statusHeader( $this->mStatusCode );
}
# Buffer output; final headers may depend on later processing
class WebResponse {
/**
- * Output a HTTP header, wrapper for PHP's header()
+ * Output an HTTP header, wrapper for PHP's header()
* @param string $string Header to output
* @param bool $replace Replace current similar header
* @param null|int $http_response_code Forces the HTTP response code to the specified value.
return null;
}
+ /**
+ * Output an HTTP status code header
+ * @since 1.26
+ * @param int $code Status code
+ */
+ public function statusHeader( $code ) {
+ HttpStatus::header( $code );
+ }
+
/**
* Set the browser cookie
* @param string $name The name of the cookie.
}
}
+ /**
+ * @since 1.26
+ * @param int $code Status code
+ */
+ public function statusHeader( $code ) {
+ $this->code = intval( $code );
+ }
+
/**
* @param string $key The name of the header to get (case insensitive).
* @return string|null The header value (if set); null otherwise.
$text = $this->getRawText();
+ // Don't return a 404 response for CSS or JavaScript;
+ // 404s aren't generally cached and it would create
+ // extra hits when user CSS/JS are on and the user doesn't
+ // have the pages.
if ( $text === false && $contentType == 'text/x-wiki' ) {
- # Don't return a 404 response for CSS or JavaScript;
- # 404s aren't generally cached and it would create
- # extra hits when user CSS/JS are on and the user doesn't
- # have the pages.
- $response->header( 'HTTP/1.x 404 Not Found' );
+ $response->statusHeader( 404 );
}
if ( !Hooks::run( 'RawPageViewBeforeOutput', array( &$this, &$text ) ) ) {
if ( !in_array( $originParam, $origins ) ) {
// origin parameter set but incorrect
// Send a 403 response
- $message = HttpStatus::getMessage( 403 );
- $response->header( "HTTP/1.1 403 $message", true, 403 );
+ $response->statusHeader( 403 );
$response->header( 'Cache-Control: no-cache' );
echo "'origin' parameter does not match Origin header\n";
if ( !$this->mPage->hasViewableContent() && $wgSend404Code && !$validUserPage ) {
// If there's no backing content, send a 404 Not Found
// for better machine handling of broken links.
- $this->getContext()->getRequest()->response()->header( "HTTP/1.1 404 Not Found" );
+ $this->getContext()->getRequest()->response()->statusHeader( 404 );
}
// Also apply the robot policy for nonexisting pages (even if a 404 was used for sanity)
$out->wrapWikiMsg( "<div id='mw-imagepage-nofile' class='plainlinks'>\n$1\n</div>", $nofile );
if ( !$this->getID() && $wgSend404Code ) {
// If there is no image, no shared image, and no description page,
- // output a 404, to be consistent with articles.
- $request->response()->header( 'HTTP/1.1 404 Not Found' );
+ // output a 404, to be consistent with Article::showMissingArticle.
+ $request->response()->statusHeader( 404 );
}
}
$out->setFileVersion( $this->displayImg );
'Third parameter overrides the HTTP/... header'
);
+ $this->response->statusHeader( 210 );
+ $this->assertEquals(
+ 210,
+ $this->response->getStatusCode(),
+ 'Handle statusHeader method'
+ );
+
$this->response->header( 'Location: http://localhost/', false, 206 );
$this->assertEquals(
206,
if ( $redirectedLocation ) {
// File has been moved. Give redirect.
$response = RequestContext::getMain()->getRequest()->response();
- $response->header( "HTTP/1.1 302 " . HttpStatus::getMessage( 302 ) );
+ $response->statusHeader( 302 );
$response->header( 'Location: ' . $redirectedLocation );
$response->header( 'Expires: ' .
gmdate( 'D, d M Y H:i:s', time() + 12 * 3600 ) . ' GMT' );
} elseif ( rawurldecode( $rel404 ) === $img->getThumbRel( $thumbName2 ) ) {
// Request for the "long" thumbnail name; redirect to canonical name
$response = RequestContext::getMain()->getRequest()->response();
- $response->header( "HTTP/1.1 301 " . HttpStatus::getMessage( 301 ) );
+ $response->statusHeader( 301 );
$response->header( 'Location: ' .
wfExpandUrl( $img->getThumbUrl( $thumbName ), PROTO_CURRENT ) );
$response->header( 'Expires: ' .