Skip to content

Commit

Permalink
offer {puny:host} and {puny:url} for --get
Browse files Browse the repository at this point in the history
Added two tests. Added docs.

Requires >= 7.88.0 at build time.

Fixes #64
  • Loading branch information
bagder committed Apr 26, 2023
1 parent 9de9960 commit bf96408
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 1 deletion.
6 changes: 6 additions & 0 deletions URL-QUIRKS.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,12 @@ Implemented in libcurl 7.81.0. Before this, the source formatting was kept.

Run-time requirement.

## `CURLU_PUNYCODE`

Added in libcurl 7.88.0.

Build-time requirement.

## Accepting % in host names

The host name parser has been made stricter over time, with the most recent
Expand Down
30 changes: 30 additions & 0 deletions tests.json
Original file line number Diff line number Diff line change
Expand Up @@ -1594,6 +1594,36 @@
"returncode": 0,
"stderr": ""
}
},
{
"input": {
"arguments": [
"https://räksmörgås.se",
"-g",
"{puny:url}"
]
},
"minbuildtime": "7.88.0",
"expected": {
"stderr": "",
"returncode": 0,
"stdout": "https://xn--rksmrgs-5wao1o.se:443/\n"
}
},
{
"input": {
"arguments": [
"https://räksmörgås.se",
"-g",
"{puny:host}"
]
},
"minbuildtime": "7.88.0",
"expected": {
"stderr": "",
"returncode": 0,
"stdout": "xn--rksmrgs-5wao1o.se\n"
}
}

]
5 changes: 5 additions & 0 deletions trurl.1
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,11 @@ You can access specific keys in the query string and out all values using the
format \fB{query-all:key}\fP. This looks for 'key' case sensitively and will
output all values for that key space-separated.

You can access the url and host components in their "punycoded" version, which
is how International Domain Names are converted into plain ascii, by using the
form \fB{puny:yrl}\fP and \fB{puny:host}\fP. If the host name is not using
IDN, this option provides the regular ascii name.

The "format" string supports the following backslash sequences:

\&\\\\ - backslash
Expand Down
24 changes: 23 additions & 1 deletion trurl.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
#include <curl/curl.h>
#include <curl/mprintf.h>

#include <locale.h> /* for setlocale() */

#include "version.h"

#ifdef _MSC_VER
Expand All @@ -48,6 +50,9 @@
#else
#define CURLU_ALLOW_SPACE 0
#endif
#if CURL_AT_LEAST_VERSION(7,88,0)
#define SUPPORTS_PUNYCODE
#endif

#define OUTPUT_URL 0 /* default */
#define OUTPUT_SCHEME 1
Expand Down Expand Up @@ -480,6 +485,8 @@ static void get(struct option *op, CURLU *uh)
char *cl;
size_t vlen;
bool urldecode = true;
bool punycode = false;
bool handled = true;
end = strchr(ptr, endbyte);
ptr++; /* pass the { */
if(!end) {
Expand All @@ -501,17 +508,31 @@ static void get(struct option *op, CURLU *uh)
showqkey(&ptr[10], end - cl - 1, urldecode, true);
else if(!strncmp(ptr, "query:", 6))
showqkey(&ptr[6], end - cl - 1, urldecode, false);
else if(!strncmp(ptr, "puny:", 5)) {
punycode = true;
#ifndef SUPPORTS_PUNYCODE
warnf("Built without punycode support");
#endif
ptr = cl + 1;
vlen = end - ptr;
handled = false;
}
else
errorf(ERROR_GET, "Bad --get syntax: %s", ptr);
}
else {
else
handled = false;
if(!handled) {
const struct var *v = comp2var(ptr, vlen);
if(v) {
char *nurl;
CURLUcode rc;
rc = curl_url_get(uh, v->part, &nurl,
CURLU_DEFAULT_PORT|
CURLU_NO_DEFAULT_PORT|
#ifdef SUPPORTS_PUNYCODE
(punycode?CURLU_PUNYCODE:0)|
#endif
(urldecode?CURLU_URLDECODE:0));
switch(rc) {
case CURLUE_OK:
Expand Down Expand Up @@ -1109,6 +1130,7 @@ int main(int argc, const char **argv)
struct option o;
struct curl_slist *node;
memset(&o, 0, sizeof(o));
setlocale(LC_ALL, "");
curl_global_init(CURL_GLOBAL_ALL);

for(argc--, argv++; argc > 0; argc--, argv++) {
Expand Down

0 comments on commit bf96408

Please sign in to comment.