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#include <string_view>
38
39#include <absl/base/nullability.h>
40#include <absl/status/status.h>
41#include <absl/status/statusor.h>
42
46#include "a11/net/http2.h"
48#include "a11/net/wire_stream.h"
49
50namespace a11::net {
51
72 absl::Time handshake_deadline = absl::InfiniteFuture();
73
75 absl::Status Validate() const;
76};
77
87 private:
88 struct ConstructorToken {};
89
90 public:
101 static absl::StatusOr<std::shared_ptr<WebSocketWireStream>> CreateClient(
102 std::string_view url, WireStreamOptions options = {},
103 WebSocketClientOptions websocket_options = {});
104
113 [[nodiscard]] const std::string& GetRequestPath() const {
114 return request_path_;
115 }
116
123 [[nodiscard]] const HttpHeaders& GetRequestHeaders() const {
124 return request_headers_;
125 }
126
127 explicit WebSocketWireStream(ConstructorToken, std::shared_ptr<State> state)
128 : ChannelWireStream(std::move(state)) {}
129
130 private:
131 static absl::StatusOr<std::shared_ptr<WebSocketWireStream>> CreateAccepted(
132 HttpRequest request, std::shared_ptr<Http2ResponseWriter> response,
134
135 // Set once, before the stream is handed to on_stream, and read-only after;
136 // no lock, because nothing ever writes them again.
137 std::string request_path_;
138 HttpHeaders request_headers_;
139
141};
142
152 std::string path = "/a11";
161 std::string path_prefix;
162 std::string bind_address = "127.0.0.1";
163 std::uint16_t port = 0;
167 // `GET /actions` on this same port, when something above filled in the
168 // handler.
174 // Response-header policy for this port's HTTP surface -- the `Server` header,
175 // cross-origin access and cache hints.
182
184 absl::Status Validate() const;
185};
186
189 std::function<a11::Task(std::shared_ptr<WebSocketWireStream>)>;
190
199 : public std::enable_shared_from_this<WebSocketWireServer> {
200 public:
208 static absl::StatusOr<std::shared_ptr<WebSocketWireServer>> Create(
211
213 absl::Status Stop();
215 absl::StatusOr<std::uint16_t> port() const;
217 [[nodiscard]] bool running() const;
219 [[nodiscard]] void* absl_nullable GetImpl() const;
220
221 private:
222 struct State;
223
224 explicit WebSocketWireServer(std::shared_ptr<State> state)
225 : state_(std::move(state)) {}
226
227 std::shared_ptr<State> state_;
228};
229
230} // namespace a11::net
231
232#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:199
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:86
WebSocketWireStream(ConstructorToken, std::shared_ptr< State > state)
Definition websocket_wire_stream.h:127
const HttpHeaders & GetRequestHeaders() const
The headers the accepted request carried.
Definition websocket_wire_stream.h:123
const std::string & GetRequestPath() const
The path this stream was accepted on, query string included.
Definition websocket_wire_stream.h:113
static absl::StatusOr< std::shared_ptr< WebSocketWireStream > > CreateClient(std::string_view url, WireStreamOptions options={}, WebSocketClientOptions websocket_options={})
Dials a WebSocket endpoint and returns a WireStream over it.
Definition websocket_wire_stream.cc:96
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:66
std::function< a11::Task(std::shared_ptr< WebSocketWireStream >)> OnWebSocketStream
Callback invoked with a fresh WireStream for each accepted connection.
Definition websocket_wire_stream.h:189
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:431
stores::ChunkStoreReaderOptions options
Definition runtime_streams.cc:31
Common response headers for A11 HTTP servers.
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:90
Client-side tuning for a WebSocketWireStream connection.
Definition websocket_wire_stream.h:59
absl::Time handshake_deadline
How long the handshake alone may take.
Definition websocket_wire_stream.h:72
HttpHeaders headers
Extra WebSocket handshake headers.
Definition websocket_wire_stream.h:61
absl::Status Validate() const
Definition websocket_wire_stream.cc:89
ChannelFramingOptions framing
Packet splitting and reassembly bounds.
Definition websocket_wire_stream.h:62
Http2Options http2_options
HTTP/2 connection and TLS policy.
Definition websocket_wire_stream.h:60
Listen address, path, and per-stream defaults for a WebSocketWireServer.
Definition websocket_wire_stream.h:151
std::string path_prefix
Also accept anything under this prefix, as well as path.
Definition websocket_wire_stream.h:161
ServerHeaderOptions headers
Response-header policy for this port's HTTP surface – the Server header, cross-origin access and cach...
Definition websocket_wire_stream.h:181
std::uint16_t port
Listen port; zero requests an ephemeral port.
Definition websocket_wire_stream.h:163
std::string bind_address
Local listen address.
Definition websocket_wire_stream.h:162
ChannelFramingOptions framing
Framing for accepted streams.
Definition websocket_wire_stream.h:165
Http2Options http2_options
Server HTTP/2 and TLS policy.
Definition websocket_wire_stream.h:166
std::string path
Exact WebSocket endpoint path.
Definition websocket_wire_stream.h:152
DescribeEndpointOptions describe
GET /actions on this same port, when something above filled in the handler.
Definition websocket_wire_stream.h:173
WireStreamOptions stream_options
Defaults for accepted streams.
Definition websocket_wire_stream.h:164
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...