aiway_plugin/
wasm_types.rs1use crate::http::{self, HeaderName, HeaderValue};
7use serde::{Deserialize, Serialize};
8use std::str::FromStr;
9
10pub const HOOK_ON_REQUEST: i32 = 1;
12pub const HOOK_ON_REQUEST_BODY: i32 = 2;
13pub const HOOK_ON_RESPONSE: i32 = 3;
14pub const HOOK_ON_RESPONSE_BODY: i32 = 4;
15pub const HOOK_ON_LOGGING: i32 = 5;
16
17#[derive(Serialize, Deserialize)]
19pub struct WasmInput {
20 pub config: String,
22 pub head: Option<WasmHead>,
24 pub body: Option<Vec<u8>>,
26 pub request_id: Option<String>,
28 pub request_ts: Option<i64>,
30}
31
32#[derive(Serialize, Deserialize)]
34pub struct WasmHead {
35 pub method: Option<String>,
37 pub uri: Option<String>,
39 pub status: Option<u16>,
41 pub headers: Vec<(String, String)>,
43}
44
45impl WasmHead {
46 pub fn from_request_parts(parts: &http::request::Parts) -> Self {
48 let headers = parts
49 .headers
50 .iter()
51 .filter_map(|(k, v)| v.to_str().ok().map(|v| (k.to_string(), v.to_string())))
52 .collect();
53
54 Self {
55 method: Some(parts.method.to_string()),
56 uri: Some(parts.uri.to_string()),
57 status: None,
58 headers,
59 }
60 }
61
62 pub fn from_response_parts(parts: &http::response::Parts) -> Self {
64 let headers = parts
65 .headers
66 .iter()
67 .filter_map(|(k, v)| v.to_str().ok().map(|v| (k.to_string(), v.to_string())))
68 .collect();
69
70 Self {
71 method: None,
72 uri: None,
73 status: Some(parts.status.as_u16()),
74 headers,
75 }
76 }
77
78 pub fn apply_to_request_parts(&self, parts: &mut http::request::Parts) {
80 if let Some(ref method) = self.method
81 && let Ok(m) = method.parse()
82 {
83 parts.method = m;
84 }
85 if let Some(ref uri) = self.uri
86 && let Ok(u) = uri.parse()
87 {
88 parts.uri = u;
89 }
90 apply_headers(&self.headers, &mut parts.headers);
91 }
92
93 pub fn apply_to_response_parts(&self, parts: &mut http::response::Parts) {
95 if let Some(status) = self.status && let Ok(s) = http::StatusCode::from_u16(status) {
96 parts.status = s;
97 }
98 apply_headers(&self.headers, &mut parts.headers);
99 }
100}
101
102fn apply_headers(wasm_headers: &[(String, String)], header_map: &mut http::HeaderMap) {
104 header_map.clear();
105 for (k, v) in wasm_headers {
106 if let (Ok(name), Ok(value)) = (HeaderName::from_str(k), HeaderValue::from_str(v)) {
107 header_map.insert(name, value);
108 }
109 }
110}
111
112#[derive(Serialize, Deserialize)]
114pub struct WasmOutput {
115 pub head: Option<WasmHead>,
117 pub body: Option<Vec<u8>>,
119}
120
121#[derive(Serialize, Deserialize)]
123pub struct WasmPluginInfo {
124 pub name: String,
125 pub version: String,
126 pub description: String,
127 pub default_config: String,
129 pub readme: Option<String>,
131}