Merge branch 'v2.6.x'
[m6w6/ext-http] / src / php_http_url.c
index c5c19a55568bd6fd10ee8f310fb5a247d0e72030..8352608982e4ab769dfeccbdea4ae01f47d58f1d 100644 (file)
@@ -263,6 +263,7 @@ php_http_url_t *php_http_url_mod(const php_http_url_t *old_url, const php_http_u
 
        /* replace directory references if path is not a single slash */
        if ((flags & PHP_HTTP_URL_SANITIZE_PATH)
+       &&      url(buf)->path
        &&      url(buf)->path[0] && url(buf)->path[1]) {
                char *ptr, *end = url(buf)->path + strlen(url(buf)->path) + 1;
                        
@@ -1029,7 +1030,6 @@ static ZEND_RESULT_CODE parse_widn(struct parse_state *state)
 #ifdef HAVE_INET_PTON
 static const char *parse_ip6(struct parse_state *state, const char *ptr)
 {
-       size_t mb, len;
        const char *error = NULL, *end = state->ptr, *tmp = memchr(ptr, ']', end - ptr);
 
        if (tmp) {
@@ -1099,9 +1099,7 @@ static ZEND_RESULT_CODE parse_hostinfo(struct parse_state *state, const char *pt
                        state->buffer[state->offset++] = *ptr;
                        break;
 
-               case '!': case '$': case '&': case '\'': case '(': case ')': case '*':
-               case '+': case ',': case ';': case '=': /* sub-delims */
-               case '-': case '.': case '_': case '~': /* unreserved */
+               case '.':
                        if (port || !label) {
                                /* sort of a compromise, just ensure we don't end up
                                 * with a dot at the beginning or two consecutive dots
@@ -1116,6 +1114,21 @@ static ZEND_RESULT_CODE parse_hostinfo(struct parse_state *state, const char *pt
                        label = NULL;
                        break;
 
+               case '-':
+                       if (!label) {
+                               /* sort of a compromise, just ensure we don't end up
+                                * with a hyphen at the beginning
+                                */
+                               php_error_docref(NULL TSRMLS_CC, E_WARNING,
+                                               "Failed to parse %s; unexpected '%c' at pos %u in '%s'",
+                                               port ? "port" : "host",
+                                               (unsigned char) *ptr, (unsigned) (ptr - tmp), tmp);
+                               return FAILURE;
+                       }
+                       /* no break */
+               case '_': case '~': /* unreserved */
+               case '!': case '$': case '&': case '\'': case '(': case ')': case '*':
+               case '+': case ',': case ';': case '=': /* sub-delims */
                case 'A': case 'B': case 'C': case 'D': case 'E': case 'F': case 'G':
                case 'H': case 'I': case 'J': case 'K': case 'L': case 'M': case 'N':
                case 'O': case 'P': case 'Q': case 'R': case 'S': case 'T': case 'U':
@@ -1468,7 +1481,7 @@ static const char *parse_scheme(struct parse_state *state)
                case '7': case '8': case '9':
                case '+': case '-': case '.':
                        if (state->ptr == tmp) {
-                               return tmp;
+                               goto softfail;
                        }
                        /* no break */
                case 'A': case 'B': case 'C': case 'D': case 'E': case 'F': case 'G':
@@ -1485,19 +1498,20 @@ static const char *parse_scheme(struct parse_state *state)
 
                default:
                        if (!(mb = parse_mb(state, PARSE_SCHEME, state->ptr, state->end, tmp, 1))) {
-                               /* soft fail; parse path next */
-                               return tmp;
+                               goto softfail;
                        }
                        state->ptr += mb - 1;
                }
        } while (++state->ptr != state->end);
 
+softfail:
+       state->offset = 0;
        return state->ptr = tmp;
 }
 
 php_http_url_t *php_http_url_parse(const char *str, size_t len, unsigned flags)
 {
-       size_t maxlen = 3 * len;
+       size_t maxlen = 3 * len + 8 /* null bytes for all components */;
        struct parse_state *state = ecalloc(1, sizeof(*state) + maxlen);
 
        state->end = str + len;
@@ -1557,6 +1571,19 @@ php_http_url_t *php_http_url_parse_authority(const char *str, size_t len, unsign
        return (php_http_url_t *) state;
 }
 
+static zend_class_entry *php_http_url_class_entry;
+static zend_class_entry *php_http_env_url_class_entry;
+
+zend_class_entry *php_http_url_get_class_entry(void)
+{
+       return php_http_url_class_entry;
+}
+
+zend_class_entry *php_http_get_env_url_class_entry(void)
+{
+       return php_http_env_url_class_entry;
+}
+
 ZEND_BEGIN_ARG_INFO_EX(ai_HttpUrl___construct, 0, 0, 0)
        ZEND_ARG_INFO(0, old_url)
        ZEND_ARG_INFO(0, new_url)
@@ -1565,12 +1592,17 @@ ZEND_END_ARG_INFO();
 PHP_METHOD(HttpUrl, __construct)
 {
        zval *new_url = NULL, *old_url = NULL;
-       zend_long flags = PHP_HTTP_URL_FROM_ENV;
+       zend_long flags = 0;
        zend_error_handling zeh;
 
        php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS(), "|z!z!l", &old_url, &new_url, &flags), invalid_arg, return);
 
-       zend_replace_error_handling(EH_THROW, php_http_exception_bad_url_class_entry, &zeh);
+       /* always set http\Url::FROM_ENV for instances of http\Env\Url */
+       if (instanceof_function(Z_OBJCE_P(getThis()), php_http_env_url_class_entry)) {
+               flags |= PHP_HTTP_URL_FROM_ENV;
+       }
+
+       zend_replace_error_handling(EH_THROW, php_http_get_exception_bad_url_class_entry(), &zeh);
        {
                php_http_url_t *res_purl, *new_purl = NULL, *old_purl = NULL;
 
@@ -1618,7 +1650,7 @@ PHP_METHOD(HttpUrl, mod)
 
        php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS(), "z!|l", &new_url, &flags), invalid_arg, return);
 
-       zend_replace_error_handling(EH_THROW, php_http_exception_bad_url_class_entry, &zeh);
+       zend_replace_error_handling(EH_THROW, php_http_get_exception_bad_url_class_entry(), &zeh);
        {
                php_http_url_t *new_purl = NULL, *old_purl = NULL;
 
@@ -1692,8 +1724,6 @@ static zend_function_entry php_http_url_methods[] = {
        EMPTY_FUNCTION_ENTRY
 };
 
-zend_class_entry *php_http_url_class_entry;
-
 PHP_MINIT_FUNCTION(http_url)
 {
        zend_class_entry ce = {0};
@@ -1733,6 +1763,9 @@ PHP_MINIT_FUNCTION(http_url)
 #endif
        zend_declare_class_constant_long(php_http_url_class_entry, ZEND_STRL("PARSE_TOPCT"), PHP_HTTP_URL_PARSE_TOPCT);
 
+       INIT_NS_CLASS_ENTRY(ce, "http\\Env", "Url", php_http_url_methods);
+       php_http_env_url_class_entry = zend_register_internal_class_ex(&ce, php_http_url_class_entry);
+
        return SUCCESS;
 }