From d4ebec64962da9ee12130ddc41e7f35ca40557c7 Mon Sep 17 00:00:00 2001 From: minimus Date: Fri, 2 Oct 2015 15:01:55 -0400 Subject: [PATCH] make mediatype default to text/plain in data URIs change `data:charset=` to `data:;charset=` and set US-ASCII as default encoding style change --- components/net/data_loader.rs | 16 ++++++++++++---- tests/unit/net/data_loader.rs | 23 +++++++++++++++++++++-- 2 files changed, 33 insertions(+), 6 deletions(-) diff --git a/components/net/data_loader.rs b/components/net/data_loader.rs index 257fd3440fc..2b90af74bd0 100644 --- a/components/net/data_loader.rs +++ b/components/net/data_loader.rs @@ -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 http://mozilla.org/MPL/2.0/. */ -use hyper::mime::Mime; +use hyper::mime::{Mime, TopLevel, SubLevel, Attr, Value}; use mime_classifier::MIMEClassifier; use net_traits::ProgressMsg::{Done, Payload}; use net_traits::{LoadConsumer, LoadData, Metadata}; @@ -48,15 +48,23 @@ pub fn load(load_data: LoadData, start_chan: LoadConsumer) { // ";base64" must come at the end of the content type, per RFC 2397. // rust-http will fail to parse it because there's no =value part. let mut is_base64 = false; - let mut ct_str = parts[0]; + let mut ct_str = parts[0].to_owned(); if ct_str.ends_with(";base64") { is_base64 = true; - ct_str = &ct_str[..ct_str.as_bytes().len() - 7]; + let end_index = ct_str.len() - 7; + ct_str.truncate(end_index); + } + if ct_str.starts_with(";charset=") { + ct_str = format!("text/plain{}", ct_str); } // Parse the content type using rust-http. // FIXME: this can go into an infinite loop! (rust-http #25) - let content_type: Option = ct_str.parse().ok(); + let mut content_type: Option = ct_str.parse().ok(); + if content_type == None { + content_type = Some(Mime(TopLevel::Text, SubLevel::Plain, + vec!((Attr::Charset, Value::Ext("US-ASCII".to_owned()))))); + } metadata.set_content_type(content_type.as_ref()); let progress_chan = start_sending(start_chan, metadata); diff --git a/tests/unit/net/data_loader.rs b/tests/unit/net/data_loader.rs index 2c9412f0af4..05f735413ab 100644 --- a/tests/unit/net/data_loader.rs +++ b/tests/unit/net/data_loader.rs @@ -47,7 +47,11 @@ fn empty_invalid() { #[test] fn plain() { - assert_parse("data:,hello%20world", None, None, Some(b"hello world".iter().map(|&x| x).collect())); + assert_parse( + "data:,hello%20world", + Some(ContentType(Mime(TopLevel::Text, SubLevel::Plain, + vec!((Attr::Charset, Value::Ext("us-ascii".to_owned())))))), + Some("US-ASCII".to_owned()), Some(b"hello world".iter().map(|&x| x).collect())); } #[test] @@ -68,9 +72,24 @@ fn plain_charset() { Some("latin1".to_string()), Some(b"hello".iter().map(|&x| x).collect())); } +#[test] +fn plain_only_charset() { + assert_parse( + "data:;charset=utf-8,hello", + Some(ContentType(Mime(TopLevel::Text, + SubLevel::Plain, + vec!((Attr::Charset, Value::Utf8))))), + Some("utf-8".to_string()), Some(b"hello".iter().map(|&x| x).collect())); +} + #[test] fn base64() { - assert_parse("data:;base64,C62+7w==", None, None, Some(vec!(0x0B, 0xAD, 0xBE, 0xEF))); + assert_parse( + "data:;base64,C62+7w==", + Some(ContentType(Mime(TopLevel::Text, + SubLevel::Plain, + vec!((Attr::Charset, Value::Ext("us-ascii".to_owned())))))), + Some("US-ASCII".to_owned()), Some(vec!(0x0B, 0xAD, 0xBE, 0xEF))); } #[test]