tool_cb_hdr.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2020, 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. ***************************************************************************/
  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. struct OutStruct *etag_save = &per->etag_save;
  55. const char *str = ptr;
  56. const size_t cb = size * nmemb;
  57. const char *end = (char *)ptr + cb;
  58. long protocol = 0;
  59. /*
  60. * Once that libcurl has called back tool_header_cb() the returned value
  61. * is checked against the amount that was intended to be written, if
  62. * it does not match then it fails with CURLE_WRITE_ERROR. So at this
  63. * point returning a value different from sz*nmemb indicates failure.
  64. */
  65. size_t failure = (size && nmemb) ? 0 : 1;
  66. if(!per->config)
  67. return failure;
  68. #ifdef DEBUGBUILD
  69. if(size * nmemb > (size_t)CURL_MAX_HTTP_HEADER) {
  70. warnf(per->config->global, "Header data exceeds single call write "
  71. "limit!\n");
  72. return failure;
  73. }
  74. #endif
  75. /*
  76. * Write header data when curl option --dump-header (-D) is given.
  77. */
  78. if(per->config->headerfile && heads->stream) {
  79. size_t rc = fwrite(ptr, size, nmemb, heads->stream);
  80. if(rc != cb)
  81. return rc;
  82. /* flush the stream to send off what we got earlier */
  83. (void)fflush(heads->stream);
  84. }
  85. /*
  86. * Write etag to file when --etag-save option is given.
  87. */
  88. if(per->config->etag_save_file && etag_save->stream) {
  89. /* match only header that start with etag (case insensitive) */
  90. if(curl_strnequal(str, "etag:", 5)) {
  91. const char *etag_h = &str[5];
  92. const char *eot = end - 1;
  93. if(*eot == '\n') {
  94. while(ISSPACE(*etag_h) && (etag_h < eot))
  95. etag_h++;
  96. while(ISSPACE(*eot))
  97. eot--;
  98. if(eot >= etag_h) {
  99. size_t etag_length = eot - etag_h + 1;
  100. fwrite(etag_h, size, etag_length, etag_save->stream);
  101. /* terminate with newline */
  102. fputc('\n', etag_save->stream);
  103. (void)fflush(etag_save->stream);
  104. }
  105. }
  106. }
  107. }
  108. /*
  109. * This callback sets the filename where output shall be written when
  110. * curl options --remote-name (-O) and --remote-header-name (-J) have
  111. * been simultaneously given and additionally server returns an HTTP
  112. * Content-Disposition header specifying a filename property.
  113. */
  114. curl_easy_getinfo(per->curl, CURLINFO_PROTOCOL, &protocol);
  115. if(hdrcbdata->honor_cd_filename &&
  116. (cb > 20) && checkprefix("Content-disposition:", str) &&
  117. (protocol & (CURLPROTO_HTTPS|CURLPROTO_HTTP))) {
  118. const char *p = str + 20;
  119. /* look for the 'filename=' parameter
  120. (encoded filenames (*=) are not supported) */
  121. for(;;) {
  122. char *filename;
  123. size_t len;
  124. while(*p && (p < end) && !ISALPHA(*p))
  125. p++;
  126. if(p > end - 9)
  127. break;
  128. if(memcmp(p, "filename=", 9)) {
  129. /* no match, find next parameter */
  130. while((p < end) && (*p != ';'))
  131. p++;
  132. continue;
  133. }
  134. p += 9;
  135. /* this expression below typecasts 'cb' only to avoid
  136. warning: signed and unsigned type in conditional expression
  137. */
  138. len = (ssize_t)cb - (p - str);
  139. filename = parse_filename(p, len);
  140. if(filename) {
  141. if(outs->stream) {
  142. /* indication of problem, get out! */
  143. free(filename);
  144. return failure;
  145. }
  146. outs->is_cd_filename = TRUE;
  147. outs->s_isreg = TRUE;
  148. outs->fopened = FALSE;
  149. outs->filename = filename;
  150. outs->alloc_filename = TRUE;
  151. hdrcbdata->honor_cd_filename = FALSE; /* done now! */
  152. if(!tool_create_output_file(outs, per->config))
  153. return failure;
  154. }
  155. break;
  156. }
  157. if(!outs->stream && !tool_create_output_file(outs, per->config))
  158. return failure;
  159. }
  160. if(hdrcbdata->config->writeout) {
  161. char *value = memchr(ptr, ':', cb);
  162. if(value) {
  163. if(per->was_last_header_empty)
  164. per->num_headers = 0;
  165. per->was_last_header_empty = FALSE;
  166. per->num_headers++;
  167. }
  168. else if(ptr[0] == '\r' || ptr[0] == '\n')
  169. per->was_last_header_empty = TRUE;
  170. }
  171. if(hdrcbdata->config->show_headers &&
  172. (protocol &
  173. (CURLPROTO_HTTP|CURLPROTO_HTTPS|CURLPROTO_RTSP|CURLPROTO_FILE))) {
  174. /* bold headers only for selected protocols */
  175. char *value = NULL;
  176. if(!outs->stream && !tool_create_output_file(outs, per->config))
  177. return failure;
  178. if(hdrcbdata->global->isatty && hdrcbdata->global->styled_output)
  179. value = memchr(ptr, ':', cb);
  180. if(value) {
  181. size_t namelen = value - ptr;
  182. fprintf(outs->stream, BOLD "%.*s" BOLDOFF ":", namelen, ptr);
  183. fwrite(&value[1], cb - namelen - 1, 1, outs->stream);
  184. }
  185. else
  186. /* not "handled", just show it */
  187. fwrite(ptr, cb, 1, outs->stream);
  188. }
  189. return cb;
  190. }
  191. /*
  192. * Copies a file name part and returns an ALLOCATED data buffer.
  193. */
  194. static char *parse_filename(const char *ptr, size_t len)
  195. {
  196. char *copy;
  197. char *p;
  198. char *q;
  199. char stop = '\0';
  200. /* simple implementation of strndup() */
  201. copy = malloc(len + 1);
  202. if(!copy)
  203. return NULL;
  204. memcpy(copy, ptr, len);
  205. copy[len] = '\0';
  206. p = copy;
  207. if(*p == '\'' || *p == '"') {
  208. /* store the starting quote */
  209. stop = *p;
  210. p++;
  211. }
  212. else
  213. stop = ';';
  214. /* scan for the end letter and stop there */
  215. q = strchr(p, stop);
  216. if(q)
  217. *q = '\0';
  218. /* if the filename contains a path, only use filename portion */
  219. q = strrchr(p, '/');
  220. if(q) {
  221. p = q + 1;
  222. if(!*p) {
  223. Curl_safefree(copy);
  224. return NULL;
  225. }
  226. }
  227. /* If the filename contains a backslash, only use filename portion. The idea
  228. is that even systems that don't handle backslashes as path separators
  229. probably want the path removed for convenience. */
  230. q = strrchr(p, '\\');
  231. if(q) {
  232. p = q + 1;
  233. if(!*p) {
  234. Curl_safefree(copy);
  235. return NULL;
  236. }
  237. }
  238. /* make sure the file name doesn't end in \r or \n */
  239. q = strchr(p, '\r');
  240. if(q)
  241. *q = '\0';
  242. q = strchr(p, '\n');
  243. if(q)
  244. *q = '\0';
  245. if(copy != p)
  246. memmove(copy, p, strlen(p) + 1);
  247. #if defined(MSDOS) || defined(WIN32)
  248. {
  249. char *sanitized;
  250. SANITIZEcode sc = sanitize_file_name(&sanitized, copy, 0);
  251. Curl_safefree(copy);
  252. if(sc)
  253. return NULL;
  254. copy = sanitized;
  255. }
  256. #endif /* MSDOS || WIN32 */
  257. /* in case we built debug enabled, we allow an environment variable
  258. * named CURL_TESTDIR to prefix the given file name to put it into a
  259. * specific directory
  260. */
  261. #ifdef DEBUGBUILD
  262. {
  263. char *tdir = curlx_getenv("CURL_TESTDIR");
  264. if(tdir) {
  265. char buffer[512]; /* suitably large */
  266. msnprintf(buffer, sizeof(buffer), "%s/%s", tdir, copy);
  267. Curl_safefree(copy);
  268. copy = strdup(buffer); /* clone the buffer, we don't use the libcurl
  269. aprintf() or similar since we want to use the
  270. same memory code as the "real" parse_filename
  271. function */
  272. curl_free(tdir);
  273. }
  274. }
  275. #endif
  276. return copy;
  277. }