debug( "$method: $url" ); $options['method'] = strtoupper( $method ); if ( !isset( $options['timeout'] ) ) { $options['timeout'] = 'default'; } if ( !isset( $options['connectTimeout'] ) ) { $options['connectTimeout'] = 'default'; } $req = MWHttpRequest::factory( $url, $options, $caller ); $status = $req->execute(); if ( $status->isOK() ) { return $req->getContent(); } else { $errors = $status->getErrorsByType( 'error' ); $logger->warning( Status::wrap( $status )->getWikiText( false, false, 'en' ), [ 'error' => $errors, 'caller' => $caller, 'content' => $req->getContent() ] ); return false; } } /** * Simple wrapper for Http::request( 'GET' ) * @see Http::request() * @since 1.25 Second parameter $timeout removed. Second parameter * is now $options which can be given a 'timeout' * * @param string $url * @param array $options * @param string $caller The method making this request, for profiling * @return string|bool false on error */ 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] : []; $options['timeout'] = $args[1]; $caller = __METHOD__; } return self::request( 'GET', $url, $options, $caller ); } /** * Simple wrapper for Http::request( 'POST' ) * @see Http::request() * * @param string $url * @param array $options * @param string $caller The method making this request, for profiling * @return string|bool false on error */ public static function post( $url, $options = [], $caller = __METHOD__ ) { return self::request( 'POST', $url, $options, $caller ); } /** * A standard user-agent we can use for external requests. * @return string */ public static function userAgent() { global $wgVersion; return "MediaWiki/$wgVersion"; } /** * Checks that the given URI is a valid one. Hardcoding the * protocols, because we only want protocols that both cURL * and php support. * * file:// should not be allowed here for security purpose (r67684) * * @todo FIXME this is wildly inaccurate and fails to actually check most stuff * * @param string $uri URI to check for validity * @return bool */ public static function isValidURI( $uri ) { return (bool)preg_match( '/^https?:\/\/[^\/\s]\S*$/D', $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 ""; } /** * Get a configured MultiHttpClient * @param array $options * @return MultiHttpClient */ public static function createMultiClient( $options = [] ) { global $wgHTTPConnectTimeout, $wgHTTPTimeout, $wgHTTPProxy; return new MultiHttpClient( $options + [ 'connTimeout' => $wgHTTPConnectTimeout, 'reqTimeout' => $wgHTTPTimeout, 'userAgent' => self::userAgent(), 'proxy' => $wgHTTPProxy, 'logger' => LoggerFactory::getInstance( 'http' ) ] ); } }