-
Notifications
You must be signed in to change notification settings - Fork 2
/
__init__.py
183 lines (135 loc) · 5.28 KB
/
__init__.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
__version__ = "0.0.0"
from typing import Optional, Set, Tuple
from xtractmime._utils import (
contains_binary,
get_archive_mime,
get_audio_video_mime,
get_extra_mime,
get_font_mime,
get_image_mime,
get_text_mime,
)
_APACHE_TYPES = [
b"text/plain",
b"text/plain; charset=ISO-8859-1",
b"text/plain; charset=iso-8859-1",
b"text/plain; charset=UTF-8",
]
WHITESPACE_BYTES = {b"\t", b"\r", b"\x0c", b"\n", b" "}
def _is_match_mime_pattern(
input_bytes: bytes, byte_pattern: bytes, pattern_mask: bytes, lstrip: Set[bytes] = None
) -> bool:
input_size = len(input_bytes)
pattern_size = len(byte_pattern)
mask_size = len(pattern_mask)
if pattern_size != mask_size:
raise ValueError("pattern's length should match mask's length")
if input_size < pattern_size:
return False
input_index, pattern_index = 0, 0
if lstrip:
while input_index < input_size and input_bytes[input_index : input_index + 1] in lstrip:
input_index += 1
while pattern_index < pattern_size:
masked_byte = bytes([input_bytes[input_index] & pattern_mask[pattern_index]])
if masked_byte != byte_pattern[pattern_index : pattern_index + 1]:
return False
input_index += 1
pattern_index += 1
return True
def _find_unknown_mimetype(input_bytes: bytes, sniff_scriptable: bool, extra_types: Optional[Tuple[Tuple[bytes, bytes, Set[bytes], str], ...]]) -> str:
if sniff_scriptable:
return get_text_mime(input_bytes)
matched_type = get_extra_mime(input_bytes, extra_types=extra_types)
if matched_type:
return matched_type
matched_type = get_image_mime(input_bytes)
if matched_type:
return matched_type
matched_type = get_audio_video_mime(input_bytes)
if matched_type:
return matched_type
matched_type = get_archive_mime(input_bytes)
if matched_type:
return matched_type
if not contains_binary(input_bytes):
return "text/plain"
return "application/octet-stream"
def _sniff_mislabled_binary(input_bytes: bytes) -> str:
input_size = len(input_bytes)
if input_size >= 2 and input_bytes.startswith((b"\xfe\xff",b"\xff\xfe",b"\xef\xbb\xbf")):
return "text/plain"
if not contains_binary(input_bytes):
return "text/plain"
return "application/octet-stream"
def _sniff_mislabled_feed(input_bytes: bytes, supplied_type: Optional[Tuple[bytes]]) -> str:
input_size = len(input_bytes)
index = 0
if input_bytes[:3] == b"\xef\xbb\xbf":
index += 3
while index < input_size:
while True:
if not input_bytes[index:index+1]:
return supplied_type
if input_bytes[index:index+1] == b"<":
index += 1
break
if input_bytes[index:index+1] not in WHITESPACE_BYTES:
return supplied_type
index += 1
while True:
if not input_bytes[index:index+1]:
return supplied_type
if input_bytes[index:index+3] == b"!--":
index += 3
while True:
if not input_bytes[index:index+1]:
return supplied_type
if input_bytes[index:index+3] == b"-->":
index += 3
return supplied_type
index += 1
if input_bytes[index:index+1] == b"!":
index += 1
while True:
if not input_bytes[index:index+1]:
return supplied_type
if input_bytes[index:index+3] == b">":
index += 1
return supplied_type
index += 1
# Completed Till Section 7.3 5.2.3.3
return ""
def extract_mime(
body: bytes,
*,
content_types: Optional[Tuple[bytes]] = None,
http_origin: bool = True,
no_sniff: bool = False,
extra_types: Optional[Tuple[Tuple[bytes, bytes, Set[bytes], str], ...]] = None,
supported_types: Set[str] = None,
) -> str:
extra_types = extra_types or tuple()
supplied_type = content_types[-1] if content_types else None
check_for_apache = http_origin and supplied_type in _APACHE_TYPES
resource_header = memoryview(body)[:1445]
if supplied_type in (None, "unknown/unknown", "application/unknown", "*/*"):
_find_unknown_mimetype(resource_header, not no_sniff, extra_types)
if no_sniff:
return supplied_type
if check_for_apache:
return _sniff_mislabled_binary(resource_header)
if supplied_type.endswith("+xml") or supplied_type in {"text/xml", "application/xml"}:
return supplied_type
if supplied_type.startswith("text/html"):
return _sniff_mislabled_feed(resource_header)
if supplied_type.startswith("image/"):
matched_type = get_image_mime(resource_header)
if matched_type in supported_types:
return matched_type
video_types = ("audio/","video/")
if supplied_type.startswith(video_types) or supplied_type.startswith("application/ogg"):
matched_type = get_audio_video_mime(resource_header)
if matched_type in supported_types:
return matched_type
return supplied_type