Skip to content

Commit cfb999e

Browse files
committed
bug #54242 [HttpClient] [EventSourceHttpClient] Fix consuming SSEs with \r\n separator (fancyweb)
This PR was merged into the 5.4 branch. Discussion ---------- [HttpClient] [EventSourceHttpClient] Fix consuming SSEs with \r\n separator | Q | A | ------------- | --- | Branch? | 5.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | Issues | - | License | MIT `\r\n` separator never worked because it splits on every line because of `[\r\n]`. Commits ------- 2c095d8 [HttpClient][EventSourceHttpClient] Fix consuming SSEs with \r\n separator
2 parents edb69e6 + 2c095d8 commit cfb999e

File tree

2 files changed

+14
-9
lines changed

2 files changed

+14
-9
lines changed

src/Symfony/Component/HttpClient/EventSourceHttpClient.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ public function request(string $method, string $url, array $options = []): Respo
121121
return;
122122
}
123123

124-
$rx = '/((?:\r\n|[\r\n]){2,})/';
124+
$rx = '/((?:\r\n){2,}|\r{2,}|\n{2,})/';
125125
$content = $state->buffer.$chunk->getContent();
126126

127127
if ($chunk->isLast()) {

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

+13-8
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,14 @@
2727
*/
2828
class EventSourceHttpClientTest extends TestCase
2929
{
30-
public function testGetServerSentEvents()
30+
/**
31+
* @testWith ["\n"]
32+
* ["\r"]
33+
* ["\r\n"]
34+
*/
35+
public function testGetServerSentEvents(string $sep)
3136
{
32-
$data = <<<TXT
37+
$data = str_replace("\n", $sep, <<<TXT
3338
event: builderror
3439
id: 46
3540
data: {"foo": "bar"}
@@ -57,7 +62,7 @@ public function testGetServerSentEvents()
5762
5863
id: 60
5964
data
60-
TXT;
65+
TXT);
6166

6267
$chunk = new DataChunk(0, $data);
6368
$response = new MockResponse('', ['canceled' => false, 'http_method' => 'GET', 'url' => 'http://localhost:8080/events', 'response_headers' => ['content-type: text/event-stream']]);
@@ -83,11 +88,11 @@ public function testGetServerSentEvents()
8388

8489
$expected = [
8590
new FirstChunk(),
86-
new ServerSentEvent("event: builderror\nid: 46\ndata: {\"foo\": \"bar\"}\n\n"),
87-
new ServerSentEvent("event: reload\nid: 47\ndata: {}\n\n"),
88-
new ServerSentEvent("event: reload\nid: 48\ndata: {}\n\n"),
89-
new ServerSentEvent("data: test\ndata:test\nid: 49\nevent: testEvent\n\n\n"),
90-
new ServerSentEvent("id: 50\ndata: <tag>\ndata\ndata: <foo />\ndata\ndata: </tag>\n\n"),
91+
new ServerSentEvent(str_replace("\n", $sep, "event: builderror\nid: 46\ndata: {\"foo\": \"bar\"}\n\n")),
92+
new ServerSentEvent(str_replace("\n", $sep, "event: reload\nid: 47\ndata: {}\n\n")),
93+
new ServerSentEvent(str_replace("\n", $sep, "event: reload\nid: 48\ndata: {}\n\n")),
94+
new ServerSentEvent(str_replace("\n", $sep, "data: test\ndata:test\nid: 49\nevent: testEvent\n\n\n")),
95+
new ServerSentEvent(str_replace("\n", $sep, "id: 50\ndata: <tag>\ndata\ndata: <foo />\ndata\ndata: </tag>\n\n")),
9196
];
9297
$i = 0;
9398

0 commit comments

Comments
 (0)