Skip to content

Commit 023c323

Browse files
committed
trurl: URL decode get components by default
Unless written like {:component} with a leading colon. Closes #47
1 parent 829ec82 commit 023c323

File tree

2 files changed

+15
-17
lines changed

2 files changed

+15
-17
lines changed

trurl.1

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -63,15 +63,13 @@ can be output when specified as \fB{component}\fP, with the name of the part
6363
within curly braces. The following component names are available: {url},
6464
{scheme}, {user}, {password}, {options}, {host}, {port}, {path}, {query},
6565
{fragment} and {zoneid}.
66+
67+
All components are shown URL decoded by default. If you instead write the
68+
component prefixed with a colon like "{:path}", you get it output URL encoded.
6669
.IP "--json"
6770
Outputs all components of the URL as a JSON object. All components of the URL
6871
that has data will get populated in the object with using their component
6972
names.
70-
.SH "MODIFIERS"
71-
.IP "--urldecode"
72-
By default each component is output using the format of the input (URL
73-
encoded). This option makes trurl instead provide it URL decoded. Note that
74-
%20 will then output space and %09 a tab etc.
7573
.SH EXAMPLES
7674
.IP "Replace the host name of a URL"
7775
.nf

trurl.c

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,6 @@ static void help(void)
105105
" OUTPUT\n"
106106
" -g, --get [{component}s] - output URL component(s)\n"
107107
" --json - output URL info as JSON\n"
108-
" MODIFIERS\n"
109-
" --urldecode - URL decode the output\n"
110108
" URL COMPONENTS:\n"
111109
" "
112110
);
@@ -134,7 +132,6 @@ struct option {
134132
const char *format;
135133
FILE *url;
136134
bool urlopen;
137-
bool urldecode;
138135
bool jsonout;
139136
unsigned char output;
140137
};
@@ -276,16 +273,14 @@ static int getarg(struct option *op,
276273
op->format = arg;
277274
*usedarg = 1;
278275
}
279-
else if(!strcmp("--urldecode", flag))
280-
op->urldecode = true;
281276
else if(!strcmp("--json", flag))
282277
op->jsonout = true;
283278
else
284279
return 1; /* unrecognized option */
285280
return 0;
286281
}
287282

288-
static void format(struct option *op, CURLU *uh)
283+
static void get(struct option *op, CURLU *uh)
289284
{
290285
FILE *stream = stdout;
291286
const char *ptr = op->format;
@@ -303,20 +298,26 @@ static void format(struct option *op, CURLU *uh)
303298
char *end;
304299
size_t vlen;
305300
int i;
301+
bool urldecode = true;
306302
end = strchr(ptr, '}');
307303
ptr++; /* pass the { */
308304
if(!end) {
309305
/* syntax error */
310306
continue;
311307
}
308+
/* {path} {:path} */
309+
if(*ptr == ':') {
310+
urldecode = false;
311+
ptr++;
312+
}
312313
vlen = end - ptr;
313314
for(i = 0; variables[i].name; i++) {
314315
if((strlen(variables[i].name) == vlen) &&
315316
!strncasecmp(ptr, variables[i].name, vlen)) {
316317
char *nurl;
317318
CURLUcode rc;
318319
rc = curl_url_get(uh, variables[i].part, &nurl, CURLU_DEFAULT_PORT|
319-
(op->urldecode?CURLU_URLDECODE:0));
320+
(urldecode?CURLU_URLDECODE:0));
320321
switch(rc) {
321322
case CURLUE_OK:
322323
fprintf(stream, "%s", nurl);
@@ -458,13 +459,13 @@ static void jsonString(FILE *stream, const char *in, bool lowercase)
458459
static void json(struct option *o, CURLU *uh)
459460
{
460461
int i;
461-
462+
(void)o;
462463
fputs("{\n", stdout);
463464
for(i = 0; variables[i].name; i++) {
464465
char *nurl;
465466
CURLUcode rc = curl_url_get(uh, variables[i].part, &nurl,
466467
(i?CURLU_DEFAULT_PORT:0)|
467-
(o->urldecode?CURLU_URLDECODE:0));
468+
CURLU_URLDECODE);
468469
if(!rc) {
469470
printf(" \"%s\": ", variables[i].name);
470471
jsonString(stdout, nurl, false);
@@ -543,13 +544,12 @@ static void singleurl(struct option *o,
543544
json(o, uh);
544545
else if(o->format) {
545546
/* custom output format */
546-
format(o, uh);
547+
get(o, uh);
547548
}
548549
else {
549550
/* default output is full URL */
550551
char *nurl = NULL;
551-
if(!curl_url_get(uh, CURLUPART_URL, &nurl,
552-
o->urldecode?CURLU_URLDECODE:0)) {
552+
if(!curl_url_get(uh, CURLUPART_URL, &nurl, 0)) {
553553
printf("%s\n", nurl);
554554
curl_free(nurl);
555555
}

0 commit comments

Comments
 (0)