Skip to content

Commit 2994c23

Browse files
author
Ilia Alshanetsky
committed
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.
1 parent eb83ca1 commit 2994c23

File tree

5 files changed

+72
-21
lines changed

5 files changed

+72
-21
lines changed

ext/standard/basic_functions.c

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,6 @@ php_basic_globals basic_globals;
107107
#include "php_fopen_wrappers.h"
108108
#include "streamsfuncs.h"
109109

110-
static zend_class_entry *incomplete_class_entry = NULL;
111-
112110
static
113111
ZEND_BEGIN_ARG_INFO(first_and_second__args_force_ref, 0)
114112
ZEND_ARG_PASS_INFO(1)
@@ -958,7 +956,7 @@ static void basic_globals_ctor(php_basic_globals *basic_globals_p TSRMLS_DC)
958956
memset(&BG(mblen_state), 0, sizeof(BG(mblen_state)));
959957
#endif
960958

961-
BG(incomplete_class) = incomplete_class_entry;
959+
BG(incomplete_class) = php_create_incomplete_class(TSRMLS_C);
962960
}
963961

964962

@@ -1024,8 +1022,6 @@ PHP_MINIT_FUNCTION(basic)
10241022
#endif
10251023
#endif
10261024

1027-
BG(incomplete_class) = incomplete_class_entry = php_create_incomplete_class(TSRMLS_C);
1028-
10291025
REGISTER_LONG_CONSTANT("CONNECTION_ABORTED", PHP_CONNECTION_ABORTED, CONST_CS | CONST_PERSISTENT);
10301026
REGISTER_LONG_CONSTANT("CONNECTION_NORMAL", PHP_CONNECTION_NORMAL, CONST_CS | CONST_PERSISTENT);
10311027
REGISTER_LONG_CONSTANT("CONNECTION_TIMEOUT", PHP_CONNECTION_TIMEOUT, CONST_CS | CONST_PERSISTENT);
@@ -1039,6 +1035,15 @@ PHP_MINIT_FUNCTION(basic)
10391035
REGISTER_LONG_CONSTANT("SUNFUNCS_RET_STRING", SUNFUNCS_RET_STRING, CONST_CS | CONST_PERSISTENT);
10401036
REGISTER_LONG_CONSTANT("SUNFUNCS_RET_DOUBLE", SUNFUNCS_RET_DOUBLE, CONST_CS | CONST_PERSISTENT);
10411037

1038+
REGISTER_LONG_CONSTANT("PHP_URL_SCHEME", PHP_URL_SCHEME, CONST_CS | CONST_PERSISTENT);
1039+
REGISTER_LONG_CONSTANT("PHP_URL_HOST", PHP_URL_HOST, CONST_CS | CONST_PERSISTENT);
1040+
REGISTER_LONG_CONSTANT("PHP_URL_PORT", PHP_URL_PORT, CONST_CS | CONST_PERSISTENT);
1041+
REGISTER_LONG_CONSTANT("PHP_URL_USER", PHP_URL_USER, CONST_CS | CONST_PERSISTENT);
1042+
REGISTER_LONG_CONSTANT("PHP_URL_PASS", PHP_URL_PASS, CONST_CS | CONST_PERSISTENT);
1043+
REGISTER_LONG_CONSTANT("PHP_URL_PATH", PHP_URL_PATH, CONST_CS | CONST_PERSISTENT);
1044+
REGISTER_LONG_CONSTANT("PHP_URL_QUERY", PHP_URL_QUERY, CONST_CS | CONST_PERSISTENT);
1045+
REGISTER_LONG_CONSTANT("PHP_URL_FRAGMENT", PHP_URL_FRAGMENT, CONST_CS | CONST_PERSISTENT);
1046+
10421047
#define REGISTER_MATH_CONSTANT(x) REGISTER_DOUBLE_CONSTANT(#x, x, CONST_CS | CONST_PERSISTENT)
10431048
REGISTER_MATH_CONSTANT(M_E);
10441049
REGISTER_MATH_CONSTANT(M_LOG2E);

ext/standard/http.c

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@ PHPAPI int php_url_encode_hash_ex(HashTable *ht, smart_str *formstr,
2929
const char *num_prefix, int num_prefix_len,
3030
const char *key_prefix, int key_prefix_len,
3131
const char *key_suffix, int key_suffix_len,
32-
zval *type TSRMLS_DC)
32+
zval *type, char *arg_sep TSRMLS_DC)
3333
{
34-
char *arg_sep = NULL, *key = NULL, *ekey, *newprefix, *p;
34+
char *key = NULL, *ekey, *newprefix, *p;
3535
int arg_sep_len, key_len, ekey_len, key_type, newprefix_len;
3636
ulong idx;
3737
zval **zdata = NULL, *copyzval;
@@ -45,9 +45,11 @@ PHPAPI int php_url_encode_hash_ex(HashTable *ht, smart_str *formstr,
4545
return SUCCESS;
4646
}
4747

48-
arg_sep = INI_STR("arg_separator.output");
49-
if (!arg_sep || !strlen(arg_sep)) {
50-
arg_sep = URL_DEFAULT_ARG_SEP;
48+
if (!arg_sep) {
49+
arg_sep = INI_STR("arg_separator.output");
50+
if (!arg_sep || !strlen(arg_sep)) {
51+
arg_sep = URL_DEFAULT_ARG_SEP;
52+
}
5153
}
5254
arg_sep_len = strlen(arg_sep);
5355

@@ -127,7 +129,7 @@ PHPAPI int php_url_encode_hash_ex(HashTable *ht, smart_str *formstr,
127129
*p = '\0';
128130
}
129131
ht->nApplyCount++;
130-
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);
132+
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);
131133
ht->nApplyCount--;
132134
efree(newprefix);
133135
} 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,
183185
}
184186
/* }}} */
185187

186-
/* {{{ proto string http_build_query(mixed formdata [, string prefix])
188+
/* {{{ proto string http_build_query(mixed formdata [, string prefix [, string arg_separator]])
187189
Generates a form-encoded query string from an associative array or object. */
188190
PHP_FUNCTION(http_build_query)
189191
{
190192
zval *formdata;
191-
char *prefix = NULL;
192-
int prefix_len = 0;
193+
char *prefix = NULL, *arg_sep=NULL;
194+
int arg_sep_len, prefix_len = 0;
193195
smart_str formstr = {0};
196+
194197

195-
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z|s", &formdata, &prefix, &prefix_len) != SUCCESS) {
198+
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z|ss", &formdata, &prefix, &prefix_len, &arg_sep, &arg_sep_len) != SUCCESS) {
196199
RETURN_FALSE;
197200
}
198201

@@ -201,7 +204,7 @@ PHP_FUNCTION(http_build_query)
201204
RETURN_FALSE;
202205
}
203206

204-
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) {
207+
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) {
205208
if (formstr.c) {
206209
efree(formstr.c);
207210
}

ext/standard/php_http.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ PHPAPI int php_url_encode_hash_ex(HashTable *ht, smart_str *formstr,
2828
const char *num_prefix, int num_prefix_len,
2929
const char *key_prefix, int key_prefix_len,
3030
const char *key_suffix, int key_suffix_len,
31-
zval *type TSRMLS_DC);
31+
zval *type, char *arg_sep TSRMLS_DC);
3232
#define php_url_encode_hash(ht, formstr) php_url_encode_hash_ex((ht), (formstr), NULL, 0, NULL, 0, NULL, 0, NULL TSRMLS_CC)
3333

3434
PHP_FUNCTION(http_build_query);

ext/standard/url.c

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -328,15 +328,16 @@ PHPAPI php_url *php_url_parse_ex(char const *str, int length)
328328
}
329329
/* }}} */
330330

331-
/* {{{ proto array parse_url(string url)
331+
/* {{{ proto mixed parse_url(string url, [int url_component])
332332
Parse a URL and return its components */
333333
PHP_FUNCTION(parse_url)
334334
{
335335
char *str;
336336
int str_len;
337337
php_url *resource;
338+
long key = -1;
338339

339-
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &str, &str_len) == FAILURE) {
340+
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|l", &str, &str_len, &key) == FAILURE) {
340341
return;
341342
}
342343

@@ -346,6 +347,39 @@ PHP_FUNCTION(parse_url)
346347
RETURN_FALSE;
347348
}
348349

350+
if (key > -1) {
351+
switch (key) {
352+
case PHP_URL_SCHEME:
353+
if (resource->scheme != NULL) RETVAL_STRING(resource->scheme, 1);
354+
break;
355+
case PHP_URL_HOST:
356+
if (resource->host != NULL) RETVAL_STRING(resource->host, 1);
357+
break;
358+
case PHP_URL_PORT:
359+
if (resource->port != 0) RETVAL_LONG(resource->port);
360+
break;
361+
case PHP_URL_USER:
362+
if (resource->user != NULL) RETVAL_STRING(resource->user, 1);
363+
break;
364+
case PHP_URL_PASS:
365+
if (resource->pass != NULL) RETVAL_STRING(resource->pass, 1);
366+
break;
367+
case PHP_URL_PATH:
368+
if (resource->path != NULL) RETVAL_STRING(resource->path, 1);
369+
break;
370+
case PHP_URL_QUERY:
371+
if (resource->query != NULL) RETVAL_STRING(resource->query, 1);
372+
break;
373+
case PHP_URL_FRAGMENT:
374+
if (resource->fragment != NULL) RETVAL_STRING(resource->fragment, 1);
375+
break;
376+
default:
377+
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid url component identifier %ld.", key);
378+
RETVAL_FALSE;
379+
}
380+
goto done;
381+
}
382+
349383
/* allocate an array for return */
350384
array_init(return_value);
351385

@@ -366,8 +400,8 @@ PHP_FUNCTION(parse_url)
366400
add_assoc_string(return_value, "query", resource->query, 1);
367401
if (resource->fragment != NULL)
368402
add_assoc_string(return_value, "fragment", resource->fragment, 1);
369-
370-
php_url_free(resource);
403+
done:
404+
php_url_free(resource);
371405
}
372406
/* }}} */
373407

ext/standard/url.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,15 @@ PHP_FUNCTION(rawurlencode);
4646
PHP_FUNCTION(rawurldecode);
4747
PHP_FUNCTION(get_headers);
4848

49+
#define PHP_URL_SCHEME 0
50+
#define PHP_URL_HOST 1
51+
#define PHP_URL_PORT 2
52+
#define PHP_URL_USER 3
53+
#define PHP_URL_PASS 4
54+
#define PHP_URL_PATH 5
55+
#define PHP_URL_QUERY 6
56+
#define PHP_URL_FRAGMENT 7
57+
4958
#endif /* URL_H */
5059

5160
/*

0 commit comments

Comments
 (0)