Fix #2909 as far as WPT is concerned, not full data-url class integration

This commit is contained in:
Patrick Shaughnessy 2019-11-26 09:49:30 -05:00
parent 799057f1e6
commit 7363db31db
4 changed files with 17 additions and 41 deletions

View file

@ -2,7 +2,7 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
use base64;
use data_url::forgiving_base64;
use mime::Mime;
use percent_encoding::percent_decode;
use servo_url::ServoUrl;
@ -16,6 +16,10 @@ pub enum DecodeError {
pub type DecodeData = (Mime, Vec<u8>);
pub fn decode(url: &ServoUrl) -> Result<DecodeData, DecodeError> {
// data_url could do all of this work for us,
// except that it currently (Nov 2019) parses mime types into a
// different Mime class than other code expects
assert_eq!(url.scheme(), "data");
// Split out content type and data.
let parts: Vec<&str> = url[Position::BeforePath..Position::AfterQuery]
@ -44,13 +48,7 @@ pub fn decode(url: &ServoUrl) -> Result<DecodeData, DecodeError> {
let mut bytes = percent_decode(parts[1].as_bytes()).collect::<Vec<_>>();
if is_base64 {
// FIXME(#2909): Its unclear what to do with non-alphabet characters,
// but Acid 3 apparently depends on spaces being ignored.
bytes = bytes
.into_iter()
.filter(|&b| b != b' ')
.collect::<Vec<u8>>();
match base64::decode_config(&bytes, base64::STANDARD.decode_allow_trailing_bits(true)) {
match forgiving_base64::decode_to_vec(&bytes) {
Err(..) => return Err(DecodeError::NonBase64DataUri),
Ok(data) => bytes = data,
}