[PATCH v3 2/3] esx: Abstract all URL-creation code into one function

Richard W.M. Jones via Devel posted 3 patches 6 days, 11 hours ago
[PATCH v3 2/3] esx: Abstract all URL-creation code into one function
Posted by Richard W.M. Jones via Devel 6 days, 11 hours ago
Abstract the places where we create URLs into one place.  This is just
refactoring and should not change the behaviour.

Signed-off-by: Richard W.M. Jones <rjones@redhat.com>
---
 src/esx/esx_driver.c | 53 +++++++++++++++++++++++++++++++++++---------
 1 file changed, 43 insertions(+), 10 deletions(-)

diff --git a/src/esx/esx_driver.c b/src/esx/esx_driver.c
index 02f30c2b19..8fdfe0a656 100644
--- a/src/esx/esx_driver.c
+++ b/src/esx/esx_driver.c
@@ -582,7 +582,37 @@ esxCapsInit(esxPrivate *priv)
     return NULL;
 }
 
+static char *
+esxCreateURL(const char *transport,
+             const char *server,
+             int port,
+             const char *path)
+{
+    char *url;
 
+    url = g_strdup_printf("%s://%s:%d%s",
+                          transport,
+                          server,
+                          port,
+                          path);
+    return url;
+}
+
+/*
+ * Same as above, but add it to a buffer because the calling code will
+ * append query strings etc.
+ */
+static void
+esxCreateURLBuffer(virBuffer *buffer,
+                   const char *transport,
+                   const char *server,
+                   int port,
+                   const char *path)
+{
+    g_autofree char *url = esxCreateURL(transport, server, port, path);
+
+    virBufferAdd(buffer, url, strlen(url));
+}
 
 static int
 esxConnectToHost(esxPrivate *priv,
@@ -619,8 +649,8 @@ esxConnectToHost(esxPrivate *priv,
                                         conn->uri->server)))
         goto cleanup;
 
-    url = g_strdup_printf("%s://%s:%d/sdk", priv->parsedUri->transport,
-                          conn->uri->server, conn->uri->port);
+    url = esxCreateURL(priv->parsedUri->transport,
+                       conn->uri->server, conn->uri->port, "/sdk");
 
     if (esxVI_Context_Alloc(&priv->host) < 0 ||
         esxVI_Context_Connect(priv->host, url, ipAddress, username, password,
@@ -706,8 +736,8 @@ esxConnectToVCenter(esxPrivate *priv,
     if (!(password = virAuthGetPassword(conn, auth, "esx", username, hostname)))
         return -1;
 
-    url = g_strdup_printf("%s://%s:%d/sdk", priv->parsedUri->transport, hostname,
-                          conn->uri->port);
+    url = esxCreateURL(priv->parsedUri->transport, hostname,
+                       conn->uri->port, "/sdk");
 
     if (esxVI_Context_Alloc(&priv->vCenter) < 0 ||
         esxVI_Context_Connect(priv->vCenter, url, ipAddress, username,
@@ -2357,8 +2387,9 @@ esxDomainScreenshot(virDomainPtr domain, virStreamPtr stream,
     }
 
     /* Build URL */
-    virBufferAsprintf(&buffer, "%s://%s:%d/screen?id=", priv->parsedUri->transport,
-                      domain->conn->uri->server, domain->conn->uri->port);
+    esxCreateURLBuffer(&buffer, priv->parsedUri->transport,
+                       domain->conn->uri->server, domain->conn->uri->port,
+                       "/screen?id=");
     virBufferURIEncodeString(&buffer, virtualMachine->obj->value);
 
     url = virBufferContentAndReset(&buffer);
@@ -2563,8 +2594,9 @@ esxDomainGetXMLDesc(virDomainPtr domain, unsigned int flags)
         goto cleanup;
     }
 
-    virBufferAsprintf(&buffer, "%s://%s:%d/folder/", priv->parsedUri->transport,
-                      domain->conn->uri->server, domain->conn->uri->port);
+    esxCreateURLBuffer(&buffer, priv->parsedUri->transport,
+                       domain->conn->uri->server, domain->conn->uri->port,
+                       "/folder/");
     virBufferURIEncodeString(&buffer, directoryAndFileName);
     virBufferAddLit(&buffer, "?dcPath=");
     esxUtil_EscapeInventoryObject(&buffer, priv->primary->datacenterPath);
@@ -2987,8 +3019,9 @@ esxDomainDefineXMLFlags(virConnectPtr conn, const char *xml, unsigned int flags)
         goto cleanup;
     }
 
-    virBufferAsprintf(&buffer, "%s://%s:%d/folder/", priv->parsedUri->transport,
-                      conn->uri->server, conn->uri->port);
+    esxCreateURLBuffer(&buffer, priv->parsedUri->transport,
+                       conn->uri->server, conn->uri->port,
+                       "/folder/");
 
     if (directoryName) {
         virBufferURIEncodeString(&buffer, directoryName);
-- 
2.52.0
Re: [PATCH v3 2/3] esx: Abstract all URL-creation code into one function
Posted by Michal Prívozník via Devel 5 days, 22 hours ago
On 1/26/26 19:09, Richard W.M. Jones wrote:
> Abstract the places where we create URLs into one place.  This is just
> refactoring and should not change the behaviour.
> 
> Signed-off-by: Richard W.M. Jones <rjones@redhat.com>
> ---
>  src/esx/esx_driver.c | 53 +++++++++++++++++++++++++++++++++++---------
>  1 file changed, 43 insertions(+), 10 deletions(-)
> 
> diff --git a/src/esx/esx_driver.c b/src/esx/esx_driver.c
> index 02f30c2b19..8fdfe0a656 100644
> --- a/src/esx/esx_driver.c
> +++ b/src/esx/esx_driver.c
> @@ -582,7 +582,37 @@ esxCapsInit(esxPrivate *priv)
>      return NULL;
>  }
>  
> +static char *
> +esxCreateURL(const char *transport,
> +             const char *server,
> +             int port,
> +             const char *path)
> +{
> +    char *url;
>  
> +    url = g_strdup_printf("%s://%s:%d%s",
> +                          transport,
> +                          server,
> +                          port,
> +                          path);
> +    return url;
> +}
> +
> +/*
> + * Same as above, but add it to a buffer because the calling code will
> + * append query strings etc.
> + */
> +static void
> +esxCreateURLBuffer(virBuffer *buffer,
> +                   const char *transport,
> +                   const char *server,
> +                   int port,
> +                   const char *path)
> +{
> +    g_autofree char *url = esxCreateURL(transport, server, port, path);
> +
> +    virBufferAdd(buffer, url, strlen(url));

Nitpick: you can s/strlen(url)/-1/ as virBufferAdd will calculate the
length in that case.

Michal
Re: [PATCH v3 2/3] esx: Abstract all URL-creation code into one function
Posted by Richard W.M. Jones via Devel 5 days, 21 hours ago
On Tue, Jan 27, 2026 at 08:25:43AM +0100, Michal Prívozník wrote:
> On 1/26/26 19:09, Richard W.M. Jones wrote:
> > +    virBufferAdd(buffer, url, strlen(url));
> 
> Nitpick: you can s/strlen(url)/-1/ as virBufferAdd will calculate the
> length in that case.

I changed it in my local copy.

Rich.

-- 
Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones
Read my programming and virtualization blog: http://rwmj.wordpress.com
virt-p2v converts physical machines to virtual machines.  Boot with a
live CD or over the network (PXE) and turn machines into KVM guests.
http://libguestfs.org/virt-v2v
Re: [PATCH v3 2/3] esx: Abstract all URL-creation code into one function
Posted by Richard W.M. Jones via Devel 5 days, 18 hours ago
On Tue, Jan 27, 2026 at 08:47:47AM +0000, Richard W.M. Jones via Devel wrote:
> On Tue, Jan 27, 2026 at 08:25:43AM +0100, Michal Prívozník wrote:
> > On 1/26/26 19:09, Richard W.M. Jones wrote:
> > > +    virBufferAdd(buffer, url, strlen(url));
> > 
> > Nitpick: you can s/strlen(url)/-1/ as virBufferAdd will calculate the
> > length in that case.
> 
> I changed it in my local copy.

I've retested and it still appears to all work.  Is it OK to push this?

Rich.

-- 
Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones
Read my programming and virtualization blog: http://rwmj.wordpress.com
virt-top is 'top' for virtual machines.  Tiny program with many
powerful monitoring features, net stats, disk stats, logging, etc.
http://people.redhat.com/~rjones/virt-top
Re: [PATCH v3 2/3] esx: Abstract all URL-creation code into one function
Posted by Michal Prívozník via Devel 5 days, 13 hours ago
On 1/27/26 12:56, Richard W.M. Jones wrote:
> On Tue, Jan 27, 2026 at 08:47:47AM +0000, Richard W.M. Jones via Devel wrote:
>> On Tue, Jan 27, 2026 at 08:25:43AM +0100, Michal Prívozník wrote:
>>> On 1/26/26 19:09, Richard W.M. Jones wrote:
>>>> +    virBufferAdd(buffer, url, strlen(url));
>>>
>>> Nitpick: you can s/strlen(url)/-1/ as virBufferAdd will calculate the
>>> length in that case.
>>
>> I changed it in my local copy.
> 
> I've retested and it still appears to all work.  Is it OK to push this?

Yes! Go ahead. You still have commit access.

Michal