tool_cb_hdr.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2019, 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. #include "strcase.h"
  24. #define ENABLE_CURLX_PRINTF
  25. /* use our own printf() functions */
  26. #include "curlx.h"
  27. #include "tool_cfgable.h"
  28. #include "tool_doswin.h"
  29. #include "tool_msgs.h"
  30. #include "tool_cb_hdr.h"
  31. #include "tool_cb_wrt.h"
  32. #include "tool_operate.h"
  33. #include "memdebug.h" /* keep this as LAST include */
  34. static char *parse_filename(const char *ptr, size_t len);
  35. #ifdef WIN32
  36. #define BOLD
  37. #define BOLDOFF
  38. #else
  39. #define BOLD "\x1b[1m"
  40. /* Switch off bold by setting "all attributes off" since the explicit
  41. bold-off code (21) isn't supported everywhere - like in the mac
  42. Terminal. */
  43. #define BOLDOFF "\x1b[0m"
  44. #endif
  45. /*
  46. ** callback for CURLOPT_HEADERFUNCTION
  47. */
  48. size_t tool_header_cb(char *ptr, size_t size, size_t nmemb, void *userdata)
  49. {
  50. struct per_transfer *per = userdata;
  51. struct HdrCbData *hdrcbdata = &per->hdrcbdata;
  52. struct OutStruct *outs = &per->outs;
  53. struct OutStruct *heads = &per->heads;
  54. const char *str = ptr;
  55. const size_t cb = size * nmemb;
  56. const char *end = (char *)ptr + cb;
  57. long protocol = 0;
  58. /*
  59. * Once that libcurl has called back tool_header_cb() the returned value
  60. * is checked against the amount that was intended to be written, if
  61. * it does not match then it fails with CURLE_WRITE_ERROR. So at this
  62. * point returning a value different from sz*nmemb indicates failure.
  63. */
  64. size_t failure = (size && nmemb) ? 0 : 1;
  65. if(!heads->config)
  66. return failure;
  67. #ifdef DEBUGBUILD
  68. if(size * nmemb > (size_t)CURL_MAX_HTTP_HEADER) {
  69. warnf(heads->config->global, "Header data exceeds single call write "
  70. "limit!\n");
  71. return failure;
  72. }
  73. #endif
  74. /*
  75. * Write header data when curl option --dump-header (-D) is given.
  76. */
  77. if(heads->config->headerfile && heads->stream) {
  78. size_t rc = fwrite(ptr, size, nmemb, heads->stream);
  79. if(rc != cb)
  80. return rc;
  81. /* flush the stream to send off what we got earlier */
  82. (void)fflush(heads->stream);
  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. curl_easy_getinfo(per->curl, CURLINFO_PROTOCOL, &protocol);
  91. if(hdrcbdata->honor_cd_filename &&
  92. (cb > 20) && checkprefix("Content-disposition:", str) &&
  93. (protocol & (CURLPROTO_HTTPS|CURLPROTO_HTTP))) {
  94. const char *p = str + 20;
  95. /* look for the 'filename=' parameter
  96. (encoded filenames (*=) are not supported) */
  97. for(;;) {
  98. char *filename;
  99. size_t len;
  100. while(*p && (p < end) && !ISALPHA(*p))
  101. p++;
  102. if(p > end - 9)
  103. break;
  104. if(memcmp(p, "filename=", 9)) {
  105. /* no match, find next parameter */
  106. while((p < end) && (*p != ';'))
  107. p++;
  108. continue;
  109. }
  110. p += 9;
  111. /* this expression below typecasts 'cb' only to avoid
  112. warning: signed and unsigned type in conditional expression
  113. */
  114. len = (ssize_t)cb - (p - str);
  115. filename = parse_filename(p, len);
  116. if(filename) {
  117. if(outs->stream) {
  118. int rc;
  119. /* already opened and possibly written to */
  120. if(outs->fopened)
  121. fclose(outs->stream);
  122. outs->stream = NULL;
  123. /* rename the initial file name to the new file name */
  124. rc = rename(outs->filename, filename);
  125. if(rc != 0) {
  126. warnf(outs->config->global, "Failed to rename %s -> %s: %s\n",
  127. outs->filename, filename, strerror(errno));
  128. }
  129. if(outs->alloc_filename)
  130. Curl_safefree(outs->filename);
  131. if(rc != 0) {
  132. free(filename);
  133. return failure;
  134. }
  135. }
  136. outs->is_cd_filename = TRUE;
  137. outs->s_isreg = TRUE;
  138. outs->fopened = FALSE;
  139. outs->filename = filename;
  140. outs->alloc_filename = TRUE;
  141. hdrcbdata->honor_cd_filename = FALSE; /* done now! */
  142. if(!tool_create_output_file(outs))
  143. return failure;
  144. }
  145. break;
  146. }
  147. if(!outs->stream && !tool_create_output_file(outs))
  148. return failure;
  149. }
  150. if(hdrcbdata->config->show_headers &&
  151. (protocol &
  152. (CURLPROTO_HTTP|CURLPROTO_HTTPS|CURLPROTO_RTSP|CURLPROTO_FILE))) {
  153. /* bold headers only for selected protocols */
  154. char *value = NULL;
  155. if(!outs->stream && !tool_create_output_file(outs))
  156. return failure;
  157. if(hdrcbdata->global->isatty && hdrcbdata->global->styled_output)
  158. value = memchr(ptr, ':', cb);
  159. if(value) {
  160. size_t namelen = value - ptr;
  161. fprintf(outs->stream, BOLD "%.*s" BOLDOFF ":", namelen, ptr);
  162. fwrite(&value[1], cb - namelen - 1, 1, outs->stream);
  163. }
  164. else
  165. /* not "handled", just show it */
  166. fwrite(ptr, cb, 1, outs->stream);
  167. }
  168. return cb;
  169. }
  170. /*
  171. * Copies a file name part and returns an ALLOCATED data buffer.
  172. */
  173. static char *parse_filename(const char *ptr, size_t len)
  174. {
  175. char *copy;
  176. char *p;
  177. char *q;
  178. char stop = '\0';
  179. /* simple implementation of strndup() */
  180. copy = malloc(len + 1);
  181. if(!copy)
  182. return NULL;
  183. memcpy(copy, ptr, len);
  184. copy[len] = '\0';
  185. p = copy;
  186. if(*p == '\'' || *p == '"') {
  187. /* store the starting quote */
  188. stop = *p;
  189. p++;
  190. }
  191. else
  192. stop = ';';
  193. /* scan for the end letter and stop there */
  194. q = strchr(p, stop);
  195. if(q)
  196. *q = '\0';
  197. /* if the filename contains a path, only use filename portion */
  198. q = strrchr(p, '/');
  199. if(q) {
  200. p = q + 1;
  201. if(!*p) {
  202. Curl_safefree(copy);
  203. return NULL;
  204. }
  205. }
  206. /* If the filename contains a backslash, only use filename portion. The idea
  207. is that even systems that don't handle backslashes as path separators
  208. probably want the path removed for convenience. */
  209. q = strrchr(p, '\\');
  210. if(q) {
  211. p = q + 1;
  212. if(!*p) {
  213. Curl_safefree(copy);
  214. return NULL;
  215. }
  216. }
  217. /* make sure the file name doesn't end in \r or \n */
  218. q = strchr(p, '\r');
  219. if(q)
  220. *q = '\0';
  221. q = strchr(p, '\n');
  222. if(q)
  223. *q = '\0';
  224. if(copy != p)
  225. memmove(copy, p, strlen(p) + 1);
  226. #if defined(MSDOS) || defined(WIN32)
  227. {
  228. char *sanitized;
  229. SANITIZEcode sc = sanitize_file_name(&sanitized, copy, 0);
  230. Curl_safefree(copy);
  231. if(sc)
  232. return NULL;
  233. copy = sanitized;
  234. }
  235. #endif /* MSDOS || WIN32 */
  236. /* in case we built debug enabled, we allow an environment variable
  237. * named CURL_TESTDIR to prefix the given file name to put it into a
  238. * specific directory
  239. */
  240. #ifdef DEBUGBUILD
  241. {
  242. char *tdir = curlx_getenv("CURL_TESTDIR");
  243. if(tdir) {
  244. char buffer[512]; /* suitably large */
  245. msnprintf(buffer, sizeof(buffer), "%s/%s", tdir, copy);
  246. Curl_safefree(copy);
  247. copy = strdup(buffer); /* clone the buffer, we don't use the libcurl
  248. aprintf() or similar since we want to use the
  249. same memory code as the "real" parse_filename
  250. function */
  251. curl_free(tdir);
  252. }
  253. }
  254. #endif
  255. return copy;
  256. }