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

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