@@ -11,9 +11,39 @@ use super::{UserDataPointer, HYPER_ITER_CONTINUE};
1111use crate :: body:: { Bytes , Frame , Incoming as IncomingBody } ;
1212
1313/// 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.
1433pub struct hyper_body ( pub ( super ) IncomingBody ) ;
1534
1635/// 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.
1747pub struct hyper_buf ( pub ( crate ) Bytes ) ;
1848
1949pub ( crate ) struct UserBody {
@@ -29,7 +59,7 @@ type hyper_body_data_callback =
2959 extern "C" fn ( * mut c_void , * mut hyper_context < ' _ > , * mut * mut hyper_buf ) -> c_int ;
3060
3161ffi_fn ! {
32- /// Create a new "empty" body.
62+ /// Creates a new "empty" body.
3363 ///
3464 /// If not configured, this body acts as an empty payload.
3565 ///
@@ -51,20 +81,31 @@ ffi_fn! {
5181}
5282
5383ffi_fn ! {
54- /// Return a task that will poll the body for the next buffer of data.
84+ /// Creates a task that will poll a response body for the next buffer of data.
5585 ///
56- /// The task value may have different types depending on the outcome:
86+ /// The task may have different types depending on the outcome:
5787 ///
5888 /// - `HYPER_TASK_BUF`: Success, and more data was received.
5989 /// - `HYPER_TASK_ERROR`: An error retrieving the data.
6090 /// - `HYPER_TASK_EMPTY`: The body has finished streaming data.
6191 ///
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+ ///
62100 /// To avoid a memory leak, the task must eventually be consumed by
63101 /// `hyper_task_free`, or taken ownership of by `hyper_executor_push`
64102 /// without subsequently being given back by `hyper_executor_poll`.
65103 ///
66- /// This does not consume the `hyper_body *`, so it may be used to again.
67- /// However, it MUST NOT be used or freed until the related task completes.
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`.
68109 fn hyper_body_data( body: * mut hyper_body) -> * mut hyper_task {
69110 // This doesn't take ownership of the Body, so don't allow destructor
70111 let mut body = ManuallyDrop :: new( non_null!( Box :: from_raw( body) ?= ptr:: null_mut( ) ) ) ;
@@ -88,18 +129,20 @@ ffi_fn! {
88129}
89130
90131ffi_fn ! {
91- /// Return a task that will poll the body and execute the callback with each
92- /// body chunk that is received.
132+ /// Creates a task to execute the callback with each body chunk received.
93133 ///
94134 /// To avoid a memory leak, the task must eventually be consumed by
95135 /// `hyper_task_free`, or taken ownership of by `hyper_executor_push`
96136 /// without subsequently being given back by `hyper_executor_poll`.
97137 ///
98- /// The `hyper_buf` pointer is only a borrowed reference, it cannot live outside
99- /// the execution of the callback. You must make a copy to retain it .
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 .
100140 ///
101141 /// The callback should return `HYPER_ITER_CONTINUE` to continue iterating
102- /// chunks as they are received, or `HYPER_ITER_BREAK` to cancel.
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.
103146 ///
104147 /// This will consume the `hyper_body *`, you shouldn't use it anymore or free it.
105148 fn hyper_body_foreach( body: * mut hyper_body, func: hyper_body_foreach_callback, userdata: * mut c_void) -> * mut hyper_task {
@@ -129,7 +172,7 @@ ffi_fn! {
129172}
130173
131174ffi_fn ! {
132- /// Set the data callback for this body.
175+ /// Set the outgoing data callback for this body.
133176 ///
134177 /// The callback is called each time hyper needs to send more data for the
135178 /// body. It is passed the value from `hyper_body_set_userdata`.
0 commit comments