From 2994c2367062a63394753fc367708b3d1f4955e8 Mon Sep 17 00:00:00 2001 From: Ilia Alshanetsky Date: Sun, 4 Dec 2005 17:44:27 +0000 Subject: [PATCH] MFH: Added 2nd optional parameter to parse_url() that allows retrieval of individual URL components. Added 3rd optional parameter to http_build_query() that allows custom param separator. --- ext/standard/basic_functions.c | 15 ++++++++---- ext/standard/http.c | 25 +++++++++++--------- ext/standard/php_http.h | 2 +- ext/standard/url.c | 42 ++++++++++++++++++++++++++++++---- ext/standard/url.h | 9 ++++++++ 5 files changed, 72 insertions(+), 21 deletions(-) diff --git a/ext/standard/basic_functions.c b/ext/standard/basic_functions.c index be910cf789c..976d577805c 100644 --- a/ext/standard/basic_functions.c +++ b/ext/standard/basic_functions.c @@ -107,8 +107,6 @@ php_basic_globals basic_globals; #include "php_fopen_wrappers.h" #include "streamsfuncs.h" -static zend_class_entry *incomplete_class_entry = NULL; - static ZEND_BEGIN_ARG_INFO(first_and_second__args_force_ref, 0) ZEND_ARG_PASS_INFO(1) @@ -958,7 +956,7 @@ static void basic_globals_ctor(php_basic_globals *basic_globals_p TSRMLS_DC) memset(&BG(mblen_state), 0, sizeof(BG(mblen_state))); #endif - BG(incomplete_class) = incomplete_class_entry; + BG(incomplete_class) = php_create_incomplete_class(TSRMLS_C); } @@ -1024,8 +1022,6 @@ PHP_MINIT_FUNCTION(basic) #endif #endif - BG(incomplete_class) = incomplete_class_entry = php_create_incomplete_class(TSRMLS_C); - REGISTER_LONG_CONSTANT("CONNECTION_ABORTED", PHP_CONNECTION_ABORTED, CONST_CS | CONST_PERSISTENT); REGISTER_LONG_CONSTANT("CONNECTION_NORMAL", PHP_CONNECTION_NORMAL, CONST_CS | CONST_PERSISTENT); REGISTER_LONG_CONSTANT("CONNECTION_TIMEOUT", PHP_CONNECTION_TIMEOUT, CONST_CS | CONST_PERSISTENT); @@ -1039,6 +1035,15 @@ PHP_MINIT_FUNCTION(basic) REGISTER_LONG_CONSTANT("SUNFUNCS_RET_STRING", SUNFUNCS_RET_STRING, CONST_CS | CONST_PERSISTENT); REGISTER_LONG_CONSTANT("SUNFUNCS_RET_DOUBLE", SUNFUNCS_RET_DOUBLE, CONST_CS | CONST_PERSISTENT); + REGISTER_LONG_CONSTANT("PHP_URL_SCHEME", PHP_URL_SCHEME, CONST_CS | CONST_PERSISTENT); + REGISTER_LONG_CONSTANT("PHP_URL_HOST", PHP_URL_HOST, CONST_CS | CONST_PERSISTENT); + REGISTER_LONG_CONSTANT("PHP_URL_PORT", PHP_URL_PORT, CONST_CS | CONST_PERSISTENT); + REGISTER_LONG_CONSTANT("PHP_URL_USER", PHP_URL_USER, CONST_CS | CONST_PERSISTENT); + REGISTER_LONG_CONSTANT("PHP_URL_PASS", PHP_URL_PASS, CONST_CS | CONST_PERSISTENT); + REGISTER_LONG_CONSTANT("PHP_URL_PATH", PHP_URL_PATH, CONST_CS | CONST_PERSISTENT); + REGISTER_LONG_CONSTANT("PHP_URL_QUERY", PHP_URL_QUERY, CONST_CS | CONST_PERSISTENT); + REGISTER_LONG_CONSTANT("PHP_URL_FRAGMENT", PHP_URL_FRAGMENT, CONST_CS | CONST_PERSISTENT); + #define REGISTER_MATH_CONSTANT(x) REGISTER_DOUBLE_CONSTANT(#x, x, CONST_CS | CONST_PERSISTENT) REGISTER_MATH_CONSTANT(M_E); REGISTER_MATH_CONSTANT(M_LOG2E); diff --git a/ext/standard/http.c b/ext/standard/http.c index afa07f13d7c..ae84293f857 100644 --- a/ext/standard/http.c +++ b/ext/standard/http.c @@ -29,9 +29,9 @@ PHPAPI int php_url_encode_hash_ex(HashTable *ht, smart_str *formstr, const char *num_prefix, int num_prefix_len, const char *key_prefix, int key_prefix_len, const char *key_suffix, int key_suffix_len, - zval *type TSRMLS_DC) + zval *type, char *arg_sep TSRMLS_DC) { - char *arg_sep = NULL, *key = NULL, *ekey, *newprefix, *p; + char *key = NULL, *ekey, *newprefix, *p; int arg_sep_len, key_len, ekey_len, key_type, newprefix_len; ulong idx; zval **zdata = NULL, *copyzval; @@ -45,9 +45,11 @@ PHPAPI int php_url_encode_hash_ex(HashTable *ht, smart_str *formstr, return SUCCESS; } - arg_sep = INI_STR("arg_separator.output"); - if (!arg_sep || !strlen(arg_sep)) { - arg_sep = URL_DEFAULT_ARG_SEP; + if (!arg_sep) { + arg_sep = INI_STR("arg_separator.output"); + if (!arg_sep || !strlen(arg_sep)) { + arg_sep = URL_DEFAULT_ARG_SEP; + } } arg_sep_len = strlen(arg_sep); @@ -127,7 +129,7 @@ PHPAPI int php_url_encode_hash_ex(HashTable *ht, smart_str *formstr, *p = '\0'; } ht->nApplyCount++; - php_url_encode_hash_ex(HASH_OF(*zdata), formstr, NULL, 0, newprefix, newprefix_len, "]", 1, (Z_TYPE_PP(zdata) == IS_OBJECT ? *zdata : NULL) TSRMLS_CC); + php_url_encode_hash_ex(HASH_OF(*zdata), formstr, NULL, 0, newprefix, newprefix_len, "]", 1, (Z_TYPE_PP(zdata) == IS_OBJECT ? *zdata : NULL), arg_sep TSRMLS_CC); ht->nApplyCount--; efree(newprefix); } else if (Z_TYPE_PP(zdata) == IS_NULL || Z_TYPE_PP(zdata) == IS_RESOURCE) { @@ -183,16 +185,17 @@ PHPAPI int php_url_encode_hash_ex(HashTable *ht, smart_str *formstr, } /* }}} */ -/* {{{ proto string http_build_query(mixed formdata [, string prefix]) +/* {{{ proto string http_build_query(mixed formdata [, string prefix [, string arg_separator]]) Generates a form-encoded query string from an associative array or object. */ PHP_FUNCTION(http_build_query) { zval *formdata; - char *prefix = NULL; - int prefix_len = 0; + char *prefix = NULL, *arg_sep=NULL; + int arg_sep_len, prefix_len = 0; smart_str formstr = {0}; + - if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z|s", &formdata, &prefix, &prefix_len) != SUCCESS) { + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z|ss", &formdata, &prefix, &prefix_len, &arg_sep, &arg_sep_len) != SUCCESS) { RETURN_FALSE; } @@ -201,7 +204,7 @@ PHP_FUNCTION(http_build_query) RETURN_FALSE; } - if (php_url_encode_hash_ex(HASH_OF(formdata), &formstr, prefix, prefix_len, NULL, 0, NULL, 0, (Z_TYPE_P(formdata) == IS_OBJECT ? formdata : NULL) TSRMLS_CC) == FAILURE) { + if (php_url_encode_hash_ex(HASH_OF(formdata), &formstr, prefix, prefix_len, NULL, 0, NULL, 0, (Z_TYPE_P(formdata) == IS_OBJECT ? formdata : NULL), arg_sep TSRMLS_CC) == FAILURE) { if (formstr.c) { efree(formstr.c); } diff --git a/ext/standard/php_http.h b/ext/standard/php_http.h index d2211916623..a2de9dc59f1 100644 --- a/ext/standard/php_http.h +++ b/ext/standard/php_http.h @@ -28,7 +28,7 @@ PHPAPI int php_url_encode_hash_ex(HashTable *ht, smart_str *formstr, const char *num_prefix, int num_prefix_len, const char *key_prefix, int key_prefix_len, const char *key_suffix, int key_suffix_len, - zval *type TSRMLS_DC); + zval *type, char *arg_sep TSRMLS_DC); #define php_url_encode_hash(ht, formstr) php_url_encode_hash_ex((ht), (formstr), NULL, 0, NULL, 0, NULL, 0, NULL TSRMLS_CC) PHP_FUNCTION(http_build_query); diff --git a/ext/standard/url.c b/ext/standard/url.c index 6e499afe953..1e5dece138c 100644 --- a/ext/standard/url.c +++ b/ext/standard/url.c @@ -328,15 +328,16 @@ end: } /* }}} */ -/* {{{ proto array parse_url(string url) +/* {{{ proto mixed parse_url(string url, [int url_component]) Parse a URL and return its components */ PHP_FUNCTION(parse_url) { char *str; int str_len; php_url *resource; + long key = -1; - if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &str, &str_len) == FAILURE) { + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|l", &str, &str_len, &key) == FAILURE) { return; } @@ -346,6 +347,39 @@ PHP_FUNCTION(parse_url) RETURN_FALSE; } + if (key > -1) { + switch (key) { + case PHP_URL_SCHEME: + if (resource->scheme != NULL) RETVAL_STRING(resource->scheme, 1); + break; + case PHP_URL_HOST: + if (resource->host != NULL) RETVAL_STRING(resource->host, 1); + break; + case PHP_URL_PORT: + if (resource->port != 0) RETVAL_LONG(resource->port); + break; + case PHP_URL_USER: + if (resource->user != NULL) RETVAL_STRING(resource->user, 1); + break; + case PHP_URL_PASS: + if (resource->pass != NULL) RETVAL_STRING(resource->pass, 1); + break; + case PHP_URL_PATH: + if (resource->path != NULL) RETVAL_STRING(resource->path, 1); + break; + case PHP_URL_QUERY: + if (resource->query != NULL) RETVAL_STRING(resource->query, 1); + break; + case PHP_URL_FRAGMENT: + if (resource->fragment != NULL) RETVAL_STRING(resource->fragment, 1); + break; + default: + php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid url component identifier %ld.", key); + RETVAL_FALSE; + } + goto done; + } + /* allocate an array for return */ array_init(return_value); @@ -366,8 +400,8 @@ PHP_FUNCTION(parse_url) add_assoc_string(return_value, "query", resource->query, 1); if (resource->fragment != NULL) add_assoc_string(return_value, "fragment", resource->fragment, 1); - - php_url_free(resource); +done: + php_url_free(resource); } /* }}} */ diff --git a/ext/standard/url.h b/ext/standard/url.h index e09ecae6fd7..ece95e0e39b 100644 --- a/ext/standard/url.h +++ b/ext/standard/url.h @@ -46,6 +46,15 @@ PHP_FUNCTION(rawurlencode); PHP_FUNCTION(rawurldecode); PHP_FUNCTION(get_headers); +#define PHP_URL_SCHEME 0 +#define PHP_URL_HOST 1 +#define PHP_URL_PORT 2 +#define PHP_URL_USER 3 +#define PHP_URL_PASS 4 +#define PHP_URL_PATH 5 +#define PHP_URL_QUERY 6 +#define PHP_URL_FRAGMENT 7 + #endif /* URL_H */ /*