Skip to content

Commit

Permalink
trurl: make --replace URL encode the provided data argument
Browse files Browse the repository at this point in the history
Makes it work like --append query= already does.

- update man page
- update test cases

Fixes #321
  • Loading branch information
bagder committed Aug 27, 2024
1 parent 2906f47 commit 445699c
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 14 deletions.
6 changes: 3 additions & 3 deletions tests.json
Original file line number Diff line number Diff line change
Expand Up @@ -2366,17 +2366,17 @@
},
"expected": {
"stdout": [{
"url": "http://example.org/?quest=%00",
"url": "http://example.org/?quest=%2500",
"parts": {
"scheme": "http",
"host": "example.org",
"path": "/",
"query": "quest=%00"
"query": "quest=%2500"
},
"params": [
{
"key": "quest",
"value": "\u0000"
"value": "%00"
}
]
}],
Expand Down
3 changes: 3 additions & 0 deletions trurl.1
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,9 @@ Replaces a URL query.
data can either take the form of a single value, or as a key/value pair in the
shape \fIfoo=bar\fP. If replace is called on an item that isn't in the list of
queries trurl ignores that item.

trurl URL encodes both sides of the '=' character in the given input data
argument.
.IP "--force-replace [data]"
Works the same as \fI--replace\fP, but trurl appends a missing query string if
it is not in the query list already.
Expand Down
29 changes: 18 additions & 11 deletions trurl.c
Original file line number Diff line number Diff line change
Expand Up @@ -472,9 +472,8 @@ static void pathadd(struct option *o, const char *path)
}
}

static void queryadd(struct option *o, const char *query)
static char *encodeassign(const char *query)
{
struct curl_slist *n;
char *p = strchr(query, '=');
char *urle;
if(p) {
Expand All @@ -487,11 +486,16 @@ static void queryadd(struct option *o, const char *query)
}
else
urle = curl_easy_escape(NULL, query, 0);
return urle;
}

static void queryadd(struct option *o, const char *query)
{
char *urle = encodeassign(query);
if(urle) {
n = curl_slist_append(o->append_query, urle);
if(n) {
struct curl_slist *n = curl_slist_append(o->append_query, urle);
if(n)
o->append_query = n;
}
curl_free(urle);
}
}
Expand Down Expand Up @@ -537,14 +541,17 @@ static void trimadd(struct option *o,
static void replaceadd(struct option *o,
const char *replace_list) /* [component]=[data] */
{
struct curl_slist *n = NULL;
if(replace_list)
n = curl_slist_append(o->replace_list, replace_list);
if(replace_list) {
char *urle = encodeassign(replace_list);
if(urle) {
struct curl_slist *n = curl_slist_append(o->replace_list, urle);
if(n)
o->replace_list = n;
curl_free(urle);
}
}
else
errorf(o, ERROR_REPL, "No data passed to replace component");

if(n)
o->replace_list = n;
}

static bool longarg(const char *flag, const char *check)
Expand Down

0 comments on commit 445699c

Please sign in to comment.