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
// Copyright 2020 Tetrate
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

extern crate std;

use std::prelude::v1::*;

use proxy_wasm::types::Bytes;

use crate::extension::Result;
use crate::host;
use crate::host::services::clients::http as http_client;

pub mod context;
pub mod ops;
pub use context::LoggerContext;

pub trait Logger {
    fn on_configure(
        &mut self,
        _configuration_size: usize,
        _logger_ops: &dyn ConfigureOps,
    ) -> Result<bool> {
        Ok(true)
    }

    fn on_log(&mut self, _logger_ops: &dyn LogOps) -> Result<()> {
        Ok(())
    }

    // Http Client callbacks

    fn on_http_call_response(
        &mut self,
        _request: http_client::RequestHandle,
        _num_headers: usize,
        _body_size: usize,
        _num_trailers: usize,
        _http_client_ops: &dyn http_client::ResponseOps,
    ) -> Result<()> {
        Ok(())
    }
}

pub trait ConfigureOps {
    fn get_configuration(&self) -> host::Result<Option<Bytes>>;
}

pub trait LogOps {
    fn get_request_headers(&self) -> host::Result<Vec<(String, String)>>;

    fn get_request_header(&self, name: &str) -> host::Result<Option<String>>;

    fn get_response_headers(&self) -> host::Result<Vec<(String, String)>>;

    fn get_response_header(&self, name: &str) -> host::Result<Option<String>>;

    fn get_response_trailers(&self) -> host::Result<Vec<(String, String)>>;

    fn get_response_trailer(&self, name: &str) -> host::Result<Option<String>>;

    fn get_property(&self, path: Vec<&str>) -> host::Result<Option<Bytes>>;
}

pub trait Ops: ConfigureOps + LogOps {
    fn as_configure_ops(&self) -> &dyn ConfigureOps;

    fn as_log_ops(&self) -> &dyn LogOps;
}

impl<T> Ops for T
where
    T: ConfigureOps + LogOps,
{
    fn as_configure_ops(&self) -> &dyn ConfigureOps {
        self
    }

    fn as_log_ops(&self) -> &dyn LogOps {
        self
    }
}