proxy-sdk 1.1.0

Write extensions for Proxy-WASM for WASM or Native environments
Documentation
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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
use std::ops::RangeBounds;

use crate::{
    calculate_range,
    context::BaseContext,
    hostcalls::{self, BufferType, MapType},
    log_concern,
    property::envoy::Attributes,
    Status,
};

/// Defines control functions for http data
pub trait HttpControl {
    /// Request or Response
    const TYPE: HttpType;

    /// Retrieve attributes for the http data
    fn attributes(&self) -> &Attributes;

    /// If `true`, this is the last block
    fn end_of_stream(&self) -> bool {
        true
    }

    /// Resume a paused HTTP request/response
    fn resume(&self) {
        log_concern(Self::TYPE.resume(), Self::TYPE.call_resume())
    }

    /// Reset the HTTP request/response
    fn reset(&self) {
        log_concern(Self::TYPE.reset(), Self::TYPE.call_reset())
    }

    /// Send an early HTTP response, terminating the current request/response
    fn send_http_response(
        &self,
        status_code: u32,
        headers: &[(&str, &[u8])],
        body: Option<&[u8]>,
    ) -> Result<(), Status> {
        hostcalls::send_http_response(status_code, headers, body)
    }

    /// Mark this transaction as complete
    fn done(&self) {
        log_concern("trigger-done", hostcalls::done());
    }
}

/// Defines functions to interact with header data
pub trait HttpHeaderControl: HttpControl {
    /// The header type
    const HEADER_TYPE: HeaderType;

    /// Number of headers contained in block
    fn header_count(&self) -> usize;

    /// Get all headers in this block
    fn all(&self) -> Vec<(String, Vec<u8>)> {
        log_concern(
            Self::HEADER_TYPE.all(),
            hostcalls::get_map(Self::HEADER_TYPE.map()),
        )
        .unwrap_or_default()
    }

    /// Check for a specific header value
    fn get(&self, name: impl AsRef<str>) -> Option<Vec<u8>> {
        log_concern(
            Self::HEADER_TYPE.get(),
            hostcalls::get_map_value(Self::HEADER_TYPE.map(), name.as_ref()),
        )
    }

    /// Set a specific header
    fn set(&self, name: impl AsRef<str>, value: impl AsRef<[u8]>) {
        log_concern(
            Self::HEADER_TYPE.set(),
            hostcalls::set_map_value(Self::HEADER_TYPE.map(), name.as_ref(), Some(value.as_ref())),
        );
    }

    /// Replace all headers in this block
    fn set_all(&self, values: &[(&str, &[u8])]) {
        log_concern(
            Self::HEADER_TYPE.set_all(),
            hostcalls::set_map(Self::HEADER_TYPE.map(), values),
        );
    }

    /// Add a header to this block (append to existing if present)
    fn add(&self, name: impl AsRef<str>, value: impl AsRef<[u8]>) {
        log_concern(
            Self::HEADER_TYPE.add(),
            hostcalls::add_map_value(Self::HEADER_TYPE.map(), name.as_ref(), value.as_ref()),
        );
    }

    /// Remove a header from this block
    fn remove(&self, name: impl AsRef<str>) {
        log_concern(
            Self::HEADER_TYPE.remove(),
            hostcalls::set_map_value(Self::HEADER_TYPE.map(), name.as_ref(), None),
        );
    }
}

/// Defines functions to interact with body data
pub trait HttpBodyControl: HttpControl {
    /// Length of this body fragment
    fn body_size(&self) -> usize;

    /// Get a range of the body block content
    fn get(&self, range: impl RangeBounds<usize>) -> Option<Vec<u8>> {
        let (start, size) = calculate_range(range, self.body_size());
        log_concern(
            Self::TYPE.get(),
            hostcalls::get_buffer(Self::TYPE.buffer(), start, size),
        )
    }

    /// Set a range of the body block content
    fn set(&self, range: impl RangeBounds<usize>, value: &[u8]) {
        let (start, size) = calculate_range(range, self.body_size());
        log_concern(
            Self::TYPE.set(),
            hostcalls::set_buffer(Self::TYPE.buffer(), start, size, value),
        );
    }

    /// Get the entire body block content
    fn all(&self) -> Option<Vec<u8>> {
        self.get(..)
    }

    /// Replace the entire body block with `value`
    fn replace(&self, value: &[u8]) {
        self.set(.., value);
    }

    /// Clear the entire body block
    fn clear(&self) {
        self.replace(&[]);
    }
}

/// Defines which section the header data belongs too
pub enum HeaderType {
    RequestHeaders,
    RequestTrailers,
    ResponseHeaders,
    ResponseTrailers,
}

impl HeaderType {
    const fn all(&self) -> &'static str {
        match self {
            Self::RequestHeaders => "get-all-request-header",
            Self::RequestTrailers => "get-all-request-trailer",
            Self::ResponseHeaders => "get-all-response-header",
            Self::ResponseTrailers => "get-all-response-trailer",
        }
    }

    const fn get(&self) -> &'static str {
        match self {
            Self::RequestHeaders => "get-request-header",
            Self::RequestTrailers => "get-request-trailer",
            Self::ResponseHeaders => "get-response-header",
            Self::ResponseTrailers => "get-response-trailer",
        }
    }

    const fn set(&self) -> &'static str {
        match self {
            Self::RequestHeaders => "set-request-header",
            Self::RequestTrailers => "set-request-trailer",
            Self::ResponseHeaders => "set-response-header",
            Self::ResponseTrailers => "set-response-trailer",
        }
    }

    const fn set_all(&self) -> &'static str {
        match self {
            Self::RequestHeaders => "set-all-request-headers",
            Self::RequestTrailers => "set-all-request-trailers",
            Self::ResponseHeaders => "set-all-response-headers",
            Self::ResponseTrailers => "set-all-response-trailers",
        }
    }

    const fn add(&self) -> &'static str {
        match self {
            Self::RequestHeaders => "add-request-headers",
            Self::RequestTrailers => "add-request-trailers",
            Self::ResponseHeaders => "add-response-headers",
            Self::ResponseTrailers => "add-response-trailers",
        }
    }

    const fn remove(&self) -> &'static str {
        match self {
            Self::RequestHeaders => "remove-request-headers",
            Self::RequestTrailers => "remove-request-trailers",
            Self::ResponseHeaders => "remove-response-headers",
            Self::ResponseTrailers => "remove-response-trailers",
        }
    }

    const fn map(&self) -> MapType {
        match self {
            HeaderType::RequestHeaders => MapType::HttpRequestHeaders,
            HeaderType::RequestTrailers => MapType::HttpRequestTrailers,
            HeaderType::ResponseHeaders => MapType::HttpResponseHeaders,
            HeaderType::ResponseTrailers => MapType::HttpResponseTrailers,
        }
    }
}

/// Defines if data belongs to a request or response
pub enum HttpType {
    Request,
    Response,
}

impl HttpType {
    const fn resume(&self) -> &'static str {
        match self {
            HttpType::Request => "resume-http-request",
            HttpType::Response => "resume-http-response",
        }
    }

    fn call_resume(&self) -> Result<(), Status> {
        match self {
            HttpType::Request => hostcalls::resume_http_request(),
            HttpType::Response => hostcalls::resume_http_response(),
        }
    }

    const fn reset(&self) -> &'static str {
        match self {
            HttpType::Request => "reset-http-request",
            HttpType::Response => "reset-http-response",
        }
    }

    fn call_reset(&self) -> Result<(), Status> {
        match self {
            HttpType::Request => hostcalls::reset_http_request(),
            HttpType::Response => hostcalls::reset_http_response(),
        }
    }

    const fn get(&self) -> &'static str {
        match self {
            HttpType::Request => "get-request-body",
            HttpType::Response => "get-response-body",
        }
    }

    const fn set(&self) -> &'static str {
        match self {
            HttpType::Request => "set-request-body",
            HttpType::Response => "set-response-body",
        }
    }

    const fn buffer(&self) -> BufferType {
        match self {
            HttpType::Request => BufferType::HttpRequestBody,
            HttpType::Response => BufferType::HttpResponseBody,
        }
    }
}

/// Return status for header callbacks
#[repr(usize)]
#[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)]
#[non_exhaustive]
pub enum FilterHeadersStatus {
    Continue = 0,
    StopIteration = 1,
    ContinueAndEndStream = 2,
    StopAllIterationAndBuffer = 3,
    StopAllIterationAndWatermark = 4,
}

/// Return status for trailer callbacks
#[repr(usize)]
#[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)]
#[non_exhaustive]
pub enum FilterTrailersStatus {
    Continue = 0,
    StopIteration = 1,
}

/// Return status for body callbacks
#[repr(usize)]
#[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)]
#[non_exhaustive]
pub enum FilterDataStatus {
    Continue = 0,
    StopAllIterationAndBuffer = 1,
    StopAllIterationAndWatermark = 2,
    StopIterationNoBuffer = 3,
}

/// Request header context
pub struct RequestHeaders {
    pub(crate) header_count: usize,
    pub(crate) end_of_stream: bool,
    pub(crate) attributes: Attributes,
}

impl HttpControl for RequestHeaders {
    const TYPE: HttpType = HttpType::Request;

    fn attributes(&self) -> &Attributes {
        &self.attributes
    }

    fn end_of_stream(&self) -> bool {
        self.end_of_stream
    }
}

impl HttpHeaderControl for RequestHeaders {
    const HEADER_TYPE: HeaderType = HeaderType::RequestHeaders;

    fn header_count(&self) -> usize {
        self.header_count
    }
}

pub struct RequestBody {
    pub(crate) body_size: usize,
    pub(crate) end_of_stream: bool,
    pub(crate) attributes: Attributes,
}

impl HttpControl for RequestBody {
    const TYPE: HttpType = HttpType::Request;

    fn attributes(&self) -> &Attributes {
        &self.attributes
    }

    fn end_of_stream(&self) -> bool {
        self.end_of_stream
    }
}

impl HttpBodyControl for RequestBody {
    fn body_size(&self) -> usize {
        self.body_size
    }
}

pub struct RequestTrailers {
    pub(crate) trailer_count: usize,
    pub(crate) attributes: Attributes,
}

impl HttpControl for RequestTrailers {
    const TYPE: HttpType = HttpType::Request;

    fn attributes(&self) -> &Attributes {
        &self.attributes
    }
}

impl HttpHeaderControl for RequestTrailers {
    const HEADER_TYPE: HeaderType = HeaderType::RequestTrailers;

    fn header_count(&self) -> usize {
        self.trailer_count
    }
}

pub struct ResponseHeaders {
    pub(crate) header_count: usize,
    pub(crate) end_of_stream: bool,
    pub(crate) attributes: Attributes,
}

impl HttpControl for ResponseHeaders {
    const TYPE: HttpType = HttpType::Response;

    fn attributes(&self) -> &Attributes {
        &self.attributes
    }

    fn end_of_stream(&self) -> bool {
        self.end_of_stream
    }
}

impl HttpHeaderControl for ResponseHeaders {
    const HEADER_TYPE: HeaderType = HeaderType::ResponseHeaders;

    fn header_count(&self) -> usize {
        self.header_count
    }
}

pub struct ResponseBody {
    pub(crate) body_size: usize,
    pub(crate) end_of_stream: bool,
    pub(crate) attributes: Attributes,
}

impl HttpControl for ResponseBody {
    const TYPE: HttpType = HttpType::Response;

    fn attributes(&self) -> &Attributes {
        &self.attributes
    }

    fn end_of_stream(&self) -> bool {
        self.end_of_stream
    }
}

impl HttpBodyControl for ResponseBody {
    fn body_size(&self) -> usize {
        self.body_size
    }
}

pub struct ResponseTrailers {
    pub(crate) trailer_count: usize,
    pub(crate) attributes: Attributes,
}

impl HttpControl for ResponseTrailers {
    const TYPE: HttpType = HttpType::Response;

    fn attributes(&self) -> &Attributes {
        &self.attributes
    }
}

impl HttpHeaderControl for ResponseTrailers {
    const HEADER_TYPE: HeaderType = HeaderType::ResponseTrailers;

    fn header_count(&self) -> usize {
        self.trailer_count
    }
}

/// Context for a HTTP filter plugin.
#[allow(unused_variables)]
pub trait HttpContext: BaseContext {
    /// Called one or more times as the proxy receives request headers. If `headers.end_of_stream()` is true, then they are the last request headers.
    fn on_http_request_headers(&mut self, headers: &RequestHeaders) -> FilterHeadersStatus {
        FilterHeadersStatus::Continue
    }

    /// Called only if and only if there is a request body. Called one or more times as the proxy receives blocks of request body data. If `body.end_of_stream()` is true, it is the last block.
    fn on_http_request_body(&mut self, body: &RequestBody) -> FilterDataStatus {
        FilterDataStatus::Continue
    }

    /// Called once if and only if any trailers are sent at the end of the request. Not called multiple times.
    fn on_http_request_trailers(&mut self, trailers: &RequestTrailers) -> FilterTrailersStatus {
        FilterTrailersStatus::Continue
    }

    /// Called one or more times as the proxy receives response headers. If `headers.end_of_stream()` is true, then they are the last response headers.
    fn on_http_response_headers(&mut self, headers: &ResponseHeaders) -> FilterHeadersStatus {
        FilterHeadersStatus::Continue
    }

    /// Called only if and only if there is a response body. Called one or more times as the proxy receives blocks of response body data. If `body.end_of_stream()` is true, it is the last block.
    fn on_http_response_body(&mut self, body: &ResponseBody) -> FilterDataStatus {
        FilterDataStatus::Continue
    }

    /// Called once if and only if any trailers are sent at the end of the response. Not called multiple times.
    fn on_http_response_trailers(&mut self, trailers: &ResponseTrailers) -> FilterTrailersStatus {
        FilterTrailersStatus::Continue
    }
}