mirror of
https://github.com/servo/servo.git
synced 2025-08-06 06:00:15 +01:00
Auto merge of #8523 - Wafflespeanut:redirects, r=jdm
Listen for cancellation message during loads and redirects... fixes #8495 <!-- Reviewable:start --> [<img src="https://reviewable.io/review_button.png" height=40 alt="Review on Reviewable"/>](https://reviewable.io/reviews/servo/servo/8523) <!-- Reviewable:end -->
This commit is contained in:
commit
bda46179b6
3 changed files with 125 additions and 37 deletions
|
@ -115,7 +115,10 @@ fn load_for_consumer(load_data: LoadData,
|
|||
let factory = NetworkHttpRequestFactory {
|
||||
connector: connector,
|
||||
};
|
||||
match load::<WrappedHttpRequest>(load_data, hsts_list, cookie_jar, devtools_chan, &factory, user_agent) {
|
||||
match load::<WrappedHttpRequest>(load_data, hsts_list,
|
||||
cookie_jar, devtools_chan,
|
||||
&factory, user_agent,
|
||||
&cancel_listener) {
|
||||
Err(LoadError::UnsupportedScheme(url)) => {
|
||||
let s = format!("{} request, but we don't support that scheme", &*url.scheme);
|
||||
send_error(url, s, start_chan)
|
||||
|
@ -127,6 +130,7 @@ fn load_for_consumer(load_data: LoadData,
|
|||
send_error(url, "too many redirects".to_owned(), start_chan)
|
||||
}
|
||||
Err(LoadError::Cors(url, msg)) |
|
||||
Err(LoadError::Cancelled(url, msg)) |
|
||||
Err(LoadError::InvalidRedirect(url, msg)) |
|
||||
Err(LoadError::Decoding(url, msg)) => {
|
||||
send_error(url, msg, start_chan)
|
||||
|
@ -143,7 +147,7 @@ fn load_for_consumer(load_data: LoadData,
|
|||
Err(LoadError::ConnectionAborted(_)) => unreachable!(),
|
||||
Ok(mut load_response) => {
|
||||
let metadata = load_response.metadata.clone();
|
||||
send_data(&mut load_response, start_chan, metadata, classifier, cancel_listener)
|
||||
send_data(&mut load_response, start_chan, metadata, classifier, &cancel_listener)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -291,6 +295,7 @@ pub enum LoadError {
|
|||
Decoding(Url, String),
|
||||
MaxRedirects(Url),
|
||||
ConnectionAborted(String),
|
||||
Cancelled(Url, String),
|
||||
}
|
||||
|
||||
fn set_default_accept_encoding(headers: &mut Headers) {
|
||||
|
@ -516,7 +521,8 @@ pub fn load<A>(load_data: LoadData,
|
|||
cookie_jar: Arc<RwLock<CookieStorage>>,
|
||||
devtools_chan: Option<Sender<DevtoolsControlMsg>>,
|
||||
request_factory: &HttpRequestFactory<R=A>,
|
||||
user_agent: String)
|
||||
user_agent: String,
|
||||
cancel_listener: &CancellationListener)
|
||||
-> Result<StreamedResponse<A::R>, LoadError> where A: HttpRequest + 'static {
|
||||
// FIXME: At the time of writing this FIXME, servo didn't have any central
|
||||
// location for configuration. If you're reading this and such a
|
||||
|
@ -531,6 +537,10 @@ pub fn load<A>(load_data: LoadData,
|
|||
let mut redirected_to = HashSet::new();
|
||||
let mut method = load_data.method.clone();
|
||||
|
||||
if cancel_listener.is_cancelled() {
|
||||
return Err(LoadError::Cancelled(url, "load cancelled".to_owned()));
|
||||
}
|
||||
|
||||
// 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
|
||||
|
@ -558,6 +568,10 @@ pub fn load<A>(load_data: LoadData,
|
|||
return Err(LoadError::UnsupportedScheme(url));
|
||||
}
|
||||
|
||||
if cancel_listener.is_cancelled() {
|
||||
return Err(LoadError::Cancelled(url, "load cancelled".to_owned()));
|
||||
}
|
||||
|
||||
info!("requesting {}", url.serialize());
|
||||
|
||||
// Avoid automatically preserving request headers when redirects occur.
|
||||
|
@ -586,6 +600,10 @@ pub fn load<A>(load_data: LoadData,
|
|||
let mut req = try!(request_factory.create(url.clone(), method.clone()));
|
||||
*req.headers_mut() = request_headers.clone();
|
||||
|
||||
if cancel_listener.is_cancelled() {
|
||||
return Err(LoadError::Cancelled(url, "load cancelled".to_owned()));
|
||||
}
|
||||
|
||||
if log_enabled!(log::LogLevel::Info) {
|
||||
info!("{}", method);
|
||||
for header in req.headers_mut().iter() {
|
||||
|
@ -623,7 +641,7 @@ pub fn load<A>(load_data: LoadData,
|
|||
method.clone(), request_headers.clone(),
|
||||
cloned_data, pipeline_id
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
response = match maybe_response {
|
||||
Ok(r) => r,
|
||||
|
@ -715,7 +733,7 @@ fn send_data<R: Read>(reader: &mut R,
|
|||
start_chan: LoadConsumer,
|
||||
metadata: Metadata,
|
||||
classifier: Arc<MIMEClassifier>,
|
||||
cancel_listener: CancellationListener) {
|
||||
cancel_listener: &CancellationListener) {
|
||||
let (progress_chan, mut chunk) = {
|
||||
let buf = match read_block(reader) {
|
||||
Ok(ReadResult::Payload(buf)) => buf,
|
||||
|
|
|
@ -208,6 +208,16 @@ pub struct CancellableResource {
|
|||
resource_task: ResourceTask,
|
||||
}
|
||||
|
||||
impl CancellableResource {
|
||||
pub fn new(receiver: Receiver<()>, res_id: ResourceId, res_task: ResourceTask) -> CancellableResource {
|
||||
CancellableResource {
|
||||
cancel_receiver: receiver,
|
||||
resource_id: res_id,
|
||||
resource_task: res_task,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// A listener which is basically a wrapped optional receiver which looks
|
||||
/// for the load cancellation message. Some of the loading processes always keep
|
||||
/// an eye out for this message and stop loading stuff once they receive it.
|
||||
|
@ -313,11 +323,7 @@ impl ResourceManager {
|
|||
let (cancel_sender, cancel_receiver) = channel();
|
||||
self.cancel_load_map.insert(current_res_id, cancel_sender);
|
||||
self.next_resource_id.0 += 1;
|
||||
CancellableResource {
|
||||
cancel_receiver: cancel_receiver,
|
||||
resource_id: current_res_id,
|
||||
resource_task: resource_task,
|
||||
}
|
||||
CancellableResource::new(cancel_receiver, current_res_id, resource_task)
|
||||
});
|
||||
|
||||
let cancel_listener = CancellationListener::new(cancel_resource);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue