Electronics & Programming

develissimo

Open Source electronics development and programming

  • You are not logged in.
  • Root
  • » PHP
  • » [PHP-DEV] [PATCH] adding RFC3984 support to http_build_query [RSS Feed]

#1 Jan. 5, 2011 12:18:23

Tjerk M.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

[PHP-DEV] [PATCH] adding RFC3984 support to http_build_query


Instead of a boolean, could you add a rfc-xx selection parameter instead,
like, in case one would like rfc 3986 instead?
On Jan 5, 2011 8:10 PM, "Rui Hirokawa" <rui_hirok***@*ahoo.co.jp> wrote:
> Hello,
>
> I made a patch to add the RFC-3984 based url-encoding support
> into http_build_query().
>
> The http_build_query() is quite useful, but,
> it isn't based on the official url-encoding scheme (RFC-3984)
> for ~ (tilde) and ' '(space).
>
> I added an optional (the 4th) parameter 'is_rfc3984'.
> If it is true (false by default, now), RFC3984 based
> url-encoding scheme (it is same as rawurlencode() ) is used.
>
> A simple example shown as bellow,
>
> $v = array('foo'=>'m o','boo'=>'');
>
> // result: foo=m+p&boo=%5B%5E%7E5D
> echo http_build_query($v, null, '&');
>
> // result: foo=m%20p&boo=%5B%5E~5D (RFC-3986 compatible)
> echo http_build_query($v, null, '&', true);
>
> // result: foo=m%20p&boo=%5B%5E~5D (RFC-3986 compatible)
> echo rawurlencode($v).'&'.rawurlencode($v);
>
>
> I'm going to commit the patch if it is accepted.
>
> Rui

Offline

#2 Jan. 5, 2011 12:51:26

Rui H.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

[PHP-DEV] [PATCH] adding RFC3984 support to http_build_query


Hello,
Thank you for the comment.

How about adding two PHP constant,
PHP_QUERY_RFC1738 (default) and PHP_QUERY_RFC3986 ?

It is like,

echo http_build_query($v, null, '&');
echo http_build_query($v, null, '&',PHP_QUERY_RFC1738);
echo http_build_query($v, null, '&', PHP_QUERY_RFC3986);

Rui

(2011/01/05 21:17), Tjerk Meesters wrote:Instead of a boolean, could you add a rfc-xx selection parameterinstead, like, in case one would like rfc 3986 instead?On Jan 5, 2011 8:10 PM, "Rui Hirokawa" <rui_hirok***@*ahoo.co.jp<mailto:rui_hirok***@*ahoo.co.jp>> wrote:> Hello,
>
> I made a patch to add the RFC-3984 based url-encoding support
> into http_build_query().
>
> The http_build_query() is quite useful, but,
> it isn't based on the official url-encoding scheme (RFC-3984)
> for ~ (tilde) and ' '(space).
>
> I added an optional (the 4th) parameter 'is_rfc3984'.
> If it is true (false by default, now), RFC3984 based
> url-encoding scheme (it is same as rawurlencode() ) is used.
>
> A simple example shown as bellow,
>
> $v = array('foo'=>'m o','boo'=>'');
>
> // result: foo=m+p&boo=%5B%5E%7E5D
> echo http_build_query($v, null, '&');
>
> // result: foo=m%20p&boo=%5B%5E~5D (RFC-3986 compatible)
> echo http_build_query($v, null, '&', true);
>
> // result: foo=m%20p&boo=%5B%5E~5D (RFC-3986 compatible)
> echo rawurlencode($v).'&'.rawurlencode($v);
>
>
> I'm going to commit the patch if it is accepted.
>
> Rui

Offline

#3 Jan. 5, 2011 15:10:30

Rui H.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

[PHP-DEV] [PATCH] adding RFC3984 support to http_build_query


Hello,

This is a revised patch based on the PHP constant.

Rui

(2011/01/05 21:50), Rui Hirokawa wrote:Hello,
Thank you for the comment.

How about adding two PHP constant,
PHP_QUERY_RFC1738 (default) and PHP_QUERY_RFC3986 ?

It is like,

echo http_build_query($v, null, '&');
echo http_build_query($v, null, '&',PHP_QUERY_RFC1738);
echo http_build_query($v, null, '&', PHP_QUERY_RFC3986);

Rui

(2011/01/05 21:17), Tjerk Meesters wrote:Instead of a boolean, could you add a rfc-xx selection parameterinstead, like, in case one would like rfc 3986 instead?On Jan 5, 2011 8:10 PM, "Rui Hirokawa" <rui_hirok***@*ahoo.co.jp<mailto:rui_hirok***@*ahoo.co.jp>> wrote:> Hello,
>
> I made a patch to add the RFC-3984 based url-encoding support
> into http_build_query().
>
> The http_build_query() is quite useful, but,
> it isn't based on the official url-encoding scheme (RFC-3984)
> for ~ (tilde) and ' '(space).
>
> I added an optional (the 4th) parameter 'is_rfc3984'.
> If it is true (false by default, now), RFC3984 based
> url-encoding scheme (it is same as rawurlencode() ) is used.
>
> A simple example shown as bellow,
>
> $v = array('foo'=>'m o','boo'=>'');
>
> // result: foo=m+p&boo=%5B%5E%7E5D
> echo http_build_query($v, null, '&');
>
> // result: foo=m%20p&boo=%5B%5E~5D (RFC-3986 compatible)
> echo http_build_query($v, null, '&', true);
>
> // result: foo=m%20p&boo=%5B%5E~5D (RFC-3986 compatible)
> echo rawurlencode($v).'&'.rawurlencode($v);
>
>
> I'm going to commit the patch if it is accepted.
>
> RuiIndex: ext/standard/basic_functions.c
===================================================================
--- ext/standard/basic_functions.c (revision 307112)
+++ ext/standard/basic_functions.c (working copy)
@@ -1513,6 +1513,7 @@
ZEND_ARG_INFO(0, formdata)
ZEND_ARG_INFO(0, prefix)
ZEND_ARG_INFO(0, arg_separator)
+ ZEND_ARG_INFO(0, enc_type)
ZEND_END_ARG_INFO()
/* }}} */
/* {{{ image.c */
@@ -3531,6 +3532,8 @@
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);
+ REGISTER_LONG_CONSTANT("PHP_QUERY_RFC1738", PHP_QUERY_RFC1738, CONST_CS
| CONST_PERSISTENT);
+ REGISTER_LONG_CONSTANT("PHP_QUERY_RFC3986", PHP_QUERY_RFC3986, CONST_CS
| CONST_PERSISTENT);

#define REGISTER_MATH_CONSTANT(x) REGISTER_DOUBLE_CONSTANT(#x, x, CONST_CS |
CONST_PERSISTENT)
REGISTER_MATH_CONSTANT(M_E);
Index: ext/standard/php_http.h
===================================================================
--- ext/standard/php_http.h (revision 307112)
+++ ext/standard/php_http.h (working copy)
@@ -28,7 +28,7 @@
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, char *arg_sep TSRMLS_DC);
+ zval *type, char *arg_sep, int enc_type
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);
Index: ext/standard/http.c
===================================================================
--- ext/standard/http.c (revision 307112)
+++ ext/standard/http.c (working copy)
@@ -29,7 +29,7 @@
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, char *arg_sep TSRMLS_DC)
+ zval *type, char *arg_sep, int enc_type TSRMLS_DC)
{
char *key = NULL, *ekey, *newprefix, *p;
int arg_sep_len, key_len, ekey_len, key_type, newprefix_len;
@@ -81,7 +81,11 @@
}
if (Z_TYPE_PP(zdata) == IS_ARRAY || Z_TYPE_PP(zdata) ==
IS_OBJECT) {
if (key_type == HASH_KEY_IS_STRING) {
- ekey = php_url_encode(key, key_len, &ekey_len);
+ if (enc_type == PHP_QUERY_RFC3986) {
+ ekey = php_raw_url_encode(key, key_len,
&ekey_len);
+ } else {
+ ekey = php_url_encode(key, key_len,
&ekey_len);
+ }
newprefix_len = key_suffix_len + ekey_len +
key_prefix_len + 3 /* %5B */;
newprefix = emalloc(newprefix_len + 1);
p = newprefix;
@@ -132,7 +136,7 @@
*p = '\0';
}
ht->nApplyCount++;
- php_url_encode_hash_ex(HASH_OF(*zdata), formstr, NULL,
0, newprefix, newprefix_len, "%5D", 3, (Z_TYPE_PP(zdata) == IS_OBJECT ? *zdata
: NULL), arg_sep TSRMLS_CC);
+ php_url_encode_hash_ex(HASH_OF(*zdata), formstr, NULL,
0, newprefix, newprefix_len, "%5D", 3, (Z_TYPE_PP(zdata) == IS_OBJECT ? *zdata
: NULL), arg_sep, enc_type TSRMLS_CC);
ht->nApplyCount--;
efree(newprefix);
} else if (Z_TYPE_PP(zdata) == IS_NULL || Z_TYPE_PP(zdata) ==
IS_RESOURCE) {
@@ -145,7 +149,11 @@
/* Simple key=value */
smart_str_appendl(formstr, key_prefix, key_prefix_len);
if (key_type == HASH_KEY_IS_STRING) {
- ekey = php_url_encode(key, key_len, &ekey_len);
+ if (enc_type == PHP_QUERY_RFC3986) {
+ ekey = php_raw_url_encode(key, key_len,
&ekey_len);
+ } else {
+ ekey = php_url_encode(key, key_len,
&ekey_len);
+ }
smart_str_appendl(formstr, ekey, ekey_len);
efree(ekey);
} else {
@@ -161,7 +169,11 @@
smart_str_appendl(formstr, "=", 1);
switch (Z_TYPE_PP(zdata)) {
case IS_STRING:
- ekey =
php_url_encode(Z_STRVAL_PP(zdata), Z_STRLEN_PP(zdata), &ekey_len);
+ if (enc_type == PHP_QUERY_RFC3986) {
+ ekey =
php_raw_url_encode(Z_STRVAL_PP(zdata), Z_STRLEN_PP(zdata), &ekey_len);
+ } else {
+ ekey =
php_url_encode(Z_STRVAL_PP(zdata), Z_STRLEN_PP(zdata), &ekey_len);

+ }
break;
case IS_LONG:
case IS_BOOL:
@@ -176,7 +188,11 @@
*copyzval = **zdata;
zval_copy_ctor(copyzval);
convert_to_string_ex(&copyzval);
- ekey =
php_url_encode(Z_STRVAL_P(copyzval), Z_STRLEN_P(copyzval), &ekey_len);
+ if (enc_type == PHP_QUERY_RFC3986) {
+ ekey =
php_raw_url_encode(Z_STRVAL_P(copyzval), Z_STRLEN_P(copyzval), &ekey_len);
+ } else {
+ ekey =
php_url_encode(Z_STRVAL_P(copyzval), Z_STRLEN_P(copyzval), &ekey_len);
+ }
zval_ptr_dtor(&copyzval);
}
smart_str_appendl(formstr, ekey, ekey_len);
@@ -188,7 +204,7 @@
}
/* }}} */

-/* {{{ 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)
{
@@ -196,9 +212,9 @@
char *prefix = NULL, *arg_sep=NULL;
int arg_sep_len = 0, prefix_len = 0;
smart_str formstr = {0};
-
+ long enc_type = PHP_QUERY_RFC1738;

- if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z|ss", &formdata,
&prefix, &prefix_len, &arg_sep, &arg_sep_len) != SUCCESS) {
+ if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z|ssl",
&formdata, &prefix, &prefix_len, &arg_sep, &arg_sep_len, &enc_type) != SUCCESS)
{
RETURN_FALSE;
}

@@ -207,7 +223,7 @@
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), arg_sep 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, enc_type TSRMLS_CC) == FAILURE) {
if (formstr.c) {
efree(formstr.c);
}
Index: ext/standard/url.h
===================================================================
--- ext/standard/url.h (revision 307112)
+++ ext/standard/url.h (working copy)
@@ -55,6 +55,9 @@
#define PHP_URL_QUERY 6
#define PHP_URL_FRAGMENT 7

+#define PHP_QUERY_RFC1738 1
+#define PHP_QUERY_RFC3986 2
+
#endif /* URL_H */

/*--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit:http://www.php.net/unsub.php

Offline

  • Root
  • » PHP
  • » [PHP-DEV] [PATCH] adding RFC3984 support to http_build_query [RSS Feed]

Board footer

Moderator control

Enjoy the 24th of October
PoweredBy

The Forums are managed by develissimo stuff members, if you find any issues or misplaced content please help us to fix it. Thank you! Tell us via Contact Options
Leave a Message
Welcome to Develissimo Live Support