@@ -11,9 +11,39 @@ use super::{UserDataPointer, HYPER_ITER_CONTINUE};
11
11
use crate :: body:: { Bytes , Frame , Incoming as IncomingBody } ;
12
12
13
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.
14
33
pub struct hyper_body ( pub ( super ) IncomingBody ) ;
15
34
16
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.
17
47
pub struct hyper_buf ( pub ( crate ) Bytes ) ;
18
48
19
49
pub ( crate ) struct UserBody {
@@ -29,7 +59,7 @@ type hyper_body_data_callback =
29
59
extern "C" fn ( * mut c_void , * mut hyper_context < ' _ > , * mut * mut hyper_buf ) -> c_int ;
30
60
31
61
ffi_fn ! {
32
- /// Create a new "empty" body.
62
+ /// Creates a new "empty" body.
33
63
///
34
64
/// If not configured, this body acts as an empty payload.
35
65
///
@@ -51,20 +81,31 @@ ffi_fn! {
51
81
}
52
82
53
83
ffi_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.
55
85
///
56
- /// The task value may have different types depending on the outcome:
86
+ /// The task may have different types depending on the outcome:
57
87
///
58
88
/// - `HYPER_TASK_BUF`: Success, and more data was received.
59
89
/// - `HYPER_TASK_ERROR`: An error retrieving the data.
60
90
/// - `HYPER_TASK_EMPTY`: The body has finished streaming data.
61
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
+ ///
62
100
/// To avoid a memory leak, the task must eventually be consumed by
63
101
/// `hyper_task_free`, or taken ownership of by `hyper_executor_push`
64
102
/// without subsequently being given back by `hyper_executor_poll`.
65
103
///
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`.
68
109
fn hyper_body_data( body: * mut hyper_body) -> * mut hyper_task {
69
110
// This doesn't take ownership of the Body, so don't allow destructor
70
111
let mut body = ManuallyDrop :: new( non_null!( Box :: from_raw( body) ?= ptr:: null_mut( ) ) ) ;
@@ -88,18 +129,20 @@ ffi_fn! {
88
129
}
89
130
90
131
ffi_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.
93
133
///
94
134
/// To avoid a memory leak, the task must eventually be consumed by
95
135
/// `hyper_task_free`, or taken ownership of by `hyper_executor_push`
96
136
/// without subsequently being given back by `hyper_executor_poll`.
97
137
///
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 .
100
140
///
101
141
/// 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.
103
146
///
104
147
/// This will consume the `hyper_body *`, you shouldn't use it anymore or free it.
105
148
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! {
129
172
}
130
173
131
174
ffi_fn ! {
132
- /// Set the data callback for this body.
175
+ /// Set the outgoing data callback for this body.
133
176
///
134
177
/// The callback is called each time hyper needs to send more data for the
135
178
/// body. It is passed the value from `hyper_body_set_userdata`.
0 commit comments