[331] | 1 | /***************************************************************************
|
---|
| 2 | * _ _ ____ _
|
---|
| 3 | * Project ___| | | | _ \| |
|
---|
| 4 | * / __| | | | |_) | |
|
---|
| 5 | * | (__| |_| | _ <| |___
|
---|
| 6 | * \___|\___/|_| \_\_____|
|
---|
| 7 | *
|
---|
| 8 | * Copyright (C) 1998 - 2017, Daniel Stenberg, <daniel@haxx.se>, et al.
|
---|
| 9 | *
|
---|
| 10 | * This software is licensed as described in the file COPYING, which
|
---|
| 11 | * you should have received as part of this distribution. The terms
|
---|
| 12 | * are also available at https://curl.haxx.se/docs/copyright.html.
|
---|
| 13 | *
|
---|
| 14 | * You may opt to use, copy, modify, merge, publish, distribute and/or sell
|
---|
| 15 | * copies of the Software, and permit persons to whom the Software is
|
---|
| 16 | * furnished to do so, under the terms of the COPYING file.
|
---|
| 17 | *
|
---|
| 18 | * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
|
---|
| 19 | * KIND, either express or implied.
|
---|
| 20 | *
|
---|
| 21 | ***************************************************************************/
|
---|
| 22 | #include "tool_setup.h"
|
---|
| 23 |
|
---|
| 24 | #include "strcase.h"
|
---|
| 25 |
|
---|
| 26 | #define ENABLE_CURLX_PRINTF
|
---|
| 27 | /* use our own printf() functions */
|
---|
| 28 | #include "curlx.h"
|
---|
| 29 |
|
---|
| 30 | #include "tool_cfgable.h"
|
---|
| 31 | #include "tool_doswin.h"
|
---|
| 32 | #include "tool_msgs.h"
|
---|
| 33 | #include "tool_cb_hdr.h"
|
---|
| 34 |
|
---|
| 35 | #include "memdebug.h" /* keep this as LAST include */
|
---|
| 36 |
|
---|
| 37 | static char *parse_filename(const char *ptr, size_t len);
|
---|
| 38 |
|
---|
| 39 | /*
|
---|
| 40 | ** callback for CURLOPT_HEADERFUNCTION
|
---|
| 41 | */
|
---|
| 42 |
|
---|
| 43 | size_t tool_header_cb(char *ptr, size_t size, size_t nmemb, void *userdata)
|
---|
| 44 | {
|
---|
| 45 | struct HdrCbData *hdrcbdata = userdata;
|
---|
| 46 | struct OutStruct *outs = hdrcbdata->outs;
|
---|
| 47 | struct OutStruct *heads = hdrcbdata->heads;
|
---|
| 48 | const char *str = ptr;
|
---|
| 49 | const size_t cb = size * nmemb;
|
---|
| 50 | const char *end = (char *)ptr + cb;
|
---|
| 51 | char *url = NULL;
|
---|
| 52 |
|
---|
| 53 | /*
|
---|
| 54 | * Once that libcurl has called back tool_header_cb() the returned value
|
---|
| 55 | * is checked against the amount that was intended to be written, if
|
---|
| 56 | * it does not match then it fails with CURLE_WRITE_ERROR. So at this
|
---|
| 57 | * point returning a value different from sz*nmemb indicates failure.
|
---|
| 58 | */
|
---|
| 59 | size_t failure = (size && nmemb) ? 0 : 1;
|
---|
| 60 |
|
---|
| 61 | if(!heads->config)
|
---|
| 62 | return failure;
|
---|
| 63 |
|
---|
| 64 | #ifdef DEBUGBUILD
|
---|
| 65 | if(size * nmemb > (size_t)CURL_MAX_HTTP_HEADER) {
|
---|
| 66 | warnf(heads->config->global, "Header data exceeds single call write "
|
---|
| 67 | "limit!\n");
|
---|
| 68 | return failure;
|
---|
| 69 | }
|
---|
| 70 | #endif
|
---|
| 71 |
|
---|
| 72 | /*
|
---|
| 73 | * Write header data when curl option --dump-header (-D) is given.
|
---|
| 74 | */
|
---|
| 75 |
|
---|
| 76 | if(heads->config->headerfile && heads->stream) {
|
---|
| 77 | size_t rc = fwrite(ptr, size, nmemb, heads->stream);
|
---|
| 78 | if(rc != cb)
|
---|
| 79 | return rc;
|
---|
| 80 | /* flush the stream to send off what we got earlier */
|
---|
| 81 | (void)fflush(heads->stream);
|
---|
| 82 | }
|
---|
| 83 |
|
---|
| 84 | /*
|
---|
| 85 | * This callback sets the filename where output shall be written when
|
---|
| 86 | * curl options --remote-name (-O) and --remote-header-name (-J) have
|
---|
| 87 | * been simultaneously given and additionally server returns an HTTP
|
---|
| 88 | * Content-Disposition header specifying a filename property.
|
---|
| 89 | */
|
---|
| 90 |
|
---|
| 91 | if(hdrcbdata->honor_cd_filename &&
|
---|
| 92 | (cb > 20) && checkprefix("Content-disposition:", str) &&
|
---|
| 93 | !curl_easy_getinfo(outs->config->easy, CURLINFO_EFFECTIVE_URL, &url) &&
|
---|
| 94 | url && (checkprefix("http://", url) || checkprefix("https://", url))) {
|
---|
| 95 | const char *p = str + 20;
|
---|
| 96 |
|
---|
| 97 | /* look for the 'filename=' parameter
|
---|
| 98 | (encoded filenames (*=) are not supported) */
|
---|
| 99 | for(;;) {
|
---|
| 100 | char *filename;
|
---|
| 101 | size_t len;
|
---|
| 102 |
|
---|
| 103 | while(*p && (p < end) && !ISALPHA(*p))
|
---|
| 104 | p++;
|
---|
| 105 | if(p > end - 9)
|
---|
| 106 | break;
|
---|
| 107 |
|
---|
| 108 | if(memcmp(p, "filename=", 9)) {
|
---|
| 109 | /* no match, find next parameter */
|
---|
| 110 | while((p < end) && (*p != ';'))
|
---|
| 111 | p++;
|
---|
| 112 | continue;
|
---|
| 113 | }
|
---|
| 114 | p += 9;
|
---|
| 115 |
|
---|
| 116 | /* this expression below typecasts 'cb' only to avoid
|
---|
| 117 | warning: signed and unsigned type in conditional expression
|
---|
| 118 | */
|
---|
| 119 | len = (ssize_t)cb - (p - str);
|
---|
| 120 | filename = parse_filename(p, len);
|
---|
| 121 | if(filename) {
|
---|
| 122 | outs->filename = filename;
|
---|
| 123 | outs->alloc_filename = TRUE;
|
---|
| 124 | outs->is_cd_filename = TRUE;
|
---|
| 125 | outs->s_isreg = TRUE;
|
---|
| 126 | outs->fopened = FALSE;
|
---|
| 127 | outs->stream = NULL;
|
---|
| 128 | hdrcbdata->honor_cd_filename = FALSE;
|
---|
| 129 | break;
|
---|
| 130 | }
|
---|
| 131 | return failure;
|
---|
| 132 | }
|
---|
| 133 | }
|
---|
| 134 |
|
---|
| 135 | return cb;
|
---|
| 136 | }
|
---|
| 137 |
|
---|
| 138 | /*
|
---|
| 139 | * Copies a file name part and returns an ALLOCATED data buffer.
|
---|
| 140 | */
|
---|
| 141 | static char *parse_filename(const char *ptr, size_t len)
|
---|
| 142 | {
|
---|
| 143 | char *copy;
|
---|
| 144 | char *p;
|
---|
| 145 | char *q;
|
---|
| 146 | char stop = '\0';
|
---|
| 147 |
|
---|
| 148 | /* simple implementation of strndup() */
|
---|
| 149 | copy = malloc(len + 1);
|
---|
| 150 | if(!copy)
|
---|
| 151 | return NULL;
|
---|
| 152 | memcpy(copy, ptr, len);
|
---|
| 153 | copy[len] = '\0';
|
---|
| 154 |
|
---|
| 155 | p = copy;
|
---|
| 156 | if(*p == '\'' || *p == '"') {
|
---|
| 157 | /* store the starting quote */
|
---|
| 158 | stop = *p;
|
---|
| 159 | p++;
|
---|
| 160 | }
|
---|
| 161 | else
|
---|
| 162 | stop = ';';
|
---|
| 163 |
|
---|
| 164 | /* scan for the end letter and stop there */
|
---|
| 165 | q = strchr(p, stop);
|
---|
| 166 | if(q)
|
---|
| 167 | *q = '\0';
|
---|
| 168 |
|
---|
| 169 | /* if the filename contains a path, only use filename portion */
|
---|
| 170 | q = strrchr(p, '/');
|
---|
| 171 | if(q) {
|
---|
| 172 | p = q + 1;
|
---|
| 173 | if(!*p) {
|
---|
| 174 | Curl_safefree(copy);
|
---|
| 175 | return NULL;
|
---|
| 176 | }
|
---|
| 177 | }
|
---|
| 178 |
|
---|
| 179 | /* If the filename contains a backslash, only use filename portion. The idea
|
---|
| 180 | is that even systems that don't handle backslashes as path separators
|
---|
| 181 | probably want the path removed for convenience. */
|
---|
| 182 | q = strrchr(p, '\\');
|
---|
| 183 | if(q) {
|
---|
| 184 | p = q + 1;
|
---|
| 185 | if(!*p) {
|
---|
| 186 | Curl_safefree(copy);
|
---|
| 187 | return NULL;
|
---|
| 188 | }
|
---|
| 189 | }
|
---|
| 190 |
|
---|
| 191 | /* make sure the file name doesn't end in \r or \n */
|
---|
| 192 | q = strchr(p, '\r');
|
---|
| 193 | if(q)
|
---|
| 194 | *q = '\0';
|
---|
| 195 |
|
---|
| 196 | q = strchr(p, '\n');
|
---|
| 197 | if(q)
|
---|
| 198 | *q = '\0';
|
---|
| 199 |
|
---|
| 200 | if(copy != p)
|
---|
| 201 | memmove(copy, p, strlen(p) + 1);
|
---|
| 202 |
|
---|
| 203 | #if defined(MSDOS) || defined(WIN32)
|
---|
| 204 | {
|
---|
| 205 | char *sanitized;
|
---|
| 206 | SANITIZEcode sc = sanitize_file_name(&sanitized, copy, 0);
|
---|
| 207 | Curl_safefree(copy);
|
---|
| 208 | if(sc)
|
---|
| 209 | return NULL;
|
---|
| 210 | copy = sanitized;
|
---|
| 211 | }
|
---|
| 212 | #endif /* MSDOS || WIN32 */
|
---|
| 213 |
|
---|
| 214 | /* in case we built debug enabled, we allow an environment variable
|
---|
| 215 | * named CURL_TESTDIR to prefix the given file name to put it into a
|
---|
| 216 | * specific directory
|
---|
| 217 | */
|
---|
| 218 | #ifdef DEBUGBUILD
|
---|
| 219 | {
|
---|
| 220 | char *tdir = curlx_getenv("CURL_TESTDIR");
|
---|
| 221 | if(tdir) {
|
---|
| 222 | char buffer[512]; /* suitably large */
|
---|
| 223 | snprintf(buffer, sizeof(buffer), "%s/%s", tdir, copy);
|
---|
| 224 | Curl_safefree(copy);
|
---|
| 225 | copy = strdup(buffer); /* clone the buffer, we don't use the libcurl
|
---|
| 226 | aprintf() or similar since we want to use the
|
---|
| 227 | same memory code as the "real" parse_filename
|
---|
| 228 | function */
|
---|
| 229 | curl_free(tdir);
|
---|
| 230 | }
|
---|
| 231 | }
|
---|
| 232 | #endif
|
---|
| 233 |
|
---|
| 234 | return copy;
|
---|
| 235 | }
|
---|
| 236 |
|
---|