Skip to content

Commit

Permalink
trurl: fix silent error in json json function
Browse files Browse the repository at this point in the history
Closes #318
  • Loading branch information
jacobmealey authored and bagder committed Aug 14, 2024
1 parent d4300ca commit ae00a7a
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 30 deletions.
57 changes: 45 additions & 12 deletions tests.json
Original file line number Diff line number Diff line change
Expand Up @@ -2088,7 +2088,8 @@
"input" : {
"arguments": [
"http://example.com/?q=mr%00smith",
"--json"
"--json",
"--urlencode"
]
},
"expected": {
Expand All @@ -2100,7 +2101,8 @@
"parts": {
"scheme": "http",
"host": "example.com",
"path": "/"
"path": "/",
"query": "q=mr%00smith"
},
"params": [
{
Expand All @@ -2116,7 +2118,8 @@
"input" : {
"arguments": [
"http://example.com/?q=mr%00sm%00ith",
"--json"
"--json",
"--urlencode"
]
},
"expected": {
Expand All @@ -2128,7 +2131,8 @@
"parts": {
"scheme": "http",
"host": "example.com",
"path": "/"
"path": "/",
"query": "q=mr%00sm%00ith"
},
"params": [
{
Expand All @@ -2144,7 +2148,8 @@
"input" : {
"arguments": [
"http://example.com/?q=mr%00%00%00smith",
"--json"
"--json",
"--urlencode"
]
},
"expected": {
Expand All @@ -2156,7 +2161,8 @@
"parts": {
"scheme": "http",
"host": "example.com",
"path": "/"
"path": "/",
"query": "q=mr%00%00%00smith"
},
"params": [
{
Expand Down Expand Up @@ -2354,7 +2360,8 @@
"example.org/?quest=best",
"--replace",
"quest=%00",
"--json"
"--json",
"--urlencode"
]
},
"expected": {
Expand All @@ -2363,7 +2370,8 @@
"parts": {
"scheme": "http",
"host": "example.org",
"path": "/"
"path": "/",
"query": "quest=%00"
},
"params": [
{
Expand Down Expand Up @@ -2419,7 +2427,8 @@
"input": {
"arguments": [
"--json",
"0?0%000000000000000000000000000000000"
"0?0%000000000000000000000000000000000",
"--urlencode"
]
},
"expected": {
Expand All @@ -2431,7 +2440,8 @@
"parts": {
"scheme": "http",
"host": "0.0.0.0",
"path": "/"
"path": "/",
"query": "0%000000000000000000000000000000000"
},
"params": [
{
Expand All @@ -2447,7 +2457,8 @@
"input": {
"arguments": [
"--json",
"0?0%000000000000000000000000000000000=000%0000000000"
"0?0%000000000000000000000000000000000=000%0000000000",
"--urlencode"
]
},
"expected": {
Expand All @@ -2459,7 +2470,8 @@
"parts": {
"scheme": "http",
"host": "0.0.0.0",
"path": "/"
"path": "/",
"query": "0%000000000000000000000000000000000=000%0000000000"
},
"params": [
{
Expand Down Expand Up @@ -2601,5 +2613,26 @@
"stderr": "",
"returncode": 0
}
},
{
"input": {
"arguments": [
"http://example.org/%18",
"--json"
]
},
"expected": {
"stdout":[
{
"url": "http://example.org/%18",
"parts": {
"scheme": "http",
"host": "example.org"
}
}
],
"stderr": "trurl note: URL decode error, most likely because of rubbish in the input (path)\n",
"returncode": 0
}
}
]
52 changes: 34 additions & 18 deletions trurl.c
Original file line number Diff line number Diff line change
Expand Up @@ -764,6 +764,29 @@ static CURLUcode geturlpart(struct option *o, int modifiers, CURLU *uh,
return rc;
}

static bool is_valid_trurl_error(CURLUcode rc)
{
switch(rc) {
case CURLUE_OK:
case CURLUE_NO_SCHEME:
case CURLUE_NO_USER:
case CURLUE_NO_PASSWORD:
case CURLUE_NO_OPTIONS:
case CURLUE_NO_HOST:
case CURLUE_NO_PORT:
case CURLUE_NO_QUERY:
case CURLUE_NO_FRAGMENT:
#ifdef SUPPORTS_ZONEID
case CURLUE_NO_ZONEID:
#endif
/* silently ignore */
return false;
default:
return true;
}
return true;
}

static void showurl(FILE *stream, struct option *o, int modifiers,
CURLU *uh)
{
Expand Down Expand Up @@ -883,29 +906,15 @@ static void get(struct option *o, CURLU *uh)
if(v) {
char *nurl;
CURLUcode rc = geturlpart(o, mods, uh, v->part, &nurl);
switch(rc) {
case CURLUE_OK:
if(rc == CURLUE_OK) {
fputs(nurl, stream);
curl_free(nurl);
case CURLUE_NO_SCHEME:
case CURLUE_NO_USER:
case CURLUE_NO_PASSWORD:
case CURLUE_NO_OPTIONS:
case CURLUE_NO_HOST:
case CURLUE_NO_PORT:
case CURLUE_NO_QUERY:
case CURLUE_NO_FRAGMENT:
#ifdef SUPPORTS_ZONEID
case CURLUE_NO_ZONEID:
#endif
/* silently ignore */
break;
default:
}
else if(is_valid_trurl_error(rc)) {
if((rc == CURLUE_URLDECODE) && strict)
errorf(o, ERROR_GET, "problems URL decoding %s", v->name);
else
trurl_warnf(o, "%s (%s)", curl_url_strerror(rc), v->name);
break;
}
}
else
Expand Down Expand Up @@ -1092,6 +1101,8 @@ static void json(struct option *o, CURLU *uh)
jsonString(stdout, url, strlen(url), false);
curl_free(url);
fputs(",\n \"parts\": {\n", stdout);
/* special error handling required to not print params array. */
bool params_errors = false;
for(i = 0; variables[i].name; i++) {
char *part;
rc = geturlpart(o, 0, uh, variables[i].part, &part);
Expand All @@ -1103,10 +1114,14 @@ static void json(struct option *o, CURLU *uh)
jsonString(stdout, part, strlen(part), false);
curl_free(part);
}
else if(is_valid_trurl_error(rc)) {
trurl_warnf(o, "%s (%s)", curl_url_strerror(rc), variables[i].name);
params_errors = true;
}
}
fputs("\n }", stdout);
first = true;
if(nqpairs) {
if(nqpairs && !params_errors) {
int j;
fputs(",\n \"params\": [\n", stdout);
for(j = 0 ; j < nqpairs; j++) {
Expand Down Expand Up @@ -1440,6 +1455,7 @@ static void replace(struct option *o)
query_is_modified = true;
}
}

static CURLUcode seturl(struct option *o, CURLU *uh, const char *url)
{
return curl_url_set(uh, CURLUPART_URL, url,
Expand Down

0 comments on commit ae00a7a

Please sign in to comment.