Skip to content

buckyos/rust-mime-sniffer

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

17 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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!(req.sniff_mime_type(), Some("application/vnd.ms-powerpoint"));
assert_eq!(req.sniff_mime_type_ext().unwrap().type_(), mime::APPLICATION);

Related

  • 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

About

Detecting mime types base on content sniffer

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Rust 100.0%