Skip to main content

UrlencodedParser

Struct UrlencodedParser 

Source
pub struct UrlencodedParser<B: AsMut<[u8]> + AsRef<[u8]>> { /* private fields */ }
Expand description

A streaming urlencoded parser over a caller scratch buffer.

A sequence that ends inside the current chunk is returned borrowed from the input; one that straddles chunks is assembled in the scratch buffer (Limits::urlencoded_buffer_len bytes) and returned borrowed from it. A sequence too long for its budgets is refused as soon as its first bytes prove it, before it is copied, so the scratch never overflows. Pairs and errors are the same for every split of the same body.

§Examples

use form_net::{Limits, UrlencodedEvent, UrlencodedParser};

// A small route: the scratch fits on the stack.
const LIMITS: Limits = Limits::DEFAULT.with_max_name_bytes(32).with_max_value_bytes(256);
let mut scratch = [0u8; LIMITS.urlencoded_buffer_len()];
let mut parser = UrlencodedParser::with_buffer(&LIMITS, &mut scratch[..])?;
let mut names = Vec::new();
for (chunk, last) in [(&b"na"[..], false), (b"me=Al&ci", false), (b"ty=Paris", true)] {
    let mut rest = chunk;
    loop {
        let step = parser.parse(rest, last)?;
        rest = &rest[step.consumed..];
        match step.event {
            UrlencodedEvent::Pair(p) => names.push(p.name_raw().to_vec()),
            _ => break,
        }
    }
}
assert_eq!(names, [b"name".to_vec(), b"city".to_vec()]);

Implementations§

Source§

impl<B: AsMut<[u8]> + AsRef<[u8]>> UrlencodedParser<B>

Source

pub fn with_buffer(limits: &Limits, scratch: B) -> Result<Self, FormError>

A parser over scratch, which must hold at least Limits::urlencoded_buffer_len bytes.

§Errors

InvalidLimits; BufferTooSmall { needed }.

Source

pub fn parse<'p>( &'p mut self, input: &'p [u8], eof: bool, ) -> Result<UrlencodedStep<'p>, FormError>

Feeds input (eof = true says it ends the body) and returns the next pair, a request for more input, or the end.

§Errors

The errors of raw_pairs; Poisoned after an error until reset.

Source

pub fn reset(&mut self)

Clears the counters and the poisoned state and wipes the scratch, for the next body.

Source

pub const fn bytes_seen(&self) -> u64

Body bytes consumed so far.

Source§

impl UrlencodedParser<Vec<u8>>

Source

pub fn new(limits: &Limits) -> Result<Self, FormError>

Available on crate feature alloc only.

A parser that owns its scratch buffer.

§Errors

InvalidLimits.

§Examples
use form_net::{Limits, UrlencodedEvent, UrlencodedParser};

let mut parser = UrlencodedParser::new(&Limits::DEFAULT)?;
let step = parser.parse(b"a=1", true)?;
assert!(matches!(step.event, UrlencodedEvent::Pair(p) if p.value_raw() == b"1"));

Trait Implementations§

Source§

impl<B: AsMut<[u8]> + AsRef<[u8]>> Debug for UrlencodedParser<B>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<B: AsMut<[u8]> + AsRef<[u8]>> Drop for UrlencodedParser<B>

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

fn pin_drop(self: Pin<&mut Self>)

🔬This is a nightly-only experimental API. (pin_ergonomics)
Execute the destructor for this type, but different to Drop::drop, it requires self to be pinned. Read more

Auto Trait Implementations§

§

impl<B> Freeze for UrlencodedParser<B>
where B: Freeze,

§

impl<B> RefUnwindSafe for UrlencodedParser<B>
where B: RefUnwindSafe,

§

impl<B> Send for UrlencodedParser<B>
where B: Send,

§

impl<B> Sync for UrlencodedParser<B>
where B: Sync,

§

impl<B> Unpin for UrlencodedParser<B>
where B: Unpin,

§

impl<B> UnsafeUnpin for UrlencodedParser<B>
where B: UnsafeUnpin,

§

impl<B> UnwindSafe for UrlencodedParser<B>
where B: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.