| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
namespace Symfony\Component\HttpClient\Internal; |
| 13 |
|
| 14 |
use Amp\ByteStream\InputStream; |
| 15 |
use Amp\ByteStream\ResourceInputStream; |
| 16 |
use Amp\Http\Client\RequestBody; |
| 17 |
use Amp\Promise; |
| 18 |
use Amp\Success; |
| 19 |
use Symfony\Component\HttpClient\Exception\TransportException; |
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
class AmpBodyV4 implements RequestBody, InputStream |
| 27 |
{ |
| 28 |
private ResourceInputStream|\Closure|string $body; |
| 29 |
private array $info; |
| 30 |
private ?int $offset = 0; |
| 31 |
private int $length = -1; |
| 32 |
private ?int $uploaded = null; |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
public function __construct( |
| 38 |
$body, |
| 39 |
&$info, |
| 40 |
private \Closure $onProgress, |
| 41 |
) { |
| 42 |
$this->info = &$info; |
| 43 |
|
| 44 |
if (\is_resource($body)) { |
| 45 |
$this->offset = ftell($body); |
| 46 |
$this->length = fstat($body)['size']; |
| 47 |
$this->body = new ResourceInputStream($body); |
| 48 |
} elseif (\is_string($body)) { |
| 49 |
$this->length = \strlen($body); |
| 50 |
$this->body = $body; |
| 51 |
} else { |
| 52 |
$this->body = $body; |
| 53 |
} |
| 54 |
} |
| 55 |
|
| 56 |
public function createBodyStream(): InputStream |
| 57 |
{ |
| 58 |
if (null !== $this->uploaded) { |
| 59 |
$this->uploaded = null; |
| 60 |
|
| 61 |
if (\is_string($this->body)) { |
| 62 |
$this->offset = 0; |
| 63 |
} elseif ($this->body instanceof ResourceInputStream) { |
| 64 |
fseek($this->body->getResource(), $this->offset); |
| 65 |
} |
| 66 |
} |
| 67 |
|
| 68 |
return $this; |
| 69 |
} |
| 70 |
|
| 71 |
public function getHeaders(): Promise |
| 72 |
{ |
| 73 |
return new Success([]); |
| 74 |
} |
| 75 |
|
| 76 |
public function getBodyLength(): Promise |
| 77 |
{ |
| 78 |
return new Success($this->length - $this->offset); |
| 79 |
} |
| 80 |
|
| 81 |
public function read(): Promise |
| 82 |
{ |
| 83 |
$this->info['size_upload'] += $this->uploaded; |
| 84 |
$this->uploaded = 0; |
| 85 |
($this->onProgress)(); |
| 86 |
|
| 87 |
$chunk = $this->doRead(); |
| 88 |
$chunk->onResolve(function ($e, $data) { |
| 89 |
if (null !== $data) { |
| 90 |
$this->uploaded = \strlen($data); |
| 91 |
} else { |
| 92 |
$this->info['upload_content_length'] = $this->info['size_upload']; |
| 93 |
} |
| 94 |
}); |
| 95 |
|
| 96 |
return $chunk; |
| 97 |
} |
| 98 |
|
| 99 |
public static function rewind(RequestBody $body): RequestBody |
| 100 |
{ |
| 101 |
if (!$body instanceof self) { |
| 102 |
return $body; |
| 103 |
} |
| 104 |
|
| 105 |
$body->uploaded = null; |
| 106 |
|
| 107 |
if ($body->body instanceof ResourceInputStream) { |
| 108 |
fseek($body->body->getResource(), $body->offset); |
| 109 |
|
| 110 |
return new $body($body->body, $body->info, $body->onProgress); |
| 111 |
} |
| 112 |
|
| 113 |
if (\is_string($body->body)) { |
| 114 |
$body->offset = 0; |
| 115 |
} |
| 116 |
|
| 117 |
return $body; |
| 118 |
} |
| 119 |
|
| 120 |
private function doRead(): Promise |
| 121 |
{ |
| 122 |
if ($this->body instanceof ResourceInputStream) { |
| 123 |
return $this->body->read(); |
| 124 |
} |
| 125 |
|
| 126 |
if (null === $this->offset || !$this->length) { |
| 127 |
return new Success(); |
| 128 |
} |
| 129 |
|
| 130 |
if (\is_string($this->body)) { |
| 131 |
$this->offset = null; |
| 132 |
|
| 133 |
return new Success($this->body); |
| 134 |
} |
| 135 |
|
| 136 |
if ('' === $data = ($this->body)(16372)) { |
| 137 |
$this->offset = null; |
| 138 |
|
| 139 |
return new Success(); |
| 140 |
} |
| 141 |
|
| 142 |
if (!\is_string($data)) { |
| 143 |
throw new TransportException(\sprintf('Return value of the "body" option callback must be string, "%s" returned.', get_debug_type($data))); |
| 144 |
} |
| 145 |
|
| 146 |
return new Success($data); |
| 147 |
} |
| 148 |
} |