X-Git-Url: https://git.m6w6.name/?p=m6w6%2Fext-http;a=blobdiff_plain;f=php_http_url.c;h=7c8077b6a31995208a8a16da0ed6789d68bd2878;hp=4560848ece40d3e53a754ed9d61b92a23ab05d42;hb=refs%2Fheads%2Fv2.1.x;hpb=4308cb174a56bdbca09b8a30e869eb2e751f5f9b diff --git a/php_http_url.c b/php_http_url.c index 4560848..7c8077b 100644 --- a/php_http_url.c +++ b/php_http_url.c @@ -6,7 +6,7 @@ | modification, are permitted provided that the conditions mentioned | | in the accompanying LICENSE file are met. | +--------------------------------------------------------------------+ - | Copyright (c) 2004-2011, Michael Wallner | + | Copyright (c) 2004-2014, Michael Wallner | +--------------------------------------------------------------------+ */ @@ -46,9 +46,6 @@ static php_url *php_http_url_from_env(php_url *url TSRMLS_DC) { zval *https, *zhost, *zport; long port; -#ifdef HAVE_GETSERVBYPORT - struct servent *se; -#endif if (!url) { url = ecalloc(1, sizeof(*url)); @@ -64,28 +61,8 @@ static php_url *php_http_url_from_env(php_url *url TSRMLS_DC) https = php_http_env_get_server_var(ZEND_STRL("HTTPS"), 1 TSRMLS_CC); if (https && !strcasecmp(Z_STRVAL_P(https), "ON")) { url->scheme = estrndup("https", lenof("https")); - } else switch (url->port) { - case 443: - url->scheme = estrndup("https", lenof("https")); - break; - -#ifndef HAVE_GETSERVBYPORT - default: -#endif - case 80: - case 0: - url->scheme = estrndup("http", lenof("http")); - break; - -#ifdef HAVE_GETSERVBYPORT - default: - if ((se = getservbyport(htons(url->port), "tcp")) && se->s_name) { - url->scheme = estrdup(se->s_name); - } else { - url->scheme = estrndup("http", lenof("http")); - } - break; -#endif + } else { + url->scheme = estrndup("http", lenof("http")); } /* host */ @@ -118,12 +95,9 @@ static php_url *php_http_url_from_env(php_url *url TSRMLS_DC) return url; } -PHP_HTTP_API void php_http_url(int flags, const php_url *old_url, const php_url *new_url, php_url **url_ptr, char **url_str, size_t *url_len TSRMLS_DC) +void php_http_url(int flags, const php_url *old_url, const php_url *new_url, php_url **url_ptr, char **url_str, size_t *url_len TSRMLS_DC) { php_url *url, *tmp_url = NULL; -#ifdef HAVE_GETSERVBYNAME - struct servent *se; -#endif /* set from env if requested */ if (flags & PHP_HTTP_URL_FROM_ENV) { @@ -277,61 +251,13 @@ PHP_HTTP_API void php_http_url(int flags, const php_url *old_url, const php_url if (url->port) { if ( ((url->port == 80) && !strcmp(url->scheme, "http")) || ((url->port ==443) && !strcmp(url->scheme, "https")) -#ifdef HAVE_GETSERVBYNAME - || ((se = getservbyname(url->scheme, "tcp")) && se->s_port && - (url->port == ntohs(se->s_port))) -#endif ) { url->port = 0; } } if (url_str) { - size_t len; - - *url_str = emalloc(PHP_HTTP_URL_MAXLEN + 1); - - **url_str = '\0'; - strlcat(*url_str, url->scheme, PHP_HTTP_URL_MAXLEN); - strlcat(*url_str, "://", PHP_HTTP_URL_MAXLEN); - - if (url->user && *url->user) { - strlcat(*url_str, url->user, PHP_HTTP_URL_MAXLEN); - if (url->pass && *url->pass) { - strlcat(*url_str, ":", PHP_HTTP_URL_MAXLEN); - strlcat(*url_str, url->pass, PHP_HTTP_URL_MAXLEN); - } - strlcat(*url_str, "@", PHP_HTTP_URL_MAXLEN); - } - - strlcat(*url_str, url->host, PHP_HTTP_URL_MAXLEN); - - if (url->port) { - char port_str[8]; - - snprintf(port_str, sizeof(port_str), "%d", (int) url->port); - strlcat(*url_str, ":", PHP_HTTP_URL_MAXLEN); - strlcat(*url_str, port_str, PHP_HTTP_URL_MAXLEN); - } - - strlcat(*url_str, url->path, PHP_HTTP_URL_MAXLEN); - - if (url->query && *url->query) { - strlcat(*url_str, "?", PHP_HTTP_URL_MAXLEN); - strlcat(*url_str, url->query, PHP_HTTP_URL_MAXLEN); - } - - if (url->fragment && *url->fragment) { - strlcat(*url_str, "#", PHP_HTTP_URL_MAXLEN); - strlcat(*url_str, url->fragment, PHP_HTTP_URL_MAXLEN); - } - - if (PHP_HTTP_URL_MAXLEN == (len = strlen(*url_str))) { - php_http_error(HE_NOTICE, PHP_HTTP_E_URL, "Length of URL exceeds PHP_HTTP_URL_MAXLEN"); - } - if (url_len) { - *url_len = len; - } + php_http_url_to_string(url, url_str, url_len TSRMLS_CC); } if (url_ptr) { @@ -341,7 +267,7 @@ PHP_HTTP_API void php_http_url(int flags, const php_url *old_url, const php_url } } -PHP_HTTP_API STATUS php_http_url_encode_hash(HashTable *hash, const char *pre_encoded_str, size_t pre_encoded_len, char **encoded_str, size_t *encoded_len TSRMLS_DC) +STATUS php_http_url_encode_hash(HashTable *hash, const char *pre_encoded_str, size_t pre_encoded_len, char **encoded_str, size_t *encoded_len TSRMLS_DC) { const char *arg_sep_str; size_t arg_sep_len; @@ -360,7 +286,7 @@ PHP_HTTP_API STATUS php_http_url_encode_hash(HashTable *hash, const char *pre_en return SUCCESS; } -PHP_HTTP_API STATUS php_http_url_encode_hash_ex(HashTable *hash, php_http_buffer_t *qstr, const char *arg_sep_str, size_t arg_sep_len, const char *val_sep_str, size_t val_sep_len, const char *pre_encoded_str, size_t pre_encoded_len TSRMLS_DC) +STATUS php_http_url_encode_hash_ex(HashTable *hash, php_http_buffer_t *qstr, const char *arg_sep_str, size_t arg_sep_len, const char *val_sep_str, size_t val_sep_len, const char *pre_encoded_str, size_t pre_encoded_len TSRMLS_DC) { if (pre_encoded_len && pre_encoded_str) { php_http_buffer_append(qstr, pre_encoded_str, pre_encoded_len); @@ -373,110 +299,93 @@ PHP_HTTP_API STATUS php_http_url_encode_hash_ex(HashTable *hash, php_http_buffer return SUCCESS; } -#define PHP_HTTP_BEGIN_ARGS(method, req_args) PHP_HTTP_BEGIN_ARGS_EX(HttpUrl, method, 0, req_args) -#define PHP_HTTP_EMPTY_ARGS(method) PHP_HTTP_EMPTY_ARGS_EX(HttpUrl, method, 0) -#define PHP_HTTP_URL_ME(method, visibility) PHP_ME(HttpUrl, method, PHP_HTTP_ARGS(HttpUrl, method), visibility) - -PHP_HTTP_BEGIN_ARGS(__construct, 0) - PHP_HTTP_ARG_VAL(old_url, 0) - PHP_HTTP_ARG_VAL(new_url, 0) - PHP_HTTP_ARG_VAL(flags, 0) -PHP_HTTP_END_ARGS; -PHP_HTTP_EMPTY_ARGS(toString); -PHP_HTTP_EMPTY_ARGS(toArray); - -PHP_HTTP_BEGIN_ARGS(mod, 1) - PHP_HTTP_ARG_VAL(more_url_parts, 0) - PHP_HTTP_ARG_VAL(flags, 0) -PHP_HTTP_END_ARGS; - -static zend_class_entry *php_http_url_class_entry; - -zend_class_entry *php_http_url_get_class_entry(void) +ZEND_BEGIN_ARG_INFO_EX(ai_HttpUrl___construct, 0, 0, 0) + ZEND_ARG_INFO(0, old_url) + ZEND_ARG_INFO(0, new_url) + ZEND_ARG_INFO(0, flags) +ZEND_END_ARG_INFO(); +PHP_METHOD(HttpUrl, __construct) { - return php_http_url_class_entry; -} + zval *new_url = NULL, *old_url = NULL; + long flags = PHP_HTTP_URL_FROM_ENV; + zend_error_handling zeh; -static zend_function_entry php_http_url_method_entry[] = { - PHP_HTTP_URL_ME(__construct, ZEND_ACC_PUBLIC|ZEND_ACC_CTOR) - PHP_HTTP_URL_ME(mod, ZEND_ACC_PUBLIC) - PHP_HTTP_URL_ME(toString, ZEND_ACC_PUBLIC) - ZEND_MALIAS(HttpUrl, __toString, toString, PHP_HTTP_ARGS(HttpUrl, toString), ZEND_ACC_PUBLIC) - PHP_HTTP_URL_ME(toArray, ZEND_ACC_PUBLIC) - EMPTY_FUNCTION_ENTRY -}; + php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|z!z!l", &old_url, &new_url, &flags), invalid_arg, return); -PHP_METHOD(HttpUrl, __construct) -{ - with_error_handling(EH_THROW, php_http_exception_get_class_entry()) { - zval *new_url = NULL, *old_url = NULL; - long flags = PHP_HTTP_URL_FROM_ENV; - - if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|z!z!l", &old_url, &new_url, &flags)) { - with_error_handling(EH_THROW, php_http_exception_get_class_entry()) { - php_url *res_purl, *new_purl = NULL, *old_purl = NULL; - - if (new_url) { - switch (Z_TYPE_P(new_url)) { - case IS_OBJECT: - case IS_ARRAY: - new_purl = php_http_url_from_struct(NULL, HASH_OF(new_url) TSRMLS_CC); - break; - default: { - zval *cpy = php_http_ztyp(IS_STRING, new_url); + zend_replace_error_handling(EH_THROW, php_http_exception_bad_url_class_entry, &zeh TSRMLS_CC); + { + php_url *res_purl, *new_purl = NULL, *old_purl = NULL; - new_purl = php_url_parse(Z_STRVAL_P(cpy)); - zval_ptr_dtor(&cpy); - break; - } - } - if (!new_purl) { - return; - } - } - if (old_url) { - switch (Z_TYPE_P(old_url)) { - case IS_OBJECT: - case IS_ARRAY: - old_purl = php_http_url_from_struct(NULL, HASH_OF(old_url) TSRMLS_CC); - break; - default: { - zval *cpy = php_http_ztyp(IS_STRING, old_url); + if (new_url) { + switch (Z_TYPE_P(new_url)) { + case IS_OBJECT: + case IS_ARRAY: + new_purl = php_http_url_from_struct(NULL, HASH_OF(new_url) TSRMLS_CC); + break; + default: { + zval *cpy = php_http_ztyp(IS_STRING, new_url); - old_purl = php_url_parse(Z_STRVAL_P(cpy)); - zval_ptr_dtor(&cpy); - break; - } - } - if (!old_purl) { - if (new_purl) { - php_url_free(new_purl); - } - return; - } + new_purl = php_url_parse(Z_STRVAL_P(cpy)); + zval_ptr_dtor(&cpy); + break; } + } + if (!new_purl) { + zend_restore_error_handling(&zeh TSRMLS_CC); + return; + } + } + if (old_url) { + switch (Z_TYPE_P(old_url)) { + case IS_OBJECT: + case IS_ARRAY: + old_purl = php_http_url_from_struct(NULL, HASH_OF(old_url) TSRMLS_CC); + break; + default: { + zval *cpy = php_http_ztyp(IS_STRING, old_url); - php_http_url(flags, old_purl, new_purl, &res_purl, NULL, NULL TSRMLS_CC); - php_http_url_to_struct(res_purl, getThis() TSRMLS_CC); - - php_url_free(res_purl); - if (old_purl) { - php_url_free(old_purl); + old_purl = php_url_parse(Z_STRVAL_P(cpy)); + zval_ptr_dtor(&cpy); + break; } + } + if (!old_purl) { if (new_purl) { php_url_free(new_purl); } - } end_error_handling(); + zend_restore_error_handling(&zeh TSRMLS_CC); + return; + } + } + + php_http_url(flags, old_purl, new_purl, &res_purl, NULL, NULL TSRMLS_CC); + php_http_url_to_struct(res_purl, getThis() TSRMLS_CC); + + php_url_free(res_purl); + if (old_purl) { + php_url_free(old_purl); + } + if (new_purl) { + php_url_free(new_purl); } - } end_error_handling(); + } + zend_restore_error_handling(&zeh TSRMLS_CC); } +ZEND_BEGIN_ARG_INFO_EX(ai_HttpUrl_mod, 0, 0, 1) + ZEND_ARG_INFO(0, more_url_parts) + ZEND_ARG_INFO(0, flags) +ZEND_END_ARG_INFO(); PHP_METHOD(HttpUrl, mod) { zval *new_url = NULL; long flags = PHP_HTTP_URL_JOIN_PATH | PHP_HTTP_URL_JOIN_QUERY; + zend_error_handling zeh; + + php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z!|l", &new_url, &flags), invalid_arg, return); - if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z!|l", &new_url, &flags)) { + zend_replace_error_handling(EH_THROW, php_http_exception_bad_url_class_entry, &zeh TSRMLS_CC); + { php_url *new_purl = NULL, *old_purl = NULL; if (new_url) { @@ -494,6 +403,7 @@ PHP_METHOD(HttpUrl, mod) } } if (!new_purl) { + zend_restore_error_handling(&zeh TSRMLS_CC); return; } } @@ -513,8 +423,11 @@ PHP_METHOD(HttpUrl, mod) php_url_free(new_purl); } } + zend_restore_error_handling(&zeh TSRMLS_CC); } +ZEND_BEGIN_ARG_INFO_EX(ai_HttpUrl_toString, 0, 0, 0) +ZEND_END_ARG_INFO(); PHP_METHOD(HttpUrl, toString) { if (SUCCESS == zend_parse_parameters_none()) { @@ -532,18 +445,39 @@ PHP_METHOD(HttpUrl, toString) RETURN_EMPTY_STRING(); } +ZEND_BEGIN_ARG_INFO_EX(ai_HttpUrl_toArray, 0, 0, 0) +ZEND_END_ARG_INFO(); PHP_METHOD(HttpUrl, toArray) { + php_url *purl; + if (SUCCESS != zend_parse_parameters_none()) { - RETURN_FALSE; + return; } - array_init(return_value); - array_copy(HASH_OF(getThis()), HASH_OF(return_value)); + + /* strip any non-URL properties */ + purl = php_http_url_from_struct(NULL, HASH_OF(getThis()) TSRMLS_CC); + php_http_url_to_struct(purl, return_value TSRMLS_CC); + php_url_free(purl); } +static zend_function_entry php_http_url_methods[] = { + PHP_ME(HttpUrl, __construct, ai_HttpUrl___construct, ZEND_ACC_PUBLIC|ZEND_ACC_CTOR) + PHP_ME(HttpUrl, mod, ai_HttpUrl_mod, ZEND_ACC_PUBLIC) + PHP_ME(HttpUrl, toString, ai_HttpUrl_toString, ZEND_ACC_PUBLIC) + ZEND_MALIAS(HttpUrl, __toString, toString, ai_HttpUrl_toString, ZEND_ACC_PUBLIC) + PHP_ME(HttpUrl, toArray, ai_HttpUrl_toArray, ZEND_ACC_PUBLIC) + EMPTY_FUNCTION_ENTRY +}; + +zend_class_entry *php_http_url_class_entry; + PHP_MINIT_FUNCTION(http_url) { - PHP_HTTP_REGISTER_CLASS(http, Url, http_url, php_http_object_get_class_entry(), 0); + zend_class_entry ce = {0}; + + INIT_NS_CLASS_ENTRY(ce, "http", "Url", php_http_url_methods); + php_http_url_class_entry = zend_register_internal_class(&ce TSRMLS_CC); zend_declare_property_null(php_http_url_class_entry, ZEND_STRL("scheme"), ZEND_ACC_PUBLIC TSRMLS_CC); zend_declare_property_null(php_http_url_class_entry, ZEND_STRL("user"), ZEND_ACC_PUBLIC TSRMLS_CC);