#include "php_http_api.h"
-#if PHP_HTTP_HAVE_IDN2
-# include <idn2.h>
-#elif PHP_HTTP_HAVE_IDN
+#if PHP_HTTP_HAVE_LIBIDN
# include <idna.h>
#endif
+#if PHP_HTTP_HAVE_LIBIDN2
+# include <idn2.h>
+#endif
+#if PHP_HTTP_HAVE_LIBICU
+# include <unicode/uchar.h>
+# include <unicode/uidna.h>
+#endif
+#if PHP_HTTP_HAVE_LIBIDNKIT || PHP_HTTP_HAVE_LIBIDNKIT2
+# include <idn/api.h>
+# include <idn/result.h>
+#endif
-#ifdef PHP_HTTP_HAVE_WCHAR
+#if PHP_HTTP_HAVE_WCHAR
# include <wchar.h>
# include <wctype.h>
#endif
-#ifdef HAVE_ARPA_INET_H
+#if HAVE_ARPA_INET_H
# include <arpa/inet.h>
#endif
static inline char *localhostname(void)
{
char hostname[1024] = {0};
-
-#ifdef PHP_WIN32
+
+#if PHP_WIN32
if (SUCCESS == gethostname(hostname, lenof(hostname))) {
return estrdup(hostname);
}
-#elif defined(HAVE_GETHOSTNAME)
+#elif HAVE_GETHOSTNAME
if (SUCCESS == gethostname(hostname, lenof(hostname))) {
-# if defined(HAVE_GETDOMAINNAME)
+# if HAVE_GETDOMAINNAME
size_t hlen = strlen(hostname);
if (hlen <= lenof(hostname) - lenof("(none)")) {
hostname[hlen++] = '.';
if (!(flags & PHP_HTTP_URL_STRIP_PASS)) {
url_copy(pass);
}
-
+
url_copy(host);
-
+
if (!(flags & PHP_HTTP_URL_STRIP_PORT)) {
url(buf)->port = url_isset(new_url, port) ? new_url->port : ((old_url) ? old_url->port : 0);
}
if ((flags & PHP_HTTP_URL_JOIN_PATH) && url_isset(old_url, path) && url_isset(new_url, path) && *new_url->path != '/') {
size_t old_path_len = strlen(old_url->path), new_path_len = strlen(new_url->path);
char *path = ecalloc(1, old_path_len + new_path_len + 1 + 1);
-
+
strcat(path, old_url->path);
if (path[old_path_len - 1] != '/') {
php_dirname(path, old_path_len);
strcat(path, "/");
}
strcat(path, new_url->path);
-
+
url(buf)->path = &buf.data[buf.used];
if (path[0] != '/') {
url_append(&buf, php_http_buffer_append(&buf, "/", 1));
if (!(flags & PHP_HTTP_URL_STRIP_QUERY)) {
if ((flags & PHP_HTTP_URL_JOIN_QUERY) && url_isset(new_url, query) && url_isset(old_url, query)) {
zval qarr, qstr;
-
+
array_init(&qarr);
-
+
ZVAL_STRING(&qstr, old_url->query);
php_http_querystring_update(&qarr, &qstr, NULL);
zval_ptr_dtor(&qstr);
ZVAL_STRING(&qstr, new_url->query);
php_http_querystring_update(&qarr, &qstr, NULL);
zval_ptr_dtor(&qstr);
-
+
ZVAL_NULL(&qstr);
php_http_querystring_update(&qarr, NULL, &qstr);
if (!(flags & PHP_HTTP_URL_STRIP_FRAGMENT)) {
url_copy(fragment);
}
-
+
/* done with copy & combine & strip */
if (flags & PHP_HTTP_URL_FROM_ENV) {
&& url(buf)->path
&& url(buf)->path[0] && url(buf)->path[1]) {
char *ptr, *end = url(buf)->path + strlen(url(buf)->path) + 1;
-
+
for (ptr = strchr(url(buf)->path, '/'); ptr; ptr = strchr(ptr, '/')) {
switch (ptr[1]) {
case '/':
memmove(&ptr[1], &ptr[2], end - &ptr[2]);
break;
-
+
case '.':
switch (ptr[2]) {
case '\0':
url(buf)->port = 0;
}
}
-
+
return url(buf);
}
HashTable *php_http_url_to_struct(const php_http_url_t *url, zval *strct)
{
- HashTable *ht;
+ HashTable *ht = NULL;
zval tmp;
if (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); \
return cpy;
}
-static size_t parse_mb_utf8(unsigned *wc, const char *ptr, const char *end)
+static inline size_t parse_mb_utf8(unsigned *wc, const char *ptr, const char *end)
{
unsigned wchar;
size_t consumed = utf8towc(&wchar, (const unsigned char *) ptr, end - ptr);
return consumed;
}
-#ifdef PHP_HTTP_HAVE_WCHAR
-static size_t parse_mb_loc(unsigned *wc, const char *ptr, const char *end)
+#if PHP_HTTP_HAVE_WCHAR
+static inline size_t parse_mb_loc(unsigned *wc, const char *ptr, const char *end)
{
wchar_t wchar;
size_t consumed = 0;
-#if defined(HAVE_MBRTOWC)
+#if HAVE_MBRTOWC
mbstate_t ps;
memset(&ps, 0, sizeof(ps));
consumed = mbrtowc(&wchar, ptr, end - ptr, &ps);
-#elif defined(HAVE_MBTOWC)
+#elif HAVE_MBTOWC
consumed = mbtowc(&wchar, ptr, end - ptr);
#endif
static const char parse_xdigits[] = "0123456789ABCDEF";
-static size_t parse_mb(struct parse_state *state, parse_mb_what_t what, const char *ptr, const char *end, const char *begin, zend_bool silent)
+static inline size_t parse_mb(struct parse_state *state, parse_mb_what_t what, const char *ptr, const char *end, const char *begin, zend_bool force_silent)
{
unsigned wchar;
size_t consumed = 0;
if (state->flags & PHP_HTTP_URL_PARSE_MBUTF8) {
consumed = parse_mb_utf8(&wchar, ptr, end);
}
-#ifdef PHP_HTTP_HAVE_WCHAR
+#if PHP_HTTP_HAVE_WCHAR
else if (state->flags & PHP_HTTP_URL_PARSE_MBLOC) {
consumed = parse_mb_loc(&wchar, ptr, end);
}
if (what == PARSE_HOSTINFO && (state->flags & PHP_HTTP_URL_PARSE_TOIDN)) {
/* idna */
} else if (state->flags & PHP_HTTP_URL_PARSE_MBUTF8) {
+#if 0&&PHP_HTTP_HAVE_LIBICU
+ if (!u_isalnum(wchar)) {
+#else
if (!isualnum(wchar)) {
+#endif
break;
}
-#ifdef PHP_HTTP_HAVE_WCHAR
+#if PHP_HTTP_HAVE_WCHAR
} else if (state->flags & PHP_HTTP_URL_PARSE_MBLOC) {
if (!iswalnum(wchar)) {
break;
}
#endif
}
- PHP_HTTP_DUFF(consumed, state->buffer[state->offset++] = *ptr++);
+
+ memcpy(&state->buffer[state->offset], ptr, consumed);
+ state->offset += consumed;
} else {
- int i = 0;
-
- PHP_HTTP_DUFF(consumed,
- state->buffer[state->offset++] = '%';
- state->buffer[state->offset++] = parse_xdigits[((unsigned char) ptr[i]) >> 4];
- state->buffer[state->offset++] = parse_xdigits[((unsigned char) ptr[i]) & 0xf];
- ++i;
- );
+ size_t i;
+
+ for (i = 0; i < consumed; ++i) {
+ state->buffer[state->offset++] = '%';
+ state->buffer[state->offset++] = parse_xdigits[((unsigned char) ptr[i]) >> 4];
+ state->buffer[state->offset++] = parse_xdigits[((unsigned char) ptr[i]) & 0xf];
+ }
}
return consumed;
}
- if (!silent) {
+ if (!force_silent && !(state->flags & PHP_HTTP_URL_SILENT_ERRORS)) {
if (consumed) {
php_error_docref(NULL, E_WARNING,
"Failed to parse %s; unexpected multibyte sequence 0x%x at pos %u in '%s'",
}
}
+ if (state->flags & PHP_HTTP_URL_IGNORE_ERRORS) {
+ state->buffer[state->offset++] = *ptr;
+ return 1;
+ }
+
return 0;
}
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;
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, 0))) {
+ 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 */
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);
+ } while(++ptr < end);
state->buffer[state->offset++] = 0;
return SUCCESS;
}
-#if defined(PHP_WIN32) || defined(HAVE_UIDNA_IDNTOASCII)
+#if PHP_WIN32 || 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)
{
}
#endif
-#ifndef MAXHOSTNAMELEN
-# define MAXHOSTNAMELEN 256
-#endif
-
-#if PHP_HTTP_HAVE_IDN2
-static ZEND_RESULT_CODE parse_idn2(struct parse_state *state, size_t prev_len)
+#if PHP_HTTP_HAVE_LIBIDN2
+# if __GNUC__
+__attribute__ ((unused))
+# endif
+static ZEND_RESULT_CODE parse_gidn_2008(struct parse_state *state, size_t prev_len)
{
char *idn = NULL;
int rv = -1;
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
+# if 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, 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 (IDNA2008); %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)
+#endif
+
+#if PHP_HTTP_HAVE_LIBIDN
+# if __GNUC__
+__attribute__ ((unused))
+# endif
+static ZEND_RESULT_CODE parse_gidn_2003(struct parse_state *state, size_t prev_len)
{
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);
+ rv = idna_to_ascii_8z(state->url.host, &idn, IDNA_ALLOW_UNASSIGNED);
}
-# ifdef PHP_HTTP_HAVE_WCHAR
+# if 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);
+ rv = idna_to_ascii_lz(state->url.host, &idn, IDNA_ALLOW_UNASSIGNED);
}
# 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 (IDNA2003); %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
-#ifdef HAVE_UIDNA_IDNTOASCII
-# if HAVE_UNICODE_UIDNA_H
-# include <unicode/uidna.h>
-# else
+#if HAVE_UIDNA_IDNTOASCII
+# if !PHP_HTTP_HAVE_LIBICU
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 ZEND_RESULT_CODE parse_uidn(struct parse_state *state)
+static ZEND_RESULT_CODE parse_uidn_2003(struct parse_state *state, size_t prev_len)
{
- char *host_ptr;
- uint16_t *uhost_str, ahost_str[MAXHOSTNAMELEN], *ahost_ptr;
+ char ebuf[64] = {0}, *error = NULL;
+ uint16_t *uhost_str, ahost_str[256];
size_t uhost_len, ahost_len;
- UErrorCode error = U_ZERO_ERROR;
+ UErrorCode rc = 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;
+ error = "failed to convert to UTF-16";
+ goto error;
}
-#ifdef PHP_HTTP_HAVE_WCHAR
+#if 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;
+ error = "failed to convert to UTF-16";
+ goto error;
}
#endif
} else {
- php_error_docref(NULL, E_WARNING, "Failed to parse IDN; codepage not specified");
- return FAILURE;
+ error = "codepage not specified";
+ goto error;
}
- ahost_len = uidna_IDNToASCII(uhost_str, uhost_len, ahost_str, MAXHOSTNAMELEN, 3, NULL, &error);
+# if __GNUC__ >= 5
+# pragma GCC diagnostic ignored "-Wdeprecated-declarations"
+# endif
+ ahost_len = uidna_IDNToASCII(uhost_str, uhost_len, ahost_str, 256, 3, NULL, &rc);
+# if __GNUC__ >= 5
+# pragma GCC diagnostic pop
+# endif
+
efree(uhost_str);
+ if (error > U_ZERO_ERROR) {
+ goto error;
+ }
+
+ state->url.host[ahost_len] = '\0';
+ state->offset += ahost_len - prev_len;
+ while (ahost_len--) {
+ state->url.host[ahost_len] = ahost_str[ahost_len];
+ }
+
+ return SUCCESS;
+
+ error:
+ if (!error) {
+ slprintf(ebuf, sizeof(ebuf)-1, "errorcode: %d", rc);
+ error = ebuf;
+ }
+ php_error_docref(NULL, E_WARNING, "Failed to parse IDN (ICU IDNA2003); %s", error);
+
+ return FAILURE;
+}
+#endif
+
+#if PHP_HTTP_HAVE_LIBICU && HAVE_UIDNA_NAMETOASCII_UTF8
+static ZEND_RESULT_CODE parse_uidn_2008(struct parse_state *state, size_t prev_len)
+{
+ char *error = NULL, ebuf[64] = {0};
+ UErrorCode rc = U_ZERO_ERROR;
+ UIDNAInfo info = UIDNA_INFO_INITIALIZER;
+ UIDNA *uidna = uidna_openUTS46(UIDNA_ALLOW_UNASSIGNED, &rc);
- if (error != U_ZERO_ERROR) {
- php_error_docref(NULL, E_WARNING, "Failed to parse IDN; ICU error %d", error);
+ if (!uidna || U_FAILURE(rc)) {
return FAILURE;
}
- host_ptr = state->url.host;
- ahost_ptr = ahost_str;
- PHP_HTTP_DUFF(ahost_len, *host_ptr++ = *ahost_ptr++);
+ if (state->flags & PHP_HTTP_URL_PARSE_MBUTF8) {
+ char ahost_str[256];
+ size_t ahost_len = uidna_nameToASCII_UTF8(uidna, state->url.host, -1, ahost_str, sizeof(ahost_str)-1, &info, &rc);
+
+ if (U_FAILURE(rc) || info.errors) {
+ goto error;
+ }
+
+ memcpy(state->url.host, ahost_str, ahost_len);
+ state->url.host[ahost_len] = '\0';
+ state->offset += ahost_len - prev_len;
+
+#if PHP_HTTP_HAVE_WCHAR
+ } else if (state->flags & PHP_HTTP_URL_PARSE_MBLOC) {
+ uint16_t *uhost_str, whost_str[256];
+ size_t uhost_len, whost_len;
+
+ if (SUCCESS != to_utf16(parse_mb_loc, state->url.host, &uhost_str, &uhost_len)) {
+ error = "could not convert to UTF-16";
+ goto error;
+ }
+
+ whost_len = uidna_nameToASCII(uidna, uhost_str, uhost_len, whost_str, sizeof(whost_str)-1, &info, &rc);
+ efree(uhost_str);
- *host_ptr = '\0';
- state->offset += host_ptr - state->url.host;
+ if (U_FAILURE(rc) || info.errors) {
+ goto error;
+ }
+ state->url.host[whost_len] = '\0';
+ state->offset += whost_len - prev_len;
+ while (whost_len--) {
+ state->url.host[whost_len] = whost_str[whost_len];
+ }
+#endif
+ } else {
+ error = "codepage not specified";
+ goto error;
+ }
+
+ uidna_close(uidna);
return SUCCESS;
+
+ error:
+ if (!error) {
+ if (U_FAILURE(rc)) {
+ slprintf(ebuf, sizeof(ebuf)-1, "%s", u_errorName(rc));
+ error = ebuf;
+ } else if (info.errors) {
+ slprintf(ebuf, sizeof(ebuf)-1, "ICU IDNA error codes: 0x%x", info.errors);
+ error = ebuf;
+ } else {
+ error = "unknown error";
+ }
+ }
+ php_error_docref(NULL, E_WARNING, "Failed to parse IDN (ICU IDNA2008); %s", error);
+
+ uidna_close(uidna);
+ return FAILURE;
+}
+#endif
+
+#if PHP_HTTP_HAVE_LIBIDNKIT || PHP_HTTP_HAVE_LIBIDNKIT2
+# if __GNUC__
+__attribute__ ((unused))
+# endif
+static ZEND_RESULT_CODE parse_kidn(struct parse_state *state, size_t prev_len)
+{
+ idn_result_t rc;
+#if PHP_HTTP_HAVE_LIBIDNKIT
+ int actions = IDN_DELIMMAP|IDN_LOCALMAP|IDN_NAMEPREP|IDN_IDNCONV|IDN_LENCHECK;
+#elif PHP_HTTP_HAVE_LIBIDNKIT2
+ int actions = IDN_MAP|IDN_ASCLOWER|IDN_RTCONV|IDN_PROHCHECK|IDN_NFCCHECK|IDN_PREFCHECK|IDN_COMBCHECK|IDN_CTXOLITECHECK|IDN_BIDICHECK|IDN_LOCALCHECK|IDN_IDNCONV|IDN_LENCHECK|IDN_RTCHECK;
+#endif
+ char ahost_str[256] = {0};
+
+ if (state->flags & PHP_HTTP_URL_PARSE_MBLOC) {
+#if PHP_HTTP_HAVE_LIBIDNKIT
+ actions |= IDN_LOCALCONV;
+#elif PHP_HTTP_HAVE_LIBIDNKIT2
+ actions |= IDN_UNICODECONV;
+#endif
+ }
+
+ rc = idn_encodename(actions, state->url.host, ahost_str, 256);
+ if (rc == idn_success) {
+ size_t ahost_len = strlen(ahost_str);
+
+ memcpy(state->url.host, ahost_str, ahost_len + 1);
+ state->offset += ahost_len - prev_len;
+
+ return SUCCESS;
+ } else {
+ php_error_docref(NULL, E_WARNING, "Failed to parse IDN; %s", idn_result_tostring(rc));
+ return FAILURE;
+ }
}
#endif
-#if 0 && defined(PHP_WIN32)
-static ZEND_RESULT_CODE parse_widn(struct parse_state *state)
+#if 0 && PHP_WIN32
+static ZEND_RESULT_CODE parse_widn_2003(struct parse_state *state, size_t prev_len)
{
char *host_ptr;
- uint16_t *uhost_str, ahost_str[MAXHOSTNAMELEN], *ahost_ptr;
- size_t uhost_len;
+ uint16_t *uhost_str, ahost_str[256];
+ size_t uhost_len, ahost_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, E_WARNING, "Failed to parse IDN");
return FAILURE;
}
-#ifdef PHP_HTTP_HAVE_WCHAR
+#if 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, 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)) {
+ if (!IdnToAscii(IDN_ALLOW_UNASSIGNED, uhost_str, uhost_len, ahost_str, 256)) {
efree(uhost_str);
php_error_docref(NULL, 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;
+ ahost_len = wcslen(ahost_str);
+ state->url.host[ahost_len] = '\0';
+ state->offset += ahost_len - prev_len;
+ while (ahost_len--) {
+ state->url.host[ahost_len] = ahost_str[ahost_len];
+ }
return SUCCESS;
}
#endif
-#ifdef HAVE_INET_PTON
+static ZEND_RESULT_CODE parse_idna(struct parse_state *state, size_t len)
+{
+#if PHP_HTTP_HAVE_IDNA2008
+ if ((state->flags & PHP_HTTP_URL_PARSE_TOIDN_2008) == PHP_HTTP_URL_PARSE_TOIDN_2008
+# if PHP_HTTP_HAVE_IDNA2003
+ || (state->flags & PHP_HTTP_URL_PARSE_TOIDN_2003) != PHP_HTTP_URL_PARSE_TOIDN_2003
+# endif
+ ) {
+#if PHP_HTTP_HAVE_LIBICU && HAVE_UIDNA_NAMETOASCII_UTF8
+ return parse_uidn_2008(state, len);
+#elif PHP_HTTP_HAVE_LIBIDN2
+ return parse_gidn_2008(state, len);
+#elif PHP_HTTP_HAVE_LIBIDNKIT2
+ return parse_kidn(state, len);
+#endif
+ }
+#endif
+
+#if PHP_HTTP_HAVE_IDNA2003
+ if ((state->flags & PHP_HTTP_URL_PARSE_TOIDN_2003) == PHP_HTTP_URL_PARSE_TOIDN_2003
+# if PHP_HTTP_HAVE_IDNA2008
+ || (state->flags & PHP_HTTP_URL_PARSE_TOIDN_2008) != PHP_HTTP_URL_PARSE_TOIDN_2008
+#endif
+ ) {
+#if HAVE_UIDNA_IDNTOASCII
+ return parse_uidn_2003(state, len);
+#elif PHP_HTTP_HAVE_LIBIDN
+ return parse_gidn_2003(state, len);
+#elif PHP_HTTP_HAVE_LIBIDNKIT
+ return parse_kidn(state, len);
+#endif
+ }
+#endif
+
+#if 0 && PHP_WIN32
+ return parse_widn_2003(state, len);
+#endif
+
+#if PHP_HTTP_HAVE_LIBICU && HAVE_UIDNA_NAMETOASCII_UTF8
+ return parse_uidn_2008(state, len);
+#elif PHP_HTTP_HAVE_LIBIDN2
+ return parse_gidn_2008(state, len);
+#elif PHP_HTTP_HAVE_LIBIDNKIT2
+ return parse_kidn(state, len);
+#elif HAVE_UIDNA_IDNTOASCII
+ return parse_uidn_2003(state, len);
+#elif PHP_HTTP_HAVE_LIBIDN
+ return parse_gidn_2003(state, len);
+#elif PHP_HTTP_HAVE_LIBIDNKIT
+ return parse_kidn(state, len);
+#endif
+
+ return SUCCESS;
+}
+
+#if 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) {
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;
}
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 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
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':
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':
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;
+ 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, 0))) {
- return FAILURE;
+ if (!(state->flags & PHP_HTTP_URL_IGNORE_ERRORS)) {
+ return FAILURE;
+ }
+ break;
}
label = ptr;
ptr += mb - 1;
}
- } while (++ptr != end);
+ } 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;
}
if (state->flags & PHP_HTTP_URL_PARSE_TOIDN) {
-#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
- return parse_uidn(state);
-#endif
-#if 0 && defined(PHP_WIN32)
- return parse_widn(state);
-#endif
+ return parse_idna(state, len);
}
return SUCCESS;
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)) {
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++;
default:
if (!(mb = parse_mb(state, PARSE_PATH, state->ptr, state->end, tmp, 0))) {
- return NULL;
+ if (!(state->flags & PHP_HTTP_URL_IGNORE_ERRORS)) {
+ return NULL;
+ }
+ break;
}
state->ptr += mb - 1;
}
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];
default:
if (!(mb = parse_mb(state, PARSE_QUERY, state->ptr, state->end, tmp, 0))) {
- return NULL;
+ if (!(state->flags & PHP_HTTP_URL_IGNORE_ERRORS)) {
+ return NULL;
+ }
+ break;
}
state->ptr += mb - 1;
}
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];
default:
if (!(mb = parse_mb(state, PARSE_FRAGMENT, state->ptr, state->end, tmp, 0))) {
- return NULL;
+ if (!(state->flags & PHP_HTTP_URL_IGNORE_ERRORS)) {
+ return NULL;
+ }
+ break;
}
state->ptr += mb - 1;
}
}
state->ptr += mb - 1;
}
- } while (++state->ptr != state->end);
+ } while (++state->ptr < state->end);
softfail:
state->offset = 0;
state->maxlen = maxlen;
if (!parse_scheme(state)) {
- php_error_docref(NULL, E_WARNING, "Failed to parse URL scheme: '%s'", state->ptr);
+ if (!(flags & PHP_HTTP_URL_SILENT_ERRORS)) {
+ php_error_docref(NULL, E_WARNING, "Failed to parse URL scheme: '%s'", state->ptr);
+ }
efree(state);
return NULL;
}
}
if (!parse_query(state)) {
- php_error_docref(NULL, E_WARNING, "Failed to parse URL query: '%s'", state->ptr);
+ if (!(flags & PHP_HTTP_URL_SILENT_ERRORS)) {
+ php_error_docref(NULL, E_WARNING, "Failed to parse URL query: '%s'", state->ptr);
+ }
efree(state);
return NULL;
}
if (!parse_fragment(state)) {
- php_error_docref(NULL, E_WARNING, "Failed to parse URL fragment: '%s'", state->ptr);
+ if (!(flags & PHP_HTTP_URL_SILENT_ERRORS)) {
+ php_error_docref(NULL, E_WARNING, "Failed to parse URL fragment: '%s'", state->ptr);
+ }
efree(state);
return NULL;
}
}
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;
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|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;
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|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;
zend_declare_class_constant_long(php_http_url_class_entry, ZEND_STRL("FROM_ENV"), PHP_HTTP_URL_FROM_ENV);
zend_declare_class_constant_long(php_http_url_class_entry, ZEND_STRL("SANITIZE_PATH"), PHP_HTTP_URL_SANITIZE_PATH);
-#ifdef PHP_HTTP_HAVE_WCHAR
+#if PHP_HTTP_HAVE_WCHAR
zend_declare_class_constant_long(php_http_url_class_entry, ZEND_STRL("PARSE_MBLOC"), PHP_HTTP_URL_PARSE_MBLOC);
#endif
zend_declare_class_constant_long(php_http_url_class_entry, ZEND_STRL("PARSE_MBUTF8"), PHP_HTTP_URL_PARSE_MBUTF8);
-#if defined(PHP_HTTP_HAVE_IDN2) || defined(PHP_HTTP_HAVE_IDN) || defined(HAVE_UIDNA_IDNTOASCII)
+#if PHP_HTTP_HAVE_LIBIDN2 || PHP_HTTP_HAVE_LIBIDN || PHP_HTTP_HAVE_LIBIDNKIT || PHP_HTTP_HAVE_LIBIDNKIT2 || HAVE_UIDNA_IDNTOASCII || HAVE_UIDNA_NAMETOASCII_UTF8
zend_declare_class_constant_long(php_http_url_class_entry, ZEND_STRL("PARSE_TOIDN"), PHP_HTTP_URL_PARSE_TOIDN);
+# if PHP_HTTP_HAVE_IDNA2003
+ zend_declare_class_constant_long(php_http_url_class_entry, ZEND_STRL("PARSE_TOIDN_2003"), PHP_HTTP_URL_PARSE_TOIDN_2003);
+# endif
+# if PHP_HTTP_HAVE_IDNA2008
+ zend_declare_class_constant_long(php_http_url_class_entry, ZEND_STRL("PARSE_TOIDN_2008"), PHP_HTTP_URL_PARSE_TOIDN_2008);
+# endif
#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);
+ 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;
}