3 releases

Uses old Rust 2015

0.1.2 Jan 12, 2018
0.1.1 Jan 6, 2017
0.1.0 Jan 4, 2017

#6 in #sniffer

Download history 1515/week @ 2024-07-20 1515/week @ 2024-07-27 1944/week @ 2024-08-03 1182/week @ 2024-08-10 1029/week @ 2024-08-17 1578/week @ 2024-08-24 1850/week @ 2024-08-31 1980/week @ 2024-09-07 1982/week @ 2024-09-14 1621/week @ 2024-09-21 1609/week @ 2024-09-28 674/week @ 2024-10-05 1287/week @ 2024-10-12 2205/week @ 2024-10-19 1558/week @ 2024-10-26 1448/week @ 2024-11-02

6,623 downloads per month
Used in 7 crates (6 directly)

Apache-2.0

42KB
643 lines

mime-sniffer travis build crate

Detecting mime types base on content sniffer.

Document

The detection workflow was copied from Chromium

For more detail, please read How Mozilla determines MIME Types.

Usage

To use mime-sniffer, first add this to your Cargo.toml:

[dependencies]
mime_sniffer = "^0.1"

Then, add this to your crate root:

extern crate mime_sniffer;

use mime_sniffer::MimeTypeSniffer;

And then, use hash function with module or hasher

use mime_sniffer::MimeTypeSniffer;

assert_eq!(Some("application/pdf"), b"%PDF-1.5".sniff_mime_type());

Examples

extern crate url;
#[macro_use]
extern crate mime;
extern crate mime_sniffer;

use url::Url;

use mime_sniffer::{HttpRequest, MimeTypeSniffer, MimeTypeSniffable, MimeTypeSnifferExt};

let url = Url::parse("http://localhost/notes.ppt").unwrap();
let req = HttpRequest {
    content: b"\xD0\xCF\x11\xE0\xA1\xB1\x1A\xE1",
    url: &url,
    type_hint: "text/plain",
};

assert!(req.should_sniff_mime_type());
assert_eq!(Some("application/vnd.ms-powerpoint"), req.sniff_mime_type());
assert_eq!(mime!(Application/("vnd.ms-powerpoint")),
           req.sniff_mime_type_ext().unwrap());
  • To recognize binary file type, you may need libmagic with rust binding rust-magic crate. crate
  • To guess MIME type by file extension, you may need mime_guess crate. crate
  • To manage MIME type as strong types, you may need mime.rs crate. crate

Dependencies

~1.5MB
~45K SLoC