Skip to content

Commit 445699c

Browse files
committed
trurl: make --replace URL encode the provided data argument
Makes it work like --append query= already does. - update man page - update test cases Fixes #321
1 parent 2906f47 commit 445699c

File tree

3 files changed

+24
-14
lines changed

3 files changed

+24
-14
lines changed

tests.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2366,17 +2366,17 @@
23662366
},
23672367
"expected": {
23682368
"stdout": [{
2369-
"url": "http://example.org/?quest=%00",
2369+
"url": "http://example.org/?quest=%2500",
23702370
"parts": {
23712371
"scheme": "http",
23722372
"host": "example.org",
23732373
"path": "/",
2374-
"query": "quest=%00"
2374+
"query": "quest=%2500"
23752375
},
23762376
"params": [
23772377
{
23782378
"key": "quest",
2379-
"value": "\u0000"
2379+
"value": "%00"
23802380
}
23812381
]
23822382
}],

trurl.1

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,9 @@ Replaces a URL query.
211211
data can either take the form of a single value, or as a key/value pair in the
212212
shape \fIfoo=bar\fP. If replace is called on an item that isn't in the list of
213213
queries trurl ignores that item.
214+
215+
trurl URL encodes both sides of the '=' character in the given input data
216+
argument.
214217
.IP "--force-replace [data]"
215218
Works the same as \fI--replace\fP, but trurl appends a missing query string if
216219
it is not in the query list already.

trurl.c

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -472,9 +472,8 @@ static void pathadd(struct option *o, const char *path)
472472
}
473473
}
474474

475-
static void queryadd(struct option *o, const char *query)
475+
static char *encodeassign(const char *query)
476476
{
477-
struct curl_slist *n;
478477
char *p = strchr(query, '=');
479478
char *urle;
480479
if(p) {
@@ -487,11 +486,16 @@ static void queryadd(struct option *o, const char *query)
487486
}
488487
else
489488
urle = curl_easy_escape(NULL, query, 0);
489+
return urle;
490+
}
491+
492+
static void queryadd(struct option *o, const char *query)
493+
{
494+
char *urle = encodeassign(query);
490495
if(urle) {
491-
n = curl_slist_append(o->append_query, urle);
492-
if(n) {
496+
struct curl_slist *n = curl_slist_append(o->append_query, urle);
497+
if(n)
493498
o->append_query = n;
494-
}
495499
curl_free(urle);
496500
}
497501
}
@@ -537,14 +541,17 @@ static void trimadd(struct option *o,
537541
static void replaceadd(struct option *o,
538542
const char *replace_list) /* [component]=[data] */
539543
{
540-
struct curl_slist *n = NULL;
541-
if(replace_list)
542-
n = curl_slist_append(o->replace_list, replace_list);
544+
if(replace_list) {
545+
char *urle = encodeassign(replace_list);
546+
if(urle) {
547+
struct curl_slist *n = curl_slist_append(o->replace_list, urle);
548+
if(n)
549+
o->replace_list = n;
550+
curl_free(urle);
551+
}
552+
}
543553
else
544554
errorf(o, ERROR_REPL, "No data passed to replace component");
545-
546-
if(n)
547-
o->replace_list = n;
548555
}
549556

550557
static bool longarg(const char *flag, const char *check)

0 commit comments

Comments
 (0)