| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
namespace Symfony\Component\HttpClient; |
| 13 |
|
| 14 |
use Symfony\Component\HttpClient\Chunk\DataChunk; |
| 15 |
use Symfony\Component\HttpClient\Chunk\ServerSentEvent; |
| 16 |
use Symfony\Component\HttpClient\Exception\EventSourceException; |
| 17 |
use Symfony\Component\HttpClient\Response\AsyncContext; |
| 18 |
use Symfony\Component\HttpClient\Response\AsyncResponse; |
| 19 |
use Symfony\Contracts\HttpClient\ChunkInterface; |
| 20 |
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface; |
| 21 |
use Symfony\Contracts\HttpClient\HttpClientInterface; |
| 22 |
use Symfony\Contracts\HttpClient\ResponseInterface; |
| 23 |
use Symfony\Contracts\Service\ResetInterface; |
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
final class EventSourceHttpClient implements HttpClientInterface, ResetInterface |
| 30 |
{ |
| 31 |
use AsyncDecoratorTrait, HttpClientTrait { |
| 32 |
AsyncDecoratorTrait::withOptions insteadof HttpClientTrait; |
| 33 |
} |
| 34 |
|
| 35 |
public function __construct( |
| 36 |
?HttpClientInterface $client = null, |
| 37 |
private float $reconnectionTime = 10.0, |
| 38 |
) { |
| 39 |
$this->client = $client ?? HttpClient::create(); |
| 40 |
} |
| 41 |
|
| 42 |
public function connect(string $url, array $options = [], string $method = 'GET'): ResponseInterface |
| 43 |
{ |
| 44 |
return $this->request($method, $url, self::mergeDefaultOptions($options, [ |
| 45 |
'buffer' => false, |
| 46 |
'headers' => [ |
| 47 |
'Accept' => 'text/event-stream', |
| 48 |
'Cache-Control' => 'no-cache', |
| 49 |
], |
| 50 |
], true)); |
| 51 |
} |
| 52 |
|
| 53 |
public function request(string $method, string $url, array $options = []): ResponseInterface |
| 54 |
{ |
| 55 |
$state = new class { |
| 56 |
public ?string $buffer = null; |
| 57 |
public ?string $lastEventId = null; |
| 58 |
public float $reconnectionTime; |
| 59 |
public ?float $lastError = null; |
| 60 |
}; |
| 61 |
$state->reconnectionTime = $this->reconnectionTime; |
| 62 |
|
| 63 |
if ($accept = self::normalizeHeaders($options['headers'] ?? [])['accept'] ?? []) { |
| 64 |
$state->buffer = \in_array($accept, [['Accept: text/event-stream'], ['accept: text/event-stream']], true) ? '' : null; |
| 65 |
|
| 66 |
if (null !== $state->buffer) { |
| 67 |
$options['extra']['trace_content'] = false; |
| 68 |
} |
| 69 |
} |
| 70 |
|
| 71 |
return new AsyncResponse($this->client, $method, $url, $options, static function (ChunkInterface $chunk, AsyncContext $context) use ($state, $method, $url, $options) { |
| 72 |
if (null !== $state->buffer) { |
| 73 |
$context->setInfo('reconnection_time', $state->reconnectionTime); |
| 74 |
$isTimeout = false; |
| 75 |
} |
| 76 |
$lastError = $state->lastError; |
| 77 |
$state->lastError = null; |
| 78 |
|
| 79 |
try { |
| 80 |
$isTimeout = $chunk->isTimeout(); |
| 81 |
|
| 82 |
if (null !== $chunk->getInformationalStatus() || $context->getInfo('canceled')) { |
| 83 |
yield $chunk; |
| 84 |
|
| 85 |
return; |
| 86 |
} |
| 87 |
} catch (TransportExceptionInterface) { |
| 88 |
$state->lastError = $lastError ?? hrtime(true) / 1E9; |
| 89 |
|
| 90 |
if (null === $state->buffer || ($isTimeout && hrtime(true) / 1E9 - $state->lastError < $state->reconnectionTime)) { |
| 91 |
yield $chunk; |
| 92 |
} else { |
| 93 |
$options['headers']['Last-Event-ID'] = $state->lastEventId; |
| 94 |
$state->buffer = ''; |
| 95 |
$state->lastError = hrtime(true) / 1E9; |
| 96 |
$context->getResponse()->cancel(); |
| 97 |
$context->replaceRequest($method, $url, $options); |
| 98 |
if ($isTimeout) { |
| 99 |
yield $chunk; |
| 100 |
} else { |
| 101 |
$context->pause($state->reconnectionTime); |
| 102 |
} |
| 103 |
} |
| 104 |
|
| 105 |
return; |
| 106 |
} |
| 107 |
|
| 108 |
if ($chunk->isFirst()) { |
| 109 |
if (preg_match('/^text\/event-stream(;|$)/i', $context->getHeaders()['content-type'][0] ?? '')) { |
| 110 |
$state->buffer = ''; |
| 111 |
} elseif (null !== $lastError || (null !== $state->buffer && 200 === $context->getStatusCode())) { |
| 112 |
throw new EventSourceException(\sprintf('Response content-type is "%s" while "text/event-stream" was expected for "%s".', $context->getHeaders()['content-type'][0] ?? '', $context->getInfo('url'))); |
| 113 |
} else { |
| 114 |
$context->passthru(); |
| 115 |
} |
| 116 |
|
| 117 |
yield $chunk; |
| 118 |
|
| 119 |
return; |
| 120 |
} |
| 121 |
|
| 122 |
if ($chunk->isLast()) { |
| 123 |
if ('' !== $content = $state->buffer) { |
| 124 |
$state->buffer = ''; |
| 125 |
yield new DataChunk(-1, $content); |
| 126 |
} |
| 127 |
|
| 128 |
yield $chunk; |
| 129 |
|
| 130 |
return; |
| 131 |
} |
| 132 |
|
| 133 |
$content = $state->buffer.$chunk->getContent(); |
| 134 |
$events = preg_split('/((?:\r\n){2,}|\r{2,}|\n{2,})/', $content, -1, \PREG_SPLIT_DELIM_CAPTURE); |
| 135 |
$state->buffer = array_pop($events); |
| 136 |
|
| 137 |
for ($i = 0; isset($events[$i]); $i += 2) { |
| 138 |
$content = $events[$i].$events[1 + $i]; |
| 139 |
if (!preg_match('/(?:^|\r\n|[\r\n])[^:\r\n]/', $content)) { |
| 140 |
yield new DataChunk(-1, $content); |
| 141 |
|
| 142 |
continue; |
| 143 |
} |
| 144 |
|
| 145 |
$event = new ServerSentEvent($content); |
| 146 |
|
| 147 |
if ('' !== $event->getId()) { |
| 148 |
$context->setInfo('last_event_id', $state->lastEventId = $event->getId()); |
| 149 |
} |
| 150 |
|
| 151 |
if ($event->getRetry()) { |
| 152 |
$context->setInfo('reconnection_time', $state->reconnectionTime = $event->getRetry()); |
| 153 |
} |
| 154 |
|
| 155 |
yield $event; |
| 156 |
} |
| 157 |
}); |
| 158 |
} |
| 159 |
} |