summaryrefslogtreecommitdiff
path: root/includes/WebRequest.php
diff options
context:
space:
mode:
Diffstat (limited to 'includes/WebRequest.php')
-rw-r--r--includes/WebRequest.php113
1 files changed, 82 insertions, 31 deletions
diff --git a/includes/WebRequest.php b/includes/WebRequest.php
index 054eceb9..b4b8be9b 100644
--- a/includes/WebRequest.php
+++ b/includes/WebRequest.php
@@ -39,6 +39,12 @@ class WebRequest {
protected $data, $headers = array();
/**
+ * Flag to make WebRequest::getHeader return an array of values.
+ * @since 1.26
+ */
+ const GETHEADER_LIST = 1;
+
+ /**
* Lazy-init response object
* @var WebResponse
*/
@@ -98,9 +104,9 @@ class WebRequest {
if ( !preg_match( '!^https?://!', $url ) ) {
$url = 'http://unused' . $url;
}
- wfSuppressWarnings();
+ MediaWiki\suppressWarnings();
$a = parse_url( $url );
- wfRestoreWarnings();
+ MediaWiki\restoreWarnings();
if ( $a ) {
$path = isset( $a['path'] ) ? $a['path'] : '';
@@ -170,6 +176,8 @@ class WebRequest {
* @return string
*/
public static function detectServer() {
+ global $wgAssumeProxiesUseDefaultProtocolPorts;
+
$proto = self::detectProtocol();
$stdPort = $proto === 'https' ? 443 : 80;
@@ -180,13 +188,15 @@ class WebRequest {
if ( !isset( $_SERVER[$varName] ) ) {
continue;
}
+
$parts = IP::splitHostAndPort( $_SERVER[$varName] );
if ( !$parts ) {
// Invalid, do not use
continue;
}
+
$host = $parts[0];
- if ( isset( $_SERVER['HTTP_X_FORWARDED_PROTO'] ) ) {
+ if ( $wgAssumeProxiesUseDefaultProtocolPorts && isset( $_SERVER['HTTP_X_FORWARDED_PROTO'] ) ) {
// Bug 70021: Assume that upstream proxy is running on the default
// port based on the protocol. We have no reliable way to determine
// the actual port in use upstream.
@@ -685,7 +695,7 @@ class WebRequest {
// This shouldn't happen!
throw new MWException( "Web server doesn't provide either " .
"REQUEST_URI, HTTP_X_ORIGINAL_URL or SCRIPT_NAME. Report details " .
- "of your web server configuration to http://bugzilla.wikimedia.org/" );
+ "of your web server configuration to https://phabricator.wikimedia.org/" );
}
// User-agents should not send a fragment with the URI, but
// if they do, and the web server passes it on to us, we
@@ -768,7 +778,7 @@ class WebRequest {
*
* @param int $deflimit Limit to use if no input and the user hasn't set the option.
* @param string $optionname To specify an option other than rclimit to pull from.
- * @return array First element is limit, second is offset
+ * @return int[] First element is limit, second is offset
*/
public function getLimitOffset( $deflimit = 50, $optionname = 'rclimit' ) {
global $wgUser;
@@ -861,7 +871,7 @@ class WebRequest {
/**
* Initialise the header list
*/
- private function initHeaders() {
+ protected function initHeaders() {
if ( count( $this->headers ) ) {
return;
}
@@ -894,19 +904,28 @@ class WebRequest {
}
/**
- * Get a request header, or false if it isn't set
- * @param string $name Case-insensitive header name
+ * Get a request header, or false if it isn't set.
*
- * @return string|bool False on failure
- */
- public function getHeader( $name ) {
+ * @param string $name Case-insensitive header name
+ * @param int $flags Bitwise combination of:
+ * WebRequest::GETHEADER_LIST Treat the header as a comma-separated list
+ * of values, as described in RFC 2616 ยง 4.2.
+ * (since 1.26).
+ * @return string|array|bool False if header is unset; otherwise the
+ * header value(s) as either a string (the default) or an array, if
+ * WebRequest::GETHEADER_LIST flag was set.
+ */
+ public function getHeader( $name, $flags = 0 ) {
$this->initHeaders();
$name = strtoupper( $name );
- if ( isset( $this->headers[$name] ) ) {
- return $this->headers[$name];
- } else {
+ if ( !isset( $this->headers[$name] ) ) {
return false;
}
+ $value = $this->headers[$name];
+ if ( $flags & self::GETHEADER_LIST ) {
+ $value = array_map( 'trim', explode( ',', $value ) );
+ }
+ return $value;
}
/**
@@ -1278,6 +1297,7 @@ class FauxRequest extends WebRequest {
private $wasPosted = false;
private $session = array();
private $requestUrl;
+ protected $cookies = array();
/**
* @param array $data Array of *non*-urlencoded key => value pairs, the
@@ -1305,11 +1325,10 @@ class FauxRequest extends WebRequest {
}
/**
- * @param string $method
- * @throws MWException
+ * Initialise the header list
*/
- private function notImplemented( $method ) {
- throw new MWException( "{$method}() not implemented" );
+ protected function initHeaders() {
+ // Nothing to init
}
/**
@@ -1352,7 +1371,38 @@ class FauxRequest extends WebRequest {
}
public function getCookie( $key, $prefix = null, $default = null ) {
- return $default;
+ if ( $prefix === null ) {
+ global $wgCookiePrefix;
+ $prefix = $wgCookiePrefix;
+ }
+ $name = $prefix . $key;
+ return isset( $this->cookies[$name] ) ? $this->cookies[$name] : $default;
+ }
+
+ /**
+ * @since 1.26
+ * @param string $name Unprefixed name of the cookie to set
+ * @param string|null $value Value of the cookie to set
+ * @param string|null $prefix Cookie prefix. Defaults to $wgCookiePrefix
+ */
+ public function setCookie( $key, $value, $prefix = null ) {
+ $this->setCookies( array( $key => $value ), $prefix );
+ }
+
+ /**
+ * @since 1.26
+ * @param array $cookies
+ * @param string|null $prefix Cookie prefix. Defaults to $wgCookiePrefix
+ */
+ public function setCookies( $cookies, $prefix = null ) {
+ if ( $prefix === null ) {
+ global $wgCookiePrefix;
+ $prefix = $wgCookiePrefix;
+ }
+ foreach ( $cookies as $key => $value ) {
+ $name = $prefix . $key;
+ $this->cookies[$name] = $value;
+ }
}
public function checkSessionCookie() {
@@ -1375,21 +1425,22 @@ class FauxRequest extends WebRequest {
}
/**
- * @param string $name The name of the header to get (case insensitive).
- * @return bool|string
+ * @param string $name
+ * @param string $val
*/
- public function getHeader( $name ) {
- $name = strtoupper( $name );
- return isset( $this->headers[$name] ) ? $this->headers[$name] : false;
+ public function setHeader( $name, $val ) {
+ $this->setHeaders( array( $name => $val ) );
}
/**
- * @param string $name
- * @param string $val
+ * @since 1.26
+ * @param array $headers
*/
- public function setHeader( $name, $val ) {
- $name = strtoupper( $name );
- $this->headers[$name] = $val;
+ public function setHeaders( $headers ) {
+ foreach ( $headers as $name => $val ) {
+ $name = strtoupper( $name );
+ $this->headers[$name] = $val;
+ }
}
/**
@@ -1488,8 +1539,8 @@ class DerivativeRequest extends FauxRequest {
return $this->base->checkSessionCookie();
}
- public function getHeader( $name ) {
- return $this->base->getHeader( $name );
+ public function getHeader( $name, $flags = 0 ) {
+ return $this->base->getHeader( $name, $flags );
}
public function getAllHeaders() {