Skip to content

Truly independent web browser

LibWeb: Read the EventSource `retry` field as milliseconds

Tim Ledbetter216f0b47ec9e
diff --git a/Libraries/LibWeb/HTML/EventSource.cpp b/Libraries/LibWeb/HTML/EventSource.cpp
index 1ba9f18d5a2..4464ba02c85 100644
--- a/Libraries/LibWeb/HTML/EventSource.cpp
+++ b/Libraries/LibWeb/HTML/EventSource.cpp
@@ -445,8 +445,10 @@ void EventSource::process_field(Utf16View field, Utf16View value)
else if (field == u"retry"sv) {
// If the field value consists of only ASCII digits, then interpret the field value as an integer in base ten,
// and set the event stream's reconnection time to that integer. Otherwise, ignore the field.
- if (auto retry = value.to_number<i64>(); retry.has_value())
- m_reconnection_time = AK::Duration::from_seconds(*retry);
+ if (all_of(value, is_ascii_digit)) {
+ if (auto retry = value.to_number<i64>(TrimWhitespace::No); retry.has_value())
+ m_reconnection_time = AK::Duration::from_milliseconds(*retry);
+ }
}
// -> Otherwise
else {
diff --git a/Tests/LibWeb/TestConfig.ini b/Tests/LibWeb/TestConfig.ini
index 5bdb2c4a24b..71215206475 100644
--- a/Tests/LibWeb/TestConfig.ini
+++ b/Tests/LibWeb/TestConfig.ini
@@ -272,6 +272,8 @@ Text/input/wpt-import/html/semantics/scripting-1/the-script-element/css-module/i
Text/input/wpt-import/html/semantics/scripting-1/the-script-element/css-module/css-module-worker-test.html
Text/input/wpt-import/eventsource/format-bom-2.any.html
Text/input/wpt-import/eventsource/format-bom.any.html
+Text/input/wpt-import/eventsource/format-field-id-null.window.html
+Text/input/wpt-import/eventsource/request-credentials.window.html
Text/input/wpt-import/html/semantics/scripting-1/the-script-element/json-module/module.html
Text/input/wpt-import/html/semantics/scripting-1/the-script-element/css-module/charset-bom.html
Text/input/wpt-import/html/semantics/scripting-1/the-script-element/json-module/charset-bom.any.html
diff --git a/Tests/LibWeb/Text/expected/HTML/EventSource-retry-field-only-ascii-digits.txt b/Tests/LibWeb/Text/expected/HTML/EventSource-retry-field-only-ascii-digits.txt
new file mode 100644
index 00000000000..fdeeaec9cec
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/HTML/EventSource-retry-field-only-ascii-digits.txt
@@ -0,0 +1 @@
+reconnected after at least 500 ms: true
diff --git a/Tests/LibWeb/Text/expected/wpt-import/eventsource/format-field-id-null.window.txt b/Tests/LibWeb/Text/expected/wpt-import/eventsource/format-field-id-null.window.txt
new file mode 100644
index 00000000000..ae4a9b6f731
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/wpt-import/eventsource/format-field-id-null.window.txt
@@ -0,0 +1,10 @@
+Harness status: OK
+
+Found 5 tests
+
+5 Pass
+Pass EventSource: id field set to %00%00
+Pass EventSource: id field set to x%00
+Pass EventSource: id field set to %00x
+Pass EventSource: id field set to x%00x
+Pass EventSource: id field set to %20%00
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/expected/wpt-import/eventsource/request-credentials.window.txt b/Tests/LibWeb/Text/expected/wpt-import/eventsource/request-credentials.window.txt
new file mode 100644
index 00000000000..d971822578a
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/wpt-import/eventsource/request-credentials.window.txt
@@ -0,0 +1,8 @@
+Harness status: OK
+
+Found 3 tests
+
+3 Pass
+Pass EventSource: credentials: credentials enabled
+Pass EventSource: credentials: credentials disabled
+Pass EventSource: credentials: credentials default
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/input/HTML/EventSource-retry-field-only-ascii-digits.html b/Tests/LibWeb/Text/input/HTML/EventSource-retry-field-only-ascii-digits.html
new file mode 100644
index 00000000000..c958b7f4c57
--- /dev/null
+++ b/Tests/LibWeb/Text/input/HTML/EventSource-retry-field-only-ascii-digits.html
@@ -0,0 +1,24 @@
+<!DOCTYPE html>
+<script src="../include.js"></script>
+<script>
+ promiseTest(async () => {
+ const server = httpTestServer();
+ const url = await server.createEcho("GET", "/event-source-retry-field-only-ascii-digits", {
+ status: 200,
+ headers: { "Content-Type": "text/event-stream", "Access-Control-Allow-Origin": "*" },
+ body: "retry: 500\nretry: -1\ndata: one\n\n",
+ });
+
+ const source = new EventSource(url);
+ const openTimes = [];
+ await new Promise(resolve => {
+ source.onopen = () => {
+ openTimes.push(performance.now());
+ if (openTimes.length === 2)
+ resolve();
+ };
+ });
+ source.close();
+ println(`reconnected after at least 500 ms: ${openTimes[1] - openTimes[0] >= 500}`);
+ });
+</script>
diff --git a/Tests/LibWeb/Text/input/wpt-import/eventsource/format-field-id-null.window.html b/Tests/LibWeb/Text/input/wpt-import/eventsource/format-field-id-null.window.html
new file mode 100644
index 00000000000..e54aa991fae
--- /dev/null
+++ b/Tests/LibWeb/Text/input/wpt-import/eventsource/format-field-id-null.window.html
@@ -0,0 +1,8 @@
+<!doctype html>
+<meta charset=utf-8>
+
+<script src="../resources/testharness.js"></script>
+<script src="../resources/testharnessreport.js"></script>
+
+<div id=log></div>
+<script src="../eventsource/format-field-id-null.window.js"></script>
diff --git a/Tests/LibWeb/Text/input/wpt-import/eventsource/format-field-id-null.window.js b/Tests/LibWeb/Text/input/wpt-import/eventsource/format-field-id-null.window.js
new file mode 100644
index 00000000000..6d564dde0f2
--- /dev/null
+++ b/Tests/LibWeb/Text/input/wpt-import/eventsource/format-field-id-null.window.js
@@ -0,0 +1,25 @@
+[
+ "\u0000\u0000",
+ "x\u0000",
+ "\u0000x",
+ "x\u0000x",
+ " \u0000"
+].forEach(idValue => {
+ const encodedIdValue = encodeURIComponent(idValue);
+ async_test(t => {
+ const source = new EventSource("resources/last-event-id.py?idvalue=" + encodedIdValue);
+ t.add_cleanup(() => source.close());
+ let seenhello = false;
+ source.onmessage = t.step_func(e => {
+ if (e.data == "hello" && !seenhello) {
+ seenhello = true;
+ assert_equals(e.lastEventId, "");
+ } else if(seenhello) {
+ assert_equals(e.data, "hello");
+ assert_equals(e.lastEventId, "");
+ t.done();
+ } else
+ assert_unreached();
+ });
+ }, "EventSource: id field set to " + encodedIdValue);
+});
diff --git a/Tests/LibWeb/Text/input/wpt-import/eventsource/request-credentials.window.html b/Tests/LibWeb/Text/input/wpt-import/eventsource/request-credentials.window.html
new file mode 100644
index 00000000000..11a549ff021
--- /dev/null
+++ b/Tests/LibWeb/Text/input/wpt-import/eventsource/request-credentials.window.html
@@ -0,0 +1,8 @@
+<!doctype html>
+<meta charset=utf-8>
+<title>EventSource: credentials</title>
+<script src="../resources/testharness.js"></script>
+<script src="../resources/testharnessreport.js"></script>
+
+<div id=log></div>
+<script src="../eventsource/request-credentials.window.js"></script>
diff --git a/Tests/LibWeb/Text/input/wpt-import/eventsource/request-credentials.window.js b/Tests/LibWeb/Text/input/wpt-import/eventsource/request-credentials.window.js
new file mode 100644
index 00000000000..d7c554aa4a2
--- /dev/null
+++ b/Tests/LibWeb/Text/input/wpt-import/eventsource/request-credentials.window.js
@@ -0,0 +1,37 @@
+// META: title=EventSource: credentials
+ var crossdomain = location.href
+ .replace('://', '://www2.')
+ .replace(/\/[^\/]*$/, '/')
+
+ function testCookie(desc, success, props, id) {
+ var test = async_test(document.title + ': credentials ' + desc)
+ test.step(function() {
+ var source = new EventSource(crossdomain + "resources/cors-cookie.py?ident=" + id, props)
+
+ source.onmessage = test.step_func(function(e) {
+ if(e.data.indexOf("first") == 0) {
+ assert_equals(e.data, "first NO_COOKIE", "cookie status")
+ }
+ else if(e.data.indexOf("second") == 0) {
+ if (success)
+ assert_equals(e.data, "second COOKIE", "cookie status")
+ else
+ assert_equals(e.data, "second NO_COOKIE", "cookie status")
+
+ source.close()
+ test.done()
+ }
+ else {
+ assert_unreached("unrecognized data returned: " + e.data)
+ source.close()
+ test.done()
+ }
+ })
+ })
+ }
+
+ testCookie('enabled', true, { withCredentials: true }, '1_' + new Date().getTime())
+ testCookie('disabled', false, { withCredentials: false }, '2_' + new Date().getTime())
+ testCookie('default', false, { }, '3_' + new Date().getTime())
+
+
diff --git a/Tests/LibWeb/Text/input/wpt-import/eventsource/resources/cors-cookie.py b/Tests/LibWeb/Text/input/wpt-import/eventsource/resources/cors-cookie.py
new file mode 100644
index 00000000000..9eaab9b95a0
--- /dev/null
+++ b/Tests/LibWeb/Text/input/wpt-import/eventsource/resources/cors-cookie.py
@@ -0,0 +1,31 @@
+from datetime import datetime
+
+def main(request, response):
+ last_event_id = request.headers.get(b"Last-Event-Id", b"")
+ ident = request.GET.first(b'ident', b"test")
+ cookie = b"COOKIE" if ident in request.cookies else b"NO_COOKIE"
+ origin = request.GET.first(b'origin', request.headers[b"origin"])
+ credentials = request.GET.first(b'credentials', b'true')
+
+ headers = []
+
+ if origin != b'none':
+ headers.append((b"Access-Control-Allow-Origin", origin));
+
+ if credentials != b'none':
+ headers.append((b"Access-Control-Allow-Credentials", credentials));
+
+ if last_event_id == b'':
+ headers.append((b"Content-Type", b"text/event-stream"))
+ response.set_cookie(ident, b"COOKIE")
+ data = b"id: 1\nretry: 200\ndata: first %s\n\n" % cookie
+ elif last_event_id == b'1':
+ headers.append((b"Content-Type", b"text/event-stream"))
+ long_long_time_ago = datetime.now().replace(year=2001, month=7, day=27)
+ response.set_cookie(ident, b"COOKIE", expires=long_long_time_ago)
+ data = b"id: 2\ndata: second %s\n\n" % cookie
+ else:
+ headers.append((b"Content-Type", b"stop"))
+ data = b"data: " + last_event_id + cookie + b"\n\n";
+
+ return headers, data
diff --git a/Tests/LibWeb/Text/input/wpt-import/eventsource/resources/last-event-id.py b/Tests/LibWeb/Text/input/wpt-import/eventsource/resources/last-event-id.py
new file mode 100644
index 00000000000..a2cb7264457
--- /dev/null
+++ b/Tests/LibWeb/Text/input/wpt-import/eventsource/resources/last-event-id.py
@@ -0,0 +1,9 @@
+def main(request, response):
+ response.headers.set(b"Content-Type", b"text/event-stream")
+
+ last_event_id = request.headers.get(b"Last-Event-ID", b"")
+ if last_event_id:
+ return b"data: " + last_event_id + b"\n\n"
+ else:
+ idvalue = request.GET.first(b"idvalue", u"\u2026".encode("utf-8"))
+ return b"id: " + idvalue + b"\nretry: 200\ndata: hello\n\n"