Extract content type and character set from HTTP response headers

We don't use this information anywhere, yet.
This commit is contained in:
Keegan McAllister 2013-10-09 18:12:12 -07:00
parent ec2111edbf
commit f73e48b32f
3 changed files with 29 additions and 3 deletions

View file

@ -54,7 +54,7 @@ fn load(url: Url, start_chan: Chan<LoadResponse>) {
}
let mut metadata = Metadata::default(url);
// We will set other fields here.
metadata.set_content_type(&response.headers.content_type);
let progress_chan = start_sending(start_chan, metadata);
loop {

View file

@ -12,6 +12,7 @@ use std::comm::{Chan, Port, SharedChan};
use std::comm;
use extra::url::Url;
use util::spawn_listener;
use http::headers::content_type::MediaType;
pub enum ControlMsg {
/// Request the data associated with a particular URL
@ -24,14 +25,37 @@ pub struct Metadata {
/// Final URL after redirects.
final_url: Url,
// Other fields (e.g. content type, charset) will go here.
/// MIME type / subtype.
content_type: Option<(~str, ~str)>,
/// Character set.
charset: Option<~str>,
}
impl Metadata {
/// Metadata with defaults for everything optional.
pub fn default(url: Url) -> Metadata {
Metadata {
final_url: url,
final_url: url,
content_type: None,
charset: None,
}
}
/// Extract the parts of a MediaType that we care about.
pub fn set_content_type(&mut self, content_type: &Option<MediaType>) {
match *content_type {
None => (),
Some(MediaType { type_: ref type_,
subtype: ref subtype,
parameters: ref parameters }) => {
self.content_type = Some((type_.clone(), subtype.clone()));
for &(ref k, ref v) in parameters.iter() {
if "charset" == k.as_slice() {
self.charset = Some(v.clone());
}
}
}
}
}
}

View file

@ -336,6 +336,8 @@ pub fn parse_html(cx: *JSContext,
resource_task.send(Load(url.clone(), input_chan));
let load_response = input_port.recv();
debug!("Fetched page; metadata is %?", load_response.metadata);
let url2 = load_response.metadata.final_url.clone();
let url3 = url2.clone();