suborbital/
req.rs

1pub mod field_type;
2
3use crate::ffi;
4use crate::util;
5use crate::STATE;
6use field_type::FieldType;
7
8extern "C" {
9	fn request_get_field(field_type: i32, key_pointer: *const u8, key_size: i32, ident: i32)
10		-> i32;
11	fn request_set_field(
12		field_type: i32,
13		key_pointer: *const u8,
14		key_size: i32,
15		val_pointer: *const u8,
16		val_size: i32,
17		ident: i32,
18	) -> i32;
19}
20
21pub fn method() -> String {
22	get_field(FieldType::Meta.into(), "method").map_or("".into(), util::to_string)
23}
24
25pub fn set_method(val: &str) -> Result<(), super::plugin::HostErr> {
26	set_field(FieldType::Meta.into(), "method", val)
27}
28
29pub fn url() -> String {
30	get_field(FieldType::Meta.into(), "url").map_or("".into(), util::to_string)
31}
32
33pub fn set_url(val: &str) -> Result<(), super::plugin::HostErr> {
34	set_field(FieldType::Meta.into(), "url", val)
35}
36
37pub fn id() -> String {
38	get_field(FieldType::Meta.into(), "id").map_or("".into(), util::to_string)
39}
40
41pub fn body_raw() -> Vec<u8> {
42	get_field(FieldType::Meta.into(), "body").unwrap_or_default()
43}
44
45pub fn set_body(val: &str) -> Result<(), super::plugin::HostErr> {
46	set_field(FieldType::Body.into(), "body", val)
47}
48
49pub fn body_field(key: &str) -> String {
50	get_field(FieldType::Body.into(), key).map_or("".into(), util::to_string)
51}
52
53pub fn set_body_field(key: &str, val: &str) -> Result<(), super::plugin::HostErr> {
54	set_field(FieldType::Body.into(), key, val)
55}
56
57pub fn header(key: &str) -> String {
58	get_field(FieldType::Header.into(), key).map_or("".into(), util::to_string)
59}
60
61pub fn set_header(key: &str, val: &str) -> Result<(), super::plugin::HostErr> {
62	set_field(FieldType::Header.into(), key, val)
63}
64
65pub fn url_param(key: &str) -> String {
66	get_field(FieldType::Params.into(), key).map_or("".into(), util::to_string)
67}
68
69pub fn set_url_param(key: &str, val: &str) -> Result<(), super::plugin::HostErr> {
70	set_field(FieldType::Params.into(), key, val)
71}
72
73pub fn state(key: &str) -> Option<String> {
74	get_field(FieldType::State.into(), key).map(util::to_string)
75}
76
77pub fn set_state(key: &str, val: &str) -> Result<(), super::plugin::HostErr> {
78	set_field(FieldType::State.into(), key, val)
79}
80
81pub fn state_raw(key: &str) -> Option<Vec<u8>> {
82	get_field(FieldType::State.into(), key)
83}
84
85pub fn query_param(key: &str) -> String {
86	get_field(FieldType::Query.into(), key).map_or("".into(), util::to_string)
87}
88
89/// Executes the request via FFI
90///
91/// Then retreives the result from the host and returns it
92fn get_field(field_type: i32, key: &str) -> Option<Vec<u8>> {
93	let result_size =
94		unsafe { request_get_field(field_type, key.as_ptr(), key.len() as i32, STATE.ident) };
95
96	ffi::result(result_size).map_or(None, Option::from)
97}
98
99fn set_field(field_type: i32, key: &str, val: &str) -> Result<(), super::plugin::HostErr> {
100	// make the request over FFI
101	let result_size = unsafe {
102		request_set_field(
103			field_type,
104			key.as_ptr(),
105			key.len() as i32,
106			val.as_ptr(),
107			val.len() as i32,
108			super::STATE.ident,
109		)
110	};
111
112	// retreive the result from the host and return it
113	match ffi::result(result_size) {
114		Ok(_) => Ok(()),
115		Err(e) => Err(e),
116	}
117}