tool_operhlp.c 6.9 KB

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