Use icu library for IDN feature
[platform/upstream/curl.git] / lib / url.c
index 8692e68..97f5d86 100644 (file)
--- a/lib/url.c
+++ b/lib/url.c
@@ -65,8 +65,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_GLIB2_HOSTNAME)
-#include <glib.h>
+#elif defined(USE_ICU_IDNA)
+#include <unicode/uidna.h>
 #endif  /* USE_LIBIDN2 */
 
 #include "urldata.h"
@@ -126,6 +126,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 */
+
 /* Local static prototypes */
 static struct connectdata *
 find_oldest_idle_connection_in_bundle(struct Curl_easy *data,
@@ -4074,15 +4078,24 @@ static void fix_hostname(struct connectdata *conn, struct hostname *host)
     }
     else
       infof(data, "Failed to convert %s to ACE;\n", host->name);
-#elif defined(USE_GLIB2_HOSTNAME)
-    gchar *ace_hostname = NULL;
-    ace_hostname = g_hostname_to_ascii(host->name);
-    if(ace_hostname) {
-      host->encalloc = (char *)ace_hostname;
+#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, "Failed to convert %s to ACE;\n", host->name);
 #else
     infof(data, "IDN support not present, can't parse Unicode domains\n");
 #endif
@@ -4104,6 +4117,9 @@ static void free_fixed_hostname(struct hostname *host)
   free(host->encalloc); /* must be freed withidn_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