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