Discard event data not followed by an empty line before eof when parsing an event-stream

https://bugs.webkit.org/show_bug.cgi?id=68833

Patch by Per-Erik Brodin <per-erik.brodin@ericsson.com> on 2011-12-21
Reviewed by Alexey Proskuryakov.

Source/WebCore:

Test: http/tests/eventsource/eventsource-eof.html

* page/EventSource.cpp:
(WebCore::EventSource::didFinishLoading):
(WebCore::EventSource::parseEventStreamLine):
* page/EventSource.h:

LayoutTests:

* http/tests/eventsource/eventsource-eof-expected.txt: Added.
* http/tests/eventsource/eventsource-eof.html: Added.
* http/tests/eventsource/eventsource-reconnect-expected.txt: Updated.
* http/tests/eventsource/eventsource-reconnect.html: Added lastEventId check.
* http/tests/eventsource/resources/es-eof.php: Added.
* http/tests/eventsource/resources/reconnect.php: Added data to be discarded.
* http/tests/eventsource/resources/response-content-type-charset.php: Added empty line before eof.
* http/tests/eventsource/resources/simple-event-stream.asis: Added empty line before eof.

git-svn-id: http://svn.webkit.org/repository/webkit/trunk@103474 268f45cc-cd09-0410-ab3c-d52691b4dbfc
parent eea810d7
2011-12-21 Per-Erik Brodin <per-erik.brodin@ericsson.com>
Discard event data not followed by an empty line before eof when parsing an event-stream
https://bugs.webkit.org/show_bug.cgi?id=68833
Reviewed by Alexey Proskuryakov.
* http/tests/eventsource/eventsource-eof-expected.txt: Added.
* http/tests/eventsource/eventsource-eof.html: Added.
* http/tests/eventsource/eventsource-reconnect-expected.txt: Updated.
* http/tests/eventsource/eventsource-reconnect.html: Added lastEventId check.
* http/tests/eventsource/resources/es-eof.php: Added.
* http/tests/eventsource/resources/reconnect.php: Added data to be discarded.
* http/tests/eventsource/resources/response-content-type-charset.php: Added empty line before eof.
* http/tests/eventsource/resources/simple-event-stream.asis: Added empty line before eof.
2011-12-21 Scott Franklin <scottfr@google.com>
Reviewed by Eric Carlson.
Test that EventSource discards event data if there is no newline before eof. Should print a series of PASS messages followed by DONE.
PASS: got event with expected data, lastEventId, and type
PASS: got event with expected data, lastEventId, and type
PASS: got event with expected data, lastEventId, and type
DONE
<html>
<body>
<p>Test that EventSource discards event data if there is no newline before eof. Should print a series of PASS messages followed by DONE.</p>
<div id="result"></div>
<script>
function log(msg) {
document.getElementById("result").innerHTML += msg + "<br>";
}
if (window.layoutTestController) {
layoutTestController.dumpAsText();
layoutTestController.waitUntilDone();
}
var count = 1;
var es = new EventSource("resources/es-eof.php");
es.onerror = function () {
if (count++ == 3) {
es.close();
if (window.layoutTestController)
layoutTestController.notifyDone();
}
};
es.onmessage = function (evt) {
if (evt.data == ("DATA" + count)) {
if (evt.lastEventId == count) {
if (evt.type == "message")
log("PASS: got event with expected data, lastEventId, and type");
else
log("FAIL: got expected data and lastEventId but type is wrong");
}
else if (evt.type == "message")
log("FAIL: got expected data but lastEventId is wrong");
else
log("FAIL: got expected data but lastEventId and type are wrong");
}
else if (count == 3 && evt.data == "DATA" && evt.lastEventId == "3.1" && evt.type == "msg")
log("DONE");
else
log("FAIL: got unexpected message event");
};
es.addEventListener("msg", es.onmessage);
</script>
</body>
</html>
......@@ -5,7 +5,7 @@ PASS: state is OPEN
PASS: got lastEventId "77"
PASS: state is CONNECTING
PASS: state is OPEN
PASS: Last-Event-ID header was correct
PASS: Last-Event-ID header and the lastEventId property were correct
PASS: state is CLOSED
DONE
......@@ -41,10 +41,12 @@ es.onmessage = function (evt) {
else
log("FAIL: unexpected lastEventId \"" + evt.lastEventId + "\"");
} else {
if (evt.data == "77")
log("PASS: Last-Event-ID header was correct");
else
if (evt.data != "77")
log("FAIL: Last-Event-ID header was incorrect/missing");
else if (evt.lastEventId != "77")
log("FAIL: the lastEventId property was incorrect");
else
log("PASS: Last-Event-ID header and the lastEventId property were correct");
}
};
......
<?php
header("Content-Type: text/event-stream");
$id = floatval($_SERVER['HTTP_LAST_EVENT_ID']) + 1;
echo "retry: 0\n";
echo "id: " . $id . "\n";
echo "data: DATA" . intval($id) . "\n\n";
echo "id: " . ($id + 0.1) . "\n";
echo "event: msg\n";
echo "data: DATA" . str_repeat("\n", $id - 1);
?>
......@@ -8,5 +8,6 @@ else {
echo "id: 77\n";
echo "retry: 300\n";
echo "data: hello\n\n";
echo "data: discarded";
}
?>
......@@ -11,3 +11,4 @@ header("Content-Type: $contentType");
id: 77
retry: 300
data: hello
2011-12-21 Per-Erik Brodin <per-erik.brodin@ericsson.com>
Discard event data not followed by an empty line before eof when parsing an event-stream
https://bugs.webkit.org/show_bug.cgi?id=68833
Reviewed by Alexey Proskuryakov.
Test: http/tests/eventsource/eventsource-eof.html
* page/EventSource.cpp:
(WebCore::EventSource::didFinishLoading):
(WebCore::EventSource::parseEventStreamLine):
* page/EventSource.h:
2011-12-21 Andreas Kling <kling@webkit.org>
Automate elements' registration as document namedItem/extraNamedItem.
......@@ -250,8 +250,13 @@ void EventSource::didFinishLoading(unsigned long, double)
ASSERT(m_requestInFlight);
if (m_receiveBuf.size() > 0 || m_data.size() > 0) {
append(m_receiveBuf, "\n\n");
parseEventStream();
// Discard everything that has not been dispatched by now.
m_receiveBuf.clear();
m_data.clear();
m_eventName = "";
m_currentlyParsedEventId = String();
}
networkRequestEnded();
}
......@@ -322,6 +327,10 @@ void EventSource::parseEventStreamLine(unsigned int bufPos, int fieldLength, int
if (!lineLength) {
if (!m_data.isEmpty()) {
m_data.removeLast();
if (!m_currentlyParsedEventId.isNull()) {
m_lastEventId.swap(m_currentlyParsedEventId);
m_currentlyParsedEventId = String();
}
dispatchEvent(createMessageEvent());
}
if (!m_eventName.isEmpty())
......@@ -347,7 +356,7 @@ void EventSource::parseEventStreamLine(unsigned int bufPos, int fieldLength, int
} else if (field == "event")
m_eventName = valueLength ? String(&m_receiveBuf[bufPos], valueLength) : "";
else if (field == "id")
m_lastEventId = valueLength ? String(&m_receiveBuf[bufPos], valueLength) : "";
m_currentlyParsedEventId = valueLength ? String(&m_receiveBuf[bufPos], valueLength) : "";
else if (field == "retry") {
if (!valueLength)
m_reconnectDelay = defaultReconnectDelay;
......
......@@ -114,6 +114,7 @@ namespace WebCore {
String m_eventName;
Vector<UChar> m_data;
String m_currentlyParsedEventId;
String m_lastEventId;
unsigned long long m_reconnectDelay;
String m_origin;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment