wiki.techinc.nl/includes/Rest/RequestFromGlobals.php

105 lines
2.5 KiB
PHP
Raw Normal View History

<?php
namespace MediaWiki\Rest;
use GuzzleHttp\Psr7\LazyOpenStream;
use GuzzleHttp\Psr7\ServerRequest;
use GuzzleHttp\Psr7\Uri;
// phpcs:disable MediaWiki.Usage.SuperGlobalsUsage.SuperGlobals
/**
* This is a request class that gets data directly from the superglobals and
* other global PHP state, notably php://input.
*/
class RequestFromGlobals extends RequestBase {
private $uri;
private $protocol;
private $uploadedFiles;
/**
* @param array $params Associative array of parameters:
* - cookiePrefix: The prefix for cookie names used by getCookie()
*/
public function __construct( $params = [] ) {
parent::__construct( $params['cookiePrefix'] ?? '' );
}
// RequestInterface
public function getMethod() {
return $_SERVER['REQUEST_METHOD'] ?? 'GET';
}
public function getUri() {
if ( $this->uri === null ) {
$requestUrl = \WebRequest::getGlobalRequestURL();
try {
$uriInstance = new Uri( $requestUrl );
} catch ( \InvalidArgumentException $e ) {
// Uri constructor will throw exception if the URL is
// relative and contains colon-number pattern that
// looks like a port.
//
// Since $requestUrl here is absolute-path references
// so all titles that contain colon followed by a
// number would be inacessible if the exception occurs.
$uriInstance = (
new Uri( '//HOST:80' . $requestUrl )
)->withScheme( '' )->withHost( '' )->withPort( null );
}
$this->uri = $uriInstance;
}
return $this->uri;
}
// MessageInterface
public function getProtocolVersion() {
if ( $this->protocol === null ) {
$serverProtocol = $_SERVER['SERVER_PROTOCOL'] ?? '';
$prefixLength = strlen( 'HTTP/' );
if ( strncmp( $serverProtocol, 'HTTP/', $prefixLength ) === 0 ) {
$this->protocol = substr( $serverProtocol, $prefixLength );
} else {
$this->protocol = '1.1';
}
}
return $this->protocol;
}
protected function initHeaders() {
WebRequest & RequestFromGlobals: get HTTP headers in one way apache_request_headers() is a vendor-specific function - it got used when present and alternative code paths were exercised otherwise. These preserved certain "special" headers, e.g. Content-Type, only inconsistently. The function getallheaders() is an alias[1] for apache_request_headers() on systems where the latter is present. Alternatively, there is a polyfill (ralouphie/getallheaders) which is already installed in mediawiki-vendor[2] (by virtue of guzzle). Using getallheaders() exclusively, will make sure these "special" headers are consistently available alongside their "regular"[3] peers and helps MediaWiki code focus on its domain. The dependency to ralouphie/getallheaders is made explicit in the same version in which it is currently locked in mediawiki-vendor[4]. This surfaced because the deprecation warning for API POST requests without a Content-Type header, introduced in bba1a0f, appeared in my development system (somewhat dated addshore/mediawiki-docker-dev/) even though the client did a fine job. Interesting implementation detail: While WebRequest keeps track of headers using keys in all upper case, REST RequestFromGlobals does so in all lower case - but both use retrieval logic complementary to their respective approach however. In case of REST RequestFromGlobals this is encapsulated inside of HeaderContainer (setting and retrieving), while WebRequest does all of this by itself. Cf. [5] and [6] [1]: https://www.php.net/manual/en/function.getallheaders.php [2]: https://github.com/wikimedia/mediawiki-vendor/tree/8f2967d/ralouphie/getallheaders [3]: https://www.php.net/manual/en/reserved.variables.server.php#110763 [4]: https://github.com/wikimedia/mediawiki-vendor/blob/8f2967d/composer.lock#L3250 [5]: https://www.w3.org/Protocols/rfc2616/rfc2616-sec4.html#sec4.2 [6]: https://www.php.net/manual/en/function.apache-request-headers.php#124236 Bug: T245535 Change-Id: Iba52f152e15928473b729a2588c2462e76e85634
2020-03-18 08:49:15 +00:00
$this->setHeaders( getallheaders() );
}
public function getBody() {
return new LazyOpenStream( 'php://input', 'r' );
}
// ServerRequestInterface
public function getServerParams() {
return $_SERVER;
}
public function getCookieParams() {
return $_COOKIE;
}
public function getQueryParams() {
return $_GET;
}
public function getUploadedFiles() {
if ( $this->uploadedFiles === null ) {
$this->uploadedFiles = ServerRequest::normalizeFiles( $_FILES );
}
return $this->uploadedFiles;
}
public function getPostParams() {
return $_POST;
}
}