/uhttp_media_type.rs

HTTP MIME/media type parser in Rust

Primary LanguageRustMIT LicenseMIT

uhttp_media_type -- HTTP MIME/media type parser

Documentation

This crate provides a zero-allocation, slice-based parser for HTTP Media Types as they appear in Content-Type and Accept headers.

Example

use uhttp_media_type::{MediaType, MediaParams, ParamValue};

let mt = MediaType::new("application/json; charset=utf-8; param=\"a value\"").unwrap();
assert_eq!(mt.mimetype, "application/json");
assert_eq!(mt.parts().unwrap(), ("application", "json"));
assert_eq!(mt.params, " charset=utf-8; param=\"a value\"");

let mut params = MediaParams::new(mt.params);

let (key, val) = params.next().unwrap().unwrap();
assert_eq!(key, "charset");
assert_eq!(val, ParamValue::Unquoted("utf-8"));
assert_eq!(val.inner(), "utf-8");

let (key, val) = params.next().unwrap().unwrap();
assert_eq!(key, "param");
assert_eq!(val, ParamValue::Quoted("a value"));
assert_eq!(val.inner(), "a value");

assert!(params.next().is_none());

Usage

This crate can be used through cargo by adding it as a dependency in Cargo.toml:

[dependencies]
uhttp_media_type = "0.5.0"

and importing it in the crate root:

extern crate uhttp_media_type;