tool_operhlp.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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 "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. /* Convert a CURLUcode into a CURLcode */
  65. CURLcode urlerr_cvt(CURLUcode ucode)
  66. {
  67. if(ucode == CURLUE_OUT_OF_MEMORY)
  68. return CURLE_OUT_OF_MEMORY;
  69. else if(ucode == CURLUE_UNSUPPORTED_SCHEME)
  70. return CURLE_UNSUPPORTED_PROTOCOL;
  71. else if(ucode == CURLUE_LACKS_IDN)
  72. return CURLE_NOT_BUILT_IN;
  73. else if(ucode == CURLUE_BAD_HANDLE)
  74. return CURLE_BAD_FUNCTION_ARGUMENT;
  75. return CURLE_URL_MALFORMAT;
  76. }
  77. /*
  78. * Adds the file name to the URL if it doesn't already have one.
  79. * url will be freed before return if the returned pointer is different
  80. */
  81. CURLcode add_file_name_to_url(CURL *curl, char **inurlp, const char *filename)
  82. {
  83. CURLcode result = CURLE_URL_MALFORMAT;
  84. CURLUcode uerr;
  85. CURLU *uh = curl_url();
  86. char *path = NULL;
  87. if(uh) {
  88. char *ptr;
  89. uerr = curl_url_set(uh, CURLUPART_URL, *inurlp,
  90. CURLU_GUESS_SCHEME|CURLU_NON_SUPPORT_SCHEME);
  91. if(uerr) {
  92. result = urlerr_cvt(uerr);
  93. goto fail;
  94. }
  95. uerr = curl_url_get(uh, CURLUPART_PATH, &path, 0);
  96. if(uerr) {
  97. result = urlerr_cvt(uerr);
  98. goto fail;
  99. }
  100. ptr = strrchr(path, '/');
  101. if(!ptr || !*++ptr) {
  102. /* The URL path has no file name part, add the local file name. In order
  103. to be able to do so, we have to create a new URL in another buffer.*/
  104. /* We only want the part of the local path that is on the right
  105. side of the rightmost slash and backslash. */
  106. const char *filep = strrchr(filename, '/');
  107. char *file2 = strrchr(filep?filep:filename, '\\');
  108. char *encfile;
  109. if(file2)
  110. filep = file2 + 1;
  111. else if(filep)
  112. filep++;
  113. else
  114. filep = filename;
  115. /* URL encode the file name */
  116. encfile = curl_easy_escape(curl, filep, 0 /* use strlen */);
  117. if(encfile) {
  118. char *newpath;
  119. char *newurl;
  120. if(ptr)
  121. /* there is a trailing slash on the path */
  122. newpath = aprintf("%s%s", path, encfile);
  123. else
  124. /* there is no trailing slash on the path */
  125. newpath = aprintf("%s/%s", path, encfile);
  126. curl_free(encfile);
  127. if(!newpath)
  128. goto fail;
  129. uerr = curl_url_set(uh, CURLUPART_PATH, newpath, 0);
  130. free(newpath);
  131. if(uerr) {
  132. result = urlerr_cvt(uerr);
  133. goto fail;
  134. }
  135. uerr = curl_url_get(uh, CURLUPART_URL, &newurl, CURLU_DEFAULT_SCHEME);
  136. if(uerr) {
  137. result = urlerr_cvt(uerr);
  138. goto fail;
  139. }
  140. free(*inurlp);
  141. *inurlp = newurl;
  142. result = CURLE_OK;
  143. }
  144. }
  145. else
  146. /* nothing to do */
  147. result = CURLE_OK;
  148. }
  149. fail:
  150. curl_url_cleanup(uh);
  151. curl_free(path);
  152. return result;
  153. }
  154. /* Extracts the name portion of the URL.
  155. * Returns a pointer to a heap-allocated string or NULL if
  156. * no name part, at location indicated by first argument.
  157. */
  158. CURLcode get_url_file_name(char **filename, const char *url)
  159. {
  160. const char *pc, *pc2;
  161. CURLU *uh = curl_url();
  162. char *path = NULL;
  163. CURLUcode uerr;
  164. if(!uh)
  165. return CURLE_OUT_OF_MEMORY;
  166. *filename = NULL;
  167. uerr = curl_url_set(uh, CURLUPART_URL, url, CURLU_GUESS_SCHEME);
  168. if(!uerr) {
  169. uerr = curl_url_get(uh, CURLUPART_PATH, &path, 0);
  170. if(!uerr) {
  171. curl_url_cleanup(uh);
  172. pc = strrchr(path, '/');
  173. pc2 = strrchr(pc ? pc + 1 : path, '\\');
  174. if(pc2)
  175. pc = pc2;
  176. if(pc)
  177. /* duplicate the string beyond the slash */
  178. pc++;
  179. else
  180. /* no slash => empty string */
  181. pc = "";
  182. *filename = strdup(pc);
  183. curl_free(path);
  184. if(!*filename)
  185. return CURLE_OUT_OF_MEMORY;
  186. #if defined(MSDOS) || defined(WIN32)
  187. {
  188. char *sanitized;
  189. SANITIZEcode sc = sanitize_file_name(&sanitized, *filename, 0);
  190. Curl_safefree(*filename);
  191. if(sc) {
  192. if(sc == SANITIZE_ERR_OUT_OF_MEMORY)
  193. return CURLE_OUT_OF_MEMORY;
  194. return CURLE_URL_MALFORMAT;
  195. }
  196. *filename = sanitized;
  197. }
  198. #endif /* MSDOS || WIN32 */
  199. /* in case we built debug enabled, we allow an environment variable
  200. * named CURL_TESTDIR to prefix the given file name to put it into a
  201. * specific directory
  202. */
  203. #ifdef DEBUGBUILD
  204. {
  205. char *tdir = curlx_getenv("CURL_TESTDIR");
  206. if(tdir) {
  207. char *alt = aprintf("%s/%s", tdir, *filename);
  208. Curl_safefree(*filename);
  209. *filename = alt;
  210. curl_free(tdir);
  211. if(!*filename)
  212. return CURLE_OUT_OF_MEMORY;
  213. }
  214. }
  215. #endif
  216. return CURLE_OK;
  217. }
  218. }
  219. curl_url_cleanup(uh);
  220. return urlerr_cvt(uerr);
  221. }