Skip to content

Commit

Permalink
Testing cancellation during redirects
Browse files Browse the repository at this point in the history
  • Loading branch information
wafflespeanut committed Nov 14, 2015
1 parent 4f855dc commit afb9b07
Show file tree
Hide file tree
Showing 3 changed files with 125 additions and 37 deletions.
28 changes: 23 additions & 5 deletions components/net/http_loader.rs
Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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)
}
}
}
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
16 changes: 11 additions & 5 deletions components/net/resource_task.rs
Expand Up @@ -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.
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit afb9b07

Please sign in to comment.