1use std::ffi::c_char;
9use std::ffi::c_void;
10use std::ffi::CStr;
11use std::fs::File;
12use std::os::fd::{AsRawFd, RawFd};
13use std::ptr::null_mut;
14use std::slice;
15use std::str;
16
17#[macro_export]
18macro_rules! C2S {
19 ($a:expr) => {{
20 unsafe { CStr::from_ptr($a).to_str().unwrap() }
21 }};
22}
23
24#[macro_export]
25macro_rules! S2C {
26 ($a:expr) => {{
27 format!("{}\0", $a)
28 }};
29}
30
31#[macro_export]
32macro_rules! uwr_write_str{
33 ($a:expr, $($arg:tt)*) => {
34 {
35 uwr_mem_write_str($a, &format!($($arg)*))
36 }
37}}
38
39pub const fn UWR_CTX_INITIALIZER() -> luw_ctx_t {
40 luw_ctx_t {
41 addr: null_mut(),
42 mem: null_mut(),
43 req: null_mut(),
44 resp: null_mut(),
45 resp_hdr: null_mut(),
46 resp_offset: 0,
47 req_buf: null_mut(),
48 hdrp: null_mut(),
49 reqp: null_mut(),
50 resp_hdr_idx: -1,
51 }
52}
53
54pub fn uwr_init_ctx(ctx: *mut luw_ctx_t, addr: *mut u8, offset: usize) {
55 unsafe {
56 luw_init_ctx(ctx, addr, offset);
57 }
58}
59
60pub fn uwr_set_req_buf(
61 ctx: *mut luw_ctx_t,
62 buf: *mut *mut u8,
63 flags: u32,
64) -> i32 {
65 unsafe { luw_set_req_buf(ctx, buf, flags) }
66}
67
68pub fn uwr_get_http_path(ctx: *const luw_ctx_t) -> &'static str {
69 C2S!(luw_get_http_path(ctx))
70}
71
72pub fn uwr_get_http_method(ctx: *const luw_ctx_t) -> &'static str {
73 C2S!(luw_get_http_method(ctx))
74}
75
76pub fn uwr_get_http_version(ctx: *const luw_ctx_t) -> &'static str {
77 C2S!(luw_get_http_version(ctx))
78}
79
80pub fn uwr_get_http_query(ctx: *const luw_ctx_t) -> &'static str {
81 C2S!(luw_get_http_query(ctx))
82}
83
84pub fn uwr_get_http_remote(ctx: *const luw_ctx_t) -> &'static str {
85 C2S!(luw_get_http_remote(ctx))
86}
87
88pub fn uwr_get_http_local_addr(ctx: *const luw_ctx_t) -> &'static str {
89 C2S!(luw_get_http_local_addr(ctx))
90}
91
92pub fn uwr_get_http_local_port(ctx: *const luw_ctx_t) -> &'static str {
93 C2S!(luw_get_http_local_port(ctx))
94}
95
96pub fn uwr_get_http_server_name(ctx: *const luw_ctx_t) -> &'static str {
97 C2S!(luw_get_http_server_name(ctx))
98}
99
100pub fn uwr_get_http_content_len(ctx: *const luw_ctx_t) -> u64 {
101 unsafe { luw_get_http_content_len(ctx) }
102}
103
104pub fn uwr_get_http_content_sent(ctx: *const luw_ctx_t) -> usize {
105 unsafe { luw_get_http_content_sent(ctx) }
106}
107
108pub fn uwr_get_http_total_content_sent(ctx: *const luw_ctx_t) -> u64 {
109 unsafe { luw_get_http_total_content_sent(ctx) }
110}
111
112pub fn uwr_get_http_content(ctx: *const luw_ctx_t) -> *const u8 {
113 unsafe { luw_get_http_content(ctx) }
114}
115
116pub fn uwr_get_http_content_str(ctx: *const luw_ctx_t) -> &'static str {
117 unsafe {
118 let slice = slice::from_raw_parts(
119 uwr_get_http_content(ctx),
120 uwr_get_http_total_content_sent(ctx).try_into().unwrap(),
121 );
122 str::from_utf8(slice).unwrap()
123 }
124}
125
126pub fn uwr_http_is_tls(ctx: *const luw_ctx_t) -> bool {
127 unsafe { luw_http_is_tls(ctx) }
128}
129
130pub fn uwr_http_hdr_iter(
131 ctx: *mut luw_ctx_t,
132 luw_http_hdr_iter_func: ::std::option::Option<
133 unsafe extern "C" fn(
134 ctx: *mut luw_ctx_t,
135 name: *const c_char,
136 value: *const c_char,
137 data: *mut c_void,
138 ) -> bool,
139 >,
140 user_data: *mut c_void,
141) {
142 unsafe { luw_http_hdr_iter(ctx, luw_http_hdr_iter_func, user_data) }
143}
144
145pub fn uwr_http_hdr_get_value(
146 ctx: *const luw_ctx_t,
147 hdr: &str,
148) -> &'static str {
149 C2S!(luw_http_hdr_get_value(ctx, S2C!(hdr).as_ptr() as *const i8))
150}
151
152pub fn uwr_get_response_data_size(ctx: *const luw_ctx_t) -> usize {
153 unsafe { luw_get_response_data_size(ctx) }
154}
155
156pub fn uwr_mem_write_str(ctx: *mut luw_ctx_t, src: &str) -> usize {
157 unsafe { luw_mem_writep_data(ctx, src.as_ptr(), src.len()) }
158}
159
160pub fn uwr_mem_write_buf(
161 ctx: *mut luw_ctx_t,
162 src: *const u8,
163 size: u64,
164) -> usize {
165 let sz = size as usize;
171 unsafe { luw_mem_writep_data(ctx, src, sz) }
172}
173
174pub fn uwr_req_buf_append(ctx: *mut luw_ctx_t, src: *const u8) {
175 unsafe {
176 luw_req_buf_append(ctx, src);
177 }
178}
179
180pub fn uwr_req_buf_copy(ctx: *mut luw_ctx_t, src: *const u8) {
181 unsafe {
182 luw_req_buf_copy(ctx, src);
183 }
184}
185
186pub fn uwr_mem_splice_file(src: *const u8, f: &mut File) -> isize {
187 let fd: RawFd = f.as_raw_fd();
188 unsafe { luw_mem_splice_file(src, fd) }
189}
190
191pub fn uwr_mem_fill_buf_from_req(ctx: *mut luw_ctx_t, from: usize) -> usize {
192 unsafe { luw_mem_fill_buf_from_req(ctx, from) }
193}
194
195pub fn uwr_luw_mem_reset(ctx: *mut luw_ctx_t) {
196 unsafe {
197 luw_mem_reset(ctx);
198 }
199}
200
201pub fn uwr_http_set_response_status(status: luw_http_status_t) {
202 unsafe {
203 luw_http_set_response_status(status);
204 }
205}
206
207pub fn uwr_http_send_response(ctx: *const luw_ctx_t) {
208 unsafe {
209 luw_http_send_response(ctx);
210 }
211}
212
213pub fn uwr_http_init_headers(ctx: *mut luw_ctx_t, nr: usize, offset: usize) {
214 unsafe {
215 luw_http_init_headers(ctx, nr, offset);
216 }
217}
218
219pub fn uwr_http_add_header(ctx: *mut luw_ctx_t, name: &str, value: &str) {
220 unsafe {
221 luw_http_add_header(
222 ctx,
223 S2C!(name).as_ptr() as *const i8,
224 S2C!(value).as_ptr() as *const i8,
225 );
226 }
227}
228
229pub fn uwr_http_add_header_content_type(ctx: *mut luw_ctx_t, ctype: &str) {
230 uwr_http_add_header(ctx, "Content-Type", ctype);
231}
232
233pub fn uwr_http_add_header_content_len(ctx: *mut luw_ctx_t) {
234 uwr_http_add_header(
235 ctx,
236 "Content-Length",
237 &format!("{}", uwr_get_response_data_size(ctx)),
238 );
239}
240
241pub fn uwr_http_send_headers(ctx: *const luw_ctx_t) {
242 unsafe {
243 luw_http_send_headers(ctx);
244 }
245}
246
247pub fn uwr_http_response_end() {
248 unsafe {
249 luw_http_response_end();
250 }
251}
252
253pub fn uwr_mem_get_init_size() -> u32 {
254 unsafe { luw_mem_get_init_size() }
255}
256
257pub fn uwr_malloc(size: u32) -> *mut u8 {
258 unsafe { luw_malloc(size as usize) as *mut u8 }
259}
260
261pub fn uwr_free(ptr: *mut u8) {
262 unsafe {
263 luw_free(ptr as *mut c_void);
264 }
265}