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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
|
/* SPDX-License-Identifier: Apache-2.0 */
/*
* Copyright (C) Andrew Clayton
* Copyright (C) F5, Inc.
*/
use std::ffi::c_char;
use std::ffi::c_void;
use std::ffi::CStr;
use std::ptr::null_mut;
use std::slice;
use std::str;
#[macro_export]
macro_rules! C2S {
($a:expr) => {{
unsafe { CStr::from_ptr($a).to_str().unwrap() }
}};
}
#[macro_export]
macro_rules! S2C {
($a:expr) => {{
format!("{}\0", $a)
}};
}
#[macro_export]
macro_rules! uwr_write_str{
($a:expr, $($arg:tt)*) => {
{
uwr_mem_write_str($a, &format!($($arg)*))
}
}}
pub const fn UWR_CTX_INITIALIZER() -> luw_ctx_t {
luw_ctx_t {
addr: null_mut(),
mem: null_mut(),
req: null_mut(),
resp: null_mut(),
resp_hdr: null_mut(),
resp_offset: 0,
req_buf: null_mut(),
hdrp: null_mut(),
reqp: null_mut(),
resp_hdr_idx: -1,
}
}
pub fn uwr_init_ctx(ctx: *mut luw_ctx_t, addr: *mut u8, offset: usize) {
unsafe {
luw_init_ctx(ctx, addr, offset);
}
}
pub fn uwr_set_req_buf(
ctx: *mut luw_ctx_t,
buf: *mut *mut u8,
flags: u32,
) -> i32 {
unsafe { luw_set_req_buf(ctx, buf, flags) }
}
pub fn uwr_get_http_path(ctx: *const luw_ctx_t) -> &'static str {
C2S!(luw_get_http_path(ctx))
}
pub fn uwr_get_http_method(ctx: *const luw_ctx_t) -> &'static str {
C2S!(luw_get_http_method(ctx))
}
pub fn uwr_get_http_version(ctx: *const luw_ctx_t) -> &'static str {
C2S!(luw_get_http_version(ctx))
}
pub fn uwr_get_http_query(ctx: *const luw_ctx_t) -> &'static str {
C2S!(luw_get_http_query(ctx))
}
pub fn uwr_get_http_remote(ctx: *const luw_ctx_t) -> &'static str {
C2S!(luw_get_http_remote(ctx))
}
pub fn uwr_get_http_local_addr(ctx: *const luw_ctx_t) -> &'static str {
C2S!(luw_get_http_local_addr(ctx))
}
pub fn uwr_get_http_local_port(ctx: *const luw_ctx_t) -> &'static str {
C2S!(luw_get_http_local_port(ctx))
}
pub fn uwr_get_http_server_name(ctx: *const luw_ctx_t) -> &'static str {
C2S!(luw_get_http_server_name(ctx))
}
pub fn uwr_get_http_content_len(ctx: *const luw_ctx_t) -> u64 {
unsafe { luw_get_http_content_len(ctx) }
}
pub fn uwr_get_http_content_sent(ctx: *const luw_ctx_t) -> usize {
unsafe { luw_get_http_content_sent(ctx) }
}
pub fn uwr_get_http_total_content_sent(ctx: *const luw_ctx_t) -> u64 {
unsafe { luw_get_http_total_content_sent(ctx) }
}
pub fn uwr_get_http_content(ctx: *const luw_ctx_t) -> *const u8 {
unsafe { luw_get_http_content(ctx) }
}
pub fn uwr_get_http_content_str(ctx: *const luw_ctx_t) -> &'static str {
unsafe {
let slice = slice::from_raw_parts(
uwr_get_http_content(ctx),
uwr_get_http_total_content_sent(ctx).try_into().unwrap(),
);
str::from_utf8(slice).unwrap()
}
}
pub fn uwr_http_is_tls(ctx: *const luw_ctx_t) -> bool {
unsafe { luw_http_is_tls(ctx) }
}
pub fn uwr_http_hdr_iter(
ctx: *mut luw_ctx_t,
luw_http_hdr_iter_func: ::std::option::Option<
unsafe extern "C" fn(
ctx: *mut luw_ctx_t,
name: *const c_char,
value: *const c_char,
data: *mut c_void,
) -> bool,
>,
user_data: *mut c_void,
) {
unsafe { luw_http_hdr_iter(ctx, luw_http_hdr_iter_func, user_data) }
}
pub fn uwr_http_hdr_get_value(
ctx: *const luw_ctx_t,
hdr: &str,
) -> &'static str {
C2S!(luw_http_hdr_get_value(ctx, S2C!(hdr).as_ptr() as *const i8))
}
pub fn uwr_get_response_data_size(ctx: *const luw_ctx_t) -> usize {
unsafe { luw_get_response_data_size(ctx) }
}
pub fn uwr_mem_write_str(ctx: *mut luw_ctx_t, src: &str) -> usize {
unsafe { luw_mem_writep_data(ctx, src.as_ptr(), src.len()) }
}
pub fn uwr_mem_write_buf(
ctx: *mut luw_ctx_t,
src: *const u8,
size: u64,
) -> usize {
/*
* We're dealing with a 32bit address space, but we allow
* size to come from the output of uwr_get_http_content_len()
* which returns a u64 to allow for larger than memory uploads.
*/
let sz = size as usize;
unsafe { luw_mem_writep_data(ctx, src, sz) }
}
pub fn uwr_req_buf_append(ctx: *mut luw_ctx_t, src: *const u8) {
unsafe {
luw_req_buf_append(ctx, src);
}
}
pub fn uwr_mem_fill_buf_from_req(ctx: *mut luw_ctx_t, from: usize) -> usize {
unsafe { luw_mem_fill_buf_from_req(ctx, from) }
}
pub fn uwr_luw_mem_reset(ctx: *mut luw_ctx_t) {
unsafe {
luw_mem_reset(ctx);
}
}
pub fn uwr_http_set_response_status(status: luw_http_status_t) {
unsafe {
luw_http_set_response_status(status);
}
}
pub fn uwr_http_send_response(ctx: *const luw_ctx_t) {
unsafe {
luw_http_send_response(ctx);
}
}
pub fn uwr_http_init_headers(ctx: *mut luw_ctx_t, nr: usize, offset: usize) {
unsafe {
luw_http_init_headers(ctx, nr, offset);
}
}
pub fn uwr_http_add_header(ctx: *mut luw_ctx_t, name: &str, value: &str) {
unsafe {
luw_http_add_header(
ctx,
S2C!(name).as_ptr() as *const i8,
S2C!(value).as_ptr() as *const i8,
);
}
}
pub fn uwr_http_add_header_content_type(ctx: *mut luw_ctx_t, ctype: &str) {
uwr_http_add_header(ctx, "Content-Type", ctype);
}
pub fn uwr_http_add_header_content_len(ctx: *mut luw_ctx_t) {
uwr_http_add_header(
ctx,
"Content-Length",
&format!("{}", uwr_get_response_data_size(ctx)),
);
}
pub fn uwr_http_send_headers(ctx: *const luw_ctx_t) {
unsafe {
luw_http_send_headers(ctx);
}
}
pub fn uwr_http_response_end() {
unsafe {
luw_http_response_end();
}
}
pub fn uwr_mem_get_init_size() -> u32 {
unsafe { luw_mem_get_init_size() }
}
pub fn uwr_malloc(size: u32) -> *mut u8 {
unsafe { luw_malloc(size as usize) as *mut u8 }
}
pub fn uwr_free(ptr: *mut u8) {
unsafe {
luw_free(ptr as *mut c_void);
}
}
|