22 * License, v. 2.0. If a copy of the MPL was not distributed with this
33 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
44
5+ use blob_loader:: load_blob_sync;
56use connector:: create_http_connector;
67use data_loader:: decode;
78use devtools_traits:: DevtoolsControlMsg ;
89use fetch:: cors_cache:: CORSCache ;
10+ use filemanager_thread:: { FileManager , UIProvider } ;
911use http_loader:: { HttpState , set_default_accept_encoding, set_request_cookies} ;
1012use http_loader:: { NetworkHttpRequestFactory , ReadResult , StreamedResponse , obtain_response, read_block} ;
1113use http_loader:: { auth_from_cache, determine_request_referrer} ;
@@ -50,23 +52,28 @@ enum Data {
5052 Done ,
5153}
5254
53- pub struct FetchContext {
55+ pub struct FetchContext < UI : ' static + UIProvider > {
5456 pub state : HttpState ,
5557 pub user_agent : Cow < ' static , str > ,
5658 pub devtools_chan : Option < Sender < DevtoolsControlMsg > > ,
59+ pub filemanager : FileManager < UI > ,
5760}
5861
5962type DoneChannel = Option < ( Sender < Data > , Receiver < Data > ) > ;
6063
6164/// [Fetch](https://fetch.spec.whatwg.org#concept-fetch)
62- pub fn fetch ( request : Rc < Request > , target : & mut Target , context : FetchContext ) -> Response {
65+ pub fn fetch < UI : ' static + UIProvider > ( request : Rc < Request > ,
66+ target : & mut Target ,
67+ context : FetchContext < UI > )
68+ -> Response {
6369 fetch_with_cors_cache ( request, & mut CORSCache :: new ( ) , target, context)
6470}
6571
66- pub fn fetch_with_cors_cache ( request : Rc < Request > ,
67- cache : & mut CORSCache ,
68- target : & mut Target ,
69- context : FetchContext ) -> Response {
72+ pub fn fetch_with_cors_cache < UI : ' static + UIProvider > ( request : Rc < Request > ,
73+ cache : & mut CORSCache ,
74+ target : & mut Target ,
75+ context : FetchContext < UI > )
76+ -> Response {
7077 // Step 1
7178 if request. window . get ( ) == Window :: Client {
7279 // TODO: Set window to request's client object if client is a Window object
@@ -131,9 +138,14 @@ pub fn fetch_with_cors_cache(request: Rc<Request>,
131138}
132139
133140/// [Main fetch](https://fetch.spec.whatwg.org/#concept-main-fetch)
134- fn main_fetch ( request : Rc < Request > , cache : & mut CORSCache , cors_flag : bool ,
135- recursive_flag : bool , target : & mut Target , done_chan : & mut DoneChannel ,
136- context : & FetchContext ) -> Response {
141+ fn main_fetch < UI : ' static + UIProvider > ( request : Rc < Request > ,
142+ cache : & mut CORSCache ,
143+ cors_flag : bool ,
144+ recursive_flag : bool ,
145+ target : & mut Target ,
146+ done_chan : & mut DoneChannel ,
147+ context : & FetchContext < UI > )
148+ -> Response {
137149 // TODO: Implement main fetch spec
138150
139151 // Step 1
@@ -389,9 +401,12 @@ fn main_fetch(request: Rc<Request>, cache: &mut CORSCache, cors_flag: bool,
389401}
390402
391403/// [Basic fetch](https://fetch.spec.whatwg.org#basic-fetch)
392- fn basic_fetch ( request : Rc < Request > , cache : & mut CORSCache ,
393- target : & mut Target , done_chan : & mut DoneChannel ,
394- context : & FetchContext ) -> Response {
404+ fn basic_fetch < UI : ' static + UIProvider > ( request : Rc < Request > ,
405+ cache : & mut CORSCache ,
406+ target : & mut Target ,
407+ done_chan : & mut DoneChannel ,
408+ context : & FetchContext < UI > )
409+ -> Response {
395410 let url = request. current_url ( ) ;
396411
397412 match url. scheme ( ) {
@@ -450,7 +465,29 @@ fn basic_fetch(request: Rc<Request>, cache: &mut CORSCache,
450465 }
451466 } ,
452467
453- "blob" | "ftp" => {
468+ "blob" => {
469+ println ! ( "Loading blob {}" , url. as_str( ) ) ;
470+ // Step 2.
471+ if * request. method . borrow ( ) != Method :: Get {
472+ return Response :: network_error ( ) ;
473+ }
474+
475+ match load_blob_sync ( url. clone ( ) , context. filemanager . clone ( ) ) {
476+ Ok ( ( headers, bytes) ) => {
477+ let mut response = Response :: new ( ) ;
478+ response. url = Some ( url. clone ( ) ) ;
479+ response. headers = headers;
480+ * response. body . lock ( ) . unwrap ( ) = ResponseBody :: Done ( bytes) ;
481+ response
482+ } ,
483+ Err ( e) => {
484+ debug ! ( "Failed to load {}: {:?}" , url, e) ;
485+ Response :: network_error ( )
486+ } ,
487+ }
488+ } ,
489+
490+ "ftp" => {
454491 // XXXManishearth handle these
455492 panic ! ( "Unimplemented scheme for Fetch" )
456493 } ,
@@ -460,14 +497,15 @@ fn basic_fetch(request: Rc<Request>, cache: &mut CORSCache,
460497}
461498
462499/// [HTTP fetch](https://fetch.spec.whatwg.org#http-fetch)
463- fn http_fetch ( request : Rc < Request > ,
464- cache : & mut CORSCache ,
465- cors_flag : bool ,
466- cors_preflight_flag : bool ,
467- authentication_fetch_flag : bool ,
468- target : & mut Target ,
469- done_chan : & mut DoneChannel ,
470- context : & FetchContext ) -> Response {
500+ fn http_fetch < UI : ' static + UIProvider > ( request : Rc < Request > ,
501+ cache : & mut CORSCache ,
502+ cors_flag : bool ,
503+ cors_preflight_flag : bool ,
504+ authentication_fetch_flag : bool ,
505+ target : & mut Target ,
506+ done_chan : & mut DoneChannel ,
507+ context : & FetchContext < UI > )
508+ -> Response {
471509 // This is a new async fetch, reset the channel we are waiting on
472510 * done_chan = None ;
473511 // Step 1
@@ -631,13 +669,14 @@ fn http_fetch(request: Rc<Request>,
631669}
632670
633671/// [HTTP redirect fetch](https://fetch.spec.whatwg.org#http-redirect-fetch)
634- fn http_redirect_fetch ( request : Rc < Request > ,
635- cache : & mut CORSCache ,
636- response : Rc < Response > ,
637- cors_flag : bool ,
638- target : & mut Target ,
639- done_chan : & mut DoneChannel ,
640- context : & FetchContext ) -> Response {
672+ fn http_redirect_fetch < UI : ' static + UIProvider > ( request : Rc < Request > ,
673+ cache : & mut CORSCache ,
674+ response : Rc < Response > ,
675+ cors_flag : bool ,
676+ target : & mut Target ,
677+ done_chan : & mut DoneChannel ,
678+ context : & FetchContext < UI > )
679+ -> Response {
641680 // Step 1
642681 assert_eq ! ( response. return_internal. get( ) , true ) ;
643682
@@ -711,11 +750,12 @@ fn http_redirect_fetch(request: Rc<Request>,
711750}
712751
713752/// [HTTP network or cache fetch](https://fetch.spec.whatwg.org#http-network-or-cache-fetch)
714- fn http_network_or_cache_fetch ( request : Rc < Request > ,
715- credentials_flag : bool ,
716- authentication_fetch_flag : bool ,
717- done_chan : & mut DoneChannel ,
718- context : & FetchContext ) -> Response {
753+ fn http_network_or_cache_fetch < UI : ' static + UIProvider > ( request : Rc < Request > ,
754+ credentials_flag : bool ,
755+ authentication_fetch_flag : bool ,
756+ done_chan : & mut DoneChannel ,
757+ context : & FetchContext < UI > )
758+ -> Response {
719759 // TODO: Implement Window enum for Request
720760 let request_has_no_window = true ;
721761
@@ -1108,8 +1148,10 @@ fn http_network_fetch(request: Rc<Request>,
11081148}
11091149
11101150/// [CORS preflight fetch](https://fetch.spec.whatwg.org#cors-preflight-fetch)
1111- fn cors_preflight_fetch ( request : Rc < Request > , cache : & mut CORSCache ,
1112- context : & FetchContext ) -> Response {
1151+ fn cors_preflight_fetch < UI : ' static + UIProvider > ( request : Rc < Request > ,
1152+ cache : & mut CORSCache ,
1153+ context : & FetchContext < UI > )
1154+ -> Response {
11131155 // Step 1
11141156 let mut preflight = Request :: new ( request. current_url ( ) , Some ( request. origin . borrow ( ) . clone ( ) ) ,
11151157 request. is_service_worker_global_scope , request. pipeline_id . get ( ) ) ;
0 commit comments