[rygel] core: Move uri generating and parsing to own class



commit 23f242580e315384061c95d69cbd1f98e14d0c3d
Author: Jens Georg <mail jensge org>
Date:   Wed Nov 25 11:38:03 2009 +0100

    core: Move uri generating and parsing to own class

 src/rygel/Makefile.am             |    1 +
 src/rygel/rygel-http-request.vala |   44 ++++++++--------------
 src/rygel/rygel-http-server.vala  |   13 +-----
 src/rygel/rygel-item-uri.vala     |   74 +++++++++++++++++++++++++++++++++++++
 4 files changed, 94 insertions(+), 38 deletions(-)
---
diff --git a/src/rygel/Makefile.am b/src/rygel/Makefile.am
index baea518..8b21a06 100644
--- a/src/rygel/Makefile.am
+++ b/src/rygel/Makefile.am
@@ -36,6 +36,7 @@ BUILT_SOURCES = rygel-1.0.vapi \
 		rygel.h
 
 rygel_SOURCES = $(VAPI_SOURCE_FILES) \
+		rygel-item-uri.vala \
 		rygel-dbus-service.vala \
 		rygel-root-device.vala \
 		rygel-root-device-factory.vala \
diff --git a/src/rygel/rygel-http-request.vala b/src/rygel/rygel-http-request.vala
index 0bd12f6..eab86e0 100644
--- a/src/rygel/rygel-http-request.vala
+++ b/src/rygel/rygel-http-request.vala
@@ -80,7 +80,22 @@ internal class Rygel.HTTPRequest : GLib.Object, Rygel.StateMachine {
             return;
         }
 
-        this.parse_uri ();
+        try {
+            var uri = new ItemUri.from_string (this.msg.uri.path,
+                    this.http_server.path_root);
+
+            this.item_id = uri.item_id;
+            this.thumbnail_index = uri.thumbnail_index;
+            if (uri.transcode_target != null) {
+                var transcoder = this.http_server.get_transcoder (
+                                                        uri.transcode_target);
+                this.handler = new HTTPTranscodeHandler (transcoder,
+                                                         this.cancellable);
+            }
+        } catch (Error err) {
+            warning ("Failed to parse query: %s", err.message);
+        }
+
 
         if (this.item_id == null) {
             this.handle_error (new HTTPRequestError.NOT_FOUND ("Not Found"));
@@ -90,7 +105,6 @@ internal class Rygel.HTTPRequest : GLib.Object, Rygel.StateMachine {
         if (this.handler == null) {
             this.handler = new HTTPIdentityHandler (this.cancellable);
         }
-
         yield this.find_item ();
     }
 
@@ -155,32 +169,6 @@ internal class Rygel.HTTPRequest : GLib.Object, Rygel.StateMachine {
         }
     }
 
-    private void parse_uri () {
-        // do not decode the path here as it may contain encoded slashes
-        var request_uri = this.msg.get_uri ().path.replace (this.http_server.path_root, "");
-        var parts = request_uri.split ("/");
-        if (parts.length < 2 && parts.length % 2 != 0)
-            warning ("Invalid uri %s", request_uri);
-        else {
-            this.item_id = Soup.URI.decode (parts[1]);
-            for (int i = 2; i < parts.length - 1; i += 2) {
-                switch (parts[i]) {
-                    case "transcoded":
-                        var transcoder = this.http_server.get_transcoder (
-                                               Soup.URI.decode (parts[i + 1]));
-                        this.handler = new HTTPTranscodeHandler (transcoder,
-                                                                 this.cancellable);
-                    break;
-                    case "thumbnail":
-                        this.thumbnail_index = parts[i + 1].to_int ();
-                        break;
-                    default:
-                        break;
-                }
-            }
-        }
-    }
-
     private void handle_error (Error error) {
         warning ("%s", error.message);
 
diff --git a/src/rygel/rygel-http-server.vala b/src/rygel/rygel-http-server.vala
index 7455875..a41476d 100644
--- a/src/rygel/rygel-http-server.vala
+++ b/src/rygel/rygel-http-server.vala
@@ -128,19 +128,12 @@ internal class Rygel.HTTPServer : Rygel.TranscodeManager, Rygel.StateMachine {
                                                   int       thumbnail_index,
                                                   string?   transcode_target,
                                                   out string protocol) {
-        string escaped = Uri.escape_string (item.id, "", true);
-        string query = "/" + escaped;
-
-        if (transcode_target != null) {
-            escaped = Uri.escape_string (transcode_target, "", true);
-            query += "/transcoded/" + escaped;
-        } else if (thumbnail_index >= 0) {
-            query += "/thumbnail/" + thumbnail_index.to_string ();
-        }
+
+        var uri = new ItemUri (item.id, thumbnail_index, transcode_target);
 
         protocol = "http-get";
 
-        return create_uri_for_path (query);
+        return create_uri_for_path (uri.to_string());
     }
 
     internal override string get_protocol () {
diff --git a/src/rygel/rygel-item-uri.vala b/src/rygel/rygel-item-uri.vala
new file mode 100644
index 0000000..1a6c261
--- /dev/null
+++ b/src/rygel/rygel-item-uri.vala
@@ -0,0 +1,74 @@
+/*
+ * Copyright (C) 2009 Jens Georg <mail jensge org>.
+ *
+ * Author: Jens Georg <mail jensge org>
+ *
+ * This file is part of Rygel.
+ *
+ * Rygel is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * Rygel is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+
+internal class Rygel.ItemUri : Object {
+    public string item_id;
+    public int thumbnail_index;
+    public string? transcode_target;
+
+    public ItemUri (string item_id,
+                    int thumbnail_index = -1,
+                    string ? transcode_target = null) {
+        this.item_id = item_id;
+        this.thumbnail_index = thumbnail_index;
+        this.transcode_target = transcode_target;
+    }
+
+    public ItemUri.from_string (string uri, string server_root = "") {
+        // do not decode the path here as it may contain encoded slashes
+        var request_uri = uri.replace (server_root, "");
+        var parts = request_uri.split ("/");
+        if (parts.length < 2 && parts.length % 2 != 0)
+            warning ("Invalid uri %s", request_uri);
+        else {
+            for (int i = 0; i < parts.length - 1; i += 2) {
+                switch (parts[i]) {
+                    case "item":
+                        this.item_id = Soup.URI.decode (parts[1]);
+                        break;
+                    case "transcoded":
+                        this.transcode_target = Soup.URI.decode (parts[i + 1]);
+                        break;
+                    case "thumbnail":
+                        this.thumbnail_index = parts[i + 1].to_int ();
+                        break;
+                    default:
+                        break;
+                }
+            }
+        }
+    }
+
+    public string to_string() {
+        string escaped = Uri.escape_string (item_id, "", true);
+        string query = "/" + escaped;
+
+        if (transcode_target != null) {
+            escaped = Uri.escape_string (transcode_target, "", true);
+            query += "/transcoded/" + escaped;
+        } else if (thumbnail_index >= 0) {
+            query += "/thumbnail/" + thumbnail_index.to_string ();
+        }
+
+        return query;
+    }
+}



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