Revamp Fetch async handling to use a fetch target and done channels

This commit is contained in:
Manish Goregaokar 2016-05-26 16:38:22 +05:30
parent 96f3404928
commit b5255f011e
5 changed files with 158 additions and 106 deletions

View file

@ -14,13 +14,14 @@ use hyper::server::{Request as HyperRequest, Response as HyperResponse};
use hyper::status::StatusCode;
use hyper::uri::RequestUri;
use net::fetch::cors_cache::CORSCache;
use net::fetch::methods::{fetch, fetch_async, fetch_with_cors_cache};
use net_traits::AsyncFetchListener;
use net::fetch::methods::{fetch, fetch_with_cors_cache};
use net_traits::FetchTaskTarget;
use net_traits::request::{Origin, RedirectMode, Referer, Request, RequestMode};
use net_traits::response::{CacheState, Response, ResponseBody, ResponseType};
use std::fs::File;
use std::io::Read;
use std::rc::Rc;
use std::thread;
use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::mpsc::{Sender, channel};
use std::sync::{Arc, Mutex};
@ -35,12 +36,22 @@ struct FetchResponseCollector {
sender: Sender<Response>,
}
impl AsyncFetchListener for FetchResponseCollector {
fn response_available(&self, response: Response) {
let _ = self.sender.send(response);
impl FetchTaskTarget for FetchResponseCollector {
fn process_request_body(&mut self, _: &Request) {}
fn process_request_eof(&mut self, _: &Request) {}
fn process_response(&mut self, _: &Response) {}
/// Fired when the response is fully fetched
fn process_response_eof(&mut self, response: &Response) {
self.sender.send(response.clone());
}
}
fn fetch_async(request: Request, target: Box<FetchTaskTarget + Send>) {
thread::spawn(move || {
fetch(Rc::new(request), Some(target));
});
}
fn make_server<H: Handler + 'static>(handler: H) -> (Listening, Url) {
// this is a Listening server because of handle_threads()
let server = Server::http("0.0.0.0:0").unwrap().handle_threads(handler, 1).unwrap();
@ -64,7 +75,7 @@ fn test_fetch_response_is_not_network_error() {
request.referer = Referer::NoReferer;
let wrapped_request = Rc::new(request);
let fetch_response = fetch(wrapped_request);
let fetch_response = fetch(wrapped_request, None);
let _ = server.close();
if fetch_response.is_network_error() {
@ -85,7 +96,7 @@ fn test_fetch_response_body_matches_const_message() {
request.referer = Referer::NoReferer;
let wrapped_request = Rc::new(request);
let fetch_response = fetch(wrapped_request);
let fetch_response = fetch(wrapped_request, None);
let _ = server.close();
assert!(!fetch_response.is_network_error());
@ -107,7 +118,7 @@ fn test_fetch_aboutblank() {
request.referer = Referer::NoReferer;
let wrapped_request = Rc::new(request);
let fetch_response = fetch(wrapped_request);
let fetch_response = fetch(wrapped_request, None);
assert!(!fetch_response.is_network_error());
assert!(*fetch_response.body.lock().unwrap() == ResponseBody::Done(vec![]));
}
@ -119,7 +130,7 @@ fn test_fetch_data() {
let request = Request::new(url, Some(origin), false);
request.same_origin_data.set(true);
let expected_resp_body = "<p>Servo</p>".to_owned();
let fetch_response = fetch(Rc::new(request));
let fetch_response = fetch(Rc::new(request), None);
assert!(!fetch_response.is_network_error());
assert_eq!(fetch_response.headers.len(), 1);
@ -148,7 +159,7 @@ fn test_fetch_file() {
let request = Request::new(url, Some(origin), false);
request.same_origin_data.set(true);
let fetch_response = fetch(Rc::new(request));
let fetch_response = fetch(Rc::new(request), None);
assert!(!fetch_response.is_network_error());
assert_eq!(fetch_response.headers.len(), 1);
let content_type: &ContentType = fetch_response.headers.get().unwrap();
@ -192,7 +203,7 @@ fn test_cors_preflight_fetch() {
request.mode = RequestMode::CORSMode;
let wrapped_request = Rc::new(request);
let fetch_response = fetch(wrapped_request);
let fetch_response = fetch(wrapped_request, None);
let _ = server.close();
assert!(!fetch_response.is_network_error());
@ -232,8 +243,8 @@ fn test_cors_preflight_cache_fetch() {
let wrapped_request0 = Rc::new(request.clone());
let wrapped_request1 = Rc::new(request);
let fetch_response0 = fetch_with_cors_cache(wrapped_request0.clone(), &mut cache);
let fetch_response1 = fetch_with_cors_cache(wrapped_request1.clone(), &mut cache);
let fetch_response0 = fetch_with_cors_cache(wrapped_request0.clone(), &mut cache, None);
let fetch_response1 = fetch_with_cors_cache(wrapped_request1.clone(), &mut cache, None);
let _ = server.close();
assert!(!fetch_response0.is_network_error() && !fetch_response1.is_network_error());
@ -281,7 +292,7 @@ fn test_cors_preflight_fetch_network_error() {
request.mode = RequestMode::CORSMode;
let wrapped_request = Rc::new(request);
let fetch_response = fetch(wrapped_request);
let fetch_response = fetch(wrapped_request, None);
let _ = server.close();
assert!(fetch_response.is_network_error());
@ -304,7 +315,7 @@ fn test_fetch_response_is_basic_filtered() {
request.referer = Referer::NoReferer;
let wrapped_request = Rc::new(request);
let fetch_response = fetch(wrapped_request);
let fetch_response = fetch(wrapped_request, None);
let _ = server.close();
assert!(!fetch_response.is_network_error());
@ -352,7 +363,7 @@ fn test_fetch_response_is_cors_filtered() {
request.mode = RequestMode::CORSMode;
let wrapped_request = Rc::new(request);
let fetch_response = fetch(wrapped_request);
let fetch_response = fetch(wrapped_request, None);
let _ = server.close();
assert!(!fetch_response.is_network_error());
@ -385,7 +396,7 @@ fn test_fetch_response_is_opaque_filtered() {
request.referer = Referer::NoReferer;
let wrapped_request = Rc::new(request);
let fetch_response = fetch(wrapped_request);
let fetch_response = fetch(wrapped_request, None);
let _ = server.close();
assert!(!fetch_response.is_network_error());
@ -435,7 +446,7 @@ fn test_fetch_response_is_opaque_redirect_filtered() {
request.redirect_mode.set(RedirectMode::Manual);
let wrapped_request = Rc::new(request);
let fetch_response = fetch(wrapped_request);
let fetch_response = fetch(wrapped_request, None);
let _ = server.close();
assert!(!fetch_response.is_network_error());
@ -473,7 +484,7 @@ fn test_fetch_with_local_urls_only() {
request.local_urls_only = true;
let wrapped_request = Rc::new(request);
fetch(wrapped_request)
fetch(wrapped_request, None)
};
let local_url = Url::parse("about:blank").unwrap();
@ -512,7 +523,7 @@ fn setup_server_and_fetch(message: &'static [u8], redirect_cap: u32) -> Response
request.referer = Referer::NoReferer;
let wrapped_request = Rc::new(request);
let fetch_response = fetch(wrapped_request);
let fetch_response = fetch(wrapped_request, None);
let _ = server.close();
fetch_response
}
@ -598,7 +609,7 @@ fn test_fetch_redirect_updates_method_runner(tx: Sender<bool>, status_code: Stat
*request.method.borrow_mut() = method;
let wrapped_request = Rc::new(request);
let _ = fetch(wrapped_request);
let _ = fetch(wrapped_request, None);
let _ = server.close();
}