Skip to content

Commit

Permalink
trurl: optimize the path append loop
Browse files Browse the repository at this point in the history
Extract the path once, do all the appends then set the final version
once in the end. Instead of doing it every loop.
  • Loading branch information
bagder committed Aug 27, 2024
1 parent caaec33 commit dcea239
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 10 deletions.
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

0 comments on commit dcea239

Please sign in to comment.