Skip to content

Commit 25387a5

Browse files
committed
Fix content type charset check
1 parent 108627a commit 25387a5

File tree

2 files changed

+60
-2
lines changed

2 files changed

+60
-2
lines changed

src/Symfony/Component/HttpClient/EventSourceHttpClient.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ final class EventSourceHttpClient implements HttpClientInterface
3333

3434
public function __construct(HttpClientInterface $client = null, float $reconnectionTime = 10.0)
3535
{
36-
$this->client = $client ?: HttpClient::create();
36+
$this->client = $client ?? HttpClient::create();
3737
$this->reconnectionTime = $reconnectionTime;
3838
}
3939

@@ -100,7 +100,7 @@ public function request(string $method, string $url, array $options = []): Respo
100100
}
101101

102102
if ($chunk->isFirst()) {
103-
if (0 === strpos($context->getHeaders()['content-type'][0] ?? '', 'text/event-stream')) {
103+
if (preg_match('/^text\/event-stream(;|$)/i', $context->getHeaders()['content-type'][0] ?? '')) {
104104
$state->buffer = '';
105105
} elseif (null !== $lastError || (null !== $state->buffer && 200 === $context->getStatusCode())) {
106106
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')));

src/Symfony/Component/HttpClient/Tests/EventSourceHttpClientTest.php

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use Symfony\Component\HttpClient\Chunk\FirstChunk;
1818
use Symfony\Component\HttpClient\Chunk\ServerSentEvent;
1919
use Symfony\Component\HttpClient\EventSourceHttpClient;
20+
use Symfony\Component\HttpClient\Exception\EventSourceException;
2021
use Symfony\Component\HttpClient\Response\MockResponse;
2122
use Symfony\Component\HttpClient\Response\ResponseStream;
2223
use Symfony\Contracts\HttpClient\HttpClientInterface;
@@ -108,4 +109,61 @@ public function testGetServerSentEvents()
108109
}
109110
}
110111
}
112+
113+
/**
114+
* @dataProvider contentTypeProvider
115+
*/
116+
public function testContentType($contentType, $expected)
117+
{
118+
$chunk = new DataChunk(0, '');
119+
$response = new MockResponse('', ['canceled' => false, 'http_method' => 'GET', 'url' => 'http://localhost:8080/events', 'response_headers' => ['content-type: '.$contentType]]);
120+
$responseStream = new ResponseStream((function () use ($response, $chunk) {
121+
yield $response => new FirstChunk();
122+
yield $response => $chunk;
123+
yield $response => new ErrorChunk(0, 'timeout');
124+
})());
125+
126+
$hasCorrectHeaders = function ($options) {
127+
$this->assertSame(['Accept: text/event-stream', 'Cache-Control: no-cache'], $options['headers']);
128+
129+
return true;
130+
};
131+
132+
$httpClient = $this->createMock(HttpClientInterface::class);
133+
$httpClient->method('request')->with('GET', 'http://localhost:8080/events', $this->callback($hasCorrectHeaders))->willReturn($response);
134+
135+
$httpClient->method('stream')->willReturn($responseStream);
136+
137+
$es = new EventSourceHttpClient($httpClient);
138+
$res = $es->connect('http://localhost:8080/events');
139+
140+
if ($expected instanceof EventSourceException) {
141+
$this->expectExceptionMessage($expected->getMessage());
142+
}
143+
144+
foreach ($es->stream($res) as $chunk) {
145+
if ($chunk->isTimeout()) {
146+
continue;
147+
}
148+
149+
if ($chunk->isLast()) {
150+
return;
151+
}
152+
}
153+
}
154+
155+
public function contentTypeProvider()
156+
{
157+
return [
158+
['text/event-stream', true],
159+
['text/event-stream;charset=utf-8', true],
160+
['text/event-stream;charset=UTF-8', true],
161+
['Text/EVENT-STREAM;Charset="utf-8"', true],
162+
['text/event-stream; charset="utf-8"', true],
163+
['text/event-stream; charset=iso-8859-15', true],
164+
['text/html', new EventSourceException('Response content-type is "text/html" while "text/event-stream" was expected for "http://localhost:8080/events".')],
165+
['text/html; charset="utf-8"', new EventSourceException('Response content-type is "text/html; charset="utf-8"" while "text/event-stream" was expected for "http://localhost:8080/events".')],
166+
['text/event-streambla', new EventSourceException('Response content-type is "text/event-streambla" while "text/event-stream" was expected for "http://localhost:8080/events".')],
167+
];
168+
}
111169
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy