rama_hyper/ffi/body.rs
1use std::ffi::c_void;
2use std::mem::ManuallyDrop;
3use std::ptr;
4use std::task::{Context, Poll};
5
6use http_body_util::BodyExt as _;
7use libc::{c_int, size_t};
8
9use super::task::{hyper_context, hyper_task, hyper_task_return_type, AsTaskType};
10use super::{UserDataPointer, HYPER_ITER_CONTINUE};
11use crate::body::{Bytes, Frame, Incoming as IncomingBody};
12
13/// A streaming HTTP body.
14///
15/// This is used both for sending requests (with `hyper_request_set_body`) and
16/// for receiving responses (with `hyper_response_body`).
17///
18/// For outgoing request bodies, call `hyper_body_set_data_func` to provide the
19/// data.
20///
21/// For incoming response bodies, call `hyper_body_data` to get a task that will
22/// yield a chunk of data each time it is polled. That task must be then be
23/// added to the executor with `hyper_executor_push`.
24///
25/// Methods:
26///
27/// - hyper_body_new: Create a new “empty” body.
28/// - hyper_body_set_userdata: Set userdata on this body, which will be passed to callback functions.
29/// - hyper_body_set_data_func: Set the data callback for this body.
30/// - hyper_body_data: Creates a task that will poll a response body for the next buffer of data.
31/// - hyper_body_foreach: Creates a task to execute the callback with each body chunk received.
32/// - hyper_body_free: Free a body.
33pub struct hyper_body(pub(super) IncomingBody);
34
35/// A buffer of bytes that is sent or received on a `hyper_body`.
36///
37/// Obtain one of these in the callback of `hyper_body_foreach` or by receiving
38/// a task of type `HYPER_TASK_BUF` from `hyper_executor_poll` (after calling
39/// `hyper_body_data` and pushing the resulting task).
40///
41/// Methods:
42///
43/// - hyper_buf_bytes: Get a pointer to the bytes in this buffer.
44/// - hyper_buf_copy: Create a new hyper_buf * by copying the provided bytes.
45/// - hyper_buf_free: Free this buffer.
46/// - hyper_buf_len: Get the length of the bytes this buffer contains.
47pub struct hyper_buf(pub(crate) Bytes);
48
49pub(crate) struct UserBody {
50 data_func: hyper_body_data_callback,
51 userdata: *mut c_void,
52}
53
54// ===== Body =====
55
56type hyper_body_foreach_callback = extern "C" fn(*mut c_void, *const hyper_buf) -> c_int;
57
58type hyper_body_data_callback =
59 extern "C" fn(*mut c_void, *mut hyper_context<'_>, *mut *mut hyper_buf) -> c_int;
60
61ffi_fn! {
62 /// Creates a new "empty" body.
63 ///
64 /// If not configured, this body acts as an empty payload.
65 ///
66 /// To avoid a memory leak, the body must eventually be consumed by
67 /// `hyper_body_free`, `hyper_body_foreach`, or `hyper_request_set_body`.
68 fn hyper_body_new() -> *mut hyper_body {
69 Box::into_raw(Box::new(hyper_body(IncomingBody::ffi())))
70 } ?= ptr::null_mut()
71}
72
73ffi_fn! {
74 /// Free a body.
75 ///
76 /// This should only be used if the request isn't consumed by
77 /// `hyper_body_foreach` or `hyper_request_set_body`.
78 fn hyper_body_free(body: *mut hyper_body) {
79 drop(non_null!(Box::from_raw(body) ?= ()));
80 }
81}
82
83ffi_fn! {
84 /// Creates a task that will poll a response body for the next buffer of data.
85 ///
86 /// The task may have different types depending on the outcome:
87 ///
88 /// - `HYPER_TASK_BUF`: Success, and more data was received.
89 /// - `HYPER_TASK_ERROR`: An error retrieving the data.
90 /// - `HYPER_TASK_EMPTY`: The body has finished streaming data.
91 ///
92 /// When the application receives the task from `hyper_executor_poll`,
93 /// if the task type is `HYPER_TASK_BUF`, it should cast the task to
94 /// `hyper_buf *` and consume all the bytes in the buffer. Then
95 /// the application should call `hyper_body_data` again for the same
96 /// `hyper_body *`, to create a task for the next buffer of data.
97 /// Repeat until the polled task type is `HYPER_TASK_ERROR` or
98 /// `HYPER_TASK_EMPTY`.
99 ///
100 /// To avoid a memory leak, the task must eventually be consumed by
101 /// `hyper_task_free`, or taken ownership of by `hyper_executor_push`
102 /// without subsequently being given back by `hyper_executor_poll`.
103 ///
104 /// This does not consume the `hyper_body *`, so it may be used again.
105 /// However, the `hyper_body *` MUST NOT be used or freed until the
106 /// related task is returned from `hyper_executor_poll`.
107 ///
108 /// For a more convenient method, see also `hyper_body_foreach`.
109 fn hyper_body_data(body: *mut hyper_body) -> *mut hyper_task {
110 // This doesn't take ownership of the Body, so don't allow destructor
111 let mut body = ManuallyDrop::new(non_null!(Box::from_raw(body) ?= ptr::null_mut()));
112
113 Box::into_raw(hyper_task::boxed(async move {
114 loop {
115 match body.0.frame().await {
116 Some(Ok(frame)) => {
117 if let Ok(data) = frame.into_data() {
118 return Ok(Some(hyper_buf(data)));
119 } else {
120 continue;
121 }
122 },
123 Some(Err(e)) => return Err(e),
124 None => return Ok(None),
125 }
126 }
127 }))
128 } ?= ptr::null_mut()
129}
130
131ffi_fn! {
132 /// Creates a task to execute the callback with each body chunk received.
133 ///
134 /// To avoid a memory leak, the task must eventually be consumed by
135 /// `hyper_task_free`, or taken ownership of by `hyper_executor_push`
136 /// without subsequently being given back by `hyper_executor_poll`.
137 ///
138 /// The `hyper_buf` pointer is only a borrowed reference. It cannot live outside
139 /// the execution of the callback. You must make a copy of the bytes to retain them.
140 ///
141 /// The callback should return `HYPER_ITER_CONTINUE` to continue iterating
142 /// chunks as they are received, or `HYPER_ITER_BREAK` to cancel. Each
143 /// invocation of the callback must consume all the bytes it is provided.
144 /// There is no mechanism to signal to Hyper that only a subset of bytes were
145 /// consumed.
146 ///
147 /// This will consume the `hyper_body *`, you shouldn't use it anymore or free it.
148 fn hyper_body_foreach(body: *mut hyper_body, func: hyper_body_foreach_callback, userdata: *mut c_void) -> *mut hyper_task {
149 let mut body = non_null!(Box::from_raw(body) ?= ptr::null_mut());
150 let userdata = UserDataPointer(userdata);
151
152 Box::into_raw(hyper_task::boxed(async move {
153 while let Some(item) = body.0.frame().await {
154 let frame = item?;
155 if let Ok(chunk) = frame.into_data() {
156 if HYPER_ITER_CONTINUE != func(userdata.0, &hyper_buf(chunk)) {
157 return Err(crate::Error::new_user_aborted_by_callback());
158 }
159 }
160 }
161 Ok(())
162 }))
163 } ?= ptr::null_mut()
164}
165
166ffi_fn! {
167 /// Set userdata on this body, which will be passed to callback functions.
168 fn hyper_body_set_userdata(body: *mut hyper_body, userdata: *mut c_void) {
169 let b = non_null!(&mut *body ?= ());
170 b.0.as_ffi_mut().userdata = userdata;
171 }
172}
173
174ffi_fn! {
175 /// Set the outgoing data callback for this body.
176 ///
177 /// The callback is called each time hyper needs to send more data for the
178 /// body. It is passed the value from `hyper_body_set_userdata`.
179 ///
180 /// If there is data available, the `hyper_buf **` argument should be set
181 /// to a `hyper_buf *` containing the data, and `HYPER_POLL_READY` should
182 /// be returned.
183 ///
184 /// Returning `HYPER_POLL_READY` while the `hyper_buf **` argument points
185 /// to `NULL` will indicate the body has completed all data.
186 ///
187 /// If there is more data to send, but it isn't yet available, a
188 /// `hyper_waker` should be saved from the `hyper_context *` argument, and
189 /// `HYPER_POLL_PENDING` should be returned. You must wake the saved waker
190 /// to signal the task when data is available.
191 ///
192 /// If some error has occurred, you can return `HYPER_POLL_ERROR` to abort
193 /// the body.
194 fn hyper_body_set_data_func(body: *mut hyper_body, func: hyper_body_data_callback) {
195 let b = non_null!{ &mut *body ?= () };
196 b.0.as_ffi_mut().data_func = func;
197 }
198}
199
200// ===== impl UserBody =====
201
202impl UserBody {
203 pub(crate) fn new() -> UserBody {
204 UserBody {
205 data_func: data_noop,
206 userdata: std::ptr::null_mut(),
207 }
208 }
209
210 pub(crate) fn poll_data(
211 &mut self,
212 cx: &mut Context<'_>,
213 ) -> Poll<Option<crate::Result<Frame<Bytes>>>> {
214 let mut out = std::ptr::null_mut();
215 match (self.data_func)(self.userdata, hyper_context::wrap(cx), &mut out) {
216 super::task::HYPER_POLL_READY => {
217 if out.is_null() {
218 Poll::Ready(None)
219 } else {
220 let buf = unsafe { Box::from_raw(out) };
221 Poll::Ready(Some(Ok(Frame::data(buf.0))))
222 }
223 }
224 super::task::HYPER_POLL_PENDING => Poll::Pending,
225 super::task::HYPER_POLL_ERROR => {
226 Poll::Ready(Some(Err(crate::Error::new_body_write_aborted())))
227 }
228 unexpected => Poll::Ready(Some(Err(crate::Error::new_body_write(format!(
229 "unexpected hyper_body_data_func return code {}",
230 unexpected
231 ))))),
232 }
233 }
234}
235
236/// cbindgen:ignore
237extern "C" fn data_noop(
238 _userdata: *mut c_void,
239 _: *mut hyper_context<'_>,
240 _: *mut *mut hyper_buf,
241) -> c_int {
242 super::task::HYPER_POLL_READY
243}
244
245unsafe impl Send for UserBody {}
246unsafe impl Sync for UserBody {}
247
248// ===== Bytes =====
249
250ffi_fn! {
251 /// Create a new `hyper_buf *` by copying the provided bytes.
252 ///
253 /// This makes an owned copy of the bytes, so the `buf` argument can be
254 /// freed (with `hyper_buf_free`) or changed afterwards.
255 ///
256 /// To avoid a memory leak, the copy must eventually be consumed by
257 /// `hyper_buf_free`.
258 ///
259 /// This returns `NULL` if allocating a new buffer fails.
260 fn hyper_buf_copy(buf: *const u8, len: size_t) -> *mut hyper_buf {
261 let slice = unsafe {
262 std::slice::from_raw_parts(buf, len)
263 };
264 Box::into_raw(Box::new(hyper_buf(Bytes::copy_from_slice(slice))))
265 } ?= ptr::null_mut()
266}
267
268ffi_fn! {
269 /// Get a pointer to the bytes in this buffer.
270 ///
271 /// This should be used in conjunction with `hyper_buf_len` to get the length
272 /// of the bytes data.
273 ///
274 /// This pointer is borrowed data, and not valid once the `hyper_buf` is
275 /// consumed/freed.
276 fn hyper_buf_bytes(buf: *const hyper_buf) -> *const u8 {
277 unsafe { (*buf).0.as_ptr() }
278 } ?= ptr::null()
279}
280
281ffi_fn! {
282 /// Get the length of the bytes this buffer contains.
283 fn hyper_buf_len(buf: *const hyper_buf) -> size_t {
284 unsafe { (*buf).0.len() }
285 }
286}
287
288ffi_fn! {
289 /// Free this buffer.
290 ///
291 /// This should be used for any buffer once it is no longer needed.
292 fn hyper_buf_free(buf: *mut hyper_buf) {
293 drop(unsafe { Box::from_raw(buf) });
294 }
295}
296
297unsafe impl AsTaskType for hyper_buf {
298 fn as_task_type(&self) -> hyper_task_return_type {
299 hyper_task_return_type::HYPER_TASK_BUF
300 }
301}