Debian Codegraph @

src/lib.rs

raw symbols SCIP
1 //! Processing of `data:` URLs according to the Fetch Standard:
2 //! <https://fetch.spec.whatwg.org/#data-urls>
3 //! but starting from a string rather than a parsed URL to avoid extra copies.
4 //!
5 //! ```rust
6 //! use data_url::{DataUrl, mime};
7 //!
8 //! let url = DataUrl::process("data:,Hello%20World!").unwrap();
9 //! let (body, fragment) = url.decode_to_vec().unwrap();
10 //!
11 //! assert!(url.mime_type().matches("text", "plain"));
12 //! assert_eq!(url.mime_type().get_parameter("charset"), Some("US-ASCII"));
13 //! assert_eq!(body, b"Hello World!");
14 //! assert!(fragment.is_none());
15 //! ```
16 #![no_std]
17
18 // For forwards compatibility
19 #[cfg(feature = "std")]
20 extern crate stdrust-analyzer cargo data-url 0.3.2 std/;
21
22 #[macro_use]
23 extern crate allocrust-analyzer cargo data-url 0.3.2 alloc/;
24
25 #[cfg(not(feature = "alloc"))]
26 compile_errorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/compile_error!!("the `alloc` feature must be enabled");
27
28 use allocrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc crate/::{stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/::Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#, vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/::Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#};
29 use corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::fmtrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/;
30
31 macro_rules! requirerust-analyzer cargo data-url 0.3.2 require! {
32 ($condition: expr) => {
33 if !$condition {
34 return None;
35 }
36 };
37 }
38
39 pub mod forgiving_base64rust-analyzer cargo data-url 0.3.2 forgiving_base64/;
40 pub mod mimerust-analyzer cargo data-url 0.3.2 mime/;
41
42 pub struct DataUrlrust-analyzer cargo data-url 0.3.2 DataUrl#<'a> {
43 mime_typerust-analyzer cargo data-url 0.3.2 DataUrl#mime_type.: mimerust-analyzer cargo data-url 0.3.2 mime/::Mimerust-analyzer cargo data-url 0.3.2 mime/Mime#,
44 base64rust-analyzer cargo data-url 0.3.2 DataUrl#base64.: bool,
45 encoded_body_plus_fragmentrust-analyzer cargo data-url 0.3.2 DataUrl#encoded_body_plus_fragment.: &'a str,
46 }
47
48 #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:)]
49 pub enum DataUrlErrorrust-analyzer cargo data-url 0.3.2 DataUrlError# {
50 NotADataUrlrust-analyzer cargo data-url 0.3.2 DataUrlError#NotADataUrl#,
51 NoCommarust-analyzer cargo data-url 0.3.2 DataUrlError#NoComma#,
52 }
53
54 impl fmtrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/::Displayrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Display# for DataUrlErrorrust-analyzer cargo data-url 0.3.2 DataUrlError# {
55 fn fmtrust-analyzer cargo data-url 0.3.2 impl#[DataUrlError][Display]fmt().(&self, f: &mut fmtrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/::Formatterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Formatter#<'_>) -> fmtrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/::Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Result# {
56 match self {
57 Selfrust-analyzer cargo data-url 0.3.2 impl#[DataUrlError][Display]::NotADataUrlrust-analyzer cargo data-url 0.3.2 DataUrlError#NotADataUrl# => writerust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/write!!(f, "not a valid data url"),
58 Selfrust-analyzer cargo data-url 0.3.2 impl#[DataUrlError][Display]::NoCommarust-analyzer cargo data-url 0.3.2 DataUrlError#NoComma# => writerust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/write!!(
59 f,
60 "data url is missing comma delimiting attributes and body"
61 ),
62 }
63 }
64 }
65
66 #[cfg(feature = "std")]
67 impl stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::errorrust-analyzer cargo std https://github.com/rust-lang/rust/library/std error/::Errorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core error/Error# for DataUrlErrorrust-analyzer cargo data-url 0.3.2 DataUrlError# {}
68
69 impl<'a> DataUrlrust-analyzer cargo data-url 0.3.2 DataUrl#<'a> {
70 /// <https://fetch.spec.whatwg.org/#data-url-processor>
71 /// but starting from a string rather than a parsed `Url`, to avoid extra string copies.
72 pub fn processrust-analyzer cargo data-url 0.3.2 impl#[`DataUrl<'a>`]process().(input: &'a str) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<Selfrust-analyzer cargo data-url 0.3.2 impl#[`DataUrl<'a>`], DataUrlErrorrust-analyzer cargo data-url 0.3.2 DataUrlError#> {
73 use craterust-analyzer cargo data-url 0.3.2 crate/::DataUrlErrorrust-analyzer cargo data-url 0.3.2 DataUrlError#::*;
74
75 let after_colon = pretend_parse_data_urlrust-analyzer cargo data-url 0.3.2 pretend_parse_data_url().(input).ok_orrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]ok_or().(NotADataUrlrust-analyzer cargo data-url 0.3.2 DataUrlError#NotADataUrl#)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
76
77 let (from_colon_to_comma, encoded_body_plus_fragment) =
78 find_comma_before_fragmentrust-analyzer cargo data-url 0.3.2 find_comma_before_fragment().(after_colon).ok_orrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]ok_or().(NoCommarust-analyzer cargo data-url 0.3.2 DataUrlError#NoComma#)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
79
80 let (mime_type, base64) = parse_headerrust-analyzer cargo data-url 0.3.2 parse_header().(from_colon_to_comma);
81
82 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(DataUrlrust-analyzer cargo data-url 0.3.2 DataUrl# {
83 mime_typerust-analyzer cargo data-url 0.3.2 DataUrl#mime_type.,
84 base64rust-analyzer cargo data-url 0.3.2 DataUrl#base64.,
85 encoded_body_plus_fragmentrust-analyzer cargo data-url 0.3.2 DataUrl#encoded_body_plus_fragment.,
86 })
87 }
88
89 pub fn mime_typerust-analyzer cargo data-url 0.3.2 impl#[`DataUrl<'a>`]mime_type().(&self) -> &mimerust-analyzer cargo data-url 0.3.2 mime/::Mimerust-analyzer cargo data-url 0.3.2 mime/Mime# {
90 &self.mime_typerust-analyzer cargo data-url 0.3.2 DataUrl#mime_type.
91 }
92
93 /// Streaming-decode the data URL’s body to `write_body_bytes`,
94 /// and return the URL’s fragment identifier if it has one.
95 pub fn decoderust-analyzer cargo data-url 0.3.2 impl#[`DataUrl<'a>`]decode().<F, E>(
96 &self,
97 write_body_bytes: F,
98 ) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<FragmentIdentifierrust-analyzer cargo data-url 0.3.2 FragmentIdentifier#<'a>>, forgiving_base64rust-analyzer cargo data-url 0.3.2 forgiving_base64/::DecodeErrorrust-analyzer cargo data-url 0.3.2 forgiving_base64/DecodeError#<E>>
99 where
100 F: FnMutrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/function/FnMut#(&[u8]) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<(), E>,
101 {
102 if self.base64rust-analyzer cargo data-url 0.3.2 DataUrl#base64. {
103 decode_with_base64rust-analyzer cargo data-url 0.3.2 decode_with_base64().(self.encoded_body_plus_fragmentrust-analyzer cargo data-url 0.3.2 DataUrl#encoded_body_plus_fragment., write_body_bytes)
104 } else {
105 decode_without_base64rust-analyzer cargo data-url 0.3.2 decode_without_base64().(self.encoded_body_plus_fragmentrust-analyzer cargo data-url 0.3.2 DataUrl#encoded_body_plus_fragment., write_body_bytes)
106 .map_errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]map_err().(forgiving_base64rust-analyzer cargo data-url 0.3.2 forgiving_base64/::DecodeErrorrust-analyzer cargo data-url 0.3.2 forgiving_base64/DecodeError#::WriteErrorrust-analyzer cargo data-url 0.3.2 forgiving_base64/DecodeError#WriteError#)
107 }
108 }
109
110 /// Return the decoded body, and the URL’s fragment identifier if it has one.
111 pub fn decode_to_vecrust-analyzer cargo data-url 0.3.2 impl#[`DataUrl<'a>`]decode_to_vec().(
112 &self,
113 ) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<(Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<u8>, Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<FragmentIdentifierrust-analyzer cargo data-url 0.3.2 FragmentIdentifier#<'a>>), forgiving_base64rust-analyzer cargo data-url 0.3.2 forgiving_base64/::InvalidBase64rust-analyzer cargo data-url 0.3.2 forgiving_base64/InvalidBase64#> {
114 let mut body = Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#::newrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T>`]new().();
115 let fragment = self.decoderust-analyzer cargo data-url 0.3.2 impl#[`DataUrl<'a>`]decode().(|bytes| {
116 body.extend_from_slicerust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]extend_from_slice().(bytes);
117 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(())
118 })?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
119 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#((body, fragment))
120 }
121 }
122
123 /// The URL’s fragment identifier (after `#`)
124 pub struct FragmentIdentifierrust-analyzer cargo data-url 0.3.2 FragmentIdentifier#<'a>(&'a str);
125
126 impl FragmentIdentifierrust-analyzer cargo data-url 0.3.2 FragmentIdentifier#<'_> {
127 /// Like in a parsed URL
128 pub fn to_percent_encodedrust-analyzer cargo data-url 0.3.2 impl#[`FragmentIdentifier<'_>`]to_percent_encoded().(&self) -> Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String# {
129 let mut string = Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#::newrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]new().();
130 for byte in self.0rust-analyzer cargo data-url 0.3.2 FragmentIdentifier#0..bytesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]bytes().() {
131 match byte {
132 // Ignore ASCII tabs or newlines like the URL parser would
133 b'\t' | b'\n' | b'\r' => continue,
134 // https://url.spec.whatwg.org/#fragment-percent-encode-set
135 b'\0'..=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeInclusive#b' ' | b'"' | b'<' | b'>' | b'`' | b'\x7F'..=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeInclusive#b'\xFF' => {
136 percent_encoderust-analyzer cargo data-url 0.3.2 percent_encode().(byte, &mut string)
137 }
138 // Printable ASCII
139 _ => string.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]push().(byte as char),
140 }
141 }
142 string
143 }
144 }
145
146 /// Similar to <https://url.spec.whatwg.org/#concept-basic-url-parser>
147 /// followed by <https://url.spec.whatwg.org/#concept-url-serializer>
148 ///
149 /// * `None`: not a data URL.
150 ///
151 /// * `Some(s)`: sort of the result of serialization, except:
152 ///
153 /// - `data:` prefix removed
154 /// - The fragment is included
155 /// - Other components are **not** UTF-8 percent-encoded
156 /// - ASCII tabs and newlines in the middle are **not** removed
157 fn pretend_parse_data_urlrust-analyzer cargo data-url 0.3.2 pretend_parse_data_url().(input: &str) -> Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<&str> {
158 // Trim C0 control or space
159 let left_trimmed = input.trim_start_matchesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]trim_start_matches().(|ch| ch rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[char][`PartialOrd<Self>`]le().<=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[char][`PartialOrd<Self>`]le(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[char][`PartialOrd<Self>`]le().' ');
160
161 let mut bytes = left_trimmed.bytesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]bytes().();
162 {
163 // Ignore ASCII tabs or newlines like the URL parser would
164 let mut iter = bytes
165 .by_refrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#by_ref().()
166 .filterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#filter().(|&byte| !rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().matchesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/matches!!(byte, b'\t' | b'\n' | b'\r'));
167 requirerust-analyzer cargo data-url 0.3.2 require!!(iter.nextrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/adapters/filter/impl#[`Filter<I, P>`][Iterator]next().()?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`][Try]branch()..eq_ignore_ascii_caserust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/impl#[u8]eq_ignore_ascii_case().(&b'd'));
168 requirerust-analyzer cargo data-url 0.3.2 require!!(iter.nextrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/adapters/filter/impl#[`Filter<I, P>`][Iterator]next().()?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`][Try]branch()..eq_ignore_ascii_caserust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/impl#[u8]eq_ignore_ascii_case().(&b'a'));
169 requirerust-analyzer cargo data-url 0.3.2 require!!(iter.nextrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/adapters/filter/impl#[`Filter<I, P>`][Iterator]next().()?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`][Try]branch()..eq_ignore_ascii_caserust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/impl#[u8]eq_ignore_ascii_case().(&b't'));
170 requirerust-analyzer cargo data-url 0.3.2 require!!(iter.nextrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/adapters/filter/impl#[`Filter<I, P>`][Iterator]next().()?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`][Try]branch()..eq_ignore_ascii_caserust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/impl#[u8]eq_ignore_ascii_case().(&b'a'));
171 requirerust-analyzer cargo data-url 0.3.2 require!!(iter.nextrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/adapters/filter/impl#[`Filter<I, P>`][Iterator]next().()?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`][Try]branch(). == b':');
172 }
173 let bytes_consumed = left_trimmed.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]len().() rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub().-rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub().bytes.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/iter/impl#[`Bytes<'_>`][ExactSizeIterator]len().();
174 let after_colon = &left_trimmed[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().bytes_consumed..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeFrom#]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().;
175
176 // Trim C0 control or space
177 Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(after_colon.trim_end_matchesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]trim_end_matches().(|ch| ch rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[char][`PartialOrd<Self>`]le().<=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[char][`PartialOrd<Self>`]le(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[char][`PartialOrd<Self>`]le().' '))
178 }
179
180 fn find_comma_before_fragmentrust-analyzer cargo data-url 0.3.2 find_comma_before_fragment().(after_colon: &str) -> Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<(&str, &str)> {
181 for (i, byte) in after_colon.bytesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]bytes().().enumeraterust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#enumerate().() {
182 if byte rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u8][`PartialEq<Self>`]eq().==rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u8][`PartialEq<Self>`]eq(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u8][`PartialEq<Self>`]eq().b',' {
183 return Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#((&after_colon[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index()...rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeTo#i]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index()., &after_colon[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().i rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().+rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().1..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeFrom#]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().));
184 }
185 if byte rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u8][`PartialEq<Self>`]eq().==rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u8][`PartialEq<Self>`]eq(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u8][`PartialEq<Self>`]eq().b'#' {
186 break;
187 }
188 }
189 Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#
190 }
191
192 fn parse_headerrust-analyzer cargo data-url 0.3.2 parse_header().(from_colon_to_comma: &str) -> (mimerust-analyzer cargo data-url 0.3.2 mime/::Mimerust-analyzer cargo data-url 0.3.2 mime/Mime#, bool) {
193 // "Strip leading and trailing ASCII whitespace"
194 // \t, \n, and \r would have been filtered by the URL parser
195 // \f percent-encoded by the URL parser
196 // space is the only remaining ASCII whitespace
197 let trimmed = from_colon_to_comma.trim_matchesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]trim_matches().(|c| matchesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/matches!!(c, ' ' | '\t' | '\n' | '\r'));
198
199 let without_base64_suffix = remove_base64_suffixrust-analyzer cargo data-url 0.3.2 remove_base64_suffix().(trimmed);
200 let base64 = without_base64_suffix.is_somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]is_some().();
201 let mime_type = without_base64_suffix.unwrap_orrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap_or().(trimmed);
202
203 let mut string = Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#::newrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]new().();
204 if mime_type.starts_withrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]starts_with().(';') {
205 string.push_strrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]push_str().("text/plain")
206 }
207 let mut in_query = false;
208 for byte in mime_type.bytesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]bytes().() {
209 match byte {
210 // Ignore ASCII tabs or newlines like the URL parser would
211 b'\t' | b'\n' | b'\r' => continue,
212
213 // https://url.spec.whatwg.org/#c0-control-percent-encode-set
214 b'\0'..=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeInclusive#b'\x1F' | b'\x7F'..=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeInclusive#b'\xFF' => percent_encoderust-analyzer cargo data-url 0.3.2 percent_encode().(byte, &mut string),
215
216 // Bytes other than the C0 percent-encode set that are percent-encoded
217 // by the URL parser in the query state.
218 // '#' is also in that list but cannot occur here
219 // since it indicates the start of the URL’s fragment.
220 b' ' | b'"' | b'<' | b'>' if in_query => percent_encoderust-analyzer cargo data-url 0.3.2 percent_encode().(byte, &mut string),
221
222 b'?' => {
223 in_query = true;
224 string.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]push().('?')
225 }
226
227 // Printable ASCII
228 _ => string.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]push().(byte as char),
229 }
230 }
231
232 // FIXME: does Mime::from_str match the MIME Sniffing Standard’s parsing algorithm?
233 // <https://mimesniff.spec.whatwg.org/#parse-a-mime-type>
234 let mime_type = string.parserust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]parse().().unwrap_or_elserust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap_or_else().(|_| mimerust-analyzer cargo data-url 0.3.2 mime/::Mimerust-analyzer cargo data-url 0.3.2 mime/Mime# {
235 type_rust-analyzer cargo data-url 0.3.2 mime/Mime#type_.: Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#::fromrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String][`From<&str>`]from().("text"),
236 subtyperust-analyzer cargo data-url 0.3.2 mime/Mime#subtype.: Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#::fromrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String][`From<&str>`]from().("plain"),
237 parametersrust-analyzer cargo data-url 0.3.2 mime/Mime#parameters.: vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![(Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#::fromrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String][`From<&str>`]from().("charset"), Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#::fromrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String][`From<&str>`]from().("US-ASCII"))],
238 });
239
240 (mime_type, base64)
241 }
242
243 /// None: no base64 suffix
244 #[allow(clippy::skip_while_next)]
245 fn remove_base64_suffixrust-analyzer cargo data-url 0.3.2 remove_base64_suffix().(s: &str) -> Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<&str> {
246 let mut bytes = s.bytesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]bytes().();
247 {
248 // Ignore ASCII tabs or newlines like the URL parser would
249 let iter = bytes
250 .by_refrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#by_ref().()
251 .filterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#filter().(|&byte| !rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().matchesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/matches!!(byte, b'\t' | b'\n' | b'\r'));
252
253 // Search from the end
254 let mut iter = iter.revrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#rev().();
255
256 requirerust-analyzer cargo data-url 0.3.2 require!!(iter.nextrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/adapters/rev/impl#[`Rev<I>`][Iterator]next().()?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`][Try]branch(). == b'4');
257 requirerust-analyzer cargo data-url 0.3.2 require!!(iter.nextrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/adapters/rev/impl#[`Rev<I>`][Iterator]next().()?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`][Try]branch(). == b'6');
258 requirerust-analyzer cargo data-url 0.3.2 require!!(iter.nextrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/adapters/rev/impl#[`Rev<I>`][Iterator]next().()?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`][Try]branch()..eq_ignore_ascii_caserust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/impl#[u8]eq_ignore_ascii_case().(&b'e'));
259 requirerust-analyzer cargo data-url 0.3.2 require!!(iter.nextrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/adapters/rev/impl#[`Rev<I>`][Iterator]next().()?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`][Try]branch()..eq_ignore_ascii_caserust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/impl#[u8]eq_ignore_ascii_case().(&b's'));
260 requirerust-analyzer cargo data-url 0.3.2 require!!(iter.nextrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/adapters/rev/impl#[`Rev<I>`][Iterator]next().()?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`][Try]branch()..eq_ignore_ascii_caserust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/impl#[u8]eq_ignore_ascii_case().(&b'a'));
261 requirerust-analyzer cargo data-url 0.3.2 require!!(iter.nextrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/adapters/rev/impl#[`Rev<I>`][Iterator]next().()?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`][Try]branch()..eq_ignore_ascii_caserust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/impl#[u8]eq_ignore_ascii_case().(&b'b'));
262 requirerust-analyzer cargo data-url 0.3.2 require!!(iter.skip_whilerust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#skip_while().(|&byte| byte == b' ').nextrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/adapters/skip_while/impl#[`SkipWhile<I, P>`][Iterator]next().()?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`][Try]branch(). == b';');
263 }
264 Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(&s[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index()...rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeTo#bytes.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/iter/impl#[`Bytes<'_>`][ExactSizeIterator]len().()]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().)
265 }
266
267 fn percent_encoderust-analyzer cargo data-url 0.3.2 percent_encode().(byte: u8, string: &mut Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#) {
268 const HEX_UPPERrust-analyzer cargo data-url 0.3.2 HEX_UPPER.: [u8; 16] = *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&T`][Deref]deref().b"0123456789ABCDEF";
269 string.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]push().('%');
270 string.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]push().(HEX_UPPERrust-analyzer cargo data-url 0.3.2 HEX_UPPER.[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`][`Index<I>`]index().(byte rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u8][`Shr<i32>`]shr().>>rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u8][`Shr<i32>`]shr(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u8][`Shr<i32>`]shr().4) as usize]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`][`Index<I>`]index(). as char);
271 string.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]push().(HEX_UPPERrust-analyzer cargo data-url 0.3.2 HEX_UPPER.[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`][`Index<I>`]index().(byte rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u8][`BitAnd<Self>`]bitand().&rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u8][`BitAnd<Self>`]bitand(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u8][`BitAnd<Self>`]bitand().0x0f) as usize]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`][`Index<I>`]index(). as char);
272 }
273
274 /// This is <https://url.spec.whatwg.org/#string-percent-decode> while also:
275 ///
276 /// * Ignoring ASCII tab or newlines
277 /// * Stopping at the first '#' (which indicates the start of the fragment)
278 ///
279 /// Anything that would have been UTF-8 percent-encoded by the URL parser
280 /// would be percent-decoded here.
281 /// We skip that round-trip and pass it through unchanged.
282 fn decode_without_base64rust-analyzer cargo data-url 0.3.2 decode_without_base64().<F, E>(
283 encoded_body_plus_fragment: &str,
284 mut write_bytes: F,
285 ) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<FragmentIdentifierrust-analyzer cargo data-url 0.3.2 FragmentIdentifier#<'_>>, E>
286 where
287 F: FnMutrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/function/FnMut#(&[u8]) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<(), E>,
288 {
289 let bytes = encoded_body_plus_fragment.as_bytesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]as_bytes().();
290 let mut slice_start = 0;
291 for (i, &byte) in bytes.iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter().().enumeraterust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#enumerate().() {
292 // We only need to look for 5 different "special" byte values.
293 // For everything else we make slices as large as possible, borrowing the input,
294 // in order to make fewer write_all() calls.
295 if matchesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/matches!!(byte, b'%' | b'#' | b'\t' | b'\n' | b'\r') {
296 // Write everything (if anything) "non-special" we’ve accumulated
297 // before this special byte
298 if i rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]gt().>rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]gt(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]gt().slice_start {
299 write_bytes(&bytes[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().slice_start..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/Range#i]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
300 slice_start = i;
301 }
302 // Then deal with the special byte.
303 match byte {
304 b'%' => {
305 let l = bytes.getrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]get().(i rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().+rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().2).and_thenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]and_then().(|&b| (b as char).to_digitrust-analyzer cargo core https://github.com/rust-lang/rust/library/core char/methods/impl#[char]to_digit().(16));
306 let h = bytes.getrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]get().(i rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().+rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().1).and_thenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]and_then().(|&b| (b as char).to_digitrust-analyzer cargo core https://github.com/rust-lang/rust/library/core char/methods/impl#[char]to_digit().(16));
307 if let (Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(h), Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(l)) = (h, l) {
308 // '%' followed by two ASCII hex digits
309 let one_byte = h as u8 rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u8][`Mul<Self>`]mul().*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u8][`Mul<Self>`]mul(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u8][`Mul<Self>`]mul().0x10 rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u8][`Add<Self>`]add().+rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u8][`Add<Self>`]add(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u8][`Add<Self>`]add().l as u8;
310 write_bytes(&[one_byte])?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
311 slice_start = i rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().+rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().3;
312 } else {
313 // Do nothing. Leave slice_start unchanged.
314 // The % sign will be part of the next slice.
315 }
316 }
317
318 b'#' => {
319 let fragment_start = i rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().+rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().1;
320 let fragment = &encoded_body_plus_fragment[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().fragment_start..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeFrom#]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().;
321 return Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(FragmentIdentifierrust-analyzer cargo data-url 0.3.2 FragmentIdentifier#(fragment)));
322 }
323
324 // Ignore over '\t' | '\n' | '\r'
325 _ => slice_start = i rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().+rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().1,
326 }
327 }
328 }
329 write_bytes(&bytes[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().slice_start..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeFrom#]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
330 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#)
331 }
332
333 /// `decode_without_base64()` composed with
334 /// <https://infra.spec.whatwg.org/#isomorphic-decode> composed with
335 /// <https://infra.spec.whatwg.org/#forgiving-base64-decode>.
336 fn decode_with_base64rust-analyzer cargo data-url 0.3.2 decode_with_base64().<F, E>(
337 encoded_body_plus_fragment: &str,
338 write_bytes: F,
339 ) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<FragmentIdentifierrust-analyzer cargo data-url 0.3.2 FragmentIdentifier#<'_>>, forgiving_base64rust-analyzer cargo data-url 0.3.2 forgiving_base64/::DecodeErrorrust-analyzer cargo data-url 0.3.2 forgiving_base64/DecodeError#<E>>
340 where
341 F: FnMutrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/function/FnMut#(&[u8]) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<(), E>,
342 {
343 let mut decoder = forgiving_base64rust-analyzer cargo data-url 0.3.2 forgiving_base64/::Decoderrust-analyzer cargo data-url 0.3.2 forgiving_base64/Decoder#::newrust-analyzer cargo data-url 0.3.2 forgiving_base64/impl#[`Decoder<F, E>`]new().(write_bytes);
344 let fragment = decode_without_base64rust-analyzer cargo data-url 0.3.2 decode_without_base64().(encoded_body_plus_fragment, |bytes| decoder.feedrust-analyzer cargo data-url 0.3.2 forgiving_base64/impl#[`Decoder<F, E>`]feed().(bytes))?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
345 decoder.finishrust-analyzer cargo data-url 0.3.2 forgiving_base64/impl#[`Decoder<F, E>`]finish().()?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
346 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(fragment)
347 }