@@ -5,8 +5,15 @@ use std::rc::Rc;
55
66use crate :: {
77 css:: { cssom:: CSSStyleSheet , parser:: parse_stylesheet, tokenize:: tokenize} ,
8+ globals:: {
9+ ERROR , ERROR_PAGE_PATH , NEW_TAB , NEW_TAB_PAGE_PATH , NO_CONNECTION , NO_CONNECTION_PAGE_PATH ,
10+ NO_CONNECTION_URL ,
11+ } ,
812 html5:: { dom:: Document , parse:: Parser } ,
9- http:: { Client , Header , Protocol , Request , url:: URL } ,
13+ http:: {
14+ Client , Header , Protocol , Request , RequestIntegrityError , RequestIntegrityErrorKind ,
15+ url:: URL ,
16+ } ,
1017 infra:: { InputStream , Serializable } ,
1118} ;
1219
@@ -69,8 +76,11 @@ impl Agent {
6976 } ) ;
7077
7178 let response = match _response {
72- Some ( resp) => resp,
73- None => return None ,
79+ Ok ( resp) => resp,
80+ Err ( _) => {
81+ eprintln ! ( "Failed to fetch stylesheet URL: {}" , resolved_url) ;
82+ return None ;
83+ }
7484 } ;
7585
7686 match response. body {
@@ -96,25 +106,48 @@ impl Agent {
96106 return Some ( css_content) ;
97107 }
98108
109+ fn open_error_page ( & mut self , error : RequestIntegrityError ) -> Rc < RefCell < Document > > {
110+ let url = match error. kind {
111+ RequestIntegrityErrorKind :: NoConnection => NO_CONNECTION_URL ,
112+ RequestIntegrityErrorKind :: InvalidMethod => "harbor:invalidmethod" ,
113+ RequestIntegrityErrorKind :: InvalidHeaders => "harbor:invalidheaders" ,
114+ RequestIntegrityErrorKind :: InvalidBody => "harbor:invalidbody" ,
115+ } ;
116+
117+ self . open ( url) . unwrap ( )
118+ }
119+
99120 pub fn open ( & mut self , url : & str ) -> Option < Rc < RefCell < Document > > > {
100121 if url. starts_with ( "harbor:" ) {
101122 let path = url. trim_start_matches ( "harbor:" ) . to_string ( ) ;
102123
103- match path. as_str ( ) {
104- "new" => {
105- let html_content = fs:: read_to_string ( "res/pages/tab.html" ) . unwrap ( ) ;
106- let mut stream =
107- InputStream :: new ( & html_content. chars ( ) . collect :: < Vec < char > > ( ) [ ..] ) ;
124+ let path = match path. as_str ( ) {
125+ NEW_TAB => NEW_TAB_PAGE_PATH ,
126+ NO_CONNECTION => NO_CONNECTION_PAGE_PATH ,
127+ ERROR => ERROR_PAGE_PATH ,
128+ _ => ERROR_PAGE_PATH ,
129+ } ;
130+
131+ println ! ( "Path: {}" , path) ;
132+ let html_content = fs:: read_to_string ( path) . unwrap ( ) ;
133+ let mut stream = InputStream :: new ( & html_content. chars ( ) . collect :: < Vec < char > > ( ) [ ..] ) ;
134+
135+ let document = Parser :: parse_stream ( & mut stream) ;
136+ document
137+ . borrow_mut ( )
138+ . insert_stylesheet ( 0 , self . ua_stylesheet . clone ( ) ) ;
108139
109- let document = Parser :: parse_stream ( & mut stream) ;
110- document
111- . borrow_mut ( )
112- . insert_stylesheet ( 0 , self . ua_stylesheet . clone ( ) ) ;
140+ let this_url = format ! (
141+ "file://{}/{}" ,
142+ std:: env:: current_dir( ) . unwrap( ) . to_str( ) . unwrap( ) ,
143+ path
144+ ) ;
113145
114- return Some ( document) ;
115- }
116- _ => return None ,
117- }
146+ println ! ( "This URL: {}" , this_url) ;
147+
148+ self . handle_link_elements ( & this_url, & document) ;
149+
150+ return Some ( document) ;
118151 }
119152
120153 let maybe_resolved_url = URL :: pure_parse ( url. to_string ( ) ) ;
@@ -144,8 +177,8 @@ impl Agent {
144177 } ) ;
145178
146179 let response = match _response {
147- Some ( resp) => resp,
148- None => return None ,
180+ Ok ( resp) => resp,
181+ Err ( e ) => return Some ( self . open_error_page ( e ) ) ,
149182 } ;
150183
151184 match response. body {
@@ -167,27 +200,33 @@ impl Agent {
167200 . borrow_mut ( )
168201 . insert_stylesheet ( 0 , self . ua_stylesheet . clone ( ) ) ;
169202
170- let links = document . borrow ( ) . get_links ( ) ;
203+ self . handle_link_elements ( url , & document ) ;
171204
172- for link in links {
173- if !link
174- . borrow ( )
175- . rel_list ( )
176- . contains ( & String :: from ( "stylesheet" ) )
177- {
178- continue ;
179- }
205+ self . cached_pages . insert ( resolved_url, Rc :: clone ( & document) ) ;
180206
181- if let Some ( stylesheet) = self . fetch_stylesheet ( & link. borrow ( ) . href ) {
182- document
183- . borrow_mut ( )
184- . insert_stylesheet ( 0 , stylesheet. clone ( ) ) ;
185- }
207+ return Some ( document) ;
208+ }
209+ }
210+
211+ fn handle_link_elements ( & mut self , root_url : & str , document : & Rc < RefCell < Document > > ) {
212+ let links = document. borrow ( ) . get_links ( ) ;
213+
214+ for link in links {
215+ if !link
216+ . borrow ( )
217+ . rel_list ( )
218+ . contains ( & String :: from ( "stylesheet" ) )
219+ {
220+ continue ;
186221 }
187222
188- self . cached_pages . insert ( resolved_url , Rc :: clone ( & document ) ) ;
223+ let joint = URL :: join_urls ( root_url . to_string ( ) , link . borrow ( ) . href . clone ( ) ) . unwrap ( ) ;
189224
190- return Some ( document) ;
225+ if let Some ( stylesheet) = self . fetch_stylesheet ( joint. serialize ( ) . as_str ( ) ) {
226+ document
227+ . borrow_mut ( )
228+ . insert_stylesheet ( 0 , stylesheet. clone ( ) ) ;
229+ }
191230 }
192231 }
193232}
0 commit comments