1
0
Fork 0
mirror of https://github.com/DanielnetoDotCom/YouPHPTube synced 2025-10-03 01:39:24 +02:00

Update socket also to respond http

This commit is contained in:
Daniel Neto 2024-10-07 11:43:06 -03:00
parent cc3af22682
commit 251e5740f0
1246 changed files with 62800 additions and 2951 deletions

View file

@ -193,51 +193,92 @@ class CurlClient implements ClientInterface, StreamingClientInterface
// END OF USER DEFINED TIMEOUTS
private function constructRequest($method, $absUrl, $headers, $params, $hasFile)
/**
* @param 'delete'|'get'|'post' $method
* @param string $absUrl
* @param string $params
* @param bool $hasFile
* @param 'v1'|'v2' $apiMode
*/
private function constructUrlAndBody($method, $absUrl, $params, $hasFile, $apiMode)
{
$method = \strtolower($method);
$params = Util\Util::objectsToIds($params);
if ('post' === $method) {
$absUrl = Util\Util::utf8($absUrl);
if ($hasFile) {
return [$absUrl, $params];
}
if ('v2' === $apiMode) {
if (\is_array($params) && 0 === \count($params)) {
// Send a request with empty body if we have no params set
// Setting the second parameter as null prevents the CURLOPT_POSTFIELDS
// from being set with the '[]', which is result of `json_encode([]).
return [$absUrl, null];
}
$opts = [];
return [$absUrl, \json_encode($params)];
}
return [$absUrl, Util\Util::encodeParameters($params)];
}
if ($hasFile) {
throw new Exception\UnexpectedValueException("Unexpected. {$method} methods don't support file attachments");
}
if (0 === \count($params)) {
return [Util\Util::utf8($absUrl), null];
}
$encoded = Util\Util::encodeParameters($params, $apiMode);
$absUrl = "{$absUrl}?{$encoded}";
$absUrl = Util\Util::utf8($absUrl);
return [$absUrl, null];
}
private function calculateDefaultOptions($method, $absUrl, $headers, $params, $hasFile)
{
if (\is_callable($this->defaultOptions)) { // call defaultOptions callback, set options to return value
$opts = \call_user_func_array($this->defaultOptions, \func_get_args());
if (!\is_array($opts)) {
$ret = \call_user_func_array($this->defaultOptions, [$method, $absUrl, $headers, $params, $hasFile]);
if (!\is_array($ret)) {
throw new Exception\UnexpectedValueException('Non-array value returned by defaultOptions CurlClient callback');
}
} elseif (\is_array($this->defaultOptions)) { // set default curlopts from array
$opts = $this->defaultOptions;
return $ret;
}
if (\is_array($this->defaultOptions)) { // set default curlopts from array
return $this->defaultOptions;
}
$params = Util\Util::objectsToIds($params);
return [];
}
private function constructCurlOptions($method, $absUrl, $headers, $body, $opts, $apiMode)
{
if ('get' === $method) {
if ($hasFile) {
throw new Exception\UnexpectedValueException(
'Issuing a GET request with a file parameter'
);
}
$opts[\CURLOPT_HTTPGET] = 1;
if (\count($params) > 0) {
$encoded = Util\Util::encodeParameters($params);
$absUrl = "{$absUrl}?{$encoded}";
}
} elseif ('post' === $method) {
$opts[\CURLOPT_POST] = 1;
$opts[\CURLOPT_POSTFIELDS] = $hasFile ? $params : Util\Util::encodeParameters($params);
} elseif ('delete' === $method) {
$opts[\CURLOPT_CUSTOMREQUEST] = 'DELETE';
if (\count($params) > 0) {
$encoded = Util\Util::encodeParameters($params);
$absUrl = "{$absUrl}?{$encoded}";
}
} else {
throw new Exception\UnexpectedValueException("Unrecognized method {$method}");
}
// It is only safe to retry network failures on POST requests if we
// add an Idempotency-Key header
if (('post' === $method) && (Stripe::$maxNetworkRetries > 0)) {
if (!$this->hasHeader($headers, 'Idempotency-Key')) {
$headers[] = 'Idempotency-Key: ' . $this->randomGenerator->uuid();
if ($body) {
$opts[\CURLOPT_POSTFIELDS] = $body;
}
// this is a little verbose, but makes v1 vs v2 behavior really clear
if (!$this->hasHeader($headers, 'Idempotency-Key')) {
// all v2 requests should have an IK
if ('v2' === $apiMode) {
if ('post' === $method || 'delete' === $method) {
$headers[] = 'Idempotency-Key: ' . $this->randomGenerator->uuid();
}
} else {
// v1 requests should keep old behavior for consistency
if ('post' === $method && Stripe::$maxNetworkRetries > 0) {
$headers[] = 'Idempotency-Key: ' . $this->randomGenerator->uuid();
}
}
}
@ -255,7 +296,6 @@ class CurlClient implements ClientInterface, StreamingClientInterface
// sending an empty `Expect:` header.
$headers[] = 'Expect: ';
$absUrl = Util\Util::utf8($absUrl);
$opts[\CURLOPT_URL] = $absUrl;
$opts[\CURLOPT_RETURNTRANSFER] = true;
$opts[\CURLOPT_CONNECTTIMEOUT] = $this->connectTimeout;
@ -271,22 +311,56 @@ class CurlClient implements ClientInterface, StreamingClientInterface
$opts[\CURLOPT_HTTP_VERSION] = \CURL_HTTP_VERSION_2TLS;
}
return $opts;
}
/**
* @param 'delete'|'get'|'post' $method
* @param string $absUrl
* @param array $headers
* @param array $params
* @param bool $hasFile
* @param 'v1'|'v2' $apiMode
*/
private function constructRequest($method, $absUrl, $headers, $params, $hasFile, $apiMode)
{
$method = \strtolower($method);
$opts = $this->calculateDefaultOptions($method, $absUrl, $headers, $params, $hasFile);
list($absUrl, $body) = $this->constructUrlAndBody($method, $absUrl, $params, $hasFile, $apiMode);
$opts = $this->constructCurlOptions($method, $absUrl, $headers, $body, $opts, $apiMode);
return [$opts, $absUrl];
}
public function request($method, $absUrl, $headers, $params, $hasFile)
/**
* @param 'delete'|'get'|'post' $method
* @param string $absUrl
* @param array $headers
* @param array $params
* @param bool $hasFile
* @param 'v1'|'v2' $apiMode
*/
public function request($method, $absUrl, $headers, $params, $hasFile, $apiMode = 'v1')
{
list($opts, $absUrl) = $this->constructRequest($method, $absUrl, $headers, $params, $hasFile);
list($opts, $absUrl) = $this->constructRequest($method, $absUrl, $headers, $params, $hasFile, $apiMode);
list($rbody, $rcode, $rheaders) = $this->executeRequestWithRetries($opts, $absUrl);
return [$rbody, $rcode, $rheaders];
}
public function requestStream($method, $absUrl, $headers, $params, $hasFile, $readBodyChunk)
/**
* @param 'delete'|'get'|'post' $method
* @param string $absUrl
* @param array $headers
* @param array $params
* @param bool $hasFile
* @param callable $readBodyChunk
* @param 'v1'|'v2' $apiMode
*/
public function requestStream($method, $absUrl, $headers, $params, $hasFile, $readBodyChunk, $apiMode = 'v1')
{
list($opts, $absUrl) = $this->constructRequest($method, $absUrl, $headers, $params, $hasFile);
list($opts, $absUrl) = $this->constructRequest($method, $absUrl, $headers, $params, $hasFile, $apiMode);
$opts[\CURLOPT_RETURNTRANSFER] = false;
list($rbody, $rcode, $rheaders) = $this->executeStreamingRequestWithRetries($opts, $absUrl, $readBodyChunk);
@ -378,15 +452,7 @@ class CurlClient implements ClientInterface, StreamingClientInterface
$errno = null;
$message = null;
$determineWriteCallback = function ($rheaders) use (
&$readBodyChunk,
&$shouldRetry,
&$rbody,
&$numRetries,
&$rcode,
&$lastRHeaders,
&$errno
) {
$determineWriteCallback = function ($rheaders) use (&$readBodyChunk, &$shouldRetry, &$rbody, &$numRetries, &$rcode, &$lastRHeaders, &$errno) {
$lastRHeaders = $rheaders;
$errno = \curl_errno($this->curlHandle);
@ -540,24 +606,24 @@ class CurlClient implements ClientInterface, StreamingClientInterface
case \CURLE_COULDNT_RESOLVE_HOST:
case \CURLE_OPERATION_TIMEOUTED:
$msg = "Could not connect to Stripe ({$url}). Please check your "
. 'internet connection and try again. If this problem persists, '
. "you should check Stripe's service status at "
. 'https://twitter.com/stripestatus, or';
. 'internet connection and try again. If this problem persists, '
. "you should check Stripe's service status at "
. 'https://twitter.com/stripestatus, or';
break;
case \CURLE_SSL_CACERT:
case \CURLE_SSL_PEER_CERTIFICATE:
$msg = "Could not verify Stripe's SSL certificate. Please make sure "
. 'that your network is not intercepting certificates. '
. "(Try going to {$url} in your browser.) "
. 'If this problem persists,';
. 'that your network is not intercepting certificates. '
. "(Try going to {$url} in your browser.) "
. 'If this problem persists,';
break;
default:
$msg = 'Unexpected error communicating with Stripe. '
. 'If this problem persists,';
. 'If this problem persists,';
}
$msg .= ' let us know at support@stripe.com.';