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_STRVAL_P(zv
), &Z_STRLEN_P(zv
));
66 static inline void prepare_escaped(zval
*zv
)
68 if (Z_TYPE_P(zv
) == IS_STRING
) {
69 size_t len
= Z_STRLEN_P(zv
);
70 zend_string
*stripped
= php_addcslashes(Z_STRVAL_P(zv
), Z_STRLEN_P(zv
), 1,
71 ZEND_STRL("\0..\37\173\\\""));
73 if (len
!= stripped
->len
|| strpbrk(stripped
->val
, "()<>@,;:\"[]?={} ")) {
74 size_t len
= stripped
->len
+ 2;
75 char *str
= emalloc(len
+ 1);
78 memcpy(&str
[1], stripped
->val
, stripped
->len
);
83 zend_string_release(stripped
);
84 ZVAL_STR(zv
, php_http_cs2zs(str
, len
));
87 ZVAL_STR(zv
, stripped
);
91 ZVAL_EMPTY_STRING(zv
);
95 static inline void sanitize_urlencoded(zval
*zv
)
97 Z_STRLEN_P(zv
) = php_raw_url_decode(Z_STRVAL_P(zv
), Z_STRLEN_P(zv
));
100 static inline void prepare_urlencoded(zval
*zv
)
102 zend_string
*str
= php_raw_url_encode(Z_STRVAL_P(zv
), Z_STRLEN_P(zv
));
108 static void sanitize_dimension(zval
*zv
)
110 zval arr
, tmp
, *cur
= NULL
;
111 char *var
= NULL
, *ptr
= Z_STRVAL_P(zv
), *end
= Z_STRVAL_P(zv
) + Z_STRLEN_P(zv
);
124 if (++level
> PG(max_input_nesting_level
)) {
126 php_error_docref(NULL
, E_WARNING
, "Max input nesting level of %ld exceeded", (long) PG(max_input_nesting_level
));
129 if (ptr
- var
== 0) {
138 convert_to_array(cur
);
143 cur
= zend_symtable_str_update(Z_ARRVAL_P(cur
), var
, ptr
- var
, &tmp
);
146 cur
= zend_hash_next_index_insert(Z_ARRVAL_P(cur
), &tmp
);
156 if (zend_hash_num_elements(Z_ARRVAL(arr
))) {
158 ZVAL_COPY_VALUE(zv
, &arr
);
164 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
);
165 static inline void shift_val(php_http_buffer_t
*buf
, zval
*zvalue
, const char *vss
, size_t vsl
, unsigned flags
);
167 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
)
169 HashTable
*ht
= HASH_OF(zvalue
);
170 php_http_arrkey_t key
;
172 php_http_buffer_t prefix
;
174 if (!ZEND_HASH_GET_APPLY_COUNT(ht
)) {
175 ZEND_HASH_INC_APPLY_COUNT(ht
);
176 php_http_buffer_init(&prefix
);
177 php_http_buffer_append(&prefix
, keybuf
->data
, keybuf
->used
);
179 ZEND_HASH_FOREACH_KEY_VAL(ht
, key
.h
, key
.key
, val
)
181 if (key
.key
&& !*key
.key
->val
) {
182 /* only public properties */
186 php_http_buffer_appends(&prefix
, "[");
188 php_http_buffer_append(&prefix
, key
.key
->val
, key
.key
->len
);
190 php_http_buffer_appendf(&prefix
, "%lu", key
.h
);
192 php_http_buffer_appends(&prefix
, "]");
194 if (Z_TYPE_P(val
) == IS_ARRAY
|| Z_TYPE_P(val
) == IS_OBJECT
) {
195 prepare_dimension(buf
, &prefix
, val
, pss
, psl
, vss
, vsl
, flags
);
197 zend_string
*cpy
= zval_get_string(val
);
201 shift_key(buf
, prefix
.data
, prefix
.used
, pss
, psl
, flags
);
202 shift_val(buf
, &tmp
, vss
, vsl
, flags
);
203 zend_string_release(cpy
);
206 php_http_buffer_cut(&prefix
, keybuf
->used
, prefix
.used
- keybuf
->used
);
208 ZEND_HASH_FOREACH_END();
209 ZEND_HASH_DEC_APPLY_COUNT(ht
);
211 php_http_buffer_dtor(&prefix
);
215 static inline void sanitize_key(unsigned flags
, const char *str
, size_t len
, zval
*zv
, zend_bool
*rfc5987
)
218 zend_string
*zs
= zend_string_init(str
, len
, 0);
221 ZVAL_STR(zv
, php_trim(zs
, NULL
, 0, 3));
222 zend_string_release(zs
);
224 if (flags
& PHP_HTTP_PARAMS_ESCAPED
) {
225 sanitize_escaped(zv
);
228 if (!Z_STRLEN_P(zv
)) {
232 eos
= &Z_STRVAL_P(zv
)[Z_STRLEN_P(zv
)-1];
239 if (flags
& PHP_HTTP_PARAMS_URLENCODED
) {
240 sanitize_urlencoded(zv
);
243 if (flags
& PHP_HTTP_PARAMS_DIMENSION
) {
244 sanitize_dimension(zv
);
248 static inline void sanitize_rfc5987(zval
*zv
, char **language
, zend_bool
*latin1
)
253 * iso-8850-1'de'bl%f6der%20schei%df%21
254 * utf-8'de-DE'bl%c3%b6der%20schei%c3%9f%21
257 switch (Z_STRVAL_P(zv
)[0]) {
260 if (!strncasecmp(Z_STRVAL_P(zv
), "iso-8859-1", lenof("iso-8859-1"))) {
262 ptr
= Z_STRVAL_P(zv
) + lenof("iso-8859-1");
268 if (!strncasecmp(Z_STRVAL_P(zv
), "utf-8", lenof("utf-8"))) {
270 ptr
= Z_STRVAL_P(zv
) + lenof("utf-8");
278 /* extract language */
280 for (*language
= ++ptr
; *ptr
&& *ptr
!= '\''; ++ptr
);
285 *language
= estrndup(*language
, ptr
- *language
);
288 ptr
= estrdup(++ptr
);
290 ZVAL_STR(zv
, php_http_cs2zs(ptr
, strlen(ptr
)));
294 static void utf8encode(zval
*zv
)
297 unsigned char *ptr
= (unsigned char *) Z_STRVAL_P(zv
);
300 if (*ptr
++ >= 0x80) {
306 ptr
= safe_emalloc(1, len
, 1);
307 for (len
= 0, pos
= 0; len
<= Z_STRLEN_P(zv
); ++len
, ++pos
) {
308 ptr
[pos
] = Z_STRVAL_P(zv
)[len
];
309 if ((ptr
[pos
]) >= 0x80) {
310 ptr
[pos
+ 1] = 0x80 | (ptr
[pos
] & 0x3f);
311 ptr
[pos
] = 0xc0 | ((ptr
[pos
] >> 6) & 0x1f);
316 ZVAL_STR(zv
, php_http_cs2zs((char *) ptr
, pos
-1));
319 static inline void sanitize_value(unsigned flags
, const char *str
, size_t len
, zval
*zv
, zend_bool rfc5987
)
321 char *language
= NULL
;
322 zend_bool latin1
= 0;
323 zend_string
*zs
= zend_string_init(str
, len
, 0);
326 ZVAL_STR(zv
, php_trim(zs
, NULL
, 0, 3));
327 zend_string_release(zs
);
330 sanitize_rfc5987(zv
, &language
, &latin1
);
333 if (flags
& PHP_HTTP_PARAMS_ESCAPED
) {
334 sanitize_escaped(zv
);
337 if ((flags
& PHP_HTTP_PARAMS_URLENCODED
) || (rfc5987
&& language
)) {
338 sanitize_urlencoded(zv
);
341 if (rfc5987
&& language
) {
348 ZVAL_COPY_VALUE(&tmp
, zv
);
350 add_assoc_zval(zv
, language
, &tmp
);
355 static inline void prepare_key(unsigned flags
, char *old_key
, size_t old_len
, char **new_key
, size_t *new_len
)
359 ZVAL_STRINGL(&zv
, old_key
, old_len
);
361 if (flags
& PHP_HTTP_PARAMS_URLENCODED
) {
362 prepare_urlencoded(&zv
);
365 if (flags
& PHP_HTTP_PARAMS_ESCAPED
) {
366 prepare_escaped(&zv
);
369 *new_key
= estrndup(Z_STRVAL(zv
), Z_STRLEN(zv
));
370 *new_len
= Z_STRLEN(zv
);
374 static inline void prepare_value(unsigned flags
, zval
*zv
)
376 if (flags
& PHP_HTTP_PARAMS_URLENCODED
) {
377 prepare_urlencoded(zv
);
380 if (flags
& PHP_HTTP_PARAMS_ESCAPED
) {
385 static void merge_param(HashTable
*params
, zval
*zdata
, zval
**current_param
, zval
**current_args
)
387 zval
*ptr
, *zdata_ptr
;
388 php_http_arrkey_t hkey
= {0};
393 INIT_PZVAL_ARRAY(&tmp
, params
);
394 fprintf(stderr
, "params = ");
395 zend_print_zval_r(&tmp
, 1 TSRMLS_CC
);
396 fprintf(stderr
, "\n");
400 zend_hash_get_current_key(Z_ARRVAL_P(zdata
), &hkey
.key
, &hkey
.h
);
402 if ((hkey
.key
&& !zend_hash_exists(params
, hkey
.key
))
403 || (!hkey
.key
&& !zend_hash_index_exists(params
, hkey
.h
))
405 zval tmp
, arg
, *args
;
407 /* create the entry if it doesn't exist */
408 ptr
= zend_hash_get_current_data(Z_ARRVAL_P(zdata
));
411 add_assoc_zval_ex(&tmp
, ZEND_STRL("value"), ptr
);
414 args
= zend_hash_str_update(Z_ARRVAL(tmp
), "arguments", lenof("arguments"), &arg
);
415 *current_args
= args
;
418 ptr
= zend_hash_update(params
, hkey
.key
, &tmp
);
420 ptr
= zend_hash_index_update(params
, hkey
.h
, &tmp
);
425 ptr
= zend_hash_find(params
, hkey
.key
);
427 ptr
= zend_hash_index_find(params
, hkey
.h
);
432 if (Z_TYPE_P(ptr
) == IS_ARRAY
433 && (ptr
= zend_hash_str_find(Z_ARRVAL_P(ptr
), "value", lenof("value")))
434 && (zdata_ptr
= zend_hash_get_current_data(Z_ARRVAL_P(zdata_ptr
)))
437 * params = [arr => [value => [0 => 1]]]
439 * zdata = [arr => [0 => NULL]]
444 while (Z_TYPE_P(zdata_ptr
) == IS_ARRAY
&& (test_ptr
= zend_hash_get_current_data(Z_ARRVAL_P(zdata_ptr
)))) {
445 if (Z_TYPE_P(test_ptr
) == IS_ARRAY
) {
446 /* now find key in ptr */
447 if (HASH_KEY_IS_STRING
== zend_hash_get_current_key(Z_ARRVAL_P(zdata_ptr
), &hkey
.key
, &hkey
.h
)) {
448 if ((ptr
= zend_hash_find(Z_ARRVAL_P(ptr
), hkey
.key
))) {
449 zdata_ptr
= test_ptr
;
451 Z_TRY_ADDREF_P(test_ptr
);
452 ptr
= zend_hash_update(Z_ARRVAL_P(ptr
), hkey
.key
, test_ptr
);
456 if ((ptr
= zend_hash_index_find(Z_ARRVAL_P(ptr
), hkey
.h
))) {
457 zdata_ptr
= test_ptr
;
459 Z_TRY_ADDREF_P(test_ptr
);
460 ptr
= zend_hash_index_update(Z_ARRVAL_P(ptr
), hkey
.h
, test_ptr
);
463 Z_TRY_ADDREF_P(test_ptr
);
464 ptr
= zend_hash_next_index_insert(Z_ARRVAL_P(ptr
), test_ptr
);
469 /* this is the leaf */
470 Z_TRY_ADDREF_P(test_ptr
);
471 if (Z_TYPE_P(ptr
) != IS_ARRAY
) {
475 if (HASH_KEY_IS_STRING
== zend_hash_get_current_key(Z_ARRVAL_P(zdata_ptr
), &hkey
.key
, &hkey
.h
)) {
476 ptr
= zend_hash_update(Z_ARRVAL_P(ptr
), hkey
.key
, test_ptr
);
478 ptr
= zend_hash_index_update(Z_ARRVAL_P(ptr
), hkey
.h
, test_ptr
);
480 ptr
= zend_hash_next_index_insert(Z_ARRVAL_P(ptr
), test_ptr
);
490 while (Z_TYPE_P(ptr
) == IS_ARRAY
) {
491 zval
*tmp
= zend_hash_get_current_data(Z_ARRVAL_P(ptr
));
499 *current_param
= ptr
;
502 static void push_param(HashTable
*params
, php_http_params_state_t
*state
, const php_http_params_opts_t
*opts
)
504 if (state
->val
.str
) {
505 if (0 < (state
->val
.len
= state
->input
.str
- state
->val
.str
)) {
506 sanitize_value(opts
->flags
, state
->val
.str
, state
->val
.len
, state
->current
.val
, state
->rfc5987
);
509 } else if (state
->arg
.str
) {
510 if (0 < (state
->arg
.len
= state
->input
.str
- state
->arg
.str
)) {
512 zend_bool rfc5987
= 0;
515 sanitize_key(opts
->flags
, state
->arg
.str
, state
->arg
.len
, &key
, &rfc5987
);
516 state
->rfc5987
= rfc5987
;
517 if (Z_TYPE(key
) == IS_STRING
&& Z_STRLEN(key
)) {
523 if ((rfc
= zend_hash_str_find(Z_ARRVAL_P(state
->current
.args
), ZEND_STRL("*rfc5987*")))) {
524 state
->current
.val
= zend_symtable_str_update(Z_ARRVAL_P(rfc
), Z_STRVAL(key
), Z_STRLEN(key
), &val
);
528 array_init_size(&tmp
, 1);
529 state
->current
.val
= zend_symtable_str_update(Z_ARRVAL(tmp
), Z_STRVAL(key
), Z_STRLEN(key
), &val
);
530 zend_symtable_str_update(Z_ARRVAL_P(state
->current
.args
), ZEND_STRL("*rfc5987*"), &tmp
);
533 state
->current
.val
= zend_symtable_str_update(Z_ARRVAL_P(state
->current
.args
), Z_STRVAL(key
), Z_STRLEN(key
), &val
);
538 } else if (state
->param
.str
) {
539 if (0 < (state
->param
.len
= state
->input
.str
- state
->param
.str
)) {
540 zval prm
, arg
, val
, key
;
541 zend_bool rfc5987
= 0;
544 sanitize_key(opts
->flags
, state
->param
.str
, state
->param
.len
, &key
, &rfc5987
);
545 state
->rfc5987
= rfc5987
;
546 if (Z_TYPE(key
) == IS_ARRAY
) {
547 merge_param(params
, &key
, &state
->current
.val
, &state
->current
.args
);
548 } else if (Z_TYPE(key
) == IS_STRING
&& Z_STRLEN(key
)) {
549 //array_init_size(&prm, 2);
552 if (!Z_ISUNDEF(opts
->defval
)) {
553 ZVAL_COPY_VALUE(&val
, &opts
->defval
);
554 zval_copy_ctor(&val
);
558 if (rfc5987
&& (opts
->flags
& PHP_HTTP_PARAMS_RFC5987
)) {
559 state
->current
.val
= zend_hash_str_update(Z_ARRVAL(prm
), "*rfc5987*", lenof("*rfc5987*"), &val
);
561 state
->current
.val
= zend_hash_str_update(Z_ARRVAL(prm
), "value", lenof("value"), &val
);
563 //array_init_size(&arg, 3);
565 state
->current
.args
= zend_hash_str_update(Z_ARRVAL(prm
), "arguments", lenof("arguments"), &arg
);
566 state
->current
.param
= zend_symtable_str_update(params
, Z_STRVAL(key
), Z_STRLEN(key
), &prm
);
573 static inline zend_bool
check_str(const char *chk_str
, size_t chk_len
, const char *sep_str
, size_t sep_len
) {
574 return 0 < sep_len
&& chk_len
>= sep_len
&& *chk_str
== *sep_str
&& !memcmp(chk_str
+ 1, sep_str
+ 1, sep_len
- 1);
577 static size_t check_sep(php_http_params_state_t
*state
, php_http_params_token_t
**separators
)
579 php_http_params_token_t
**sep
= separators
;
581 if (state
->quotes
|| state
->escape
) {
585 if (sep
) while (*sep
) {
586 if (check_str(state
->input
.str
, state
->input
.len
, (*sep
)->str
, (*sep
)->len
)) {
594 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
)
598 state
->input
.str
+= skip
;
599 state
->input
.len
-= skip
;
601 while ( (param
&& (sep_len
= check_sep(state
, param
)))
602 || (arg
&& (sep_len
= check_sep(state
, arg
)))
603 || (val
&& (sep_len
= check_sep(state
, val
)))
605 state
->input
.str
+= sep_len
;
606 state
->input
.len
-= sep_len
;
610 HashTable
*php_http_params_parse(HashTable
*params
, const php_http_params_opts_t
*opts
)
612 php_http_params_state_t state
= {{NULL
,0}, {NULL
,0}, {NULL
,0}, {NULL
,0}, {NULL
,NULL
,NULL
}, 0, 0};
614 state
.input
.str
= opts
->input
.str
;
615 state
.input
.len
= opts
->input
.len
;
618 ALLOC_HASHTABLE(params
);
619 ZEND_INIT_SYMTABLE(params
);
622 while (state
.input
.len
) {
623 if (*state
.input
.str
== '"' && !state
.escape
) {
624 state
.quotes
= !state
.quotes
;
626 state
.escape
= (*state
.input
.str
== '\\');
629 if (!state
.param
.str
) {
631 skip_sep(0, &state
, opts
->param
, opts
->arg
, opts
->val
);
632 state
.param
.str
= state
.input
.str
;
635 /* are we at a param separator? */
636 if (0 < (sep_len
= check_sep(&state
, opts
->param
))) {
637 push_param(params
, &state
, opts
);
639 skip_sep(sep_len
, &state
, opts
->param
, opts
->arg
, opts
->val
);
641 /* start off with a new param */
642 state
.param
.str
= state
.input
.str
;
644 state
.arg
.str
= NULL
;
646 state
.val
.str
= NULL
;
652 /* are we at an arg separator? */
653 if (0 < (sep_len
= check_sep(&state
, opts
->arg
))) {
654 push_param(params
, &state
, opts
);
656 skip_sep(sep_len
, &state
, NULL
, opts
->arg
, opts
->val
);
658 /* continue with a new arg */
659 state
.arg
.str
= state
.input
.str
;
661 state
.val
.str
= NULL
;
667 /* are we at a val separator? */
668 if (0 < (sep_len
= check_sep(&state
, opts
->val
))) {
669 /* only handle separator if we're not already reading in a val */
670 if (!state
.val
.str
) {
671 push_param(params
, &state
, opts
);
673 skip_sep(sep_len
, &state
, NULL
, NULL
, opts
->val
);
675 state
.val
.str
= state
.input
.str
;
683 if (state
.input
.len
) {
689 push_param(params
, &state
, opts
);
694 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
)
700 php_http_buffer_append(buf
, ass
, asl
);
703 prepare_key(flags
, key_str
, key_len
, &str
, &len
);
704 php_http_buffer_append(buf
, str
, len
);
708 static inline void shift_rfc5987(php_http_buffer_t
*buf
, zval
*zvalue
, const char *vss
, size_t vsl
, unsigned flags
)
710 HashTable
*ht
= HASH_OF(zvalue
);
713 php_http_arrkey_t key
= {0};
715 if ((zdata
= zend_hash_get_current_data(ht
))
716 && HASH_KEY_NON_EXISTENT
!= zend_hash_get_current_key(ht
, &key
.key
, &key
.h
)
718 php_http_arrkey_stringify(&key
, NULL
);
719 php_http_buffer_appendf(buf
, "*%.*sutf-8'%.*s'",
720 (int) (vsl
> INT_MAX
? INT_MAX
: vsl
), vss
,
721 (int) (key
.key
->len
> INT_MAX
? INT_MAX
: key
.key
->len
), key
.key
->val
);
722 php_http_arrkey_dtor(&key
);
724 zs
= zval_get_string(zdata
);
726 prepare_value(flags
| PHP_HTTP_PARAMS_URLENCODED
, &tmp
);
727 php_http_buffer_append(buf
, Z_STRVAL(tmp
), Z_STRLEN(tmp
));
732 static inline void shift_val(php_http_buffer_t
*buf
, zval
*zvalue
, const char *vss
, size_t vsl
, unsigned flags
)
737 switch (Z_TYPE_P(zvalue
)) {
742 php_http_buffer_append(buf
, vss
, vsl
);
743 php_http_buffer_appends(buf
, "0");
747 zs
= zval_get_string(zvalue
);
750 prepare_value(flags
, &tmp
);
751 php_http_buffer_append(buf
, vss
, vsl
);
752 php_http_buffer_append(buf
, Z_STRVAL(tmp
), Z_STRLEN(tmp
));
759 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
)
761 if (Z_TYPE_P(zvalue
) == IS_ARRAY
|| Z_TYPE_P(zvalue
) == IS_OBJECT
) {
762 php_http_arrkey_t key
;
763 HashTable
*ht
= HASH_OF(zvalue
);
765 zend_bool rfc5987
= !strcmp(key_str
, "*rfc5987*");
768 shift_key(buf
, key_str
, key_len
, ass
, asl
, flags
);
770 ZEND_HASH_FOREACH_KEY_VAL(ht
, key
.h
, key
.key
, val
)
772 /* did you mean recursion? */
773 php_http_arrkey_stringify(&key
, NULL
);
774 if (rfc5987
&& (Z_TYPE_P(val
) == IS_ARRAY
|| Z_TYPE_P(val
) == IS_OBJECT
)) {
775 shift_key(buf
, key
.key
->val
, key
.key
->len
, ass
, asl
, flags
);
776 shift_rfc5987(buf
, val
, vss
, vsl
, flags
);
778 shift_arg(buf
, key
.key
->val
, key
.key
->len
, val
, ass
, asl
, vss
, vsl
, flags
);
780 php_http_arrkey_dtor(&key
);
782 ZEND_HASH_FOREACH_END();
784 shift_key(buf
, key_str
, key_len
, ass
, asl
, flags
);
785 shift_val(buf
, zvalue
, vss
, vsl
, flags
);
789 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
)
791 if (Z_TYPE_P(zvalue
) == IS_ARRAY
|| Z_TYPE_P(zvalue
) == IS_OBJECT
) {
792 /* treat as arguments, unless we care for dimensions or rfc5987 */
793 if (flags
& PHP_HTTP_PARAMS_DIMENSION
) {
794 php_http_buffer_t
*keybuf
= php_http_buffer_from_string(key_str
, key_len
);
795 prepare_dimension(buf
, keybuf
, zvalue
, pss
, psl
, vss
, vsl
, flags
);
796 php_http_buffer_free(&keybuf
);
797 } else if (rfc5987
) {
798 shift_key(buf
, key_str
, key_len
, pss
, psl
, flags
);
799 shift_rfc5987(buf
, zvalue
, vss
, vsl
, flags
);
801 shift_arg(buf
, key_str
, key_len
, zvalue
, ass
, asl
, vss
, vsl
, flags
);
804 shift_key(buf
, key_str
, key_len
, pss
, psl
, flags
);
805 shift_val(buf
, zvalue
, vss
, vsl
, flags
);
809 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
)
812 php_http_arrkey_t key
;
813 zend_bool rfc5987
= 0;
816 buf
= php_http_buffer_init(NULL
);
819 ZEND_HASH_FOREACH_KEY_VAL(params
, key
.h
, key
.key
, zparam
)
821 zval
*zvalue
, *zargs
;
823 if (Z_TYPE_P(zparam
) != IS_ARRAY
) {
826 if (!(zvalue
= zend_hash_str_find(Z_ARRVAL_P(zparam
), ZEND_STRL("value")))) {
827 if (!(zvalue
= zend_hash_str_find(Z_ARRVAL_P(zparam
), ZEND_STRL("*rfc5987*")))) {
835 php_http_arrkey_stringify(&key
, NULL
);
836 shift_param(buf
, key
.key
->val
, key
.key
->len
, zvalue
, pss
, psl
, ass
, asl
, vss
, vsl
, flags
, rfc5987
);
837 php_http_arrkey_dtor(&key
);
839 if (Z_TYPE_P(zparam
) == IS_ARRAY
) {
840 zval
*tmp
= zend_hash_str_find(Z_ARRVAL_P(zparam
), ZEND_STRL("arguments"));
844 } else if (zvalue
== zparam
) {
850 if (Z_TYPE_P(zvalue
) == IS_ARRAY
) {
851 ZEND_HASH_FOREACH_KEY_VAL(Z_ARRVAL_P(zvalue
), key
.h
, key
.key
, zargs
)
853 if (zvalue
== zparam
&& key
.key
&& zend_string_equals_literal(key
.key
, "value")) {
857 php_http_arrkey_stringify(&key
, NULL
);
858 shift_arg(buf
, key
.key
->val
, key
.key
->len
, zargs
, ass
, asl
, vss
, vsl
, flags
);
859 php_http_arrkey_dtor(&key
);
861 ZEND_HASH_FOREACH_END();
864 ZEND_HASH_FOREACH_END();
866 php_http_buffer_shrink(buf
);
867 php_http_buffer_fix(buf
);
872 php_http_params_token_t
**php_http_params_separator_init(zval
*zv
)
875 php_http_params_token_t
**ret
, **tmp
;
882 convert_to_array(zv
);
883 ret
= ecalloc(zend_hash_num_elements(Z_ARRVAL_P(zv
)) + 1, sizeof(*ret
));
886 ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(zv
), sep
)
888 zend_string
*zs
= zval_get_string(sep
);
891 *tmp
= emalloc(sizeof(**tmp
));
892 (*tmp
)->str
= estrndup(zs
->val
, (*tmp
)->len
= zs
->len
);
895 zend_string_release(zs
);
897 ZEND_HASH_FOREACH_END();
904 void php_http_params_separator_free(php_http_params_token_t
**separator
)
906 php_http_params_token_t
**sep
= separator
;
909 PTR_FREE((*sep
)->str
);
917 ZEND_BEGIN_ARG_INFO_EX(ai_HttpParams___construct
, 0, 0, 0)
918 ZEND_ARG_INFO(0, params
)
919 ZEND_ARG_INFO(0, param_sep
)
920 ZEND_ARG_INFO(0, arg_sep
)
921 ZEND_ARG_INFO(0, val_sep
)
922 ZEND_ARG_INFO(0, flags
)
924 PHP_METHOD(HttpParams
, __construct
)
926 zval
*zparams
= NULL
, *param_sep
= NULL
, *arg_sep
= NULL
, *val_sep
= NULL
;
927 zend_long flags
= PHP_HTTP_PARAMS_DEFAULT
;
928 zend_error_handling zeh
;
931 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);
933 zend_replace_error_handling(EH_THROW
, php_http_exception_runtime_class_entry
, &zeh
);
935 switch (ZEND_NUM_ARGS()) {
937 zend_update_property_long(php_http_params_class_entry
, getThis(), ZEND_STRL("flags"), flags
);
940 zend_update_property(php_http_params_class_entry
, getThis(), ZEND_STRL("val_sep"), val_sep
);
943 zend_update_property(php_http_params_class_entry
, getThis(), ZEND_STRL("arg_sep"), arg_sep
);
946 zend_update_property(php_http_params_class_entry
, getThis(), ZEND_STRL("param_sep"), param_sep
);
951 switch (Z_TYPE_P(zparams
)) {
954 convert_to_array(zparams
);
955 zend_update_property(php_http_params_class_entry
, getThis(), ZEND_STRL("params"), zparams
);
958 zs
= zval_get_string(zparams
);
960 php_http_params_opts_t opts
= {
962 php_http_params_separator_init(zend_read_property(php_http_params_class_entry
, getThis(), ZEND_STRL("param_sep"), 0)),
963 php_http_params_separator_init(zend_read_property(php_http_params_class_entry
, getThis(), ZEND_STRL("arg_sep"), 0)),
964 php_http_params_separator_init(zend_read_property(php_http_params_class_entry
, getThis(), ZEND_STRL("val_sep"), 0)),
969 php_http_params_parse(Z_ARRVAL_P(zparams
), &opts
);
970 zend_update_property(php_http_params_class_entry
, getThis(), ZEND_STRL("params"), zparams
);
972 php_http_params_separator_free(opts
.param
);
973 php_http_params_separator_free(opts
.arg
);
974 php_http_params_separator_free(opts
.val
);
980 zend_update_property(php_http_params_class_entry
, getThis(), ZEND_STRL("params"), zparams
);
983 zend_restore_error_handling(&zeh
);
986 ZEND_BEGIN_ARG_INFO_EX(ai_HttpParams_toArray
, 0, 0, 0)
988 PHP_METHOD(HttpParams
, toArray
)
992 if (SUCCESS
!= zend_parse_parameters_none()) {
995 zparams
= zend_read_property(php_http_params_class_entry
, getThis(), ZEND_STRL("params"), 0);
996 RETURN_ZVAL_FAST(zparams
);
999 ZEND_BEGIN_ARG_INFO_EX(ai_HttpParams_toString
, 0, 0, 0)
1000 ZEND_END_ARG_INFO();
1001 PHP_METHOD(HttpParams
, toString
)
1003 zval
*tmp
, *zparams
, *zpsep
, *zasep
, *zvsep
;
1004 zend_string
*psep
, *asep
, *vsep
;
1006 php_http_buffer_t buf
;
1008 zparams
= zend_read_property(php_http_params_class_entry
, getThis(), ZEND_STRL("params"), 0);
1009 convert_to_array_ex(zparams
);
1010 flags
= zval_get_long(zend_read_property(php_http_params_class_entry
, getThis(), ZEND_STRL("flags"), 0));
1012 zpsep
= zend_read_property(php_http_params_class_entry
, getThis(), ZEND_STRL("param_sep"), 0);
1013 if (Z_TYPE_P(zpsep
) == IS_ARRAY
&& (tmp
= zend_hash_get_current_data(Z_ARRVAL_P(zpsep
)))) {
1014 psep
= zval_get_string(tmp
);
1016 psep
= zval_get_string(zpsep
);
1018 zasep
= zend_read_property(php_http_params_class_entry
, getThis(), ZEND_STRL("arg_sep"), 0);
1019 if (Z_TYPE_P(zasep
) == IS_ARRAY
&& (tmp
= zend_hash_get_current_data(Z_ARRVAL_P(zasep
)))) {
1020 asep
= zval_get_string(tmp
);
1022 asep
= zval_get_string(zasep
);
1024 zvsep
= zend_read_property(php_http_params_class_entry
, getThis(), ZEND_STRL("val_sep"), 0);
1025 if (Z_TYPE_P(zvsep
) == IS_ARRAY
&& (tmp
= zend_hash_get_current_data(Z_ARRVAL_P(zvsep
)))) {
1026 vsep
= zval_get_string(tmp
);
1028 vsep
= zval_get_string(zvsep
);
1031 php_http_buffer_init(&buf
);
1032 php_http_params_to_string(&buf
, Z_ARRVAL_P(zparams
), psep
->val
, psep
->len
, asep
->val
, asep
->len
, vsep
->val
, vsep
->len
, flags
);
1034 zend_string_release(psep
);
1035 zend_string_release(asep
);
1036 zend_string_release(vsep
);
1038 RETVAL_STR(php_http_cs2zs(buf
.data
, buf
.used
));
1041 ZEND_BEGIN_ARG_INFO_EX(ai_HttpParams_offsetExists
, 0, 0, 1)
1042 ZEND_ARG_INFO(0, name
)
1043 ZEND_END_ARG_INFO();
1044 PHP_METHOD(HttpParams
, offsetExists
)
1047 zval
*zparam
, *zparams
;
1049 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS(), "S", &name
)) {
1053 zparams
= zend_read_property(php_http_params_class_entry
, getThis(), ZEND_STRL("params"), 0);
1055 if (Z_TYPE_P(zparams
) == IS_ARRAY
&& (zparam
= zend_symtable_find(Z_ARRVAL_P(zparams
), name
))) {
1056 RETVAL_BOOL(Z_TYPE_P(zparam
) != IS_NULL
);
1062 ZEND_BEGIN_ARG_INFO_EX(ai_HttpParams_offsetGet
, 0, 0, 1)
1063 ZEND_ARG_INFO(0, name
)
1064 ZEND_END_ARG_INFO();
1065 PHP_METHOD(HttpParams
, offsetGet
)
1068 zval
*zparam
, *zparams
;
1070 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS(), "S", &name
)) {
1074 zparams
= zend_read_property(php_http_params_class_entry
, getThis(), ZEND_STRL("params"), 0);
1076 if (Z_TYPE_P(zparams
) == IS_ARRAY
&& (zparam
= zend_symtable_find(Z_ARRVAL_P(zparams
), name
))) {
1077 RETVAL_ZVAL_FAST(zparam
);
1081 ZEND_BEGIN_ARG_INFO_EX(ai_HttpParams_offsetUnset
, 0, 0, 1)
1082 ZEND_ARG_INFO(0, name
)
1083 ZEND_END_ARG_INFO();
1084 PHP_METHOD(HttpParams
, offsetUnset
)
1089 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS(), "S", &name
)) {
1093 zparams
= zend_read_property(php_http_params_class_entry
, getThis(), ZEND_STRL("params"), 0);
1095 if (Z_TYPE_P(zparams
) == IS_ARRAY
) {
1096 zend_symtable_del(Z_ARRVAL_P(zparams
), name
);
1097 zend_update_property(php_http_params_class_entry
, getThis(), ZEND_STRL("params"), zparams
);
1101 ZEND_BEGIN_ARG_INFO_EX(ai_HttpParams_offsetSet
, 0, 0, 2)
1102 ZEND_ARG_INFO(0, name
)
1103 ZEND_ARG_INFO(0, value
)
1104 ZEND_END_ARG_INFO();
1105 PHP_METHOD(HttpParams
, offsetSet
)
1108 zval
*zparam
, *zparams
, *nvalue
;
1110 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "Sz", &name
, &nvalue
)) {
1114 zparams
= zend_read_property(php_http_params_class_entry
, getThis(), ZEND_STRL("params"), 0);
1115 SEPARATE_ZVAL(zparams
);
1116 convert_to_array(zparams
);
1119 if (Z_TYPE_P(nvalue
) == IS_ARRAY
) {
1122 if ((zparam
= zend_symtable_find(Z_ARRVAL_P(zparams
), name
))) {
1123 new_zparam
= zparam
;
1124 SEPARATE_ZVAL(new_zparam
);
1125 convert_to_array(new_zparam
);
1126 array_join(Z_ARRVAL_P(nvalue
), Z_ARRVAL_P(new_zparam
), 0, 0);
1128 new_zparam
= nvalue
;
1129 Z_TRY_ADDREF_P(new_zparam
);
1131 add_assoc_zval_ex(zparams
, name
->val
, name
->len
, new_zparam
);
1135 if ((zparam
= zend_symtable_find(Z_ARRVAL_P(zparams
), name
))) {
1137 SEPARATE_ZVAL(&tmp
);
1138 convert_to_array(&tmp
);
1143 Z_TRY_ADDREF_P(nvalue
);
1144 add_assoc_zval_ex(&tmp
, ZEND_STRL("value"), nvalue
);
1145 add_assoc_zval_ex(zparams
, name
->val
, name
->len
, &tmp
);
1149 zend_string
*zs
= zval_get_string(nvalue
);
1152 add_assoc_bool_ex(&arr
, ZEND_STRL("value"), 1);
1153 add_assoc_zval_ex(zparams
, zs
->val
, zs
->len
, &arr
);
1154 zend_string_release(zs
);
1157 zend_update_property(php_http_params_class_entry
, getThis(), ZEND_STRL("params"), zparams
);
1158 zval_ptr_dtor(zparams
);
1161 static zend_function_entry php_http_params_methods
[] = {
1162 PHP_ME(HttpParams
, __construct
, ai_HttpParams___construct
, ZEND_ACC_PUBLIC
|ZEND_ACC_CTOR
|ZEND_ACC_FINAL
)
1164 PHP_ME(HttpParams
, toArray
, ai_HttpParams_toArray
, ZEND_ACC_PUBLIC
)
1165 PHP_ME(HttpParams
, toString
, ai_HttpParams_toString
, ZEND_ACC_PUBLIC
)
1166 ZEND_MALIAS(HttpParams
, __toString
, toString
, ai_HttpParams_toString
, ZEND_ACC_PUBLIC
)
1168 PHP_ME(HttpParams
, offsetExists
, ai_HttpParams_offsetExists
, ZEND_ACC_PUBLIC
)
1169 PHP_ME(HttpParams
, offsetUnset
, ai_HttpParams_offsetUnset
, ZEND_ACC_PUBLIC
)
1170 PHP_ME(HttpParams
, offsetSet
, ai_HttpParams_offsetSet
, ZEND_ACC_PUBLIC
)
1171 PHP_ME(HttpParams
, offsetGet
, ai_HttpParams_offsetGet
, ZEND_ACC_PUBLIC
)
1173 EMPTY_FUNCTION_ENTRY
1176 zend_class_entry
*php_http_params_class_entry
;
1178 PHP_MINIT_FUNCTION(http_params
)
1180 zend_class_entry ce
= {0};
1182 INIT_NS_CLASS_ENTRY(ce
, "http", "Params", php_http_params_methods
);
1183 php_http_params_class_entry
= zend_register_internal_class(&ce
);
1184 php_http_params_class_entry
->create_object
= php_http_params_object_new
;
1185 zend_class_implements(php_http_params_class_entry
, 1, zend_ce_arrayaccess
);
1187 zend_declare_class_constant_stringl(php_http_params_class_entry
, ZEND_STRL("DEF_PARAM_SEP"), ZEND_STRL(","));
1188 zend_declare_class_constant_stringl(php_http_params_class_entry
, ZEND_STRL("DEF_ARG_SEP"), ZEND_STRL(";"));
1189 zend_declare_class_constant_stringl(php_http_params_class_entry
, ZEND_STRL("DEF_VAL_SEP"), ZEND_STRL("="));
1190 zend_declare_class_constant_stringl(php_http_params_class_entry
, ZEND_STRL("COOKIE_PARAM_SEP"), ZEND_STRL(""));
1192 zend_declare_class_constant_long(php_http_params_class_entry
, ZEND_STRL("PARSE_RAW"), PHP_HTTP_PARAMS_RAW
);
1193 zend_declare_class_constant_long(php_http_params_class_entry
, ZEND_STRL("PARSE_ESCAPED"), PHP_HTTP_PARAMS_ESCAPED
);
1194 zend_declare_class_constant_long(php_http_params_class_entry
, ZEND_STRL("PARSE_URLENCODED"), PHP_HTTP_PARAMS_URLENCODED
);
1195 zend_declare_class_constant_long(php_http_params_class_entry
, ZEND_STRL("PARSE_DIMENSION"), PHP_HTTP_PARAMS_DIMENSION
);
1196 zend_declare_class_constant_long(php_http_params_class_entry
, ZEND_STRL("PARSE_RFC5987"), PHP_HTTP_PARAMS_RFC5987
);
1197 zend_declare_class_constant_long(php_http_params_class_entry
, ZEND_STRL("PARSE_DEFAULT"), PHP_HTTP_PARAMS_DEFAULT
);
1198 zend_declare_class_constant_long(php_http_params_class_entry
, ZEND_STRL("PARSE_QUERY"), PHP_HTTP_PARAMS_QUERY
);
1200 zend_declare_property_null(php_http_params_class_entry
, ZEND_STRL("params"), ZEND_ACC_PUBLIC
);
1201 zend_declare_property_stringl(php_http_params_class_entry
, ZEND_STRL("param_sep"), ZEND_STRL(","), ZEND_ACC_PUBLIC
);
1202 zend_declare_property_stringl(php_http_params_class_entry
, ZEND_STRL("arg_sep"), ZEND_STRL(";"), ZEND_ACC_PUBLIC
);
1203 zend_declare_property_stringl(php_http_params_class_entry
, ZEND_STRL("val_sep"), ZEND_STRL("="), ZEND_ACC_PUBLIC
);
1204 zend_declare_property_long(php_http_params_class_entry
, ZEND_STRL("flags"), PHP_HTTP_PARAMS_DEFAULT
, ZEND_ACC_PUBLIC
);
1214 * vim600: noet sw=4 ts=4 fdm=marker
1215 * vim<600: noet sw=4 ts=4