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: make --replace URL encode the provided data argument #327

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
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