From e6e625cc83018fd4776169241e9d1019e3bcffa7 Mon Sep 17 00:00:00 2001
From: Tatsuhiro Tsujikawa <tatsuhiro.t@gmail.com>
Date: Fri, 25 May 2012 17:33:28 +0900
Subject: [PATCH] Made -D option work with -O and -J.

To achieve this, first new structure HeaderData is defined to
hold necessary data to perform header-related work.  Then
tool_header_cb now receives HeaderData pointer as userdata.  All
header-related work (currently, dumping header and
Content-Disposition inspection) are done in this callback
function.  HeaderData.outs->config is used to determine whether
each work is done.

Unit tests were also updated because after this change, curl code
always sets CURLOPT_HEADERFUNCTION and CURLOPT_HEADERDATA.

Tested with -O -J -D, -O -J -i and -O -J -D -i and all worked
fine.
---
 src/tool_cb_hdr.c   |   12 ++++++++++--
 src/tool_cb_hdr.h   |   10 ++++++++++
 src/tool_operate.c  |   18 +++++++-----------
 tests/data/test1400 |    2 ++
 tests/data/test1401 |    2 ++
 tests/data/test1402 |    2 ++
 tests/data/test1403 |    2 ++
 tests/data/test1404 |    2 ++
 tests/data/test1405 |    2 ++
 tests/data/test1406 |    2 ++
 tests/data/test1407 |    2 ++
 11 files changed, 43 insertions(+), 13 deletions(-)

diff --git a/src/tool_cb_hdr.c b/src/tool_cb_hdr.c
index e963311..738cd5d 100644
--- a/src/tool_cb_hdr.c
+++ b/src/tool_cb_hdr.c
@@ -41,7 +41,10 @@ static char *parse_filename(const char *ptr, size_t len);
 
 size_t tool_header_cb(void *ptr, size_t size, size_t nmemb, void *userdata)
 {
-  struct OutStruct *outs = userdata;
+  HeaderData *hdrdata = userdata;
+  struct getout *urlnode = hdrdata->urlnode;
+  struct OutStruct *outs = hdrdata->outs;
+  struct OutStruct *heads = hdrdata->heads;
   const char *str = ptr;
   const size_t cb = size * nmemb;
   const char *end = (char*)ptr + cb;
@@ -63,8 +66,13 @@ size_t tool_header_cb(void *ptr, size_t size, size_t nmemb, void *userdata)
     return failure;
   }
 #endif
+  /* --dump-header option */
+  if(outs->config->headerfile) {
+    fwrite(ptr, size, nmemb, heads->stream);
+  }
 
-  if((cb > 20) && checkprefix("Content-disposition:", str)) {
+  if((urlnode->flags & GETOUT_USEREMOTE) && outs->config->content_disposition
+     && (cb > 20) && checkprefix("Content-disposition:", str)) {
     const char *p = str + 20;
 
     /* look for the 'filename=' parameter
diff --git a/src/tool_cb_hdr.h b/src/tool_cb_hdr.h
index 5909336..0300c00 100644
--- a/src/tool_cb_hdr.h
+++ b/src/tool_cb_hdr.h
@@ -23,6 +23,16 @@
  ***************************************************************************/
 #include "tool_setup.h"
 
+/* Structure to pass as userdata in tool_header_cb */
+typedef struct {
+  /* getout object pointer currently processing */
+  struct getout *urlnode;
+  /* output stream */
+  struct OutStruct *outs;
+  /* header output stream */
+  struct OutStruct *heads;
+} HeaderData;
+
 /*
 ** callback for CURLOPT_HEADERFUNCTION
 */
diff --git a/src/tool_operate.c b/src/tool_operate.c
index e3d0021..689ffb2 100644
--- a/src/tool_operate.c
+++ b/src/tool_operate.c
@@ -504,6 +504,7 @@ int operate(struct Configurable *config, int argc, argv_item_t argv[])
         long retry_sleep_default;
         long retry_sleep;
         char *this_url;
+        HeaderData hdrdata;
 
         outfile = NULL;
         infdopen = FALSE;
@@ -1211,17 +1212,12 @@ int operate(struct Configurable *config, int argc, argv_item_t argv[])
         if(config->proto_redir_present)
           my_setopt_flags(curl, CURLOPT_REDIR_PROTOCOLS, config->proto_redir);
 
-        if((urlnode->flags & GETOUT_USEREMOTE)
-           && config->content_disposition) {
-          my_setopt(curl, CURLOPT_HEADERFUNCTION, tool_header_cb);
-          my_setopt(curl, CURLOPT_HEADERDATA, &outs);
-        }
-        else {
-          /* if HEADERFUNCTION was set to something in the previous loop, it
-             is important that we set it (back) to NULL now */
-          my_setopt(curl, CURLOPT_HEADERFUNCTION, NULL);
-          my_setopt(curl, CURLOPT_HEADERDATA, config->headerfile?&heads:NULL);
-        }
+        hdrdata.urlnode = urlnode;
+        hdrdata.outs = &outs;
+        hdrdata.heads = &heads;
+
+        my_setopt(curl, CURLOPT_HEADERFUNCTION, tool_header_cb);
+        my_setopt(curl, CURLOPT_HEADERDATA, &hdrdata);
 
         if(config->resolve)
           /* new in 7.21.3 */
diff --git a/tests/data/test1400 b/tests/data/test1400
index 59b2856..72989c4 100644
--- a/tests/data/test1400
+++ b/tests/data/test1400
@@ -85,6 +85,8 @@ int main(int argc, char *argv[])
   CURLOPT_STDERR set to a objectpointer
   CURLOPT_DEBUGFUNCTION set to a functionpointer
   CURLOPT_DEBUGDATA set to a objectpointer
+  CURLOPT_HEADERFUNCTION set to a functionpointer
+  CURLOPT_HEADERDATA set to a objectpointer
 
   */
 
diff --git a/tests/data/test1401 b/tests/data/test1401
index e094ec4..e709c8e 100644
--- a/tests/data/test1401
+++ b/tests/data/test1401
@@ -104,6 +104,8 @@ int main(int argc, char *argv[])
   CURLOPT_STDERR set to a objectpointer
   CURLOPT_DEBUGFUNCTION set to a functionpointer
   CURLOPT_DEBUGDATA set to a objectpointer
+  CURLOPT_HEADERFUNCTION set to a functionpointer
+  CURLOPT_HEADERDATA set to a objectpointer
 
   */
 
diff --git a/tests/data/test1402 b/tests/data/test1402
index d567792..c3bf834 100644
--- a/tests/data/test1402
+++ b/tests/data/test1402
@@ -92,6 +92,8 @@ int main(int argc, char *argv[])
   CURLOPT_STDERR set to a objectpointer
   CURLOPT_DEBUGFUNCTION set to a functionpointer
   CURLOPT_DEBUGDATA set to a objectpointer
+  CURLOPT_HEADERFUNCTION set to a functionpointer
+  CURLOPT_HEADERDATA set to a objectpointer
 
   */
 
diff --git a/tests/data/test1403 b/tests/data/test1403
index 3e4a034..3ec7dd0 100644
--- a/tests/data/test1403
+++ b/tests/data/test1403
@@ -87,6 +87,8 @@ int main(int argc, char *argv[])
   CURLOPT_STDERR set to a objectpointer
   CURLOPT_DEBUGFUNCTION set to a functionpointer
   CURLOPT_DEBUGDATA set to a objectpointer
+  CURLOPT_HEADERFUNCTION set to a functionpointer
+  CURLOPT_HEADERDATA set to a objectpointer
 
   */
 
diff --git a/tests/data/test1404 b/tests/data/test1404
index f856a74..88a06ba 100644
--- a/tests/data/test1404
+++ b/tests/data/test1404
@@ -141,6 +141,8 @@ int main(int argc, char *argv[])
   CURLOPT_STDERR set to a objectpointer
   CURLOPT_DEBUGFUNCTION set to a functionpointer
   CURLOPT_DEBUGDATA set to a objectpointer
+  CURLOPT_HEADERFUNCTION set to a functionpointer
+  CURLOPT_HEADERDATA set to a objectpointer
 
   */
 
diff --git a/tests/data/test1405 b/tests/data/test1405
index 187f849..692bb1a 100644
--- a/tests/data/test1405
+++ b/tests/data/test1405
@@ -103,6 +103,8 @@ int main(int argc, char *argv[])
   CURLOPT_STDERR set to a objectpointer
   CURLOPT_DEBUGFUNCTION set to a functionpointer
   CURLOPT_DEBUGDATA set to a objectpointer
+  CURLOPT_HEADERFUNCTION set to a functionpointer
+  CURLOPT_HEADERDATA set to a objectpointer
 
   */
 
diff --git a/tests/data/test1406 b/tests/data/test1406
index 216d68e..478e45f 100644
--- a/tests/data/test1406
+++ b/tests/data/test1406
@@ -95,6 +95,8 @@ int main(int argc, char *argv[])
   CURLOPT_STDERR set to a objectpointer
   CURLOPT_DEBUGFUNCTION set to a functionpointer
   CURLOPT_DEBUGDATA set to a objectpointer
+  CURLOPT_HEADERFUNCTION set to a functionpointer
+  CURLOPT_HEADERDATA set to a objectpointer
 
   */
 
diff --git a/tests/data/test1407 b/tests/data/test1407
index ab25a30..0b40a47 100644
--- a/tests/data/test1407
+++ b/tests/data/test1407
@@ -75,6 +75,8 @@ int main(int argc, char *argv[])
   CURLOPT_STDERR set to a objectpointer
   CURLOPT_DEBUGFUNCTION set to a functionpointer
   CURLOPT_DEBUGDATA set to a objectpointer
+  CURLOPT_HEADERFUNCTION set to a functionpointer
+  CURLOPT_HEADERDATA set to a objectpointer
 
   */
 
-- 
1.7.9.1

