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

Handle Errors returns from geturlpart() in json function #318

Closed
wants to merge 2 commits into from
Closed
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
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