/src/serenity/Userland/Libraries/LibWeb/MimeSniff/Resource.h
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright (c) 2023, Kemal Zebari <kemalzebra@gmail.com>. |
3 | | * |
4 | | * SPDX-License-Identifier: BSD-2-Clause |
5 | | */ |
6 | | |
7 | | #pragma once |
8 | | |
9 | | #include <LibWeb/MimeSniff/MimeType.h> |
10 | | |
11 | | namespace Web::MimeSniff { |
12 | | |
13 | | enum class SniffingContext { |
14 | | None, |
15 | | Browsing, |
16 | | Image, |
17 | | AudioOrVideo, |
18 | | Font, |
19 | | |
20 | | // Non-standard but used in cases where the spec expects us to only run |
21 | | // https://mimesniff.spec.whatwg.org/#sniffing-a-mislabeled-binary-resource |
22 | | TextOrBinary, |
23 | | }; |
24 | | |
25 | | struct SniffingConfiguration { |
26 | | SniffingContext sniffing_context { SniffingContext::None }; |
27 | | StringView scheme { ""sv }; |
28 | | Optional<MimeType> supplied_type = {}; |
29 | | bool no_sniff { false }; |
30 | | }; |
31 | | |
32 | | // https://mimesniff.spec.whatwg.org/#resource |
33 | | class Resource { |
34 | | public: |
35 | | static Resource create(ReadonlyBytes data, SniffingConfiguration configuration = {}); |
36 | | static MimeType sniff(ReadonlyBytes data, SniffingConfiguration configuration = {}); |
37 | | |
38 | | ~Resource(); |
39 | | |
40 | 0 | MimeType const& computed_mime_type() const { return m_computed_mime_type; } |
41 | 0 | ReadonlyBytes resource_header() const { return m_resource_header; } |
42 | | |
43 | | private: |
44 | | Resource(ReadonlyBytes data, bool no_sniff, MimeType&& default_computed_mime_type); |
45 | | |
46 | | void read_the_resource_header(ReadonlyBytes data); |
47 | | void supplied_mime_type_detection_algorithm(StringView scheme, Optional<MimeType> supplied_type); |
48 | | void mime_type_sniffing_algorithm(); |
49 | | |
50 | | void rules_for_distinguishing_if_a_resource_is_text_or_binary(); |
51 | | |
52 | | void context_specific_sniffing_algorithm(SniffingContext sniffing_context); |
53 | | void rules_for_sniffing_images_specifically(); |
54 | | void rules_for_sniffing_audio_or_video_specifically(); |
55 | | void rules_for_sniffing_fonts_specifically(); |
56 | | |
57 | | // https://mimesniff.spec.whatwg.org/#supplied-mime-type |
58 | | // A supplied MIME type, the MIME type determined by the supplied MIME type detection algorithm. |
59 | | Optional<MimeType> m_supplied_mime_type; |
60 | | |
61 | | // https://mimesniff.spec.whatwg.org/#check-for-apache-bug-flag |
62 | | // A check-for-apache-bug flag, which defaults to unset. |
63 | | bool m_check_for_apache_bug_flag { false }; |
64 | | |
65 | | // https://mimesniff.spec.whatwg.org/#no-sniff-flag |
66 | | // A no-sniff flag, which defaults to set if the user agent does not wish to perform sniffing on the resource and unset otherwise. |
67 | | bool m_no_sniff { false }; |
68 | | |
69 | | // https://mimesniff.spec.whatwg.org/#computed-mime-type |
70 | | // A computed MIME type, the MIME type determined by the MIME type sniffing algorithm. |
71 | | MimeType m_computed_mime_type; |
72 | | |
73 | | // https://mimesniff.spec.whatwg.org/#resource-header |
74 | | // A resource header is the byte sequence at the beginning of a resource, as determined by reading the resource header. |
75 | | ByteBuffer m_resource_header; |
76 | | }; |
77 | | |
78 | | } |