MWHttpRequest::factory() will pass in a logger to move the dependency up
to the factory instead of individual functions.
Change-Id: I4e428f060c90ef49cb3acb3e3dceab64bd952330
if ( $this->followRedirects && $this->canFollowRedirects() ) {
MediaWiki\suppressWarnings();
if ( !curl_setopt( $curlHandle, CURLOPT_FOLLOWLOCATION, true ) ) {
if ( $this->followRedirects && $this->canFollowRedirects() ) {
MediaWiki\suppressWarnings();
if ( !curl_setopt( $curlHandle, CURLOPT_FOLLOWLOCATION, true ) ) {
- wfDebug( __METHOD__ . ": Couldn't set CURLOPT_FOLLOWLOCATION. " .
+ $this->logger->debug( __METHOD__ . ": Couldn't set CURLOPT_FOLLOWLOCATION. " .
"Probably open_basedir is set.\n" );
// Continue the processing. If it were in curl_setopt_array,
// processing would have halted on its entry
"Probably open_basedir is set.\n" );
// Continue the processing. If it were in curl_setopt_array,
// processing would have halted on its entry
public function canFollowRedirects() {
$curlVersionInfo = curl_version();
if ( $curlVersionInfo['version_number'] < 0x071304 ) {
public function canFollowRedirects() {
$curlVersionInfo = curl_version();
if ( $curlVersionInfo['version_number'] < 0x071304 ) {
- wfDebug( "Cannot follow redirects with libcurl < 7.19.4 due to CVE-2009-0037\n" );
+ $this->logger->debug( "Cannot follow redirects with libcurl < 7.19.4 due to CVE-2009-0037\n" );
return false;
}
if ( version_compare( PHP_VERSION, '5.6.0', '<' ) ) {
if ( strval( ini_get( 'open_basedir' ) ) !== '' ) {
return false;
}
if ( version_compare( PHP_VERSION, '5.6.0', '<' ) ) {
if ( strval( ini_get( 'open_basedir' ) ) !== '' ) {
- wfDebug( "Cannot follow redirects when open_basedir is set\n" );
+ $this->logger->debug( "Cannot follow redirects when open_basedir is set\n" );
* to avoid attacks on intranet services accessible by HTTP.
* - userAgent A user agent, if you want to override the default
* MediaWiki/$wgVersion
* to avoid attacks on intranet services accessible by HTTP.
* - userAgent A user agent, if you want to override the default
* MediaWiki/$wgVersion
+ * - logger A \Psr\Logger\LoggerInterface instance for debug logging
* @param string $caller The method making this request, for profiling
* @return string|bool (bool)false on failure or a string on success
*/
* @param string $caller The method making this request, for profiling
* @return string|bool (bool)false on failure or a string on success
*/
+use MediaWiki\Logger\LoggerFactory;
+use Psr\Log\LoggerInterface;
+use Psr\Log\LoggerAwareInterface;
+use Psr\Log\NullLogger;
+
/**
* This wrapper class will call out to curl (if available) or fallback
* to regular PHP if necessary for handling internal HTTP requests.
/**
* This wrapper class will call out to curl (if available) or fallback
* to regular PHP if necessary for handling internal HTTP requests.
* Renamed from HttpRequest to MWHttpRequest to avoid conflict with
* PHP's HTTP extension.
*/
* Renamed from HttpRequest to MWHttpRequest to avoid conflict with
* PHP's HTTP extension.
*/
+class MWHttpRequest implements LoggerAwareInterface {
const SUPPORTS_FILE_POSTS = false;
protected $content;
const SUPPORTS_FILE_POSTS = false;
protected $content;
*/
protected $profileName;
*/
protected $profileName;
+ /**
+ * @var LoggerInterface;
+ */
+ protected $logger;
+
/**
* @param string $url Url to use. If protocol-relative, will be expanded to an http:// URL
* @param array $options (optional) extra params to pass (see Http::request())
/**
* @param string $url Url to use. If protocol-relative, will be expanded to an http:// URL
* @param array $options (optional) extra params to pass (see Http::request())
$this->url = wfExpandUrl( $url, PROTO_HTTP );
$this->parsedUrl = wfParseUrl( $this->url );
$this->url = wfExpandUrl( $url, PROTO_HTTP );
$this->parsedUrl = wfParseUrl( $this->url );
+ if ( isset( $options['logger'] ) ) {
+ $this->logger = $options['logger'];
+ } else {
+ $this->logger = new NullLogger();
+ }
+
if ( !$this->parsedUrl || !Http::isValidURI( $this->url ) ) {
$this->status = Status::newFatal( 'http-invalid-url', $url );
} else {
if ( !$this->parsedUrl || !Http::isValidURI( $this->url ) ) {
$this->status = Status::newFatal( 'http-invalid-url', $url );
} else {
$this->profileName = $caller;
}
$this->profileName = $caller;
}
+ /**
+ * @param LoggerInterface $logger
+ */
+ public function setLogger( LoggerInterface $logger ) {
+ $this->logger = $logger;
+ }
+
/**
* Simple function to test if we can make any sort of requests at all, using
* cURL or fopen()
/**
* Simple function to test if we can make any sort of requests at all, using
* cURL or fopen()
' Http::$httpEngine is set to "curl"' );
}
' Http::$httpEngine is set to "curl"' );
}
+ if ( !is_array( $options ) ) {
+ $options = [];
+ }
+
+ if ( !isset( $options['logger'] ) ) {
+ $options['logger'] = LoggerFactory::getInstance( 'http' );
+ }
+
switch ( Http::$httpEngine ) {
case 'curl':
return new CurlHttpRequest( $url, $options, $caller, Profiler::instance() );
switch ( Http::$httpEngine ) {
case 'curl':
return new CurlHttpRequest( $url, $options, $caller, Profiler::instance() );
-use MediaWiki\Logger\LoggerFactory;
class PhpHttpRequest extends MWHttpRequest {
class PhpHttpRequest extends MWHttpRequest {
$url = $this->getResponseHeader( "Location" );
if ( !Http::isValidURI( $url ) ) {
$url = $this->getResponseHeader( "Location" );
if ( !Http::isValidURI( $url ) ) {
- wfDebug( __METHOD__ . ": insecure redirection\n" );
+ $this->logger->debug( __METHOD__ . ": insecure redirection\n" );
break;
}
} while ( true );
break;
}
} while ( true );
if ( $fh === false ) {
if ( $this->fopenErrors ) {
if ( $fh === false ) {
if ( $this->fopenErrors ) {
- LoggerFactory::getInstance( 'http' )->warning( __CLASS__
+ $this->logger->warning( __CLASS__
. ': error opening connection: {errstr1}', $this->fopenErrors );
}
$this->status->fatal( 'http-request-error' );
. ': error opening connection: {errstr1}', $this->fopenErrors );
}
$this->status->fatal( 'http-request-error' );