Function http_box::util::parse_field [] [src]

pub fn parse_field<T: FieldClosure>(field: &[u8],
                                    delimiter: u8,
                                    normalize: bool,
                                    field_fn: T)
                                    -> Result<usize, FieldError>

Parse the content of a header field.

Arguments

field

The field data to be parsed.

delimiter

The delimiting byte.

normalize

Indicates that field names should be normalized to lower-case.

field_fn

The FieldClosure implementation that receives instances of FieldSegment

Returns

usize

The amount of data that was parsed.

Errors

Examples

This is an example of a FieldClosure implementation that is a single closure that accepts instances of FieldSegment.

use http_box::util::FieldSegment;
use http_box::util;

util::parse_field(b"name-no-value; name1=value1; name2=\"value2\"", b';', true,
    |s: FieldSegment| {
        if s.has_value() {
            s.name();
            s.value().unwrap();
        } else {
            s.name();
        }

        true
    }
);

This is an example of a FieldClosure implementation that is a tuple of two closures. The first closure accepts the current field value byte, and returns a boolean indicating that the byte is valid. The second closure accepts the instance of FieldSegment.

You will notice that in the 'name2' value, there is a null byte. The validation closure checks for this, and since it returns false, parse_field() returns an error.

use http_box::util::{ FieldError, FieldSegment };
use http_box::util;

match util::parse_field(b"name-no-value; name1=value1; name2=\"value2\0\"", b';', true,
    ( // byte validation closure
     |b: u8| {
         b != b'\0'
     },
     // field segment closure
     |s: FieldSegment| {
         if s.has_value() {
             s.name();
             s.value().unwrap();
         } else {
             s.name();
         }

         true
     })
) {
    Err(FieldError::Value(b'\0')) => { },
    _ => panic!()
}