X-Git-Url: https://git.heureux-cyclage.org/?a=blobdiff_plain;f=includes%2Fhttp%2FHttpRequestFactory.php;h=8433df6e0890cedc2f1385c44aaf3151326e2846;hb=327e8ea41681487382d8abe7cb111501954b9d1b;hp=a3a14d0aecce164a37937ff18c9eb774f8ded5ae;hpb=314291057be8864871100eaaaa291a9049ca9a65;p=lhc%2Fweb%2Fwiklou.git diff --git a/includes/http/HttpRequestFactory.php b/includes/http/HttpRequestFactory.php index a3a14d0aec..8433df6e08 100644 --- a/includes/http/HttpRequestFactory.php +++ b/includes/http/HttpRequestFactory.php @@ -20,34 +20,56 @@ namespace MediaWiki\Http; use CurlHttpRequest; -use DomainException; +use GuzzleHttpRequest; use Http; use MediaWiki\Logger\LoggerFactory; use MWHttpRequest; use PhpHttpRequest; use Profiler; -use GuzzleHttpRequest; +use RuntimeException; +use Status; /** * Factory creating MWHttpRequest objects. */ class HttpRequestFactory { - /** * Generate a new MWHttpRequest object * @param string $url Url to use - * @param array $options (optional) extra params to pass (see Http::request()) + * @param array $options Possible keys for the array: + * - timeout Timeout length in seconds + * - connectTimeout Timeout for connection, in seconds (curl only) + * - postData An array of key-value pairs or a url-encoded form data + * - proxy The proxy to use. + * Otherwise it will use $wgHTTPProxy (if set) + * Otherwise it will use the environment variable "http_proxy" (if set) + * - noProxy Don't use any proxy at all. Takes precedence over proxy value(s). + * - sslVerifyHost Verify hostname against certificate + * - sslVerifyCert Verify SSL certificate + * - caInfo Provide CA information + * - maxRedirects Maximum number of redirects to follow (defaults to 5) + * - followRedirects Whether to follow redirects (defaults to false). + * Note: this should only be used when the target URL is trusted, + * 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 + * - username Username for HTTP Basic Authentication + * - password Password for HTTP Basic Authentication + * - originalRequest Information about the original request (as a WebRequest object or + * an associative array with 'ip' and 'userAgent'). + * @codingStandardsIgnoreStart + * @phan-param array{timeout?:int,connectTimeout?:int,postData?:array,proxy?:string,noProxy?:bool,sslVerifyHost?:bool,sslVerifyCert?:bool,caInfo?:string,maxRedirects?:int,followRedirects?:bool,userAgent?:string,logger?:\Psr\Logger\LoggerInterface,username?:string,password?:string,originalRequest?:WebRequest|array{ip:string,userAgent:string}} $options + * @codingStandardsIgnoreEnd * @param string $caller The method making this request, for profiling - * @throws DomainException + * @throws RuntimeException * @return MWHttpRequest * @see MWHttpRequest::__construct + * @suppress PhanUndeclaredTypeParameter */ public function create( $url, array $options = [], $caller = __METHOD__ ) { if ( !Http::$httpEngine ) { Http::$httpEngine = 'guzzle'; - } elseif ( Http::$httpEngine == 'curl' && !function_exists( 'curl_init' ) ) { - throw new DomainException( __METHOD__ . ': curl (https://secure.php.net/curl) is not ' . - 'installed, but Http::$httpEngine is set to "curl"' ); } if ( !isset( $options['logger'] ) ) { @@ -60,16 +82,9 @@ class HttpRequestFactory { case 'curl': return new CurlHttpRequest( $url, $options, $caller, Profiler::instance() ); case 'php': - if ( !wfIniGetBool( 'allow_url_fopen' ) ) { - throw new DomainException( __METHOD__ . ': allow_url_fopen ' . - 'needs to be enabled for pure PHP http requests to ' . - 'work. If possible, curl should be used instead. See ' . - 'https://secure.php.net/curl.' - ); - } return new PhpHttpRequest( $url, $options, $caller, Profiler::instance() ); default: - throw new DomainException( __METHOD__ . ': The setting of Http::$httpEngine is not valid.' ); + throw new RuntimeException( __METHOD__ . ': The requested engine is not valid.' ); } } @@ -82,4 +97,75 @@ class HttpRequestFactory { return function_exists( 'curl_init' ) || wfIniGetBool( 'allow_url_fopen' ); } + /** + * Perform an HTTP request + * + * @since 1.34 + * @param string $method HTTP method. Usually GET/POST + * @param string $url Full URL to act on. If protocol-relative, will be expanded to an http:// + * URL + * @param array $options See HttpRequestFactory::create + * @param string $caller The method making this request, for profiling + * @return string|null null on failure or a string on success + */ + public function request( $method, $url, array $options = [], $caller = __METHOD__ ) { + $logger = LoggerFactory::getInstance( 'http' ); + $logger->debug( "$method: $url" ); + + $options['method'] = strtoupper( $method ); + + if ( !isset( $options['timeout'] ) ) { + $options['timeout'] = 'default'; + } + if ( !isset( $options['connectTimeout'] ) ) { + $options['connectTimeout'] = 'default'; + } + + $req = $this->create( $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 null; + } + } + + /** + * Simple wrapper for request( 'GET' ), parameters have same meaning as for request() + * + * @since 1.34 + * @param string $url + * @param array $options + * @param string $caller + * @return string|null + */ + public function get( $url, array $options = [], $caller = __METHOD__ ) { + return $this->request( 'GET', $url, $options, $caller ); + } + + /** + * Simple wrapper for request( 'POST' ), parameters have same meaning as for request() + * + * @since 1.34 + * @param string $url + * @param array $options + * @param string $caller + * @return string|null + */ + public function post( $url, array $options = [], $caller = __METHOD__ ) { + return $this->request( 'POST', $url, $options, $caller ); + } + + /** + * @return string + */ + public function getUserAgent() { + global $wgVersion; + + return "MediaWiki/$wgVersion"; + } }