Merge branch 'R_2_3'
authorMichael Wallner <mike@php.net>
Mon, 2 Mar 2015 14:09:59 +0000 (15:09 +0100)
committerMichael Wallner <mike@php.net>
Mon, 2 Mar 2015 14:09:59 +0000 (15:09 +0100)
config9.m4
package.xml
php_http.h
php_http_url.c
php_http_utf8.h

index af3d55fd25531df5b77f6a1f8ecc07cf759f06d8..ec202b685e00c9dc70658050dbe3e7015129356b 100644 (file)
@@ -122,6 +122,11 @@ dnl ----
        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
                AC_MSG_RESULT([found in $IDNA_DIR])
                AC_DEFINE([PHP_HTTP_HAVE_IDN], [1], [Have libidn support])
index 9b2fa30dbace664441dab78b89d5bd5a77f6c9db..c406bca44af637c3c9e665f3e9fad79f0fff00e3 100644 (file)
@@ -23,10 +23,12 @@ It provides powerful request functionality with support for
 parallel requests.
 
 Documentation:
-http://devel-m6w6.rhcloud.com/mdref/http
+v1: http://php.net/http
+v2: http://devel-m6w6.rhcloud.com/mdref/http
 
 Code Coverage:
-http://dev.iworks.at/ext-http/lcov/ext/http/
+v1: http://dev.iworks.at/ext-http/lcov_html/ext/http/
+v2: http://dev.iworks.at/ext-http/lcov/ext/http/
 
 ]]></description>
  <lead>
@@ -35,14 +37,14 @@ http://dev.iworks.at/ext-http/lcov/ext/http/
   <email>mike@php.net</email>
   <active>yes</active>
  </lead>
- <date>2015-03-02</date>
+ <date>2015-03-01</date>
  <version>
-  <release>2.3.2dev</release>
-  <api>2.3.0</api>
+  <release>2.4.0dev</release>
+  <api>2.4.0</api>
  </version>
  <stability>
-  <release>stable</release>
-  <api>stable</api>
+  <release>beta</release>
+  <api>beta</api>
  </stability>
  <license>BSD, revised</license>
  <notes><![CDATA[
index aafc7f400d8635f8255a981fbb350411ef291332..baacd4b61945060475dd90e7634b3635ea18f8cb 100644 (file)
@@ -13,7 +13,7 @@
 #ifndef PHP_EXT_HTTP_H
 #define PHP_EXT_HTTP_H
 
-#define PHP_PECL_HTTP_VERSION "2.3.2dev"
+#define PHP_PECL_HTTP_VERSION "2.4.0dev"
 
 extern zend_module_entry http_module_entry;
 #define phpext_http_ptr &http_module_entry
index 6bfca2480d0b233685bc7192a48fe6c676cb5853..173bf475723daf1e729090b023354ee9ae94f9ab 100644 (file)
@@ -661,8 +661,9 @@ static size_t parse_mb_loc(unsigned *wc, const char *ptr, const char *end)
        wchar_t wchar;
        size_t consumed = 0;
 #if defined(HAVE_MBRTOWC)
-       mbstate_t ps = {0};
+       mbstate_t ps;
 
+       memset(&ps, 0, sizeof(ps));
        consumed = mbrtowc(&wchar, ptr, end - ptr, &ps);
 #elif defined(HAVE_MBTOWC)
        consumed = mbtowc(&wchar, ptr, end - ptr);
@@ -745,9 +746,15 @@ static size_t parse_mb(struct parse_state *state, parse_mb_what_t what, const ch
 
        if (!silent) {
                TSRMLS_FETCH_FROM_CTX(state->ts);
-               php_error_docref(NULL TSRMLS_CC, E_WARNING,
-                               "Failed to parse %s; unexpected byte 0x%02x at pos %u in '%s'",
-                               parse_what[what], (unsigned char) *ptr, (unsigned) (ptr - begin), begin);
+               if (consumed) {
+                       php_error_docref(NULL TSRMLS_CC, E_WARNING,
+                                       "Failed to parse %s; unexpected multibyte sequence 0x%x at pos %u in '%s'",
+                                       parse_what[what], wchar, (unsigned) (ptr - begin), begin);
+               } else {
+                       php_error_docref(NULL TSRMLS_CC, E_WARNING,
+                                       "Failed to parse %s; unexpected byte 0x%02x at pos %u in '%s'",
+                                       parse_what[what], (unsigned char) *ptr, (unsigned) (ptr - begin), begin);
+               }
        }
 
        return 0;
@@ -818,13 +825,176 @@ static STATUS parse_userinfo(struct parse_state *state, const char *ptr)
        return SUCCESS;
 }
 
+#if defined(PHP_WIN32) || defined(HAVE_UIDNA_IDNTOASCII)
+typedef size_t (*parse_mb_func)(unsigned *wc, const char *ptr, const char *end);
+static STATUS to_utf16(parse_mb_func fn, const char *u8, uint16_t **u16, size_t *len)
+{
+       size_t offset = 0, u8_len = strlen(u8);
+
+       *u16 = ecalloc(4 * sizeof(uint16_t), u8_len + 1);
+       *len = 0;
+
+       while (offset < u8_len) {
+               unsigned wc;
+               uint16_t buf[2], *ptr = buf;
+               size_t consumed = fn(&wc, &u8[offset], &u8[u8_len]);
+
+               if (!consumed) {
+                       efree(*u16);
+                       php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed to parse UTF-8 at pos %zu of '%s'", offset, u8);
+                       return FAILURE;
+               } else {
+                       offset += consumed;
+               }
+
+               switch (wctoutf16(buf, wc)) {
+               case 2:
+                       (*u16)[(*len)++] = *ptr++;
+                       /* no break */
+               case 1:
+                       (*u16)[(*len)++] = *ptr++;
+                       break;
+               case 0:
+               default:
+                       efree(*u16);
+                       php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed to convert UTF-32 'U+%X' to UTF-16", wc);
+                       return FAILURE;
+               }
+       }
+
+       return SUCCESS;
+}
+#endif
+
+#ifndef MAXHOSTNAMELEN
+#      define MAXHOSTNAMELEN 256
+#endif
+
+#ifdef PHP_HTTP_HAVE_IDN
+static STATUS parse_idn(struct parse_state *state)
+{
+       char *idn = NULL;
+       int rv = -1;
+
+       if (state->flags & PHP_HTTP_URL_PARSE_MBUTF8) {
+               rv = idna_to_ascii_8z(state->url.host, &idn, IDNA_ALLOW_UNASSIGNED|IDNA_USE_STD3_ASCII_RULES);
+       }
+#      ifdef PHP_HTTP_HAVE_WCHAR
+       else if (state->flags & PHP_HTTP_URL_PARSE_MBLOC) {
+               rv = idna_to_ascii_lz(state->url.host, &idn, IDNA_ALLOW_UNASSIGNED|IDNA_USE_STD3_ASCII_RULES);
+       }
+#      endif
+       if (rv != IDNA_SUCCESS) {
+               php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed to parse IDN; %s", idna_strerror(rv));
+               return FAILURE;
+       } else {
+               size_t idnlen = strlen(idn);
+               memcpy(state->url.host, idn, idnlen + 1);
+               free(idn);
+               state->offset += idnlen - len;
+               return SUCCESS;
+       }
+}
+#endif
+
+#ifdef HAVE_UIDNA_IDNTOASCII
+#      if HAVE_UNICODE_UIDNA_H
+#              include <unicode/uidna.h>
+#      else
+typedef uint16_t UChar;
+typedef enum { U_ZERO_ERROR = 0 } UErrorCode;
+int32_t uidna_IDNToASCII(const UChar *src, int32_t srcLength, UChar *dest, int32_t destCapacity, int32_t options, void *parseError, UErrorCode *status);
+#      endif
+static STATUS parse_uidn(struct parse_state *state)
+{
+       char *host_ptr;
+       uint16_t *uhost_str, ahost_str[MAXHOSTNAMELEN], *ahost_ptr;
+       size_t uhost_len, ahost_len;
+       UErrorCode error = U_ZERO_ERROR;
+
+       if (state->flags & PHP_HTTP_URL_PARSE_MBUTF8) {
+               if (SUCCESS != to_utf16(parse_mb_utf8, state->url.host, &uhost_str, &uhost_len)) {
+                       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)) {
+                       return FAILURE;
+               }
+#endif
+       } else {
+               php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed to parse IDN; codepage not specified");
+               return FAILURE;
+       }
+
+       ahost_len = uidna_IDNToASCII(uhost_str, uhost_len, ahost_str, MAXHOSTNAMELEN, 3, NULL, &error);
+       efree(uhost_str);
+
+       if (error != U_ZERO_ERROR) {
+               php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed to parse IDN; ICU error %d", error);
+               return FAILURE;
+       }
+
+       host_ptr = state->url.host;
+       ahost_ptr = ahost_str;
+       PHP_HTTP_DUFF(ahost_len, *host_ptr++ = *ahost_ptr++);
+
+       *host_ptr = '\0';
+       state->offset += host_ptr - state->url.host;
+
+       return SUCCESS;
+}
+#endif
+
+#if 0 && defined(PHP_WIN32)
+static STATUS parse_widn(struct parse_state *state)
+{
+       char *host_ptr;
+       uint16_t *uhost_str, ahost_str[MAXHOSTNAMELEN], *ahost_ptr;
+       size_t uhost_len;
+
+       if (state->flags & PHP_HTTP_URL_PARSE_MBUTF8) {
+               if (SUCCESS != to_utf16(parse_mb_utf8, state->url.host, &uhost_str, &uhost_len)) {
+                       php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed to parse IDN");
+                       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)) {
+                       php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed to parse IDN");
+                       return FAILURE;
+               }
+#endif
+       } else {
+               php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed to parse IDN");
+               return FAILURE;
+       }
+
+       if (!IdnToAscii(IDN_ALLOW_UNASSIGNED|IDN_USE_STD3_ASCII_RULES, uhost_str, uhost_len, ahost_str, MAXHOSTNAMELEN)) {
+               efree(uhost_str);
+               php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed to parse IDN");
+               return FAILURE;
+       }
+
+       efree(uhost_str);
+       host_ptr = state->url.host;
+       ahost_ptr = ahost_str;
+       PHP_HTTP_DUFF(wcslen(ahost_str), *host_ptr++ = *ahost_ptr++);
+       efree(ahost_str);
+
+       *host_ptr = '\0';
+       state->offset += host_ptr - state->url.host;
+
+       return SUCCESS;
+}
+#endif
+
 static STATUS parse_hostinfo(struct parse_state *state, const char *ptr)
 {
        size_t mb, len;
        const char *end = state->ptr, *tmp = ptr, *port = NULL;
        TSRMLS_FETCH_FROM_CTX(state->ts);
 
-
 #ifdef HAVE_INET_PTON
        if (*ptr == '[') {
                char *error = NULL, *tmp = memchr(ptr, ']', end - ptr);
@@ -932,30 +1102,14 @@ static STATUS parse_hostinfo(struct parse_state *state, const char *ptr)
                state->buffer[state->offset++] = 0;
        }
 
-#ifdef PHP_HTTP_HAVE_IDN
        if (state->flags & PHP_HTTP_URL_PARSE_TOIDN) {
-               char *idn = NULL;
-               int rv = -1;
-
-               if (state->flags & PHP_HTTP_URL_PARSE_MBUTF8) {
-                       rv = idna_to_ascii_8z(state->url.host, &idn, IDNA_ALLOW_UNASSIGNED|IDNA_USE_STD3_ASCII_RULES);
-               }
-#      ifdef PHP_HTTP_HAVE_WCHAR
-               else if (state->flags & PHP_HTTP_URL_PARSE_MBLOC) {
-                       rv = idna_to_ascii_lz(state->url.host, &idn, IDNA_ALLOW_UNASSIGNED|IDNA_USE_STD3_ASCII_RULES);
-               }
-#      endif
-               if (rv != IDNA_SUCCESS) {
-                       php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed to parse IDN; %s", idna_strerror(rv));
-                       return FAILURE;
-               } else {
-                       size_t idnlen = strlen(idn);
-                       memcpy(state->url.host, idn, idnlen + 1);
-                       free(idn);
-                       state->offset += idnlen - len;
-               }
-       }
+#ifdef PHP_HTTP_HAVE_IDN
+               return parse_idn(state);
 #endif
+#ifdef HAVE_UIDNA_IDNTOASCII
+               return parse_uidn(state);
+#endif
+       }
 
        return SUCCESS;
 }
@@ -1494,7 +1648,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);
-#ifdef PHP_HTTP_HAVE_IDN
+#if 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);
index 2503f0644e605db64aae577b6523e641b3e54098..f9008032dff0eea98abc8c59cb1134b35ab3b7c3 100644 (file)
@@ -625,6 +625,39 @@ static inline zend_bool isualnum(unsigned ch)
        return isualpha(ch);
 }
 
+static inline size_t wctoutf16(unsigned short u16[2], unsigned wc)
+{
+       if (wc > 0x10ffff || (wc >= 0xd800 && wc <= 0xdfff)) {
+               return 0;
+       }
+
+       if (wc <= 0xffff) {
+               u16[0] = (unsigned short) wc;
+               return 1;
+       }
+
+       wc -= 0x10000;
+       u16[0] = (unsigned short) ((wc >> 10) + 0xd800);
+       u16[1] = (unsigned short) ((wc & 0x3ff) + 0xdc00);
+       return 2;
+}
+
+static inline size_t utf16towc(unsigned *wc, unsigned short *u16_str, size_t u16_len)
+{
+       if (u16_len < 1) {
+               return 0;
+       }
+       if (u16_str[0] - 0xd800 >= 0x800) {
+               *wc = u16_str[0];
+               return 1;
+       }
+       if (u16_len < 2 || (u16_str[0] & 0xfffffc00) != 0xd800 || (u16_str[1] & 0xfffffc00) != 0xdc00) {
+               return 0;
+       }
+       *wc = (u16_str[0] << 10) + u16_str[1] - 0x35fdc00;
+       return 2;
+}
+
 #endif /* PHP_HTTP_UTF8_H */
 
 /*