2011-09-24 11:38:16 -04:00
|
|
|
/***************************************************************************
|
|
|
|
* _ _ ____ _
|
|
|
|
* Project ___| | | | _ \| |
|
|
|
|
* / __| | | | |_) | |
|
|
|
|
* | (__| |_| | _ <| |___
|
|
|
|
* \___|\___/|_| \_\_____|
|
|
|
|
*
|
2017-03-26 11:02:22 -04:00
|
|
|
* Copyright (C) 1998 - 2017, Daniel Stenberg, <daniel@haxx.se>, et al.
|
2011-09-24 11:38:16 -04:00
|
|
|
*
|
|
|
|
* This software is licensed as described in the file COPYING, which
|
|
|
|
* you should have received as part of this distribution. The terms
|
2016-02-02 18:19:02 -05:00
|
|
|
* are also available at https://curl.haxx.se/docs/copyright.html.
|
2011-09-24 11:38:16 -04:00
|
|
|
*
|
|
|
|
* You may opt to use, copy, modify, merge, publish, distribute and/or sell
|
|
|
|
* copies of the Software, and permit persons to whom the Software is
|
|
|
|
* furnished to do so, under the terms of the COPYING file.
|
|
|
|
*
|
|
|
|
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
|
|
|
|
* KIND, either express or implied.
|
|
|
|
*
|
|
|
|
***************************************************************************/
|
2012-04-06 17:35:15 -04:00
|
|
|
#include "tool_setup.h"
|
2011-09-24 11:38:16 -04:00
|
|
|
|
2016-09-30 12:54:02 -04:00
|
|
|
#include "strcase.h"
|
2011-09-24 11:38:16 -04:00
|
|
|
|
|
|
|
#define ENABLE_CURLX_PRINTF
|
|
|
|
/* use our own printf() functions */
|
|
|
|
#include "curlx.h"
|
|
|
|
|
|
|
|
#include "tool_cfgable.h"
|
2016-01-26 17:23:15 -05:00
|
|
|
#include "tool_doswin.h"
|
2011-09-26 07:07:34 -04:00
|
|
|
#include "tool_msgs.h"
|
2011-09-24 11:38:16 -04:00
|
|
|
#include "tool_cb_hdr.h"
|
|
|
|
|
2013-01-03 20:50:28 -05:00
|
|
|
#include "memdebug.h" /* keep this as LAST include */
|
2011-09-24 11:38:16 -04:00
|
|
|
|
|
|
|
static char *parse_filename(const char *ptr, size_t len);
|
|
|
|
|
|
|
|
/*
|
|
|
|
** callback for CURLOPT_HEADERFUNCTION
|
|
|
|
*/
|
|
|
|
|
2017-03-29 04:02:34 -04:00
|
|
|
size_t tool_header_cb(char *ptr, size_t size, size_t nmemb, void *userdata)
|
2011-09-24 11:38:16 -04:00
|
|
|
{
|
2012-06-08 23:42:39 -04:00
|
|
|
struct HdrCbData *hdrcbdata = userdata;
|
|
|
|
struct OutStruct *outs = hdrcbdata->outs;
|
|
|
|
struct OutStruct *heads = hdrcbdata->heads;
|
2011-09-24 11:38:16 -04:00
|
|
|
const char *str = ptr;
|
|
|
|
const size_t cb = size * nmemb;
|
2016-11-23 02:30:18 -05:00
|
|
|
const char *end = (char *)ptr + cb;
|
2016-05-01 16:07:04 -04:00
|
|
|
char *url = NULL;
|
2011-09-24 11:38:16 -04:00
|
|
|
|
2011-09-26 07:07:34 -04:00
|
|
|
/*
|
|
|
|
* Once that libcurl has called back tool_header_cb() the returned value
|
|
|
|
* is checked against the amount that was intended to be written, if
|
|
|
|
* it does not match then it fails with CURLE_WRITE_ERROR. So at this
|
|
|
|
* point returning a value different from sz*nmemb indicates failure.
|
|
|
|
*/
|
2017-03-30 08:14:53 -04:00
|
|
|
size_t failure = (size && nmemb) ? 0 : 1;
|
2011-09-26 07:07:34 -04:00
|
|
|
|
2012-06-08 23:42:39 -04:00
|
|
|
if(!heads->config)
|
2011-09-30 14:56:56 -04:00
|
|
|
return failure;
|
|
|
|
|
2011-09-26 07:07:34 -04:00
|
|
|
#ifdef DEBUGBUILD
|
2011-10-24 11:28:41 -04:00
|
|
|
if(size * nmemb > (size_t)CURL_MAX_HTTP_HEADER) {
|
2015-02-27 15:48:38 -05:00
|
|
|
warnf(heads->config->global, "Header data exceeds single call write "
|
|
|
|
"limit!\n");
|
2011-09-26 07:07:34 -04:00
|
|
|
return failure;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2012-06-07 15:57:53 -04:00
|
|
|
/*
|
2012-06-08 23:42:39 -04:00
|
|
|
* Write header data when curl option --dump-header (-D) is given.
|
|
|
|
*/
|
2012-06-07 15:57:53 -04:00
|
|
|
|
2012-06-08 23:42:39 -04:00
|
|
|
if(heads->config->headerfile && heads->stream) {
|
|
|
|
size_t rc = fwrite(ptr, size, nmemb, heads->stream);
|
|
|
|
if(rc != cb)
|
|
|
|
return rc;
|
2015-06-23 19:53:02 -04:00
|
|
|
/* flush the stream to send off what we got earlier */
|
|
|
|
(void)fflush(heads->stream);
|
2012-06-08 23:42:39 -04:00
|
|
|
}
|
2012-06-07 15:57:53 -04:00
|
|
|
|
2012-06-08 23:42:39 -04:00
|
|
|
/*
|
|
|
|
* This callback sets the filename where output shall be written when
|
|
|
|
* curl options --remote-name (-O) and --remote-header-name (-J) have
|
|
|
|
* been simultaneously given and additionally server returns an HTTP
|
|
|
|
* Content-Disposition header specifying a filename property.
|
|
|
|
*/
|
2012-06-07 15:57:53 -04:00
|
|
|
|
2012-06-08 23:42:39 -04:00
|
|
|
if(hdrcbdata->honor_cd_filename &&
|
2016-05-01 16:07:04 -04:00
|
|
|
(cb > 20) && checkprefix("Content-disposition:", str) &&
|
|
|
|
!curl_easy_getinfo(outs->config->easy, CURLINFO_EFFECTIVE_URL, &url) &&
|
|
|
|
url && (checkprefix("http://", url) || checkprefix("https://", url))) {
|
2011-09-24 11:38:16 -04:00
|
|
|
const char *p = str + 20;
|
|
|
|
|
|
|
|
/* look for the 'filename=' parameter
|
|
|
|
(encoded filenames (*=) are not supported) */
|
|
|
|
for(;;) {
|
|
|
|
char *filename;
|
|
|
|
size_t len;
|
|
|
|
|
|
|
|
while(*p && (p < end) && !ISALPHA(*p))
|
|
|
|
p++;
|
|
|
|
if(p > end - 9)
|
|
|
|
break;
|
|
|
|
|
|
|
|
if(memcmp(p, "filename=", 9)) {
|
|
|
|
/* no match, find next parameter */
|
|
|
|
while((p < end) && (*p != ';'))
|
|
|
|
p++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
p += 9;
|
|
|
|
|
|
|
|
/* this expression below typecasts 'cb' only to avoid
|
|
|
|
warning: signed and unsigned type in conditional expression
|
|
|
|
*/
|
|
|
|
len = (ssize_t)cb - (p - str);
|
|
|
|
filename = parse_filename(p, len);
|
2016-02-05 01:44:27 -05:00
|
|
|
if(filename) {
|
|
|
|
outs->filename = filename;
|
|
|
|
outs->alloc_filename = TRUE;
|
|
|
|
outs->is_cd_filename = TRUE;
|
|
|
|
outs->s_isreg = TRUE;
|
|
|
|
outs->fopened = FALSE;
|
|
|
|
outs->stream = NULL;
|
|
|
|
hdrcbdata->honor_cd_filename = FALSE;
|
|
|
|
break;
|
2016-01-26 17:23:15 -05:00
|
|
|
}
|
2017-03-10 08:28:37 -05:00
|
|
|
return failure;
|
2011-09-24 11:38:16 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return cb;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Copies a file name part and returns an ALLOCATED data buffer.
|
|
|
|
*/
|
|
|
|
static char *parse_filename(const char *ptr, size_t len)
|
|
|
|
{
|
|
|
|
char *copy;
|
|
|
|
char *p;
|
|
|
|
char *q;
|
|
|
|
char stop = '\0';
|
|
|
|
|
|
|
|
/* simple implementation of strndup() */
|
2017-09-09 17:55:08 -04:00
|
|
|
copy = malloc(len + 1);
|
2011-09-24 11:38:16 -04:00
|
|
|
if(!copy)
|
|
|
|
return NULL;
|
|
|
|
memcpy(copy, ptr, len);
|
|
|
|
copy[len] = '\0';
|
|
|
|
|
|
|
|
p = copy;
|
|
|
|
if(*p == '\'' || *p == '"') {
|
|
|
|
/* store the starting quote */
|
|
|
|
stop = *p;
|
|
|
|
p++;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
stop = ';';
|
|
|
|
|
2017-10-07 10:47:11 -04:00
|
|
|
/* scan for the end letter and stop there */
|
|
|
|
q = strchr(p, stop);
|
|
|
|
if(q)
|
|
|
|
*q = '\0';
|
|
|
|
|
2011-09-24 11:38:16 -04:00
|
|
|
/* if the filename contains a path, only use filename portion */
|
2017-10-07 10:47:11 -04:00
|
|
|
q = strrchr(p, '/');
|
2011-09-24 11:38:16 -04:00
|
|
|
if(q) {
|
|
|
|
p = q + 1;
|
|
|
|
if(!*p) {
|
|
|
|
Curl_safefree(copy);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If the filename contains a backslash, only use filename portion. The idea
|
|
|
|
is that even systems that don't handle backslashes as path separators
|
|
|
|
probably want the path removed for convenience. */
|
|
|
|
q = strrchr(p, '\\');
|
|
|
|
if(q) {
|
|
|
|
p = q + 1;
|
|
|
|
if(!*p) {
|
|
|
|
Curl_safefree(copy);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* make sure the file name doesn't end in \r or \n */
|
|
|
|
q = strchr(p, '\r');
|
|
|
|
if(q)
|
|
|
|
*q = '\0';
|
|
|
|
|
|
|
|
q = strchr(p, '\n');
|
|
|
|
if(q)
|
|
|
|
*q = '\0';
|
|
|
|
|
|
|
|
if(copy != p)
|
|
|
|
memmove(copy, p, strlen(p) + 1);
|
|
|
|
|
2016-02-05 01:44:27 -05:00
|
|
|
#if defined(MSDOS) || defined(WIN32)
|
|
|
|
{
|
|
|
|
char *sanitized;
|
|
|
|
SANITIZEcode sc = sanitize_file_name(&sanitized, copy, 0);
|
|
|
|
Curl_safefree(copy);
|
|
|
|
if(sc)
|
|
|
|
return NULL;
|
|
|
|
copy = sanitized;
|
|
|
|
}
|
|
|
|
#endif /* MSDOS || WIN32 */
|
|
|
|
|
2017-03-26 11:02:22 -04:00
|
|
|
/* in case we built debug enabled, we allow an environment variable
|
2011-09-24 11:38:16 -04:00
|
|
|
* named CURL_TESTDIR to prefix the given file name to put it into a
|
|
|
|
* specific directory
|
|
|
|
*/
|
2011-09-26 07:07:34 -04:00
|
|
|
#ifdef DEBUGBUILD
|
2011-09-24 11:38:16 -04:00
|
|
|
{
|
|
|
|
char *tdir = curlx_getenv("CURL_TESTDIR");
|
|
|
|
if(tdir) {
|
|
|
|
char buffer[512]; /* suitably large */
|
|
|
|
snprintf(buffer, sizeof(buffer), "%s/%s", tdir, copy);
|
|
|
|
Curl_safefree(copy);
|
|
|
|
copy = strdup(buffer); /* clone the buffer, we don't use the libcurl
|
|
|
|
aprintf() or similar since we want to use the
|
|
|
|
same memory code as the "real" parse_filename
|
|
|
|
function */
|
|
|
|
curl_free(tdir);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return copy;
|
|
|
|
}
|
|
|
|
|