Blame SOURCES/0016-curl-Fix-D-curl.verbose-1-option.patch

538efe
From f421e599d3507f22d3d06b2dab070811e7e4f41c Mon Sep 17 00:00:00 2001
538efe
From: "Richard W.M. Jones" <rjones@redhat.com>
538efe
Date: Sat, 28 Mar 2020 18:22:42 +0000
538efe
Subject: [PATCH 16/19] curl: Fix -D curl.verbose=1 option.
538efe
538efe
It didn't work previously for various reasons:
538efe
538efe
 - Passed int instead of long to curl_easy_setopt.
538efe
538efe
 - No CURLOPT_DEBUGFUNCTION callback was supplied, so messages were
538efe
   sent to stderr, which meant they were never logged for the majority
538efe
   of use cases.
538efe
538efe
This also removes extra debugging in the regular header callback.
538efe
This is no longer needed as the now-working -D curl.verbose=1 option
538efe
will log the headers.
538efe
538efe
Fixes commit 2ba11ee8f154ad1c84e10b43479b265fca2e996b.
538efe
538efe
(cherry picked from commit 6791c69bddf76577b65fa3ddfde652c0594ce340)
538efe
---
538efe
 plugins/curl/Makefile.am |  2 ++
538efe
 plugins/curl/curl.c      | 73 ++++++++++++++++++++++++++++++----------
538efe
 tests/test-curl.c        |  3 +-
538efe
 3 files changed, 60 insertions(+), 18 deletions(-)
538efe
538efe
diff --git a/plugins/curl/Makefile.am b/plugins/curl/Makefile.am
538efe
index 6595eb95..024ddb6d 100644
538efe
--- a/plugins/curl/Makefile.am
538efe
+++ b/plugins/curl/Makefile.am
538efe
@@ -44,6 +44,7 @@ nbdkit_curl_plugin_la_SOURCES = \
538efe
 
538efe
 nbdkit_curl_plugin_la_CPPFLAGS = \
538efe
 	-I$(top_srcdir)/include \
538efe
+	-I$(top_srcdir)/common/utils \
538efe
 	$(NULL)
538efe
 nbdkit_curl_plugin_la_CFLAGS = \
538efe
 	$(WARNINGS_CFLAGS) \
538efe
@@ -51,6 +52,7 @@ nbdkit_curl_plugin_la_CFLAGS = \
538efe
 	$(NULL)
538efe
 nbdkit_curl_plugin_la_LIBADD = \
538efe
 	$(CURL_LIBS) \
538efe
+	$(top_builddir)/common/utils/libutils.la \
538efe
 	$(NULL)
538efe
 nbdkit_curl_plugin_la_LDFLAGS = \
538efe
 	-module -avoid-version -shared \
538efe
diff --git a/plugins/curl/curl.c b/plugins/curl/curl.c
538efe
index 8b341ae0..b1693dc0 100644
538efe
--- a/plugins/curl/curl.c
538efe
+++ b/plugins/curl/curl.c
538efe
@@ -56,6 +56,8 @@
538efe
 
538efe
 #include <nbdkit-plugin.h>
538efe
 
538efe
+#include "cleanup.h"
538efe
+
538efe
 static const char *url = NULL;
538efe
 static const char *user = NULL;
538efe
 static char *password = NULL;
538efe
@@ -283,6 +285,8 @@ struct curl_handle {
538efe
                   curl_easy_strerror ((r)), (h)->errbuf);       \
538efe
   } while (0)
538efe
 
538efe
+static int debug_cb (CURL *handle, curl_infotype type,
538efe
+                     const char *data, size_t size, void *);
538efe
 static size_t header_cb (void *ptr, size_t size, size_t nmemb, void *opaque);
538efe
 static size_t write_cb (char *ptr, size_t size, size_t nmemb, void *opaque);
538efe
 static size_t read_cb (void *ptr, size_t size, size_t nmemb, void *opaque);
538efe
@@ -311,11 +315,13 @@ curl_open (int readonly)
538efe
     goto err;
538efe
   }
538efe
 
538efe
-  /* Note this writes the output to stderr directly.  We should
538efe
-   * consider using CURLOPT_DEBUGFUNCTION so we can handle it with
538efe
-   * nbdkit_debug.
538efe
-   */
538efe
-  curl_easy_setopt (h->c, CURLOPT_VERBOSE, curl_debug_verbose);
538efe
+  if (curl_debug_verbose) {
538efe
+    /* NB: Constants must be explicitly long because the parameter is
538efe
+     * varargs.
538efe
+     */
538efe
+    curl_easy_setopt (h->c, CURLOPT_VERBOSE, 1L);
538efe
+    curl_easy_setopt (h->c, CURLOPT_DEBUGFUNCTION, debug_cb);
538efe
+  }
538efe
 
538efe
   curl_easy_setopt (h->c, CURLOPT_ERRORBUFFER, h->errbuf);
538efe
 
538efe
@@ -441,12 +447,56 @@ curl_open (int readonly)
538efe
   return NULL;
538efe
 }
538efe
 
538efe
+/* When using CURLOPT_VERBOSE, this callback is used to redirect
538efe
+ * messages to nbdkit_debug (instead of stderr).
538efe
+ */
538efe
+static int
538efe
+debug_cb (CURL *handle, curl_infotype type,
538efe
+          const char *data, size_t size, void *opaque)
538efe
+{
538efe
+  size_t origsize = size;
538efe
+  CLEANUP_FREE char *str;
538efe
+
538efe
+  /* The data parameter passed is NOT \0-terminated, but also it may
538efe
+   * have \n or \r\n line endings.  The only sane way to deal with
538efe
+   * this is to copy the string.  (The data strings may also be
538efe
+   * multi-line, but we don't deal with that here).
538efe
+   */
538efe
+  str = malloc (size + 1);
538efe
+  if (str == NULL)
538efe
+    goto out;
538efe
+  memcpy (str, data, size);
538efe
+  str[size] = '\0';
538efe
+
538efe
+  while (size > 0 && (str[size-1] == '\n' || str[size-1] == '\r')) {
538efe
+    str[size-1] = '\0';
538efe
+    size--;
538efe
+  }
538efe
+
538efe
+  switch (type) {
538efe
+  case CURLINFO_TEXT:
538efe
+    nbdkit_debug ("%s", str);
538efe
+    break;
538efe
+  case CURLINFO_HEADER_IN:
538efe
+    nbdkit_debug ("S: %s", str);
538efe
+    break;
538efe
+  case CURLINFO_HEADER_OUT:
538efe
+    nbdkit_debug ("C: %s", str);
538efe
+    break;
538efe
+  default:
538efe
+    /* Assume everything else is binary data that we cannot print. */
538efe
+    nbdkit_debug ("<data with size=%zu>", origsize);
538efe
+  }
538efe
+
538efe
+ out:
538efe
+  return 0;
538efe
+}
538efe
+
538efe
 static size_t
538efe
 header_cb (void *ptr, size_t size, size_t nmemb, void *opaque)
538efe
 {
538efe
   struct curl_handle *h = opaque;
538efe
   size_t realsize = size * nmemb;
538efe
-  size_t len;
538efe
   const char *accept_line = "Accept-Ranges: bytes";
538efe
   const char *line = ptr;
538efe
 
538efe
@@ -454,17 +504,6 @@ header_cb (void *ptr, size_t size, size_t nmemb, void *opaque)
538efe
       strncmp (line, accept_line, strlen (accept_line)) == 0)
538efe
     h->accept_range = true;
538efe
 
538efe
-  /* Useful to print the server headers when debugging.  However we
538efe
-   * must strip off trailing \r?\n from each line.
538efe
-   */
538efe
-  len = realsize;
538efe
-  if (len > 0 && line[len-1] == '\n')
538efe
-    len--;
538efe
-  if (len > 0 && line[len-1] == '\r')
538efe
-    len--;
538efe
-  if (len > 0)
538efe
-    nbdkit_debug ("S: %.*s", (int) len, line);
538efe
-
538efe
   return realsize;
538efe
 }
538efe
 
538efe
diff --git a/tests/test-curl.c b/tests/test-curl.c
538efe
index 2b7e3beb..165edb35 100644
538efe
--- a/tests/test-curl.c
538efe
+++ b/tests/test-curl.c
538efe
@@ -74,9 +74,10 @@ main (int argc, char *argv[])
538efe
     exit (EXIT_FAILURE);
538efe
   }
538efe
   if (test_start_nbdkit ("curl",
538efe
+                         "-D", "curl.verbose=1",
538efe
+                         "http://localhost/disk",
538efe
                          "cookie=foo=bar; baz=1;",
538efe
                          usp_param, /* unix-socket-path=... */
538efe
-                         "http://localhost/disk",
538efe
                          NULL) == -1)
538efe
     exit (EXIT_FAILURE);
538efe
 
538efe
-- 
538efe
2.18.2
538efe