form-net 0.11.0

Sans-IO form parsing for Rust: application/x-www-form-urlencoded and streaming multipart/form-data, with budgets checked before allocation, safe upload spilling, and typed extraction. No third-party dependencies.
Documentation
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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
//! Hooks for other crates to take part in a parse without a second pass:
//! an [`Inspector`] sees every part before it is stored, and a [`Sink`]
//! decides where file data goes.
//!
//! Hooks are generic parameters, so a hook that does nothing
//! ([`NoInspector`], [`DiscardSink`]) compiles to nothing.

use crate::error::FormError;
use crate::multipart::parser::{PartHead, PartSummary};

/// Why an inspector refused the input: the HTTP status it wants and a
/// stable, machine-readable code for the reason.
///
/// [`FormError::status_hint`](crate::FormError::status_hint) returns the
/// status (400 when it is outside 400 to 599: a refusal is never a success);
/// [`FormError::code`](crate::FormError::code) returns the code. The code
/// must never contain input.
///
/// # Examples
///
/// ```
/// use form_net::Refusal;
///
/// let r = Refusal::new(415, "customs.avatar_not_image");
/// assert_eq!((r.status, r.code), (415, "customs.avatar_not_image"));
/// ```
#[non_exhaustive]
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct Refusal {
    /// The HTTP status the inspector wants.
    pub status: u16,
    /// The inspector's stable code for the reason.
    pub code: &'static str,
}

impl Refusal {
    /// A refusal with this status and code.
    ///
    /// # Examples
    ///
    /// ```
    /// use form_net::{Refusal, Verdict};
    ///
    /// const BLOCKED: Refusal = Refusal::new(403, "app.blocked");
    /// let verdict = Verdict::Refuse(BLOCKED);
    /// assert_eq!(verdict, Verdict::Refuse(Refusal::new(403, "app.blocked")));
    /// ```
    #[must_use]
    pub const fn new(status: u16, code: &'static str) -> Refusal {
        Refusal { status, code }
    }
}

/// What an inspector says about what it saw.
///
/// # Examples
///
/// ```
/// use form_net::{Refusal, Verdict};
///
/// let v = Verdict::Refuse(Refusal::new(422, "example.not_allowed"));
/// assert!(matches!(v, Verdict::Refuse(r) if r.status == 422));
/// ```
#[must_use]
#[non_exhaustive]
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Verdict {
    /// Go on.
    Continue,
    /// Discard the rest of this part (or this field); it still counts
    /// against the budgets and is left out of the result.
    Skip,
    /// Stop the parse with this refusal.
    Refuse(Refusal),
}

/// Takes part in a parse: called for every part head, the first bytes of
/// every file, every data chunk, every complete text field, every part end,
/// the end of the body, and once on any failure.
///
/// Every method has a default, so an inspector implements only what it
/// checks.
///
/// **Order guarantee**, per part and at every split of the input:
///
/// ```text
/// part_start ─▶ sniff (file parts, when sniff_bytes > 0) ─▶ data* ─▶ field (text fields) ─▶ part_end
/// ... after the last part: end
/// ... on any error, at any point: failed (once), then the error is returned
/// ```
///
/// For urlencoded bodies only `field`, `end`, and `failed` are called.
///
/// # Examples
///
/// ```
/// use form_net::{Inspector, PartHead, Refusal, Verdict};
///
/// /// Allows only PNG and JPEG avatars, judged by their bytes.
/// struct ImagesOnly;
///
/// impl Inspector for ImagesOnly {
///     fn part_start(&mut self, head: &PartHead<'_>) -> Verdict {
///         match (head.name(), head.is_file()) {
///             ("avatar", true) | ("title", false) => Verdict::Continue,
///             _ => Verdict::Refuse(Refusal::new(422, "example.unexpected_field")),
///         }
///     }
///     fn sniff(&mut self, _head: &PartHead<'_>, first: &[u8]) -> Verdict {
///         if first.starts_with(b"\x89PNG\r\n\x1a\n") || first.starts_with(b"\xff\xd8\xff") {
///             Verdict::Continue
///         } else {
///             Verdict::Refuse(Refusal::new(415, "example.avatar_not_image"))
///         }
///     }
/// }
/// ```
pub trait Inspector {
    /// A part's headers were read and passed every rule. Called before any
    /// byte of the part reaches a sink.
    ///
    /// # Examples
    ///
    /// ```
    /// use form_net::{Inspector, PartHead, Refusal, Verdict};
    ///
    /// struct NoFiles;
    /// impl Inspector for NoFiles {
    ///     fn part_start(&mut self, head: &PartHead<'_>) -> Verdict {
    ///         if head.is_file() { Verdict::Refuse(Refusal::new(415, "app.no_files")) } else { Verdict::Continue }
    ///     }
    /// }
    ///
    /// # let ct = b"multipart/form-data; boundary=X";
    /// # let (limits, policy) = (form_net::Limits::DEFAULT, form_net::Policy::DEFAULT);
    /// let body = b"--X\r\nContent-Disposition: form-data; name=f; filename=a.exe\r\n\r\nMZ..\r\n--X--";
    /// let r = form_net::parse_multipart_inspected(ct, body, &limits, &policy, &mut NoFiles);
    /// assert_eq!(r.unwrap_err().code(), "app.no_files");
    /// ```
    fn part_start(&mut self, head: &PartHead<'_>) -> Verdict {
        let _ = head;
        Verdict::Continue
    }

    /// The first `min(sniff_bytes, part length)` bytes of a file part, as one
    /// contiguous slice however the input was split, before any `data` call
    /// for that part.
    ///
    /// # Examples
    ///
    /// ```
    /// use form_net::{Inspector, PartHead, Refusal, Verdict};
    ///
    /// struct NoPrograms;
    /// impl Inspector for NoPrograms {
    ///     fn sniff(&mut self, _head: &PartHead<'_>, first: &[u8]) -> Verdict {
    ///         if first.starts_with(b"MZ") { Verdict::Refuse(Refusal::new(415, "app.program")) } else { Verdict::Continue }
    ///     }
    /// }
    ///
    /// # let ct = b"multipart/form-data; boundary=X";
    /// # let (limits, policy) = (form_net::Limits::DEFAULT, form_net::Policy::DEFAULT);
    /// let body = b"--X\r\nContent-Disposition: form-data; name=f; filename=a.exe\r\n\r\nMZ..\r\n--X--";
    /// let r = form_net::parse_multipart_inspected(ct, body, &limits, &policy, &mut NoPrograms);
    /// assert_eq!(r.unwrap_err().status_hint(), 415);
    /// ```
    fn sniff(&mut self, head: &PartHead<'_>, first: &[u8]) -> Verdict {
        let _ = (head, first);
        Verdict::Continue
    }

    /// Bytes of part `part`'s data, text fields included.
    ///
    /// # Examples
    ///
    /// ```
    /// use form_net::{Inspector, Verdict};
    ///
    /// struct Bytes(usize);
    /// impl Inspector for Bytes {
    ///     fn data(&mut self, _part: u32, chunk: &[u8]) -> Verdict {
    ///         self.0 += chunk.len();
    ///         Verdict::Continue
    ///     }
    /// }
    ///
    /// # let ct = b"multipart/form-data; boundary=X";
    /// # let (limits, policy) = (form_net::Limits::DEFAULT, form_net::Policy::DEFAULT);
    /// let body = b"--X\r\nContent-Disposition: form-data; name=a\r\n\r\n12\r\n--X\r\nContent-Disposition: form-data; name=f; filename=a.txt\r\n\r\nabc\r\n--X--";
    /// let mut seen = Bytes(0);
    /// form_net::parse_multipart_inspected(ct, body, &limits, &policy, &mut seen)?;
    /// assert_eq!(seen.0, 5);
    /// # Ok::<(), form_net::FormError>(())
    /// ```
    fn data(&mut self, part: u32, chunk: &[u8]) -> Verdict {
        let _ = (part, chunk);
        Verdict::Continue
    }

    /// A complete text field (multipart) or a decoded pair (urlencoded): the
    /// value as bytes, so the inspector can apply its own charset rules.
    /// `Skip` leaves the field out of the result.
    ///
    /// # Examples
    ///
    /// ```
    /// use form_net::{Inspector, Limits, Policy, Verdict};
    ///
    /// /// Leaves debugging fields out of the result.
    /// struct DropDebug;
    /// impl Inspector for DropDebug {
    ///     fn field(&mut self, name: &str, _value: &[u8]) -> Verdict {
    ///         if name.starts_with("debug_") { Verdict::Skip } else { Verdict::Continue }
    ///     }
    /// }
    ///
    /// let form = form_net::parse_urlencoded_inspected(b"a=1&debug_sql=1", &Limits::DEFAULT, &Policy::DEFAULT, &mut DropDebug)?;
    /// assert_eq!((form.field("a"), form.field("debug_sql")), (Some("1"), None));
    /// # Ok::<(), form_net::FormError>(())
    /// ```
    fn field(&mut self, name: &str, value: &[u8]) -> Verdict {
        let _ = (name, value);
        Verdict::Continue
    }

    /// A part ended. `Skip` here is treated as `Continue`.
    ///
    /// # Examples
    ///
    /// ```
    /// use form_net::{Inspector, PartSummary, Verdict};
    ///
    /// struct Sizes(Vec<u64>);
    /// impl Inspector for Sizes {
    ///     fn part_end(&mut self, summary: &PartSummary) -> Verdict {
    ///         self.0.push(summary.bytes);
    ///         Verdict::Continue
    ///     }
    /// }
    ///
    /// # let ct = b"multipart/form-data; boundary=X";
    /// # let (limits, policy) = (form_net::Limits::DEFAULT, form_net::Policy::DEFAULT);
    /// let body = b"--X\r\nContent-Disposition: form-data; name=a\r\n\r\n12\r\n--X\r\nContent-Disposition: form-data; name=b\r\n\r\nabc\r\n--X--";
    /// let mut sizes = Sizes(Vec::new());
    /// form_net::parse_multipart_inspected(ct, body, &limits, &policy, &mut sizes)?;
    /// assert_eq!(sizes.0, [2, 3]);
    /// # Ok::<(), form_net::FormError>(())
    /// ```
    fn part_end(&mut self, summary: &PartSummary) -> Verdict {
        let _ = summary;
        Verdict::Continue
    }

    /// The body ended. `Skip` here is treated as `Continue`.
    ///
    /// # Examples
    ///
    /// ```
    /// use form_net::{Inspector, Limits, Policy, Refusal, Verdict};
    ///
    /// /// Refuses a body without an `email` field, once the whole body is read.
    /// struct NeedsEmail(bool);
    /// impl Inspector for NeedsEmail {
    ///     fn field(&mut self, name: &str, _value: &[u8]) -> Verdict {
    ///         self.0 |= name == "email";
    ///         Verdict::Continue
    ///     }
    ///     fn end(&mut self) -> Verdict {
    ///         if self.0 { Verdict::Continue } else { Verdict::Refuse(Refusal::new(422, "app.no_email")) }
    ///     }
    /// }
    ///
    /// let r = form_net::parse_urlencoded_inspected(b"name=a", &Limits::DEFAULT, &Policy::DEFAULT, &mut NeedsEmail(false));
    /// assert_eq!(r.unwrap_err().code(), "app.no_email");
    /// ```
    fn end(&mut self) -> Verdict {
        Verdict::Continue
    }

    /// The parse failed with `error` (for any reason, this inspector's
    /// refusal included). Called once; it cannot change the outcome. The
    /// hook for security signals: a stream of refusals from one client is a
    /// probe.
    ///
    /// # Examples
    ///
    /// ```
    /// use form_net::{FormError, Inspector, Limits, Policy};
    ///
    /// /// Keeps the code of the error, for a detection layer.
    /// struct LastError(Option<&'static str>);
    /// impl Inspector for LastError {
    ///     fn failed(&mut self, error: &FormError) {
    ///         self.0 = Some(error.code());
    ///     }
    /// }
    ///
    /// let mut log = LastError(None);
    /// let _ = form_net::parse_urlencoded_inspected(b"a=%", &Limits::DEFAULT, &Policy::DEFAULT, &mut log);
    /// assert_eq!(log.0, Some("encoding.invalid_percent"));
    /// ```
    fn failed(&mut self, error: &FormError) {
        let _ = error;
    }
}

impl<I: Inspector + ?Sized> Inspector for &mut I {
    fn part_start(&mut self, head: &PartHead<'_>) -> Verdict {
        (**self).part_start(head)
    }
    fn sniff(&mut self, head: &PartHead<'_>, first: &[u8]) -> Verdict {
        (**self).sniff(head, first)
    }
    fn data(&mut self, part: u32, chunk: &[u8]) -> Verdict {
        (**self).data(part, chunk)
    }
    fn field(&mut self, name: &str, value: &[u8]) -> Verdict {
        (**self).field(name, value)
    }
    fn part_end(&mut self, summary: &PartSummary) -> Verdict {
        (**self).part_end(summary)
    }
    fn end(&mut self) -> Verdict {
        (**self).end()
    }
    fn failed(&mut self, error: &FormError) {
        (**self).failed(error);
    }
}

/// Two inspectors in one pass, `self.0` first (D-088). A refusal from either
/// wins and the second is not asked; otherwise the part is skipped if either
/// says `Skip`. `failed` reaches both. Nest pairs for longer chains:
/// `(customs, (sentinel, app))`.
///
/// # Examples
///
/// ```
/// use form_net::{Inspector, Limits, Policy, Refusal, Verdict};
///
/// struct NoAdmin;
/// impl Inspector for NoAdmin {
///     fn field(&mut self, _name: &str, value: &[u8]) -> Verdict {
///         if value == b"admin" { Verdict::Refuse(Refusal::new(403, "app.admin")) } else { Verdict::Continue }
///     }
/// }
/// struct Count(u32);
/// impl Inspector for Count {
///     fn field(&mut self, _name: &str, _value: &[u8]) -> Verdict {
///         self.0 += 1;
///         Verdict::Continue
///     }
/// }
///
/// let mut chain = (Count(0), NoAdmin);
/// let r = form_net::parse_urlencoded_inspected(b"a=1&b=admin", &Limits::DEFAULT, &Policy::DEFAULT, &mut chain);
/// assert_eq!(r.unwrap_err().status_hint(), 403);
/// assert_eq!(chain.0 .0, 2);
/// ```
impl<A: Inspector, B: Inspector> Inspector for (A, B) {
    fn part_start(&mut self, head: &PartHead<'_>) -> Verdict {
        both(self.0.part_start(head), || self.1.part_start(head))
    }
    fn sniff(&mut self, head: &PartHead<'_>, first: &[u8]) -> Verdict {
        both(self.0.sniff(head, first), || self.1.sniff(head, first))
    }
    fn data(&mut self, part: u32, chunk: &[u8]) -> Verdict {
        both(self.0.data(part, chunk), || self.1.data(part, chunk))
    }
    fn field(&mut self, name: &str, value: &[u8]) -> Verdict {
        both(self.0.field(name, value), || self.1.field(name, value))
    }
    fn part_end(&mut self, summary: &PartSummary) -> Verdict {
        both(self.0.part_end(summary), || self.1.part_end(summary))
    }
    fn end(&mut self) -> Verdict {
        both(self.0.end(), || self.1.end())
    }
    fn failed(&mut self, error: &FormError) {
        self.0.failed(error);
        self.1.failed(error);
    }
}

/// Combines two verdicts: the first refusal wins, then any skip.
fn both(first: Verdict, second: impl FnOnce() -> Verdict) -> Verdict {
    if matches!(first, Verdict::Refuse(_)) {
        return first;
    }
    match second() {
        Verdict::Refuse(r) => Verdict::Refuse(r),
        Verdict::Skip => Verdict::Skip,
        _ => first,
    }
}

/// A boxed inspector, for a chain chosen at run time (D-088).
#[cfg(feature = "alloc")]
impl<I: Inspector + ?Sized> Inspector for alloc::boxed::Box<I> {
    fn part_start(&mut self, head: &PartHead<'_>) -> Verdict {
        (**self).part_start(head)
    }
    fn sniff(&mut self, head: &PartHead<'_>, first: &[u8]) -> Verdict {
        (**self).sniff(head, first)
    }
    fn data(&mut self, part: u32, chunk: &[u8]) -> Verdict {
        (**self).data(part, chunk)
    }
    fn field(&mut self, name: &str, value: &[u8]) -> Verdict {
        (**self).field(name, value)
    }
    fn part_end(&mut self, summary: &PartSummary) -> Verdict {
        (**self).part_end(summary)
    }
    fn end(&mut self) -> Verdict {
        (**self).end()
    }
    fn failed(&mut self, error: &FormError) {
        (**self).failed(error);
    }
}

/// An inspector that accepts everything; it compiles to nothing.
///
/// # Examples
///
/// ```
/// use form_net::{Limits, NoInspector, Policy};
///
/// let form = form_net::parse_urlencoded_inspected(b"a=1", &Limits::DEFAULT, &Policy::DEFAULT, &mut NoInspector)?;
/// assert_eq!(form.field("a"), Some("1"));
/// # Ok::<(), form_net::FormError>(())
/// ```
#[derive(Clone, Copy, Debug, Default)]
pub struct NoInspector;

impl Inspector for NoInspector {}

/// Verifies the anti-forgery token the moment its field ends, before any
/// later part starts: a forged upload is refused before its first file
/// opens a sink, so it costs no disk.
///
/// `verify` is called once, with the value of the text field named `name`
/// (byte for byte, as [`TokenField`](crate::TokenField) matches it); it
/// does the comparison (csrf-sec's `Csrf::verify`, constant time) and
/// returns `Err` with the refusal to report. The check refuses, with
/// status 403:
///
/// - `csrf.missing`: a file part starts before a token was verified, or the
///   body ends without one;
/// - `csrf.duplicate`: a second field with the token's name (two tokens
///   would let an attacker choose which one is verified);
/// - `csrf.not_text`: a token that is not UTF-8;
/// - whatever `verify` returns.
///
/// Another inspector in the pair must not skip the token's part: the check
/// never sees a value that was not read, so it refuses the body with
/// `csrf.token_skipped` and status 500, a configuration error to fix in the
/// other inspector, not a client's.
///
/// It works for multipart and urlencoded bodies alike, and composes with
/// other inspectors as a pair: `(customs, check)`. Browsers send fields in
/// document order, so the form puts the hidden token input before its file
/// inputs. With `TokenField::anywhere`, where files may come first, verify
/// [`captured_token`](crate::MultipartParser::captured_token) (or the
/// upload's) after the parse instead. The token field stays in the result; leave it out of a typed
/// form with `ignore`.
///
/// Cost: one name comparison per text field, and `verify` once.
///
/// # Examples
///
/// ```
/// use form_net::{Limits, Policy, Refusal, TokenCheck};
///
/// # struct Csrf;
/// # impl Csrf { fn verify(&self, _session: &str, token: &str) -> bool { token == "good" } }
/// # let (csrf, session) = (Csrf, "session-id");
/// let mut check = TokenCheck::new("csrf_token", |token| {
///     if csrf.verify(session, token) { Ok(()) } else { Err(Refusal::new(403, "csrf.rejected")) }
/// });
/// let body = b"csrf_token=good&email=a%40b.example";
/// let form = form_net::parse_urlencoded_inspected(body, &Limits::DEFAULT, &Policy::DEFAULT, &mut check)?;
/// assert!(check.verified());
/// assert_eq!(form.field("email"), Some("a@b.example"));
///
/// let mut check = TokenCheck::new("csrf_token", |_| Err(Refusal::new(403, "csrf.rejected")));
/// let err = form_net::parse_urlencoded_inspected(b"csrf_token=bad", &Limits::DEFAULT, &Policy::DEFAULT, &mut check)
///     .unwrap_err();
/// assert_eq!((err.status_hint(), err.code()), (403, "csrf.rejected"));
/// # Ok::<(), form_net::FormError>(())
/// ```
pub struct TokenCheck<V> {
    name: &'static str,
    verify: V,
    seen: bool,
    verified: bool,
    /// The token's part started and its value has not come yet.
    pending: bool,
}

impl<V: FnMut(&str) -> Result<(), Refusal>> TokenCheck<V> {
    /// A check of the field `name` with `verify`.
    ///
    /// # Examples
    ///
    /// ```
    /// use form_net::{Limits, Policy, Refusal, TokenCheck};
    ///
    /// let mut check = TokenCheck::new("csrf_token", |token| {
    ///     if token == "expected" { Ok(()) } else { Err(Refusal::new(403, "csrf.rejected")) }
    /// });
    /// let body = b"csrf_token=forged&amount=100";
    /// let err = form_net::parse_urlencoded_inspected(body, &Limits::DEFAULT, &Policy::DEFAULT, &mut check).unwrap_err();
    /// assert_eq!(err.code(), "csrf.rejected");
    /// ```
    #[must_use]
    pub const fn new(name: &'static str, verify: V) -> TokenCheck<V> {
        TokenCheck {
            name,
            verify,
            seen: false,
            verified: false,
            pending: false,
        }
    }

    /// Whether the token was seen and `verify` accepted it.
    ///
    /// # Examples
    ///
    /// ```
    /// use form_net::{Limits, Policy, TokenCheck};
    ///
    /// let mut check = TokenCheck::new("csrf_token", |_token| Ok(()));
    /// assert!(!check.verified());
    /// form_net::parse_urlencoded_inspected(b"csrf_token=t", &Limits::DEFAULT, &Policy::DEFAULT, &mut check)?;
    /// assert!(check.verified());
    ///
    /// # Ok::<(), form_net::FormError>(())
    /// ```
    #[must_use]
    pub const fn verified(&self) -> bool {
        self.verified
    }
}

impl<V> core::fmt::Debug for TokenCheck<V> {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        f.debug_struct("TokenCheck")
            .field("name", &self.name)
            .field("seen", &self.seen)
            .field("verified", &self.verified)
            .finish_non_exhaustive()
    }
}

const TOKEN_MISSING: Refusal = Refusal::new(403, "csrf.missing");
const TOKEN_DUPLICATE: Refusal = Refusal::new(403, "csrf.duplicate");
const TOKEN_NOT_TEXT: Refusal = Refusal::new(403, "csrf.not_text");
const TOKEN_SKIPPED: Refusal = Refusal::new(500, "csrf.token_skipped");

impl<V: FnMut(&str) -> Result<(), Refusal>> Inspector for TokenCheck<V> {
    fn part_start(&mut self, head: &PartHead<'_>) -> Verdict {
        if head.is_file() && !self.verified {
            return Verdict::Refuse(TOKEN_MISSING);
        }
        self.pending = !head.is_file() && head.name() == self.name;
        Verdict::Continue
    }

    fn field(&mut self, name: &str, value: &[u8]) -> Verdict {
        if name != self.name {
            return Verdict::Continue;
        }
        self.pending = false;
        if self.seen {
            return Verdict::Refuse(TOKEN_DUPLICATE);
        }
        self.seen = true;
        let Ok(token) = core::str::from_utf8(value) else {
            return Verdict::Refuse(TOKEN_NOT_TEXT);
        };
        match (self.verify)(token) {
            Ok(()) => {
                self.verified = true;
                Verdict::Continue
            }
            Err(refusal) => Verdict::Refuse(refusal),
        }
    }

    fn part_end(&mut self, _summary: &PartSummary) -> Verdict {
        // The token's part ended without its value reaching `field`:
        // another inspector skipped it.
        if core::mem::take(&mut self.pending) {
            return Verdict::Refuse(TOKEN_SKIPPED);
        }
        Verdict::Continue
    }

    fn end(&mut self) -> Verdict {
        if self.verified {
            Verdict::Continue
        } else {
            Verdict::Refuse(TOKEN_MISSING)
        }
    }
}

/// Where file parts go: one part at a time, `open`, any number of `write`,
/// then `close` or `abort`.
///
/// The [`Driver`](crate::Driver) opens a sink only after the inspector has
/// accepted the part's head and sniff window and the first chunk of data
/// (or at the end of an empty file), so a refused or skipped part never
/// reaches storage. Budgets are enforced before `write` is called, so a sink
/// never has to count. `abort` must release everything, cannot fail, and
/// must do nothing when no part is open; drivers call it on every error
/// path.
///
/// # Examples
///
/// ```
/// use form_net::{FormError, PartHead, Sink};
///
/// /// Counts bytes per file without keeping them.
/// struct Counter(u64);
///
/// impl Sink for Counter {
///     type Output = u64;
///     fn open(&mut self, _head: &PartHead<'_>) -> Result<(), FormError> {
///         self.0 = 0;
///         Ok(())
///     }
///     fn write(&mut self, chunk: &[u8]) -> Result<(), FormError> {
///         self.0 += chunk.len() as u64;
///         Ok(())
///     }
///     fn close(&mut self) -> Result<u64, FormError> {
///         Ok(self.0)
///     }
///     fn abort(&mut self) {}
/// }
/// ```
pub trait Sink {
    /// What a closed part becomes.
    type Output;

    /// A file part starts.
    ///
    /// # Errors
    ///
    /// The sink's own errors (storage, I/O).
    ///
    /// # Examples
    ///
    /// ```
    /// use form_net::{MemorySink, OwnedPartHead, Sink};
    ///
    /// let head = OwnedPartHead::new();
    /// let mut sink = MemorySink::new();
    /// sink.open(&head.view())?;
    /// sink.write(b"hi")?;
    /// assert_eq!(sink.close()?.bytes(), b"hi");
    ///
    /// # Ok::<(), form_net::FormError>(())
    /// ```
    fn open(&mut self, head: &PartHead<'_>) -> Result<(), FormError>;

    /// More bytes of the current file part.
    ///
    /// # Errors
    ///
    /// The sink's own errors.
    ///
    /// # Examples
    ///
    /// ```
    /// use form_net::{DiscardSink, OwnedPartHead, Sink};
    ///
    /// let mut sink = DiscardSink::default();
    /// sink.open(&OwnedPartHead::new().view())?;
    /// sink.write(b"first ")?;
    /// sink.write(b"second")?;
    /// assert_eq!(sink.close()?, 12);
    ///
    /// # Ok::<(), form_net::FormError>(())
    /// ```
    fn write(&mut self, chunk: &[u8]) -> Result<(), FormError>;

    /// The current file part ended.
    ///
    /// # Errors
    ///
    /// The sink's own errors.
    ///
    /// # Examples
    ///
    /// ```
    /// use form_net::{MemorySink, OwnedPartHead, Sink};
    ///
    /// let mut sink = MemorySink::new();
    /// sink.open(&OwnedPartHead::new().view())?;
    /// sink.write(b"data")?;
    /// let file = sink.close()?;
    /// assert_eq!(file.len(), 4);
    ///
    /// # Ok::<(), form_net::FormError>(())
    /// ```
    fn close(&mut self) -> Result<Self::Output, FormError>;

    /// Discards the current file part, if any, and releases everything.
    ///
    /// # Examples
    ///
    /// ```
    /// use form_net::{MemorySink, OwnedPartHead, Sink};
    ///
    /// let mut sink = MemorySink::new();
    /// sink.open(&OwnedPartHead::new().view())?;
    /// sink.write(b"partial")?;
    /// sink.abort();
    /// // Nothing of the aborted part is kept.
    /// assert!(sink.close()?.is_empty());
    ///
    /// # Ok::<(), form_net::FormError>(())
    /// ```
    fn abort(&mut self);
}

/// A sink that discards file data and reports how many bytes it saw.
///
/// # Examples
///
/// ```
/// use form_net::{DiscardSink, Sink};
///
/// let mut s = DiscardSink::default();
/// s.write(b"abc")?;
/// assert_eq!(s.close()?, 3);
/// # Ok::<(), form_net::FormError>(())
/// ```
#[derive(Clone, Copy, Debug, Default)]
pub struct DiscardSink {
    bytes: u64,
}

impl Sink for DiscardSink {
    type Output = u64;

    fn open(&mut self, _head: &PartHead<'_>) -> Result<(), FormError> {
        self.bytes = 0;
        Ok(())
    }

    fn write(&mut self, chunk: &[u8]) -> Result<(), FormError> {
        self.bytes = self.bytes.saturating_add(chunk.len() as u64);
        Ok(())
    }

    fn close(&mut self) -> Result<u64, FormError> {
        Ok(core::mem::take(&mut self.bytes))
    }

    fn abort(&mut self) {
        self.bytes = 0;
    }
}

#[cfg(feature = "alloc")]
mod memory {
    use alloc::string::String;
    use alloc::vec::Vec;

    use super::Sink;
    use crate::error::FormError;
    use crate::extract::FileData;
    use crate::multipart::parser::PartHead;

    /// A sink that keeps each file in memory as a [`FileData`].
    ///
    /// # Examples
    ///
    /// ```
    /// use form_net::{Driver, MemorySink, MultipartParser, Boundary, Limits, Policy, NoInspector, Progress};
    ///
    /// let parser = MultipartParser::new(Boundary::new(b"X")?, &Limits::DEFAULT, &Policy::DEFAULT)?;
    /// let mut driver = Driver::new(parser, MemorySink::new());
    /// let body = b"--X\r\nContent-Disposition: form-data; name=f; filename=a.txt\r\n\r\nhi\r\n--X--";
    /// let (_, progress) = driver.push(body, true, &mut NoInspector)?;
    /// match progress {
    ///     Progress::File(file) => assert_eq!(file.bytes(), b"hi"),
    ///     _ => unreachable!(),
    /// }
    /// # Ok::<(), form_net::FormError>(())
    /// ```
    #[derive(Debug, Default)]
    pub struct MemorySink {
        current: Option<FileData>,
    }

    impl MemorySink {
        /// An empty sink.
        ///
        /// # Examples
        ///
        /// ```
        /// use form_net::{MemorySink, OwnedPartHead, Sink};
        ///
        /// let mut sink = MemorySink::new();
        /// sink.open(&OwnedPartHead::new().view())?;
        /// sink.write(b"hi")?;
        /// assert_eq!(sink.close()?.bytes(), b"hi");
        ///
        /// # Ok::<(), form_net::FormError>(())
        /// ```
        #[must_use]
        pub fn new() -> MemorySink {
            MemorySink { current: None }
        }
    }

    impl Sink for MemorySink {
        type Output = FileData;

        fn open(&mut self, head: &PartHead<'_>) -> Result<(), FormError> {
            // Text only when it is text: a file name that is not UTF-8 is
            // reported as absent rather than repaired.
            let filename = head
                .filename()
                .and_then(|f| core::str::from_utf8(f.raw()).ok().map(String::from));
            let content_type = head
                .content_type()
                .and_then(|c| core::str::from_utf8(c.raw()).ok().map(String::from));
            self.current = Some(FileData::new(Vec::new(), filename, content_type));
            Ok(())
        }

        fn write(&mut self, chunk: &[u8]) -> Result<(), FormError> {
            if let Some(f) = self.current.as_mut() {
                f.append(chunk);
            }
            Ok(())
        }

        fn close(&mut self) -> Result<FileData, FormError> {
            Ok(self.current.take().unwrap_or_default())
        }

        fn abort(&mut self) {
            self.current = None;
        }
    }
}

#[cfg(feature = "alloc")]
pub use memory::MemorySink;