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: allow "control bytes" in JSON output #333

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