tool_cb_hdr.c 12 KB

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