#media #media-type #type #http #mime #mimetype

nightly uhttp_media_type

Zero-allocation HTTP MIME/media type parser

1 unstable release

Uses old Rust 2015

0.5.0 Jan 29, 2017

#22 in #media-type


Used in uhttp_json_api

MIT license

22KB
179 lines

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;

Dependencies

~620KB