diff --git a/src/tool_cb_hdr.c b/src/tool_cb_hdr.c index e963311773bf5365732e6a7e88ac46c0ed4636bc..738cd5dfc0751b3c29a7a63fe05e76e71141f168 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 5909336e6d09a7a9c551ce4eb9ea369852a75238..0300c0068ab0f3489a75ac241f63aa4ab408e19e 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 e3d00213316e13550fa1d7a5393b78fee6bd177b..689ffb2d7d5aae02c2798167d3ec78227a9dd0f4 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 59b2856dd5b6463f88700fa416da981cbf4e9ad1..72989c4c4a807e952fe0d85e06382ab2e51ba120 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 e094ec456032493a26f75e018bf0b8aa2a7d13e5..e709c8e08d65c2862ae1dd66dc9dbd6e3adbbfc5 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 d56779207bce8511869e871c2b83e969515710fb..c3bf834bde30a3b9d8f31a32a30edf5aceb73c75 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 3e4a03440fad5aac2e2ffa070f5112bd68984659..3ec7dd032fd790ed687173bfccccf0dfb3ceee10 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 f856a74163f39b4a5a4857708dd82010d7927b8c..88a06ba39c3d7394ffde85343653ebf47a124358 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 187f8498346603087e4ddc0844d3e7f2fae40ee4..692bb1af77fe5601aab018097c24da2531eba74b 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 216d68e81132783803c7b0d34be2438b07344b9b..478e45ff394daa1ca29082795ffcc4ad05a78ed3 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 ab25a30a3cf9f6a13e18553d48d787d3428cfeca..0b40a4732a73e90625d36cbc75a3fe05c707151a 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 */