Skip to content

Commit

Permalink
trurl: only append the first iterate loop
Browse files Browse the repository at this point in the history
Since it works with the same URL object, doing repeated appends will
keep appending every loop which is not intended.

Add test cases to verify.

Reported-by: Jacob Mealey
Fixes #299
  • Loading branch information
bagder committed May 14, 2024
1 parent 13d7449 commit d80d308
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 26 deletions.
32 changes: 32 additions & 0 deletions tests.json
Original file line number Diff line number Diff line change
Expand Up @@ -2512,5 +2512,37 @@
"stderr": "",
"returncode": 0
}
},
{
"input": {
"arguments": [
"example.com",
"--append",
"query=add",
"--iterate",
"scheme=http ftp"
]
},
"expected": {
"stdout": "http://example.com/?add\nftp://example.com/?add\n",
"stderr": "",
"returncode": 0
}
},
{
"input": {
"arguments": [
"example.com",
"--append",
"path=add",
"--iterate",
"scheme=http ftp"
]
},
"expected": {
"stdout": "http://example.com/add\nftp://example.com/add\n",
"stderr": "",
"returncode": 0
}
}
]
58 changes: 32 additions & 26 deletions trurl.c
Original file line number Diff line number Diff line change
Expand Up @@ -1396,6 +1396,7 @@ static void singleurl(struct option *o,
struct curl_slist *iter)
{
CURLU *uh = iinfo->uh;
bool first_lap = true;
if(!uh) {
uh = curl_url();
if(!uh)
Expand Down Expand Up @@ -1498,29 +1499,31 @@ static void singleurl(struct option *o,
}
}

/* append path segments */
for(p = o->append_path; p; p = p->next) {
char *apath = p->data;
char *opath;
char *npath;
size_t olen;
/* extract the current path */
curl_url_get(uh, CURLUPART_PATH, &opath, 0);

/* does the existing path end with a slash, then don't
add one in between */
olen = strlen(opath);

/* append the new segment */
npath = curl_maprintf("%s%s%s", opath,
opath[olen-1] == '/' ? "" : "/",
apath);
if(npath) {
/* set the new path */
curl_url_set(uh, CURLUPART_PATH, npath, 0);
if(first_lap) {
/* append path segments */
for(p = o->append_path; p; p = p->next) {
char *apath = p->data;
char *opath;
char *npath;
size_t olen;
/* extract the current path */
curl_url_get(uh, CURLUPART_PATH, &opath, 0);

/* does the existing path end with a slash, then don't
add one in between */
olen = strlen(opath);

/* append the new segment */
npath = curl_maprintf("%s%s%s", opath,
opath[olen-1] == '/' ? "" : "/",
apath);
if(npath) {
/* set the new path */
curl_url_set(uh, CURLUPART_PATH, npath, 0);
}
curl_free(npath);
curl_free(opath);
}
curl_free(npath);
curl_free(opath);
}

extractqpairs(uh, o);
Expand All @@ -1531,10 +1534,12 @@ static void singleurl(struct option *o,
/* replace parts */
replace(o);

/* append query segments */
for(p = o->append_query; p; p = p->next) {
addqpair(p->data, strlen(p->data), o->jsonout);
query_is_modified = true;
if(first_lap) {
/* append query segments */
for(p = o->append_query; p; p = p->next) {
addqpair(p->data, strlen(p->data), o->jsonout);
query_is_modified = true;
}
}

sortquery(o);
Expand Down Expand Up @@ -1603,6 +1608,7 @@ static void singleurl(struct option *o,

o->urls++;

first_lap = false;
} while(iinfo->ptr);
if(!iinfo->uh)
curl_url_cleanup(uh);
Expand Down

0 comments on commit d80d308

Please sign in to comment.