Skip to content

Commit b93d1cb

Browse files
committed
if query is not updated, leave it untouched
Updated the tests accordingly. Fixes #281
1 parent 5cbe3e3 commit b93d1cb

File tree

2 files changed

+23
-16
lines changed

2 files changed

+23
-16
lines changed

tests.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1552,12 +1552,12 @@
15521552
"expected": {
15531553
"stdout": [
15541554
{
1555-
"url": "https://curl.se/",
1555+
"url": "https://curl.se/?&&&",
15561556
"parts": {
15571557
"scheme": "https",
15581558
"host": "curl.se",
15591559
"path": "/",
1560-
"query": ""
1560+
"query": "&&&"
15611561
},
15621562
"params": []
15631563
}

trurl.c

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,7 @@ static void trurl_warnf(struct option *o, const char *fmt, ...)
317317
struct string qpairs[MAX_QPAIRS]; /* encoded */
318318
struct string qpairsdec[MAX_QPAIRS]; /* decoded */
319319
int nqpairs; /* how many is stored */
320+
bool query_is_modified = false;
320321

321322
static void trurl_cleanup_options(struct option *o)
322323
{
@@ -1081,6 +1082,7 @@ static void trim(struct option *o)
10811082
}
10821083
free(temp);
10831084
}
1085+
query_is_modified = true;
10841086
}
10851087
}
10861088

@@ -1218,21 +1220,23 @@ static void extractqpairs(CURLU *uh, struct option *o)
12181220

12191221
static void qpair2query(CURLU *uh, struct option *o)
12201222
{
1221-
int i;
1222-
char *nq = NULL;
1223-
for(i = 0; i<nqpairs; i++) {
1224-
char *oldnq = nq;
1225-
nq = curl_maprintf("%s%s%s", nq?nq:"",
1226-
(nq && *nq && *(qpairs[i].str))? o->qsep: "",
1227-
qpairs[i].str);
1228-
curl_free(oldnq);
1229-
}
1230-
if(nq) {
1231-
int rc = curl_url_set(uh, CURLUPART_QUERY, nq, 0);
1232-
if(rc)
1233-
trurl_warnf(o, "internal problem");
1223+
if(query_is_modified) {
1224+
int i;
1225+
char *nq = NULL;
1226+
for(i = 0; i<nqpairs; i++) {
1227+
char *oldnq = nq;
1228+
nq = curl_maprintf("%s%s%s", nq?nq:"",
1229+
(nq && *nq && *(qpairs[i].str))? o->qsep: "",
1230+
qpairs[i].str);
1231+
curl_free(oldnq);
1232+
}
1233+
if(nq) {
1234+
int rc = curl_url_set(uh, CURLUPART_QUERY, nq, 0);
1235+
if(rc)
1236+
trurl_warnf(o, "internal problem");
1237+
}
1238+
curl_free(nq);
12341239
}
1235-
curl_free(nq);
12361240
}
12371241

12381242
/* sort case insensitively */
@@ -1258,6 +1262,7 @@ static void sortquery(struct option *o)
12581262
/* not these two lists may no longer be the same order after the sort */
12591263
qsort(&qpairs[0], nqpairs, sizeof(struct string), cmpfunc);
12601264
qsort(&qpairsdec[0], nqpairs, sizeof(struct string), cmpfunc);
1265+
query_is_modified = true;
12611266
}
12621267
}
12631268

@@ -1313,6 +1318,7 @@ static void replace(struct option *o)
13131318
key.str);
13141319
addqpair(key.str, strlen(key.str), o->jsonout);
13151320
}
1321+
query_is_modified = true;
13161322
}
13171323
}
13181324
static CURLUcode seturl(struct option *o, CURLU *uh, const char *url)
@@ -1470,6 +1476,7 @@ static void singleurl(struct option *o,
14701476
/* append query segments */
14711477
for(p = o->append_query; p; p = p->next) {
14721478
addqpair(p->data, strlen(p->data), o->jsonout);
1479+
query_is_modified = true;
14731480
}
14741481

14751482
sortquery(o);

0 commit comments

Comments
 (0)