[gnome-boxes] wizard: be more strict about spice uri



commit c94e493550f7a854063739689b2097e6e73d7a8c
Author: Marc-Andrà Lureau <marcandre lureau gmail com>
Date:   Sat Oct 27 02:30:18 2012 +0200

    wizard: be more strict about spice uri
    
    A spice URI need either a port or a query string with port.
    
    Avoid the follwing critical:
    (gnome-boxes:9570): Boxes-CRITICAL **: boxes_query_construct: assertion `query != NULL' failed
    
    https://bugzilla.gnome.org/show_bug.cgi?id=684233

 src/spice-display.vala |   29 +++++++++++++++++++++++++++++
 src/wizard.vala        |   23 ++++++++++++++++-------
 2 files changed, 45 insertions(+), 7 deletions(-)
---
diff --git a/src/spice-display.vala b/src/spice-display.vala
index e0a4c82..35bbdfa 100644
--- a/src/spice-display.vala
+++ b/src/spice-display.vala
@@ -213,3 +213,32 @@ private class Boxes.SpiceDisplay: Boxes.Display, Boxes.IPropertiesProvider {
         return list;
     }
 }
+
+// FIXME: this kind of function should be part of spice-gtk
+static void spice_validate_uri (string uri_as_text,
+                                out int? port = null,
+                                out int? tls_port = null) throws Boxes.Error {
+    var uri = Xml.URI.parse (uri_as_text);
+
+    if (uri == null)
+        throw new Boxes.Error.INVALID (_("Invalid URI"));
+
+    tls_port = 0;
+    port = uri.port;
+    var query_str = uri.query_raw ?? uri.query;
+
+    if (query_str != null) {
+        var query = new Boxes.Query (query_str);
+        if (query.get ("port") != null) {
+            if (port > 0)
+                throw new Boxes.Error.INVALID (_("The port must be specified once"));
+            port = int.parse (query.get ("port"));
+        }
+
+        if (query.get ("tls-port") != null)
+            tls_port = int.parse (query.get ("tls-port"));
+    }
+
+    if (port <= 0 && tls_port <= 0)
+        throw new Boxes.Error.INVALID (_("Missing port in Spice URI"));
+}
diff --git a/src/wizard.vala b/src/wizard.vala
index 8b6885c..ccb9489 100644
--- a/src/wizard.vala
+++ b/src/wizard.vala
@@ -251,9 +251,10 @@ private class Boxes.Wizard: Boxes.UI {
 
         source = new CollectionSource (uri.server ?? uri_as_text, uri.scheme, uri_as_text);
 
-        if (uri.scheme == "spice" ||
-            uri.scheme == "vnc") {
-            // accept any vnc:// or spice:// uri
+        if (uri.scheme == "spice") {
+            spice_validate_uri (uri_as_text);
+        } else if (uri.scheme == "vnc") {
+            // accept any vnc:// uri
         } else if (uri.scheme.has_prefix ("qemu")) {
             // accept any qemu..:// uri
             source.source_type = "libvirt";
@@ -381,10 +382,18 @@ private class Boxes.Wizard: Boxes.UI {
 
             switch (uri.scheme) {
             case "spice":
-                var query = new Query (uri.query_raw ?? uri.query);
-
-                summary.add_property (_("Port"), query.get ("port"));
-                summary.add_property (_("TLS Port"), query.get ("tls-port"));
+                try {
+                    int port = 0, tls_port = 0;
+
+                    spice_validate_uri (source.uri, out port, out tls_port);
+                    if (port > 0)
+                        summary.add_property (_("Port"), port.to_string ());
+                    if (tls_port > 0)
+                        summary.add_property (_("TLS Port"), tls_port.to_string ());
+                } catch (Boxes.Error error) {
+                    // this shouldn't happen, since the URI was validated before
+                    critical (error.message);
+                }
                 break;
 
             case "vnc":



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