2014-08-03 21:18:02 +04:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* 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/. */
|
|
|
|
|
2016-10-14 15:20:05 +03:00
|
|
|
use blob_loader::load_blob_sync;
|
2016-04-01 12:42:30 +03:00
|
|
|
use data_loader::decode;
|
2016-07-29 19:44:20 +03:00
|
|
|
use devtools_traits::DevtoolsControlMsg;
|
2016-11-10 23:43:36 +03:00
|
|
|
use fetch::cors_cache::CorsCache;
|
2016-11-22 03:28:19 +03:00
|
|
|
use filemanager_thread::FileManager;
|
2016-11-26 07:20:26 +03:00
|
|
|
use http_loader::{HttpState, determine_request_referrer, http_fetch, set_default_accept_language};
|
|
|
|
use hyper::header::{Accept, AcceptLanguage, ContentLanguage, ContentType};
|
|
|
|
use hyper::header::{HeaderView, QualityItem, Referer as RefererHeader, q, qitem};
|
2015-08-20 16:43:56 +03:00
|
|
|
use hyper::method::Method;
|
2016-04-09 22:51:04 +03:00
|
|
|
use hyper::mime::{Mime, SubLevel, TopLevel};
|
2015-05-18 17:23:43 +03:00
|
|
|
use hyper::status::StatusCode;
|
2016-04-30 08:54:20 +03:00
|
|
|
use mime_guess::guess_mime_type;
|
2016-11-26 07:20:26 +03:00
|
|
|
use net_traits::{FetchTaskTarget, NetworkError, ReferrerPolicy};
|
2016-09-16 23:43:54 +03:00
|
|
|
use net_traits::request::{RedirectMode, Referrer, Request, RequestMode, ResponseTainting};
|
2016-06-12 03:30:28 +03:00
|
|
|
use net_traits::request::{Type, Origin, Window};
|
2016-11-26 07:20:26 +03:00
|
|
|
use net_traits::response::{Response, ResponseBody, ResponseType};
|
2016-10-11 12:04:20 +03:00
|
|
|
use std::borrow::Cow;
|
2016-04-30 08:54:20 +03:00
|
|
|
use std::fs::File;
|
2016-01-19 01:54:09 +03:00
|
|
|
use std::io::Read;
|
2016-12-10 00:13:27 +03:00
|
|
|
use std::mem;
|
2015-11-27 09:09:32 +03:00
|
|
|
use std::rc::Rc;
|
2016-11-26 07:20:26 +03:00
|
|
|
use std::sync::mpsc::{Sender, Receiver};
|
2017-01-08 10:14:37 +03:00
|
|
|
use subresource_integrity::is_response_integrity_valid;
|
2014-08-03 21:18:02 +04:00
|
|
|
|
2016-12-16 01:02:42 +03:00
|
|
|
pub type Target<'a> = &'a mut (FetchTaskTarget + Send);
|
2016-06-12 03:30:28 +03:00
|
|
|
|
2016-11-26 07:20:26 +03:00
|
|
|
pub enum Data {
|
2016-06-12 03:30:28 +03:00
|
|
|
Payload(Vec<u8>),
|
|
|
|
Done,
|
|
|
|
}
|
|
|
|
|
2016-11-22 03:28:19 +03:00
|
|
|
pub struct FetchContext {
|
2016-06-12 03:30:28 +03:00
|
|
|
pub state: HttpState,
|
2016-10-11 12:04:20 +03:00
|
|
|
pub user_agent: Cow<'static, str>,
|
2016-07-29 19:44:20 +03:00
|
|
|
pub devtools_chan: Option<Sender<DevtoolsControlMsg>>,
|
2016-11-22 03:28:19 +03:00
|
|
|
pub filemanager: FileManager,
|
2015-12-30 05:20:43 +03:00
|
|
|
}
|
|
|
|
|
2016-11-26 07:20:26 +03:00
|
|
|
pub type DoneChannel = Option<(Sender<Data>, Receiver<Data>)>;
|
2016-06-12 03:30:28 +03:00
|
|
|
|
2015-12-30 05:20:43 +03:00
|
|
|
/// [Fetch](https://fetch.spec.whatwg.org#concept-fetch)
|
2016-11-22 03:28:19 +03:00
|
|
|
pub fn fetch(request: Rc<Request>,
|
2016-12-16 01:02:42 +03:00
|
|
|
target: Target,
|
|
|
|
context: &FetchContext) {
|
|
|
|
fetch_with_cors_cache(request, &mut CorsCache::new(), target, context);
|
2016-04-28 23:36:05 +03:00
|
|
|
}
|
|
|
|
|
2016-11-22 03:28:19 +03:00
|
|
|
pub fn fetch_with_cors_cache(request: Rc<Request>,
|
|
|
|
cache: &mut CorsCache,
|
2016-12-16 01:02:42 +03:00
|
|
|
target: Target,
|
|
|
|
context: &FetchContext) {
|
2015-12-30 05:20:43 +03:00
|
|
|
// Step 1
|
2016-02-27 17:52:22 +03:00
|
|
|
if request.window.get() == Window::Client {
|
|
|
|
// TODO: Set window to request's client object if client is a Window object
|
|
|
|
} else {
|
|
|
|
request.window.set(Window::NoWindow);
|
|
|
|
}
|
2015-12-30 05:20:43 +03:00
|
|
|
|
2016-02-27 17:52:22 +03:00
|
|
|
// Step 2
|
|
|
|
if *request.origin.borrow() == Origin::Client {
|
|
|
|
// TODO: set request's origin to request's client's origin
|
|
|
|
unimplemented!()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Step 3
|
|
|
|
if !request.headers.borrow().has::<Accept>() {
|
|
|
|
let value = match request.type_ {
|
|
|
|
// Substep 2
|
|
|
|
_ if request.is_navigation_request() =>
|
2016-04-08 19:30:15 +03:00
|
|
|
vec![qitem(mime!(Text / Html)),
|
2016-02-27 17:52:22 +03:00
|
|
|
// FIXME: This should properly generate a MimeType that has a
|
|
|
|
// SubLevel of xhtml+xml (https://github.com/hyperium/mime.rs/issues/22)
|
2016-04-08 19:30:15 +03:00
|
|
|
qitem(mime!(Application / ("xhtml+xml") )),
|
|
|
|
QualityItem::new(mime!(Application / Xml), q(0.9)),
|
|
|
|
QualityItem::new(mime!(_ / _), q(0.8))],
|
2016-02-27 17:52:22 +03:00
|
|
|
|
|
|
|
// Substep 3
|
|
|
|
Type::Image =>
|
2016-04-08 19:30:15 +03:00
|
|
|
vec![qitem(mime!(Image / Png)),
|
2016-02-27 17:52:22 +03:00
|
|
|
// FIXME: This should properly generate a MimeType that has a
|
|
|
|
// SubLevel of svg+xml (https://github.com/hyperium/mime.rs/issues/22)
|
2016-04-08 19:30:15 +03:00
|
|
|
qitem(mime!(Image / ("svg+xml") )),
|
|
|
|
QualityItem::new(mime!(Image / _), q(0.8)),
|
|
|
|
QualityItem::new(mime!(_ / _), q(0.5))],
|
2016-02-27 17:52:22 +03:00
|
|
|
|
|
|
|
// Substep 3
|
|
|
|
Type::Style =>
|
2016-04-08 19:30:15 +03:00
|
|
|
vec![qitem(mime!(Text / Css)),
|
|
|
|
QualityItem::new(mime!(_ / _), q(0.1))],
|
2016-02-27 17:52:22 +03:00
|
|
|
// Substep 1
|
2016-04-08 19:30:15 +03:00
|
|
|
_ => vec![qitem(mime!(_ / _))]
|
2015-12-30 05:20:43 +03:00
|
|
|
};
|
|
|
|
|
2016-02-27 17:52:22 +03:00
|
|
|
// Substep 4
|
2016-01-08 10:07:38 +03:00
|
|
|
request.headers.borrow_mut().set(Accept(value));
|
2015-05-20 07:08:41 +03:00
|
|
|
}
|
|
|
|
|
2016-02-27 17:52:22 +03:00
|
|
|
// Step 4
|
2016-11-08 07:04:27 +03:00
|
|
|
set_default_accept_language(&mut request.headers.borrow_mut());
|
2015-05-18 17:23:43 +03:00
|
|
|
|
2016-02-27 17:52:22 +03:00
|
|
|
// Step 5
|
2015-12-30 05:20:43 +03:00
|
|
|
// TODO: Figure out what a Priority object is
|
2016-02-27 17:52:22 +03:00
|
|
|
|
|
|
|
// Step 6
|
|
|
|
if request.is_subresource_request() {
|
|
|
|
// TODO: create a fetch record and append it to request's client's fetch group list
|
|
|
|
}
|
2016-06-12 03:30:28 +03:00
|
|
|
|
2016-02-27 17:52:22 +03:00
|
|
|
// Step 7
|
2016-12-16 01:02:42 +03:00
|
|
|
main_fetch(request, cache, false, false, target, &mut None, &context);
|
2015-12-30 05:20:43 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/// [Main fetch](https://fetch.spec.whatwg.org/#concept-main-fetch)
|
2016-11-26 07:20:26 +03:00
|
|
|
pub fn main_fetch(request: Rc<Request>,
|
|
|
|
cache: &mut CorsCache,
|
|
|
|
cors_flag: bool,
|
|
|
|
recursive_flag: bool,
|
2016-12-16 01:02:42 +03:00
|
|
|
target: Target,
|
2016-11-26 07:20:26 +03:00
|
|
|
done_chan: &mut DoneChannel,
|
|
|
|
context: &FetchContext)
|
|
|
|
-> Response {
|
2015-12-30 05:20:43 +03:00
|
|
|
// TODO: Implement main fetch spec
|
2016-01-30 02:25:27 +03:00
|
|
|
|
|
|
|
// Step 1
|
|
|
|
let mut response = None;
|
|
|
|
|
|
|
|
// Step 2
|
|
|
|
if request.local_urls_only {
|
2016-04-23 21:28:31 +03:00
|
|
|
match request.current_url().scheme() {
|
2016-03-25 10:12:48 +03:00
|
|
|
"about" | "blob" | "data" | "filesystem" => (), // Ok, the URL is local.
|
2016-11-02 11:26:42 +03:00
|
|
|
_ => response = Some(Response::network_error(NetworkError::Internal("Non-local scheme".into())))
|
2016-03-25 10:12:48 +03:00
|
|
|
}
|
2016-01-30 02:25:27 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Step 3
|
|
|
|
// TODO be able to execute report CSP
|
|
|
|
|
|
|
|
// Step 4
|
2016-06-12 03:30:28 +03:00
|
|
|
// TODO this step, based off of http_loader.rs (upgrade)
|
2016-01-30 02:25:27 +03:00
|
|
|
|
|
|
|
// Step 5
|
2016-06-12 03:30:28 +03:00
|
|
|
// TODO this step (CSP port/content blocking)
|
2016-12-24 14:41:14 +03:00
|
|
|
if let Some(port) = request.url().port() {
|
|
|
|
let is_ftp = request.url().scheme() == "ftp" && (port == 20 || port == 21);
|
|
|
|
static BAD_PORTS: [u16; 64] = [1, 7, 9, 11, 13, 15, 17, 19, 20, 21, 22, 23, 25, 37, 42,
|
|
|
|
43, 53, 77, 79, 87, 95, 101, 102, 103, 104, 109, 110, 111,
|
|
|
|
113, 115, 117, 119, 123, 135, 139, 143, 179, 389, 465, 512,
|
|
|
|
513, 514, 515, 526, 530, 531, 532, 540, 556, 563, 587, 601,
|
|
|
|
636, 993, 995, 2049, 3659, 4045, 6000, 6665, 6666, 6667,
|
|
|
|
6668, 6669];
|
|
|
|
if !is_ftp && BAD_PORTS.binary_search(&port).is_ok() {
|
|
|
|
response = Some(Response::network_error(NetworkError::Internal("Request attempted on bad port".into())));
|
|
|
|
}
|
|
|
|
}
|
2016-01-30 02:25:27 +03:00
|
|
|
|
|
|
|
// Step 6
|
2016-09-16 23:43:54 +03:00
|
|
|
// TODO this step (referrer policy)
|
|
|
|
// currently the clients themselves set referrer policy in RequestInit
|
2016-01-30 02:25:27 +03:00
|
|
|
|
|
|
|
// Step 7
|
2016-12-08 22:52:35 +03:00
|
|
|
let referrer_policy = request.referrer_policy.get().unwrap_or(ReferrerPolicy::NoReferrerWhenDowngrade);
|
|
|
|
request.referrer_policy.set(Some(referrer_policy));
|
2016-01-30 02:25:27 +03:00
|
|
|
|
|
|
|
// Step 8
|
2016-12-10 00:13:27 +03:00
|
|
|
{
|
|
|
|
let mut referrer = request.referrer.borrow_mut();
|
|
|
|
let referrer_url = match mem::replace(&mut *referrer, Referrer::NoReferrer) {
|
|
|
|
Referrer::NoReferrer => None,
|
|
|
|
Referrer::Client => {
|
|
|
|
// FIXME(#14507): We should never get this value here; it should
|
|
|
|
// already have been handled in the script thread.
|
|
|
|
request.headers.borrow_mut().remove::<RefererHeader>();
|
|
|
|
None
|
|
|
|
},
|
|
|
|
Referrer::ReferrerUrl(url) => {
|
|
|
|
request.headers.borrow_mut().remove::<RefererHeader>();
|
|
|
|
determine_request_referrer(&mut *request.headers.borrow_mut(),
|
|
|
|
referrer_policy,
|
|
|
|
url,
|
|
|
|
request.current_url().clone())
|
|
|
|
}
|
|
|
|
};
|
|
|
|
if let Some(referrer_url) = referrer_url {
|
|
|
|
*referrer = Referrer::ReferrerUrl(referrer_url);
|
|
|
|
}
|
2016-06-12 03:30:28 +03:00
|
|
|
}
|
2016-01-30 02:25:27 +03:00
|
|
|
|
|
|
|
// Step 9
|
2016-12-29 11:35:09 +03:00
|
|
|
if !request.current_url().is_secure_scheme() && request.current_url().domain().is_some() {
|
|
|
|
if context.state
|
|
|
|
.hsts_list
|
|
|
|
.read()
|
|
|
|
.unwrap()
|
|
|
|
.is_host_secure(request.current_url().domain().unwrap()) {
|
|
|
|
request.url_list.borrow_mut().last_mut().unwrap().as_mut_url().unwrap().set_scheme("https").unwrap();
|
|
|
|
}
|
|
|
|
}
|
2016-06-12 03:30:28 +03:00
|
|
|
|
|
|
|
// Step 10
|
|
|
|
// this step is obsoleted by fetch_async
|
|
|
|
|
|
|
|
// Step 11
|
2016-03-11 05:57:19 +03:00
|
|
|
let response = match response {
|
|
|
|
Some(response) => response,
|
|
|
|
None => {
|
|
|
|
let current_url = request.current_url();
|
|
|
|
let same_origin = if let Origin::Origin(ref origin) = *request.origin.borrow() {
|
|
|
|
*origin == current_url.origin()
|
|
|
|
} else {
|
|
|
|
false
|
|
|
|
};
|
|
|
|
|
|
|
|
if (same_origin && !cors_flag ) ||
|
2016-10-01 13:19:05 +03:00
|
|
|
current_url.scheme() == "data" ||
|
|
|
|
current_url.scheme() == "file" ||
|
2016-04-23 21:28:31 +03:00
|
|
|
current_url.scheme() == "about" ||
|
2016-03-11 05:57:19 +03:00
|
|
|
request.mode == RequestMode::Navigate {
|
2016-06-12 03:30:28 +03:00
|
|
|
basic_fetch(request.clone(), cache, target, done_chan, context)
|
2016-03-11 05:57:19 +03:00
|
|
|
|
|
|
|
} else if request.mode == RequestMode::SameOrigin {
|
2016-11-02 11:26:42 +03:00
|
|
|
Response::network_error(NetworkError::Internal("Cross-origin response".into()))
|
2016-03-11 05:57:19 +03:00
|
|
|
|
2016-11-10 23:43:36 +03:00
|
|
|
} else if request.mode == RequestMode::NoCors {
|
2016-03-11 05:57:19 +03:00
|
|
|
request.response_tainting.set(ResponseTainting::Opaque);
|
2016-06-12 03:30:28 +03:00
|
|
|
basic_fetch(request.clone(), cache, target, done_chan, context)
|
2016-03-11 05:57:19 +03:00
|
|
|
|
2016-04-23 21:28:31 +03:00
|
|
|
} else if !matches!(current_url.scheme(), "http" | "https") {
|
2016-11-02 11:26:42 +03:00
|
|
|
Response::network_error(NetworkError::Internal("Non-http scheme".into()))
|
2016-03-11 05:57:19 +03:00
|
|
|
|
|
|
|
} else if request.use_cors_preflight ||
|
|
|
|
(request.unsafe_request &&
|
|
|
|
(!is_simple_method(&request.method.borrow()) ||
|
|
|
|
request.headers.borrow().iter().any(|h| !is_simple_header(&h)))) {
|
2016-11-10 23:43:36 +03:00
|
|
|
request.response_tainting.set(ResponseTainting::CorsTainting);
|
2016-03-11 05:57:19 +03:00
|
|
|
request.redirect_mode.set(RedirectMode::Error);
|
2016-07-29 19:44:20 +03:00
|
|
|
let response = http_fetch(request.clone(), cache, true, true, false,
|
|
|
|
target, done_chan, context);
|
2016-03-11 05:57:19 +03:00
|
|
|
if response.is_network_error() {
|
|
|
|
// TODO clear cache entries using request
|
|
|
|
}
|
|
|
|
response
|
|
|
|
|
|
|
|
} else {
|
2016-11-10 23:43:36 +03:00
|
|
|
request.response_tainting.set(ResponseTainting::CorsTainting);
|
2016-06-12 03:30:28 +03:00
|
|
|
http_fetch(request.clone(), cache, true, false, false, target, done_chan, context)
|
2016-01-30 02:25:27 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-06-12 03:30:28 +03:00
|
|
|
// Step 12
|
2016-01-30 02:25:27 +03:00
|
|
|
if recursive_flag {
|
|
|
|
return response;
|
|
|
|
}
|
|
|
|
|
2016-06-12 03:30:28 +03:00
|
|
|
// Step 13
|
2016-01-30 02:25:27 +03:00
|
|
|
// no need to check if response is a network error, since the type would not be `Default`
|
2016-03-09 18:21:30 +03:00
|
|
|
let response = if response.response_type == ResponseType::Default {
|
2016-01-30 02:25:27 +03:00
|
|
|
let response_type = match request.response_tainting.get() {
|
|
|
|
ResponseTainting::Basic => ResponseType::Basic,
|
2016-11-10 23:43:36 +03:00
|
|
|
ResponseTainting::CorsTainting => ResponseType::Cors,
|
2016-01-30 02:25:27 +03:00
|
|
|
ResponseTainting::Opaque => ResponseType::Opaque,
|
|
|
|
};
|
2016-03-01 23:19:29 +03:00
|
|
|
response.to_filtered(response_type)
|
2016-01-30 02:25:27 +03:00
|
|
|
} else {
|
|
|
|
response
|
|
|
|
};
|
|
|
|
|
2017-01-08 10:14:37 +03:00
|
|
|
let mut response_loaded = false;
|
2016-03-01 23:19:29 +03:00
|
|
|
{
|
2016-06-12 03:30:28 +03:00
|
|
|
// Step 14
|
2016-11-02 11:26:42 +03:00
|
|
|
let network_error_res;
|
|
|
|
let internal_response = if let Some(error) = response.get_network_error() {
|
|
|
|
network_error_res = Response::network_error(error.clone());
|
2016-03-01 23:19:29 +03:00
|
|
|
&network_error_res
|
|
|
|
} else {
|
2016-04-13 18:58:25 +03:00
|
|
|
response.actual_response()
|
2016-03-01 23:19:29 +03:00
|
|
|
};
|
2016-01-30 02:25:27 +03:00
|
|
|
|
2016-06-12 03:30:28 +03:00
|
|
|
// Step 15
|
|
|
|
if internal_response.url_list.borrow().is_empty() {
|
|
|
|
*internal_response.url_list.borrow_mut() = request.url_list.borrow().clone();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Step 16
|
|
|
|
// TODO this step (CSP/blocking)
|
2016-03-01 23:19:29 +03:00
|
|
|
|
2016-06-12 03:30:28 +03:00
|
|
|
// Step 17
|
2016-03-01 23:19:29 +03:00
|
|
|
if !response.is_network_error() && (is_null_body_status(&internal_response.status) ||
|
|
|
|
match *request.method.borrow() {
|
|
|
|
Method::Head | Method::Connect => true,
|
|
|
|
_ => false })
|
|
|
|
{
|
2016-03-08 02:15:25 +03:00
|
|
|
// when Fetch is used only asynchronously, we will need to make sure
|
|
|
|
// that nothing tries to write to the body at this point
|
|
|
|
let mut body = internal_response.body.lock().unwrap();
|
|
|
|
*body = ResponseBody::Empty;
|
2016-03-01 23:19:29 +03:00
|
|
|
}
|
|
|
|
}
|
2017-01-08 10:14:37 +03:00
|
|
|
// Step 18
|
|
|
|
let response = if !response.is_network_error() && *request.integrity_metadata.borrow() != "" {
|
|
|
|
// Substep 1
|
|
|
|
wait_for_response(&response, target, done_chan);
|
|
|
|
response_loaded = true;
|
|
|
|
|
|
|
|
// Substep 2
|
|
|
|
let ref integrity_metadata = *request.integrity_metadata.borrow();
|
|
|
|
if response.termination_reason.is_none() &&
|
|
|
|
!is_response_integrity_valid(integrity_metadata, &response) {
|
|
|
|
Response::network_error(NetworkError::Internal("Subresource integrity validation failed".into()))
|
|
|
|
} else {
|
|
|
|
response
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
response
|
|
|
|
};
|
2016-01-30 02:25:27 +03:00
|
|
|
|
2016-06-12 03:30:28 +03:00
|
|
|
// Step 19
|
2016-01-30 02:25:27 +03:00
|
|
|
if request.synchronous {
|
2016-12-16 01:02:42 +03:00
|
|
|
// process_response is not supposed to be used
|
|
|
|
// by sync fetch, but we overload it here for simplicity
|
|
|
|
target.process_response(&response);
|
2017-01-08 10:14:37 +03:00
|
|
|
if !response_loaded {
|
|
|
|
wait_for_response(&response, target, done_chan);
|
2016-06-12 03:30:28 +03:00
|
|
|
}
|
|
|
|
// overloaded similarly to process_response
|
2016-12-16 01:02:42 +03:00
|
|
|
target.process_response_eof(&response);
|
2016-01-30 02:25:27 +03:00
|
|
|
return response;
|
|
|
|
}
|
|
|
|
|
2016-06-12 03:30:28 +03:00
|
|
|
// Step 20
|
2016-04-23 21:28:31 +03:00
|
|
|
if request.body.borrow().is_some() && matches!(request.current_url().scheme(), "http" | "https") {
|
2016-12-16 01:02:42 +03:00
|
|
|
// XXXManishearth: We actually should be calling process_request
|
|
|
|
// in http_network_fetch. However, we can't yet follow the request
|
|
|
|
// upload progress, so I'm keeping it here for now and pretending
|
|
|
|
// the body got sent in one chunk
|
|
|
|
target.process_request_body(&request);
|
|
|
|
target.process_request_eof(&request);
|
2016-01-30 02:25:27 +03:00
|
|
|
}
|
|
|
|
|
2016-06-12 03:30:28 +03:00
|
|
|
// Step 21
|
2016-12-16 01:02:42 +03:00
|
|
|
target.process_response(&response);
|
2016-01-30 02:25:27 +03:00
|
|
|
|
2016-06-12 03:30:28 +03:00
|
|
|
// Step 22
|
2017-01-08 10:14:37 +03:00
|
|
|
if !response_loaded {
|
|
|
|
wait_for_response(&response, target, done_chan);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Step 24
|
|
|
|
target.process_response_eof(&response);
|
|
|
|
|
|
|
|
// TODO remove this line when only asynchronous fetches are used
|
|
|
|
return response;
|
|
|
|
}
|
|
|
|
|
|
|
|
fn wait_for_response(response: &Response, target: Target, done_chan: &mut DoneChannel) {
|
2016-06-12 03:30:28 +03:00
|
|
|
if let Some(ref ch) = *done_chan {
|
|
|
|
loop {
|
|
|
|
match ch.1.recv()
|
|
|
|
.expect("fetch worker should always send Done before terminating") {
|
|
|
|
Data::Payload(vec) => {
|
2016-12-16 01:02:42 +03:00
|
|
|
target.process_response_chunk(vec);
|
2017-01-08 10:14:37 +03:00
|
|
|
},
|
2016-06-12 03:30:28 +03:00
|
|
|
Data::Done => break,
|
|
|
|
}
|
|
|
|
}
|
2016-12-16 01:02:42 +03:00
|
|
|
} else {
|
2016-09-22 02:49:33 +03:00
|
|
|
let body = response.body.lock().unwrap();
|
|
|
|
if let ResponseBody::Done(ref vec) = *body {
|
2016-06-12 03:30:28 +03:00
|
|
|
// in case there was no channel to wait for, the body was
|
|
|
|
// obtained synchronously via basic_fetch for data/file/about/etc
|
|
|
|
// We should still send the body across as a chunk
|
|
|
|
target.process_response_chunk(vec.clone());
|
|
|
|
} else {
|
2016-09-22 02:49:33 +03:00
|
|
|
assert!(*body == ResponseBody::Empty)
|
2016-06-12 03:30:28 +03:00
|
|
|
}
|
|
|
|
}
|
2015-12-30 05:20:43 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/// [Basic fetch](https://fetch.spec.whatwg.org#basic-fetch)
|
2016-11-22 03:28:19 +03:00
|
|
|
fn basic_fetch(request: Rc<Request>,
|
|
|
|
cache: &mut CorsCache,
|
2016-12-16 01:02:42 +03:00
|
|
|
target: Target,
|
2016-11-22 03:28:19 +03:00
|
|
|
done_chan: &mut DoneChannel,
|
|
|
|
context: &FetchContext)
|
|
|
|
-> Response {
|
2016-01-08 10:07:38 +03:00
|
|
|
let url = request.current_url();
|
2015-12-30 05:20:43 +03:00
|
|
|
|
2016-04-23 21:28:31 +03:00
|
|
|
match url.scheme() {
|
|
|
|
"about" if url.path() == "blank" => {
|
2016-11-10 18:19:52 +03:00
|
|
|
let mut response = Response::new(url);
|
2016-04-23 21:28:31 +03:00
|
|
|
response.headers.set(ContentType(mime!(Text / Html; Charset = Utf8)));
|
|
|
|
*response.body.lock().unwrap() = ResponseBody::Done(vec![]);
|
|
|
|
response
|
2015-12-30 05:20:43 +03:00
|
|
|
},
|
2014-08-29 11:13:01 +04:00
|
|
|
|
2015-12-30 05:20:43 +03:00
|
|
|
"http" | "https" => {
|
2016-06-12 03:30:28 +03:00
|
|
|
http_fetch(request.clone(), cache, false, false, false, target, done_chan, context)
|
2015-12-30 05:20:43 +03:00
|
|
|
},
|
|
|
|
|
2016-04-01 12:42:30 +03:00
|
|
|
"data" => {
|
|
|
|
if *request.method.borrow() == Method::Get {
|
|
|
|
match decode(&url) {
|
|
|
|
Ok((mime, bytes)) => {
|
2016-11-10 18:19:52 +03:00
|
|
|
let mut response = Response::new(url);
|
2016-04-01 12:42:30 +03:00
|
|
|
*response.body.lock().unwrap() = ResponseBody::Done(bytes);
|
|
|
|
response.headers.set(ContentType(mime));
|
|
|
|
response
|
|
|
|
},
|
2016-11-02 11:26:42 +03:00
|
|
|
Err(_) => Response::network_error(NetworkError::Internal("Decoding data URL failed".into()))
|
2016-04-01 12:42:30 +03:00
|
|
|
}
|
|
|
|
} else {
|
2016-11-02 11:26:42 +03:00
|
|
|
Response::network_error(NetworkError::Internal("Unexpected method for data".into()))
|
2016-04-01 12:42:30 +03:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2016-04-30 08:54:20 +03:00
|
|
|
"file" => {
|
|
|
|
if *request.method.borrow() == Method::Get {
|
|
|
|
match url.to_file_path() {
|
|
|
|
Ok(file_path) => {
|
2016-11-02 11:26:42 +03:00
|
|
|
match File::open(file_path.clone()) {
|
|
|
|
Ok(mut file) => {
|
|
|
|
let mut bytes = vec![];
|
|
|
|
let _ = file.read_to_end(&mut bytes);
|
|
|
|
let mime = guess_mime_type(file_path);
|
|
|
|
|
2016-11-10 18:19:52 +03:00
|
|
|
let mut response = Response::new(url);
|
2016-11-02 11:26:42 +03:00
|
|
|
*response.body.lock().unwrap() = ResponseBody::Done(bytes);
|
|
|
|
response.headers.set(ContentType(mime));
|
|
|
|
response
|
|
|
|
},
|
|
|
|
_ => Response::network_error(NetworkError::Internal("Opening file failed".into())),
|
|
|
|
}
|
2016-04-30 08:54:20 +03:00
|
|
|
},
|
2016-11-02 11:26:42 +03:00
|
|
|
_ => Response::network_error(NetworkError::Internal("Constructing file path failed".into()))
|
2016-04-30 08:54:20 +03:00
|
|
|
}
|
|
|
|
} else {
|
2016-11-02 11:26:42 +03:00
|
|
|
Response::network_error(NetworkError::Internal("Unexpected method for file".into()))
|
2016-04-30 08:54:20 +03:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2016-10-14 15:20:05 +03:00
|
|
|
"blob" => {
|
|
|
|
println!("Loading blob {}", url.as_str());
|
|
|
|
// Step 2.
|
|
|
|
if *request.method.borrow() != Method::Get {
|
2016-11-02 11:26:42 +03:00
|
|
|
return Response::network_error(NetworkError::Internal("Unexpected method for blob".into()));
|
2016-10-14 15:20:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
match load_blob_sync(url.clone(), context.filemanager.clone()) {
|
|
|
|
Ok((headers, bytes)) => {
|
2016-11-10 18:19:52 +03:00
|
|
|
let mut response = Response::new(url);
|
2016-10-14 15:20:05 +03:00
|
|
|
response.headers = headers;
|
|
|
|
*response.body.lock().unwrap() = ResponseBody::Done(bytes);
|
|
|
|
response
|
|
|
|
},
|
|
|
|
Err(e) => {
|
|
|
|
debug!("Failed to load {}: {:?}", url, e);
|
2016-11-02 11:26:42 +03:00
|
|
|
Response::network_error(e)
|
2016-10-14 15:20:05 +03:00
|
|
|
},
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
"ftp" => {
|
2016-11-02 18:59:18 +03:00
|
|
|
debug!("ftp is not implemented");
|
|
|
|
Response::network_error(NetworkError::Internal("Unexpected scheme".into()))
|
2015-12-30 05:20:43 +03:00
|
|
|
},
|
|
|
|
|
2016-11-02 11:26:42 +03:00
|
|
|
_ => Response::network_error(NetworkError::Internal("Unexpected scheme".into()))
|
2014-08-29 11:13:01 +04:00
|
|
|
}
|
2015-12-30 05:20:43 +03:00
|
|
|
}
|
|
|
|
|
2016-06-12 03:30:28 +03:00
|
|
|
/// https://fetch.spec.whatwg.org/#cors-safelisted-request-header
|
2016-11-26 07:20:26 +03:00
|
|
|
pub fn is_simple_header(h: &HeaderView) -> bool {
|
2015-05-18 17:23:43 +03:00
|
|
|
if h.is::<ContentType>() {
|
|
|
|
match h.value() {
|
|
|
|
Some(&ContentType(Mime(TopLevel::Text, SubLevel::Plain, _))) |
|
|
|
|
Some(&ContentType(Mime(TopLevel::Application, SubLevel::WwwFormUrlEncoded, _))) |
|
|
|
|
Some(&ContentType(Mime(TopLevel::Multipart, SubLevel::FormData, _))) => true,
|
|
|
|
_ => false
|
|
|
|
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
h.is::<Accept>() || h.is::<AcceptLanguage>() || h.is::<ContentLanguage>()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-26 07:20:26 +03:00
|
|
|
pub fn is_simple_method(m: &Method) -> bool {
|
2015-05-18 17:23:43 +03:00
|
|
|
match *m {
|
|
|
|
Method::Get | Method::Head | Method::Post => true,
|
|
|
|
_ => false
|
|
|
|
}
|
2014-08-03 21:18:02 +04:00
|
|
|
}
|
2015-12-30 05:20:43 +03:00
|
|
|
|
|
|
|
// fn modify_request_headers(headers: &mut Headers) -> {
|
|
|
|
// // TODO this function
|
|
|
|
|
|
|
|
// }
|
2016-01-30 02:25:27 +03:00
|
|
|
|
|
|
|
fn is_null_body_status(status: &Option<StatusCode>) -> bool {
|
|
|
|
match *status {
|
|
|
|
Some(status) => match status {
|
|
|
|
StatusCode::SwitchingProtocols | StatusCode::NoContent |
|
|
|
|
StatusCode::ResetContent | StatusCode::NotModified => true,
|
|
|
|
_ => false
|
|
|
|
},
|
|
|
|
_ => false
|
|
|
|
}
|
|
|
|
}
|
2017-01-08 10:14:37 +03:00
|
|
|
|