Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

trurl: optimize the path append loop #330

Merged
merged 1 commit into from
Aug 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions tests.json
Original file line number Diff line number Diff line change
Expand Up @@ -2557,6 +2557,22 @@
"returncode": 0
}
},
{
"input": {
"arguments": [
"example.com",
"--append",
"path=add",
"--append",
"path=two"
]
},
"expected": {
"stdout": "http://example.com/add/two\n",
"stderr": "",
"returncode": 0
}
},
{
"input": {
"arguments": [
Expand Down
24 changes: 14 additions & 10 deletions trurl.c
Original file line number Diff line number Diff line change
Expand Up @@ -1685,15 +1685,17 @@ static void singleurl(struct option *o,
}

if(first_lap) {
/* extract the current path */
char *opath;
bool path_is_modified = false;
if(curl_url_get(uh, CURLUPART_PATH, &opath, 0))
errorf(o, ERROR_ITER, "out of memory");

/* 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 */
if(curl_url_get(uh, CURLUPART_PATH, &opath, 0))
errorf(o, ERROR_ITER, "out of memory");

/* does the existing path end with a slash, then don't
add one in between */
Expand All @@ -1703,14 +1705,16 @@ static void singleurl(struct option *o,
npath = curl_maprintf("%s%s%s", opath,
opath[olen-1] == '/' ? "" : "/",
apath);
if(npath) {
/* set the new path */
if(curl_url_set(uh, CURLUPART_PATH, npath, 0))
errorf(o, ERROR_ITER, "out of memory");
}
curl_free(npath);
curl_free(opath);
opath = npath;
path_is_modified = true;
}
if(path_is_modified) {
/* set the new path */
if(curl_url_set(uh, CURLUPART_PATH, opath, 0))
errorf(o, ERROR_ITER, "out of memory");
}
curl_free(opath);
}

query_is_modified |= extractqpairs(uh, o);
Expand Down