summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorDaniel Stenberg <daniel@haxx.se>2022-10-10 09:15:18 +0200
committerDaniel Stenberg <daniel@haxx.se>2022-10-10 15:46:05 +0200
commitb82eb72d8097c93313329a4aecd41317a293f506 (patch)
tree0e4c9cb26ed68d6b9643485af32718b9622cbcb0 /src
parent0554de58c662096150105a301e0cad2a44dab37f (diff)
downloadcurl-b82eb72d8097c93313329a4aecd41317a293f506.tar.gz
single_transfer: use the libcurl URL parser when appending query parts
Instead of doing "manual" error-prone parsing in another place. Used when --data contents is added to the URL query when -G is provided. Closes #9681
Diffstat (limited to 'src')
-rw-r--r--src/tool_operate.c51
1 files changed, 15 insertions, 36 deletions
diff --git a/src/tool_operate.c b/src/tool_operate.c
index a54140016..d45c3315e 100644
--- a/src/tool_operate.c
+++ b/src/tool_operate.c
@@ -1191,43 +1191,22 @@ static CURLcode single_transfer(struct GlobalConfig *global,
}
if(httpgetfields) {
- char *urlbuffer;
- /* Find out whether the url contains a file name */
- const char *pc = strstr(per->this_url, "://");
- char sep = '?';
- if(pc)
- pc += 3;
- else
- pc = per->this_url;
-
- pc = strrchr(pc, '/'); /* check for a slash */
-
- if(pc) {
- /* there is a slash present in the URL */
-
- if(strchr(pc, '?'))
- /* Ouch, there's already a question mark in the URL string, we
- then append the data with an ampersand separator instead! */
- sep = '&';
- }
- /*
- * Then append ? followed by the get fields to the url.
- */
- if(pc)
- urlbuffer = aprintf("%s%c%s", per->this_url, sep, httpgetfields);
- else
- /* Append / before the ? to create a well-formed url
- if the url contains a hostname only
- */
- urlbuffer = aprintf("%s/?%s", per->this_url, httpgetfields);
-
- if(!urlbuffer) {
- result = CURLE_OUT_OF_MEMORY;
- break;
+ CURLU *uh = curl_url();
+ if(uh) {
+ char *updated;
+ if(curl_url_set(uh, CURLUPART_URL, per->this_url,
+ CURLU_GUESS_SCHEME) ||
+ curl_url_set(uh, CURLUPART_QUERY, httpgetfields,
+ CURLU_APPENDQUERY) ||
+ curl_url_get(uh, CURLUPART_URL, &updated, CURLU_GUESS_SCHEME)) {
+ curl_url_cleanup(uh);
+ result = CURLE_OUT_OF_MEMORY;
+ break;
+ }
+ Curl_safefree(per->this_url); /* free previous URL */
+ per->this_url = updated; /* use our new URL instead! */
+ curl_url_cleanup(uh);
}
-
- Curl_safefree(per->this_url); /* free previous URL */
- per->this_url = urlbuffer; /* use our new URL instead! */
}
if(!global->errors)