mirror of
https://github.com/servo/servo.git
synced 2025-08-03 12:40:06 +01:00
auto merge of #5219 : doublec/servo/view_source_protocol_and_plain_text, r=jdm
Implements view-source protocol by having a view-source handler, and modifying the content type to be text/plain if that is used. Implements text/plain handling. This allows view-source content to display as plain text. Example usage: ./mach run http://cd.pn/x.txt ./mach run view-source:http://tinyvid.tv/ This fixes issue #4181. Issue #3649 includes "support text/plain" so this possibly fixes some of that issue as well.
This commit is contained in:
commit
7bd6cb0091
5 changed files with 69 additions and 23 deletions
|
@ -14,6 +14,7 @@ use hyper::client::Request;
|
||||||
use hyper::header::{ContentLength, ContentType, Host, Location};
|
use hyper::header::{ContentLength, ContentType, Host, Location};
|
||||||
use hyper::HttpError;
|
use hyper::HttpError;
|
||||||
use hyper::method::Method;
|
use hyper::method::Method;
|
||||||
|
use hyper::mime::{Mime, TopLevel, SubLevel};
|
||||||
use hyper::net::HttpConnector;
|
use hyper::net::HttpConnector;
|
||||||
use hyper::status::{StatusCode, StatusClass};
|
use hyper::status::{StatusCode, StatusClass};
|
||||||
use std::error::Error;
|
use std::error::Error;
|
||||||
|
@ -59,6 +60,26 @@ fn load(mut load_data: LoadData, start_chan: Sender<TargetedLoadResponse>, cooki
|
||||||
eventual_consumer: load_data.consumer
|
eventual_consumer: load_data.consumer
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// If the URL is a view-source scheme then the scheme data contains the
|
||||||
|
// real URL that should be used for which the source is to be viewed.
|
||||||
|
// Change our existing URL to that and keep note that we are viewing
|
||||||
|
// the source rather than rendering the contents of the URL.
|
||||||
|
let viewing_source = if url.scheme == "view-source" {
|
||||||
|
let inner_url = load_data.url.non_relative_scheme_data().unwrap();
|
||||||
|
url = Url::parse(inner_url).unwrap();
|
||||||
|
match url.scheme.as_slice() {
|
||||||
|
"http" | "https" => {}
|
||||||
|
_ => {
|
||||||
|
let s = format!("The {} scheme with view-source is not supported", url.scheme);
|
||||||
|
send_error(url, s, senders);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
true
|
||||||
|
} else {
|
||||||
|
false
|
||||||
|
};
|
||||||
|
|
||||||
// Loop to handle redirects.
|
// Loop to handle redirects.
|
||||||
loop {
|
loop {
|
||||||
iters = iters + 1;
|
iters = iters + 1;
|
||||||
|
@ -259,12 +280,16 @@ reason: \"certificate verify failed\" }]";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let mut adjusted_headers = response.headers.clone();
|
||||||
|
if viewing_source {
|
||||||
|
adjusted_headers.set(ContentType(Mime(TopLevel::Text, SubLevel::Plain, vec![])));
|
||||||
|
}
|
||||||
let mut metadata = Metadata::default(url);
|
let mut metadata = Metadata::default(url);
|
||||||
metadata.set_content_type(match response.headers.get() {
|
metadata.set_content_type(match adjusted_headers.get() {
|
||||||
Some(&ContentType(ref mime)) => Some(mime),
|
Some(&ContentType(ref mime)) => Some(mime),
|
||||||
None => None
|
None => None
|
||||||
});
|
});
|
||||||
metadata.headers = Some(response.headers.clone());
|
metadata.headers = Some(adjusted_headers);
|
||||||
metadata.status = Some(response.status_raw().clone());
|
metadata.status = Some(response.status_raw().clone());
|
||||||
|
|
||||||
let progress_chan = match start_sending_opt(senders, metadata) {
|
let progress_chan = match start_sending_opt(senders, metadata) {
|
||||||
|
|
|
@ -333,7 +333,7 @@ impl ResourceManager {
|
||||||
|
|
||||||
let loader = match load_data.url.scheme.as_slice() {
|
let loader = match load_data.url.scheme.as_slice() {
|
||||||
"file" => from_factory(file_loader::factory),
|
"file" => from_factory(file_loader::factory),
|
||||||
"http" | "https" => http_loader::factory(self.resource_task.clone()),
|
"http" | "https" | "view-source" => http_loader::factory(self.resource_task.clone()),
|
||||||
"data" => from_factory(data_loader::factory),
|
"data" => from_factory(data_loader::factory),
|
||||||
"about" => from_factory(about_loader::factory),
|
"about" => from_factory(about_loader::factory),
|
||||||
_ => {
|
_ => {
|
||||||
|
|
|
@ -180,6 +180,22 @@ pub fn parse_html(document: JSRef<Document>,
|
||||||
task_state::enter(IN_HTML_PARSER);
|
task_state::enter(IN_HTML_PARSER);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn parse_progress(parser: &JSRef<ServoHTMLParser>, url: &Url, load_response: &LoadResponse) {
|
||||||
|
for msg in load_response.progress_port.iter() {
|
||||||
|
match msg {
|
||||||
|
ProgressMsg::Payload(data) => {
|
||||||
|
// FIXME: use Vec<u8> (html5ever #34)
|
||||||
|
let data = UTF_8.decode(data.as_slice(), DecoderTrap::Replace).unwrap();
|
||||||
|
parser.parse_chunk(data);
|
||||||
|
}
|
||||||
|
ProgressMsg::Done(Err(err)) => {
|
||||||
|
panic!("Failed to load page URL {}, error: {}", url.serialize(), err);
|
||||||
|
}
|
||||||
|
ProgressMsg::Done(Ok(())) => break,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
match input {
|
match input {
|
||||||
HTMLInput::InputString(s) => {
|
HTMLInput::InputString(s) => {
|
||||||
parser.parse_chunk(s);
|
parser.parse_chunk(s);
|
||||||
|
@ -190,20 +206,22 @@ pub fn parse_html(document: JSRef<Document>,
|
||||||
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") &&
|
||||||
|
st.as_slice().eq_ignore_ascii_case("plain") => {
|
||||||
|
// FIXME: When servo/html5ever#109 is fixed remove <plaintext> usage and
|
||||||
|
// replace with fix from that issue.
|
||||||
|
|
||||||
|
// text/plain documents require setting the tokenizer into PLAINTEXT mode.
|
||||||
|
// This is done by using a <plaintext> element as the html5ever tokenizer
|
||||||
|
// provides no other way to change to that state.
|
||||||
|
// Spec for text/plain handling is:
|
||||||
|
// https://html.spec.whatwg.org/multipage/browsers.html#read-text
|
||||||
|
let page = format!("<pre>\u{000A}<plaintext>");
|
||||||
|
parser.parse_chunk(page);
|
||||||
|
parse_progress(&parser, url, &load_response);
|
||||||
|
},
|
||||||
_ => {
|
_ => {
|
||||||
for msg in load_response.progress_port.iter() {
|
parse_progress(&parser, url, &load_response);
|
||||||
match msg {
|
|
||||||
ProgressMsg::Payload(data) => {
|
|
||||||
// FIXME: use Vec<u8> (html5ever #34)
|
|
||||||
let data = UTF_8.decode(data.as_slice(), DecoderTrap::Replace).unwrap();
|
|
||||||
parser.parse_chunk(data);
|
|
||||||
}
|
|
||||||
ProgressMsg::Done(Err(err)) => {
|
|
||||||
panic!("Failed to load page URL {}, error: {}", url.serialize(), err);
|
|
||||||
}
|
|
||||||
ProgressMsg::Done(Ok(())) => break,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -83,6 +83,7 @@ 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};
|
||||||
|
@ -978,10 +979,18 @@ impl ScriptTask {
|
||||||
headers.get().map(|&LastModified(ref tm)| dom_last_modified(tm))
|
headers.get().map(|&LastModified(ref tm)| dom_last_modified(tm))
|
||||||
});
|
});
|
||||||
|
|
||||||
|
let content_type = match response.metadata.content_type {
|
||||||
|
Some((ref t, ref st)) if t.as_slice().eq_ignore_ascii_case("text") &&
|
||||||
|
st.as_slice().eq_ignore_ascii_case("plain") => {
|
||||||
|
Some("text/plain".to_owned())
|
||||||
|
}
|
||||||
|
_ => None
|
||||||
|
};
|
||||||
|
|
||||||
let document = Document::new(window.r(),
|
let document = Document::new(window.r(),
|
||||||
Some(final_url.clone()),
|
Some(final_url.clone()),
|
||||||
IsHTMLDocument::HTMLDocument,
|
IsHTMLDocument::HTMLDocument,
|
||||||
None,
|
content_type,
|
||||||
last_modified,
|
last_modified,
|
||||||
DocumentSource::FromParser).root();
|
DocumentSource::FromParser).root();
|
||||||
|
|
||||||
|
|
|
@ -1,11 +1,5 @@
|
||||||
[load-text-plain.html]
|
[load-text-plain.html]
|
||||||
type: testharness
|
type: testharness
|
||||||
[Checking document metadata for text file]
|
|
||||||
expected: FAIL
|
|
||||||
|
|
||||||
[Checking DOM for text file]
|
|
||||||
expected: FAIL
|
|
||||||
|
|
||||||
[Checking contents for text file]
|
[Checking contents for text file]
|
||||||
expected: FAIL
|
expected: FAIL
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue