support libidn2 and ICU as fallbacksx
authorMichael Wallner <mike@php.net>
Fri, 20 Mar 2015 07:35:10 +0000 (08:35 +0100)
committerMichael Wallner <mike@php.net>
Fri, 20 Mar 2015 07:36:10 +0000 (08:36 +0100)
config9.m4
php_http.c
php_http_url.c

index 0207513..356fb81 100644 (file)
@@ -11,7 +11,7 @@ PHP_ARG_WITH([http-libcurl-dir], [],
 PHP_ARG_WITH([http-libevent-dir], [],
 [  --with-http-libevent-dir[=DIR] HTTP: where to find libevent], $PHP_HTTP_LIBCURL_DIR, "")
 PHP_ARG_WITH([http-libidn-dir], [],
-[  --with-http-libidn-dir=[=DIR]  HTTP: where to find libidn], $PHP_HTTP_LIBCURL_DIR, "")
+[  --with-http-libidn-dir[=DIR]   HTTP: where to find libidn], $PHP_HTTP_LIBCURL_DIR, "")
 
 if test "$PHP_HTTP" != "no"; then
 
@@ -120,18 +120,70 @@ dnl ----
                        break;
                fi
        done
-       if test "x$IDNA_DIR" = "x"; then
-               AC_MSG_RESULT([not found])
-               case $host_os in
-               darwin*)
-                       AC_CHECK_HEADERS(unicode/uidna.h)
-                       PHP_CHECK_FUNC(uidna_IDNToASCII, icucore);;
-               esac
-       else
+       if test "x$IDNA_DIR" != "x"; then
                AC_MSG_RESULT([found in $IDNA_DIR])
                AC_DEFINE([PHP_HTTP_HAVE_IDN], [1], [Have libidn support])
                PHP_ADD_INCLUDE($IDNA_DIR/include)
                PHP_ADD_LIBRARY_WITH_PATH(idn, $IDNA_DIR/$PHP_LIBDIR, HTTP_SHARED_LIBADD)
+               AC_MSG_CHECKING([for libidn version])
+               IDNA_VER=$(pkg-config --version libidn 2>/dev/null || echo unknown)
+               AC_MSG_RESULT([$IDNA_VER])
+               AC_DEFINE_UNQUOTED([PHP_HTTP_LIBIDN_VERSION], "$IDNA_VER", [ ])
+       else
+               AC_MSG_RESULT([not found])
+               AC_MSG_CHECKING([for idn2.h])
+               IDNA_DIR=
+               for i in "$PHP_HTTP_LIBIDN_DIR" "$IDN_DIR" /usr/local /usr /opt; do
+                       if test -f "$i/include/idn2.h"; then
+                               IDNA_DIR=$i
+                               break;
+                       fi
+               done
+               if test "x$IDNA_DIR" != "x"; then
+                       AC_MSG_RESULT([found in $IDNA_DIR])
+                       AC_DEFINE([PHP_HTTP_HAVE_IDN2], [1], [Have libidn2 support])
+                       PHP_ADD_INCLUDE($IDNA_DIR/include)
+                       PHP_ADD_LIBRARY_WITH_PATH(idn2, $IDNA_DIR/$PHP_LIBDIR, HTTP_SHARED_LIBADD)
+                       AC_MSG_CHECKING([for libidn2 version])
+                       IDNA_VER=`$EGREP "define IDN2_VERSION " $IDNA_DIR/include/idn2.h | $SED -e's/^.*VERSION //g' -e 's/[[^0-9\.]]//g'`
+                       AC_MSG_RESULT([$IDNA_VER])
+                       AC_DEFINE_UNQUOTED([PHP_HTTP_LIBIDN2_VERSION], "$IDNA_VER", [ ])
+               else
+                       AC_MSG_RESULT([not found])
+                       AC_CHECK_HEADERS([unicode/uidna.h])
+                       case $host_os in
+                       darwin*)
+                               PHP_CHECK_FUNC(uidna_IDNToASCII, icucore);;
+                       *)
+                               AC_PATH_PROG(ICU_CONFIG, icu-config, no, [$PATH:/usr/local/bin])
+                               if test ! -x "$ICU_CONFIG"; then
+                                       ICU_CONFIG="icu-config"
+                               fi
+                               AC_MSG_CHECKING([for uidna_IDNToASCII])
+                               if ! test -x "$ICU_CONFIG"; then
+                                       ICU_CONFIG=icu-config
+                               fi
+                               if $ICU_CONFIG --exists 2>/dev/null >&2; then
+                                       save_LIBS=$LIBS
+                                       LIBS=$($ICU_CONFIG --ldflags)
+                                       AC_TRY_RUN([
+                                               #include <unicode/uidna.h>
+                                               int main(int argc, char *argv[]) {
+                                                       return uidna_IDNToASCII(0, 0, 0, 0, 0, 0, 0);
+                                               }
+                                       ], [
+                                               AC_MSG_RESULT([yes])
+                                               AC_DEFINE([HAVE_UIDNA_IDNTOASCII], [1], [ ])
+                                               LIBS=$save_LIBS
+                                               PHP_EVAL_LIBLINE(`$ICU_CONFIG --ldflags`, HTTP_SHARED_LIBADD)
+                                       ], [
+                                               LIBS=$save_LIBS
+                                               AC_MSG_RESULT([no])
+                                       ])
+                               fi
+                               ;;
+                       esac
+               fi
        fi
 
 dnl ----
index bd7baff..2ff20f1 100644 (file)
 #              endif
 #      endif
 #endif
-#if PHP_HTTP_HAVE_SERF
-#      include <serf.h>
+#if PHP_HTTP_HAVE_IDN2
+#      include <idn2.h>
+#elif PHP_HTTP_HAVE_IDN
+#      include <idna.h>
 #endif
 
 ZEND_DECLARE_MODULE_GLOBALS(php_http);
@@ -227,6 +229,12 @@ PHP_MINFO_FUNCTION(http)
        php_info_print_table_row(3, "libevent", "disabled", "disabled");
 #endif
 
+#if PHP_HTTP_HAVE_IDN2
+       php_info_print_table_row(3, "libidn2 (IDNA2008)", IDN2_VERSION, idn2_check_version(NULL));
+#elif PHP_HTTP_HAVE_IDN
+       php_info_print_table_row(3, "libidn (IDNA2003)", PHP_HTTP_LIBIDN_VERSION, "unknown");
+#endif
+
        php_info_print_table_end();
        
        DISPLAY_INI_ENTRIES();
index 35178dc..951af23 100644 (file)
@@ -12,7 +12,9 @@
 
 #include "php_http_api.h"
 
-#ifdef PHP_HTTP_HAVE_IDN
+#if PHP_HTTP_HAVE_IDN2
+#      include <idn2.h>
+#elif PHP_HTTP_HAVE_IDN
 #      include <idna.h>
 #endif
 
@@ -827,7 +829,7 @@ static ZEND_RESULT_CODE parse_userinfo(struct parse_state *state, const char *pt
 
 #if defined(PHP_WIN32) || defined(HAVE_UIDNA_IDNTOASCII)
 typedef size_t (*parse_mb_func)(unsigned *wc, const char *ptr, const char *end);
-static ZEND_RESULT_CODE to_utf16(parse_mb_func fn, const char *u8, uint16_t **u16, size_t *len)
+static ZEND_RESULT_CODE to_utf16(parse_mb_func fn, const char *u8, uint16_t **u16, size_t *len TSRMLS_DC)
 {
        size_t offset = 0, u8_len = strlen(u8);
 
@@ -870,7 +872,33 @@ static ZEND_RESULT_CODE to_utf16(parse_mb_func fn, const char *u8, uint16_t **u1
 #      define MAXHOSTNAMELEN 256
 #endif
 
-#ifdef PHP_HTTP_HAVE_IDN
+#if PHP_HTTP_HAVE_IDN2
+static ZEND_RESULT_CODE parse_idn2(struct parse_state *state, size_t prev_len)
+{
+       char *idn = NULL;
+       int rv = -1;
+       TSRMLS_FETCH_FROM_CTX(state->ts);
+
+       if (state->flags & PHP_HTTP_URL_PARSE_MBUTF8) {
+               rv = idn2_lookup_u8((const unsigned char *) state->url.host, (unsigned char **) &idn, IDN2_NFC_INPUT);
+       }
+#      ifdef PHP_HTTP_HAVE_WCHAR
+       else if (state->flags & PHP_HTTP_URL_PARSE_MBLOC) {
+               rv = idn2_lookup_ul(state->url.host, &idn, 0);
+       }
+#      endif
+       if (rv != IDN2_OK) {
+               php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed to parse IDN; %s", idn2_strerror(rv));
+               return FAILURE;
+       } else {
+               size_t idnlen = strlen(idn);
+               memcpy(state->url.host, idn, idnlen + 1);
+               free(idn);
+               state->offset += idnlen - prev_len;
+               return SUCCESS;
+       }
+}
+#elif PHP_HTTP_HAVE_IDN
 static ZEND_RESULT_CODE parse_idn(struct parse_state *state, size_t prev_len)
 {
        char *idn = NULL;
@@ -915,12 +943,12 @@ static ZEND_RESULT_CODE parse_uidn(struct parse_state *state)
        TSRMLS_FETCH_FROM_CTX(state->ts);
 
        if (state->flags & PHP_HTTP_URL_PARSE_MBUTF8) {
-               if (SUCCESS != to_utf16(parse_mb_utf8, state->url.host, &uhost_str, &uhost_len)) {
+               if (SUCCESS != to_utf16(parse_mb_utf8, state->url.host, &uhost_str, &uhost_len TSRMLS_CC)) {
                        return FAILURE;
                }
 #ifdef PHP_HTTP_HAVE_WCHAR
        } else if (state->flags & PHP_HTTP_URL_PARSE_MBLOC) {
-               if (SUCCESS != to_utf16(parse_mb_loc, state->url.host, &uhost_str, &uhost_len)) {
+               if (SUCCESS != to_utf16(parse_mb_loc, state->url.host, &uhost_str, &uhost_len TSRMLS_CC)) {
                        return FAILURE;
                }
 #endif
@@ -1106,7 +1134,9 @@ static ZEND_RESULT_CODE parse_hostinfo(struct parse_state *state, const char *pt
        }
 
        if (state->flags & PHP_HTTP_URL_PARSE_TOIDN) {
-#ifdef PHP_HTTP_HAVE_IDN
+#if PHP_HTTP_HAVE_IDN2
+               return parse_idn2(state, len);
+#elif PHP_HTTP_HAVE_IDN
                return parse_idn(state, len);
 #endif
 #ifdef HAVE_UIDNA_IDNTOASCII
@@ -1654,7 +1684,7 @@ PHP_MINIT_FUNCTION(http_url)
        zend_declare_class_constant_long(php_http_url_class_entry, ZEND_STRL("PARSE_MBLOC"), PHP_HTTP_URL_PARSE_MBLOC TSRMLS_CC);
 #endif
        zend_declare_class_constant_long(php_http_url_class_entry, ZEND_STRL("PARSE_MBUTF8"), PHP_HTTP_URL_PARSE_MBUTF8 TSRMLS_CC);
-#if defined(PHP_HTTP_HAVE_IDN) || defined(HAVE_UIDNA_IDNTOASCII)
+#if defined(PHP_HTTP_HAVE_IDN2) || defined(PHP_HTTP_HAVE_IDN) || defined(HAVE_UIDNA_IDNTOASCII)
        zend_declare_class_constant_long(php_http_url_class_entry, ZEND_STRL("PARSE_TOIDN"), PHP_HTTP_URL_PARSE_TOIDN TSRMLS_CC);
 #endif
        zend_declare_class_constant_long(php_http_url_class_entry, ZEND_STRL("PARSE_TOPCT"), PHP_HTTP_URL_PARSE_TOPCT TSRMLS_CC);