2 +--------------------------------------------------------------------+
4 +--------------------------------------------------------------------+
5 | Redistribution and use in source and binary forms, with or without |
6 | modification, are permitted provided that the conditions mentioned |
7 | in the accompanying LICENSE file are met. |
8 +--------------------------------------------------------------------+
9 | Copyright (c) 2004-2014, Michael Wallner <mike@php.net> |
10 +--------------------------------------------------------------------+
13 #include "php_http_api.h"
15 static php_http_params_token_t def_param_sep
= {",", 1}, *def_param_sep_ptr
[] = {&def_param_sep
, NULL
};
16 static php_http_params_token_t def_arg_sep
= {";", 1}, *def_arg_sep_ptr
[] = {&def_arg_sep
, NULL
};
17 static php_http_params_token_t def_val_sep
= {"=", 1}, *def_val_sep_ptr
[] = {&def_val_sep
, NULL
};
18 static php_http_params_opts_t def_opts
= {
24 PHP_HTTP_PARAMS_DEFAULT
27 php_http_params_opts_t
*php_http_params_opts_default_get(php_http_params_opts_t
*opts
)
30 opts
= emalloc(sizeof(*opts
));
33 memcpy(opts
, &def_opts
, sizeof(def_opts
));
38 typedef struct php_http_params_state
{
39 php_http_params_token_t input
;
40 php_http_params_token_t param
;
41 php_http_params_token_t arg
;
42 php_http_params_token_t val
;
51 } php_http_params_state_t
;
53 static inline void sanitize_escaped(zval
*zv
)
55 if (Z_STRVAL_P(zv
)[0] == '"' && Z_STRVAL_P(zv
)[Z_STRLEN_P(zv
) - 1] == '"') {
56 size_t deq_len
= Z_STRLEN_P(zv
) - 2;
57 char *deq
= estrndup(Z_STRVAL_P(zv
) + 1, deq_len
);
60 ZVAL_STR(zv
, php_http_cs2zs(deq
, deq_len
));
63 php_stripcslashes(Z_STR_P(zv
));
66 static inline void quote_string(zend_string
**zs
, zend_bool force
)
70 *zs
= php_addcslashes(*zs
, 1, ZEND_STRL("\0..\37\173\\\""));
72 if (force
|| len
!= (*zs
)->len
|| strpbrk((*zs
)->val
, "()<>@,;:\"[]?={} ")) {
73 int len
= (*zs
)->len
+ 2;
75 *zs
= zend_string_extend(*zs
, len
, 0);
77 memmove(&(*zs
)->val
[1], (*zs
)->val
, (*zs
)->len
);
79 (*zs
)->val
[len
-1] = '"';
80 (*zs
)->val
[len
] = '\0';
82 zend_string_forget_hash_val(*zs
);
86 /* if (Z_TYPE_P(zv) == IS_STRING) {
87 size_t len = Z_STRLEN_P(zv);
88 zend_string *stripped = php_addcslashes(Z_STR_P(zv), 0,
89 ZEND_STRL("\0..\37\173\\\""));
91 if (len != stripped->len || strpbrk(stripped->val, "()<>@,;:\"[]?={} ")) {
92 size_t len = stripped->len + 2;
93 char *str = emalloc(len + 1);
96 memcpy(&str[1], stripped->val, stripped->len);
101 zend_string_release(stripped);
102 ZVAL_STR(zv, php_http_cs2zs(str, len));
105 ZVAL_STR(zv, stripped);
109 static inline void prepare_escaped(zval
*zv
)
111 if (Z_TYPE_P(zv
) == IS_STRING
) {
112 quote_string(&Z_STR_P(zv
), 0);
115 ZVAL_EMPTY_STRING(zv
);
119 static inline void sanitize_urlencoded(zval
*zv
)
121 Z_STRLEN_P(zv
) = php_raw_url_decode(Z_STRVAL_P(zv
), Z_STRLEN_P(zv
));
124 static inline void prepare_urlencoded(zval
*zv
)
126 zend_string
*str
= php_raw_url_encode(Z_STRVAL_P(zv
), Z_STRLEN_P(zv
));
132 static void sanitize_dimension(zval
*zv
)
134 zval arr
, tmp
, *cur
= NULL
;
135 char *var
= NULL
, *ptr
= Z_STRVAL_P(zv
), *end
= Z_STRVAL_P(zv
) + Z_STRLEN_P(zv
);
148 if (++level
> PG(max_input_nesting_level
)) {
150 php_error_docref(NULL
, E_WARNING
, "Max input nesting level of %ld exceeded", (long) PG(max_input_nesting_level
));
153 if (ptr
- var
== 0) {
162 convert_to_array(cur
);
167 cur
= zend_symtable_str_update(Z_ARRVAL_P(cur
), var
, ptr
- var
, &tmp
);
170 cur
= zend_hash_next_index_insert(Z_ARRVAL_P(cur
), &tmp
);
180 if (zend_hash_num_elements(Z_ARRVAL(arr
))) {
182 ZVAL_COPY_VALUE(zv
, &arr
);
188 static inline void shift_key(php_http_buffer_t
*buf
, char *key_str
, size_t key_len
, const char *ass
, size_t asl
, unsigned flags
);
189 static inline void shift_val(php_http_buffer_t
*buf
, zval
*zvalue
, const char *vss
, size_t vsl
, unsigned flags
);
191 static void prepare_dimension(php_http_buffer_t
*buf
, php_http_buffer_t
*keybuf
, zval
*zvalue
, const char *pss
, size_t psl
, const char *vss
, size_t vsl
, unsigned flags
)
193 HashTable
*ht
= HASH_OF(zvalue
);
194 php_http_arrkey_t key
;
196 php_http_buffer_t prefix
;
198 if (!ZEND_HASH_GET_APPLY_COUNT(ht
)) {
199 ZEND_HASH_INC_APPLY_COUNT(ht
);
200 php_http_buffer_init(&prefix
);
201 php_http_buffer_append(&prefix
, keybuf
->data
, keybuf
->used
);
203 ZEND_HASH_FOREACH_KEY_VAL_IND(ht
, key
.h
, key
.key
, val
)
205 if (key
.key
&& !*key
.key
->val
) {
206 /* only public properties */
210 php_http_buffer_appends(&prefix
, "[");
212 php_http_buffer_append(&prefix
, key
.key
->val
, key
.key
->len
);
214 php_http_buffer_appendf(&prefix
, "%lu", key
.h
);
216 php_http_buffer_appends(&prefix
, "]");
218 if (Z_TYPE_P(val
) == IS_ARRAY
|| Z_TYPE_P(val
) == IS_OBJECT
) {
219 prepare_dimension(buf
, &prefix
, val
, pss
, psl
, vss
, vsl
, flags
);
221 zend_string
*cpy
= zval_get_string(val
);
225 shift_key(buf
, prefix
.data
, prefix
.used
, pss
, psl
, flags
);
226 shift_val(buf
, &tmp
, vss
, vsl
, flags
);
227 zend_string_release(cpy
);
230 php_http_buffer_cut(&prefix
, keybuf
->used
, prefix
.used
- keybuf
->used
);
232 ZEND_HASH_FOREACH_END();
233 ZEND_HASH_DEC_APPLY_COUNT(ht
);
235 php_http_buffer_dtor(&prefix
);
239 static inline void sanitize_key(unsigned flags
, const char *str
, size_t len
, zval
*zv
, zend_bool
*rfc5987
)
242 zend_string
*zs
= zend_string_init(str
, len
, 0);
245 ZVAL_STR(zv
, php_trim(zs
, NULL
, 0, 3));
246 zend_string_release(zs
);
248 if (flags
& PHP_HTTP_PARAMS_ESCAPED
) {
249 sanitize_escaped(zv
);
252 if (!Z_STRLEN_P(zv
)) {
256 if (flags
& PHP_HTTP_PARAMS_RFC5987
) {
257 eos
= &Z_STRVAL_P(zv
)[Z_STRLEN_P(zv
)-1];
265 if (flags
& PHP_HTTP_PARAMS_URLENCODED
) {
266 sanitize_urlencoded(zv
);
269 if (flags
& PHP_HTTP_PARAMS_DIMENSION
) {
270 sanitize_dimension(zv
);
274 static inline void sanitize_rfc5987(zval
*zv
, char **language
, zend_bool
*latin1
)
279 * iso-8850-1'de'bl%f6der%20schei%df%21
280 * utf-8'de-DE'bl%c3%b6der%20schei%c3%9f%21
283 switch (Z_STRVAL_P(zv
)[0]) {
286 if (!strncasecmp(Z_STRVAL_P(zv
), "iso-8859-1", lenof("iso-8859-1"))) {
288 ptr
= Z_STRVAL_P(zv
) + lenof("iso-8859-1");
294 if (!strncasecmp(Z_STRVAL_P(zv
), "utf-8", lenof("utf-8"))) {
296 ptr
= Z_STRVAL_P(zv
) + lenof("utf-8");
304 /* extract language */
306 for (*language
= ++ptr
; *ptr
&& *ptr
!= '\''; ++ptr
);
311 *language
= estrndup(*language
, ptr
- *language
);
314 ptr
= estrdup(++ptr
);
316 ZVAL_STR(zv
, php_http_cs2zs(ptr
, strlen(ptr
)));
320 static inline void sanitize_rfc5988(char *str
, size_t len
, zval
*zv
)
322 zend_string
*zs
= zend_string_init(str
, len
, 0);
325 ZVAL_STR(zv
, php_trim(zs
, " ><", 3, 3));
326 zend_string_release(zs
);
329 static inline void prepare_rfc5988(zval
*zv
)
331 if (Z_TYPE_P(zv
) != IS_STRING
) {
333 ZVAL_EMPTY_STRING(zv
);
337 static void utf8encode(zval
*zv
)
340 unsigned char *ptr
= (unsigned char *) Z_STRVAL_P(zv
);
343 if (*ptr
++ >= 0x80) {
349 ptr
= safe_emalloc(1, len
, 1);
350 for (len
= 0, pos
= 0; len
<= Z_STRLEN_P(zv
); ++len
, ++pos
) {
351 ptr
[pos
] = Z_STRVAL_P(zv
)[len
];
352 if ((ptr
[pos
]) >= 0x80) {
353 ptr
[pos
+ 1] = 0x80 | (ptr
[pos
] & 0x3f);
354 ptr
[pos
] = 0xc0 | ((ptr
[pos
] >> 6) & 0x1f);
359 ZVAL_STR(zv
, php_http_cs2zs((char *) ptr
, pos
-1));
362 static inline void sanitize_value(unsigned flags
, const char *str
, size_t len
, zval
*zv
, zend_bool rfc5987
)
364 char *language
= NULL
;
365 zend_bool latin1
= 0;
366 zend_string
*zs
= zend_string_init(str
, len
, 0);
369 ZVAL_STR(zv
, php_trim(zs
, NULL
, 0, 3));
370 zend_string_release(zs
);
373 sanitize_rfc5987(zv
, &language
, &latin1
);
376 if (flags
& PHP_HTTP_PARAMS_ESCAPED
) {
377 sanitize_escaped(zv
);
380 if ((flags
& PHP_HTTP_PARAMS_URLENCODED
) || (rfc5987
&& language
)) {
381 sanitize_urlencoded(zv
);
384 if (rfc5987
&& language
) {
391 ZVAL_COPY_VALUE(&tmp
, zv
);
393 add_assoc_zval(zv
, language
, &tmp
);
398 static inline void prepare_key(unsigned flags
, char *old_key
, size_t old_len
, char **new_key
, size_t *new_len
)
402 ZVAL_STRINGL(&zv
, old_key
, old_len
);
404 if (flags
& PHP_HTTP_PARAMS_URLENCODED
) {
405 prepare_urlencoded(&zv
);
408 if (flags
& PHP_HTTP_PARAMS_ESCAPED
) {
409 if (flags
& PHP_HTTP_PARAMS_RFC5988
) {
410 prepare_rfc5988(&zv
);
412 prepare_escaped(&zv
);
416 *new_key
= estrndup(Z_STRVAL(zv
), Z_STRLEN(zv
));
417 *new_len
= Z_STRLEN(zv
);
421 static inline void prepare_value(unsigned flags
, zval
*zv
)
423 if (flags
& PHP_HTTP_PARAMS_URLENCODED
) {
424 prepare_urlencoded(zv
);
427 if (flags
& PHP_HTTP_PARAMS_ESCAPED
) {
432 static void merge_param(HashTable
*params
, zval
*zdata
, zval
**current_param
, zval
**current_args
)
434 zval
*ptr
, *zdata_ptr
;
435 php_http_arrkey_t hkey
= {0};
440 INIT_PZVAL_ARRAY(&tmp
, params
);
441 fprintf(stderr
, "params = ");
442 zend_print_zval_r(&tmp
, 1);
443 fprintf(stderr
, "\n");
447 zend_hash_get_current_key(Z_ARRVAL_P(zdata
), &hkey
.key
, &hkey
.h
);
449 if ((hkey
.key
&& !zend_hash_exists(params
, hkey
.key
))
450 || (!hkey
.key
&& !zend_hash_index_exists(params
, hkey
.h
))
452 zval tmp
, arg
, *args
;
454 /* create the entry if it doesn't exist */
455 ptr
= zend_hash_get_current_data(Z_ARRVAL_P(zdata
));
458 add_assoc_zval_ex(&tmp
, ZEND_STRL("value"), ptr
);
461 args
= zend_hash_str_update(Z_ARRVAL(tmp
), "arguments", lenof("arguments"), &arg
);
462 *current_args
= args
;
465 ptr
= zend_hash_update(params
, hkey
.key
, &tmp
);
467 ptr
= zend_hash_index_update(params
, hkey
.h
, &tmp
);
472 ptr
= zend_hash_find(params
, hkey
.key
);
474 ptr
= zend_hash_index_find(params
, hkey
.h
);
479 if (Z_TYPE_P(ptr
) == IS_ARRAY
480 && (ptr
= zend_hash_str_find(Z_ARRVAL_P(ptr
), "value", lenof("value")))
481 && (zdata_ptr
= zend_hash_get_current_data(Z_ARRVAL_P(zdata_ptr
)))
484 * params = [arr => [value => [0 => 1]]]
486 * zdata = [arr => [0 => NULL]]
491 while (Z_TYPE_P(zdata_ptr
) == IS_ARRAY
&& (test_ptr
= zend_hash_get_current_data(Z_ARRVAL_P(zdata_ptr
)))) {
492 if (Z_TYPE_P(test_ptr
) == IS_ARRAY
&& Z_TYPE_P(ptr
) == IS_ARRAY
) {
495 /* now find key in ptr */
496 if (HASH_KEY_IS_STRING
== zend_hash_get_current_key(Z_ARRVAL_P(zdata_ptr
), &hkey
.key
, &hkey
.h
)) {
497 if ((ptr
= zend_hash_find(Z_ARRVAL_P(ptr
), hkey
.key
))) {
498 zdata_ptr
= test_ptr
;
501 Z_TRY_ADDREF_P(test_ptr
);
502 ptr
= zend_hash_update(Z_ARRVAL_P(ptr
), hkey
.key
, test_ptr
);
506 if ((ptr
= zend_hash_index_find(Z_ARRVAL_P(ptr
), hkey
.h
))) {
507 zdata_ptr
= test_ptr
;
510 Z_TRY_ADDREF_P(test_ptr
);
511 ptr
= zend_hash_index_update(Z_ARRVAL_P(ptr
), hkey
.h
, test_ptr
);
515 Z_TRY_ADDREF_P(test_ptr
);
516 ptr
= zend_hash_next_index_insert(Z_ARRVAL_P(ptr
), test_ptr
);
521 /* this is the leaf */
522 Z_TRY_ADDREF_P(test_ptr
);
523 if (Z_TYPE_P(ptr
) != IS_ARRAY
) {
527 if (HASH_KEY_IS_STRING
== zend_hash_get_current_key(Z_ARRVAL_P(zdata_ptr
), &hkey
.key
, &hkey
.h
)) {
528 ptr
= zend_hash_update(Z_ARRVAL_P(ptr
), hkey
.key
, test_ptr
);
530 ptr
= zend_hash_index_update(Z_ARRVAL_P(ptr
), hkey
.h
, test_ptr
);
532 ptr
= zend_hash_next_index_insert(Z_ARRVAL_P(ptr
), test_ptr
);
542 while (Z_TYPE_P(ptr
) == IS_ARRAY
) {
543 zval
*tmp
= zend_hash_get_current_data(Z_ARRVAL_P(ptr
));
551 *current_param
= ptr
;
554 static void push_param(HashTable
*params
, php_http_params_state_t
*state
, const php_http_params_opts_t
*opts
)
556 if (state
->val
.str
) {
557 if (!state
->current
.val
) {
559 } else if (0 < (state
->val
.len
= state
->input
.str
- state
->val
.str
)) {
560 sanitize_value(opts
->flags
, state
->val
.str
, state
->val
.len
, state
->current
.val
, state
->rfc5987
);
562 ZVAL_EMPTY_STRING(state
->current
.val
);
565 } else if (state
->arg
.str
) {
566 if (0 < (state
->arg
.len
= state
->input
.str
- state
->arg
.str
)) {
568 zend_bool rfc5987
= 0;
571 sanitize_key(opts
->flags
, state
->arg
.str
, state
->arg
.len
, &key
, &rfc5987
);
572 state
->rfc5987
= rfc5987
;
573 if (Z_TYPE(key
) == IS_STRING
&& Z_STRLEN(key
)) {
579 if ((rfc
= zend_hash_str_find(Z_ARRVAL_P(state
->current
.args
), ZEND_STRL("*rfc5987*")))) {
580 state
->current
.val
= zend_symtable_str_update(Z_ARRVAL_P(rfc
), Z_STRVAL(key
), Z_STRLEN(key
), &val
);
584 array_init_size(&tmp
, 1);
585 state
->current
.val
= zend_symtable_str_update(Z_ARRVAL(tmp
), Z_STRVAL(key
), Z_STRLEN(key
), &val
);
586 zend_symtable_str_update(Z_ARRVAL_P(state
->current
.args
), ZEND_STRL("*rfc5987*"), &tmp
);
589 state
->current
.val
= zend_symtable_str_update(Z_ARRVAL_P(state
->current
.args
), Z_STRVAL(key
), Z_STRLEN(key
), &val
);
594 } else if (state
->param
.str
) {
595 if (0 < (state
->param
.len
= state
->input
.str
- state
->param
.str
)) {
596 zval prm
, arg
, val
, key
;
597 zend_bool rfc5987
= 0;
600 if (opts
->flags
& PHP_HTTP_PARAMS_RFC5988
) {
601 sanitize_rfc5988(state
->param
.str
, state
->param
.len
, &key
);
603 sanitize_key(opts
->flags
, state
->param
.str
, state
->param
.len
, &key
, &rfc5987
);
604 state
->rfc5987
= rfc5987
;
606 if (Z_TYPE(key
) == IS_ARRAY
) {
607 merge_param(params
, &key
, &state
->current
.val
, &state
->current
.args
);
608 } else if (Z_TYPE(key
) == IS_STRING
&& Z_STRLEN(key
)) {
609 // FIXME: array_init_size(&prm, 2);
612 if (!Z_ISUNDEF(opts
->defval
)) {
613 ZVAL_COPY_VALUE(&val
, &opts
->defval
);
614 zval_copy_ctor(&val
);
618 if (rfc5987
&& (opts
->flags
& PHP_HTTP_PARAMS_RFC5987
)) {
619 state
->current
.val
= zend_hash_str_update(Z_ARRVAL(prm
), "*rfc5987*", lenof("*rfc5987*"), &val
);
621 state
->current
.val
= zend_hash_str_update(Z_ARRVAL(prm
), "value", lenof("value"), &val
);
623 // FIXME: array_init_size(&arg, 3);
625 state
->current
.args
= zend_hash_str_update(Z_ARRVAL(prm
), "arguments", lenof("arguments"), &arg
);
626 state
->current
.param
= zend_symtable_str_update(params
, Z_STRVAL(key
), Z_STRLEN(key
), &prm
);
633 static inline zend_bool
check_str(const char *chk_str
, size_t chk_len
, const char *sep_str
, size_t sep_len
) {
634 return 0 < sep_len
&& chk_len
>= sep_len
&& *chk_str
== *sep_str
&& !memcmp(chk_str
+ 1, sep_str
+ 1, sep_len
- 1);
637 static size_t check_sep(php_http_params_state_t
*state
, php_http_params_token_t
**separators
)
639 php_http_params_token_t
**sep
= separators
;
641 if (state
->quotes
|| state
->escape
) {
645 if (sep
) while (*sep
) {
646 if (check_str(state
->input
.str
, state
->input
.len
, (*sep
)->str
, (*sep
)->len
)) {
654 static void skip_sep(size_t skip
, php_http_params_state_t
*state
, php_http_params_token_t
**param
, php_http_params_token_t
**arg
, php_http_params_token_t
**val
)
658 state
->input
.str
+= skip
;
659 state
->input
.len
-= skip
;
661 while ( (param
&& (sep_len
= check_sep(state
, param
)))
662 || (arg
&& (sep_len
= check_sep(state
, arg
)))
663 || (val
&& (sep_len
= check_sep(state
, val
)))
665 state
->input
.str
+= sep_len
;
666 state
->input
.len
-= sep_len
;
670 HashTable
*php_http_params_parse(HashTable
*params
, const php_http_params_opts_t
*opts
)
672 php_http_params_state_t state
= {{NULL
,0}, {NULL
,0}, {NULL
,0}, {NULL
,0}, {NULL
,NULL
,NULL
}, 0, 0};
674 state
.input
.str
= opts
->input
.str
;
675 state
.input
.len
= opts
->input
.len
;
678 ALLOC_HASHTABLE(params
);
679 ZEND_INIT_SYMTABLE(params
);
682 while (state
.input
.len
) {
683 if ((opts
->flags
& PHP_HTTP_PARAMS_RFC5988
) && !state
.arg
.str
) {
684 if (*state
.input
.str
== '<') {
686 } else if (*state
.input
.str
== '>') {
689 } else if (*state
.input
.str
== '"' && !state
.escape
) {
690 state
.quotes
= !state
.quotes
;
692 state
.escape
= (*state
.input
.str
== '\\');
695 if (!state
.param
.str
) {
697 skip_sep(0, &state
, opts
->param
, opts
->arg
, opts
->val
);
698 state
.param
.str
= state
.input
.str
;
701 /* are we at a param separator? */
702 if (0 < (sep_len
= check_sep(&state
, opts
->param
))) {
703 push_param(params
, &state
, opts
);
705 skip_sep(sep_len
, &state
, opts
->param
, opts
->arg
, opts
->val
);
707 /* start off with a new param */
708 state
.param
.str
= state
.input
.str
;
710 state
.arg
.str
= NULL
;
712 state
.val
.str
= NULL
;
718 /* are we at an arg separator? */
719 if (0 < (sep_len
= check_sep(&state
, opts
->arg
))) {
720 push_param(params
, &state
, opts
);
722 skip_sep(sep_len
, &state
, NULL
, opts
->arg
, opts
->val
);
724 /* continue with a new arg */
725 state
.arg
.str
= state
.input
.str
;
727 state
.val
.str
= NULL
;
733 /* are we at a val separator? */
734 if (0 < (sep_len
= check_sep(&state
, opts
->val
))) {
735 /* only handle separator if we're not already reading in a val */
736 if (!state
.val
.str
) {
737 push_param(params
, &state
, opts
);
739 skip_sep(sep_len
, &state
, NULL
, NULL
, opts
->val
);
741 state
.val
.str
= state
.input
.str
;
749 if (state
.input
.len
) {
755 push_param(params
, &state
, opts
);
760 static inline void shift_key(php_http_buffer_t
*buf
, char *key_str
, size_t key_len
, const char *ass
, size_t asl
, unsigned flags
)
766 php_http_buffer_append(buf
, ass
, asl
);
769 prepare_key(flags
, key_str
, key_len
, &str
, &len
);
770 php_http_buffer_append(buf
, str
, len
);
774 static inline void shift_rfc5987(php_http_buffer_t
*buf
, zval
*zvalue
, const char *vss
, size_t vsl
, unsigned flags
)
776 HashTable
*ht
= HASH_OF(zvalue
);
779 php_http_arrkey_t key
= {0};
781 if ((zdata
= zend_hash_get_current_data(ht
))
782 && HASH_KEY_NON_EXISTENT
!= zend_hash_get_current_key(ht
, &key
.key
, &key
.h
)
784 php_http_arrkey_stringify(&key
, NULL
);
785 php_http_buffer_appendf(buf
, "*%.*sutf-8'%.*s'",
786 (int) (vsl
> INT_MAX
? INT_MAX
: vsl
), vss
,
787 (int) (key
.key
->len
> INT_MAX
? INT_MAX
: key
.key
->len
), key
.key
->val
);
788 php_http_arrkey_dtor(&key
);
790 if (Z_TYPE_P(zdata
) == IS_INDIRECT
) {
791 zdata
= Z_INDIRECT_P(zdata
);
793 zs
= zval_get_string(zdata
);
795 prepare_value(flags
| PHP_HTTP_PARAMS_URLENCODED
, &tmp
);
796 php_http_buffer_append(buf
, Z_STRVAL(tmp
), Z_STRLEN(tmp
));
801 static inline void shift_rfc5988(php_http_buffer_t
*buf
, char *key_str
, size_t key_len
, const char *ass
, size_t asl
, unsigned flags
)
807 php_http_buffer_append(buf
, ass
, asl
);
810 prepare_key(flags
, key_str
, key_len
, &str
, &len
);
811 php_http_buffer_appends(buf
, "<");
812 php_http_buffer_append(buf
, str
, len
);
813 php_http_buffer_appends(buf
, ">");
817 static inline void shift_rfc5988_val(php_http_buffer_t
*buf
, zval
*zv
, const char *vss
, size_t vsl
, unsigned flags
)
819 zend_string
*zs
= zval_get_string(zv
);
821 quote_string(&zs
, 1);
822 php_http_buffer_append(buf
, vss
, vsl
);
823 php_http_buffer_append(buf
, zs
->val
, zs
->len
);
825 zend_string_release(zs
);
828 static inline void shift_val(php_http_buffer_t
*buf
, zval
*zvalue
, const char *vss
, size_t vsl
, unsigned flags
)
833 switch (Z_TYPE_P(zvalue
)) {
838 php_http_buffer_append(buf
, vss
, vsl
);
839 php_http_buffer_appends(buf
, "0");
843 zs
= zval_get_string(zvalue
);
846 prepare_value(flags
, &tmp
);
847 php_http_buffer_append(buf
, vss
, vsl
);
848 php_http_buffer_append(buf
, Z_STRVAL(tmp
), Z_STRLEN(tmp
));
855 static void shift_arg(php_http_buffer_t
*buf
, char *key_str
, size_t key_len
, zval
*zvalue
, const char *ass
, size_t asl
, const char *vss
, size_t vsl
, unsigned flags
)
857 if (Z_TYPE_P(zvalue
) == IS_ARRAY
|| Z_TYPE_P(zvalue
) == IS_OBJECT
) {
858 php_http_arrkey_t key
;
859 HashTable
*ht
= HASH_OF(zvalue
);
861 zend_bool rfc5987
= !strcmp(key_str
, "*rfc5987*");
864 shift_key(buf
, key_str
, key_len
, ass
, asl
, flags
);
866 ZEND_HASH_FOREACH_KEY_VAL_IND(ht
, key
.h
, key
.key
, val
)
868 /* did you mean recursion? */
869 php_http_arrkey_stringify(&key
, NULL
);
870 if (rfc5987
&& (Z_TYPE_P(val
) == IS_ARRAY
|| Z_TYPE_P(val
) == IS_OBJECT
)) {
871 shift_key(buf
, key
.key
->val
, key
.key
->len
, ass
, asl
, flags
);
872 shift_rfc5987(buf
, val
, vss
, vsl
, flags
);
874 shift_arg(buf
, key
.key
->val
, key
.key
->len
, val
, ass
, asl
, vss
, vsl
, flags
);
876 php_http_arrkey_dtor(&key
);
878 ZEND_HASH_FOREACH_END();
880 shift_key(buf
, key_str
, key_len
, ass
, asl
, flags
);
882 if (flags
& PHP_HTTP_PARAMS_RFC5988
) {
886 case lenof("anchor"):
887 /* some args must be quoted */
888 if (0 <= php_http_select_str(key_str
, 3, "rel", "title", "anchor")) {
889 shift_rfc5988_val(buf
, zvalue
, vss
, vsl
, flags
);
896 shift_val(buf
, zvalue
, vss
, vsl
, flags
);
900 static void shift_param(php_http_buffer_t
*buf
, char *key_str
, size_t key_len
, zval
*zvalue
, const char *pss
, size_t psl
, const char *ass
, size_t asl
, const char *vss
, size_t vsl
, unsigned flags
, zend_bool rfc5987
)
902 if (Z_TYPE_P(zvalue
) == IS_ARRAY
|| Z_TYPE_P(zvalue
) == IS_OBJECT
) {
903 /* treat as arguments, unless we care for dimensions or rfc5987 */
904 if (flags
& PHP_HTTP_PARAMS_DIMENSION
) {
905 php_http_buffer_t
*keybuf
= php_http_buffer_from_string(key_str
, key_len
);
906 prepare_dimension(buf
, keybuf
, zvalue
, pss
, psl
, vss
, vsl
, flags
);
907 php_http_buffer_free(&keybuf
);
908 } else if (rfc5987
) {
909 shift_key(buf
, key_str
, key_len
, pss
, psl
, flags
);
910 shift_rfc5987(buf
, zvalue
, vss
, vsl
, flags
);
912 shift_arg(buf
, key_str
, key_len
, zvalue
, ass
, asl
, vss
, vsl
, flags
);
915 if (flags
& PHP_HTTP_PARAMS_RFC5988
) {
916 shift_rfc5988(buf
, key_str
, key_len
, pss
, psl
, flags
);
918 shift_key(buf
, key_str
, key_len
, pss
, psl
, flags
);
920 shift_val(buf
, zvalue
, vss
, vsl
, flags
);
924 php_http_buffer_t
*php_http_params_to_string(php_http_buffer_t
*buf
, HashTable
*params
, const char *pss
, size_t psl
, const char *ass
, size_t asl
, const char *vss
, size_t vsl
, unsigned flags
)
927 php_http_arrkey_t key
;
928 zend_bool rfc5987
= 0;
931 buf
= php_http_buffer_init(NULL
);
934 ZEND_HASH_FOREACH_KEY_VAL(params
, key
.h
, key
.key
, zparam
)
936 zval
*zvalue
, *zargs
;
938 if (Z_TYPE_P(zparam
) != IS_ARRAY
) {
941 if (!(zvalue
= zend_hash_str_find(Z_ARRVAL_P(zparam
), ZEND_STRL("value")))) {
942 if (!(zvalue
= zend_hash_str_find(Z_ARRVAL_P(zparam
), ZEND_STRL("*rfc5987*")))) {
950 php_http_arrkey_stringify(&key
, NULL
);
951 shift_param(buf
, key
.key
->val
, key
.key
->len
, zvalue
, pss
, psl
, ass
, asl
, vss
, vsl
, flags
, rfc5987
);
952 php_http_arrkey_dtor(&key
);
954 if (Z_TYPE_P(zparam
) == IS_ARRAY
) {
955 zval
*tmp
= zend_hash_str_find(Z_ARRVAL_P(zparam
), ZEND_STRL("arguments"));
959 } else if (zvalue
== zparam
) {
966 if (Z_TYPE_P(zvalue
) == IS_ARRAY
) {
967 ZEND_HASH_FOREACH_KEY_VAL(Z_ARRVAL_P(zvalue
), key
.h
, key
.key
, zargs
)
969 if (zvalue
== zparam
&& key
.key
&& zend_string_equals_literal(key
.key
, "value")) {
973 php_http_arrkey_stringify(&key
, NULL
);
974 shift_arg(buf
, key
.key
->val
, key
.key
->len
, zargs
, ass
, asl
, vss
, vsl
, flags
);
975 php_http_arrkey_dtor(&key
);
977 ZEND_HASH_FOREACH_END();
980 ZEND_HASH_FOREACH_END();
982 php_http_buffer_shrink(buf
);
983 php_http_buffer_fix(buf
);
988 php_http_params_token_t
**php_http_params_separator_init(zval
*zv
)
991 php_http_params_token_t
**ret
, **tmp
;
999 convert_to_array(zv
);
1001 ret
= ecalloc(zend_hash_num_elements(Z_ARRVAL_P(zv
)) + 1, sizeof(*ret
));
1004 ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(zv
), sep
)
1006 zend_string
*zs
= zval_get_string(sep
);
1009 *tmp
= emalloc(sizeof(**tmp
));
1010 (*tmp
)->str
= estrndup(zs
->val
, (*tmp
)->len
= zs
->len
);
1013 zend_string_release(zs
);
1015 ZEND_HASH_FOREACH_END();
1017 zval_ptr_dtor(&ztmp
);
1023 void php_http_params_separator_free(php_http_params_token_t
**separator
)
1025 php_http_params_token_t
**sep
= separator
;
1028 PTR_FREE((*sep
)->str
);
1036 static zend_class_entry
*php_http_params_class_entry
;
1037 zend_class_entry
*php_http_params_get_class_entry(void)
1039 return php_http_params_class_entry
;
1042 ZEND_BEGIN_ARG_INFO_EX(ai_HttpParams___construct
, 0, 0, 0)
1043 ZEND_ARG_INFO(0, params
)
1044 ZEND_ARG_INFO(0, param_sep
)
1045 ZEND_ARG_INFO(0, arg_sep
)
1046 ZEND_ARG_INFO(0, val_sep
)
1047 ZEND_ARG_INFO(0, flags
)
1048 ZEND_END_ARG_INFO();
1049 PHP_METHOD(HttpParams
, __construct
)
1051 zval
*zparams
= NULL
, *param_sep
= NULL
, *arg_sep
= NULL
, *val_sep
= NULL
;
1052 zend_long flags
= PHP_HTTP_PARAMS_DEFAULT
;
1053 zend_error_handling zeh
;
1056 php_http_expect(SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS(), "|z!/z/z/z/l", &zparams
, ¶m_sep
, &arg_sep
, &val_sep
, &flags
), invalid_arg
, return);
1058 zend_replace_error_handling(EH_THROW
, php_http_get_exception_runtime_class_entry(), &zeh
);
1060 switch (ZEND_NUM_ARGS()) {
1062 zend_update_property_long(php_http_params_class_entry
, getThis(), ZEND_STRL("flags"), flags
);
1065 zend_update_property(php_http_params_class_entry
, getThis(), ZEND_STRL("val_sep"), val_sep
);
1068 zend_update_property(php_http_params_class_entry
, getThis(), ZEND_STRL("arg_sep"), arg_sep
);
1071 zend_update_property(php_http_params_class_entry
, getThis(), ZEND_STRL("param_sep"), param_sep
);
1076 switch (Z_TYPE_P(zparams
)) {
1079 convert_to_array(zparams
);
1080 zend_update_property(php_http_params_class_entry
, getThis(), ZEND_STRL("params"), zparams
);
1083 zs
= zval_get_string(zparams
);
1087 php_http_params_opts_t opts
= {
1089 php_http_params_separator_init(zend_read_property(php_http_params_class_entry
, getThis(), ZEND_STRL("param_sep"), 0, &tmp
)),
1090 php_http_params_separator_init(zend_read_property(php_http_params_class_entry
, getThis(), ZEND_STRL("arg_sep"), 0, &tmp
)),
1091 php_http_params_separator_init(zend_read_property(php_http_params_class_entry
, getThis(), ZEND_STRL("val_sep"), 0, &tmp
)),
1096 php_http_params_parse(Z_ARRVAL(tmp
), &opts
);
1097 zend_update_property(php_http_params_class_entry
, getThis(), ZEND_STRL("params"), &tmp
);
1098 zval_ptr_dtor(&tmp
);
1100 php_http_params_separator_free(opts
.param
);
1101 php_http_params_separator_free(opts
.arg
);
1102 php_http_params_separator_free(opts
.val
);
1104 zend_string_release(zs
);
1111 zend_update_property(php_http_params_class_entry
, getThis(), ZEND_STRL("params"), &tmp
);
1112 zval_ptr_dtor(&tmp
);
1115 zend_restore_error_handling(&zeh
);
1118 ZEND_BEGIN_ARG_INFO_EX(ai_HttpParams_toArray
, 0, 0, 0)
1119 ZEND_END_ARG_INFO();
1120 PHP_METHOD(HttpParams
, toArray
)
1122 zval zparams_tmp
, *zparams
;
1124 if (SUCCESS
!= zend_parse_parameters_none()) {
1127 zparams
= zend_read_property(php_http_params_class_entry
, getThis(), ZEND_STRL("params"), 0, &zparams_tmp
);
1128 RETURN_ZVAL(zparams
, 1, 0);
1131 ZEND_BEGIN_ARG_INFO_EX(ai_HttpParams_toString
, 0, 0, 0)
1132 ZEND_END_ARG_INFO();
1133 PHP_METHOD(HttpParams
, toString
)
1135 zval
*tmp
, *zparams
, *zpsep
, *zasep
, *zvsep
;
1136 zval zparams_tmp
, flags_tmp
, psep_tmp
, asep_tmp
, vsep_tmp
;
1137 zend_string
*psep
, *asep
, *vsep
;
1139 php_http_buffer_t buf
;
1141 zparams
= zend_read_property(php_http_params_class_entry
, getThis(), ZEND_STRL("params"), 0, &zparams_tmp
);
1142 convert_to_array_ex(zparams
);
1143 flags
= zval_get_long(zend_read_property(php_http_params_class_entry
, getThis(), ZEND_STRL("flags"), 0, &flags_tmp
));
1145 zpsep
= zend_read_property(php_http_params_class_entry
, getThis(), ZEND_STRL("param_sep"), 0, &psep_tmp
);
1146 if (Z_TYPE_P(zpsep
) == IS_ARRAY
&& (tmp
= zend_hash_get_current_data(Z_ARRVAL_P(zpsep
)))) {
1147 psep
= zval_get_string(tmp
);
1149 psep
= zval_get_string(zpsep
);
1151 zasep
= zend_read_property(php_http_params_class_entry
, getThis(), ZEND_STRL("arg_sep"), 0, &asep_tmp
);
1152 if (Z_TYPE_P(zasep
) == IS_ARRAY
&& (tmp
= zend_hash_get_current_data(Z_ARRVAL_P(zasep
)))) {
1153 asep
= zval_get_string(tmp
);
1155 asep
= zval_get_string(zasep
);
1157 zvsep
= zend_read_property(php_http_params_class_entry
, getThis(), ZEND_STRL("val_sep"), 0, &vsep_tmp
);
1158 if (Z_TYPE_P(zvsep
) == IS_ARRAY
&& (tmp
= zend_hash_get_current_data(Z_ARRVAL_P(zvsep
)))) {
1159 vsep
= zval_get_string(tmp
);
1161 vsep
= zval_get_string(zvsep
);
1164 php_http_buffer_init(&buf
);
1165 php_http_params_to_string(&buf
, Z_ARRVAL_P(zparams
), psep
->val
, psep
->len
, asep
->val
, asep
->len
, vsep
->val
, vsep
->len
, flags
);
1167 zend_string_release(psep
);
1168 zend_string_release(asep
);
1169 zend_string_release(vsep
);
1171 RETVAL_STR(php_http_cs2zs(buf
.data
, buf
.used
));
1174 ZEND_BEGIN_ARG_INFO_EX(ai_HttpParams_offsetExists
, 0, 0, 1)
1175 ZEND_ARG_INFO(0, name
)
1176 ZEND_END_ARG_INFO();
1177 PHP_METHOD(HttpParams
, offsetExists
)
1180 zval zparams_tmp
, *zparam
, *zparams
;
1182 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS(), "S", &name
)) {
1186 zparams
= zend_read_property(php_http_params_class_entry
, getThis(), ZEND_STRL("params"), 0, &zparams_tmp
);
1188 if (Z_TYPE_P(zparams
) == IS_ARRAY
&& (zparam
= zend_symtable_find(Z_ARRVAL_P(zparams
), name
))) {
1189 RETVAL_BOOL(Z_TYPE_P(zparam
) != IS_NULL
);
1195 ZEND_BEGIN_ARG_INFO_EX(ai_HttpParams_offsetGet
, 0, 0, 1)
1196 ZEND_ARG_INFO(0, name
)
1197 ZEND_END_ARG_INFO();
1198 PHP_METHOD(HttpParams
, offsetGet
)
1201 zval zparams_tmp
, *zparam
, *zparams
;
1203 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS(), "S", &name
)) {
1207 zparams
= zend_read_property(php_http_params_class_entry
, getThis(), ZEND_STRL("params"), 0, &zparams_tmp
);
1209 if (Z_TYPE_P(zparams
) == IS_ARRAY
&& (zparam
= zend_symtable_find(Z_ARRVAL_P(zparams
), name
))) {
1210 RETVAL_ZVAL(zparam
, 1, 0);
1214 ZEND_BEGIN_ARG_INFO_EX(ai_HttpParams_offsetUnset
, 0, 0, 1)
1215 ZEND_ARG_INFO(0, name
)
1216 ZEND_END_ARG_INFO();
1217 PHP_METHOD(HttpParams
, offsetUnset
)
1220 zval zparams_tmp
, *zparams
;
1222 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS(), "S", &name
)) {
1226 zparams
= zend_read_property(php_http_params_class_entry
, getThis(), ZEND_STRL("params"), 0, &zparams_tmp
);
1228 if (Z_TYPE_P(zparams
) == IS_ARRAY
) {
1229 zend_symtable_del(Z_ARRVAL_P(zparams
), name
);
1233 ZEND_BEGIN_ARG_INFO_EX(ai_HttpParams_offsetSet
, 0, 0, 2)
1234 ZEND_ARG_INFO(0, name
)
1235 ZEND_ARG_INFO(0, value
)
1236 ZEND_END_ARG_INFO();
1237 PHP_METHOD(HttpParams
, offsetSet
)
1240 zval zparams_tmp
, *zparam
, *zparams
, *nvalue
;
1242 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS(), "Sz", &name
, &nvalue
)) {
1246 zparams
= zend_read_property(php_http_params_class_entry
, getThis(), ZEND_STRL("params"), 0, &zparams_tmp
);
1247 convert_to_array(zparams
);
1250 if (Z_TYPE_P(nvalue
) == IS_ARRAY
) {
1251 if ((zparam
= zend_symtable_find(Z_ARRVAL_P(zparams
), name
))) {
1252 convert_to_array(zparam
);
1253 array_join(Z_ARRVAL_P(nvalue
), Z_ARRVAL_P(zparam
), 0, 0);
1255 Z_TRY_ADDREF_P(nvalue
);
1256 add_assoc_zval_ex(zparams
, name
->val
, name
->len
, nvalue
);
1261 if ((zparam
= zend_symtable_find(Z_ARRVAL_P(zparams
), name
))) {
1262 ZVAL_DUP(&tmp
, zparam
);
1263 convert_to_array(&tmp
);
1268 Z_TRY_ADDREF_P(nvalue
);
1269 add_assoc_zval_ex(&tmp
, ZEND_STRL("value"), nvalue
);
1270 add_assoc_zval_ex(zparams
, name
->val
, name
->len
, &tmp
);
1274 zend_string
*zs
= zval_get_string(nvalue
);
1277 add_assoc_bool_ex(&arr
, ZEND_STRL("value"), 1);
1278 add_assoc_zval_ex(zparams
, zs
->val
, zs
->len
, &arr
);
1279 zend_string_release(zs
);
1283 static zend_function_entry php_http_params_methods
[] = {
1284 PHP_ME(HttpParams
, __construct
, ai_HttpParams___construct
, ZEND_ACC_PUBLIC
|ZEND_ACC_CTOR
|ZEND_ACC_FINAL
)
1286 PHP_ME(HttpParams
, toArray
, ai_HttpParams_toArray
, ZEND_ACC_PUBLIC
)
1287 PHP_ME(HttpParams
, toString
, ai_HttpParams_toString
, ZEND_ACC_PUBLIC
)
1288 ZEND_MALIAS(HttpParams
, __toString
, toString
, ai_HttpParams_toString
, ZEND_ACC_PUBLIC
)
1290 PHP_ME(HttpParams
, offsetExists
, ai_HttpParams_offsetExists
, ZEND_ACC_PUBLIC
)
1291 PHP_ME(HttpParams
, offsetUnset
, ai_HttpParams_offsetUnset
, ZEND_ACC_PUBLIC
)
1292 PHP_ME(HttpParams
, offsetSet
, ai_HttpParams_offsetSet
, ZEND_ACC_PUBLIC
)
1293 PHP_ME(HttpParams
, offsetGet
, ai_HttpParams_offsetGet
, ZEND_ACC_PUBLIC
)
1295 EMPTY_FUNCTION_ENTRY
1298 PHP_MINIT_FUNCTION(http_params
)
1300 zend_class_entry ce
= {0};
1302 INIT_NS_CLASS_ENTRY(ce
, "http", "Params", php_http_params_methods
);
1303 php_http_params_class_entry
= zend_register_internal_class(&ce
);
1304 php_http_params_class_entry
->create_object
= php_http_params_object_new
;
1305 zend_class_implements(php_http_params_class_entry
, 1, zend_ce_arrayaccess
);
1307 zend_declare_class_constant_stringl(php_http_params_class_entry
, ZEND_STRL("DEF_PARAM_SEP"), ZEND_STRL(","));
1308 zend_declare_class_constant_stringl(php_http_params_class_entry
, ZEND_STRL("DEF_ARG_SEP"), ZEND_STRL(";"));
1309 zend_declare_class_constant_stringl(php_http_params_class_entry
, ZEND_STRL("DEF_VAL_SEP"), ZEND_STRL("="));
1310 zend_declare_class_constant_stringl(php_http_params_class_entry
, ZEND_STRL("COOKIE_PARAM_SEP"), ZEND_STRL(""));
1312 zend_declare_class_constant_long(php_http_params_class_entry
, ZEND_STRL("PARSE_RAW"), PHP_HTTP_PARAMS_RAW
);
1313 zend_declare_class_constant_long(php_http_params_class_entry
, ZEND_STRL("PARSE_ESCAPED"), PHP_HTTP_PARAMS_ESCAPED
);
1314 zend_declare_class_constant_long(php_http_params_class_entry
, ZEND_STRL("PARSE_URLENCODED"), PHP_HTTP_PARAMS_URLENCODED
);
1315 zend_declare_class_constant_long(php_http_params_class_entry
, ZEND_STRL("PARSE_DIMENSION"), PHP_HTTP_PARAMS_DIMENSION
);
1316 zend_declare_class_constant_long(php_http_params_class_entry
, ZEND_STRL("PARSE_RFC5987"), PHP_HTTP_PARAMS_RFC5987
);
1317 zend_declare_class_constant_long(php_http_params_class_entry
, ZEND_STRL("PARSE_RFC5988"), PHP_HTTP_PARAMS_RFC5988
);
1318 zend_declare_class_constant_long(php_http_params_class_entry
, ZEND_STRL("PARSE_DEFAULT"), PHP_HTTP_PARAMS_DEFAULT
);
1319 zend_declare_class_constant_long(php_http_params_class_entry
, ZEND_STRL("PARSE_QUERY"), PHP_HTTP_PARAMS_QUERY
);
1321 zend_declare_property_null(php_http_params_class_entry
, ZEND_STRL("params"), ZEND_ACC_PUBLIC
);
1322 zend_declare_property_stringl(php_http_params_class_entry
, ZEND_STRL("param_sep"), ZEND_STRL(","), ZEND_ACC_PUBLIC
);
1323 zend_declare_property_stringl(php_http_params_class_entry
, ZEND_STRL("arg_sep"), ZEND_STRL(";"), ZEND_ACC_PUBLIC
);
1324 zend_declare_property_stringl(php_http_params_class_entry
, ZEND_STRL("val_sep"), ZEND_STRL("="), ZEND_ACC_PUBLIC
);
1325 zend_declare_property_long(php_http_params_class_entry
, ZEND_STRL("flags"), PHP_HTTP_PARAMS_DEFAULT
, ZEND_ACC_PUBLIC
);
1335 * vim600: noet sw=4 ts=4 fdm=marker
1336 * vim<600: noet sw=4 ts=4