1use crate::hostcalls;
6use crate::types::*;
7use std::time::{Duration, SystemTime};
8
9pub trait Context {
10 fn get_current_time(&self) -> SystemTime {
11 hostcalls::get_current_time().unwrap()
12 }
13
14 fn get_property(&self, path: Vec<&str>) -> Option<Bytes> {
15 hostcalls::get_property(path).unwrap()
16 }
17
18 fn set_property(&self, path: Vec<&str>, value: Option<&[u8]>) {
19 hostcalls::set_property(path, value).unwrap()
20 }
21
22 fn get_shared_data(&self, key: &str) -> (Option<Bytes>, Option<u32>) {
23 hostcalls::get_shared_data(key).unwrap()
24 }
25
26 fn set_shared_data(
27 &self,
28 key: &str,
29 value: Option<&[u8]>,
30 cas: Option<u32>,
31 ) -> Result<(), Status> {
32 hostcalls::set_shared_data(key, value, cas)
33 }
34
35 fn remove_shared_data(&self, key: &str, cas: Option<u32>) -> Result<(), Status> {
36 hostcalls::set_shared_data(key, None, cas)
37 }
38
39 fn register_shared_queue(&self, name: &str) -> u32 {
40 hostcalls::register_shared_queue(name).unwrap()
41 }
42
43 fn resolve_shared_queue(&self, vm_id: &str, name: &str) -> Option<u32> {
44 hostcalls::resolve_shared_queue(vm_id, name).unwrap()
45 }
46
47 fn dequeue_shared_queue(&self, queue_id: u32) -> Result<Option<Bytes>, Status> {
48 hostcalls::dequeue_shared_queue(queue_id)
49 }
50
51 fn enqueue_shared_queue(&self, queue_id: u32, value: Option<&[u8]>) -> Result<(), Status> {
52 hostcalls::enqueue_shared_queue(queue_id, value)
53 }
54
55 fn dispatch_http_call(
56 &self,
57 upstream: &str,
58 headers: Vec<(&str, &str)>,
59 body: Option<&[u8]>,
60 trailers: Vec<(&str, &str)>,
61 timeout: Duration,
62 ) -> Result<u32, Status> {
63 hostcalls::dispatch_http_call(upstream, headers, body, trailers, timeout)
64 }
65
66 fn on_http_call_response(
67 &mut self,
68 _token_id: u32,
69 _num_headers: usize,
70 _body_size: usize,
71 _num_trailers: usize,
72 ) {
73 }
74
75 fn get_http_call_response_headers(&self) -> Vec<(String, String)> {
76 hostcalls::get_map(MapType::HttpCallResponseHeaders).unwrap()
77 }
78
79 fn get_http_call_response_headers_bytes(&self) -> Vec<(String, Bytes)> {
80 hostcalls::get_map_bytes(MapType::HttpCallResponseHeaders).unwrap()
81 }
82
83 fn get_http_call_response_header(&self, name: &str) -> Option<String> {
84 hostcalls::get_map_value(MapType::HttpCallResponseHeaders, name).unwrap()
85 }
86
87 fn get_http_call_response_header_bytes(&self, name: &str) -> Option<Bytes> {
88 hostcalls::get_map_value_bytes(MapType::HttpCallResponseHeaders, name).unwrap()
89 }
90
91 fn get_http_call_response_body(&self, start: usize, max_size: usize) -> Option<Bytes> {
92 hostcalls::get_buffer(BufferType::HttpCallResponseBody, start, max_size).unwrap()
93 }
94
95 fn get_http_call_response_trailers(&self) -> Vec<(String, String)> {
96 hostcalls::get_map(MapType::HttpCallResponseTrailers).unwrap()
97 }
98
99 fn get_http_call_response_trailers_bytes(&self) -> Vec<(String, Bytes)> {
100 hostcalls::get_map_bytes(MapType::HttpCallResponseTrailers).unwrap()
101 }
102
103 fn get_http_call_response_trailer(&self, name: &str) -> Option<String> {
104 hostcalls::get_map_value(MapType::HttpCallResponseTrailers, name).unwrap()
105 }
106
107 fn get_http_call_response_trailer_bytes(&self, name: &str) -> Option<Bytes> {
108 hostcalls::get_map_value_bytes(MapType::HttpCallResponseTrailers, name).unwrap()
109 }
110
111 fn dispatch_grpc_call(
112 &self,
113 upstream_name: &str,
114 service_name: &str,
115 method_name: &str,
116 initial_metadata: Vec<(&str, &[u8])>,
117 message: Option<&[u8]>,
118 timeout: Duration,
119 ) -> Result<u32, Status> {
120 hostcalls::dispatch_grpc_call(
121 upstream_name,
122 service_name,
123 method_name,
124 initial_metadata,
125 message,
126 timeout,
127 )
128 }
129
130 fn on_grpc_call_response(&mut self, _token_id: u32, _status_code: u32, _response_size: usize) {}
131
132 fn get_grpc_call_response_body(&self, start: usize, max_size: usize) -> Option<Bytes> {
133 hostcalls::get_buffer(BufferType::GrpcReceiveBuffer, start, max_size).unwrap()
134 }
135
136 fn cancel_grpc_call(&self, token_id: u32) {
137 hostcalls::cancel_grpc_call(token_id).unwrap()
138 }
139
140 fn open_grpc_stream(
141 &self,
142 cluster_name: &str,
143 service_name: &str,
144 method_name: &str,
145 initial_metadata: Vec<(&str, &[u8])>,
146 ) -> Result<u32, Status> {
147 hostcalls::open_grpc_stream(cluster_name, service_name, method_name, initial_metadata)
148 }
149
150 fn on_grpc_stream_initial_metadata(&mut self, _token_id: u32, _num_elements: u32) {}
151
152 fn get_grpc_stream_initial_metadata(&self) -> Vec<(String, Bytes)> {
153 hostcalls::get_map_bytes(MapType::GrpcReceiveInitialMetadata).unwrap()
154 }
155
156 fn get_grpc_stream_initial_metadata_value(&self, name: &str) -> Option<Bytes> {
157 hostcalls::get_map_value_bytes(MapType::GrpcReceiveInitialMetadata, name).unwrap()
158 }
159
160 fn send_grpc_stream_message(&self, token_id: u32, message: Option<&[u8]>, end_stream: bool) {
161 hostcalls::send_grpc_stream_message(token_id, message, end_stream).unwrap()
162 }
163
164 fn on_grpc_stream_message(&mut self, _token_id: u32, _message_size: usize) {}
165
166 fn get_grpc_stream_message(&mut self, start: usize, max_size: usize) -> Option<Bytes> {
167 hostcalls::get_buffer(BufferType::GrpcReceiveBuffer, start, max_size).unwrap()
168 }
169
170 fn on_grpc_stream_trailing_metadata(&mut self, _token_id: u32, _num_elements: u32) {}
171
172 fn get_grpc_stream_trailing_metadata(&self) -> Vec<(String, Bytes)> {
173 hostcalls::get_map_bytes(MapType::GrpcReceiveTrailingMetadata).unwrap()
174 }
175
176 fn get_grpc_stream_trailing_metadata_value(&self, name: &str) -> Option<Bytes> {
177 hostcalls::get_map_value_bytes(MapType::GrpcReceiveTrailingMetadata, name).unwrap()
178 }
179
180 fn cancel_grpc_stream(&self, token_id: u32) {
181 hostcalls::cancel_grpc_stream(token_id).unwrap()
182 }
183
184 fn close_grpc_stream(&self, token_id: u32) {
185 hostcalls::close_grpc_stream(token_id).unwrap()
186 }
187
188 fn on_grpc_stream_close(&mut self, _token_id: u32, _status_code: u32) {}
189
190 fn get_grpc_status(&self) -> (u32, Option<String>) {
191 hostcalls::get_grpc_status().unwrap()
192 }
193
194 fn on_foreign_function(&mut self, _function_id: u32, _arguments_size: usize) {}
195
196 fn call_foreign_function(
197 &self,
198 function_name: &str,
199 arguments: Option<&[u8]>,
200 ) -> Result<Option<Bytes>, Status> {
201 hostcalls::call_foreign_function(function_name, arguments)
202 }
203
204 fn on_done(&mut self) -> bool {
205 true
206 }
207
208 fn done(&self) {
209 hostcalls::done().unwrap()
210 }
211}
212
213pub trait RootContext: Context {
214 fn on_vm_start(&mut self, _vm_configuration_size: usize) -> bool {
215 true
216 }
217
218 fn get_vm_configuration(&self) -> Option<Bytes> {
219 hostcalls::get_buffer(BufferType::VmConfiguration, 0, usize::MAX).unwrap()
220 }
221
222 fn on_configure(&mut self, _plugin_configuration_size: usize) -> bool {
223 true
224 }
225
226 fn get_plugin_configuration(&self) -> Option<Bytes> {
227 hostcalls::get_buffer(BufferType::PluginConfiguration, 0, usize::MAX).unwrap()
228 }
229
230 fn set_tick_period(&self, period: Duration) {
231 hostcalls::set_tick_period(period).unwrap()
232 }
233
234 fn on_tick(&mut self) {}
235
236 fn on_queue_ready(&mut self, _queue_id: u32) {}
237
238 fn on_log(&mut self) {}
239
240 fn create_http_context(&self, _context_id: u32) -> Option<Box<dyn HttpContext>> {
241 None
242 }
243
244 fn create_stream_context(&self, _context_id: u32) -> Option<Box<dyn StreamContext>> {
245 None
246 }
247
248 fn get_type(&self) -> Option<ContextType> {
249 None
250 }
251}
252
253pub trait StreamContext: Context {
254 fn on_new_connection(&mut self) -> Action {
255 Action::Continue
256 }
257
258 fn on_downstream_data(&mut self, _data_size: usize, _end_of_stream: bool) -> Action {
259 Action::Continue
260 }
261
262 fn get_downstream_data(&self, start: usize, max_size: usize) -> Option<Bytes> {
263 hostcalls::get_buffer(BufferType::DownstreamData, start, max_size).unwrap()
264 }
265
266 fn set_downstream_data(&self, start: usize, size: usize, value: &[u8]) {
267 hostcalls::set_buffer(BufferType::DownstreamData, start, size, value).unwrap()
268 }
269
270 fn resume_downstream(&self) {
271 hostcalls::resume_downstream().unwrap()
272 }
273
274 fn close_downstream(&self) {
275 hostcalls::close_downstream().unwrap()
276 }
277
278 fn on_downstream_close(&mut self, _peer_type: PeerType) {}
279
280 fn on_upstream_data(&mut self, _data_size: usize, _end_of_stream: bool) -> Action {
281 Action::Continue
282 }
283
284 fn get_upstream_data(&self, start: usize, max_size: usize) -> Option<Bytes> {
285 hostcalls::get_buffer(BufferType::UpstreamData, start, max_size).unwrap()
286 }
287
288 fn set_upstream_data(&self, start: usize, size: usize, value: &[u8]) {
289 hostcalls::set_buffer(BufferType::UpstreamData, start, size, value).unwrap()
290 }
291
292 fn resume_upstream(&self) {
293 hostcalls::resume_upstream().unwrap()
294 }
295
296 fn close_upstream(&self) {
297 hostcalls::close_upstream().unwrap()
298 }
299
300 fn on_upstream_close(&mut self, _peer_type: PeerType) {}
301
302 fn on_log(&mut self) {}
303}
304
305pub trait HttpContext: Context {
306 fn on_http_request_headers(&mut self, _num_headers: usize, _end_of_stream: bool) -> Action {
307 Action::Continue
308 }
309
310 fn get_http_request_headers(&self) -> Vec<(String, String)> {
311 hostcalls::get_map(MapType::HttpRequestHeaders).unwrap()
312 }
313
314 fn get_http_request_headers_bytes(&self) -> Vec<(String, Bytes)> {
315 hostcalls::get_map_bytes(MapType::HttpRequestHeaders).unwrap()
316 }
317
318 fn set_http_request_headers(&self, headers: Vec<(&str, &str)>) {
319 hostcalls::set_map(MapType::HttpRequestHeaders, headers).unwrap()
320 }
321
322 fn set_http_request_headers_bytes(&self, headers: Vec<(&str, &[u8])>) {
323 hostcalls::set_map_bytes(MapType::HttpRequestHeaders, headers).unwrap()
324 }
325
326 fn get_http_request_header(&self, name: &str) -> Option<String> {
327 hostcalls::get_map_value(MapType::HttpRequestHeaders, name).unwrap()
328 }
329
330 fn get_http_request_header_bytes(&self, name: &str) -> Option<Bytes> {
331 hostcalls::get_map_value_bytes(MapType::HttpRequestHeaders, name).unwrap()
332 }
333
334 fn set_http_request_header(&self, name: &str, value: Option<&str>) {
335 hostcalls::set_map_value(MapType::HttpRequestHeaders, name, value).unwrap()
336 }
337
338 fn set_http_request_header_bytes(&self, name: &str, value: Option<&[u8]>) {
339 hostcalls::set_map_value_bytes(MapType::HttpRequestHeaders, name, value).unwrap()
340 }
341
342 fn add_http_request_header(&self, name: &str, value: &str) {
343 hostcalls::add_map_value(MapType::HttpRequestHeaders, name, value).unwrap()
344 }
345
346 fn add_http_request_header_bytes(&self, name: &str, value: &[u8]) {
347 hostcalls::add_map_value_bytes(MapType::HttpRequestHeaders, name, value).unwrap()
348 }
349
350 fn remove_http_request_header(&self, name: &str) {
351 hostcalls::remove_map_value(MapType::HttpRequestHeaders, name).unwrap()
352 }
353
354 fn on_http_request_body(&mut self, _body_size: usize, _end_of_stream: bool) -> Action {
355 Action::Continue
356 }
357
358 fn get_http_request_body(&self, start: usize, max_size: usize) -> Option<Bytes> {
359 hostcalls::get_buffer(BufferType::HttpRequestBody, start, max_size).unwrap()
360 }
361
362 fn set_http_request_body(&self, start: usize, size: usize, value: &[u8]) {
363 hostcalls::set_buffer(BufferType::HttpRequestBody, start, size, value).unwrap()
364 }
365
366 fn on_http_request_trailers(&mut self, _num_trailers: usize) -> Action {
367 Action::Continue
368 }
369
370 fn get_http_request_trailers(&self) -> Vec<(String, String)> {
371 hostcalls::get_map(MapType::HttpRequestTrailers).unwrap()
372 }
373
374 fn get_http_request_trailers_bytes(&self) -> Vec<(String, Bytes)> {
375 hostcalls::get_map_bytes(MapType::HttpRequestTrailers).unwrap()
376 }
377
378 fn set_http_request_trailers(&self, trailers: Vec<(&str, &str)>) {
379 hostcalls::set_map(MapType::HttpRequestTrailers, trailers).unwrap()
380 }
381
382 fn set_http_request_trailers_bytes(&self, trailers: Vec<(&str, &[u8])>) {
383 hostcalls::set_map_bytes(MapType::HttpRequestTrailers, trailers).unwrap()
384 }
385
386 fn get_http_request_trailer(&self, name: &str) -> Option<String> {
387 hostcalls::get_map_value(MapType::HttpRequestTrailers, name).unwrap()
388 }
389
390 fn get_http_request_trailer_bytes(&self, name: &str) -> Option<Bytes> {
391 hostcalls::get_map_value_bytes(MapType::HttpRequestTrailers, name).unwrap()
392 }
393
394 fn set_http_request_trailer(&self, name: &str, value: Option<&str>) {
395 hostcalls::set_map_value(MapType::HttpRequestTrailers, name, value).unwrap()
396 }
397
398 fn set_http_request_trailer_bytes(&self, name: &str, value: Option<&[u8]>) {
399 hostcalls::set_map_value_bytes(MapType::HttpRequestTrailers, name, value).unwrap()
400 }
401
402 fn add_http_request_trailer(&self, name: &str, value: &str) {
403 hostcalls::add_map_value(MapType::HttpRequestTrailers, name, value).unwrap()
404 }
405
406 fn add_http_request_trailer_bytes(&self, name: &str, value: &[u8]) {
407 hostcalls::add_map_value_bytes(MapType::HttpRequestTrailers, name, value).unwrap()
408 }
409
410 fn remove_http_request_trailer(&self, name: &str) {
411 hostcalls::remove_map_value(MapType::HttpRequestTrailers, name).unwrap()
412 }
413
414 fn resume_http_request(&self) {
415 hostcalls::resume_http_request().unwrap()
416 }
417
418 fn reset_http_request(&self) {
419 hostcalls::reset_http_request().unwrap()
420 }
421
422 fn on_http_response_headers(&mut self, _num_headers: usize, _end_of_stream: bool) -> Action {
423 Action::Continue
424 }
425
426 fn get_http_response_headers(&self) -> Vec<(String, String)> {
427 hostcalls::get_map(MapType::HttpResponseHeaders).unwrap()
428 }
429
430 fn get_http_response_headers_bytes(&self) -> Vec<(String, Bytes)> {
431 hostcalls::get_map_bytes(MapType::HttpResponseHeaders).unwrap()
432 }
433
434 fn set_http_response_headers(&self, headers: Vec<(&str, &str)>) {
435 hostcalls::set_map(MapType::HttpResponseHeaders, headers).unwrap()
436 }
437
438 fn set_http_response_headers_bytes(&self, headers: Vec<(&str, &[u8])>) {
439 hostcalls::set_map_bytes(MapType::HttpResponseHeaders, headers).unwrap()
440 }
441
442 fn get_http_response_header(&self, name: &str) -> Option<String> {
443 hostcalls::get_map_value(MapType::HttpResponseHeaders, name).unwrap()
444 }
445
446 fn get_http_response_header_bytes(&self, name: &str) -> Option<Bytes> {
447 hostcalls::get_map_value_bytes(MapType::HttpResponseHeaders, name).unwrap()
448 }
449
450 fn set_http_response_header(&self, name: &str, value: Option<&str>) {
451 hostcalls::set_map_value(MapType::HttpResponseHeaders, name, value).unwrap()
452 }
453
454 fn set_http_response_header_bytes(&self, name: &str, value: Option<&[u8]>) {
455 hostcalls::set_map_value_bytes(MapType::HttpResponseHeaders, name, value).unwrap()
456 }
457
458 fn add_http_response_header(&self, name: &str, value: &str) {
459 hostcalls::add_map_value(MapType::HttpResponseHeaders, name, value).unwrap()
460 }
461
462 fn add_http_response_header_bytes(&self, name: &str, value: &[u8]) {
463 hostcalls::add_map_value_bytes(MapType::HttpResponseHeaders, name, value).unwrap()
464 }
465
466 fn remove_http_response_header(&self, name: &str) {
467 hostcalls::remove_map_value(MapType::HttpResponseHeaders, name).unwrap()
468 }
469
470 fn on_http_response_body(&mut self, _body_size: usize, _end_of_stream: bool) -> Action {
471 Action::Continue
472 }
473
474 fn get_http_response_body(&self, start: usize, max_size: usize) -> Option<Bytes> {
475 hostcalls::get_buffer(BufferType::HttpResponseBody, start, max_size).unwrap()
476 }
477
478 fn set_http_response_body(&self, start: usize, size: usize, value: &[u8]) {
479 hostcalls::set_buffer(BufferType::HttpResponseBody, start, size, value).unwrap()
480 }
481
482 fn on_http_response_trailers(&mut self, _num_trailers: usize) -> Action {
483 Action::Continue
484 }
485
486 fn get_http_response_trailers(&self) -> Vec<(String, String)> {
487 hostcalls::get_map(MapType::HttpResponseTrailers).unwrap()
488 }
489
490 fn get_http_response_trailers_bytes(&self) -> Vec<(String, Bytes)> {
491 hostcalls::get_map_bytes(MapType::HttpResponseTrailers).unwrap()
492 }
493
494 fn set_http_response_trailers(&self, trailers: Vec<(&str, &str)>) {
495 hostcalls::set_map(MapType::HttpResponseTrailers, trailers).unwrap()
496 }
497
498 fn set_http_response_trailers_bytes(&self, trailers: Vec<(&str, &[u8])>) {
499 hostcalls::set_map_bytes(MapType::HttpResponseTrailers, trailers).unwrap()
500 }
501
502 fn get_http_response_trailer(&self, name: &str) -> Option<String> {
503 hostcalls::get_map_value(MapType::HttpResponseTrailers, name).unwrap()
504 }
505
506 fn get_http_response_trailer_bytes(&self, name: &str) -> Option<Bytes> {
507 hostcalls::get_map_value_bytes(MapType::HttpResponseTrailers, name).unwrap()
508 }
509
510 fn set_http_response_trailer(&self, name: &str, value: Option<&str>) {
511 hostcalls::set_map_value(MapType::HttpResponseTrailers, name, value).unwrap()
512 }
513
514 fn set_http_response_trailer_bytes(&self, name: &str, value: Option<&[u8]>) {
515 hostcalls::set_map_value_bytes(MapType::HttpResponseTrailers, name, value).unwrap()
516 }
517
518 fn add_http_response_trailer(&self, name: &str, value: &str) {
519 hostcalls::add_map_value(MapType::HttpResponseTrailers, name, value).unwrap()
520 }
521
522 fn add_http_response_trailer_bytes(&self, name: &str, value: &[u8]) {
523 hostcalls::add_map_value_bytes(MapType::HttpResponseTrailers, name, value).unwrap()
524 }
525
526 fn remove_http_response_trailer(&self, name: &str) {
527 hostcalls::remove_map_value(MapType::HttpResponseTrailers, name).unwrap()
528 }
529
530 fn resume_http_response(&self) {
531 hostcalls::resume_http_response().unwrap()
532 }
533
534 fn reset_http_response(&self) {
535 hostcalls::reset_http_response().unwrap()
536 }
537
538 fn send_http_response(
539 &self,
540 status_code: u32,
541 headers: Vec<(&str, &str)>,
542 body: Option<&[u8]>,
543 ) {
544 hostcalls::send_http_response(status_code, headers, body).unwrap()
545 }
546
547 fn send_grpc_response(
548 &self,
549 grpc_status: GrpcStatusCode,
550 grpc_status_message: Option<&str>,
551 custom_metadata: Vec<(&str, &[u8])>,
552 ) {
553 hostcalls::send_grpc_response(grpc_status, grpc_status_message, custom_metadata).unwrap()
554 }
555
556 fn on_log(&mut self) {}
557}