From 023c3234ad8f59a458e776539f1f83866c909c37 Mon Sep 17 00:00:00 2001 From: Daniel Stenberg Date: Mon, 3 Apr 2023 17:47:52 +0200 Subject: [PATCH] trurl: URL decode get components by default Unless written like {:component} with a leading colon. Closes #47 --- trurl.1 | 8 +++----- trurl.c | 24 ++++++++++++------------ 2 files changed, 15 insertions(+), 17 deletions(-) diff --git a/trurl.1 b/trurl.1 index 2d86cbc8..7ee16d17 100644 --- a/trurl.1 +++ b/trurl.1 @@ -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 diff --git a/trurl.c b/trurl.c index d5ede9fe..feba03d8 100644 --- a/trurl.c +++ b/trurl.c @@ -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" " " ); @@ -134,7 +132,6 @@ struct option { const char *format; FILE *url; bool urlopen; - bool urldecode; bool jsonout; unsigned char output; }; @@ -276,8 +273,6 @@ 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 @@ -285,7 +280,7 @@ static int getarg(struct option *op, 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; @@ -303,12 +298,18 @@ 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) && @@ -316,7 +317,7 @@ static void format(struct option *op, CURLU *uh) 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); @@ -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); @@ -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); }