Use icu library for IDN feature
[platform/upstream/curl.git] / lib / url.c
index 945d4e3..03c2057 100644 (file)
--- a/lib/url.c
+++ b/lib/url.c
@@ -63,6 +63,8 @@
 #elif defined(USE_WIN32_IDN)
 /* prototype for curl_win32_idn_to_ascii() */
 bool curl_win32_idn_to_ascii(const char *in, char **out);
+#elif defined(USE_ICU_IDNA)
+#include <unicode/uidna.h>
 #endif  /* USE_LIBIDN2 */
 
 #include "urldata.h"
@@ -125,6 +127,10 @@ bool curl_win32_idn_to_ascii(const char *in, char **out);
 #include "curl_memory.h"
 #include "memdebug.h"
 
+#if defined(USE_ICU_IDNA)
+#define MAX_DOMAIN_NAME_LEN 256
+#endif  /* USE_ICU_IDNA */
+
 static void conn_free(struct connectdata *conn);
 static void free_fixed_hostname(struct hostname *host);
 static void signalPipeClose(struct curl_llist *pipeline, bool pipe_broke);
@@ -1731,6 +1737,23 @@ static CURLcode fix_hostname(struct connectdata *conn, struct hostname *host)
     else {
       failf(data, "Failed to convert %s to ACE;\n", host->name);
       return CURLE_URL_MALFORMAT;
+#elif defined(USE_ICU_IDNA)
+    char *ace_hostname = malloc(MAX_DOMAIN_NAME_LEN * sizeof(char));
+    UErrorCode errorCode = U_ZERO_ERROR;
+    UIDNAInfo info = UIDNA_INFO_INITIALIZER;
+    UIDNA *uts46 =
+      uidna_openUTS46(UIDNA_USE_STD3_RULES|UIDNA_NONTRANSITIONAL_TO_UNICODE,
+      &errorCode);
+    int32_t length = uidna_nameToASCII_UTF8(uts46,
+      host->name, strlen(host->name),
+      ace_hostname, MAX_DOMAIN_NAME_LEN, &info, &errorCode);
+    uidna_close(uts46);
+
+    if(errorCode != U_ZERO_ERROR || info.errors || length < 1)
+      infof(data, "Failed to convert %s to ACE;\n", host->name);
+    else {
+      host->encalloc = ace_hostname;
+      host->name = host->encalloc;
     }
 #else
     infof(data, "IDN support not present, can't parse Unicode domains\n");
@@ -1763,6 +1786,9 @@ static void free_fixed_hostname(struct hostname *host)
   free(host->encalloc); /* must be freed with free() since this was
                            allocated by curl_win32_idn_to_ascii */
   host->encalloc = NULL;
+#elif defined(USE_ICU_IDNA)
+  free(host->encalloc);
+  host->encalloc = NULL;
 #else
   (void)host;
 #endif