| 1 |
/*************************************************************************** |
| 2 |
* _ _ ____ _ |
| 3 |
* Project ___| | | | _ \| | |
| 4 |
* / __| | | | |_) | | |
| 5 |
* | (__| |_| | _ <| |___ |
| 6 |
* \___|\___/|_| \_\_____| |
| 7 |
* |
| 8 |
* Copyright (C) 1998 - 2020, Daniel Stenberg, <[email protected]>, 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 |
#include "tool_cb_wrt.h" |
| 35 |
#include "tool_operate.h" |
| 36 |
|
| 37 |
#include "memdebug.h" /* keep this as LAST include */ |
| 38 |
|
| 39 |
static char *parse_filename(const char *ptr, size_t len); |
| 40 |
|
| 41 |
#ifdef WIN32 |
| 42 |
#define BOLD |
| 43 |
#define BOLDOFF |
| 44 |
#else |
| 45 |
#define BOLD "\x1b[1m" |
| 46 |
/* Switch off bold by setting "all attributes off" since the explicit |
| 47 |
bold-off code (21) isn't supported everywhere - like in the mac |
| 48 |
Terminal. */ |
| 49 |
#define BOLDOFF "\x1b[0m" |
| 50 |
#endif |
| 51 |
|
| 52 |
/* |
| 53 |
** callback for CURLOPT_HEADERFUNCTION |
| 54 |
*/ |
| 55 |
|
| 56 |
size_t tool_header_cb(char *ptr, size_t size, size_t nmemb, void *userdata) |
| 57 |
{ |
| 58 |
struct per_transfer *per = userdata; |
| 59 |
struct HdrCbData *hdrcbdata = &per->hdrcbdata; |
| 60 |
struct OutStruct *outs = &per->outs; |
| 61 |
struct OutStruct *heads = &per->heads; |
| 62 |
struct OutStruct *etag_save = &per->etag_save; |
| 63 |
const char *str = ptr; |
| 64 |
const size_t cb = size * nmemb; |
| 65 |
const char *end = (char *)ptr + cb; |
| 66 |
long protocol = 0; |
| 67 |
|
| 68 |
/* |
| 69 |
* Once that libcurl has called back tool_header_cb() the returned value |
| 70 |
* is checked against the amount that was intended to be written, if |
| 71 |
* it does not match then it fails with CURLE_WRITE_ERROR. So at this |
| 72 |
* point returning a value different from sz*nmemb indicates failure. |
| 73 |
*/ |
| 74 |
size_t failure = (size && nmemb) ? 0 : 1; |
| 75 |
|
| 76 |
if(!per->config) |
| 77 |
return failure; |
| 78 |
|
| 79 |
#ifdef DEBUGBUILD |
| 80 |
if(size * nmemb > (size_t)CURL_MAX_HTTP_HEADER) { |
| 81 |
warnf(per->config->global, "Header data exceeds single call write " |
| 82 |
"limit!\n"); |
| 83 |
return failure; |
| 84 |
} |
| 85 |
#endif |
| 86 |
|
| 87 |
/* |
| 88 |
* Write header data when curl option --dump-header (-D) is given. |
| 89 |
*/ |
| 90 |
|
| 91 |
if(per->config->headerfile && heads->stream) { |
| 92 |
size_t rc = fwrite(ptr, size, nmemb, heads->stream); |
| 93 |
if(rc != cb) |
| 94 |
return rc; |
| 95 |
/* flush the stream to send off what we got earlier */ |
| 96 |
(void)fflush(heads->stream); |
| 97 |
} |
| 98 |
|
| 99 |
/* |
| 100 |
* Write etag to file when --etag-save option is given. |
| 101 |
* etag string that we want is enveloped in double quotes |
| 102 |
*/ |
| 103 |
if(per->config->etag_save_file && etag_save->stream) { |
| 104 |
/* match only header that start with etag (case insensitive) */ |
| 105 |
if(curl_strnequal(str, "etag:", 5)) { |
| 106 |
char *etag_h = NULL; |
| 107 |
char *first = NULL; |
| 108 |
char *last = NULL; |
| 109 |
size_t etag_length = 0; |
| 110 |
|
| 111 |
etag_h = ptr; |
| 112 |
/* point to first occurrence of double quote */ |
| 113 |
first = memchr(etag_h, '\"', cb); |
| 114 |
|
| 115 |
/* |
| 116 |
* if server side messed with the etag header and doesn't include |
| 117 |
* double quotes around the etag, kindly exit with a warning |
| 118 |
*/ |
| 119 |
|
| 120 |
if(!first) { |
| 121 |
warnf(per->config->global, |
| 122 |
"\nReceived header etag is missing double quote/s\n"); |
| 123 |
return 1; |
| 124 |
} |
| 125 |
else { |
| 126 |
/* discard first double quote */ |
| 127 |
first++; |
| 128 |
} |
| 129 |
|
| 130 |
/* point to last occurrence of double quote */ |
| 131 |
last = memchr(first, '\"', cb); |
| 132 |
|
| 133 |
if(!last) { |
| 134 |
warnf(per->config->global, |
| 135 |
"\nReceived header etag is missing double quote/s\n"); |
| 136 |
return 1; |
| 137 |
} |
| 138 |
|
| 139 |
/* get length of desired etag */ |
| 140 |
etag_length = (size_t)last - (size_t)first; |
| 141 |
|
| 142 |
fwrite(first, size, etag_length, etag_save->stream); |
| 143 |
/* terminate with new line */ |
| 144 |
fputc('\n', etag_save->stream); |
| 145 |
} |
| 146 |
|
| 147 |
(void)fflush(etag_save->stream); |
| 148 |
} |
| 149 |
|
| 150 |
/* |
| 151 |
* This callback sets the filename where output shall be written when |
| 152 |
* curl options --remote-name (-O) and --remote-header-name (-J) have |
| 153 |
* been simultaneously given and additionally server returns an HTTP |
| 154 |
* Content-Disposition header specifying a filename property. |
| 155 |
*/ |
| 156 |
|
| 157 |
curl_easy_getinfo(per->curl, CURLINFO_PROTOCOL, &protocol); |
| 158 |
if(hdrcbdata->honor_cd_filename && |
| 159 |
(cb > 20) && checkprefix("Content-disposition:", str) && |
| 160 |
(protocol & (CURLPROTO_HTTPS|CURLPROTO_HTTP))) { |
| 161 |
const char *p = str + 20; |
| 162 |
|
| 163 |
/* look for the 'filename=' parameter |
| 164 |
(encoded filenames (*=) are not supported) */ |
| 165 |
for(;;) { |
| 166 |
char *filename; |
| 167 |
size_t len; |
| 168 |
|
| 169 |
while(*p && (p < end) && !ISALPHA(*p)) |
| 170 |
p++; |
| 171 |
if(p > end - 9) |
| 172 |
break; |
| 173 |
|
| 174 |
if(memcmp(p, "filename=", 9)) { |
| 175 |
/* no match, find next parameter */ |
| 176 |
while((p < end) && (*p != ';')) |
| 177 |
p++; |
| 178 |
continue; |
| 179 |
} |
| 180 |
p += 9; |
| 181 |
|
| 182 |
/* this expression below typecasts 'cb' only to avoid |
| 183 |
warning: signed and unsigned type in conditional expression |
| 184 |
*/ |
| 185 |
len = (ssize_t)cb - (p - str); |
| 186 |
filename = parse_filename(p, len); |
| 187 |
if(filename) { |
| 188 |
if(outs->stream) { |
| 189 |
int rc; |
| 190 |
/* already opened and possibly written to */ |
| 191 |
if(outs->fopened) |
| 192 |
fclose(outs->stream); |
| 193 |
outs->stream = NULL; |
| 194 |
|
| 195 |
/* rename the initial file name to the new file name */ |
| 196 |
rc = rename(outs->filename, filename); |
| 197 |
if(rc != 0) { |
| 198 |
warnf(per->config->global, "Failed to rename %s -> %s: %s\n", |
| 199 |
outs->filename, filename, strerror(errno)); |
| 200 |
} |
| 201 |
if(outs->alloc_filename) |
| 202 |
Curl_safefree(outs->filename); |
| 203 |
if(rc != 0) { |
| 204 |
free(filename); |
| 205 |
return failure; |
| 206 |
} |
| 207 |
} |
| 208 |
outs->is_cd_filename = TRUE; |
| 209 |
outs->s_isreg = TRUE; |
| 210 |
outs->fopened = FALSE; |
| 211 |
outs->filename = filename; |
| 212 |
outs->alloc_filename = TRUE; |
| 213 |
hdrcbdata->honor_cd_filename = FALSE; /* done now! */ |
| 214 |
if(!tool_create_output_file(outs, per->config)) |
| 215 |
return failure; |
| 216 |
} |
| 217 |
break; |
| 218 |
} |
| 219 |
if(!outs->stream && !tool_create_output_file(outs, per->config)) |
| 220 |
return failure; |
| 221 |
} |
| 222 |
|
| 223 |
if(hdrcbdata->config->show_headers && |
| 224 |
(protocol & |
| 225 |
(CURLPROTO_HTTP|CURLPROTO_HTTPS|CURLPROTO_RTSP|CURLPROTO_FILE))) { |
| 226 |
/* bold headers only for selected protocols */ |
| 227 |
char *value = NULL; |
| 228 |
|
| 229 |
if(!outs->stream && !tool_create_output_file(outs, per->config)) |
| 230 |
return failure; |
| 231 |
|
| 232 |
if(hdrcbdata->global->isatty && hdrcbdata->global->styled_output) |
| 233 |
value = memchr(ptr, ':', cb); |
| 234 |
if(value) { |
| 235 |
size_t namelen = value - ptr; |
| 236 |
fprintf(outs->stream, BOLD "%.*s" BOLDOFF ":", namelen, ptr); |
| 237 |
fwrite(&value[1], cb - namelen - 1, 1, outs->stream); |
| 238 |
} |
| 239 |
else |
| 240 |
/* not "handled", just show it */ |
| 241 |
fwrite(ptr, cb, 1, outs->stream); |
| 242 |
} |
| 243 |
return cb; |
| 244 |
} |
| 245 |
|
| 246 |
/* |
| 247 |
* Copies a file name part and returns an ALLOCATED data buffer. |
| 248 |
*/ |
| 249 |
static char *parse_filename(const char *ptr, size_t len) |
| 250 |
{ |
| 251 |
char *copy; |
| 252 |
char *p; |
| 253 |
char *q; |
| 254 |
char stop = '\0'; |
| 255 |
|
| 256 |
/* simple implementation of strndup() */ |
| 257 |
copy = malloc(len + 1); |
| 258 |
if(!copy) |
| 259 |
return NULL; |
| 260 |
memcpy(copy, ptr, len); |
| 261 |
copy[len] = '\0'; |
| 262 |
|
| 263 |
p = copy; |
| 264 |
if(*p == '\'' || *p == '"') { |
| 265 |
/* store the starting quote */ |
| 266 |
stop = *p; |
| 267 |
p++; |
| 268 |
} |
| 269 |
else |
| 270 |
stop = ';'; |
| 271 |
|
| 272 |
/* scan for the end letter and stop there */ |
| 273 |
q = strchr(p, stop); |
| 274 |
if(q) |
| 275 |
*q = '\0'; |
| 276 |
|
| 277 |
/* if the filename contains a path, only use filename portion */ |
| 278 |
q = strrchr(p, '/'); |
| 279 |
if(q) { |
| 280 |
p = q + 1; |
| 281 |
if(!*p) { |
| 282 |
Curl_safefree(copy); |
| 283 |
return NULL; |
| 284 |
} |
| 285 |
} |
| 286 |
|
| 287 |
/* If the filename contains a backslash, only use filename portion. The idea |
| 288 |
is that even systems that don't handle backslashes as path separators |
| 289 |
probably want the path removed for convenience. */ |
| 290 |
q = strrchr(p, '\\'); |
| 291 |
if(q) { |
| 292 |
p = q + 1; |
| 293 |
if(!*p) { |
| 294 |
Curl_safefree(copy); |
| 295 |
return NULL; |
| 296 |
} |
| 297 |
} |
| 298 |
|
| 299 |
/* make sure the file name doesn't end in \r or \n */ |
| 300 |
q = strchr(p, '\r'); |
| 301 |
if(q) |
| 302 |
*q = '\0'; |
| 303 |
|
| 304 |
q = strchr(p, '\n'); |
| 305 |
if(q) |
| 306 |
*q = '\0'; |
| 307 |
|
| 308 |
if(copy != p) |
| 309 |
memmove(copy, p, strlen(p) + 1); |
| 310 |
|
| 311 |
#if defined(MSDOS) || defined(WIN32) |
| 312 |
{ |
| 313 |
char *sanitized; |
| 314 |
SANITIZEcode sc = sanitize_file_name(&sanitized, copy, 0); |
| 315 |
Curl_safefree(copy); |
| 316 |
if(sc) |
| 317 |
return NULL; |
| 318 |
copy = sanitized; |
| 319 |
} |
| 320 |
#endif /* MSDOS || WIN32 */ |
| 321 |
|
| 322 |
/* in case we built debug enabled, we allow an environment variable |
| 323 |
* named CURL_TESTDIR to prefix the given file name to put it into a |
| 324 |
* specific directory |
| 325 |
*/ |
| 326 |
#ifdef DEBUGBUILD |
| 327 |
{ |
| 328 |
char *tdir = curlx_getenv("CURL_TESTDIR"); |
| 329 |
if(tdir) { |
| 330 |
char buffer[512]; /* suitably large */ |
| 331 |
msnprintf(buffer, sizeof(buffer), "%s/%s", tdir, copy); |
| 332 |
Curl_safefree(copy); |
| 333 |
copy = strdup(buffer); /* clone the buffer, we don't use the libcurl |
| 334 |
aprintf() or similar since we want to use the |
| 335 |
same memory code as the "real" parse_filename |
| 336 |
function */ |
| 337 |
curl_free(tdir); |
| 338 |
} |
| 339 |
} |
| 340 |
#endif |
| 341 |
|
| 342 |
return copy; |
| 343 |
} |