Skip to content

Commit

Permalink
trurl: introduce --qtrim for trimming queries (only)
Browse files Browse the repository at this point in the history
--trim is now deprecated. When we added that we thought we would trim
other components over time but that has not materialized. Switching to
--qtrim makes for easier command lines with no functionality loss.

--trim is no longer displayed in the help output but is still tested in
several test cases.

Closes #364
  • Loading branch information
bagder committed Sep 13, 2024
1 parent 1a5a63a commit 4eea8d5
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 37 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ $ trurl "https://fake.host/hello#frag" --set user=::moo:: --json
**Remove tracking tuples from query:**

```text
$ trurl "https://curl.se?search=hey&utm_source=tracker" --trim query="utm_*"
$ trurl "https://curl.se?search=hey&utm_source=tracker" --qtrim "utm_*"
https://curl.se/?search=hey
```

Expand All @@ -114,7 +114,7 @@ https://example.com?a=c&b=a&c=b
**Work with a query that uses a semicolon separator:**

```text
$ trurl "https://curl.se?search=fool;page=5" --trim query="search" --query-separator ";"
$ trurl "https://curl.se?search=fool;page=5" --qtrim "search" --query-separator ";"
https://curl.se?page=5
```

Expand Down
68 changes: 48 additions & 20 deletions tests.json
Original file line number Diff line number Diff line change
Expand Up @@ -827,6 +827,20 @@
"returncode": 0
}
},
{
"input": {
"arguments": [
"https://example.com?search=hello&utm_source=tracker",
"--qtrim",
"utm_*"
]
},
"expected": {
"stdout": "https://example.com/?search=hello\n",
"stderr": "",
"returncode": 0
}
},
{
"input": {
"arguments": [
Expand All @@ -841,12 +855,26 @@
"returncode": 0
}
},
{
"input": {
"arguments": [
"https://example.com?search=hello&utm_source=tracker&more=data",
"--qtrim",
"utm_*"
]
},
"expected": {
"stdout": "https://example.com/?search=hello&more=data\n",
"stderr": "",
"returncode": 0
}
},
{
"input": {
"arguments": [
"https://example.com?search=hello&more=data",
"--trim",
"query=utm_*"
"--qtrim",
"utm_*"
]
},
"expected": {
Expand All @@ -873,8 +901,8 @@
"input": {
"arguments": [
"https://example.com?search=hello&utm_source=tracker&more=data",
"--trim",
"query=utm_source"
"--qtrim",
"utm_source"
]
},
"expected": {
Expand All @@ -887,12 +915,12 @@
"input": {
"arguments": [
"https://example.com?search=hello&utm_source=tracker&more=data",
"--trim",
"query=utm_source",
"--trim",
"query=more",
"--trim",
"query=search"
"--qtrim",
"utm_source",
"--qtrim",
"more",
"--qtrim",
"search"
]
},
"expected": {
Expand Down Expand Up @@ -951,8 +979,8 @@
"input": {
"arguments": [
"https://example.com?moo&search=hello",
"--trim",
"query=search"
"--qtrim",
"search"
]
},
"expected": {
Expand All @@ -965,8 +993,8 @@
"input": {
"arguments": [
"https://example.com?search=hello&moo",
"--trim",
"query=search"
"--qtrim",
"search"
]
},
"expected": {
Expand All @@ -979,8 +1007,8 @@
"input": {
"arguments": [
"https://example.com?search=hello",
"--trim",
"query=search",
"--qtrim",
"search",
"--append",
"query=moo"
]
Expand Down Expand Up @@ -2157,8 +2185,8 @@
{
"input": {
"arguments": [
"--trim",
"query=a",
"--qtrim",
"a",
"-a",
"query=a=ciao",
"-a",
Expand Down Expand Up @@ -2267,8 +2295,8 @@
"arguments": [
"--url",
"https://curl.se/we/are.html?*=moo&user=many#more",
"--trim",
"query=\\*"
"--qtrim",
"\\*"
]
},
"expected": {
Expand Down
21 changes: 11 additions & 10 deletions trurl.c
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ static const struct var variables[] = {
#define ERROR_SET 5 /* a --set problem */
#define ERROR_MEM 6 /* out of memory */
#define ERROR_URL 7 /* could not get a URL out of the set components */
#define ERROR_TRIM 8 /* a --trim problem */
#define ERROR_TRIM 8 /* a --qtrim problem */
#define ERROR_BADURL 9 /* if --verify is set and the URL cannot parse */
#define ERROR_GET 10 /* bad --get syntax */
#define ERROR_ITER 11 /* bad --iterate syntax */
Expand Down Expand Up @@ -244,14 +244,14 @@ static void help(void)
" --keep-port - keep known default ports\n"
" --no-guess-scheme - require scheme in URLs\n"
" --punycode - encode hostnames in punycode\n"
" --qtrim [what] - trim the query\n"
" --query-separator [letter] - if something else than '&'\n"
" --quiet - Suppress (some) notes and comments\n"
" --redirect [URL] - redirect to this\n"
" --replace [data] - replaces a query [data]\n"
" --replace-append [data] - appends a new query if not found\n"
" -s, --set [component]=[data] - set component content\n"
" --sort-query - alpha-sort the query pairs\n"
" --trim [component]=[what] - trim component\n"
" --url [URL] - URL to work with\n"
" --urlencode - URL encode components by default\n"
" -v, --version - show version\n"
Expand Down Expand Up @@ -652,6 +652,13 @@ static int getarg(struct option *o,
*usedarg = gap;
}
else if(checkoptarg(o, "--trim", flag, arg)) {
if(strncmp(arg, "query=", 6))
errorf(o, ERROR_TRIM, "Unsupported trim component: %s", arg);

trimadd(o, &arg[6]);
*usedarg = gap;
}
else if(checkoptarg(o, "--qtrim", flag, arg)) {
trimadd(o, arg);
*usedarg = gap;
}
Expand Down Expand Up @@ -1234,21 +1241,15 @@ static bool trim(struct option *o)
bool query_is_modified = false;
struct curl_slist *node;
for(node = o->trim_list; node; node = node->next) {
char *ptr;
char *instr = node->data;
if(strncmp(instr, "query", 5))
/* for now we can only trim query components */
errorf(o, ERROR_TRIM, "Unsupported trim component: %s", instr);
ptr = strchr(instr, '=');
if(ptr && (ptr > instr)) {
char *ptr = node->data;
if(ptr) {
/* 'ptr' should be a fixed string or a pattern ending with an
asterisk */
size_t inslen;
bool pattern = false;
int i;
char *temp = NULL;

ptr++; /* pass the = */
inslen = strlen(ptr);
if(inslen) {
pattern = ptr[inslen - 1] == '*';
Expand Down
23 changes: 18 additions & 5 deletions trurl.md
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,17 @@ Uses the punycode version of the hostname, which is how International Domain
Names are converted into plain ASCII. If the hostname is not using IDN, the
regular ASCII name is used.

## --qtrim [what]

Trims data off a query.

*what* is specified as a full name of a name/value pair, or as a word prefix
(using a single trailing asterisk (`*`)) which makes trurl remove the tuples
from the query string that match the instruction.

To match a literal trailing asterisk instead of using a wildcard, escape it
with a backslash in front of it. Like `\\*`.

## --query-separator [what]

Specify the single letter used for separating query pairs. The default is `&`
Expand Down Expand Up @@ -292,6 +303,8 @@ otherwise only had their query pairs in different orders.

## --trim [component]=[what]

Deprecated: use **--qtrim**.

Trims data off a component. Currently this can only trim a query component.

*what* is specified as a full word or as a word prefix (using a single
Expand Down Expand Up @@ -546,9 +559,9 @@ them first at least increases the chances of it working:
http://alpha/?one=real&three=alsoreal&two=fake

Remove name/value pairs from the URL by specifying exact name or wildcard
pattern with **--trim**:
pattern with **--qtrim**:

$ trurl 'https://example.com?a12=hej&a23=moo&b12=foo' --trim 'query=a*'
$ trurl 'https://example.com?a12=hej&a23=moo&b12=foo' --qtrim a*'
https://example.com/?b12=foo

## fragment
Expand Down Expand Up @@ -746,7 +759,7 @@ $ trurl "https://fake.host/search?q=answers&user=me#frag" --json
## Remove tracking tuples from query

~~~
$ trurl "https://curl.se?search=hey&utm_source=tracker" --trim query="utm_*"
$ trurl "https://curl.se?search=hey&utm_source=tracker" --qtrim "utm_*"
https://curl.se/?search=hey
~~~

Expand All @@ -767,7 +780,7 @@ https://example.com?a=c&b=a&c=b
## Work with a query that uses a semicolon separator

~~~
$ trurl "https://curl.se?search=fool;page=5" --trim query="search" --query-separator ";"
$ trurl "https://curl.se?search=fool;page=5" --qtrim "search" --query-separator ";"
https://curl.se?page=5
~~~

Expand Down Expand Up @@ -821,7 +834,7 @@ Could not output a valid URL

## 8

A problem with --trim
A problem with --qtrim

## 9

Expand Down

0 comments on commit 4eea8d5

Please sign in to comment.