[librsvg: 7/22] css::parse_into_css_styles() - Take a base_url, not a handle



commit a5ae9bba2fdaa6eecf77f80b09c9d30a473f844a
Author: Federico Mena Quintero <federico gnome org>
Date:   Mon Jan 7 19:41:55 2019 -0600

    css::parse_into_css_styles() - Take a base_url, not a handle
    
    Relatedly, make handle::load_css() take an AllowedUrl, not a string.
    
    This removes usage of RsvgHandle from css.rs.

 rsvg_internals/src/allowed_url.rs |  6 ++++++
 rsvg_internals/src/css.rs         | 18 ++++++++++++------
 rsvg_internals/src/handle.rs      | 22 +++++-----------------
 rsvg_internals/src/xml.rs         | 19 +++++++++++++------
 4 files changed, 36 insertions(+), 29 deletions(-)
---
diff --git a/rsvg_internals/src/allowed_url.rs b/rsvg_internals/src/allowed_url.rs
index 63e79577..bb2e400c 100644
--- a/rsvg_internals/src/allowed_url.rs
+++ b/rsvg_internals/src/allowed_url.rs
@@ -110,6 +110,12 @@ impl AllowedUrl {
     }
 }
 
+impl fmt::Display for AllowedUrl {
+    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+        self.0.fmt(f)
+    }
+}
+
 impl error::Error for AllowedUrlError {
     fn description(&self) -> &str {
         match *self {
diff --git a/rsvg_internals/src/css.rs b/rsvg_internals/src/css.rs
index 9777eaa7..bc4d4152 100644
--- a/rsvg_internals/src/css.rs
+++ b/rsvg_internals/src/css.rs
@@ -4,13 +4,15 @@ use std::ptr;
 use std::str::{self, FromStr};
 
 use libc;
+use url::Url;
 
 use glib::translate::*;
 use glib_sys::{gboolean, gpointer, GList};
 
+use allowed_url::AllowedUrl;
 use attributes::Attribute;
 use croco::*;
-use handle::{self, RsvgHandle};
+use handle;
 use state::State;
 use util::utf8_cstr;
 
@@ -79,7 +81,7 @@ impl CssStyles {
 }
 
 struct DocHandlerData<'a> {
-    handle: *mut RsvgHandle,
+    base_url: Option<Url>,
     css_styles: &'a mut CssStyles,
     selector: *mut CRSelector,
 }
@@ -90,14 +92,14 @@ macro_rules! get_doc_handler_data {
     };
 }
 
-pub fn parse_into_css_styles(css_styles: &mut CssStyles, handle: *mut RsvgHandle, buf: &str) {
+pub fn parse_into_css_styles(css_styles: &mut CssStyles, base_url: Option<Url>, buf: &str) {
     if buf.len() == 0 {
         return; // libcroco doesn't like empty strings :(
     }
 
     unsafe {
         let mut handler_data = DocHandlerData {
-            handle,
+            base_url,
             css_styles,
             selector: ptr::null_mut(),
         };
@@ -151,8 +153,12 @@ unsafe extern "C" fn css_import_style(
     let raw_uri = cr_string_peek_raw_str(a_uri);
     let uri = utf8_cstr(raw_uri);
 
-    // FIXME: handle CSS errors
-    let _ = handle::load_css(handler_data.css_styles, handler_data.handle, uri);
+    if let Ok(aurl) = AllowedUrl::from_href(uri, handler_data.base_url.as_ref()) {
+        // FIXME: handle CSS errors
+        let _ = handle::load_css(handler_data.css_styles, &aurl);
+    } else {
+        rsvg_log!("disallowed URL \"{}\" for importing CSS", uri);
+    }
 }
 
 unsafe extern "C" fn css_start_selector(
diff --git a/rsvg_internals/src/handle.rs b/rsvg_internals/src/handle.rs
index cb2d7b20..6280e6e6 100644
--- a/rsvg_internals/src/handle.rs
+++ b/rsvg_internals/src/handle.rs
@@ -670,20 +670,8 @@ pub fn load_image_to_surface(
 
 // This function just slurps CSS data from a possibly-relative href
 // and parses it.  We'll move it to a better place in the end.
-pub fn load_css(
-    css_styles: &mut CssStyles,
-    handle: *mut RsvgHandle,
-    href_str: &str,
-) -> Result<(), LoadingError> {
-    let rhandle = get_rust_handle(handle);
-
-    let aurl =
-        AllowedUrl::from_href(href_str, rhandle.base_url.borrow().as_ref()).map_err(|_| {
-            rsvg_log!("Could not load \"{}\" for CSS data", href_str);
-            LoadingError::BadUrl
-        })?;
-
-    io::acquire_data(&aurl, None)
+pub fn load_css(css_styles: &mut CssStyles, aurl: &AllowedUrl) -> Result<(), LoadingError> {
+    io::acquire_data(aurl, None)
         .and_then(|data| {
             let BinaryData {
                 data: bytes,
@@ -693,7 +681,7 @@ pub fn load_css(
             if content_type.as_ref().map(String::as_ref) == Some("text/css") {
                 Ok(bytes)
             } else {
-                rsvg_log!("\"{}\" is not of type text/css; ignoring", href_str);
+                rsvg_log!("\"{}\" is not of type text/css; ignoring", aurl);
                 Err(LoadingError::BadCss)
             }
         })
@@ -701,13 +689,13 @@ pub fn load_css(
             String::from_utf8(bytes).map_err(|_| {
                 rsvg_log!(
                     "\"{}\" does not contain valid UTF-8 CSS data; ignoring",
-                    href_str
+                    aurl
                 );
                 LoadingError::BadCss
             })
         })
         .and_then(|utf8| {
-            css::parse_into_css_styles(css_styles, handle, &utf8);
+            css::parse_into_css_styles(css_styles, Some(aurl.url().clone()), &utf8);
             Ok(()) // FIXME: return CSS parsing errors
         })
 }
diff --git a/rsvg_internals/src/xml.rs b/rsvg_internals/src/xml.rs
index 02be735f..5dc849a0 100644
--- a/rsvg_internals/src/xml.rs
+++ b/rsvg_internals/src/xml.rs
@@ -220,12 +220,15 @@ impl XmlState {
                 && type_.as_ref().map(String::as_str) == Some("text/css")
                 && href.is_some()
             {
-                // FIXME: handle CSS errors
-                let _ = handle::load_css(
-                    self.css_styles.as_mut().unwrap(),
-                    self.handle,
+                if let Ok(aurl) = AllowedUrl::from_href(
                     &href.unwrap(),
-                );
+                    handle::get_base_url(self.handle).as_ref(),
+                ) {
+                    // FIXME: handle CSS errors
+                    let _ = handle::load_css(self.css_styles.as_mut().unwrap(), &aurl);
+                } else {
+                    self.error("disallowed URL in xml-stylesheet");
+                }
             }
         } else {
             self.error("invalid processing instruction data in xml-stylesheet");
@@ -284,7 +287,11 @@ impl XmlState {
         if node.get_type() == NodeType::Style {
             let css_data = node.with_impl(|style: &NodeStyle| style.get_css(&node));
 
-            css::parse_into_css_styles(self.css_styles.as_mut().unwrap(), self.handle, &css_data);
+            css::parse_into_css_styles(
+                self.css_styles.as_mut().unwrap(),
+                handle::get_base_url(self.handle).clone(),
+                &css_data,
+            );
         }
 
         self.current_node = node.get_parent();


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]