mirror of
https://github.com/servo/servo.git
synced 2025-08-02 20:20:14 +01:00
Implement view-source protocol. Fixes #4181.
This follows the recommendation from issue #4181. A handler for 'view-source' delegates to the HTTP loader. In that loader I check for view-source, adjust the URL to be the URL to be viewed and modify the Content-Type header to be text/plain. This doesn't actually result in the source being viewed as rendering text/plain is not yet implemented.
This commit is contained in:
parent
f5ddbcf19f
commit
82c52a7a1c
3 changed files with 34 additions and 3 deletions
|
@ -14,6 +14,7 @@ use hyper::client::Request;
|
|||
use hyper::header::{ContentLength, ContentType, Host, Location};
|
||||
use hyper::HttpError;
|
||||
use hyper::method::Method;
|
||||
use hyper::mime::{Mime, TopLevel, SubLevel};
|
||||
use hyper::net::HttpConnector;
|
||||
use hyper::status::{StatusCode, StatusClass};
|
||||
use std::error::Error;
|
||||
|
@ -54,6 +55,18 @@ fn load(mut load_data: LoadData, start_chan: Sender<TargetedLoadResponse>, cooki
|
|||
let mut url = load_data.url.clone();
|
||||
let mut redirected_to = HashSet::new();
|
||||
|
||||
// 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();
|
||||
true
|
||||
} else {
|
||||
false
|
||||
};
|
||||
|
||||
let senders = ResponseSenders {
|
||||
immediate_consumer: start_chan,
|
||||
eventual_consumer: load_data.consumer
|
||||
|
@ -259,12 +272,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);
|
||||
metadata.set_content_type(match response.headers.get() {
|
||||
metadata.set_content_type(match adjusted_headers.get() {
|
||||
Some(&ContentType(ref mime)) => Some(mime),
|
||||
None => None
|
||||
});
|
||||
metadata.headers = Some(response.headers.clone());
|
||||
metadata.headers = Some(adjusted_headers);
|
||||
metadata.status = Some(response.status_raw().clone());
|
||||
|
||||
let progress_chan = match start_sending_opt(senders, metadata) {
|
||||
|
|
|
@ -333,7 +333,7 @@ impl ResourceManager {
|
|||
|
||||
let loader = match load_data.url.scheme.as_slice() {
|
||||
"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),
|
||||
"about" => from_factory(about_loader::factory),
|
||||
_ => {
|
||||
|
|
|
@ -190,6 +190,20 @@ pub fn parse_html(document: JSRef<Document>,
|
|||
let page = format!("<html><body><img src='{}' /></body></html>", url.serialize());
|
||||
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() {
|
||||
match msg {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue