X-Git-Url: https://git.heureux-cyclage.org/?p=lhc%2Fweb%2Fwiklou.git;a=blobdiff_plain;f=includes%2Fhttp%2FGuzzleHttpRequest.php;h=3af7f56a5d168f79d390ef48532a422a5960787d;hp=db8a09b9709469d4aaad7578f8c56567702da70f;hb=a3cd158d8433e41cbeb299f0e268dfef363b2afd;hpb=0770f85a0a293e6c7af6f1d3d3a1dbd2d13c1e09 diff --git a/includes/http/GuzzleHttpRequest.php b/includes/http/GuzzleHttpRequest.php index db8a09b970..3af7f56a5d 100644 --- a/includes/http/GuzzleHttpRequest.php +++ b/includes/http/GuzzleHttpRequest.php @@ -25,8 +25,10 @@ use GuzzleHttp\Psr7\Request; * MWHttpRequest implemented using the Guzzle library * * Differences from the CurlHttpRequest implementation: - * 1) the MWHttpRequest 'callback" option is unsupported. Instead, use the 'sink' option to - * send a filename/stream (see http://docs.guzzlephp.org/en/stable/request-options.html#sink) + * 1) a new 'sink' option is available as an alternative to callbacks. See: + * http://docs.guzzlephp.org/en/stable/request-options.html#sink) + * The 'callback' option remains available as well. If both 'sink' and 'callback' are + * specified, 'sink' is used. * 2) callers may set a custom handler via the 'handler' option. * If this is not set, Guzzle will use curl (if available) or PHP streams (otherwise) * 3) setting either sslVerifyHost or sslVerifyCert will enable both. Guzzle does not allow @@ -43,13 +45,13 @@ class GuzzleHttpRequest extends MWHttpRequest { /** * @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 array $options (optional) extra params to pass (see HttpRequestFactory::create()) * @param string $caller The method making this request, for profiling * @param Profiler|null $profiler An instance of the profiler for profiling, or null * @throws Exception */ public function __construct( - $url, array $options = [], $caller = __METHOD__, $profiler = null + $url, array $options = [], $caller = __METHOD__, Profiler $profiler = null ) { parent::__construct( $url, $options, $caller, $profiler ); @@ -61,6 +63,48 @@ class GuzzleHttpRequest extends MWHttpRequest { } } + /** + * Set a read callback to accept data read from the HTTP request. + * By default, data is appended to an internal buffer which can be + * retrieved through $req->getContent(). + * + * To handle data as it comes in -- especially for large files that + * would not fit in memory -- you can instead set your own callback, + * in the form function($resource, $buffer) where the first parameter + * is the low-level resource being read (implementation specific), + * and the second parameter is the data buffer. + * + * You MUST return the number of bytes handled in the buffer; if fewer + * bytes are reported handled than were passed to you, the HTTP fetch + * will be aborted. + * + * This function overrides any 'sink' or 'callback' constructor option. + * + * @param callable|null $callback + * @throws InvalidArgumentException + */ + public function setCallback( $callback ) { + $this->sink = null; + $this->doSetCallback( $callback ); + } + + /** + * Worker function for setting callbacks. Calls can originate both internally and externally + * via setCallback). Defaults to the internal read callback if $callback is null. + * + * If a sink is already specified, this does nothing. This causes the 'sink' constructor + * option to override the 'callback' constructor option. + * + * @param callable|null $callback + * @throws InvalidArgumentException + */ + protected function doSetCallback( $callback ) { + if ( !$this->sink ) { + parent::doSetCallback( $callback ); + $this->sink = new MWCallbackStream( $this->callback ); + } + } + /** * @see MWHttpRequest::execute * @@ -124,11 +168,9 @@ class GuzzleHttpRequest extends MWHttpRequest { $request = new Request( $this->method, $this->url ); $response = $client->send( $request ); $this->headerList = $response->getHeaders(); - $this->content = $response->getBody()->getContents(); $this->respVersion = $response->getProtocolVersion(); $this->respStatus = $response->getStatusCode() . ' ' . $response->getReasonPhrase(); - } catch ( GuzzleHttp\Exception\ConnectException $e ) { // ConnectException is thrown for several reasons besides generic "timeout": // Connection refused @@ -179,6 +221,11 @@ class GuzzleHttpRequest extends MWHttpRequest { return Status::wrap( $this->status ); // TODO B/C; move this to callers } + protected function prepare() { + $this->doSetCallback( $this->callback ); + parent::prepare(); + } + /** * @return bool */ @@ -189,7 +236,7 @@ class GuzzleHttpRequest extends MWHttpRequest { /** * Guzzle provides headers as an array. Reprocess to match our expectations. Guzzle will - * have already parsed and removed the status line (in EasyHandle::createResponse)z. + * have already parsed and removed the status line (in EasyHandle::createResponse). */ protected function parseHeader() { // Failure without (valid) headers gets a response status of zero