Skip to content

Commit

Permalink
trurl: allow "control bytes" in JSON output
Browse files Browse the repository at this point in the history
For example when doing

    trurl 'http://example.org/%18' --json

Adapted and extended test cases to verify.

Fixes #262
Reported-by: Emanuele Torre
  • Loading branch information
bagder committed Aug 27, 2024
1 parent 8bb43b8 commit a12ed0f
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 22 deletions.
64 changes: 44 additions & 20 deletions tests.json
Original file line number Diff line number Diff line change
Expand Up @@ -2644,27 +2644,51 @@
"returncode": 0
}
},
{
"input": {
"arguments": [
"http://example.org/%18",
"--json"
]
},
"expected": {
"stdout":[
{
"url": "http://example.org/%18",
"parts": {
"scheme": "http",
"host": "example.org"
{
"input": {
"arguments": [
"http://example.org/%18",
"--json"
]
},
"expected": {
"stdout":[
{
"url": "http://example.org/%18",
"parts": {
"scheme": "http",
"host": "example.org",
"path": "/\u0018"
}
}
}
],
"stderr": "trurl note: URL decode error, most likely because of rubbish in the input (path)\n",
"returncode": 0
}
},
],
"stderr": "",
"returncode": 0
}
},
{
"input": {
"arguments": [
"http://example.org/%18",
"--json",
"--urlencode"
]
},
"expected": {
"stdout":[
{
"url": "http://example.org/%18",
"parts": {
"scheme": "http",
"host": "example.org",
"path": "/%18"
}
}
],
"stderr": "",
"returncode": 0
}
},
{
"input": {
"arguments": [
Expand Down
32 changes: 30 additions & 2 deletions trurl.c
Original file line number Diff line number Diff line change
Expand Up @@ -1120,14 +1120,42 @@ static void json(struct option *o, CURLU *uh)
bool params_errors = false;
for(i = 0; variables[i].name; i++) {
char *part;
rc = geturlpart(o, 0, uh, variables[i].part, &part);
/* ask for the URL encoded version so that weird control characters do not
cause problems. URL decode it when push to json. */
rc = geturlpart(o, VARMODIFIER_URLENCODED, uh, variables[i].part, &part);
if(!rc) {
int olen;
char *dec = NULL;

if(!o->urlencode) {
if(variables[i].part == CURLUPART_QUERY) {
/* query parts have '+' for space */
char *n;
char *p = part;
do {
n = strchr(p, '+');
if(n) {
*n = ' ';
p = n + 1;
}
} while(n);
}

dec = curl_easy_unescape(NULL, part, 0, &olen);
if(!dec)
errorf(o, ERROR_MEM, "out of memory");
}

if(!first)
fputs(",\n", stdout);
first = false;
printf(" \"%s\": ", variables[i].name);
jsonString(stdout, part, strlen(part), false);
if(dec)
jsonString(stdout, dec, (size_t)olen, false);
else
jsonString(stdout, part, strlen(part), false);
curl_free(part);
curl_free(dec);
}
else if(is_valid_trurl_error(rc)) {
trurl_warnf(o, "%s (%s)", curl_url_strerror(rc), variables[i].name);
Expand Down

0 comments on commit a12ed0f

Please sign in to comment.