Skip to content

Commit a12ed0f

Browse files
committed
trurl: allow "control bytes" in JSON output
For example when doing trurl 'http://example.org/%18' --json Adapted and extended test cases to verify. Fixes #262 Reported-by: Emanuele Torre
1 parent 8bb43b8 commit a12ed0f

File tree

2 files changed

+74
-22
lines changed

2 files changed

+74
-22
lines changed

tests.json

Lines changed: 44 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2644,27 +2644,51 @@
26442644
"returncode": 0
26452645
}
26462646
},
2647-
{
2648-
"input": {
2649-
"arguments": [
2650-
"http://example.org/%18",
2651-
"--json"
2652-
]
2653-
},
2654-
"expected": {
2655-
"stdout":[
2656-
{
2657-
"url": "http://example.org/%18",
2658-
"parts": {
2659-
"scheme": "http",
2660-
"host": "example.org"
2647+
{
2648+
"input": {
2649+
"arguments": [
2650+
"http://example.org/%18",
2651+
"--json"
2652+
]
2653+
},
2654+
"expected": {
2655+
"stdout":[
2656+
{
2657+
"url": "http://example.org/%18",
2658+
"parts": {
2659+
"scheme": "http",
2660+
"host": "example.org",
2661+
"path": "/\u0018"
2662+
}
26612663
}
2662-
}
2663-
],
2664-
"stderr": "trurl note: URL decode error, most likely because of rubbish in the input (path)\n",
2665-
"returncode": 0
2666-
}
2667-
},
2664+
],
2665+
"stderr": "",
2666+
"returncode": 0
2667+
}
2668+
},
2669+
{
2670+
"input": {
2671+
"arguments": [
2672+
"http://example.org/%18",
2673+
"--json",
2674+
"--urlencode"
2675+
]
2676+
},
2677+
"expected": {
2678+
"stdout":[
2679+
{
2680+
"url": "http://example.org/%18",
2681+
"parts": {
2682+
"scheme": "http",
2683+
"host": "example.org",
2684+
"path": "/%18"
2685+
}
2686+
}
2687+
],
2688+
"stderr": "",
2689+
"returncode": 0
2690+
}
2691+
},
26682692
{
26692693
"input": {
26702694
"arguments": [

trurl.c

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1120,14 +1120,42 @@ static void json(struct option *o, CURLU *uh)
11201120
bool params_errors = false;
11211121
for(i = 0; variables[i].name; i++) {
11221122
char *part;
1123-
rc = geturlpart(o, 0, uh, variables[i].part, &part);
1123+
/* ask for the URL encoded version so that weird control characters do not
1124+
cause problems. URL decode it when push to json. */
1125+
rc = geturlpart(o, VARMODIFIER_URLENCODED, uh, variables[i].part, &part);
11241126
if(!rc) {
1127+
int olen;
1128+
char *dec = NULL;
1129+
1130+
if(!o->urlencode) {
1131+
if(variables[i].part == CURLUPART_QUERY) {
1132+
/* query parts have '+' for space */
1133+
char *n;
1134+
char *p = part;
1135+
do {
1136+
n = strchr(p, '+');
1137+
if(n) {
1138+
*n = ' ';
1139+
p = n + 1;
1140+
}
1141+
} while(n);
1142+
}
1143+
1144+
dec = curl_easy_unescape(NULL, part, 0, &olen);
1145+
if(!dec)
1146+
errorf(o, ERROR_MEM, "out of memory");
1147+
}
1148+
11251149
if(!first)
11261150
fputs(",\n", stdout);
11271151
first = false;
11281152
printf(" \"%s\": ", variables[i].name);
1129-
jsonString(stdout, part, strlen(part), false);
1153+
if(dec)
1154+
jsonString(stdout, dec, (size_t)olen, false);
1155+
else
1156+
jsonString(stdout, part, strlen(part), false);
11301157
curl_free(part);
1158+
curl_free(dec);
11311159
}
11321160
else if(is_valid_trurl_error(rc)) {
11331161
trurl_warnf(o, "%s (%s)", curl_url_strerror(rc), variables[i].name);

0 commit comments

Comments
 (0)