Initial Commit

This commit is contained in:
David Stone
2024-11-30 18:24:12 -07:00
commit e8f7955c1c
5432 changed files with 1397750 additions and 0 deletions

View File

@ -0,0 +1,13 @@
<?php
namespace WP_Ultimo\Dependencies\Amp\Http\Client\Internal;
/** @internal */
trait ForbidCloning
{
protected final function __clone()
{
// clone is automatically denied to all external calls
// final protected instead of private to also force denial for all children
}
}

View File

@ -0,0 +1,12 @@
<?php
namespace WP_Ultimo\Dependencies\Amp\Http\Client\Internal;
/** @internal */
trait ForbidSerialization
{
public final function __sleep()
{
throw new \Error(__CLASS__ . ' does not support serialization');
}
}

View File

@ -0,0 +1,19 @@
<?php
namespace WP_Ultimo\Dependencies\Amp\Http\Client\Internal;
/** @internal */
final class HarAttributes
{
use ForbidCloning;
use ForbidSerialization;
public const STARTED_DATE_TIME = 'amp.http.client.har.startedDateTime';
public const SERVER_IP_ADDRESS = 'amp.http.client.har.serverIPAddress';
public const TIME_START = 'amp.http.client.har.timings.start';
public const TIME_SSL = 'amp.http.client.har.timings.ssl';
public const TIME_CONNECT = 'amp.http.client.har.timings.connect';
public const TIME_SEND = 'amp.http.client.har.timings.send';
public const TIME_WAIT = 'amp.http.client.har.timings.wait';
public const TIME_RECEIVE = 'amp.http.client.har.timings.receive';
public const TIME_COMPLETE = 'amp.http.client.har.timings.complete';
}

View File

@ -0,0 +1,40 @@
<?php
namespace WP_Ultimo\Dependencies\Amp\Http\Client\Internal;
use WP_Ultimo\Dependencies\Amp\ByteStream\InputStream;
use WP_Ultimo\Dependencies\Amp\CancellationTokenSource;
use WP_Ultimo\Dependencies\Amp\Promise;
/** @internal */
final class ResponseBodyStream implements InputStream
{
use ForbidSerialization;
use ForbidCloning;
/** @var InputStream */
private $body;
/** @var CancellationTokenSource */
private $bodyCancellation;
/** @var bool */
private $successfulEnd = \false;
public function __construct(InputStream $body, CancellationTokenSource $bodyCancellation)
{
$this->body = $body;
$this->bodyCancellation = $bodyCancellation;
}
public function read() : Promise
{
$promise = $this->body->read();
$promise->onResolve(function ($error, $value) {
if ($value === null && $error === null) {
$this->successfulEnd = \true;
}
});
return $promise;
}
public function __destruct()
{
if (!$this->successfulEnd) {
$this->bodyCancellation->cancel();
}
}
}

View File

@ -0,0 +1,46 @@
<?php
namespace WP_Ultimo\Dependencies\Amp\Http\Client\Internal;
use WP_Ultimo\Dependencies\Amp\ByteStream\InputStream;
use WP_Ultimo\Dependencies\Amp\Failure;
use WP_Ultimo\Dependencies\Amp\Http\Client\ParseException;
use WP_Ultimo\Dependencies\Amp\Http\Status;
use WP_Ultimo\Dependencies\Amp\Promise;
/** @internal */
final class SizeLimitingInputStream implements InputStream
{
use ForbidSerialization;
use ForbidCloning;
/** @var InputStream|null */
private $source;
/** @var int */
private $bytesRead = 0;
/** @var int */
private $sizeLimit;
/** @var \Throwable|null */
private $exception;
public function __construct(InputStream $source, int $sizeLimit)
{
$this->source = $source;
$this->sizeLimit = $sizeLimit;
}
public function read() : Promise
{
if ($this->exception) {
return new Failure($this->exception);
}
\assert($this->source !== null);
$promise = $this->source->read();
$promise->onResolve(function ($error, $value) {
if ($value !== null) {
$this->bytesRead += \strlen($value);
if ($this->bytesRead > $this->sizeLimit) {
$this->exception = new ParseException("Configured body size exceeded: {$this->bytesRead} bytes received, while the configured limit is {$this->sizeLimit} bytes", Status::PAYLOAD_TOO_LARGE);
$this->source = null;
}
}
});
return $promise;
}
}

View File

@ -0,0 +1,26 @@
<?php
namespace WP_Ultimo\Dependencies\Amp\Http\Client\Internal;
use WP_Ultimo\Dependencies\Amp\Http\Client\InvalidRequestException;
use WP_Ultimo\Dependencies\Amp\Http\Client\Request;
/**
* @param Request $request
*
* @return string
* @throws InvalidRequestException
*
* @internal
*/
function normalizeRequestPathWithQuery(Request $request) : string
{
$path = $request->getUri()->getPath();
$query = $request->getUri()->getQuery();
if ($path === '') {
return '/' . ($query !== '' ? '?' . $query : '');
}
if ($path[0] !== '/') {
throw new InvalidRequestException($request, 'Relative path (' . $path . ') is not allowed in the request URI');
}
return $path . ($query !== '' ? '?' . $query : '');
}