xf.li | bfc6e71 | 2025-02-07 01:54:34 -0800 | [diff] [blame^] | 1 | /*************************************************************************** |
| 2 | * _ _ ____ _ |
| 3 | * Project ___| | | | _ \| | |
| 4 | * / __| | | | |_) | | |
| 5 | * | (__| |_| | _ <| |___ |
| 6 | * \___|\___/|_| \_\_____| |
| 7 | * |
| 8 | * Copyright (C) 1998 - 2022, 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.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 | * SPDX-License-Identifier: curl |
| 22 | * |
| 23 | ***************************************************************************/ |
| 24 | #include "tool_setup.h" |
| 25 | |
| 26 | #include "strcase.h" |
| 27 | |
| 28 | #define ENABLE_CURLX_PRINTF |
| 29 | /* use our own printf() functions */ |
| 30 | #include "curlx.h" |
| 31 | |
| 32 | #include "tool_cfgable.h" |
| 33 | #include "tool_doswin.h" |
| 34 | #include "tool_msgs.h" |
| 35 | #include "tool_cb_hdr.h" |
| 36 | #include "tool_cb_wrt.h" |
| 37 | #include "tool_operate.h" |
| 38 | #include "tool_libinfo.h" |
| 39 | |
| 40 | #include "memdebug.h" /* keep this as LAST include */ |
| 41 | |
| 42 | static char *parse_filename(const char *ptr, size_t len); |
| 43 | |
| 44 | #ifdef WIN32 |
| 45 | #define BOLD |
| 46 | #define BOLDOFF |
| 47 | #else |
| 48 | #define BOLD "\x1b[1m" |
| 49 | /* Switch off bold by setting "all attributes off" since the explicit |
| 50 | bold-off code (21) isn't supported everywhere - like in the mac |
| 51 | Terminal. */ |
| 52 | #define BOLDOFF "\x1b[0m" |
| 53 | /* OSC 8 hyperlink escape sequence */ |
| 54 | #define LINK "\x1b]8;;" |
| 55 | #define LINKST "\x1b\\" |
| 56 | #define LINKOFF LINK LINKST |
| 57 | #endif |
| 58 | |
| 59 | #ifdef LINK |
| 60 | static void write_linked_location(CURL *curl, const char *location, |
| 61 | size_t loclen, FILE *stream); |
| 62 | #endif |
| 63 | |
| 64 | /* |
| 65 | ** callback for CURLOPT_HEADERFUNCTION |
| 66 | */ |
| 67 | |
| 68 | size_t tool_header_cb(char *ptr, size_t size, size_t nmemb, void *userdata) |
| 69 | { |
| 70 | struct per_transfer *per = userdata; |
| 71 | struct HdrCbData *hdrcbdata = &per->hdrcbdata; |
| 72 | struct OutStruct *outs = &per->outs; |
| 73 | struct OutStruct *heads = &per->heads; |
| 74 | struct OutStruct *etag_save = &per->etag_save; |
| 75 | const char *str = ptr; |
| 76 | const size_t cb = size * nmemb; |
| 77 | const char *end = (char *)ptr + cb; |
| 78 | const char *scheme = NULL; |
| 79 | |
| 80 | /* |
| 81 | * Once that libcurl has called back tool_header_cb() the returned value |
| 82 | * is checked against the amount that was intended to be written, if |
| 83 | * it does not match then it fails with CURLE_WRITE_ERROR. So at this |
| 84 | * point returning a value different from sz*nmemb indicates failure. |
| 85 | */ |
| 86 | size_t failure = (size && nmemb) ? 0 : 1; |
| 87 | |
| 88 | if(!per->config) |
| 89 | return failure; |
| 90 | |
| 91 | #ifdef DEBUGBUILD |
| 92 | if(size * nmemb > (size_t)CURL_MAX_HTTP_HEADER) { |
| 93 | warnf(per->config->global, "Header data exceeds single call write " |
| 94 | "limit!\n"); |
| 95 | return failure; |
| 96 | } |
| 97 | #endif |
| 98 | |
| 99 | /* |
| 100 | * Write header data when curl option --dump-header (-D) is given. |
| 101 | */ |
| 102 | |
| 103 | if(per->config->headerfile && heads->stream) { |
| 104 | size_t rc = fwrite(ptr, size, nmemb, heads->stream); |
| 105 | if(rc != cb) |
| 106 | return rc; |
| 107 | /* flush the stream to send off what we got earlier */ |
| 108 | (void)fflush(heads->stream); |
| 109 | } |
| 110 | |
| 111 | /* |
| 112 | * Write etag to file when --etag-save option is given. |
| 113 | */ |
| 114 | if(per->config->etag_save_file && etag_save->stream) { |
| 115 | /* match only header that start with etag (case insensitive) */ |
| 116 | if(curl_strnequal(str, "etag:", 5)) { |
| 117 | const char *etag_h = &str[5]; |
| 118 | const char *eot = end - 1; |
| 119 | if(*eot == '\n') { |
| 120 | while(ISBLANK(*etag_h) && (etag_h < eot)) |
| 121 | etag_h++; |
| 122 | while(ISSPACE(*eot)) |
| 123 | eot--; |
| 124 | |
| 125 | if(eot >= etag_h) { |
| 126 | size_t etag_length = eot - etag_h + 1; |
| 127 | fwrite(etag_h, size, etag_length, etag_save->stream); |
| 128 | /* terminate with newline */ |
| 129 | fputc('\n', etag_save->stream); |
| 130 | (void)fflush(etag_save->stream); |
| 131 | } |
| 132 | } |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | /* |
| 137 | * This callback sets the filename where output shall be written when |
| 138 | * curl options --remote-name (-O) and --remote-header-name (-J) have |
| 139 | * been simultaneously given and additionally server returns an HTTP |
| 140 | * Content-Disposition header specifying a filename property. |
| 141 | */ |
| 142 | |
| 143 | curl_easy_getinfo(per->curl, CURLINFO_SCHEME, &scheme); |
| 144 | scheme = proto_token(scheme); |
| 145 | if(hdrcbdata->honor_cd_filename && |
| 146 | (cb > 20) && checkprefix("Content-disposition:", str) && |
| 147 | (scheme == proto_http || scheme == proto_https)) { |
| 148 | const char *p = str + 20; |
| 149 | |
| 150 | /* look for the 'filename=' parameter |
| 151 | (encoded filenames (*=) are not supported) */ |
| 152 | for(;;) { |
| 153 | char *filename; |
| 154 | size_t len; |
| 155 | |
| 156 | while(*p && (p < end) && !ISALPHA(*p)) |
| 157 | p++; |
| 158 | if(p > end - 9) |
| 159 | break; |
| 160 | |
| 161 | if(memcmp(p, "filename=", 9)) { |
| 162 | /* no match, find next parameter */ |
| 163 | while((p < end) && (*p != ';')) |
| 164 | p++; |
| 165 | continue; |
| 166 | } |
| 167 | p += 9; |
| 168 | |
| 169 | /* this expression below typecasts 'cb' only to avoid |
| 170 | warning: signed and unsigned type in conditional expression |
| 171 | */ |
| 172 | len = (ssize_t)cb - (p - str); |
| 173 | filename = parse_filename(p, len); |
| 174 | if(filename) { |
| 175 | if(outs->stream) { |
| 176 | /* indication of problem, get out! */ |
| 177 | free(filename); |
| 178 | return failure; |
| 179 | } |
| 180 | |
| 181 | outs->is_cd_filename = TRUE; |
| 182 | outs->s_isreg = TRUE; |
| 183 | outs->fopened = FALSE; |
| 184 | outs->filename = filename; |
| 185 | outs->alloc_filename = TRUE; |
| 186 | hdrcbdata->honor_cd_filename = FALSE; /* done now! */ |
| 187 | if(!tool_create_output_file(outs, per->config)) |
| 188 | return failure; |
| 189 | } |
| 190 | break; |
| 191 | } |
| 192 | if(!outs->stream && !tool_create_output_file(outs, per->config)) |
| 193 | return failure; |
| 194 | } |
| 195 | if(hdrcbdata->config->writeout) { |
| 196 | char *value = memchr(ptr, ':', cb); |
| 197 | if(value) { |
| 198 | if(per->was_last_header_empty) |
| 199 | per->num_headers = 0; |
| 200 | per->was_last_header_empty = FALSE; |
| 201 | per->num_headers++; |
| 202 | } |
| 203 | else if(ptr[0] == '\r' || ptr[0] == '\n') |
| 204 | per->was_last_header_empty = TRUE; |
| 205 | } |
| 206 | if(hdrcbdata->config->show_headers && |
| 207 | (scheme == proto_http || scheme == proto_https || |
| 208 | scheme == proto_rtsp || scheme == proto_file)) { |
| 209 | /* bold headers only for selected protocols */ |
| 210 | char *value = NULL; |
| 211 | |
| 212 | if(!outs->stream && !tool_create_output_file(outs, per->config)) |
| 213 | return failure; |
| 214 | |
| 215 | if(hdrcbdata->global->isatty && hdrcbdata->global->styled_output) |
| 216 | value = memchr(ptr, ':', cb); |
| 217 | if(value) { |
| 218 | size_t namelen = value - ptr; |
| 219 | fprintf(outs->stream, BOLD "%.*s" BOLDOFF ":", namelen, ptr); |
| 220 | #ifndef LINK |
| 221 | fwrite(&value[1], cb - namelen - 1, 1, outs->stream); |
| 222 | #else |
| 223 | if(curl_strnequal("Location", ptr, namelen)) { |
| 224 | write_linked_location(per->curl, &value[1], cb - namelen - 1, |
| 225 | outs->stream); |
| 226 | } |
| 227 | else |
| 228 | fwrite(&value[1], cb - namelen - 1, 1, outs->stream); |
| 229 | #endif |
| 230 | } |
| 231 | else |
| 232 | /* not "handled", just show it */ |
| 233 | fwrite(ptr, cb, 1, outs->stream); |
| 234 | } |
| 235 | return cb; |
| 236 | } |
| 237 | |
| 238 | /* |
| 239 | * Copies a file name part and returns an ALLOCATED data buffer. |
| 240 | */ |
| 241 | static char *parse_filename(const char *ptr, size_t len) |
| 242 | { |
| 243 | char *copy; |
| 244 | char *p; |
| 245 | char *q; |
| 246 | char stop = '\0'; |
| 247 | |
| 248 | /* simple implementation of strndup() */ |
| 249 | copy = malloc(len + 1); |
| 250 | if(!copy) |
| 251 | return NULL; |
| 252 | memcpy(copy, ptr, len); |
| 253 | copy[len] = '\0'; |
| 254 | |
| 255 | p = copy; |
| 256 | if(*p == '\'' || *p == '"') { |
| 257 | /* store the starting quote */ |
| 258 | stop = *p; |
| 259 | p++; |
| 260 | } |
| 261 | else |
| 262 | stop = ';'; |
| 263 | |
| 264 | /* scan for the end letter and stop there */ |
| 265 | q = strchr(p, stop); |
| 266 | if(q) |
| 267 | *q = '\0'; |
| 268 | |
| 269 | /* if the filename contains a path, only use filename portion */ |
| 270 | q = strrchr(p, '/'); |
| 271 | if(q) { |
| 272 | p = q + 1; |
| 273 | if(!*p) { |
| 274 | Curl_safefree(copy); |
| 275 | return NULL; |
| 276 | } |
| 277 | } |
| 278 | |
| 279 | /* If the filename contains a backslash, only use filename portion. The idea |
| 280 | is that even systems that don't handle backslashes as path separators |
| 281 | probably want the path removed for convenience. */ |
| 282 | q = strrchr(p, '\\'); |
| 283 | if(q) { |
| 284 | p = q + 1; |
| 285 | if(!*p) { |
| 286 | Curl_safefree(copy); |
| 287 | return NULL; |
| 288 | } |
| 289 | } |
| 290 | |
| 291 | /* make sure the file name doesn't end in \r or \n */ |
| 292 | q = strchr(p, '\r'); |
| 293 | if(q) |
| 294 | *q = '\0'; |
| 295 | |
| 296 | q = strchr(p, '\n'); |
| 297 | if(q) |
| 298 | *q = '\0'; |
| 299 | |
| 300 | if(copy != p) |
| 301 | memmove(copy, p, strlen(p) + 1); |
| 302 | |
| 303 | #if defined(MSDOS) || defined(WIN32) |
| 304 | { |
| 305 | char *sanitized; |
| 306 | SANITIZEcode sc = sanitize_file_name(&sanitized, copy, 0); |
| 307 | Curl_safefree(copy); |
| 308 | if(sc) |
| 309 | return NULL; |
| 310 | copy = sanitized; |
| 311 | } |
| 312 | #endif /* MSDOS || WIN32 */ |
| 313 | |
| 314 | /* in case we built debug enabled, we allow an environment variable |
| 315 | * named CURL_TESTDIR to prefix the given file name to put it into a |
| 316 | * specific directory |
| 317 | */ |
| 318 | #ifdef DEBUGBUILD |
| 319 | { |
| 320 | char *tdir = curlx_getenv("CURL_TESTDIR"); |
| 321 | if(tdir) { |
| 322 | char buffer[512]; /* suitably large */ |
| 323 | msnprintf(buffer, sizeof(buffer), "%s/%s", tdir, copy); |
| 324 | Curl_safefree(copy); |
| 325 | copy = strdup(buffer); /* clone the buffer, we don't use the libcurl |
| 326 | aprintf() or similar since we want to use the |
| 327 | same memory code as the "real" parse_filename |
| 328 | function */ |
| 329 | curl_free(tdir); |
| 330 | } |
| 331 | } |
| 332 | #endif |
| 333 | |
| 334 | return copy; |
| 335 | } |
| 336 | |
| 337 | #ifdef LINK |
| 338 | /* |
| 339 | * Treat the Location: header specially, by writing a special escape |
| 340 | * sequence that adds a hyperlink to the displayed text. This makes |
| 341 | * the absolute URL of the redirect clickable in supported terminals, |
| 342 | * which couldn't happen otherwise for relative URLs. The Location: |
| 343 | * header is supposed to always be absolute so this theoretically |
| 344 | * shouldn't be needed but the real world returns plenty of relative |
| 345 | * URLs here. |
| 346 | */ |
| 347 | static |
| 348 | void write_linked_location(CURL *curl, const char *location, size_t loclen, |
| 349 | FILE *stream) { |
| 350 | /* This would so simple if CURLINFO_REDIRECT_URL were available here */ |
| 351 | CURLU *u = NULL; |
| 352 | char *copyloc = NULL, *locurl = NULL, *scheme = NULL, *finalurl = NULL; |
| 353 | const char *loc = location; |
| 354 | size_t llen = loclen; |
| 355 | |
| 356 | /* Strip leading whitespace of the redirect URL */ |
| 357 | while(llen && *loc == ' ') { |
| 358 | ++loc; |
| 359 | --llen; |
| 360 | } |
| 361 | |
| 362 | /* Strip the trailing end-of-line characters, normally "\r\n" */ |
| 363 | while(llen && (loc[llen-1] == '\n' || loc[llen-1] == '\r')) |
| 364 | --llen; |
| 365 | |
| 366 | /* CURLU makes it easy to handle the relative URL case */ |
| 367 | u = curl_url(); |
| 368 | if(!u) |
| 369 | goto locout; |
| 370 | |
| 371 | /* Create a NUL-terminated and whitespace-stripped copy of Location: */ |
| 372 | copyloc = malloc(llen + 1); |
| 373 | if(!copyloc) |
| 374 | goto locout; |
| 375 | memcpy(copyloc, loc, llen); |
| 376 | copyloc[llen] = 0; |
| 377 | |
| 378 | /* The original URL to use as a base for a relative redirect URL */ |
| 379 | if(curl_easy_getinfo(curl, CURLINFO_EFFECTIVE_URL, &locurl)) |
| 380 | goto locout; |
| 381 | if(curl_url_set(u, CURLUPART_URL, locurl, 0)) |
| 382 | goto locout; |
| 383 | |
| 384 | /* Redirected location. This can be either absolute or relative. */ |
| 385 | if(curl_url_set(u, CURLUPART_URL, copyloc, 0)) |
| 386 | goto locout; |
| 387 | |
| 388 | if(curl_url_get(u, CURLUPART_URL, &finalurl, CURLU_NO_DEFAULT_PORT)) |
| 389 | goto locout; |
| 390 | |
| 391 | if(curl_url_get(u, CURLUPART_SCHEME, &scheme, 0)) |
| 392 | goto locout; |
| 393 | |
| 394 | if(!strcmp("http", scheme) || |
| 395 | !strcmp("https", scheme) || |
| 396 | !strcmp("ftp", scheme) || |
| 397 | !strcmp("ftps", scheme)) { |
| 398 | fprintf(stream, LINK "%s" LINKST "%.*s" LINKOFF, |
| 399 | finalurl, loclen, location); |
| 400 | goto locdone; |
| 401 | } |
| 402 | |
| 403 | /* Not a "safe" URL: don't linkify it */ |
| 404 | |
| 405 | locout: |
| 406 | /* Write the normal output in case of error or unsafe */ |
| 407 | fwrite(location, loclen, 1, stream); |
| 408 | |
| 409 | locdone: |
| 410 | if(u) { |
| 411 | curl_free(finalurl); |
| 412 | curl_free(scheme); |
| 413 | curl_url_cleanup(u); |
| 414 | free(copyloc); |
| 415 | } |
| 416 | } |
| 417 | #endif |