-
Notifications
You must be signed in to change notification settings - Fork 1k
Expand file tree
/
Copy pathhttp_wire.h
More file actions
354 lines (313 loc) · 10.7 KB
/
http_wire.h
File metadata and controls
354 lines (313 loc) · 10.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
#ifndef SPACETIMEDB_HTTP_WIRE_H
#define SPACETIMEDB_HTTP_WIRE_H
#pragma once
#include <string>
#include <vector>
#include <optional>
#include <cstdint>
#include "spacetimedb/bsatn/bsatn.h"
#include "spacetimedb/bsatn/traits.h"
#include "spacetimedb/bsatn/time_duration.h"
/**
* @file http_wire.h
* @brief BSATN wire format types for HTTP requests/responses
*
* These types mirror the Rust types in `spacetimedb_lib::http` and are used for
* BSATN encoding/decoding when communicating with the SpacetimeDB host.
*
* CRITICAL: Field order MUST match Rust exactly for BSATN compatibility!
*
* These types are internal implementation details. User code should use the types
* in http.h instead. Conversion functions in http_conversions.h handle the mapping.
*
* @warning Do NOT change the field order or layout of these types without coordinating
* with the Rust side. Breaking BSATN compatibility will cause runtime failures.
*
* @ingroup sdk_internal
*/
namespace SpacetimeDB {
namespace wire {
/**
* @brief Wire format for HTTP method
*
* Matches Rust: `spacetimedb_lib::http::Method`
*
* BSATN enum representation:
* - Standard methods (Get, Head, Post, etc.) are represented as unit variants (no payload)
* - Extension(String) is represented as a variant with a String payload
*/
struct HttpMethod {
enum class Tag : uint8_t {
Get = 0,
Head = 1,
Post = 2,
Put = 3,
Delete = 4,
Connect = 5,
Options = 6,
Trace = 7,
Patch = 8,
Extension = 9,
};
Tag tag;
std::string extension; // Only valid when tag == Extension
};
inline bool operator==(const HttpMethod& lhs, const HttpMethod& rhs) {
return lhs.tag == rhs.tag && lhs.extension == rhs.extension;
}
inline bool operator!=(const HttpMethod& lhs, const HttpMethod& rhs) {
return !(lhs == rhs);
}
/**
* @brief Wire format for HTTP version
*
* Matches Rust: `spacetimedb_lib::http::Version`
*
* BSATN enum representation (unit variants only):
* - Http09 = 0
* - Http10 = 1
* - Http11 = 2
* - Http2 = 3
* - Http3 = 4
*/
struct HttpVersion {
enum class Tag : uint8_t {
Http09 = 0,
Http10 = 1,
Http11 = 2,
Http2 = 3,
Http3 = 4,
};
Tag tag;
};
/**
* @brief Wire format for a single HTTP header name/value pair
*
* Matches Rust: `spacetimedb_lib::http::HttpHeaderPair`
*
* Field order: name, value (MUST match Rust!)
*
* Note: The `is_sensitive` flag from the user-facing HttpHeader type is NOT transmitted.
* It's a local-only hint and is not part of the wire format.
*/
struct HttpHeaderPair {
std::string name; // Field 0: Header name (valid HTTP header name)
std::vector<uint8_t> value; // Field 1: Header value bytes
};
/**
* @brief Wire format for HTTP headers collection
*
* Matches Rust: `spacetimedb_lib::http::Headers`
*
* Field order: entries (MUST match Rust!)
*
* BSATN representation:
* - Single field `entries` which is a Vec<HttpHeaderPair>
* - Headers with the same name appear as multiple entries
*/
struct HttpHeaders {
std::vector<HttpHeaderPair> entries; // Field 0: Array of header pairs
};
/**
* @brief Wire format for HTTP request
*
* Matches Rust: `spacetimedb_lib::http::Request`
*
* Field order (CRITICAL - MUST match Rust exactly!):
* 0. method: HttpMethod
* 1. headers: HttpHeaders
* 2. timeout: Option<TimeDuration>
* 3. uri: String
* 4. version: HttpVersion
*
* Note: The request body is NOT part of this struct. It's passed separately
* to the host via the ConsumeBytes() mechanism.
*/
struct HttpRequest {
HttpMethod method; // Field 0
HttpHeaders headers; // Field 1
std::optional<TimeDuration> timeout; // Field 2
std::string uri; // Field 3
HttpVersion version; // Field 4
};
/**
* @brief Wire format for HTTP response
*
* Matches Rust: `spacetimedb_lib::http::Response`
*
* Field order (CRITICAL - MUST match Rust exactly!):
* 0. headers: HttpHeaders
* 1. version: HttpVersion
* 2. code: u16
*
* Note: The response body is NOT part of this struct. It's received separately
* from the host via the ConsumeBytes() mechanism.
*/
struct HttpResponse {
HttpHeaders headers; // Field 0
HttpVersion version; // Field 1
uint16_t code; // Field 2: HTTP status code
};
} // namespace wire
} // namespace SpacetimeDB
// ==================== BSATN Serialization Traits ====================
namespace SpacetimeDB::bsatn {
// Forward declarations for recursive types
template<> struct bsatn_traits<wire::HttpMethod>;
template<> struct bsatn_traits<wire::HttpVersion>;
template<> struct bsatn_traits<wire::HttpHeaderPair>;
template<> struct bsatn_traits<wire::HttpHeaders>;
template<> struct bsatn_traits<wire::HttpRequest>;
template<> struct bsatn_traits<wire::HttpResponse>;
// HttpMethod enum serialization
template<>
struct bsatn_traits<wire::HttpMethod> {
static void serialize(Writer& writer, const wire::HttpMethod& value) {
// Encode tag as u8
writer.write_u8(static_cast<uint8_t>(value.tag));
// If Extension variant, encode the string payload
if (value.tag == wire::HttpMethod::Tag::Extension) {
bsatn::serialize(writer, value.extension);
}
}
static wire::HttpMethod deserialize(Reader& reader) {
wire::HttpMethod result;
result.tag = static_cast<wire::HttpMethod::Tag>(reader.read_u8());
// If Extension variant, decode the string payload
if (result.tag == wire::HttpMethod::Tag::Extension) {
result.extension = bsatn::deserialize<std::string>(reader);
}
return result;
}
static AlgebraicType algebraic_type() {
return AlgebraicType::U8();
}
};
// HttpVersion enum serialization (simple tag-only enum)
template<>
struct bsatn_traits<wire::HttpVersion> {
static void serialize(Writer& writer, const wire::HttpVersion& value) {
writer.write_u8(static_cast<uint8_t>(value.tag));
}
static wire::HttpVersion deserialize(Reader& reader) {
wire::HttpVersion result;
result.tag = static_cast<wire::HttpVersion::Tag>(reader.read_u8());
return result;
}
static AlgebraicType algebraic_type() {
return AlgebraicType::U8();
}
};
// HttpHeaderPair struct serialization
template<>
struct bsatn_traits<wire::HttpHeaderPair> {
static void serialize(Writer& writer, const wire::HttpHeaderPair& value) {
// Field 0: name (String)
bsatn::serialize(writer, value.name);
// Field 1: value (Vec<u8>)
bsatn::serialize(writer, value.value);
}
static wire::HttpHeaderPair deserialize(Reader& reader) {
wire::HttpHeaderPair result;
// Field 0: name
result.name = bsatn::deserialize<std::string>(reader);
// Field 1: value
result.value = bsatn::deserialize<std::vector<uint8_t>>(reader);
return result;
}
static AlgebraicType algebraic_type() {
ProductTypeBuilder builder;
builder.with_field<std::string>("name");
builder.with_field<std::vector<uint8_t>>("value");
return AlgebraicType::make_product(builder.build());
}
};
// HttpHeaders struct serialization
template<>
struct bsatn_traits<wire::HttpHeaders> {
static void serialize(Writer& writer, const wire::HttpHeaders& value) {
// Field 0: entries (Vec<HttpHeaderPair>)
bsatn::serialize(writer, value.entries);
}
static wire::HttpHeaders deserialize(Reader& reader) {
wire::HttpHeaders result;
// Field 0: entries
result.entries = bsatn::deserialize<std::vector<wire::HttpHeaderPair>>(reader);
return result;
}
static AlgebraicType algebraic_type() {
ProductTypeBuilder builder;
builder.with_field<std::vector<wire::HttpHeaderPair>>("entries");
return AlgebraicType::make_product(builder.build());
}
};
// HttpRequest struct serialization
template<>
struct bsatn_traits<wire::HttpRequest> {
static void serialize(Writer& writer, const wire::HttpRequest& value) {
// Field 0: method
bsatn::serialize(writer, value.method);
// Field 1: headers
bsatn::serialize(writer, value.headers);
// Field 2: timeout
bsatn::serialize(writer, value.timeout);
// Field 3: uri
bsatn::serialize(writer, value.uri);
// Field 4: version
bsatn::serialize(writer, value.version);
}
static wire::HttpRequest deserialize(Reader& reader) {
wire::HttpRequest result;
// Field 0: method
result.method = bsatn::deserialize<wire::HttpMethod>(reader);
// Field 1: headers
result.headers = bsatn::deserialize<wire::HttpHeaders>(reader);
// Field 2: timeout
result.timeout = bsatn::deserialize<std::optional<TimeDuration>>(reader);
// Field 3: uri
result.uri = bsatn::deserialize<std::string>(reader);
// Field 4: version
result.version = bsatn::deserialize<wire::HttpVersion>(reader);
return result;
}
static AlgebraicType algebraic_type() {
ProductTypeBuilder builder;
builder.with_field<wire::HttpMethod>("method");
builder.with_field<wire::HttpHeaders>("headers");
builder.with_field<std::optional<TimeDuration>>("timeout");
builder.with_field<std::string>("uri");
builder.with_field<wire::HttpVersion>("version");
return AlgebraicType::make_product(builder.build());
}
};
// HttpResponse struct serialization
template<>
struct bsatn_traits<wire::HttpResponse> {
static void serialize(Writer& writer, const wire::HttpResponse& value) {
// Field 0: headers
bsatn::serialize(writer, value.headers);
// Field 1: version
bsatn::serialize(writer, value.version);
// Field 2: code
bsatn::serialize(writer, value.code);
}
static wire::HttpResponse deserialize(Reader& reader) {
wire::HttpResponse result;
// Field 0: headers
result.headers = bsatn::deserialize<wire::HttpHeaders>(reader);
// Field 1: version
result.version = bsatn::deserialize<wire::HttpVersion>(reader);
// Field 2: code
result.code = bsatn::deserialize<uint16_t>(reader);
return result;
}
static AlgebraicType algebraic_type() {
ProductTypeBuilder builder;
builder.with_field<wire::HttpHeaders>("headers");
builder.with_field<wire::HttpVersion>("version");
builder.with_field<uint16_t>("code");
return AlgebraicType::make_product(builder.build());
}
};
} // namespace SpacetimeDB::bsatn
#endif // SPACETIMEDB_HTTP_WIRE_H