| 1 |
<?php |
| 2 |
|
| 3 |
/* |
| 4 |
* This file is part of the Symfony package. |
| 5 |
* |
| 6 |
* (c) Fabien Potencier <[email protected]> |
| 7 |
* |
| 8 |
* For the full copyright and license information, please view the LICENSE |
| 9 |
* file that was distributed with this source code. |
| 10 |
*/ |
| 11 |
|
| 12 |
namespace Symfony\Component\HttpClient\Response; |
| 13 |
|
| 14 |
use Symfony\Component\HttpClient\Chunk\DataChunk; |
| 15 |
use Symfony\Component\HttpClient\Chunk\LastChunk; |
| 16 |
use Symfony\Component\HttpClient\Exception\TransportException; |
| 17 |
use Symfony\Contracts\HttpClient\ChunkInterface; |
| 18 |
use Symfony\Contracts\HttpClient\HttpClientInterface; |
| 19 |
use Symfony\Contracts\HttpClient\ResponseInterface; |
| 20 |
|
| 21 |
/** |
| 22 |
* A DTO to work with AsyncResponse. |
| 23 |
* |
| 24 |
* @author Nicolas Grekas <[email protected]> |
| 25 |
*/ |
| 26 |
final class AsyncContext |
| 27 |
{ |
| 28 |
private $passthru; |
| 29 |
private $client; |
| 30 |
private $response; |
| 31 |
private $info = []; |
| 32 |
private $content; |
| 33 |
private $offset; |
| 34 |
|
| 35 |
public function __construct(&$passthru, HttpClientInterface $client, ResponseInterface &$response, array &$info, $content, int $offset) |
| 36 |
{ |
| 37 |
$this->passthru = &$passthru; |
| 38 |
$this->client = $client; |
| 39 |
$this->response = &$response; |
| 40 |
$this->info = &$info; |
| 41 |
$this->content = $content; |
| 42 |
$this->offset = $offset; |
| 43 |
} |
| 44 |
|
| 45 |
/** |
| 46 |
* Returns the HTTP status without consuming the response. |
| 47 |
*/ |
| 48 |
public function getStatusCode(): int |
| 49 |
{ |
| 50 |
return $this->response->getInfo('http_code'); |
| 51 |
} |
| 52 |
|
| 53 |
/** |
| 54 |
* Returns the headers without consuming the response. |
| 55 |
*/ |
| 56 |
public function getHeaders(): array |
| 57 |
{ |
| 58 |
$headers = []; |
| 59 |
|
| 60 |
foreach ($this->response->getInfo('response_headers') as $h) { |
| 61 |
if (11 <= \strlen($h) && '/' === $h[4] && preg_match('#^HTTP/\d+(?:\.\d+)? ([123456789]\d\d)(?: |$)#', $h, $m)) { |
| 62 |
$headers = []; |
| 63 |
} elseif (2 === \count($m = explode(':', $h, 2))) { |
| 64 |
$headers[strtolower($m[0])][] = ltrim($m[1]); |
| 65 |
} |
| 66 |
} |
| 67 |
|
| 68 |
return $headers; |
| 69 |
} |
| 70 |
|
| 71 |
/** |
| 72 |
* @return resource|null The PHP stream resource where the content is buffered, if it is |
| 73 |
*/ |
| 74 |
public function getContent() |
| 75 |
{ |
| 76 |
return $this->content; |
| 77 |
} |
| 78 |
|
| 79 |
/** |
| 80 |
* Creates a new chunk of content. |
| 81 |
*/ |
| 82 |
public function createChunk(string $data): ChunkInterface |
| 83 |
{ |
| 84 |
return new DataChunk($this->offset, $data); |
| 85 |
} |
| 86 |
|
| 87 |
/** |
| 88 |
* Pauses the request for the given number of seconds. |
| 89 |
*/ |
| 90 |
public function pause(float $duration): void |
| 91 |
{ |
| 92 |
if (\is_callable($pause = $this->response->getInfo('pause_handler'))) { |
| 93 |
$pause($duration); |
| 94 |
} elseif (0 < $duration) { |
| 95 |
usleep(1E6 * $duration); |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
/** |
| 100 |
* Cancels the request and returns the last chunk to yield. |
| 101 |
*/ |
| 102 |
public function cancel(): ChunkInterface |
| 103 |
{ |
| 104 |
$this->info['canceled'] = true; |
| 105 |
$this->info['error'] = 'Response has been canceled.'; |
| 106 |
$this->response->cancel(); |
| 107 |
|
| 108 |
return new LastChunk(); |
| 109 |
} |
| 110 |
|
| 111 |
/** |
| 112 |
* Returns the current info of the response. |
| 113 |
*/ |
| 114 |
public function getInfo(string $type = null) |
| 115 |
{ |
| 116 |
if (null !== $type) { |
| 117 |
return $this->info[$type] ?? $this->response->getInfo($type); |
| 118 |
} |
| 119 |
|
| 120 |
return $this->info + $this->response->getInfo(); |
| 121 |
} |
| 122 |
|
| 123 |
/** |
| 124 |
* Attaches an info to the response. |
| 125 |
* |
| 126 |
* @return $this |
| 127 |
*/ |
| 128 |
public function setInfo(string $type, $value): self |
| 129 |
{ |
| 130 |
if ('canceled' === $type && $value !== $this->info['canceled']) { |
| 131 |
throw new \LogicException('You cannot set the "canceled" info directly.'); |
| 132 |
} |
| 133 |
|
| 134 |
if (null === $value) { |
| 135 |
unset($this->info[$type]); |
| 136 |
} else { |
| 137 |
$this->info[$type] = $value; |
| 138 |
} |
| 139 |
|
| 140 |
return $this; |
| 141 |
} |
| 142 |
|
| 143 |
/** |
| 144 |
* Returns the currently processed response. |
| 145 |
*/ |
| 146 |
public function getResponse(): ResponseInterface |
| 147 |
{ |
| 148 |
return $this->response; |
| 149 |
} |
| 150 |
|
| 151 |
/** |
| 152 |
* Replaces the currently processed response by doing a new request. |
| 153 |
*/ |
| 154 |
public function replaceRequest(string $method, string $url, array $options = []): ResponseInterface |
| 155 |
{ |
| 156 |
$this->info['previous_info'][] = $info = $this->response->getInfo(); |
| 157 |
if (null !== $onProgress = $options['on_progress'] ?? null) { |
| 158 |
$thisInfo = &$this->info; |
| 159 |
$options['on_progress'] = static function (int $dlNow, int $dlSize, array $info) use (&$thisInfo, $onProgress) { |
| 160 |
$onProgress($dlNow, $dlSize, $thisInfo + $info); |
| 161 |
}; |
| 162 |
} |
| 163 |
if (0 < ($info['max_duration'] ?? 0) && 0 < ($info['total_time'] ?? 0)) { |
| 164 |
if (0 >= $options['max_duration'] = $info['max_duration'] - $info['total_time']) { |
| 165 |
throw new TransportException(sprintf('Max duration was reached for "%s".', $info['url'])); |
| 166 |
} |
| 167 |
} |
| 168 |
|
| 169 |
return $this->response = $this->client->request($method, $url, ['buffer' => false] + $options); |
| 170 |
} |
| 171 |
|
| 172 |
/** |
| 173 |
* Replaces the currently processed response by another one. |
| 174 |
*/ |
| 175 |
public function replaceResponse(ResponseInterface $response): ResponseInterface |
| 176 |
{ |
| 177 |
$this->info['previous_info'][] = $this->response->getInfo(); |
| 178 |
|
| 179 |
return $this->response = $response; |
| 180 |
} |
| 181 |
|
| 182 |
/** |
| 183 |
* Replaces or removes the chunk filter iterator. |
| 184 |
* |
| 185 |
* @param ?callable(ChunkInterface, self): ?\Iterator $passthru |
| 186 |
*/ |
| 187 |
public function passthru(callable $passthru = null): void |
| 188 |
{ |
| 189 |
$this->passthru = $passthru ?? static function ($chunk, $context) { |
| 190 |
$context->passthru = null; |
| 191 |
|
| 192 |
yield $chunk; |
| 193 |
}; |
| 194 |
} |
| 195 |
} |
| 196 |
|