proxy_wasm/
traits.rs

1// Copyright 2020 Google LLC
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//      http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15use 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 register_shared_queue(&self, name: &str) -> u32 {
46        hostcalls::register_shared_queue(name).unwrap()
47    }
48
49    fn resolve_shared_queue(&self, vm_id: &str, name: &str) -> Option<u32> {
50        hostcalls::resolve_shared_queue(vm_id, name).unwrap()
51    }
52
53    fn dequeue_shared_queue(&self, queue_id: u32) -> Result<Option<Bytes>, Status> {
54        hostcalls::dequeue_shared_queue(queue_id)
55    }
56
57    fn enqueue_shared_queue(&self, queue_id: u32, value: Option<&[u8]>) -> Result<(), Status> {
58        hostcalls::enqueue_shared_queue(queue_id, value)
59    }
60
61    fn dispatch_http_call(
62        &self,
63        upstream: &str,
64        headers: Vec<(&str, &str)>,
65        body: Option<&[u8]>,
66        trailers: Vec<(&str, &str)>,
67        timeout: Duration,
68    ) -> Result<u32, Status> {
69        hostcalls::dispatch_http_call(upstream, headers, body, trailers, timeout)
70    }
71
72    fn on_http_call_response(
73        &mut self,
74        _token_id: u32,
75        _num_headers: usize,
76        _body_size: usize,
77        _num_trailers: usize,
78    ) {
79    }
80
81    fn get_http_call_response_headers(&self) -> Vec<(String, String)> {
82        hostcalls::get_map(MapType::HttpCallResponseHeaders).unwrap()
83    }
84
85    fn get_http_call_response_headers_bytes(&self) -> Vec<(String, Bytes)> {
86        hostcalls::get_map_bytes(MapType::HttpCallResponseHeaders).unwrap()
87    }
88
89    fn get_http_call_response_header(&self, name: &str) -> Option<String> {
90        hostcalls::get_map_value(MapType::HttpCallResponseHeaders, name).unwrap()
91    }
92
93    fn get_http_call_response_header_bytes(&self, name: &str) -> Option<Bytes> {
94        hostcalls::get_map_value_bytes(MapType::HttpCallResponseHeaders, name).unwrap()
95    }
96
97    fn get_http_call_response_body(&self, start: usize, max_size: usize) -> Option<Bytes> {
98        hostcalls::get_buffer(BufferType::HttpCallResponseBody, start, max_size).unwrap()
99    }
100
101    fn get_http_call_response_trailers(&self) -> Vec<(String, String)> {
102        hostcalls::get_map(MapType::HttpCallResponseTrailers).unwrap()
103    }
104
105    fn get_http_call_response_trailers_bytes(&self) -> Vec<(String, Bytes)> {
106        hostcalls::get_map_bytes(MapType::HttpCallResponseTrailers).unwrap()
107    }
108
109    fn get_http_call_response_trailer(&self, name: &str) -> Option<String> {
110        hostcalls::get_map_value(MapType::HttpCallResponseTrailers, name).unwrap()
111    }
112
113    fn get_http_call_response_trailer_bytes(&self, name: &str) -> Option<Bytes> {
114        hostcalls::get_map_value_bytes(MapType::HttpCallResponseTrailers, name).unwrap()
115    }
116
117    fn dispatch_grpc_call(
118        &self,
119        upstream_name: &str,
120        service_name: &str,
121        method_name: &str,
122        initial_metadata: Vec<(&str, &[u8])>,
123        message: Option<&[u8]>,
124        timeout: Duration,
125    ) -> Result<u32, Status> {
126        hostcalls::dispatch_grpc_call(
127            upstream_name,
128            service_name,
129            method_name,
130            initial_metadata,
131            message,
132            timeout,
133        )
134    }
135
136    fn on_grpc_call_response(&mut self, _token_id: u32, _status_code: u32, _response_size: usize) {}
137
138    fn get_grpc_call_response_body(&self, start: usize, max_size: usize) -> Option<Bytes> {
139        hostcalls::get_buffer(BufferType::GrpcReceiveBuffer, start, max_size).unwrap()
140    }
141
142    fn cancel_grpc_call(&self, token_id: u32) {
143        hostcalls::cancel_grpc_call(token_id).unwrap()
144    }
145
146    fn open_grpc_stream(
147        &self,
148        cluster_name: &str,
149        service_name: &str,
150        method_name: &str,
151        initial_metadata: Vec<(&str, &[u8])>,
152    ) -> Result<u32, Status> {
153        hostcalls::open_grpc_stream(cluster_name, service_name, method_name, initial_metadata)
154    }
155
156    fn on_grpc_stream_initial_metadata(&mut self, _token_id: u32, _num_elements: u32) {}
157
158    fn get_grpc_stream_initial_metadata(&self) -> Vec<(String, Bytes)> {
159        hostcalls::get_map_bytes(MapType::GrpcReceiveInitialMetadata).unwrap()
160    }
161
162    fn get_grpc_stream_initial_metadata_value(&self, name: &str) -> Option<Bytes> {
163        hostcalls::get_map_value_bytes(MapType::GrpcReceiveInitialMetadata, name).unwrap()
164    }
165
166    fn send_grpc_stream_message(&self, token_id: u32, message: Option<&[u8]>, end_stream: bool) {
167        hostcalls::send_grpc_stream_message(token_id, message, end_stream).unwrap()
168    }
169
170    fn on_grpc_stream_message(&mut self, _token_id: u32, _message_size: usize) {}
171
172    fn get_grpc_stream_message(&mut self, start: usize, max_size: usize) -> Option<Bytes> {
173        hostcalls::get_buffer(BufferType::GrpcReceiveBuffer, start, max_size).unwrap()
174    }
175
176    fn on_grpc_stream_trailing_metadata(&mut self, _token_id: u32, _num_elements: u32) {}
177
178    fn get_grpc_stream_trailing_metadata(&self) -> Vec<(String, Bytes)> {
179        hostcalls::get_map_bytes(MapType::GrpcReceiveTrailingMetadata).unwrap()
180    }
181
182    fn get_grpc_stream_trailing_metadata_value(&self, name: &str) -> Option<Bytes> {
183        hostcalls::get_map_value_bytes(MapType::GrpcReceiveTrailingMetadata, name).unwrap()
184    }
185
186    fn cancel_grpc_stream(&self, token_id: u32) {
187        hostcalls::cancel_grpc_stream(token_id).unwrap()
188    }
189
190    fn close_grpc_stream(&self, token_id: u32) {
191        hostcalls::close_grpc_stream(token_id).unwrap()
192    }
193
194    fn on_grpc_stream_close(&mut self, _token_id: u32, _status_code: u32) {}
195
196    fn get_grpc_status(&self) -> (u32, Option<String>) {
197        hostcalls::get_grpc_status().unwrap()
198    }
199
200    fn call_foreign_function(
201        &self,
202        function_name: &str,
203        arguments: Option<&[u8]>,
204    ) -> Result<Option<Bytes>, Status> {
205        hostcalls::call_foreign_function(function_name, arguments)
206    }
207
208    fn on_done(&mut self) -> bool {
209        true
210    }
211
212    fn done(&self) {
213        hostcalls::done().unwrap()
214    }
215}
216
217pub trait RootContext: Context {
218    fn on_vm_start(&mut self, _vm_configuration_size: usize) -> bool {
219        true
220    }
221
222    fn get_vm_configuration(&self) -> Option<Bytes> {
223        hostcalls::get_buffer(BufferType::VmConfiguration, 0, usize::MAX).unwrap()
224    }
225
226    fn on_configure(&mut self, _plugin_configuration_size: usize) -> bool {
227        true
228    }
229
230    fn get_plugin_configuration(&self) -> Option<Bytes> {
231        hostcalls::get_buffer(BufferType::PluginConfiguration, 0, usize::MAX).unwrap()
232    }
233
234    fn set_tick_period(&self, period: Duration) {
235        hostcalls::set_tick_period(period).unwrap()
236    }
237
238    fn on_tick(&mut self) {}
239
240    fn on_queue_ready(&mut self, _queue_id: u32) {}
241
242    fn on_log(&mut self) {}
243
244    fn create_http_context(&self, _context_id: u32) -> Option<Box<dyn HttpContext>> {
245        None
246    }
247
248    fn create_stream_context(&self, _context_id: u32) -> Option<Box<dyn StreamContext>> {
249        None
250    }
251
252    fn get_type(&self) -> Option<ContextType> {
253        None
254    }
255}
256
257pub trait StreamContext: Context {
258    fn on_new_connection(&mut self) -> Action {
259        Action::Continue
260    }
261
262    fn on_downstream_data(&mut self, _data_size: usize, _end_of_stream: bool) -> Action {
263        Action::Continue
264    }
265
266    fn get_downstream_data(&self, start: usize, max_size: usize) -> Option<Bytes> {
267        hostcalls::get_buffer(BufferType::DownstreamData, start, max_size).unwrap()
268    }
269
270    fn set_downstream_data(&self, start: usize, size: usize, value: &[u8]) {
271        hostcalls::set_buffer(BufferType::DownstreamData, start, size, value).unwrap()
272    }
273
274    fn resume_downstream(&self) {
275        hostcalls::resume_downstream().unwrap()
276    }
277
278    fn close_downstream(&self) {
279        hostcalls::close_downstream().unwrap()
280    }
281
282    fn on_downstream_close(&mut self, _peer_type: PeerType) {}
283
284    fn on_upstream_data(&mut self, _data_size: usize, _end_of_stream: bool) -> Action {
285        Action::Continue
286    }
287
288    fn get_upstream_data(&self, start: usize, max_size: usize) -> Option<Bytes> {
289        hostcalls::get_buffer(BufferType::UpstreamData, start, max_size).unwrap()
290    }
291
292    fn set_upstream_data(&self, start: usize, size: usize, value: &[u8]) {
293        hostcalls::set_buffer(BufferType::UpstreamData, start, size, value).unwrap()
294    }
295
296    fn resume_upstream(&self) {
297        hostcalls::resume_upstream().unwrap()
298    }
299
300    fn close_upstream(&self) {
301        hostcalls::close_upstream().unwrap()
302    }
303
304    fn on_upstream_close(&mut self, _peer_type: PeerType) {}
305
306    fn on_log(&mut self) {}
307}
308
309pub trait HttpContext: Context {
310    fn on_http_request_headers(&mut self, _num_headers: usize, _end_of_stream: bool) -> Action {
311        Action::Continue
312    }
313
314    fn get_http_request_headers(&self) -> Vec<(String, String)> {
315        hostcalls::get_map(MapType::HttpRequestHeaders).unwrap()
316    }
317
318    fn get_http_request_headers_bytes(&self) -> Vec<(String, Bytes)> {
319        hostcalls::get_map_bytes(MapType::HttpRequestHeaders).unwrap()
320    }
321
322    fn set_http_request_headers(&self, headers: Vec<(&str, &str)>) {
323        hostcalls::set_map(MapType::HttpRequestHeaders, headers).unwrap()
324    }
325
326    fn set_http_request_headers_bytes(&self, headers: Vec<(&str, &[u8])>) {
327        hostcalls::set_map_bytes(MapType::HttpRequestHeaders, headers).unwrap()
328    }
329
330    fn get_http_request_header(&self, name: &str) -> Option<String> {
331        hostcalls::get_map_value(MapType::HttpRequestHeaders, name).unwrap()
332    }
333
334    fn get_http_request_header_bytes(&self, name: &str) -> Option<Bytes> {
335        hostcalls::get_map_value_bytes(MapType::HttpRequestHeaders, name).unwrap()
336    }
337
338    fn set_http_request_header(&self, name: &str, value: Option<&str>) {
339        hostcalls::set_map_value(MapType::HttpRequestHeaders, name, value).unwrap()
340    }
341
342    fn set_http_request_header_bytes(&self, name: &str, value: Option<&[u8]>) {
343        hostcalls::set_map_value_bytes(MapType::HttpRequestHeaders, name, value).unwrap()
344    }
345
346    fn add_http_request_header(&self, name: &str, value: &str) {
347        hostcalls::add_map_value(MapType::HttpRequestHeaders, name, value).unwrap()
348    }
349
350    fn add_http_request_header_bytes(&self, name: &str, value: &[u8]) {
351        hostcalls::add_map_value_bytes(MapType::HttpRequestHeaders, name, value).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 resume_http_request(&self) {
411        hostcalls::resume_http_request().unwrap()
412    }
413
414    fn reset_http_request(&self) {
415        hostcalls::reset_http_request().unwrap()
416    }
417
418    fn on_http_response_headers(&mut self, _num_headers: usize, _end_of_stream: bool) -> Action {
419        Action::Continue
420    }
421
422    fn get_http_response_headers(&self) -> Vec<(String, String)> {
423        hostcalls::get_map(MapType::HttpResponseHeaders).unwrap()
424    }
425
426    fn get_http_response_headers_bytes(&self) -> Vec<(String, Bytes)> {
427        hostcalls::get_map_bytes(MapType::HttpResponseHeaders).unwrap()
428    }
429
430    fn set_http_response_headers(&self, headers: Vec<(&str, &str)>) {
431        hostcalls::set_map(MapType::HttpResponseHeaders, headers).unwrap()
432    }
433
434    fn set_http_response_headers_bytes(&self, headers: Vec<(&str, &[u8])>) {
435        hostcalls::set_map_bytes(MapType::HttpResponseHeaders, headers).unwrap()
436    }
437
438    fn get_http_response_header(&self, name: &str) -> Option<String> {
439        hostcalls::get_map_value(MapType::HttpResponseHeaders, name).unwrap()
440    }
441
442    fn get_http_response_header_bytes(&self, name: &str) -> Option<Bytes> {
443        hostcalls::get_map_value_bytes(MapType::HttpResponseHeaders, name).unwrap()
444    }
445
446    fn set_http_response_header(&self, name: &str, value: Option<&str>) {
447        hostcalls::set_map_value(MapType::HttpResponseHeaders, name, value).unwrap()
448    }
449
450    fn set_http_response_header_bytes(&self, name: &str, value: Option<&[u8]>) {
451        hostcalls::set_map_value_bytes(MapType::HttpResponseHeaders, name, value).unwrap()
452    }
453
454    fn add_http_response_header(&self, name: &str, value: &str) {
455        hostcalls::add_map_value(MapType::HttpResponseHeaders, name, value).unwrap()
456    }
457
458    fn add_http_response_header_bytes(&self, name: &str, value: &[u8]) {
459        hostcalls::add_map_value_bytes(MapType::HttpResponseHeaders, name, value).unwrap()
460    }
461
462    fn on_http_response_body(&mut self, _body_size: usize, _end_of_stream: bool) -> Action {
463        Action::Continue
464    }
465
466    fn get_http_response_body(&self, start: usize, max_size: usize) -> Option<Bytes> {
467        hostcalls::get_buffer(BufferType::HttpResponseBody, start, max_size).unwrap()
468    }
469
470    fn set_http_response_body(&self, start: usize, size: usize, value: &[u8]) {
471        hostcalls::set_buffer(BufferType::HttpResponseBody, start, size, value).unwrap()
472    }
473
474    fn on_http_response_trailers(&mut self, _num_trailers: usize) -> Action {
475        Action::Continue
476    }
477
478    fn get_http_response_trailers(&self) -> Vec<(String, String)> {
479        hostcalls::get_map(MapType::HttpResponseTrailers).unwrap()
480    }
481
482    fn get_http_response_trailers_bytes(&self) -> Vec<(String, Bytes)> {
483        hostcalls::get_map_bytes(MapType::HttpResponseTrailers).unwrap()
484    }
485
486    fn set_http_response_trailers(&self, trailers: Vec<(&str, &str)>) {
487        hostcalls::set_map(MapType::HttpResponseTrailers, trailers).unwrap()
488    }
489
490    fn set_http_response_trailers_bytes(&self, trailers: Vec<(&str, &[u8])>) {
491        hostcalls::set_map_bytes(MapType::HttpResponseTrailers, trailers).unwrap()
492    }
493
494    fn get_http_response_trailer(&self, name: &str) -> Option<String> {
495        hostcalls::get_map_value(MapType::HttpResponseTrailers, name).unwrap()
496    }
497
498    fn get_http_response_trailer_bytes(&self, name: &str) -> Option<Bytes> {
499        hostcalls::get_map_value_bytes(MapType::HttpResponseTrailers, name).unwrap()
500    }
501
502    fn set_http_response_trailer(&self, name: &str, value: Option<&str>) {
503        hostcalls::set_map_value(MapType::HttpResponseTrailers, name, value).unwrap()
504    }
505
506    fn set_http_response_trailer_bytes(&self, name: &str, value: Option<&[u8]>) {
507        hostcalls::set_map_value_bytes(MapType::HttpResponseTrailers, name, value).unwrap()
508    }
509
510    fn add_http_response_trailer(&self, name: &str, value: &str) {
511        hostcalls::add_map_value(MapType::HttpResponseTrailers, name, value).unwrap()
512    }
513
514    fn add_http_response_trailer_bytes(&self, name: &str, value: &[u8]) {
515        hostcalls::add_map_value_bytes(MapType::HttpResponseTrailers, name, value).unwrap()
516    }
517
518    fn resume_http_response(&self) {
519        hostcalls::resume_http_response().unwrap()
520    }
521
522    fn reset_http_response(&self) {
523        hostcalls::reset_http_response().unwrap()
524    }
525
526    fn send_http_response(
527        &self,
528        status_code: u32,
529        headers: Vec<(&str, &str)>,
530        body: Option<&[u8]>,
531    ) {
532        hostcalls::send_http_response(status_code, headers, body).unwrap()
533    }
534
535    fn send_grpc_response(
536        &self,
537        grpc_status: GrpcStatusCode,
538        grpc_status_message: Option<&str>,
539        custom_metadata: Vec<(&str, &[u8])>,
540    ) {
541        hostcalls::send_grpc_response(grpc_status, grpc_status_message, custom_metadata).unwrap()
542    }
543
544    fn on_log(&mut self) {}
545}