|
|
@@ -1,37 +1,49 @@
|
|
|
<?php
|
|
|
+
|
|
|
namespace GuzzleHttp\Handler;
|
|
|
|
|
|
use GuzzleHttp\Exception\ConnectException;
|
|
|
use GuzzleHttp\Exception\RequestException;
|
|
|
use GuzzleHttp\Promise\FulfilledPromise;
|
|
|
-use GuzzleHttp\Psr7;
|
|
|
+use GuzzleHttp\Promise\PromiseInterface;
|
|
|
use GuzzleHttp\Psr7\LazyOpenStream;
|
|
|
use GuzzleHttp\TransferStats;
|
|
|
+use GuzzleHttp\Utils;
|
|
|
use Psr\Http\Message\RequestInterface;
|
|
|
|
|
|
/**
|
|
|
* Creates curl resources from a request
|
|
|
+ *
|
|
|
+ * @final
|
|
|
*/
|
|
|
class CurlFactory implements CurlFactoryInterface
|
|
|
{
|
|
|
- const CURL_VERSION_STR = 'curl_version';
|
|
|
- const LOW_CURL_VERSION_NUMBER = '7.21.2';
|
|
|
+ public const CURL_VERSION_STR = 'curl_version';
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @deprecated
|
|
|
+ */
|
|
|
+ public const LOW_CURL_VERSION_NUMBER = '7.21.2';
|
|
|
|
|
|
- /** @var array */
|
|
|
+ /**
|
|
|
+ * @var resource[]
|
|
|
+ */
|
|
|
private $handles = [];
|
|
|
|
|
|
- /** @var int Total number of idle handles to keep in cache */
|
|
|
+ /**
|
|
|
+ * @var int Total number of idle handles to keep in cache
|
|
|
+ */
|
|
|
private $maxHandles;
|
|
|
|
|
|
/**
|
|
|
* @param int $maxHandles Maximum number of idle handles.
|
|
|
*/
|
|
|
- public function __construct($maxHandles)
|
|
|
+ public function __construct(int $maxHandles)
|
|
|
{
|
|
|
$this->maxHandles = $maxHandles;
|
|
|
}
|
|
|
|
|
|
- public function create(RequestInterface $request, array $options)
|
|
|
+ public function create(RequestInterface $request, array $options): EasyHandle
|
|
|
{
|
|
|
if (isset($options['curl']['body_as_string'])) {
|
|
|
$options['_body_as_string'] = $options['curl']['body_as_string'];
|
|
|
@@ -49,35 +61,35 @@ class CurlFactory implements CurlFactoryInterface
|
|
|
|
|
|
// Add handler options from the request configuration options
|
|
|
if (isset($options['curl'])) {
|
|
|
- $conf = array_replace($conf, $options['curl']);
|
|
|
+ $conf = \array_replace($conf, $options['curl']);
|
|
|
}
|
|
|
|
|
|
- $conf[CURLOPT_HEADERFUNCTION] = $this->createHeaderFn($easy);
|
|
|
+ $conf[\CURLOPT_HEADERFUNCTION] = $this->createHeaderFn($easy);
|
|
|
$easy->handle = $this->handles
|
|
|
- ? array_pop($this->handles)
|
|
|
- : curl_init();
|
|
|
+ ? \array_pop($this->handles)
|
|
|
+ : \curl_init();
|
|
|
curl_setopt_array($easy->handle, $conf);
|
|
|
|
|
|
return $easy;
|
|
|
}
|
|
|
|
|
|
- public function release(EasyHandle $easy)
|
|
|
+ public function release(EasyHandle $easy): void
|
|
|
{
|
|
|
$resource = $easy->handle;
|
|
|
unset($easy->handle);
|
|
|
|
|
|
- if (count($this->handles) >= $this->maxHandles) {
|
|
|
- curl_close($resource);
|
|
|
+ if (\count($this->handles) >= $this->maxHandles) {
|
|
|
+ \curl_close($resource);
|
|
|
} else {
|
|
|
// Remove all callback functions as they can hold onto references
|
|
|
// and are not cleaned up by curl_reset. Using curl_setopt_array
|
|
|
// does not work for some reason, so removing each one
|
|
|
// individually.
|
|
|
- curl_setopt($resource, CURLOPT_HEADERFUNCTION, null);
|
|
|
- curl_setopt($resource, CURLOPT_READFUNCTION, null);
|
|
|
- curl_setopt($resource, CURLOPT_WRITEFUNCTION, null);
|
|
|
- curl_setopt($resource, CURLOPT_PROGRESSFUNCTION, null);
|
|
|
- curl_reset($resource);
|
|
|
+ \curl_setopt($resource, \CURLOPT_HEADERFUNCTION, null);
|
|
|
+ \curl_setopt($resource, \CURLOPT_READFUNCTION, null);
|
|
|
+ \curl_setopt($resource, \CURLOPT_WRITEFUNCTION, null);
|
|
|
+ \curl_setopt($resource, \CURLOPT_PROGRESSFUNCTION, null);
|
|
|
+ \curl_reset($resource);
|
|
|
$this->handles[] = $resource;
|
|
|
}
|
|
|
}
|
|
|
@@ -86,17 +98,14 @@ class CurlFactory implements CurlFactoryInterface
|
|
|
* Completes a cURL transaction, either returning a response promise or a
|
|
|
* rejected promise.
|
|
|
*
|
|
|
- * @param callable $handler
|
|
|
- * @param EasyHandle $easy
|
|
|
- * @param CurlFactoryInterface $factory Dictates how the handle is released
|
|
|
- *
|
|
|
- * @return \GuzzleHttp\Promise\PromiseInterface
|
|
|
+ * @param callable(RequestInterface, array): PromiseInterface $handler
|
|
|
+ * @param CurlFactoryInterface $factory Dictates how the handle is released
|
|
|
*/
|
|
|
public static function finish(
|
|
|
callable $handler,
|
|
|
EasyHandle $easy,
|
|
|
CurlFactoryInterface $factory
|
|
|
- ) {
|
|
|
+ ): PromiseInterface {
|
|
|
if (isset($easy->options['on_stats'])) {
|
|
|
self::invokeStats($easy);
|
|
|
}
|
|
|
@@ -117,10 +126,10 @@ class CurlFactory implements CurlFactoryInterface
|
|
|
return new FulfilledPromise($easy->response);
|
|
|
}
|
|
|
|
|
|
- private static function invokeStats(EasyHandle $easy)
|
|
|
+ private static function invokeStats(EasyHandle $easy): void
|
|
|
{
|
|
|
- $curlStats = curl_getinfo($easy->handle);
|
|
|
- $curlStats['appconnect_time'] = curl_getinfo($easy->handle, CURLINFO_APPCONNECT_TIME);
|
|
|
+ $curlStats = \curl_getinfo($easy->handle);
|
|
|
+ $curlStats['appconnect_time'] = \curl_getinfo($easy->handle, \CURLINFO_APPCONNECT_TIME);
|
|
|
$stats = new TransferStats(
|
|
|
$easy->request,
|
|
|
$easy->response,
|
|
|
@@ -128,21 +137,24 @@ class CurlFactory implements CurlFactoryInterface
|
|
|
$easy->errno,
|
|
|
$curlStats
|
|
|
);
|
|
|
- call_user_func($easy->options['on_stats'], $stats);
|
|
|
+ ($easy->options['on_stats'])($stats);
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * @param callable(RequestInterface, array): PromiseInterface $handler
|
|
|
+ */
|
|
|
private static function finishError(
|
|
|
callable $handler,
|
|
|
EasyHandle $easy,
|
|
|
CurlFactoryInterface $factory
|
|
|
- ) {
|
|
|
+ ): PromiseInterface {
|
|
|
// Get error information and release the handle to the factory.
|
|
|
$ctx = [
|
|
|
'errno' => $easy->errno,
|
|
|
- 'error' => curl_error($easy->handle),
|
|
|
- 'appconnect_time' => curl_getinfo($easy->handle, CURLINFO_APPCONNECT_TIME),
|
|
|
- ] + curl_getinfo($easy->handle);
|
|
|
- $ctx[self::CURL_VERSION_STR] = curl_version()['version'];
|
|
|
+ 'error' => \curl_error($easy->handle),
|
|
|
+ 'appconnect_time' => \curl_getinfo($easy->handle, \CURLINFO_APPCONNECT_TIME),
|
|
|
+ ] + \curl_getinfo($easy->handle);
|
|
|
+ $ctx[self::CURL_VERSION_STR] = \curl_version()['version'];
|
|
|
$factory->release($easy);
|
|
|
|
|
|
// Retry when nothing is present or when curl failed to rewind.
|
|
|
@@ -155,14 +167,14 @@ class CurlFactory implements CurlFactoryInterface
|
|
|
return self::createRejection($easy, $ctx);
|
|
|
}
|
|
|
|
|
|
- private static function createRejection(EasyHandle $easy, array $ctx)
|
|
|
+ private static function createRejection(EasyHandle $easy, array $ctx): PromiseInterface
|
|
|
{
|
|
|
static $connectionErrors = [
|
|
|
- CURLE_OPERATION_TIMEOUTED => true,
|
|
|
- CURLE_COULDNT_RESOLVE_HOST => true,
|
|
|
- CURLE_COULDNT_CONNECT => true,
|
|
|
- CURLE_SSL_CONNECT_ERROR => true,
|
|
|
- CURLE_GOT_NOTHING => true,
|
|
|
+ \CURLE_OPERATION_TIMEOUTED => true,
|
|
|
+ \CURLE_COULDNT_RESOLVE_HOST => true,
|
|
|
+ \CURLE_COULDNT_CONNECT => true,
|
|
|
+ \CURLE_SSL_CONNECT_ERROR => true,
|
|
|
+ \CURLE_GOT_NOTHING => true,
|
|
|
];
|
|
|
|
|
|
// If an exception was encountered during the onHeaders event, then
|
|
|
@@ -178,21 +190,16 @@ class CurlFactory implements CurlFactoryInterface
|
|
|
)
|
|
|
);
|
|
|
}
|
|
|
- if (version_compare($ctx[self::CURL_VERSION_STR], self::LOW_CURL_VERSION_NUMBER)) {
|
|
|
- $message = sprintf(
|
|
|
- 'cURL error %s: %s (%s)',
|
|
|
- $ctx['errno'],
|
|
|
- $ctx['error'],
|
|
|
- 'see https://curl.haxx.se/libcurl/c/libcurl-errors.html'
|
|
|
- );
|
|
|
- } else {
|
|
|
- $message = sprintf(
|
|
|
- 'cURL error %s: %s (%s) for %s',
|
|
|
- $ctx['errno'],
|
|
|
- $ctx['error'],
|
|
|
- 'see https://curl.haxx.se/libcurl/c/libcurl-errors.html',
|
|
|
- $easy->request->getUri()
|
|
|
- );
|
|
|
+
|
|
|
+ $message = \sprintf(
|
|
|
+ 'cURL error %s: %s (%s)',
|
|
|
+ $ctx['errno'],
|
|
|
+ $ctx['error'],
|
|
|
+ 'see https://curl.haxx.se/libcurl/c/libcurl-errors.html'
|
|
|
+ );
|
|
|
+ $uriString = (string) $easy->request->getUri();
|
|
|
+ if ($uriString !== '' && false === \strpos($ctx['error'], $uriString)) {
|
|
|
+ $message .= \sprintf(' for %s', $uriString);
|
|
|
}
|
|
|
|
|
|
// Create a connection exception if it was a specific error code.
|
|
|
@@ -203,34 +210,37 @@ class CurlFactory implements CurlFactoryInterface
|
|
|
return \GuzzleHttp\Promise\rejection_for($error);
|
|
|
}
|
|
|
|
|
|
- private function getDefaultConf(EasyHandle $easy)
|
|
|
+ /**
|
|
|
+ * @return array<int|string, mixed>
|
|
|
+ */
|
|
|
+ private function getDefaultConf(EasyHandle $easy): array
|
|
|
{
|
|
|
$conf = [
|
|
|
- '_headers' => $easy->request->getHeaders(),
|
|
|
- CURLOPT_CUSTOMREQUEST => $easy->request->getMethod(),
|
|
|
- CURLOPT_URL => (string) $easy->request->getUri()->withFragment(''),
|
|
|
- CURLOPT_RETURNTRANSFER => false,
|
|
|
- CURLOPT_HEADER => false,
|
|
|
- CURLOPT_CONNECTTIMEOUT => 150,
|
|
|
+ '_headers' => $easy->request->getHeaders(),
|
|
|
+ \CURLOPT_CUSTOMREQUEST => $easy->request->getMethod(),
|
|
|
+ \CURLOPT_URL => (string) $easy->request->getUri()->withFragment(''),
|
|
|
+ \CURLOPT_RETURNTRANSFER => false,
|
|
|
+ \CURLOPT_HEADER => false,
|
|
|
+ \CURLOPT_CONNECTTIMEOUT => 150,
|
|
|
];
|
|
|
|
|
|
- if (defined('CURLOPT_PROTOCOLS')) {
|
|
|
- $conf[CURLOPT_PROTOCOLS] = CURLPROTO_HTTP | CURLPROTO_HTTPS;
|
|
|
+ if (\defined('CURLOPT_PROTOCOLS')) {
|
|
|
+ $conf[\CURLOPT_PROTOCOLS] = \CURLPROTO_HTTP | \CURLPROTO_HTTPS;
|
|
|
}
|
|
|
|
|
|
$version = $easy->request->getProtocolVersion();
|
|
|
if ($version == 1.1) {
|
|
|
- $conf[CURLOPT_HTTP_VERSION] = CURL_HTTP_VERSION_1_1;
|
|
|
+ $conf[\CURLOPT_HTTP_VERSION] = \CURL_HTTP_VERSION_1_1;
|
|
|
} elseif ($version == 2.0) {
|
|
|
- $conf[CURLOPT_HTTP_VERSION] = CURL_HTTP_VERSION_2_0;
|
|
|
+ $conf[\CURLOPT_HTTP_VERSION] = \CURL_HTTP_VERSION_2_0;
|
|
|
} else {
|
|
|
- $conf[CURLOPT_HTTP_VERSION] = CURL_HTTP_VERSION_1_0;
|
|
|
+ $conf[\CURLOPT_HTTP_VERSION] = \CURL_HTTP_VERSION_1_0;
|
|
|
}
|
|
|
|
|
|
return $conf;
|
|
|
}
|
|
|
|
|
|
- private function applyMethod(EasyHandle $easy, array &$conf)
|
|
|
+ private function applyMethod(EasyHandle $easy, array &$conf): void
|
|
|
{
|
|
|
$body = $easy->request->getBody();
|
|
|
$size = $body->getSize();
|
|
|
@@ -242,22 +252,22 @@ class CurlFactory implements CurlFactoryInterface
|
|
|
|
|
|
$method = $easy->request->getMethod();
|
|
|
if ($method === 'PUT' || $method === 'POST') {
|
|
|
- // See http://tools.ietf.org/html/rfc7230#section-3.3.2
|
|
|
+ // See https://tools.ietf.org/html/rfc7230#section-3.3.2
|
|
|
if (!$easy->request->hasHeader('Content-Length')) {
|
|
|
- $conf[CURLOPT_HTTPHEADER][] = 'Content-Length: 0';
|
|
|
+ $conf[\CURLOPT_HTTPHEADER][] = 'Content-Length: 0';
|
|
|
}
|
|
|
} elseif ($method === 'HEAD') {
|
|
|
- $conf[CURLOPT_NOBODY] = true;
|
|
|
+ $conf[\CURLOPT_NOBODY] = true;
|
|
|
unset(
|
|
|
- $conf[CURLOPT_WRITEFUNCTION],
|
|
|
- $conf[CURLOPT_READFUNCTION],
|
|
|
- $conf[CURLOPT_FILE],
|
|
|
- $conf[CURLOPT_INFILE]
|
|
|
+ $conf[\CURLOPT_WRITEFUNCTION],
|
|
|
+ $conf[\CURLOPT_READFUNCTION],
|
|
|
+ $conf[\CURLOPT_FILE],
|
|
|
+ $conf[\CURLOPT_INFILE]
|
|
|
);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- private function applyBody(RequestInterface $request, array $options, array &$conf)
|
|
|
+ private function applyBody(RequestInterface $request, array $options, array &$conf): void
|
|
|
{
|
|
|
$size = $request->hasHeader('Content-Length')
|
|
|
? (int) $request->getHeaderLine('Content-Length')
|
|
|
@@ -268,37 +278,37 @@ class CurlFactory implements CurlFactoryInterface
|
|
|
if (($size !== null && $size < 1000000) ||
|
|
|
!empty($options['_body_as_string'])
|
|
|
) {
|
|
|
- $conf[CURLOPT_POSTFIELDS] = (string) $request->getBody();
|
|
|
+ $conf[\CURLOPT_POSTFIELDS] = (string) $request->getBody();
|
|
|
// Don't duplicate the Content-Length header
|
|
|
$this->removeHeader('Content-Length', $conf);
|
|
|
$this->removeHeader('Transfer-Encoding', $conf);
|
|
|
} else {
|
|
|
- $conf[CURLOPT_UPLOAD] = true;
|
|
|
+ $conf[\CURLOPT_UPLOAD] = true;
|
|
|
if ($size !== null) {
|
|
|
- $conf[CURLOPT_INFILESIZE] = $size;
|
|
|
+ $conf[\CURLOPT_INFILESIZE] = $size;
|
|
|
$this->removeHeader('Content-Length', $conf);
|
|
|
}
|
|
|
$body = $request->getBody();
|
|
|
if ($body->isSeekable()) {
|
|
|
$body->rewind();
|
|
|
}
|
|
|
- $conf[CURLOPT_READFUNCTION] = function ($ch, $fd, $length) use ($body) {
|
|
|
+ $conf[\CURLOPT_READFUNCTION] = static function ($ch, $fd, $length) use ($body) {
|
|
|
return $body->read($length);
|
|
|
};
|
|
|
}
|
|
|
|
|
|
// If the Expect header is not present, prevent curl from adding it
|
|
|
if (!$request->hasHeader('Expect')) {
|
|
|
- $conf[CURLOPT_HTTPHEADER][] = 'Expect:';
|
|
|
+ $conf[\CURLOPT_HTTPHEADER][] = 'Expect:';
|
|
|
}
|
|
|
|
|
|
// cURL sometimes adds a content-type by default. Prevent this.
|
|
|
if (!$request->hasHeader('Content-Type')) {
|
|
|
- $conf[CURLOPT_HTTPHEADER][] = 'Content-Type:';
|
|
|
+ $conf[\CURLOPT_HTTPHEADER][] = 'Content-Type:';
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- private function applyHeaders(EasyHandle $easy, array &$conf)
|
|
|
+ private function applyHeaders(EasyHandle $easy, array &$conf): void
|
|
|
{
|
|
|
foreach ($conf['_headers'] as $name => $values) {
|
|
|
foreach ($values as $value) {
|
|
|
@@ -306,16 +316,16 @@ class CurlFactory implements CurlFactoryInterface
|
|
|
if ($value === '') {
|
|
|
// cURL requires a special format for empty headers.
|
|
|
// See https://github.com/guzzle/guzzle/issues/1882 for more details.
|
|
|
- $conf[CURLOPT_HTTPHEADER][] = "$name;";
|
|
|
+ $conf[\CURLOPT_HTTPHEADER][] = "$name;";
|
|
|
} else {
|
|
|
- $conf[CURLOPT_HTTPHEADER][] = "$name: $value";
|
|
|
+ $conf[\CURLOPT_HTTPHEADER][] = "$name: $value";
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
// Remove the Accept header if one was not set
|
|
|
if (!$easy->request->hasHeader('Accept')) {
|
|
|
- $conf[CURLOPT_HTTPHEADER][] = 'Accept:';
|
|
|
+ $conf[\CURLOPT_HTTPHEADER][] = 'Accept:';
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@@ -325,41 +335,47 @@ class CurlFactory implements CurlFactoryInterface
|
|
|
* @param string $name Case-insensitive header to remove
|
|
|
* @param array $options Array of options to modify
|
|
|
*/
|
|
|
- private function removeHeader($name, array &$options)
|
|
|
+ private function removeHeader(string $name, array &$options): void
|
|
|
{
|
|
|
- foreach (array_keys($options['_headers']) as $key) {
|
|
|
- if (!strcasecmp($key, $name)) {
|
|
|
+ foreach (\array_keys($options['_headers']) as $key) {
|
|
|
+ if (!\strcasecmp($key, $name)) {
|
|
|
unset($options['_headers'][$key]);
|
|
|
return;
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- private function applyHandlerOptions(EasyHandle $easy, array &$conf)
|
|
|
+ private function applyHandlerOptions(EasyHandle $easy, array &$conf): void
|
|
|
{
|
|
|
$options = $easy->options;
|
|
|
if (isset($options['verify'])) {
|
|
|
if ($options['verify'] === false) {
|
|
|
- unset($conf[CURLOPT_CAINFO]);
|
|
|
- $conf[CURLOPT_SSL_VERIFYHOST] = 0;
|
|
|
- $conf[CURLOPT_SSL_VERIFYPEER] = false;
|
|
|
+ unset($conf[\CURLOPT_CAINFO]);
|
|
|
+ $conf[\CURLOPT_SSL_VERIFYHOST] = 0;
|
|
|
+ $conf[\CURLOPT_SSL_VERIFYPEER] = false;
|
|
|
} else {
|
|
|
- $conf[CURLOPT_SSL_VERIFYHOST] = 2;
|
|
|
- $conf[CURLOPT_SSL_VERIFYPEER] = true;
|
|
|
- if (is_string($options['verify'])) {
|
|
|
+ $conf[\CURLOPT_SSL_VERIFYHOST] = 2;
|
|
|
+ $conf[\CURLOPT_SSL_VERIFYPEER] = true;
|
|
|
+ if (\is_string($options['verify'])) {
|
|
|
// Throw an error if the file/folder/link path is not valid or doesn't exist.
|
|
|
- if (!file_exists($options['verify'])) {
|
|
|
+ if (!\file_exists($options['verify'])) {
|
|
|
throw new \InvalidArgumentException(
|
|
|
"SSL CA bundle not found: {$options['verify']}"
|
|
|
);
|
|
|
}
|
|
|
// If it's a directory or a link to a directory use CURLOPT_CAPATH.
|
|
|
// If not, it's probably a file, or a link to a file, so use CURLOPT_CAINFO.
|
|
|
- if (is_dir($options['verify']) ||
|
|
|
- (is_link($options['verify']) && is_dir(readlink($options['verify'])))) {
|
|
|
- $conf[CURLOPT_CAPATH] = $options['verify'];
|
|
|
+ if (
|
|
|
+ \is_dir($options['verify']) ||
|
|
|
+ (
|
|
|
+ \is_link($options['verify']) === true &&
|
|
|
+ ($verifyLink = \readlink($options['verify'])) !== false &&
|
|
|
+ \is_dir($verifyLink)
|
|
|
+ )
|
|
|
+ ) {
|
|
|
+ $conf[\CURLOPT_CAPATH] = $options['verify'];
|
|
|
} else {
|
|
|
- $conf[CURLOPT_CAINFO] = $options['verify'];
|
|
|
+ $conf[\CURLOPT_CAINFO] = $options['verify'];
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
@@ -368,72 +384,74 @@ class CurlFactory implements CurlFactoryInterface
|
|
|
if (!empty($options['decode_content'])) {
|
|
|
$accept = $easy->request->getHeaderLine('Accept-Encoding');
|
|
|
if ($accept) {
|
|
|
- $conf[CURLOPT_ENCODING] = $accept;
|
|
|
+ $conf[\CURLOPT_ENCODING] = $accept;
|
|
|
} else {
|
|
|
- $conf[CURLOPT_ENCODING] = '';
|
|
|
+ $conf[\CURLOPT_ENCODING] = '';
|
|
|
// Don't let curl send the header over the wire
|
|
|
- $conf[CURLOPT_HTTPHEADER][] = 'Accept-Encoding:';
|
|
|
+ $conf[\CURLOPT_HTTPHEADER][] = 'Accept-Encoding:';
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- if (isset($options['sink'])) {
|
|
|
+ // Do not connect a sink for HEAD requests.
|
|
|
+ if ($easy->request->getMethod() !== 'HEAD') {
|
|
|
+ if (!isset($options['sink'])) {
|
|
|
+ // Use a default temp stream if no sink was set.
|
|
|
+ $options['sink'] = \fopen('php://temp', 'w+');
|
|
|
+ }
|
|
|
$sink = $options['sink'];
|
|
|
- if (!is_string($sink)) {
|
|
|
+ if (!\is_string($sink)) {
|
|
|
$sink = \GuzzleHttp\Psr7\stream_for($sink);
|
|
|
- } elseif (!is_dir(dirname($sink))) {
|
|
|
+ } elseif (!\is_dir(\dirname($sink))) {
|
|
|
// Ensure that the directory exists before failing in curl.
|
|
|
- throw new \RuntimeException(sprintf(
|
|
|
+ throw new \RuntimeException(\sprintf(
|
|
|
'Directory %s does not exist for sink value of %s',
|
|
|
- dirname($sink),
|
|
|
+ \dirname($sink),
|
|
|
$sink
|
|
|
));
|
|
|
} else {
|
|
|
$sink = new LazyOpenStream($sink, 'w+');
|
|
|
}
|
|
|
$easy->sink = $sink;
|
|
|
- $conf[CURLOPT_WRITEFUNCTION] = function ($ch, $write) use ($sink) {
|
|
|
+ $conf[\CURLOPT_WRITEFUNCTION] = static function ($ch, $write) use ($sink): int {
|
|
|
return $sink->write($write);
|
|
|
};
|
|
|
- } else {
|
|
|
- // Use a default temp stream if no sink was set.
|
|
|
- $conf[CURLOPT_FILE] = fopen('php://temp', 'w+');
|
|
|
- $easy->sink = Psr7\stream_for($conf[CURLOPT_FILE]);
|
|
|
}
|
|
|
+
|
|
|
$timeoutRequiresNoSignal = false;
|
|
|
if (isset($options['timeout'])) {
|
|
|
$timeoutRequiresNoSignal |= $options['timeout'] < 1;
|
|
|
- $conf[CURLOPT_TIMEOUT_MS] = $options['timeout'] * 1000;
|
|
|
+ $conf[\CURLOPT_TIMEOUT_MS] = $options['timeout'] * 1000;
|
|
|
}
|
|
|
|
|
|
// CURL default value is CURL_IPRESOLVE_WHATEVER
|
|
|
if (isset($options['force_ip_resolve'])) {
|
|
|
if ('v4' === $options['force_ip_resolve']) {
|
|
|
- $conf[CURLOPT_IPRESOLVE] = CURL_IPRESOLVE_V4;
|
|
|
+ $conf[\CURLOPT_IPRESOLVE] = \CURL_IPRESOLVE_V4;
|
|
|
} elseif ('v6' === $options['force_ip_resolve']) {
|
|
|
- $conf[CURLOPT_IPRESOLVE] = CURL_IPRESOLVE_V6;
|
|
|
+ $conf[\CURLOPT_IPRESOLVE] = \CURL_IPRESOLVE_V6;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
if (isset($options['connect_timeout'])) {
|
|
|
$timeoutRequiresNoSignal |= $options['connect_timeout'] < 1;
|
|
|
- $conf[CURLOPT_CONNECTTIMEOUT_MS] = $options['connect_timeout'] * 1000;
|
|
|
+ $conf[\CURLOPT_CONNECTTIMEOUT_MS] = $options['connect_timeout'] * 1000;
|
|
|
}
|
|
|
|
|
|
- if ($timeoutRequiresNoSignal && strtoupper(substr(PHP_OS, 0, 3)) !== 'WIN') {
|
|
|
- $conf[CURLOPT_NOSIGNAL] = true;
|
|
|
+ if ($timeoutRequiresNoSignal && \strtoupper(\substr(\PHP_OS, 0, 3)) !== 'WIN') {
|
|
|
+ $conf[\CURLOPT_NOSIGNAL] = true;
|
|
|
}
|
|
|
|
|
|
if (isset($options['proxy'])) {
|
|
|
- if (!is_array($options['proxy'])) {
|
|
|
- $conf[CURLOPT_PROXY] = $options['proxy'];
|
|
|
+ if (!\is_array($options['proxy'])) {
|
|
|
+ $conf[\CURLOPT_PROXY] = $options['proxy'];
|
|
|
} else {
|
|
|
$scheme = $easy->request->getUri()->getScheme();
|
|
|
if (isset($options['proxy'][$scheme])) {
|
|
|
$host = $easy->request->getUri()->getHost();
|
|
|
if (!isset($options['proxy']['no']) ||
|
|
|
- !\GuzzleHttp\is_host_in_noproxy($host, $options['proxy']['no'])
|
|
|
+ !Utils::isHostInNoProxy($host, $options['proxy']['no'])
|
|
|
) {
|
|
|
- $conf[CURLOPT_PROXY] = $options['proxy'][$scheme];
|
|
|
+ $conf[\CURLOPT_PROXY] = $options['proxy'][$scheme];
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
@@ -441,58 +459,53 @@ class CurlFactory implements CurlFactoryInterface
|
|
|
|
|
|
if (isset($options['cert'])) {
|
|
|
$cert = $options['cert'];
|
|
|
- if (is_array($cert)) {
|
|
|
- $conf[CURLOPT_SSLCERTPASSWD] = $cert[1];
|
|
|
+ if (\is_array($cert)) {
|
|
|
+ $conf[\CURLOPT_SSLCERTPASSWD] = $cert[1];
|
|
|
$cert = $cert[0];
|
|
|
}
|
|
|
- if (!file_exists($cert)) {
|
|
|
+ if (!\file_exists($cert)) {
|
|
|
throw new \InvalidArgumentException(
|
|
|
"SSL certificate not found: {$cert}"
|
|
|
);
|
|
|
}
|
|
|
- $conf[CURLOPT_SSLCERT] = $cert;
|
|
|
+ $conf[\CURLOPT_SSLCERT] = $cert;
|
|
|
}
|
|
|
|
|
|
if (isset($options['ssl_key'])) {
|
|
|
- if (is_array($options['ssl_key'])) {
|
|
|
- if (count($options['ssl_key']) === 2) {
|
|
|
- list($sslKey, $conf[CURLOPT_SSLKEYPASSWD]) = $options['ssl_key'];
|
|
|
+ if (\is_array($options['ssl_key'])) {
|
|
|
+ if (\count($options['ssl_key']) === 2) {
|
|
|
+ [$sslKey, $conf[\CURLOPT_SSLKEYPASSWD]] = $options['ssl_key'];
|
|
|
} else {
|
|
|
- list($sslKey) = $options['ssl_key'];
|
|
|
+ [$sslKey] = $options['ssl_key'];
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- $sslKey = isset($sslKey) ? $sslKey: $options['ssl_key'];
|
|
|
+ $sslKey = $sslKey ?? $options['ssl_key'];
|
|
|
|
|
|
- if (!file_exists($sslKey)) {
|
|
|
+ if (!\file_exists($sslKey)) {
|
|
|
throw new \InvalidArgumentException(
|
|
|
"SSL private key not found: {$sslKey}"
|
|
|
);
|
|
|
}
|
|
|
- $conf[CURLOPT_SSLKEY] = $sslKey;
|
|
|
+ $conf[\CURLOPT_SSLKEY] = $sslKey;
|
|
|
}
|
|
|
|
|
|
if (isset($options['progress'])) {
|
|
|
$progress = $options['progress'];
|
|
|
- if (!is_callable($progress)) {
|
|
|
+ if (!\is_callable($progress)) {
|
|
|
throw new \InvalidArgumentException(
|
|
|
'progress client option must be callable'
|
|
|
);
|
|
|
}
|
|
|
- $conf[CURLOPT_NOPROGRESS] = false;
|
|
|
- $conf[CURLOPT_PROGRESSFUNCTION] = function () use ($progress) {
|
|
|
- $args = func_get_args();
|
|
|
- // PHP 5.5 pushed the handle onto the start of the args
|
|
|
- if (is_resource($args[0])) {
|
|
|
- array_shift($args);
|
|
|
- }
|
|
|
- call_user_func_array($progress, $args);
|
|
|
+ $conf[\CURLOPT_NOPROGRESS] = false;
|
|
|
+ $conf[\CURLOPT_PROGRESSFUNCTION] = static function ($resource, int $downloadSize, int $downloaded, int $uploadSize, int $uploaded) use ($progress) {
|
|
|
+ $progress($downloadSize, $downloaded, $uploadSize, $uploaded);
|
|
|
};
|
|
|
}
|
|
|
|
|
|
if (!empty($options['debug'])) {
|
|
|
- $conf[CURLOPT_STDERR] = \GuzzleHttp\debug_resource($options['debug']);
|
|
|
- $conf[CURLOPT_VERBOSE] = true;
|
|
|
+ $conf[\CURLOPT_STDERR] = Utils::debugResource($options['debug']);
|
|
|
+ $conf[\CURLOPT_VERBOSE] = true;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@@ -504,12 +517,14 @@ class CurlFactory implements CurlFactoryInterface
|
|
|
* stream, and then encountered a "necessary data rewind wasn't possible"
|
|
|
* error, causing the request to be sent through curl_multi_info_read()
|
|
|
* without an error status.
|
|
|
+ *
|
|
|
+ * @param callable(RequestInterface, array): PromiseInterface $handler
|
|
|
*/
|
|
|
private static function retryFailedRewind(
|
|
|
callable $handler,
|
|
|
EasyHandle $easy,
|
|
|
array $ctx
|
|
|
- ) {
|
|
|
+ ): PromiseInterface {
|
|
|
try {
|
|
|
// Only rewind if the body has been read from.
|
|
|
$body = $easy->request->getBody();
|
|
|
@@ -542,24 +557,24 @@ class CurlFactory implements CurlFactoryInterface
|
|
|
return $handler($easy->request, $easy->options);
|
|
|
}
|
|
|
|
|
|
- private function createHeaderFn(EasyHandle $easy)
|
|
|
+ private function createHeaderFn(EasyHandle $easy): callable
|
|
|
{
|
|
|
if (isset($easy->options['on_headers'])) {
|
|
|
$onHeaders = $easy->options['on_headers'];
|
|
|
|
|
|
- if (!is_callable($onHeaders)) {
|
|
|
+ if (!\is_callable($onHeaders)) {
|
|
|
throw new \InvalidArgumentException('on_headers must be callable');
|
|
|
}
|
|
|
} else {
|
|
|
$onHeaders = null;
|
|
|
}
|
|
|
|
|
|
- return function ($ch, $h) use (
|
|
|
+ return static function ($ch, $h) use (
|
|
|
$onHeaders,
|
|
|
$easy,
|
|
|
&$startingResponse
|
|
|
) {
|
|
|
- $value = trim($h);
|
|
|
+ $value = \trim($h);
|
|
|
if ($value === '') {
|
|
|
$startingResponse = true;
|
|
|
$easy->createResponse();
|
|
|
@@ -579,7 +594,7 @@ class CurlFactory implements CurlFactoryInterface
|
|
|
} else {
|
|
|
$easy->headers[] = $value;
|
|
|
}
|
|
|
- return strlen($h);
|
|
|
+ return \strlen($h);
|
|
|
};
|
|
|
}
|
|
|
}
|