Skip to content

Commit

Permalink
trurl: URL decode get components by default
Browse files Browse the repository at this point in the history
Unless written like {:component} with a leading colon.

Closes #47
  • Loading branch information
bagder committed Apr 3, 2023
1 parent 829ec82 commit 023c323
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 17 deletions.
8 changes: 3 additions & 5 deletions trurl.1
Original file line number Diff line number Diff line change
Expand Up @@ -63,15 +63,13 @@ can be output when specified as \fB{component}\fP, with the name of the part
within curly braces. The following component names are available: {url},
{scheme}, {user}, {password}, {options}, {host}, {port}, {path}, {query},
{fragment} and {zoneid}.

All components are shown URL decoded by default. If you instead write the
component prefixed with a colon like "{:path}", you get it output URL encoded.
.IP "--json"
Outputs all components of the URL as a JSON object. All components of the URL
that has data will get populated in the object with using their component
names.
.SH "MODIFIERS"
.IP "--urldecode"
By default each component is output using the format of the input (URL
encoded). This option makes trurl instead provide it URL decoded. Note that
%20 will then output space and %09 a tab etc.
.SH EXAMPLES
.IP "Replace the host name of a URL"
.nf
Expand Down
24 changes: 12 additions & 12 deletions trurl.c
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,6 @@ static void help(void)
" OUTPUT\n"
" -g, --get [{component}s] - output URL component(s)\n"
" --json - output URL info as JSON\n"
" MODIFIERS\n"
" --urldecode - URL decode the output\n"
" URL COMPONENTS:\n"
" "
);
Expand Down Expand Up @@ -134,7 +132,6 @@ struct option {
const char *format;
FILE *url;
bool urlopen;
bool urldecode;
bool jsonout;
unsigned char output;
};
Expand Down Expand Up @@ -276,16 +273,14 @@ static int getarg(struct option *op,
op->format = arg;
*usedarg = 1;
}
else if(!strcmp("--urldecode", flag))
op->urldecode = true;
else if(!strcmp("--json", flag))
op->jsonout = true;
else
return 1; /* unrecognized option */
return 0;
}

static void format(struct option *op, CURLU *uh)
static void get(struct option *op, CURLU *uh)
{
FILE *stream = stdout;
const char *ptr = op->format;
Expand All @@ -303,20 +298,26 @@ static void format(struct option *op, CURLU *uh)
char *end;
size_t vlen;
int i;
bool urldecode = true;
end = strchr(ptr, '}');
ptr++; /* pass the { */
if(!end) {
/* syntax error */
continue;
}
/* {path} {:path} */
if(*ptr == ':') {
urldecode = false;
ptr++;
}
vlen = end - ptr;
for(i = 0; variables[i].name; i++) {
if((strlen(variables[i].name) == vlen) &&
!strncasecmp(ptr, variables[i].name, vlen)) {
char *nurl;
CURLUcode rc;
rc = curl_url_get(uh, variables[i].part, &nurl, CURLU_DEFAULT_PORT|
(op->urldecode?CURLU_URLDECODE:0));
(urldecode?CURLU_URLDECODE:0));
switch(rc) {
case CURLUE_OK:
fprintf(stream, "%s", nurl);
Expand Down Expand Up @@ -458,13 +459,13 @@ static void jsonString(FILE *stream, const char *in, bool lowercase)
static void json(struct option *o, CURLU *uh)
{
int i;

(void)o;
fputs("{\n", stdout);
for(i = 0; variables[i].name; i++) {
char *nurl;
CURLUcode rc = curl_url_get(uh, variables[i].part, &nurl,
(i?CURLU_DEFAULT_PORT:0)|
(o->urldecode?CURLU_URLDECODE:0));
CURLU_URLDECODE);
if(!rc) {
printf(" \"%s\": ", variables[i].name);
jsonString(stdout, nurl, false);
Expand Down Expand Up @@ -543,13 +544,12 @@ static void singleurl(struct option *o,
json(o, uh);
else if(o->format) {
/* custom output format */
format(o, uh);
get(o, uh);
}
else {
/* default output is full URL */
char *nurl = NULL;
if(!curl_url_get(uh, CURLUPART_URL, &nurl,
o->urldecode?CURLU_URLDECODE:0)) {
if(!curl_url_get(uh, CURLUPART_URL, &nurl, 0)) {
printf("%s\n", nurl);
curl_free(nurl);
}
Expand Down

0 comments on commit 023c323

Please sign in to comment.