Skip to content

Commit

Permalink
trurl: add --curl to only count as valid URLs supported by libcurl
Browse files Browse the repository at this point in the history
  • Loading branch information
emanuele6 committed Oct 18, 2023
1 parent b18bf4f commit 724fa02
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 6 deletions.
14 changes: 14 additions & 0 deletions tests.json
Original file line number Diff line number Diff line change
Expand Up @@ -2113,5 +2113,19 @@
"stderr": "",
"returncode": 0
}
},
{
"input": {
"arguments": [
"--curl",
"--verify",
"foo://bar"
]
},
"expected": {
"stdout": "",
"stderr": "trurl error: Unsupported URL scheme [foo://bar]\ntrurl error: Try trurl -h for help\n",
"returncode": 9
}
}
]
17 changes: 11 additions & 6 deletions trurl.c
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ static void help(void)
"Usage: " PROGNAME " [options] [URL]\n"
" -a, --append [component]=[data] - append data to component\n"
" --accept-space - give in to this URL abuse\n"
" --curl - only protocols supported by libcurl\n"
" --default-port - add known default ports\n"
" -f, --url-file [file/-] - read URLs from file or stdin\n"
" -g, --get [{component}s] - output component(s)\n"
Expand Down Expand Up @@ -257,6 +258,7 @@ struct option {
bool jsonout;
bool verify;
bool accept_space;
bool curl;
bool default_port;
bool keep_port;
bool punycode;
Expand Down Expand Up @@ -480,6 +482,8 @@ static int getarg(struct option *op,
warnf("built with too old libcurl version, --accept-space does not work");
#endif
}
else if(!strcmp("--curl", flag))
op->curl = true;
else if(!strcmp("--default-port", flag))
op->default_port = true;
else if(!strcmp("--keep-port", flag))
Expand Down Expand Up @@ -541,7 +545,7 @@ static CURLUcode geturlpart(struct option *o, int modifiers, CURLU *uh,
(((modifiers & VARMODIFIER_PUNY) || o->punycode) ?
CURLU_PUNYCODE : 0)|
#endif
CURLU_NON_SUPPORT_SCHEME|
(o->curl ? 0 : CURLU_NON_SUPPORT_SCHEME)|
(((modifiers & VARMODIFIER_URLENCODED) ||
o->urlencode) ?
0 :CURLU_URLDECODE));
Expand Down Expand Up @@ -712,7 +716,8 @@ static void get(struct option *op, CURLU *uh)
fputc('\n', stream);
}

static const struct var *setone(CURLU *uh, const char *setline)
static const struct var *setone(CURLU *uh, const char *setline,
const struct option *o)
{
char *ptr = strchr(setline, '=');
const struct var *v = NULL;
Expand All @@ -728,7 +733,7 @@ static const struct var *setone(CURLU *uh, const char *setline)
if(v) {
CURLUcode rc;
rc = curl_url_set(uh, v->part, ptr[1] ? &ptr[1] : NULL,
CURLU_NON_SUPPORT_SCHEME|
(o->curl ? 0 : CURLU_NON_SUPPORT_SCHEME)|
(urlencode ? CURLU_URLENCODE : 0) );
if(rc)
warnf("Error setting %s: %s", v->name, curl_url_strerror(rc));
Expand All @@ -750,7 +755,7 @@ static unsigned int set(CURLU *uh,
for(node = o->set_list; node; node = node->next) {
const struct var *v;
char *setline = node->data;
v = setone(uh, setline);
v = setone(uh, setline, o);
if(v) {
if(mask & (1 << v->part))
errorf(ERROR_SET, "duplicate --set for component %s", v->name);
Expand Down Expand Up @@ -1113,7 +1118,7 @@ static CURLUcode seturl(struct option *o, CURLU *uh, const char *url)
return curl_url_set(uh, CURLUPART_URL, url,
(o->no_guess_scheme ?
0 : CURLU_GUESS_SCHEME)|
CURLU_NON_SUPPORT_SCHEME|
(o->curl ? 0 : CURLU_NON_SUPPORT_SCHEME)|
(o->accept_space ?
CURLU_ALLOW_SPACE : 0)|
CURLU_URLENCODE);
Expand Down Expand Up @@ -1218,7 +1223,7 @@ static void singleurl(struct option *o,
curl_msnprintf(iterbuf, sizeof(iterbuf), "%.*s%s=%.*s", (int)plen, part,
urlencode ? "" : ":",
(int)wlen, w);
setone(uh, iterbuf);
setone(uh, iterbuf, o);
if(iter->next) {
struct iterinfo info;
memset(&info, 0, sizeof(info));
Expand Down

0 comments on commit 724fa02

Please sign in to comment.