-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
LibWeb: Implement EventSource for server-sent events
EventSource allows opening a persistent HTTP connection to a server over which events are continuously streamed. Unfortunately, our test infrastructure does not allow for automating any tests of this feature yet. It only works with HTTP connections.
- Loading branch information
Showing
12 changed files
with
619 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
/* | ||
* Copyright (c) 2024, Tim Flynn <trflynn89@serenityos.org> | ||
* | ||
* SPDX-License-Identifier: BSD-2-Clause | ||
*/ | ||
|
||
#pragma once | ||
|
||
#include <AK/String.h> | ||
#include <AK/StringBuilder.h> | ||
#include <AK/StringView.h> | ||
#include <AK/Time.h> | ||
#include <LibJS/Forward.h> | ||
#include <LibJS/Heap/GCPtr.h> | ||
#include <LibURL/URL.h> | ||
#include <LibWeb/DOM/EventTarget.h> | ||
#include <LibWeb/Forward.h> | ||
#include <LibWeb/WebIDL/ExceptionOr.h> | ||
#include <LibWeb/WebIDL/Types.h> | ||
|
||
namespace Web::HTML { | ||
|
||
struct EventSourceInit { | ||
bool with_credentials { false }; | ||
}; | ||
|
||
class EventSource : public DOM::EventTarget { | ||
WEB_PLATFORM_OBJECT(EventSource, DOM::EventTarget); | ||
JS_DECLARE_ALLOCATOR(EventSource); | ||
|
||
public: | ||
virtual ~EventSource() override; | ||
|
||
static WebIDL::ExceptionOr<JS::NonnullGCPtr<EventSource>> construct_impl(JS::Realm&, StringView url, EventSourceInit event_source_init_dict = {}); | ||
|
||
// https://html.spec.whatwg.org/multipage/server-sent-events.html#dom-eventsource-url | ||
String url() const { return MUST(String::from_byte_string(m_url.serialize())); } | ||
|
||
// https://html.spec.whatwg.org/multipage/server-sent-events.html#dom-eventsource-withcredentials | ||
bool with_credentials() const { return m_with_credentials; } | ||
|
||
enum class ReadyState : WebIDL::UnsignedShort { | ||
Connecting = 0, | ||
Open = 1, | ||
Closed = 2, | ||
}; | ||
|
||
// https://html.spec.whatwg.org/multipage/server-sent-events.html#dom-eventsource-readystate | ||
ReadyState ready_state() const { return m_ready_state; } | ||
|
||
void set_onopen(WebIDL::CallbackType*); | ||
WebIDL::CallbackType* onopen(); | ||
|
||
void set_onmessage(WebIDL::CallbackType*); | ||
WebIDL::CallbackType* onmessage(); | ||
|
||
void set_onerror(WebIDL::CallbackType*); | ||
WebIDL::CallbackType* onerror(); | ||
|
||
void close(); | ||
void forcibly_close(); | ||
|
||
private: | ||
explicit EventSource(JS::Realm&); | ||
|
||
virtual void initialize(JS::Realm&) override; | ||
virtual void finalize() override; | ||
virtual void visit_edges(Cell::Visitor&) override; | ||
|
||
void announce_the_connection(); | ||
void reestablish_the_connection(); | ||
void fail_the_connection(); | ||
|
||
void interpret_response(StringView); | ||
void process_field(StringView field, StringView value); | ||
void dispatch_the_event(); | ||
|
||
// https://html.spec.whatwg.org/multipage/server-sent-events.html#concept-eventsource-url | ||
URL::URL m_url; | ||
|
||
// https://html.spec.whatwg.org/multipage/server-sent-events.html#concept-event-stream-request | ||
JS::GCPtr<Fetch::Infrastructure::Request> m_request; | ||
|
||
// https://html.spec.whatwg.org/multipage/server-sent-events.html#concept-event-stream-reconnection-time | ||
Duration m_reconnection_time { Duration::from_seconds(3) }; | ||
|
||
// https://html.spec.whatwg.org/multipage/server-sent-events.html#concept-event-stream-last-event-id | ||
String m_last_event_id; | ||
|
||
String m_event_type; | ||
StringBuilder m_data; | ||
|
||
bool m_with_credentials { false }; | ||
|
||
ReadyState m_ready_state { ReadyState::Connecting }; | ||
|
||
JS::GCPtr<Fetch::Infrastructure::FetchAlgorithms> m_fetch_algorithms; | ||
JS::GCPtr<Fetch::Infrastructure::FetchController> m_fetch_controller; | ||
}; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
#import <DOM/EventHandler.idl> | ||
#import <DOM/EventTarget.idl> | ||
|
||
// https://html.spec.whatwg.org/multipage/server-sent-events.html#eventsource | ||
[Exposed=(Window,Worker)] | ||
interface EventSource : EventTarget { | ||
constructor(USVString url, optional EventSourceInit eventSourceInitDict = {}); | ||
|
||
readonly attribute USVString url; | ||
readonly attribute boolean withCredentials; | ||
|
||
// ready state | ||
const unsigned short CONNECTING = 0; | ||
const unsigned short OPEN = 1; | ||
const unsigned short CLOSED = 2; | ||
readonly attribute unsigned short readyState; | ||
|
||
// networking | ||
attribute EventHandler onopen; | ||
attribute EventHandler onmessage; | ||
attribute EventHandler onerror; | ||
undefined close(); | ||
}; | ||
|
||
dictionary EventSourceInit { | ||
boolean withCredentials = false; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters