* @param string $caller The method making this request, for profiling
* @return string|bool (bool)false on failure or a string on success
*/
- public static function request( $method, $url, $options = array(), $caller = __METHOD__ ) {
+ public static function request( $method, $url, $options = [], $caller = __METHOD__ ) {
wfDebug( "HTTP: $method: $url\n" );
$options['method'] = strtoupper( $method );
} else {
$errors = $status->getErrorsByType( 'error' );
$logger = LoggerFactory::getInstance( 'http' );
- $logger->warning( $status->getWikiText(),
- array( 'error' => $errors, 'caller' => $caller, 'content' => $req->getContent() ) );
+ $logger->warning( $status->getWikiText( false, false, 'en' ),
+ [ 'error' => $errors, 'caller' => $caller, 'content' => $req->getContent() ] );
return false;
}
}
* @param string $caller The method making this request, for profiling
* @return string|bool false on error
*/
- public static function get( $url, $options = array(), $caller = __METHOD__ ) {
+ public static function get( $url, $options = [], $caller = __METHOD__ ) {
$args = func_get_args();
if ( isset( $args[1] ) && ( is_string( $args[1] ) || is_numeric( $args[1] ) ) ) {
// Second was used to be the timeout
// And third parameter used to be $options
wfWarn( "Second parameter should not be a timeout.", 2 );
$options = isset( $args[2] ) && is_array( $args[2] ) ?
- $args[2] : array();
+ $args[2] : [];
$options['timeout'] = $args[1];
$caller = __METHOD__;
}
* @param string $caller The method making this request, for profiling
* @return string|bool false on error
*/
- public static function post( $url, $options = array(), $caller = __METHOD__ ) {
+ public static function post( $url, $options = [], $caller = __METHOD__ ) {
return Http::request( 'POST', $url, $options, $caller );
}
- /**
- * Check if the URL can be served by localhost
- *
- * @param string $url Full url to check
- * @return bool
- */
- public static function isLocalURL( $url ) {
- global $wgCommandLineMode, $wgLocalVirtualHosts, $wgConf;
-
- if ( $wgCommandLineMode ) {
- return false;
- }
-
- // Extract host part
- $matches = array();
- if ( preg_match( '!^http://([\w.-]+)[/:].*$!', $url, $matches ) ) {
- $host = $matches[1];
- // Split up dotwise
- $domainParts = explode( '.', $host );
- // Check if this domain or any superdomain is listed as a local virtual host
- $domainParts = array_reverse( $domainParts );
-
- $domain = '';
- $countParts = count( $domainParts );
- for ( $i = 0; $i < $countParts; $i++ ) {
- $domainPart = $domainParts[$i];
- if ( $i == 0 ) {
- $domain = $domainPart;
- } else {
- $domain = $domainPart . '.' . $domain;
- }
-
- if ( in_array( $domain, $wgLocalVirtualHosts )
- || $wgConf->isLocalVHost( $domain )
- ) {
- return true;
- }
- }
- }
-
- return false;
- }
-
/**
* A standard user-agent we can use for external requests.
* @return string
$uri
);
}
+
+ /**
+ * Gets the relevant proxy from $wgHTTPProxy
+ *
+ * @return mixed The proxy address or an empty string if not set.
+ */
+ public static function getProxy() {
+ global $wgHTTPProxy;
+
+ if ( $wgHTTPProxy ) {
+ return $wgHTTPProxy;
+ }
+
+ return "";
+ }
}
/**
protected $sslVerifyCert = true;
protected $caInfo = null;
protected $method = "GET";
- protected $reqHeaders = array();
+ protected $reqHeaders = [];
protected $url;
protected $parsedUrl;
protected $callback;
*/
protected $cookieJar;
- protected $headerList = array();
+ protected $headerList = [];
protected $respVersion = "0.9";
protected $respStatus = "200 Ok";
- protected $respHeaders = array();
+ protected $respHeaders = [];
public $status;
* @param Profiler $profiler An instance of the profiler for profiling, or null
*/
protected function __construct(
- $url, $options = array(), $caller = __METHOD__, $profiler = null
+ $url, $options = [], $caller = __METHOD__, $profiler = null
) {
global $wgHTTPTimeout, $wgHTTPConnectTimeout;
$this->setUserAgent( $options['userAgent'] );
}
- $members = array( "postData", "proxy", "noProxy", "sslVerifyHost", "caInfo",
- "method", "followRedirects", "maxRedirects", "sslVerifyCert", "callback" );
+ $members = [ "postData", "proxy", "noProxy", "sslVerifyHost", "caInfo",
+ "method", "followRedirects", "maxRedirects", "sslVerifyCert", "callback" ];
foreach ( $members as $o ) {
if ( isset( $options[$o] ) ) {
* @return void
*/
public function proxySetup() {
- global $wgHTTPProxy;
-
// If there is an explicit proxy set and proxies are not disabled, then use it
if ( $this->proxy && !$this->noProxy ) {
return;
}
- // Otherwise, fallback to $wgHTTPProxy/http_proxy (when set) if this is not a machine
+ // Otherwise, fallback to $wgHTTPProxy if this is not a machine
// local URL and proxies are not disabled
- if ( Http::isLocalURL( $this->url ) || $this->noProxy ) {
+ if ( self::isLocalURL( $this->url ) || $this->noProxy ) {
$this->proxy = '';
- } elseif ( $wgHTTPProxy ) {
- $this->proxy = $wgHTTPProxy;
- } elseif ( getenv( "http_proxy" ) ) {
- $this->proxy = getenv( "http_proxy" );
+ } else {
+ $this->proxy = Http::getProxy();
}
}
+ /**
+ * Check if the URL can be served by localhost
+ *
+ * @param string $url Full url to check
+ * @return bool
+ */
+ private static function isLocalURL( $url ) {
+ global $wgCommandLineMode, $wgLocalVirtualHosts;
+
+ if ( $wgCommandLineMode ) {
+ return false;
+ }
+
+ // Extract host part
+ $matches = [];
+ if ( preg_match( '!^https?://([\w.-]+)[/:].*$!', $url, $matches ) ) {
+ $host = $matches[1];
+ // Split up dotwise
+ $domainParts = explode( '.', $host );
+ // Check if this domain or any superdomain is listed as a local virtual host
+ $domainParts = array_reverse( $domainParts );
+
+ $domain = '';
+ $countParts = count( $domainParts );
+ for ( $i = 0; $i < $countParts; $i++ ) {
+ $domainPart = $domainParts[$i];
+ if ( $i == 0 ) {
+ $domain = $domainPart;
+ } else {
+ $domain = $domainPart . '.' . $domain;
+ }
+
+ if ( in_array( $domain, $wgLocalVirtualHosts ) ) {
+ return true;
+ }
+ }
+ }
+
+ return false;
+ }
+
/**
* Set the user agent
* @param string $UA
* @return array
*/
public function getHeaderList() {
- $list = array();
+ $list = [];
if ( $this->cookieJar ) {
$this->reqHeaders['Cookie'] =
$this->proxySetup(); // set up any proxy as needed
if ( !$this->callback ) {
- $this->setCallback( array( $this, 'read' ) );
+ $this->setCallback( [ $this, 'read' ] );
}
if ( !isset( $this->reqHeaders['User-Agent'] ) ) {
* Returns the value of the given response header.
*
* @param string $header
- * @return string
+ * @return string|null
*/
public function getResponseHeader( $header ) {
if ( !$this->respHeaders ) {
class CurlHttpRequest extends MWHttpRequest {
const SUPPORTS_FILE_POSTS = true;
- protected $curlOptions = array();
+ protected $curlOptions = [];
protected $headerText = "";
/**
$this->curlOptions[CURLOPT_HTTP_VERSION] = CURL_HTTP_VERSION_1_0;
$this->curlOptions[CURLOPT_WRITEFUNCTION] = $this->callback;
- $this->curlOptions[CURLOPT_HEADERFUNCTION] = array( $this, "readHeader" );
+ $this->curlOptions[CURLOPT_HEADERFUNCTION] = [ $this, "readHeader" ];
$this->curlOptions[CURLOPT_MAXREDIRS] = $this->maxRedirects;
$this->curlOptions[CURLOPT_ENCODING] = ""; # Enable compression
class PhpHttpRequest extends MWHttpRequest {
- private $fopenErrors = array();
+ private $fopenErrors = [];
/**
* @param string $url
* @throws DomainException
*/
protected function getCertOptions() {
- $certOptions = array();
- $certLocations = array();
+ $certOptions = [];
+ $certLocations = [];
if ( $this->caInfo ) {
- $certLocations = array( 'manual' => $this->caInfo );
+ $certLocations = [ 'manual' => $this->caInfo ];
} elseif ( version_compare( PHP_VERSION, '5.6.0', '<' ) ) {
// @codingStandardsIgnoreStart Generic.Files.LineLength
// Default locations, based on
// PHP 5.6+ gets the CA location from OpenSSL as long as it is not set manually,
// so we should leave capath/cafile empty there.
// @codingStandardsIgnoreEnd
- $certLocations = array_filter( array(
+ $certLocations = array_filter( [
getenv( 'SSL_CERT_DIR' ),
getenv( 'SSL_CERT_PATH' ),
'/etc/pki/tls/certs/ca-bundle.crt', # Fedora et al
'/etc/pki/tls/certs/ca-bundle.trust.crt',
'/etc/pki/ca-trust/extracted/pem/tls-ca-bundle.pem',
'/System/Library/OpenSSL', # OSX
- ) );
+ ] );
}
foreach ( $certLocations as $key => $cert ) {
*/
public function errorHandler( $errno, $errstr ) {
$n = count( $this->fopenErrors ) + 1;
- $this->fopenErrors += array( "errno$n" => $errno, "errstr$n" => $errstr );
+ $this->fopenErrors += [ "errno$n" => $errno, "errstr$n" => $errstr ];
}
public function execute() {
}
// Set up PHP stream context
- $options = array(
- 'http' => array(
+ $options = [
+ 'http' => [
'method' => $this->method,
'header' => implode( "\r\n", $this->getHeaderList() ),
'protocol_version' => '1.1',
// Curl options in case curlwrappers are installed
'curl_verify_ssl_host' => $this->sslVerifyHost ? 2 : 0,
'curl_verify_ssl_peer' => $this->sslVerifyCert,
- ),
- 'ssl' => array(
+ ],
+ 'ssl' => [
'verify_peer' => $this->sslVerifyCert,
'SNI_enabled' => true,
'ciphers' => 'HIGH:!SSLv2:!SSLv3:-ADH:-kDH:-kECDH:-DSS',
'disable_compression' => true,
- ),
- );
+ ],
+ ];
if ( $this->proxy ) {
- $options['http']['proxy'] = $this->urlToTCP( $this->proxy );
+ $options['http']['proxy'] = $this->urlToTcp( $this->proxy );
$options['http']['request_fulluri'] = true;
}
$context = stream_context_create( $options );
- $this->headerList = array();
+ $this->headerList = [];
$reqCount = 0;
$url = $this->url;
- $result = array();
+ $result = [];
if ( $this->profiler ) {
$profileSection = $this->profiler->scopedProfileIn(
}
do {
$reqCount++;
- $this->fopenErrors = array();
- set_error_handler( array( $this, 'errorHandler' ) );
+ $this->fopenErrors = [];
+ set_error_handler( [ $this, 'errorHandler' ] );
$fh = fopen( $url, "r", false, $context );
restore_error_handler();