#mime #classifier #sniffer #media-type #guesser

mime_classifier

Servo的MIME分类器/媒体类型嗅探器,作为一个Rust库

1个不稳定版本

0.0.1 2022年4月11日

#18#sniffer

Download history 116/week @ 2024-03-13 370/week @ 2024-03-20 412/week @ 2024-03-27 104/week @ 2024-04-03 13/week @ 2024-04-10 43/week @ 2024-04-17 34/week @ 2024-04-24 177/week @ 2024-05-01 23/week @ 2024-05-08 126/week @ 2024-05-15 50/week @ 2024-05-22 52/week @ 2024-05-29 21/week @ 2024-06-05 61/week @ 2024-06-12 283/week @ 2024-06-19 305/week @ 2024-06-26

673 每月下载量
3 个crate中使用 (通过 iroh-gateway)

MPL-2.0 许可证

41KB
1K SLoC

MIME分类器/媒体类型嗅探器

此crate公开了来自Servo网络引擎的MIME分类器作为一个独立库。

它实现了WHATWG MIME嗅探标准,用于猜测资源的媒体类型(也称为MIME类型),从其内容中猜测。它使得浏览器即使在Content-Type头部缺失或无效的情况下也能正确解释服务器响应。

该库的当前版本是在2022-04-11日期从提交8d684eff7d6f8815422cb4c30b43df0035c5069a中提取的。如果您是Servo项目的成员并希望自行维护此库,我很乐意将crate的所有权转让给您:请打开一个GitHub问题或发送电子邮件给我。

用法

use mime_classifier::{ApacheBugFlag, LoadContext, MimeClassifier, NoSniffFlag};

pub fn main() {
    // Create a classifier using default configuration
    let classifier = MimeClassifier::new();
    // Select the context, this is used to help the classifier based on where
    // the resource is loaded from. `Browsing` corresponds to simply typing
    // the URL in the address bar.
    let context = LoadContext::Browsing;
    // Flag indicating that sniffing should be avoided. This usually corresponds
    // to the server sending the header value `X-Content-Type-Options = "nosniff"`
    // but may also be applied automatically by the browser (e.g. `fetch` API)
    let no_sniff_flag = NoSniffFlag::Off;
    // Enable workaround for an Apache bug when server incorrectly sends a
    // `text/plain` or similar `Content-Type`.
    // See <https://mimesniff.spec.whatwg.org/#ref-for-check-for-apache-bug-flag>
    let apache_bug_flag = ApacheBugFlag::Off;
    // `Content-Type` set by the server, if any
    let supplied_type: Option<mime::Mime> = None;
    // Response body to classify
    let body: &[u8] = include_bytes!("../servo_logo.png");

    let computed_type = classifier.classify(context, no_sniff_flag, apache_bug_flag, &supplied_type, body);
    assert_eq!(computed_type, mime::IMAGE_PNG);
}

许可证

此crate中的代码保留了Servo项目原始的Mozilla公共许可证,版本2.0 (MPL-2.0)许可证。

依赖关系

~0.5–1MB
~25K SLoC