All of lore.kernel.org
 help / color / mirror / Atom feed
From: "Marc-André Lureau" <marcandre.lureau@redhat.com>
To: qemu-devel@nongnu.org
Cc: "Kevin Wolf" <kwolf@redhat.com>,
	integration@gluster.org, sheepdog@lists.wpkg.org,
	qemu-block@nongnu.org, "Peter Lieven" <pl@kamp.de>,
	"Richard W.M. Jones" <rjones@redhat.com>,
	"Max Reitz" <mreitz@redhat.com>,
	"Marc-André Lureau" <marcandre.lureau@redhat.com>,
	"Liu Yuan" <namei.unix@gmail.com>
Subject: [PATCH 13/13] ssh: add GUri-based URI parsing
Date: Thu,  9 Jul 2020 23:42:34 +0400	[thread overview]
Message-ID: <20200709194234.2117650-14-marcandre.lureau@redhat.com> (raw)
In-Reply-To: <20200709194234.2117650-1-marcandre.lureau@redhat.com>

Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
---
 block/ssh.c | 75 +++++++++++++++++++++++++++++++++++++++++------------
 1 file changed, 58 insertions(+), 17 deletions(-)

diff --git a/block/ssh.c b/block/ssh.c
index c8f6ad79e3c..d2bc6277613 100644
--- a/block/ssh.c
+++ b/block/ssh.c
@@ -180,9 +180,37 @@ static void sftp_error_trace(BDRVSSHState *s, const char *op)
 
 static int parse_uri(const char *filename, QDict *options, Error **errp)
 {
+    g_autofree char *port_str = NULL;
+    const char *scheme, *server, *path, *user, *key, *value;
+    gint port;
+
+#ifdef HAVE_GLIB_GURI
+    g_autoptr(GUri) uri = NULL;
+    g_autoptr(GHashTable) params = NULL;
+    g_autoptr(GError) err = NULL;
+    GHashTableIter iter;
+
+    uri = g_uri_parse(filename, G_URI_FLAGS_ENCODED_QUERY, &err);
+    if (!uri) {
+        error_setg(errp, "Failed to parse SSH URI: %s", err->message);
+        return -EINVAL;
+    }
+
+    params = g_uri_parse_params(g_uri_get_query(uri), -1,
+                                "&;", G_URI_PARAMS_NONE, &err);
+    if (err) {
+        error_report("Failed to parse SSH URI query: %s", err->message);
+        return -EINVAL;
+    }
+
+    scheme = g_uri_get_scheme(uri);
+    user = g_uri_get_user(uri);
+    server = g_uri_get_host(uri);
+    path = g_uri_get_path(uri);
+    port = g_uri_get_port(uri);
+#else
     g_autoptr(URI) uri = NULL;
     g_autoptr(QueryParams) qp = NULL;
-    g_autofree char *port_str = NULL;
     int i;
 
     uri = uri_parse(filename);
@@ -190,44 +218,57 @@ static int parse_uri(const char *filename, QDict *options, Error **errp)
         return -EINVAL;
     }
 
-    if (g_strcmp0(uri->scheme, "ssh") != 0) {
-        error_setg(errp, "URI scheme must be 'ssh'");
+    qp = query_params_parse(uri->query);
+    if (!qp) {
+        error_setg(errp, "could not parse query parameters");
         return -EINVAL;
     }
 
-    if (!uri->server || strcmp(uri->server, "") == 0) {
-        error_setg(errp, "missing hostname in URI");
+    scheme = uri->scheme;
+    user = uri->user;
+    server = uri->server;
+    path = uri->path;
+    port = uri->port;
+#endif
+    if (g_strcmp0(scheme, "ssh") != 0) {
+        error_setg(errp, "URI scheme must be 'ssh'");
         return -EINVAL;
     }
 
-    if (!uri->path || strcmp(uri->path, "") == 0) {
-        error_setg(errp, "missing remote path in URI");
+    if (!server || strcmp(server, "") == 0) {
+        error_setg(errp, "missing hostname in URI");
         return -EINVAL;
     }
 
-    qp = query_params_parse(uri->query);
-    if (!qp) {
-        error_setg(errp, "could not parse query parameters");
+    if (!path || strcmp(path, "") == 0) {
+        error_setg(errp, "missing remote path in URI");
         return -EINVAL;
     }
 
-    if(uri->user && strcmp(uri->user, "") != 0) {
-        qdict_put_str(options, "user", uri->user);
+    if (user && strcmp(user, "") != 0) {
+        qdict_put_str(options, "user", user);
     }
 
-    qdict_put_str(options, "server.host", uri->server);
+    qdict_put_str(options, "server.host", server);
 
-    port_str = g_strdup_printf("%d", uri->port ?: 22);
+    port_str = g_strdup_printf("%d", port ?: 22);
     qdict_put_str(options, "server.port", port_str);
 
-    qdict_put_str(options, "path", uri->path);
+    qdict_put_str(options, "path", path);
 
     /* Pick out any query parameters that we understand, and ignore
      * the rest.
      */
+#ifdef HAVE_GLIB_GURI
+    g_hash_table_iter_init(&iter, params);
+    while (g_hash_table_iter_next(&iter, (void **)&key, (void **)&value)) {
+#else
     for (i = 0; i < qp->n; ++i) {
-        if (strcmp(qp->p[i].name, "host_key_check") == 0) {
-            qdict_put_str(options, "host_key_check", qp->p[i].value);
+        key = qp->p[i].name;
+        value = qp->p[i].value;
+#endif
+        if (g_strcmp0(key, "host_key_check") == 0) {
+            qdict_put_str(options, "host_key_check", value);
         }
     }
 
-- 
2.27.0.221.ga08a83db2b



  parent reply	other threads:[~2020-07-09 19:46 UTC|newest]

Thread overview: 19+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-07-09 19:42 [PATCH 00/13] RFC: use upcoming GUri for URI handling Marc-André Lureau
2020-07-09 19:42 ` [PATCH 01/13] uri: add g_auto macros for URI & QueryParams Marc-André Lureau
2020-07-09 19:42 ` [PATCH 02/13] block/nbd: auto-ify URI parsing variables Marc-André Lureau
2020-07-09 19:42 ` [PATCH 03/13] block/vxhs: " Marc-André Lureau
2020-07-09 19:42 ` [PATCH 04/13] block/sheepdog: " Marc-André Lureau
2020-07-09 19:42 ` [PATCH 05/13] block/ssh: " Marc-André Lureau
2020-07-23 12:30   ` Richard W.M. Jones
2020-07-09 19:42 ` [PATCH 06/13] block/nfs: " Marc-André Lureau
2020-07-09 19:42 ` [PATCH 07/13] block/gluster: " Marc-André Lureau
2020-07-09 19:42 ` [PATCH 08/13] build-sys: add HAVE_GLIB_GURI Marc-André Lureau
2020-07-09 19:42 ` [PATCH 09/13] nbd: add GUri-based URI parsing version Marc-André Lureau
2020-07-10  8:31   ` Daniel P. Berrangé
2020-07-09 19:42 ` [PATCH 10/13] sheepdog: add GUri-based URI parsing Marc-André Lureau
2020-07-09 19:42 ` [PATCH 11/13] nfs: " Marc-André Lureau
2020-07-09 19:42 ` [PATCH 12/13] gluster: " Marc-André Lureau
2020-07-09 19:42 ` Marc-André Lureau [this message]
2020-07-23 12:31   ` [PATCH 13/13] ssh: " Richard W.M. Jones
2020-07-23 12:58     ` Marc-André Lureau
2020-07-23 13:06       ` Daniel P. Berrangé

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20200709194234.2117650-14-marcandre.lureau@redhat.com \
    --to=marcandre.lureau@redhat.com \
    --cc=integration@gluster.org \
    --cc=kwolf@redhat.com \
    --cc=mreitz@redhat.com \
    --cc=namei.unix@gmail.com \
    --cc=pl@kamp.de \
    --cc=qemu-block@nongnu.org \
    --cc=qemu-devel@nongnu.org \
    --cc=rjones@redhat.com \
    --cc=sheepdog@lists.wpkg.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.