comparison mupdf-source/thirdparty/curl/src/tool_operhlp.c @ 2:b50eed0cc0ef upstream

ADD: MuPDF v1.26.7: the MuPDF source as downloaded by a default build of PyMuPDF 1.26.4. The directory name has changed: no version number in the expanded directory now.
author Franz Glasner <fzglas.hg@dom66.de>
date Mon, 15 Sep 2025 11:43:07 +0200
parents
children
comparison
equal deleted inserted replaced
1:1d09e1dec1d9 2:b50eed0cc0ef
1 /***************************************************************************
2 * _ _ ____ _
3 * Project ___| | | | _ \| |
4 * / __| | | | |_) | |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
7 *
8 * Copyright (C) 1998 - 2019, 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
24 #include "strcase.h"
25
26 #define ENABLE_CURLX_PRINTF
27 /* use our own printf() functions */
28 #include "curlx.h"
29
30 #include "tool_cfgable.h"
31 #include "tool_convert.h"
32 #include "tool_doswin.h"
33 #include "tool_operhlp.h"
34 #include "tool_metalink.h"
35
36 #include "memdebug.h" /* keep this as LAST include */
37
38 void clean_getout(struct OperationConfig *config)
39 {
40 struct getout *next;
41 struct getout *node = config->url_list;
42
43 while(node) {
44 next = node->next;
45 Curl_safefree(node->url);
46 Curl_safefree(node->outfile);
47 Curl_safefree(node->infile);
48 Curl_safefree(node);
49 node = next;
50 }
51 config->url_list = NULL;
52 }
53
54 bool output_expected(const char *url, const char *uploadfile)
55 {
56 if(!uploadfile)
57 return TRUE; /* download */
58 if(checkprefix("http://", url) || checkprefix("https://", url))
59 return TRUE; /* HTTP(S) upload */
60
61 return FALSE; /* non-HTTP upload, probably no output should be expected */
62 }
63
64 bool stdin_upload(const char *uploadfile)
65 {
66 return (!strcmp(uploadfile, "-") ||
67 !strcmp(uploadfile, ".")) ? TRUE : FALSE;
68 }
69
70 /*
71 * Adds the file name to the URL if it doesn't already have one.
72 * url will be freed before return if the returned pointer is different
73 */
74 char *add_file_name_to_url(char *url, const char *filename)
75 {
76 /* If no file name part is given in the URL, we add this file name */
77 char *ptr = strstr(url, "://");
78 CURL *curl = curl_easy_init(); /* for url escaping */
79 if(!curl)
80 return NULL; /* error! */
81 if(ptr)
82 ptr += 3;
83 else
84 ptr = url;
85 ptr = strrchr(ptr, '/');
86 if(!ptr || !strlen(++ptr)) {
87 /* The URL has no file name part, add the local file name. In order
88 to be able to do so, we have to create a new URL in another
89 buffer.*/
90
91 /* We only want the part of the local path that is on the right
92 side of the rightmost slash and backslash. */
93 const char *filep = strrchr(filename, '/');
94 char *file2 = strrchr(filep?filep:filename, '\\');
95 char *encfile;
96
97 if(file2)
98 filep = file2 + 1;
99 else if(filep)
100 filep++;
101 else
102 filep = filename;
103
104 /* URL encode the file name */
105 encfile = curl_easy_escape(curl, filep, 0 /* use strlen */);
106 if(encfile) {
107 char *urlbuffer;
108 if(ptr)
109 /* there is a trailing slash on the URL */
110 urlbuffer = aprintf("%s%s", url, encfile);
111 else
112 /* there is no trailing slash on the URL */
113 urlbuffer = aprintf("%s/%s", url, encfile);
114
115 curl_free(encfile);
116 Curl_safefree(url);
117
118 if(!urlbuffer)
119 return NULL;
120
121 url = urlbuffer; /* use our new URL instead! */
122 }
123 else
124 Curl_safefree(url);
125 }
126 curl_easy_cleanup(curl);
127 return url;
128 }
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
138 *filename = NULL;
139
140 /* Find and get the remote file name */
141 pc = strstr(url, "://");
142 if(pc)
143 pc += 3;
144 else
145 pc = url;
146
147 pc2 = strrchr(pc, '\\');
148 pc = strrchr(pc, '/');
149 if(pc2 && (!pc || pc < pc2))
150 pc = pc2;
151
152 if(pc)
153 /* duplicate the string beyond the slash */
154 pc++;
155 else
156 /* no slash => empty string */
157 pc = "";
158
159 *filename = strdup(pc);
160 if(!*filename)
161 return CURLE_OUT_OF_MEMORY;
162
163 #if defined(MSDOS) || defined(WIN32)
164 {
165 char *sanitized;
166 SANITIZEcode sc = sanitize_file_name(&sanitized, *filename, 0);
167 Curl_safefree(*filename);
168 if(sc)
169 return CURLE_URL_MALFORMAT;
170 *filename = sanitized;
171 }
172 #endif /* MSDOS || WIN32 */
173
174 /* in case we built debug enabled, we allow an environment variable
175 * named CURL_TESTDIR to prefix the given file name to put it into a
176 * specific directory
177 */
178 #ifdef DEBUGBUILD
179 {
180 char *tdir = curlx_getenv("CURL_TESTDIR");
181 if(tdir) {
182 char buffer[512]; /* suitably large */
183 msnprintf(buffer, sizeof(buffer), "%s/%s", tdir, *filename);
184 Curl_safefree(*filename);
185 *filename = strdup(buffer); /* clone the buffer */
186 curl_free(tdir);
187 if(!*filename)
188 return CURLE_OUT_OF_MEMORY;
189 }
190 }
191 #endif
192
193 return CURLE_OK;
194 }