18#ifndef A11_NET_HTTP_SSE_WIRE_STREAM_H_
19#define A11_NET_HTTP_SSE_WIRE_STREAM_H_
27#include <absl/base/nullability.h>
28#include <absl/status/status.h>
29#include <absl/status/statusor.h>
30#include <absl/time/time.h>
48 "/streams/{id}/message";
84 public std::enable_shared_from_this<HttpSseWireStream> {
96 absl::Status
Abort(absl::Status status)
override;
124 std::shared_ptr<State> state);
133 void SetId(std::string
id);
145 std::shared_ptr<InProcessWireStream> application_;
146 std::shared_ptr<InProcessWireStream> bridge_;
147 std::shared_ptr<State> state_;
162 struct ConstructorToken {};
175 static absl::StatusOr<std::shared_ptr<HttpSseClientWireStream>>
Create(
177 std::shared_ptr<Http2Client>
client =
nullptr,
187 std::shared_ptr<State> state,
196 static void ReceiveSseLoop(
197 const std::shared_ptr<HttpSseClientWireStream>&
self);
199 std::shared_ptr<ClientState> client_state_;
214 struct ConstructorToken {};
224 std::shared_ptr<State> state,
234 std::shared_ptr<ServerStreamState> server_state_;
241 std::function<
a11::Task(std::shared_ptr<HttpSseServerWireStream>)>;
260 static absl::StatusOr<std::shared_ptr<HttpSseServer>>
Create(
261 std::string bind_address, std::uint16_t
port,
281 : state_(std::move(state)) {}
283 static a11::Task HandleRequest(
const std::shared_ptr<State>& state,
285 std::shared_ptr<Http2ResponseWriter> response);
286 static a11::Task HandleConnect(
const std::shared_ptr<State>& state,
288 std::shared_ptr<Http2ResponseWriter> response);
289 static a11::Task HandleMessage(
const std::shared_ptr<State>& state,
291 std::shared_ptr<Http2ResponseWriter> response);
293 std::shared_ptr<State> state_;
The client-side HTTP SSE wire stream, dialing out to a server URL.
Definition http_sse_wire_stream.h:158
std::shared_ptr< Http2Client > client() const
Definition http_sse_wire_stream.cc:615
void *absl_nullable TransportImpl() const override
Definition http_sse_wire_stream.cc:831
a11::Task OpenTransport() override
Definition http_sse_wire_stream.cc:620
static absl::StatusOr< std::shared_ptr< HttpSseClientWireStream > > Create(std::string url, HttpSseOptions options={}, std::shared_ptr< Http2Client > client=nullptr, HttpHeaders request_headers={})
Creates a client SSE wire stream connecting to url.
Definition http_sse_wire_stream.cc:560
absl::Status Transmit(data::WireMessage message) override
Definition http_sse_wire_stream.cc:697
The server-side HTTP SSE wire stream, accepted from a client.
Definition http_sse_wire_stream.h:210
a11::Task Accepted() const
Definition http_sse_wire_stream.cc:859
void *absl_nullable TransportImpl() const override
Definition http_sse_wire_stream.cc:914
void TransportDone() override
Definition http_sse_wire_stream.cc:918
absl::Status Transmit(data::WireMessage message) override
Definition http_sse_wire_stream.cc:895
a11::Task OpenTransport() override
Definition http_sse_wire_stream.cc:863
Hosts HTTP SSE wire streams on top of an HTTP/2 server.
Definition http_sse_wire_stream.h:249
std::uint16_t port() const
Definition http_sse_wire_stream.cc:1297
absl::Status Stop()
Stops the server and releases its resources.
Definition http_sse_wire_stream.cc:1269
~HttpSseServer()
Definition http_sse_wire_stream.cc:1044
std::shared_ptr< Http2Server > http2_server() const
Definition http_sse_wire_stream.cc:1308
bool running() const
Definition http_sse_wire_stream.cc:1302
static absl::StatusOr< std::shared_ptr< HttpSseServer > > Create(std::string bind_address, std::uint16_t port, OnHttpSseConnect on_connect={}, HttpSseOptions options={})
Creates and starts an SSE server accepting A11 wire streams.
Definition http_sse_wire_stream.cc:1008
a11::Future< std::shared_ptr< HttpSseServerWireStream > > WaitForStream()
Definition http_sse_wire_stream.cc:1249
Common base for the client and server HTTP SSE wire streams.
Definition http_sse_wire_stream.h:84
Role
Definition http_sse_wire_stream.h:119
absl::Status Abort(absl::Status status) override
Abort the stream, discarding buffered work.
Definition http_sse_wire_stream.cc:411
a11::Task Start(OnMessage on_message, OnDone on_done) override
Begin the stream as the initiating ("start") side.
Definition http_sse_wire_stream.cc:235
void FailTransport(absl::Status status)
Definition http_sse_wire_stream.cc:387
a11::Task ReceiveTransportMessage(data::WireMessage message)
Definition http_sse_wire_stream.cc:366
absl::Status SetHttpRequestHeaders(HttpHeaders headers)
Sets HTTP headers to send on the SSE request; call before connecting.
Definition http_sse_wire_stream.cc:465
virtual absl::Status Transmit(data::WireMessage message)=0
a11::Task Accept(OnMessage on_message, OnDone on_done) override
Begin the stream as the accepting ("accept") side.
Definition http_sse_wire_stream.cc:239
absl::Status SetDeadline()
Clear any deadline (equivalent to an infinite deadline).
Definition wire_stream.h:154
absl::Status GetStatus() const override
Definition http_sse_wire_stream.cc:431
absl::Status Send(data::WireMessage message) override
Enqueue a message for delivery to the peer.
Definition http_sse_wire_stream.cc:231
absl::Status HalfClose()
Half-close with no trailers.
Definition wire_stream.h:125
void *absl_nullable GetImpl() const override
Definition http_sse_wire_stream.cc:451
void MarkHttpHeadersReady(HttpHeaders headers)
Definition http_sse_wire_stream.cc:505
a11::Task StartEndpoint(bool accept, OnMessage on_message, OnDone on_done)
Definition http_sse_wire_stream.cc:243
HttpHeaders GetHttpRequestHeaders() const
Definition http_sse_wire_stream.cc:455
virtual void TransportDone()
Definition http_sse_wire_stream.h:141
std::string GetId() const override
Definition http_sse_wire_stream.cc:446
std::optional< HttpHeaders > GetHttpResponseHeaders() const
Definition http_sse_wire_stream.cc:460
a11::Task StartInternalBridge()
Definition http_sse_wire_stream.cc:289
absl::Status SetHttpResponseHeaders(HttpHeaders headers)
Sets HTTP headers to send on the SSE response (server side).
Definition http_sse_wire_stream.cc:483
a11::Task WaitForHttpHeaders() const
Definition http_sse_wire_stream.cc:501
absl::Time deadline() const override
Definition http_sse_wire_stream.cc:427
virtual void *absl_nullable TransportImpl() const =0
std::shared_ptr< InProcessWireStream > bridge() const
Definition http_sse_wire_stream.cc:530
void SetId(std::string id)
Definition http_sse_wire_stream.cc:520
virtual a11::Task OpenTransport()=0
std::optional< data::ByteMap > GetTrailers() const override
Definition http_sse_wire_stream.cc:442
a11::Task HandleBridgeMessage(std::optional< data::WireMessage > message)
Definition http_sse_wire_stream.cc:311
HttpSseOptions options() const
Definition http_sse_wire_stream.cc:525
a11::Task HandleBridgeDone()
Definition http_sse_wire_stream.cc:331
a11::Task DrainOutgoingMessages() override
Await delivery of all buffered outbound messages.
Definition http_sse_wire_stream.cc:407
~HttpSseWireStream() override=default
std::pair< std::shared_ptr< InProcessWireStream >, std::shared_ptr< InProcessWireStream > > Pair
A connected pair of endpoints returned by CreatePair().
Definition in_process_wire_stream.h:53
A bidirectional, message-oriented channel between two A11 endpoints.
Definition wire_stream.h:89
absl::Status SetDeadline()
Clear any deadline (equivalent to an infinite deadline).
Definition wire_stream.h:154
absl::Status HalfClose()
Half-close with no trailers.
Definition wire_stream.h:125
Completion values used by every asynchronous A11 operation.
A11's nghttp2 HTTP/2 client, server, and streaming primitives.
In-memory paired WireStream that connects two endpoints without a network.
absl::flat_hash_map< std::string, Bytes > ByteMap
String-keyed map of byte values (headers, attributes, etc.).
Definition types.h:42
std::function< a11::Task(std::optional< data::WireMessage > message)> OnMessage
Called for each inbound message; std::nullopt signals the peer half-closed.
Definition wire_stream.h:65
std::function< a11::Task()> OnDone
Called once, when the stream has fully finished (cleanly or via abort).
Definition wire_stream.h:67
std::vector< std::pair< std::string, std::string > > HttpHeaders
An ordered list of (name, value) HTTP/2 header fields.
Definition http2.h:45
constexpr std::string_view kSseStreamIdHeader
Response header naming the stream id assigned to an SSE connection.
Definition http_sse_wire_stream.h:41
constexpr std::string_view kDefaultSseConnectEndpoint
Default path on which a client opens the SSE event stream.
Definition http_sse_wire_stream.h:45
constexpr std::string_view kSseHttpHeaderPrefix
Prefix under which application HTTP headers are tunneled over SSE.
Definition http_sse_wire_stream.h:43
constexpr std::string_view kDefaultSseMessageEndpoint
Default message-post path template ({id} is the stream id).
Definition http_sse_wire_stream.h:47
std::function< a11::Task(std::shared_ptr< HttpSseServerWireStream >)> OnHttpSseConnect
Callback invoked with each accepted server-side SSE wire stream.
Definition http_sse_wire_stream.h:241
Future< Unit > Task
Asynchronous operation whose only successful result is completion itself.
Definition future.h:403
Future< T > SubmitWithCancellationHook(absl::AnyInvocable< absl::StatusOr< T >() && > work, std::function< void()> cancellation_hook, thread::TreeOptions tree_options)
Run work on A11's fiber pool with application-specific cancellation.
Definition executor.h:30
The top-level frame exchanged between two A11 endpoints.
Definition types.h:275
Body-size limits, buffering thresholds, deadline, and TLS for an HTTP/2 client or server.
Definition http2.h:103
A parsed HTTP/2 request: pseudo-headers, fields, and body.
Definition http2.h:64
Definition http_sse_wire_stream.cc:534
Endpoint paths and transport tuning for an HTTP SSE wire stream.
Definition http_sse_wire_stream.h:58
std::string cors_allow_headers
Access-Control-Allow-Headers value.
Definition http_sse_wire_stream.h:67
std::string cors_allow_origin
Access-Control-Allow-Origin value.
Definition http_sse_wire_stream.h:65
WireStreamOptions stream_options
Per-stream buffering and deadline.
Definition http_sse_wire_stream.h:59
std::string message_endpoint
Outbound POST template.
Definition http_sse_wire_stream.h:63
std::string cors_allow_methods
Access-Control-Allow-Methods value.
Definition http_sse_wire_stream.h:66
absl::Status Validate() const
Definition http_sse_wire_stream.cc:193
std::string cors_expose_headers
Access-Control-Expose-Headers value.
Definition http_sse_wire_stream.h:68
std::string connect_endpoint
SSE connect POST path.
Definition http_sse_wire_stream.h:61
Http2Options http2_options
Shared HTTP/2 transport and TLS policy.
Definition http_sse_wire_stream.h:60
Definition http_sse_wire_stream.cc:838
Definition http_sse_wire_stream.cc:928
Buffering, sizing, and deadline limits for a WireStream endpoint.
Definition wire_stream.h:44
A11's core wire value types: chunks, node fragments and messages.
A11's transport abstraction: the bidirectional WireStream channel and the options/callbacks that driv...