folk_api/request.rs
1//! Request-side streaming types — the request counterpart to [`ResponseChunk`].
2//!
3//! In streaming mode (`stream_request_body`) the HTTP plugin delivers the
4//! request body to PHP as a channel instead of buffering it. Two shapes:
5//!
6//! * [`RequestBody::Raw`] — opaque body bytes (`folk_read` / `folk_read_all`).
7//! * [`RequestBody::Parts`] — a parsed `multipart/form-data` stream
8//! (`folk_next_part` + `folk_part_read`), produced by a `multer` task in the
9//! plugin so PHP never sees boundary bytes.
10//!
11//! [`ResponseChunk`]: crate::response::ResponseChunk
12
13use bytes::Bytes;
14use tokio::sync::mpsc;
15
16/// One element of a streamed `multipart/form-data` request.
17///
18/// The plugin emits, per field: one [`RequestPart::Start`], zero or more
19/// [`RequestPart::Data`] chunks, then one [`RequestPart::End`]. The channel
20/// closing signals "no more parts".
21#[derive(Debug, Clone)]
22pub enum RequestPart {
23 /// A new part begins. `name` is the form field name; `filename` is set for
24 /// file parts (`None` for plain fields); `content_type` is the part's
25 /// declared `Content-Type`, if any.
26 Start {
27 name: Option<String>,
28 filename: Option<String>,
29 content_type: Option<String>,
30 },
31 /// A chunk of the current part's body.
32 Data(Bytes),
33 /// The current part is complete.
34 End,
35}
36
37/// What feeds a worker's request body in streaming mode.
38///
39/// Exactly one variant is created per streamed request, chosen by the HTTP
40/// plugin from the request `Content-Type`.
41pub enum RequestBody {
42 /// Opaque body bytes — consumed by `folk_read` / `folk_read_all`.
43 Raw(mpsc::Receiver<Bytes>),
44 /// A parsed multipart stream — consumed by `folk_next_part` /
45 /// `folk_part_read` / `folk_part_read_all`.
46 Parts(mpsc::Receiver<RequestPart>),
47}