Skip to content

Instantly share code, notes, and snippets.

@physikerwelt
Created July 14, 2021 21:41
Show Gist options
  • Save physikerwelt/e34f231aea94b939391524097f9e35d3 to your computer and use it in GitHub Desktop.
Save physikerwelt/e34f231aea94b939391524097f9e35d3 to your computer and use it in GitHub Desktop.
Possible solution for https://phabricator.wikimedia.org/T286542 from 567bc5a9359923b06470c483bfa116ac213d1066 in https://gerrit.wikimedia.org/r/mediawiki/core
diff --git a/includes/http/GuzzleHttpRequest.php b/includes/http/GuzzleHttpRequest.php
index 74edf8e861..3936aae52a 100644
--- a/includes/http/GuzzleHttpRequest.php
+++ b/includes/http/GuzzleHttpRequest.php
@@ -42,6 +42,7 @@ use Psr\Http\Message\RequestInterface;
class GuzzleHttpRequest extends MWHttpRequest {
public const SUPPORTS_FILE_POSTS = true;
+ protected $stream = null;
protected $handler = null;
protected $sink = null;
/** @var array */
@@ -67,6 +68,9 @@ class GuzzleHttpRequest extends MWHttpRequest {
if ( isset( $options['sink'] ) ) {
$this->sink = $options['sink'];
}
+ if ( isset( $options['stream'] ) ) {
+ $this->stream = $options['stream'];
+ }
}
/**
@@ -126,7 +130,12 @@ class GuzzleHttpRequest extends MWHttpRequest {
if ( $this->proxy ) {
$this->guzzleOptions['proxy'] = $this->proxy;
}
-
+ if ( $this->stream ) {
+ $this->guzzleOptions['stream'] = $this->stream;
+ if ( $this->timeout === 'default' ) {
+ $this->timeout = null;
+ }
+ }
$this->guzzleOptions['timeout'] = $this->timeout;
$this->guzzleOptions['connect_timeout'] = $this->connectTimeout;
$this->guzzleOptions['version'] = '1.1';
diff --git a/includes/http/HttpRequestFactory.php b/includes/http/HttpRequestFactory.php
index 2b55db8431..b7ffac00ac 100644
--- a/includes/http/HttpRequestFactory.php
+++ b/includes/http/HttpRequestFactory.php
@@ -87,6 +87,7 @@ class HttpRequestFactory {
* - password Password for HTTP Basic Authentication
* - originalRequest Information about the original request (as a WebRequest object or
* an associative array with 'ip' and 'userAgent').
+ * - stream Stream the response and don't wait for the end of the body.
* @phpcs:ignore Generic.Files.LineLength
* @phan-param array{timeout?:int|string,connectTimeout?:int|string,postData?:string|array,proxy?:?string,noProxy?:bool,sslVerifyHost?:bool,sslVerifyCert?:bool,caInfo?:?string,maxRedirects?:int,followRedirects?:bool,userAgent?:string,method?:string,logger?:\Psr\Log\LoggerInterface,username?:string,password?:string,originalRequest?:\WebRequest|array{ip:string,userAgent:string}} $options
* @param string $caller The method making this request, for profiling
@@ -102,18 +103,20 @@ class HttpRequestFactory {
if ( !isset( $options['logger'] ) ) {
$options['logger'] = $this->logger;
}
- $options['timeout'] = $this->normalizeTimeout(
- $options['timeout'] ?? null,
- $options['maxTimeout'] ?? null,
- $this->options->get( 'HTTPTimeout' ),
- $this->options->get( 'HTTPMaxTimeout' )
- );
- $options['connectTimeout'] = $this->normalizeTimeout(
- $options['connectTimeout'] ?? null,
- $options['maxConnectTimeout'] ?? null,
- $this->options->get( 'HTTPConnectTimeout' ),
- $this->options->get( 'HTTPMaxConnectTimeout' )
- );
+ if ( !isset($options['stream']) || $options['stream'] !== true ){
+ $options['timeout'] = $this->normalizeTimeout(
+ $options['timeout'] ?? null,
+ $options['maxTimeout'] ?? null,
+ $this->options->get( 'HTTPTimeout' ),
+ $this->options->get( 'HTTPMaxTimeout' )
+ );
+ $options['connectTimeout'] = $this->normalizeTimeout(
+ $options['connectTimeout'] ?? null,
+ $options['maxConnectTimeout'] ?? null,
+ $this->options->get( 'HTTPConnectTimeout' ),
+ $this->options->get( 'HTTPMaxConnectTimeout' )
+ );
+ }
switch ( Http::$httpEngine ) {
case 'guzzle':
diff --git a/includes/http/MWHttpRequest.php b/includes/http/MWHttpRequest.php
index e365658b17..cc523263b8 100644
--- a/includes/http/MWHttpRequest.php
+++ b/includes/http/MWHttpRequest.php
@@ -111,7 +111,7 @@ abstract class MWHttpRequest implements LoggerAwareInterface {
if ( isset( $options['timeout'] ) && $options['timeout'] != 'default' ) {
$this->timeout = $options['timeout'];
- } else {
+ } elseif ( !isset($options['stream']) || $options['stream'] !== true ) {
// The timeout should always be set by HttpRequestFactory, so this
// should only happen if the class was directly constructed
wfDeprecated( __METHOD__ . ' without the timeout option', '1.35' );
@@ -120,7 +120,7 @@ abstract class MWHttpRequest implements LoggerAwareInterface {
}
if ( isset( $options['connectTimeout'] ) && $options['connectTimeout'] != 'default' ) {
$this->connectTimeout = $options['connectTimeout'];
- } else {
+ } elseif ( !isset($options['stream']) || $options['stream'] !== true ) {
// The timeout should always be set by HttpRequestFactory, so this
// should only happen if the class was directly constructed
wfDeprecated( __METHOD__ . ' without the connectTimeout option', '1.35' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment