X-Git-Url: https://git.m6w6.name/?p=m6w6%2Fext-http;a=blobdiff_plain;f=src%2Fphp_http_url.c;h=2332fb55b8382572312dc9382c091bc4c15b67c2;hp=3ed2e3cbf2fd6beeee72a41ca50345b957efbdf6;hb=b2be5b163e896bb9cbb3f28314551d0debf8408d;hpb=e57370d1f00da9e12de0917a9f907281ab8e84b5 diff --git a/src/php_http_url.c b/src/php_http_url.c index 3ed2e3c..2332fb5 100644 --- a/src/php_http_url.c +++ b/src/php_http_url.c @@ -503,7 +503,7 @@ php_http_url_t *php_http_url_from_struct(HashTable *ht) HashTable *php_http_url_to_struct(const php_http_url_t *url, zval *strct) { - HashTable *ht; + HashTable *ht = NULL; zval tmp; if (strct) { @@ -523,8 +523,8 @@ HashTable *php_http_url_to_struct(const php_http_url_t *url, zval *strct) } #define url_struct_add(part) \ - if (Z_TYPE_P(strct) == IS_ARRAY) { \ - zend_hash_str_update(Z_ARRVAL_P(strct), part, lenof(part), &tmp); \ + if (!strct || Z_TYPE_P(strct) == IS_ARRAY) { \ + zend_hash_str_update(ht, part, lenof(part), &tmp); \ } else { \ zend_update_property(Z_OBJCE_P(strct), strct, part, lenof(part), &tmp); \ zval_ptr_dtor(&tmp); \ @@ -785,10 +785,16 @@ static ZEND_RESULT_CODE parse_userinfo(struct parse_state *state, const char *pt switch (*ptr) { case ':': if (password) { - php_error_docref(NULL, E_WARNING, - "Failed to parse password; duplicate ':' at pos %u in '%s'", - (unsigned) (ptr - tmp), tmp); - return FAILURE; + if (!(state->flags & PHP_HTTP_URL_SILENT_ERRORS)) { + php_error_docref(NULL, E_WARNING, + "Failed to parse password; duplicate ':' at pos %u in '%s'", + (unsigned) (ptr - tmp), tmp); + } + if (!(state->flags & PHP_HTTP_URL_IGNORE_ERRORS)) { + return FAILURE; + } + state->buffer[state->offset++] = *ptr; + break; } password = ptr + 1; state->buffer[state->offset++] = 0; @@ -797,16 +803,31 @@ static ZEND_RESULT_CODE parse_userinfo(struct parse_state *state, const char *pt case '%': if (ptr[1] != '%' && (end - ptr <= 2 || !isxdigit(*(ptr+1)) || !isxdigit(*(ptr+2)))) { - php_error_docref(NULL, E_WARNING, - "Failed to parse userinfo; invalid percent encoding at pos %u in '%s'", - (unsigned) (ptr - tmp), tmp); - return FAILURE; + if (!(state->flags & PHP_HTTP_URL_SILENT_ERRORS)) { + php_error_docref(NULL, E_WARNING, + "Failed to parse userinfo; invalid percent encoding at pos %u in '%s'", + (unsigned) (ptr - tmp), tmp); + } + if (!(state->flags & PHP_HTTP_URL_IGNORE_ERRORS)) { + return FAILURE; + } + state->buffer[state->offset++] = *ptr++; + break; } state->buffer[state->offset++] = *ptr++; state->buffer[state->offset++] = *ptr++; state->buffer[state->offset++] = *ptr; break; + default: + if ((mb = parse_mb(state, PARSE_USERINFO, ptr, end, tmp, state->flags & PHP_HTTP_URL_SILENT_ERRORS))) { + ptr += mb - 1; + break; + } + if (!(state->flags & PHP_HTTP_URL_IGNORE_ERRORS)) { + return FAILURE; + } + /* no break */ case '!': case '$': case '&': case '\'': case '(': case ')': case '*': case '+': case ',': case ';': case '=': /* sub-delims */ case '-': case '.': case '_': case '~': /* unreserved */ @@ -824,11 +845,6 @@ static ZEND_RESULT_CODE parse_userinfo(struct parse_state *state, const char *pt state->buffer[state->offset++] = *ptr; break; - default: - if (!(mb = parse_mb(state, PARSE_USERINFO, ptr, end, tmp, 0))) { - return FAILURE; - } - ptr += mb - 1; } } while(++ptr != end); @@ -898,15 +914,19 @@ static ZEND_RESULT_CODE parse_idn2(struct parse_state *state, size_t prev_len) } # endif if (rv != IDN2_OK) { - php_error_docref(NULL, E_WARNING, "Failed to parse IDN; %s", idn2_strerror(rv)); - return FAILURE; + if (!(state->flags & PHP_HTTP_URL_SILENT_ERRORS)) { + php_error_docref(NULL, E_WARNING, "Failed to parse IDN; %s", idn2_strerror(rv)); + } + if (!(state->flags & PHP_HTTP_URL_IGNORE_ERRORS)) { + return FAILURE; + } } else { size_t idnlen = strlen(idn); memcpy(state->url.host, idn, idnlen + 1); free(idn); state->offset += idnlen - prev_len; - return SUCCESS; } + return SUCCESS; } #elif PHP_HTTP_HAVE_IDN static ZEND_RESULT_CODE parse_idn(struct parse_state *state, size_t prev_len) @@ -923,15 +943,19 @@ static ZEND_RESULT_CODE parse_idn(struct parse_state *state, size_t prev_len) } # endif if (rv != IDNA_SUCCESS) { - php_error_docref(NULL, E_WARNING, "Failed to parse IDN; %s", idna_strerror(rv)); - return FAILURE; + if (!(state->flags & PHP_HTTP_URL_SILENT_ERRORS)) { + php_error_docref(NULL, E_WARNING, "Failed to parse IDN; %s", idna_strerror(rv)); + } + if (!(state->flags & PHP_HTTP_URL_IGNORE_ERRORS)) { + return FAILURE; + } } else { size_t idnlen = strlen(idn); memcpy(state->url.host, idn, idnlen + 1); free(idn); state->offset += idnlen - prev_len; - return SUCCESS; } + return SUCCESS; } #endif @@ -1030,6 +1054,7 @@ 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) { + unsigned pos = 0; const char *error = NULL, *end = state->ptr, *tmp = memchr(ptr, ']', end - ptr); if (tmp) { @@ -1046,17 +1071,21 @@ static const char *parse_ip6(struct parse_state *state, const char *ptr) state->buffer[state->offset++] = 0; ptr = tmp + 1; } else if (rv == -1) { + pos = 1; error = strerror(errno); } else { error = "unexpected '['"; } efree(addr); } else { + pos = end - ptr; error = "expected ']'"; } if (error) { - php_error_docref(NULL, E_WARNING, "Failed to parse hostinfo; %s", error); + if (!(state->flags & PHP_HTTP_URL_SILENT_ERRORS)) { + php_error_docref(NULL, E_WARNING, "Failed to parse hostinfo; %s at pos %u in '%s'", error, pos, ptr); + } return NULL; } @@ -1066,12 +1095,15 @@ static const char *parse_ip6(struct parse_state *state, const char *ptr) static ZEND_RESULT_CODE parse_hostinfo(struct parse_state *state, const char *ptr) { - size_t mb, len; + size_t mb, len = state->offset; const char *end = state->ptr, *tmp = ptr, *port = NULL, *label = NULL; #ifdef HAVE_INET_PTON if (*ptr == '[' && !(ptr = parse_ip6(state, ptr))) { - return FAILURE; + if (!(state->flags & PHP_HTTP_URL_IGNORE_ERRORS)) { + return FAILURE; + } + ptr = tmp; } #endif @@ -1079,43 +1111,76 @@ static ZEND_RESULT_CODE parse_hostinfo(struct parse_state *state, const char *pt switch (*ptr) { case ':': if (port) { - php_error_docref(NULL, E_WARNING, - "Failed to parse port; unexpected ':' at pos %u in '%s'", - (unsigned) (ptr - tmp), tmp); - return FAILURE; + if (!(state->flags & PHP_HTTP_URL_SILENT_ERRORS)) { + php_error_docref(NULL, E_WARNING, + "Failed to parse port; unexpected ':' at pos %u in '%s'", + (unsigned) (ptr - tmp), tmp); + } + if (!(state->flags & PHP_HTTP_URL_IGNORE_ERRORS)) { + return FAILURE; + } } port = ptr + 1; break; case '%': if (ptr[1] != '%' && (end - ptr <= 2 || !isxdigit(*(ptr+1)) || !isxdigit(*(ptr+2)))) { - php_error_docref(NULL, E_WARNING, - "Failed to parse hostinfo; invalid percent encoding at pos %u in '%s'", - (unsigned) (ptr - tmp), tmp); - return FAILURE; + if (!(state->flags & PHP_HTTP_URL_SILENT_ERRORS)) { + php_error_docref(NULL, E_WARNING, + "Failed to parse hostinfo; invalid percent encoding at pos %u in '%s'", + (unsigned) (ptr - tmp), tmp); + } + if (!(state->flags & PHP_HTTP_URL_IGNORE_ERRORS)) { + return FAILURE; + } + state->buffer[state->offset++] = *ptr++; + break; } state->buffer[state->offset++] = *ptr++; state->buffer[state->offset++] = *ptr++; 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 */ - php_error_docref(NULL, 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; + if (!(state->flags & PHP_HTTP_URL_SILENT_ERRORS)) { + php_error_docref(NULL, E_WARNING, + "Failed to parse %s; unexpected '%c' at pos %u in '%s'", + port ? "port" : "host", + (unsigned char) *ptr, (unsigned) (ptr - tmp), tmp); + } + if (!(state->flags & PHP_HTTP_URL_IGNORE_ERRORS)) { + return FAILURE; + } + break; } state->buffer[state->offset++] = *ptr; label = NULL; break; + case '-': + if (!label) { + /* sort of a compromise, just ensure we don't end up + * with a hyphen at the beginning + */ + if (!(state->flags & PHP_HTTP_URL_SILENT_ERRORS)) { + php_error_docref(NULL, E_WARNING, + "Failed to parse %s; unexpected '%c' at pos %u in '%s'", + port ? "port" : "host", + (unsigned char) *ptr, (unsigned) (ptr - tmp), tmp); + } + if (!(state->flags & PHP_HTTP_URL_IGNORE_ERRORS)) { + return FAILURE; + } + break; + } + /* 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': @@ -1125,10 +1190,15 @@ static ZEND_RESULT_CODE parse_hostinfo(struct parse_state *state, const char *pt case 'o': case 'p': case 'q': case 'r': case 's': case 't': case 'u': case 'v': case 'w': case 'x': case 'y': case 'z': if (port) { - php_error_docref(NULL, E_WARNING, - "Failed to parse port; unexpected char '%c' at pos %u in '%s'", - (unsigned char) *ptr, (unsigned) (ptr - tmp), tmp); - return FAILURE; + if (!(state->flags & PHP_HTTP_URL_SILENT_ERRORS)) { + php_error_docref(NULL, E_WARNING, + "Failed to parse port; unexpected char '%c' at pos %u in '%s'", + (unsigned char) *ptr, (unsigned) (ptr - tmp), tmp); + } + if (!(state->flags & PHP_HTTP_URL_IGNORE_ERRORS)) { + return FAILURE; + } + break; } /* no break */ case '0': case '1': case '2': case '3': case '4': case '5': case '6': @@ -1147,12 +1217,20 @@ static ZEND_RESULT_CODE parse_hostinfo(struct parse_state *state, const char *pt if (ptr == end) { break; } else if (port) { - php_error_docref(NULL, E_WARNING, - "Failed to parse port; unexpected byte 0x%02x at pos %u in '%s'", - (unsigned char) *ptr, (unsigned) (ptr - tmp), tmp); - return FAILURE; - } else if (!(mb = parse_mb(state, PARSE_HOSTINFO, ptr, end, tmp, 0))) { - return FAILURE; + if (!(state->flags & PHP_HTTP_URL_SILENT_ERRORS)) { + php_error_docref(NULL, E_WARNING, + "Failed to parse port; unexpected byte 0x%02x at pos %u in '%s'", + (unsigned char) *ptr, (unsigned) (ptr - tmp), tmp); + } + if (!(state->flags & PHP_HTTP_URL_IGNORE_ERRORS)) { + return FAILURE; + } + break; + } else if (!(mb = parse_mb(state, PARSE_HOSTINFO, ptr, end, tmp, state->flags & PHP_HTTP_URL_SILENT_ERRORS))) { + if (!(state->flags & PHP_HTTP_URL_IGNORE_ERRORS)) { + return FAILURE; + } + break; } label = ptr; ptr += mb - 1; @@ -1160,7 +1238,7 @@ static ZEND_RESULT_CODE parse_hostinfo(struct parse_state *state, const char *pt } while (++ptr != end); if (!state->url.host) { - len = (port ? port - tmp - 1 : end - tmp); + len = state->offset - len; state->url.host = &state->buffer[state->offset - len]; state->buffer[state->offset++] = 0; } @@ -1191,9 +1269,14 @@ static const char *parse_authority(struct parse_state *state) case '@': /* userinfo delimiter */ if (host) { - php_error_docref(NULL, E_WARNING, - "Failed to parse userinfo; unexpected '@'"); - return NULL; + if (!(state->flags & PHP_HTTP_URL_SILENT_ERRORS)) { + php_error_docref(NULL, E_WARNING, + "Failed to parse userinfo; unexpected '@'"); + } + if (!(state->flags & PHP_HTTP_URL_IGNORE_ERRORS)) { + return NULL; + } + break; } host = state->ptr + 1; if (tmp != state->ptr && SUCCESS != parse_userinfo(state, tmp)) { @@ -1239,10 +1322,16 @@ static const char *parse_path(struct parse_state *state) case '%': if (state->ptr[1] != '%' && (state->end - state->ptr <= 2 || !isxdigit(*(state->ptr+1)) || !isxdigit(*(state->ptr+2)))) { - php_error_docref(NULL, E_WARNING, - "Failed to parse path; invalid percent encoding at pos %u in '%s'", - (unsigned) (state->ptr - tmp), tmp); - return NULL; + if (!(state->flags & PHP_HTTP_URL_SILENT_ERRORS)) { + php_error_docref(NULL, E_WARNING, + "Failed to parse path; invalid percent encoding at pos %u in '%s'", + (unsigned) (state->ptr - tmp), tmp); + } + if (!(state->flags & PHP_HTTP_URL_IGNORE_ERRORS)) { + return NULL; + } + state->buffer[state->offset++] = *state->ptr; + break; } state->buffer[state->offset++] = *state->ptr++; state->buffer[state->offset++] = *state->ptr++; @@ -1269,8 +1358,11 @@ static const char *parse_path(struct parse_state *state) break; default: - if (!(mb = parse_mb(state, PARSE_PATH, state->ptr, state->end, tmp, 0))) { - return NULL; + if (!(mb = parse_mb(state, PARSE_PATH, state->ptr, state->end, tmp, state->flags & PHP_HTTP_URL_SILENT_ERRORS))) { + if (!(state->flags & PHP_HTTP_URL_IGNORE_ERRORS)) { + return NULL; + } + break; } state->ptr += mb - 1; } @@ -1307,21 +1399,27 @@ static const char *parse_query(struct parse_state *state) case '%': if (state->ptr[1] != '%' && (state->end - state->ptr <= 2 || !isxdigit(*(state->ptr+1)) || !isxdigit(*(state->ptr+2)))) { - php_error_docref(NULL, E_WARNING, - "Failed to parse query; invalid percent encoding at pos %u in '%s'", - (unsigned) (state->ptr - tmp), tmp); - return NULL; + if (!(state->flags & PHP_HTTP_URL_SILENT_ERRORS)) { + php_error_docref(NULL, E_WARNING, + "Failed to parse query; invalid percent encoding at pos %u in '%s'", + (unsigned) (state->ptr - tmp), tmp); + } + if (!(state->flags & PHP_HTTP_URL_IGNORE_ERRORS)) { + return NULL; + } + /* fallthrough, pct-encode the percent sign */ + } else { + state->buffer[state->offset++] = *state->ptr++; + state->buffer[state->offset++] = *state->ptr++; + state->buffer[state->offset++] = *state->ptr; + break; } - state->buffer[state->offset++] = *state->ptr++; - state->buffer[state->offset++] = *state->ptr++; - state->buffer[state->offset++] = *state->ptr; - break; - - /* RFC1738 unsafe */ + /* no break */ case '{': case '}': case '<': case '>': case '[': case ']': case '|': case '\\': case '^': case '`': case '"': case ' ': + /* RFC1738 unsafe */ if (state->flags & PHP_HTTP_URL_PARSE_TOPCT) { state->buffer[state->offset++] = '%'; state->buffer[state->offset++] = parse_xdigits[((unsigned char) *state->ptr) >> 4]; @@ -1350,8 +1448,11 @@ static const char *parse_query(struct parse_state *state) break; default: - if (!(mb = parse_mb(state, PARSE_QUERY, state->ptr, state->end, tmp, 0))) { - return NULL; + if (!(mb = parse_mb(state, PARSE_QUERY, state->ptr, state->end, tmp, state->flags & PHP_HTTP_URL_SILENT_ERRORS))) { + if (!(state->flags & PHP_HTTP_URL_IGNORE_ERRORS)) { + return NULL; + } + break; } state->ptr += mb - 1; } @@ -1380,23 +1481,42 @@ static const char *parse_fragment(struct parse_state *state) do { switch (*state->ptr) { - case '%': - if (state->ptr[1] != '%' && (state->end - state->ptr <= 2 || !isxdigit(*(state->ptr+1)) || !isxdigit(*(state->ptr+2)))) { + case '#': + if (!(state->flags & PHP_HTTP_URL_SILENT_ERRORS)) { php_error_docref(NULL, E_WARNING, - "Failed to parse fragment; invalid percent encoding at pos %u in '%s'", + "Failed to parse fragment; invalid fragment identifier at pos %u in '%s'", (unsigned) (state->ptr - tmp), tmp); + } + if (!(state->flags & PHP_HTTP_URL_IGNORE_ERRORS)) { return NULL; } - state->buffer[state->offset++] = *state->ptr++; - state->buffer[state->offset++] = *state->ptr++; state->buffer[state->offset++] = *state->ptr; break; - /* RFC1738 unsafe */ + case '%': + if (state->ptr[1] != '%' && (state->end - state->ptr <= 2 || !isxdigit(*(state->ptr+1)) || !isxdigit(*(state->ptr+2)))) { + if (!(state->flags & PHP_HTTP_URL_SILENT_ERRORS)) { + php_error_docref(NULL, E_WARNING, + "Failed to parse fragment; invalid percent encoding at pos %u in '%s'", + (unsigned) (state->ptr - tmp), tmp); + } + if (!(state->flags & PHP_HTTP_URL_IGNORE_ERRORS)) { + return NULL; + } + /* fallthrough */ + } else { + state->buffer[state->offset++] = *state->ptr++; + state->buffer[state->offset++] = *state->ptr++; + state->buffer[state->offset++] = *state->ptr; + break; + } + /* no break */ + case '{': case '}': case '<': case '>': case '[': case ']': case '|': case '\\': case '^': case '`': case '"': case ' ': + /* RFC1738 unsafe */ if (state->flags & PHP_HTTP_URL_PARSE_TOPCT) { state->buffer[state->offset++] = '%'; state->buffer[state->offset++] = parse_xdigits[((unsigned char) *state->ptr) >> 4]; @@ -1425,8 +1545,11 @@ static const char *parse_fragment(struct parse_state *state) break; default: - if (!(mb = parse_mb(state, PARSE_FRAGMENT, state->ptr, state->end, tmp, 0))) { - return NULL; + if (!(mb = parse_mb(state, PARSE_FRAGMENT, state->ptr, state->end, tmp, state->flags & PHP_HTTP_URL_SILENT_ERRORS))) { + if (!(state->flags & PHP_HTTP_URL_IGNORE_ERRORS)) { + return NULL; + } + break; } state->ptr += mb - 1; } @@ -1548,11 +1671,15 @@ php_http_url_t *php_http_url_parse_authority(const char *str, size_t len, unsign } if (state->ptr != state->end) { - php_error_docref(NULL, E_WARNING, - "Failed to parse URL authority, unexpected character at pos %u in '%s'", - (unsigned) (state->ptr - str), str); - efree(state); - return NULL; + if (!(state->flags & PHP_HTTP_URL_SILENT_ERRORS)) { + php_error_docref(NULL, E_WARNING, + "Failed to parse URL authority, unexpected character at pos %u in '%s'", + (unsigned) (state->ptr - str), str); + } + if (!(state->flags & PHP_HTTP_URL_IGNORE_ERRORS)) { + efree(state); + return NULL; + } } return (php_http_url_t *) state; @@ -1589,7 +1716,13 @@ PHP_METHOD(HttpUrl, __construct) flags |= PHP_HTTP_URL_FROM_ENV; } - zend_replace_error_handling(EH_THROW, php_http_get_exception_bad_url_class_entry(), &zeh); + if (flags & PHP_HTTP_URL_SILENT_ERRORS) { + zend_replace_error_handling(EH_SUPPRESS, NULL, &zeh); + } else if (flags & PHP_HTTP_URL_IGNORE_ERRORS) { + zend_replace_error_handling(EH_NORMAL, NULL, &zeh); + } else { + 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; @@ -1637,7 +1770,13 @@ 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_get_exception_bad_url_class_entry(), &zeh); + if (flags & PHP_HTTP_URL_SILENT_ERRORS) { + zend_replace_error_handling(EH_SUPPRESS, NULL, &zeh); + } else if (flags & PHP_HTTP_URL_IGNORE_ERRORS) { + zend_replace_error_handling(EH_NORMAL, NULL, &zeh); + } else { + 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; @@ -1753,6 +1892,11 @@ PHP_MINIT_FUNCTION(http_url) 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); + zend_declare_class_constant_long(php_http_url_class_entry, ZEND_STRL("IGNORE_ERRORS"), PHP_HTTP_URL_IGNORE_ERRORS); + zend_declare_class_constant_long(php_http_url_class_entry, ZEND_STRL("SILENT_ERRORS"), PHP_HTTP_URL_SILENT_ERRORS); + + zend_declare_class_constant_long(php_http_url_class_entry, ZEND_STRL("STDFLAGS"), PHP_HTTP_URL_STDFLAGS); + return SUCCESS; }