A11 (C++ runtime)
Native C++ implementation of the A11 streaming action runtime
Loading...
Searching...
No Matches
websocket_wire_stream.h
Go to the documentation of this file.
1/*
2 * Copyright 2026 The A11 Authors
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
30#ifndef A11_NET_WEBSOCKET_WIRE_STREAM_H_
31#define A11_NET_WEBSOCKET_WIRE_STREAM_H_
32
33#include <cstdint>
34#include <functional>
35#include <memory>
36#include <string>
37
38#include <absl/base/nullability.h>
39#include <absl/status/status.h>
40#include <absl/status/statusor.h>
41
45#include "a11/net/http2.h"
47#include "a11/net/wire_stream.h"
48
49namespace a11::net {
50
71 absl::Time handshake_deadline = absl::InfiniteFuture();
72
74 absl::Status Validate() const;
75};
76
86 private:
87 struct ConstructorToken {};
88
89 public:
100 static absl::StatusOr<std::shared_ptr<WebSocketWireStream>> CreateClient(
101 const std::string& url, WireStreamOptions options = {},
102 WebSocketClientOptions websocket_options = {});
103
112 [[nodiscard]] const std::string& GetRequestPath() const {
113 return request_path_;
114 }
115
122 [[nodiscard]] const HttpHeaders& GetRequestHeaders() const {
123 return request_headers_;
124 }
125
126 explicit WebSocketWireStream(ConstructorToken, std::shared_ptr<State> state)
127 : ChannelWireStream(std::move(state)) {}
128
129 private:
130 static absl::StatusOr<std::shared_ptr<WebSocketWireStream>> CreateAccepted(
131 HttpRequest request, std::shared_ptr<Http2ResponseWriter> response,
133
134 // Set once, before the stream is handed to on_stream, and read-only after;
135 // no lock, because nothing ever writes them again.
136 std::string request_path_;
137 HttpHeaders request_headers_;
138
140};
141
151 std::string path = "/a11";
160 std::string path_prefix;
161 std::string bind_address = "127.0.0.1";
162 std::uint16_t port = 0;
166 // `GET /actions` on this same port, when something above filled in the
167 // handler.
173 // Response-header policy for this port's HTTP surface -- the `Server` header,
174 // cross-origin access and cache hints.
181
183 absl::Status Validate() const;
184};
185
188 std::function<a11::Task(std::shared_ptr<WebSocketWireStream>)>;
189
198 : public std::enable_shared_from_this<WebSocketWireServer> {
199 public:
207 static absl::StatusOr<std::shared_ptr<WebSocketWireServer>> Create(
208 OnWebSocketStream on_stream, WebSocketServerOptions options = {});
210
212 absl::Status Stop();
214 absl::StatusOr<std::uint16_t> port() const;
216 [[nodiscard]] bool running() const;
218 [[nodiscard]] void* absl_nullable GetImpl() const;
219
220 private:
221 struct State;
222
223 explicit WebSocketWireServer(std::shared_ptr<State> state)
224 : state_(std::move(state)) {}
225
226 std::shared_ptr<State> state_;
227};
228
229} // namespace a11::net
230
231#endif // A11_NET_WEBSOCKET_WIRE_STREAM_H_
Shared WireStream lifecycle and framing for binary channels.
Definition channel_wire_stream.h:69
Accepts inbound WebSocket connections and hands each to a callback.
Definition websocket_wire_stream.h:198
bool running() const
Definition websocket_wire_stream.cc:287
absl::StatusOr< std::uint16_t > port() const
Definition websocket_wire_stream.cc:279
~WebSocketWireServer()
Definition websocket_wire_stream.cc:266
absl::Status Stop()
Stops the server and closes the listening socket.
Definition websocket_wire_stream.cc:270
void *absl_nullable GetImpl() const
Definition websocket_wire_stream.cc:292
static absl::StatusOr< std::shared_ptr< WebSocketWireServer > > Create(OnWebSocketStream on_stream, WebSocketServerOptions options={})
Starts a WebSocket server accepting A11 connections.
Definition websocket_wire_stream.cc:185
A WireStream that carries A11 traffic over a client or accepted WebSocket.
Definition websocket_wire_stream.h:85
WebSocketWireStream(ConstructorToken, std::shared_ptr< State > state)
Definition websocket_wire_stream.h:126
const HttpHeaders & GetRequestHeaders() const
The headers the accepted request carried.
Definition websocket_wire_stream.h:122
static absl::StatusOr< std::shared_ptr< WebSocketWireStream > > CreateClient(const std::string &url, WireStreamOptions options={}, WebSocketClientOptions websocket_options={})
Dials a WebSocket endpoint and returns a WireStream over it.
Definition websocket_wire_stream.cc:96
const std::string & GetRequestPath() const
The path this stream was accepted on, query string included.
Definition websocket_wire_stream.h:112
GET /actions, on whichever server happens to hold the port.
Completion values used by every asynchronous A11 operation.
A11's nghttp2 HTTP/2 client, server, and streaming primitives.
Definition action.h:65
std::function< a11::Task(std::shared_ptr< WebSocketWireStream >)> OnWebSocketStream
Callback invoked with a fresh WireStream for each accepted connection.
Definition websocket_wire_stream.h:188
std::vector< std::pair< std::string, std::string > > HttpHeaders
An ordered list of (name, value) HTTP/2 header fields.
Definition http2.h:70
Future< Unit > Task
Asynchronous operation whose only successful result is completion itself.
Definition future.h:411
The response headers every A11 HTTP server sends, in one place.
Packetisation and incomplete-message bounds for a binary WireStream.
Definition channel_wire_stream.h:46
Where and whether a server answers discovery over plain HTTP.
Definition describe_endpoint.h:72
Body-size limits, buffering thresholds, deadline, TLS, and HTTP protocol negotiation for an HTTP clie...
Definition http2.h:150
A parsed HTTP/2 request: pseudo-headers, fields, and body.
Definition http2.h:90
The response-header policy of one A11 HTTP surface.
Definition server_headers.h:112
Client-side tuning for a WebSocketWireStream connection.
Definition websocket_wire_stream.h:58
absl::Time handshake_deadline
How long the handshake alone may take.
Definition websocket_wire_stream.h:71
HttpHeaders headers
Extra WebSocket handshake headers.
Definition websocket_wire_stream.h:60
absl::Status Validate() const
Definition websocket_wire_stream.cc:89
ChannelFramingOptions framing
Packet splitting and reassembly bounds.
Definition websocket_wire_stream.h:61
Http2Options http2_options
HTTP/2 connection and TLS policy.
Definition websocket_wire_stream.h:59
Listen address, path, and per-stream defaults for a WebSocketWireServer.
Definition websocket_wire_stream.h:150
std::string path_prefix
Also accept anything under this prefix, as well as path.
Definition websocket_wire_stream.h:160
ServerHeaderOptions headers
Response-header policy for this port's HTTP surface – the Server header, cross-origin access and cach...
Definition websocket_wire_stream.h:180
std::uint16_t port
Listen port; zero requests an ephemeral port.
Definition websocket_wire_stream.h:162
std::string bind_address
Local listen address.
Definition websocket_wire_stream.h:161
ChannelFramingOptions framing
Framing for accepted streams.
Definition websocket_wire_stream.h:164
Http2Options http2_options
Server HTTP/2 and TLS policy.
Definition websocket_wire_stream.h:165
std::string path
Exact WebSocket endpoint path.
Definition websocket_wire_stream.h:151
DescribeEndpointOptions describe
GET /actions on this same port, when something above filled in the handler.
Definition websocket_wire_stream.h:172
WireStreamOptions stream_options
Defaults for accepted streams.
Definition websocket_wire_stream.h:163
absl::Status Validate() const
Definition websocket_wire_stream.cc:154
Definition websocket_wire_stream.cc:173
Buffering, sizing, and deadline limits for a WireStream endpoint.
Definition wire_stream.h:58
A11's transport abstraction: the bidirectional WireStream channel and the options/callbacks that driv...