tool_operhlp.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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. #include "tool_operate.h"
  26. #include "strcase.h"
  27. #define ENABLE_CURLX_PRINTF
  28. /* use our own printf() functions */
  29. #include "curlx.h"
  30. #include "tool_cfgable.h"
  31. #include "tool_doswin.h"
  32. #include "tool_operhlp.h"
  33. #include "memdebug.h" /* keep this as LAST include */
  34. void clean_getout(struct OperationConfig *config)
  35. {
  36. if(config) {
  37. struct getout *next;
  38. struct getout *node = config->url_list;
  39. while(node) {
  40. next = node->next;
  41. Curl_safefree(node->url);
  42. Curl_safefree(node->outfile);
  43. Curl_safefree(node->infile);
  44. Curl_safefree(node);
  45. node = next;
  46. }
  47. config->url_list = NULL;
  48. }
  49. single_transfer_cleanup(config);
  50. }
  51. bool output_expected(const char *url, const char *uploadfile)
  52. {
  53. if(!uploadfile)
  54. return TRUE; /* download */
  55. if(checkprefix("http://", url) || checkprefix("https://", url))
  56. return TRUE; /* HTTP(S) upload */
  57. return FALSE; /* non-HTTP upload, probably no output should be expected */
  58. }
  59. bool stdin_upload(const char *uploadfile)
  60. {
  61. return (!strcmp(uploadfile, "-") ||
  62. !strcmp(uploadfile, ".")) ? TRUE : FALSE;
  63. }
  64. /*
  65. * Adds the file name to the URL if it doesn't already have one.
  66. * url will be freed before return if the returned pointer is different
  67. */
  68. CURLcode add_file_name_to_url(CURL *curl, char **inurlp, const char *filename)
  69. {
  70. CURLcode result = CURLE_OUT_OF_MEMORY;
  71. CURLU *uh = curl_url();
  72. char *path = NULL;
  73. if(uh) {
  74. char *ptr;
  75. if(curl_url_set(uh, CURLUPART_URL, *inurlp,
  76. CURLU_GUESS_SCHEME|CURLU_NON_SUPPORT_SCHEME))
  77. goto fail;
  78. if(curl_url_get(uh, CURLUPART_PATH, &path, 0))
  79. goto fail;
  80. ptr = strrchr(path, '/');
  81. if(!ptr || !*++ptr) {
  82. /* The URL path has no file name part, add the local file name. In order
  83. to be able to do so, we have to create a new URL in another buffer.*/
  84. /* We only want the part of the local path that is on the right
  85. side of the rightmost slash and backslash. */
  86. const char *filep = strrchr(filename, '/');
  87. char *file2 = strrchr(filep?filep:filename, '\\');
  88. char *encfile;
  89. if(file2)
  90. filep = file2 + 1;
  91. else if(filep)
  92. filep++;
  93. else
  94. filep = filename;
  95. /* URL encode the file name */
  96. encfile = curl_easy_escape(curl, filep, 0 /* use strlen */);
  97. if(encfile) {
  98. char *newpath;
  99. char *newurl;
  100. CURLUcode uerr;
  101. if(ptr)
  102. /* there is a trailing slash on the path */
  103. newpath = aprintf("%s%s", path, encfile);
  104. else
  105. /* there is no trailing slash on the path */
  106. newpath = aprintf("%s/%s", path, encfile);
  107. curl_free(encfile);
  108. if(!newpath)
  109. goto fail;
  110. uerr = curl_url_set(uh, CURLUPART_PATH, newpath, 0);
  111. free(newpath);
  112. if(uerr)
  113. goto fail;
  114. if(curl_url_get(uh, CURLUPART_URL, &newurl, CURLU_DEFAULT_SCHEME))
  115. goto fail;
  116. free(*inurlp);
  117. *inurlp = newurl;
  118. result = CURLE_OK;
  119. }
  120. }
  121. else
  122. /* nothing to do */
  123. result = CURLE_OK;
  124. }
  125. fail:
  126. curl_url_cleanup(uh);
  127. curl_free(path);
  128. return result;
  129. }
  130. /* Extracts the name portion of the URL.
  131. * Returns a pointer to a heap-allocated string or NULL if
  132. * no name part, at location indicated by first argument.
  133. */
  134. CURLcode get_url_file_name(char **filename, const char *url)
  135. {
  136. const char *pc, *pc2;
  137. CURLU *uh = curl_url();
  138. char *path = NULL;
  139. if(!uh)
  140. return CURLE_OUT_OF_MEMORY;
  141. *filename = NULL;
  142. if(!curl_url_set(uh, CURLUPART_URL, url, CURLU_GUESS_SCHEME) &&
  143. !curl_url_get(uh, CURLUPART_PATH, &path, 0)) {
  144. curl_url_cleanup(uh);
  145. pc = strrchr(path, '/');
  146. pc2 = strrchr(pc ? pc + 1 : path, '\\');
  147. if(pc2)
  148. pc = pc2;
  149. if(pc)
  150. /* duplicate the string beyond the slash */
  151. pc++;
  152. else
  153. /* no slash => empty string */
  154. pc = "";
  155. *filename = strdup(pc);
  156. curl_free(path);
  157. if(!*filename)
  158. return CURLE_OUT_OF_MEMORY;
  159. #if defined(MSDOS) || defined(WIN32)
  160. {
  161. char *sanitized;
  162. SANITIZEcode sc = sanitize_file_name(&sanitized, *filename, 0);
  163. Curl_safefree(*filename);
  164. if(sc)
  165. return CURLE_URL_MALFORMAT;
  166. *filename = sanitized;
  167. }
  168. #endif /* MSDOS || WIN32 */
  169. /* in case we built debug enabled, we allow an environment variable
  170. * named CURL_TESTDIR to prefix the given file name to put it into a
  171. * specific directory
  172. */
  173. #ifdef DEBUGBUILD
  174. {
  175. char *tdir = curlx_getenv("CURL_TESTDIR");
  176. if(tdir) {
  177. char *alt = aprintf("%s/%s", tdir, *filename);
  178. Curl_safefree(*filename);
  179. *filename = alt;
  180. curl_free(tdir);
  181. if(!*filename)
  182. return CURLE_OUT_OF_MEMORY;
  183. }
  184. }
  185. #endif
  186. return CURLE_OK;
  187. }
  188. curl_url_cleanup(uh);
  189. return CURLE_URL_MALFORMAT;
  190. }