1use crate::hostcalls;
16use crate::types::*;
17use std::time::{Duration, SystemTime};
18
19use crate::error::Result;
20
21pub trait Context {
22 fn get_current_time(&self) -> SystemTime {
23 hostcalls::get_current_time().unwrap()
24 }
25
26 fn get_property(&self, path: Vec<&str>) -> Option<ByteString> {
27 hostcalls::get_property(&path).unwrap()
28 }
29
30 fn set_property(&self, path: Vec<&str>, value: Option<&[u8]>) {
31 hostcalls::set_property(&path, value).unwrap()
32 }
33
34 fn get_shared_data(&self, key: &str) -> (Option<ByteString>, Option<u32>) {
35 hostcalls::get_shared_data(key).unwrap()
36 }
37
38 fn set_shared_data(&self, key: &str, value: Option<&[u8]>, cas: Option<u32>) -> Result<()> {
39 hostcalls::set_shared_data(key, value, cas)
40 }
41
42 fn register_shared_queue(&self, name: &str) -> u32 {
43 hostcalls::register_shared_queue(name).unwrap()
44 }
45
46 fn resolve_shared_queue(&self, vm_id: &str, name: &str) -> Option<u32> {
47 hostcalls::resolve_shared_queue(vm_id, name).unwrap()
48 }
49
50 fn dequeue_shared_queue(&self, queue_id: u32) -> Result<Option<ByteString>> {
51 hostcalls::dequeue_shared_queue(queue_id)
52 }
53
54 fn enqueue_shared_queue(&self, queue_id: u32, value: Option<&[u8]>) -> Result<()> {
55 hostcalls::enqueue_shared_queue(queue_id, value)
56 }
57
58 fn dispatch_http_call(
59 &self,
60 upstream: &str,
61 headers: Vec<(&str, &str)>,
62 body: Option<&[u8]>,
63 trailers: Vec<(&str, &str)>,
64 timeout: Duration,
65 ) -> Result<u32> {
66 hostcalls::dispatch_http_call(upstream, &headers, body, &trailers, timeout)
67 }
68
69 fn on_http_call_response(
70 &mut self,
71 _token_id: u32,
72 _num_headers: usize,
73 _body_size: usize,
74 _num_trailers: usize,
75 ) {
76 }
77
78 fn get_http_call_response_headers(&self) -> Vec<(ByteString, ByteString)> {
79 hostcalls::get_map(MapType::HttpCallResponseHeaders).unwrap()
80 }
81
82 fn get_http_call_response_body(&self, start: usize, max_size: usize) -> Option<ByteString> {
83 hostcalls::get_buffer(BufferType::HttpCallResponseBody, start, max_size).unwrap()
84 }
85
86 fn get_http_call_response_trailers(&self) -> Vec<(ByteString, ByteString)> {
87 hostcalls::get_map(MapType::HttpCallResponseTrailers).unwrap()
88 }
89
90 fn on_done(&mut self) -> bool {
91 true
92 }
93
94 fn done(&self) {
95 hostcalls::done().unwrap()
96 }
97}
98
99pub trait RootContext: Context {
100 fn on_vm_start(&mut self, _vm_configuration_size: usize) -> bool {
101 true
102 }
103
104 fn on_configure(&mut self, _plugin_configuration_size: usize) -> bool {
105 true
106 }
107
108 fn set_tick_period(&self, period: Duration) {
109 hostcalls::set_tick_period(period).unwrap()
110 }
111
112 fn on_tick(&mut self) {}
113
114 fn on_queue_ready(&mut self, _queue_id: u32) {}
115
116 fn on_log(&mut self) {}
117
118 fn create_http_context(&self, _context_id: u32) -> Option<Box<dyn HttpContext>> {
119 None
120 }
121
122 fn create_stream_context(&self, _context_id: u32) -> Option<Box<dyn StreamContext>> {
123 None
124 }
125
126 fn get_type(&self) -> Option<ContextType> {
127 None
128 }
129}
130
131pub trait StreamContext: Context {
132 fn on_new_connection(&mut self) -> Action {
133 Action::Continue
134 }
135
136 fn on_downstream_data(&mut self, _data_size: usize, _end_of_stream: bool) -> Action {
137 Action::Continue
138 }
139
140 fn get_downstream_data(&self, start: usize, max_size: usize) -> Option<ByteString> {
141 hostcalls::get_buffer(BufferType::DownstreamData, start, max_size).unwrap()
142 }
143
144 fn set_downstream_data(&self, start: usize, size: usize, value: &[u8]) {
145 hostcalls::set_buffer(BufferType::DownstreamData, start, size, value).unwrap()
146 }
147
148 fn on_downstream_close(&mut self, _peer_type: PeerType) {}
149
150 fn on_upstream_data(&mut self, _data_size: usize, _end_of_stream: bool) -> Action {
151 Action::Continue
152 }
153
154 fn get_upstream_data(&self, start: usize, max_size: usize) -> Option<ByteString> {
155 hostcalls::get_buffer(BufferType::UpstreamData, start, max_size).unwrap()
156 }
157
158 fn set_upstream_data(&self, start: usize, size: usize, value: &[u8]) {
159 hostcalls::set_buffer(BufferType::UpstreamData, start, size, value).unwrap()
160 }
161
162 fn on_upstream_close(&mut self, _peer_type: PeerType) {}
163
164 fn on_log(&mut self) {}
165}
166
167pub trait HttpContext: Context {
168 fn on_http_request_headers(&mut self, _num_headers: usize, _end_of_stream: bool) -> Action {
169 Action::Continue
170 }
171
172 fn get_http_request_headers(&self) -> Vec<(ByteString, ByteString)> {
173 hostcalls::get_map(MapType::HttpRequestHeaders).unwrap()
174 }
175
176 fn set_http_request_headers(&self, headers: Vec<(&str, &str)>) {
177 hostcalls::set_map(MapType::HttpRequestHeaders, &headers).unwrap()
178 }
179
180 fn get_http_request_header(&self, name: &str) -> Option<ByteString> {
181 hostcalls::get_map_value(MapType::HttpRequestHeaders, &name).unwrap()
182 }
183
184 fn set_http_request_header(&self, name: &str, value: Option<&str>) {
185 hostcalls::set_map_value(MapType::HttpRequestHeaders, &name, value).unwrap()
186 }
187
188 fn add_http_request_header(&self, name: &str, value: &str) {
189 hostcalls::add_map_value(MapType::HttpRequestHeaders, &name, value).unwrap()
190 }
191
192 fn on_http_request_body(&mut self, _body_size: usize, _end_of_stream: bool) -> Action {
193 Action::Continue
194 }
195
196 fn get_http_request_body(&self, start: usize, max_size: usize) -> Option<ByteString> {
197 hostcalls::get_buffer(BufferType::HttpRequestBody, start, max_size).unwrap()
198 }
199
200 fn set_http_request_body(&self, start: usize, size: usize, value: &[u8]) {
201 hostcalls::set_buffer(BufferType::HttpRequestBody, start, size, value).unwrap()
202 }
203
204 fn on_http_request_trailers(&mut self, _num_trailers: usize) -> Action {
205 Action::Continue
206 }
207
208 fn get_http_request_trailers(&self) -> Vec<(ByteString, ByteString)> {
209 hostcalls::get_map(MapType::HttpRequestTrailers).unwrap()
210 }
211
212 fn set_http_request_trailers(&self, trailers: Vec<(&str, &str)>) {
213 hostcalls::set_map(MapType::HttpRequestTrailers, &trailers).unwrap()
214 }
215
216 fn get_http_request_trailer(&self, name: &str) -> Option<ByteString> {
217 hostcalls::get_map_value(MapType::HttpRequestTrailers, &name).unwrap()
218 }
219
220 fn set_http_request_trailer(&self, name: &str, value: Option<&str>) {
221 hostcalls::set_map_value(MapType::HttpRequestTrailers, &name, value).unwrap()
222 }
223
224 fn add_http_request_trailer(&self, name: &str, value: &str) {
225 hostcalls::add_map_value(MapType::HttpRequestTrailers, &name, value).unwrap()
226 }
227
228 fn resume_http_request(&self) {
229 hostcalls::continue_stream(StreamType::Request).unwrap()
230 }
231
232 fn on_http_response_headers(&mut self, _num_headers: usize, _end_of_stream: bool) -> Action {
233 Action::Continue
234 }
235
236 fn get_http_response_headers(&self) -> Vec<(ByteString, ByteString)> {
237 hostcalls::get_map(MapType::HttpResponseHeaders).unwrap()
238 }
239
240 fn set_http_response_headers(&self, headers: Vec<(&str, &str)>) {
241 hostcalls::set_map(MapType::HttpResponseHeaders, &headers).unwrap()
242 }
243
244 fn get_http_response_header(&self, name: &str) -> Option<ByteString> {
245 hostcalls::get_map_value(MapType::HttpResponseHeaders, &name).unwrap()
246 }
247
248 fn set_http_response_header(&self, name: &str, value: Option<&str>) {
249 hostcalls::set_map_value(MapType::HttpResponseHeaders, &name, value).unwrap()
250 }
251
252 fn add_http_response_header(&self, name: &str, value: &str) {
253 hostcalls::add_map_value(MapType::HttpResponseHeaders, &name, value).unwrap()
254 }
255
256 fn on_http_response_body(&mut self, _body_size: usize, _end_of_stream: bool) -> Action {
257 Action::Continue
258 }
259
260 fn get_http_response_body(&self, start: usize, max_size: usize) -> Option<ByteString> {
261 hostcalls::get_buffer(BufferType::HttpResponseBody, start, max_size).unwrap()
262 }
263
264 fn set_http_response_body(&self, start: usize, size: usize, value: &[u8]) {
265 hostcalls::set_buffer(BufferType::HttpResponseBody, start, size, value).unwrap()
266 }
267
268 fn on_http_response_trailers(&mut self, _num_trailers: usize) -> Action {
269 Action::Continue
270 }
271
272 fn get_http_response_trailers(&self) -> Vec<(ByteString, ByteString)> {
273 hostcalls::get_map(MapType::HttpResponseTrailers).unwrap()
274 }
275
276 fn set_http_response_trailers(&self, headers: Vec<(&str, &str)>) {
277 hostcalls::set_map(MapType::HttpResponseTrailers, &headers).unwrap()
278 }
279
280 fn get_http_response_trailer(&self, name: &str) -> Option<ByteString> {
281 hostcalls::get_map_value(MapType::HttpResponseTrailers, &name).unwrap()
282 }
283
284 fn set_http_response_trailer(&self, name: &str, value: Option<&str>) {
285 hostcalls::set_map_value(MapType::HttpResponseTrailers, &name, value).unwrap()
286 }
287
288 fn add_http_response_trailer(&self, name: &str, value: &str) {
289 hostcalls::add_map_value(MapType::HttpResponseTrailers, &name, value).unwrap()
290 }
291
292 fn resume_http_response(&self) {
293 hostcalls::continue_stream(StreamType::Response).unwrap()
294 }
295
296 fn send_http_response(
297 &self,
298 status_code: u32,
299 headers: Vec<(&str, &str)>,
300 body: Option<&[u8]>,
301 ) {
302 hostcalls::send_http_response(status_code, &headers, body).unwrap()
303 }
304
305 fn on_log(&mut self) {}
306}