mirror of
https://github.com/servo/servo.git
synced 2025-06-13 02:44:29 +00:00
Make Metadata use ContentType
This commit is contained in:
parent
fe81ce942a
commit
19ffab82f1
5 changed files with 47 additions and 18 deletions
|
@ -9,7 +9,9 @@ use resource_task::start_sending;
|
||||||
use file_loader;
|
use file_loader;
|
||||||
|
|
||||||
use url::Url;
|
use url::Url;
|
||||||
|
use hyper::header::ContentType;
|
||||||
use hyper::http::RawStatus;
|
use hyper::http::RawStatus;
|
||||||
|
use hyper::mime::{Mime, TopLevel, SubLevel};
|
||||||
use util::resource_files::resources_dir_path;
|
use util::resource_files::resources_dir_path;
|
||||||
|
|
||||||
use std::borrow::IntoCow;
|
use std::borrow::IntoCow;
|
||||||
|
@ -22,7 +24,7 @@ pub fn factory(mut load_data: LoadData, classifier: Arc<MIMEClassifier>) {
|
||||||
let start_chan = load_data.consumer;
|
let start_chan = load_data.consumer;
|
||||||
let chan = start_sending(start_chan, Metadata {
|
let chan = start_sending(start_chan, Metadata {
|
||||||
final_url: load_data.url,
|
final_url: load_data.url,
|
||||||
content_type: Some(("text".to_string(), "html".to_string())),
|
content_type: Some(ContentType(Mime(TopLevel::Text, SubLevel::Html, vec![]))),
|
||||||
charset: Some("utf-8".to_string()),
|
charset: Some("utf-8".to_string()),
|
||||||
headers: None,
|
headers: None,
|
||||||
status: Some(RawStatus(200, "OK".into_cow())),
|
status: Some(RawStatus(200, "OK".into_cow())),
|
||||||
|
|
|
@ -18,8 +18,8 @@ use net_traits::ProgressMsg::Done;
|
||||||
use util::opts;
|
use util::opts;
|
||||||
use util::task::spawn_named;
|
use util::task::spawn_named;
|
||||||
|
|
||||||
use hyper::header::UserAgent;
|
use hyper::header::{ContentType, Header, SetCookie, UserAgent};
|
||||||
use hyper::header::{Header, SetCookie};
|
use hyper::mime::{Mime, TopLevel, SubLevel};
|
||||||
|
|
||||||
use std::borrow::ToOwned;
|
use std::borrow::ToOwned;
|
||||||
use std::boxed;
|
use std::boxed;
|
||||||
|
@ -27,6 +27,7 @@ use std::collections::HashMap;
|
||||||
use std::env;
|
use std::env;
|
||||||
use std::fs::File;
|
use std::fs::File;
|
||||||
use std::io::{BufReader, Read};
|
use std::io::{BufReader, Read};
|
||||||
|
use std::str::FromStr;
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
use std::sync::mpsc::{channel, Receiver, Sender};
|
use std::sync::mpsc::{channel, Receiver, Sender};
|
||||||
use std::thunk::Invoke;
|
use std::thunk::Invoke;
|
||||||
|
@ -78,8 +79,33 @@ pub fn start_sending_sniffed_opt(start_chan: Sender<LoadResponse>, mut metadata:
|
||||||
let nosniff = false;
|
let nosniff = false;
|
||||||
let check_for_apache_bug = false;
|
let check_for_apache_bug = false;
|
||||||
|
|
||||||
metadata.content_type = classifier.classify(nosniff, check_for_apache_bug,
|
metadata.content_type = match metadata.content_type {
|
||||||
&metadata.content_type, &partial_body);
|
Some(ContentType(Mime(toplevel, sublevel, _))) => {
|
||||||
|
let content_type = classifier.classify(nosniff, check_for_apache_bug,
|
||||||
|
&Some((format!("{}", toplevel), format!("{}", sublevel))), &partial_body);
|
||||||
|
match content_type {
|
||||||
|
Some((tp, sb)) => {
|
||||||
|
let mime_tp: TopLevel = FromStr::from_str(&tp).unwrap();
|
||||||
|
let mime_sb: SubLevel = FromStr::from_str(&sb).unwrap();
|
||||||
|
Some(ContentType(Mime(mime_tp, mime_sb, vec!())))
|
||||||
|
}
|
||||||
|
None => None
|
||||||
|
}
|
||||||
|
}
|
||||||
|
None => {
|
||||||
|
let content_type = classifier.classify(nosniff, check_for_apache_bug,
|
||||||
|
&None, &partial_body);
|
||||||
|
|
||||||
|
match content_type {
|
||||||
|
Some((tp, sb)) => {
|
||||||
|
let mime_tp: TopLevel = FromStr::from_str(&tp).unwrap();
|
||||||
|
let mime_sb: SubLevel = FromStr::from_str(&sb).unwrap();
|
||||||
|
Some(ContentType(Mime(mime_tp, mime_sb, vec!())))
|
||||||
|
}
|
||||||
|
None => None
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -18,7 +18,7 @@ extern crate stb_image;
|
||||||
extern crate url;
|
extern crate url;
|
||||||
extern crate util;
|
extern crate util;
|
||||||
|
|
||||||
use hyper::header::Headers;
|
use hyper::header::{ContentType, Headers};
|
||||||
use hyper::http::RawStatus;
|
use hyper::http::RawStatus;
|
||||||
use hyper::method::Method;
|
use hyper::method::Method;
|
||||||
use hyper::mime::{Mime, Attr};
|
use hyper::mime::{Mime, Attr};
|
||||||
|
@ -108,7 +108,7 @@ pub struct Metadata {
|
||||||
pub final_url: Url,
|
pub final_url: Url,
|
||||||
|
|
||||||
/// MIME type / subtype.
|
/// MIME type / subtype.
|
||||||
pub content_type: Option<(String, String)>,
|
pub content_type: Option<(ContentType)>,
|
||||||
|
|
||||||
/// Character set.
|
/// Character set.
|
||||||
pub charset: Option<String>,
|
pub charset: Option<String>,
|
||||||
|
@ -137,8 +137,9 @@ impl Metadata {
|
||||||
pub fn set_content_type(&mut self, content_type: Option<&Mime>) {
|
pub fn set_content_type(&mut self, content_type: Option<&Mime>) {
|
||||||
match content_type {
|
match content_type {
|
||||||
None => (),
|
None => (),
|
||||||
Some(&Mime(ref type_, ref subtype, ref parameters)) => {
|
Some(mime) => {
|
||||||
self.content_type = Some((type_.to_string(), subtype.to_string()));
|
self.content_type = Some(ContentType(mime.clone()));
|
||||||
|
let &Mime(_, _, ref parameters) = mime;
|
||||||
for &(ref k, ref v) in parameters.iter() {
|
for &(ref k, ref v) in parameters.iter() {
|
||||||
if &Attr::Charset == k {
|
if &Attr::Charset == k {
|
||||||
self.charset = Some(v.to_string());
|
self.charset = Some(v.to_string());
|
||||||
|
|
|
@ -38,7 +38,6 @@ use net_traits::{ProgressMsg, LoadResponse};
|
||||||
use util::str::DOMString;
|
use util::str::DOMString;
|
||||||
use util::task_state;
|
use util::task_state;
|
||||||
use util::task_state::IN_HTML_PARSER;
|
use util::task_state::IN_HTML_PARSER;
|
||||||
use std::ascii::AsciiExt;
|
|
||||||
use std::borrow::Cow;
|
use std::borrow::Cow;
|
||||||
use std::old_io::{Writer, IoResult};
|
use std::old_io::{Writer, IoResult};
|
||||||
use url::Url;
|
use url::Url;
|
||||||
|
@ -49,6 +48,9 @@ use html5ever::serialize::TraversalScope::{IncludeNode, ChildrenOnly};
|
||||||
use html5ever::tree_builder::{TreeSink, QuirksMode, NodeOrText, AppendNode, AppendText};
|
use html5ever::tree_builder::{TreeSink, QuirksMode, NodeOrText, AppendNode, AppendText};
|
||||||
use string_cache::QualName;
|
use string_cache::QualName;
|
||||||
|
|
||||||
|
use hyper::header::ContentType;
|
||||||
|
use hyper::mime::{Mime, TopLevel, SubLevel};
|
||||||
|
|
||||||
pub enum HTMLInput {
|
pub enum HTMLInput {
|
||||||
InputString(String),
|
InputString(String),
|
||||||
InputUrl(LoadResponse),
|
InputUrl(LoadResponse),
|
||||||
|
@ -297,12 +299,11 @@ pub fn parse_html(document: JSRef<Document>,
|
||||||
}
|
}
|
||||||
HTMLInput::InputUrl(load_response) => {
|
HTMLInput::InputUrl(load_response) => {
|
||||||
match load_response.metadata.content_type {
|
match load_response.metadata.content_type {
|
||||||
Some((ref t, _)) if t.as_slice().eq_ignore_ascii_case("image") => {
|
Some(ContentType(Mime(TopLevel::Image, _, _))) => {
|
||||||
let page = format!("<html><body><img src='{}' /></body></html>", url.serialize());
|
let page = format!("<html><body><img src='{}' /></body></html>", url.serialize());
|
||||||
parser.parse_chunk(page);
|
parser.parse_chunk(page);
|
||||||
},
|
},
|
||||||
Some((ref t, ref st)) if t.as_slice().eq_ignore_ascii_case("text") &&
|
Some(ContentType(Mime(TopLevel::Text, SubLevel::Plain, _))) => {
|
||||||
st.as_slice().eq_ignore_ascii_case("plain") => {
|
|
||||||
// FIXME: When servo/html5ever#109 is fixed remove <plaintext> usage and
|
// FIXME: When servo/html5ever#109 is fixed remove <plaintext> usage and
|
||||||
// replace with fix from that issue.
|
// replace with fix from that issue.
|
||||||
|
|
||||||
|
|
|
@ -83,7 +83,6 @@ use js;
|
||||||
use url::Url;
|
use url::Url;
|
||||||
|
|
||||||
use libc;
|
use libc;
|
||||||
use std::ascii::AsciiExt;
|
|
||||||
use std::any::Any;
|
use std::any::Any;
|
||||||
use std::borrow::ToOwned;
|
use std::borrow::ToOwned;
|
||||||
use std::cell::{Cell, RefCell};
|
use std::cell::{Cell, RefCell};
|
||||||
|
@ -96,6 +95,9 @@ use std::result::Result;
|
||||||
use std::sync::mpsc::{channel, Sender, Receiver, Select};
|
use std::sync::mpsc::{channel, Sender, Receiver, Select};
|
||||||
use time::Tm;
|
use time::Tm;
|
||||||
|
|
||||||
|
use hyper::header::ContentType;
|
||||||
|
use hyper::mime::{Mime, TopLevel, SubLevel};
|
||||||
|
|
||||||
thread_local!(pub static STACK_ROOTS: Cell<Option<RootCollectionPtr>> = Cell::new(None));
|
thread_local!(pub static STACK_ROOTS: Cell<Option<RootCollectionPtr>> = Cell::new(None));
|
||||||
thread_local!(static SCRIPT_TASK_ROOT: RefCell<Option<*const ScriptTask>> = RefCell::new(None));
|
thread_local!(static SCRIPT_TASK_ROOT: RefCell<Option<*const ScriptTask>> = RefCell::new(None));
|
||||||
|
|
||||||
|
@ -1052,10 +1054,7 @@ impl ScriptTask {
|
||||||
});
|
});
|
||||||
|
|
||||||
let content_type = match response.metadata.content_type {
|
let content_type = match response.metadata.content_type {
|
||||||
Some((ref t, ref st)) if t.as_slice().eq_ignore_ascii_case("text") &&
|
Some(ContentType(Mime(TopLevel::Text, SubLevel::Plain, _))) => Some("text/plain".to_owned()),
|
||||||
st.as_slice().eq_ignore_ascii_case("plain") => {
|
|
||||||
Some("text/plain".to_owned())
|
|
||||||
}
|
|
||||||
_ => None
|
_ => None
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue