tool_operhlp.c 5.2 KB

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