Merge branch 'v2.6.x'
[m6w6/ext-http] / src / php_http_params.c
1 /*
2 +--------------------------------------------------------------------+
3 | PECL :: http |
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 +--------------------------------------------------------------------+
11 */
12
13 #include "php_http_api.h"
14
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 = {
19 {NULL, 0},
20 def_param_sep_ptr,
21 def_arg_sep_ptr,
22 def_val_sep_ptr,
23 {{0}},
24 PHP_HTTP_PARAMS_DEFAULT
25 };
26
27 php_http_params_opts_t *php_http_params_opts_default_get(php_http_params_opts_t *opts)
28 {
29 if (!opts) {
30 opts = emalloc(sizeof(*opts));
31 }
32
33 memcpy(opts, &def_opts, sizeof(def_opts));
34
35 return opts;
36 }
37
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;
43 struct {
44 zval *param;
45 zval *args;
46 zval *val;
47 } current;
48 unsigned quotes:1;
49 unsigned escape:1;
50 unsigned rfc5987:1;
51 } php_http_params_state_t;
52
53 static inline void sanitize_escaped(zval *zv)
54 {
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);
58
59 zval_dtor(zv);
60 ZVAL_STR(zv, php_http_cs2zs(deq, deq_len));
61 }
62
63 php_stripcslashes(Z_STR_P(zv));
64 }
65
66 static inline void quote_string(zend_string **zs, zend_bool force)
67 {
68 int len = (*zs)->len;
69
70 *zs = php_addcslashes(*zs, 1, ZEND_STRL("\0..\37\173\\\""));
71
72 if (force || len != (*zs)->len || strpbrk((*zs)->val, "()<>@,;:\"[]?={} ")) {
73 int len = (*zs)->len + 2;
74
75 *zs = zend_string_extend(*zs, len, 0);
76
77 memmove(&(*zs)->val[1], (*zs)->val, (*zs)->len);
78 (*zs)->val[0] = '"';
79 (*zs)->val[len-1] = '"';
80 (*zs)->val[len] = '\0';
81
82 zend_string_forget_hash_val(*zs);
83 }
84 }
85
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\\\""));
90
91 if (len != stripped->len || strpbrk(stripped->val, "()<>@,;:\"[]?={} ")) {
92 size_t len = stripped->len + 2;
93 char *str = emalloc(len + 1);
94
95 str[0] = '"';
96 memcpy(&str[1], stripped->val, stripped->len);
97 str[len-1] = '"';
98 str[len] = '\0';
99
100 zval_dtor(zv);
101 zend_string_release(stripped);
102 ZVAL_STR(zv, php_http_cs2zs(str, len));
103 } else {
104 zval_dtor(zv);
105 ZVAL_STR(zv, stripped);
106 }
107 */
108
109 static inline void prepare_escaped(zval *zv)
110 {
111 if (Z_TYPE_P(zv) == IS_STRING) {
112 quote_string(&Z_STR_P(zv), 0);
113 } else {
114 zval_dtor(zv);
115 ZVAL_EMPTY_STRING(zv);
116 }
117 }
118
119 static inline void sanitize_urlencoded(zval *zv)
120 {
121 Z_STRLEN_P(zv) = php_raw_url_decode(Z_STRVAL_P(zv), Z_STRLEN_P(zv));
122 }
123
124 static inline void prepare_urlencoded(zval *zv)
125 {
126 zend_string *str = php_raw_url_encode(Z_STRVAL_P(zv), Z_STRLEN_P(zv));
127
128 zval_dtor(zv);
129 ZVAL_STR(zv, str);
130 }
131
132 static void sanitize_dimension(zval *zv)
133 {
134 zval arr, tmp, *cur = NULL;
135 char *var = NULL, *ptr = Z_STRVAL_P(zv), *end = Z_STRVAL_P(zv) + Z_STRLEN_P(zv);
136 long level = 0;
137
138 array_init(&arr);
139 cur = &arr;
140
141 while (ptr < end) {
142 if (!var) {
143 var = ptr;
144 }
145
146 switch (*ptr) {
147 case '[':
148 if (++level > PG(max_input_nesting_level)) {
149 zval_ptr_dtor(&arr);
150 php_error_docref(NULL, E_WARNING, "Max input nesting level of %ld exceeded", (long) PG(max_input_nesting_level));
151 return;
152 }
153 if (ptr - var == 0) {
154 ++var;
155 break;
156 }
157 /* no break */
158
159 case ']':
160
161 ZVAL_NULL(&tmp);
162 convert_to_array(cur);
163
164 if (ptr - var) {
165 char chr = *ptr;
166 *ptr = '\0';
167 cur = zend_symtable_str_update(Z_ARRVAL_P(cur), var, ptr - var, &tmp);
168 *ptr = chr;
169 } else {
170 cur = zend_hash_next_index_insert(Z_ARRVAL_P(cur), &tmp);
171 }
172
173 var = NULL;
174 break;
175 }
176
177 ++ptr;
178 }
179
180 if (zend_hash_num_elements(Z_ARRVAL(arr))) {
181 zval_dtor(zv);
182 ZVAL_COPY_VALUE(zv, &arr);
183 } else {
184 zval_ptr_dtor(&arr);
185 }
186 }
187
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);
190
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)
192 {
193 HashTable *ht = HASH_OF(zvalue);
194 php_http_arrkey_t key;
195 zval *val;
196 php_http_buffer_t prefix;
197
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);
202
203 ZEND_HASH_FOREACH_KEY_VAL_IND(ht, key.h, key.key, val)
204 {
205 if (key.key && !*key.key->val) {
206 /* only public properties */
207 continue;
208 }
209
210 php_http_buffer_appends(&prefix, "[");
211 if (key.key) {
212 php_http_buffer_append(&prefix, key.key->val, key.key->len);
213 } else {
214 php_http_buffer_appendf(&prefix, "%lu", key.h);
215 }
216 php_http_buffer_appends(&prefix, "]");
217
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);
220 } else {
221 zend_string *cpy = zval_get_string(val);
222 zval tmp;
223
224 ZVAL_STR(&tmp, cpy);
225 shift_key(buf, prefix.data, prefix.used, pss, psl, flags);
226 shift_val(buf, &tmp, vss, vsl, flags);
227 zend_string_release(cpy);
228 }
229
230 php_http_buffer_cut(&prefix, keybuf->used, prefix.used - keybuf->used);
231 }
232 ZEND_HASH_FOREACH_END();
233 ZEND_HASH_DEC_APPLY_COUNT(ht);
234
235 php_http_buffer_dtor(&prefix);
236 }
237 }
238
239 static inline void sanitize_key(unsigned flags, const char *str, size_t len, zval *zv, zend_bool *rfc5987)
240 {
241 char *eos;
242 zend_string *zs = zend_string_init(str, len, 0);
243
244 zval_dtor(zv);
245 ZVAL_STR(zv, php_trim(zs, NULL, 0, 3));
246 zend_string_release(zs);
247
248 if (flags & PHP_HTTP_PARAMS_ESCAPED) {
249 sanitize_escaped(zv);
250 }
251
252 if (!Z_STRLEN_P(zv)) {
253 return;
254 }
255
256 if (flags & PHP_HTTP_PARAMS_RFC5987) {
257 eos = &Z_STRVAL_P(zv)[Z_STRLEN_P(zv)-1];
258 if (*eos == '*') {
259 *eos = '\0';
260 *rfc5987 = 1;
261 Z_STRLEN_P(zv) -= 1;
262 }
263 }
264
265 if (flags & PHP_HTTP_PARAMS_URLENCODED) {
266 sanitize_urlencoded(zv);
267 }
268
269 if (flags & PHP_HTTP_PARAMS_DIMENSION) {
270 sanitize_dimension(zv);
271 }
272 }
273
274 static inline void sanitize_rfc5987(zval *zv, char **language, zend_bool *latin1)
275 {
276 char *ptr;
277
278 /* examples:
279 * iso-8850-1'de'bl%f6der%20schei%df%21
280 * utf-8'de-DE'bl%c3%b6der%20schei%c3%9f%21
281 */
282
283 switch (Z_STRVAL_P(zv)[0]) {
284 case 'I':
285 case 'i':
286 if (!strncasecmp(Z_STRVAL_P(zv), "iso-8859-1", lenof("iso-8859-1"))) {
287 *latin1 = 1;
288 ptr = Z_STRVAL_P(zv) + lenof("iso-8859-1");
289 break;
290 }
291 /* no break */
292 case 'U':
293 case 'u':
294 if (!strncasecmp(Z_STRVAL_P(zv), "utf-8", lenof("utf-8"))) {
295 *latin1 = 0;
296 ptr = Z_STRVAL_P(zv) + lenof("utf-8");
297 break;
298 }
299 /* no break */
300 default:
301 return;
302 }
303
304 /* extract language */
305 if (*ptr == '\'') {
306 for (*language = ++ptr; *ptr && *ptr != '\''; ++ptr);
307 if (!*ptr) {
308 *language = NULL;
309 return;
310 }
311 *language = estrndup(*language, ptr - *language);
312
313 /* remainder */
314 ptr = estrdup(++ptr);
315 zval_dtor(zv);
316 ZVAL_STR(zv, php_http_cs2zs(ptr, strlen(ptr)));
317 }
318 }
319
320 static inline void sanitize_rfc5988(char *str, size_t len, zval *zv)
321 {
322 zend_string *zs = zend_string_init(str, len, 0);
323
324 zval_dtor(zv);
325 ZVAL_STR(zv, php_trim(zs, " ><", 3, 3));
326 zend_string_release(zs);
327 }
328
329 static inline void prepare_rfc5988(zval *zv)
330 {
331 if (Z_TYPE_P(zv) != IS_STRING) {
332 zval_dtor(zv);
333 ZVAL_EMPTY_STRING(zv);
334 }
335 }
336
337 static void utf8encode(zval *zv)
338 {
339 size_t pos, len = 0;
340 unsigned char *ptr = (unsigned char *) Z_STRVAL_P(zv);
341
342 while (*ptr) {
343 if (*ptr++ >= 0x80) {
344 ++len;
345 }
346 ++len;
347 }
348
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);
355 ++pos;
356 }
357 }
358 zval_dtor(zv);
359 ZVAL_STR(zv, php_http_cs2zs((char *) ptr, pos-1));
360 }
361
362 static inline void sanitize_value(unsigned flags, const char *str, size_t len, zval *zv, zend_bool rfc5987)
363 {
364 char *language = NULL;
365 zend_bool latin1 = 0;
366 zend_string *zs = zend_string_init(str, len, 0);
367
368 zval_dtor(zv);
369 ZVAL_STR(zv, php_trim(zs, NULL, 0, 3));
370 zend_string_release(zs);
371
372 if (rfc5987) {
373 sanitize_rfc5987(zv, &language, &latin1);
374 }
375
376 if (flags & PHP_HTTP_PARAMS_ESCAPED) {
377 sanitize_escaped(zv);
378 }
379
380 if ((flags & PHP_HTTP_PARAMS_URLENCODED) || (rfc5987 && language)) {
381 sanitize_urlencoded(zv);
382 }
383
384 if (rfc5987 && language) {
385 zval tmp;
386
387 if (latin1) {
388 utf8encode(zv);
389 }
390
391 ZVAL_COPY_VALUE(&tmp, zv);
392 array_init(zv);
393 add_assoc_zval(zv, language, &tmp);
394 efree(language);
395 }
396 }
397
398 static inline void prepare_key(unsigned flags, char *old_key, size_t old_len, char **new_key, size_t *new_len)
399 {
400 zval zv;
401
402 ZVAL_STRINGL(&zv, old_key, old_len);
403
404 if (flags & PHP_HTTP_PARAMS_URLENCODED) {
405 prepare_urlencoded(&zv);
406 }
407
408 if (flags & PHP_HTTP_PARAMS_ESCAPED) {
409 if (flags & PHP_HTTP_PARAMS_RFC5988) {
410 prepare_rfc5988(&zv);
411 } else {
412 prepare_escaped(&zv);
413 }
414 }
415
416 *new_key = estrndup(Z_STRVAL(zv), Z_STRLEN(zv));
417 *new_len = Z_STRLEN(zv);
418 zval_ptr_dtor(&zv);
419 }
420
421 static inline void prepare_value(unsigned flags, zval *zv)
422 {
423 if (flags & PHP_HTTP_PARAMS_URLENCODED) {
424 prepare_urlencoded(zv);
425 }
426
427 if (flags & PHP_HTTP_PARAMS_ESCAPED) {
428 prepare_escaped(zv);
429 }
430 }
431
432 static void merge_param(HashTable *params, zval *zdata, zval **current_param, zval **current_args)
433 {
434 zval *ptr, *zdata_ptr;
435 php_http_arrkey_t hkey = {0};
436
437 #if 0
438 {
439 zval tmp;
440 INIT_PZVAL_ARRAY(&tmp, params);
441 fprintf(stderr, "params = ");
442 zend_print_zval_r(&tmp, 1);
443 fprintf(stderr, "\n");
444 }
445 #endif
446
447 zend_hash_get_current_key(Z_ARRVAL_P(zdata), &hkey.key, &hkey.h);
448
449 if ((hkey.key && !zend_hash_exists(params, hkey.key))
450 || (!hkey.key && !zend_hash_index_exists(params, hkey.h))
451 ) {
452 zval tmp, arg, *args;
453
454 /* create the entry if it doesn't exist */
455 ptr = zend_hash_get_current_data(Z_ARRVAL_P(zdata));
456 Z_TRY_ADDREF_P(ptr);
457 array_init(&tmp);
458 add_assoc_zval_ex(&tmp, ZEND_STRL("value"), ptr);
459
460 array_init(&arg);
461 args = zend_hash_str_update(Z_ARRVAL(tmp), "arguments", lenof("arguments"), &arg);
462 *current_args = args;
463
464 if (hkey.key) {
465 ptr = zend_hash_update(params, hkey.key, &tmp);
466 } else {
467 ptr = zend_hash_index_update(params, hkey.h, &tmp);
468 }
469 } else {
470 /* merge */
471 if (hkey.key) {
472 ptr = zend_hash_find(params, hkey.key);
473 } else {
474 ptr = zend_hash_index_find(params, hkey.h);
475 }
476
477 zdata_ptr = zdata;
478
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)))
482 ) {
483 /*
484 * params = [arr => [value => [0 => 1]]]
485 * ^- ptr
486 * zdata = [arr => [0 => NULL]]
487 * ^- zdata_ptr
488 */
489 zval *test_ptr;
490
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) {
493 zval *tmp_ptr = ptr;
494
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;
499 } else {
500 ptr = tmp_ptr;
501 Z_TRY_ADDREF_P(test_ptr);
502 ptr = zend_hash_update(Z_ARRVAL_P(ptr), hkey.key, test_ptr);
503 break;
504 }
505 } else {
506 if ((ptr = zend_hash_index_find(Z_ARRVAL_P(ptr), hkey.h))) {
507 zdata_ptr = test_ptr;
508 } else if (hkey.h) {
509 ptr = tmp_ptr;
510 Z_TRY_ADDREF_P(test_ptr);
511 ptr = zend_hash_index_update(Z_ARRVAL_P(ptr), hkey.h, test_ptr);
512 break;
513 } else {
514 ptr = tmp_ptr;
515 Z_TRY_ADDREF_P(test_ptr);
516 ptr = zend_hash_next_index_insert(Z_ARRVAL_P(ptr), test_ptr);
517 break;
518 }
519 }
520 } else {
521 /* this is the leaf */
522 Z_TRY_ADDREF_P(test_ptr);
523 if (Z_TYPE_P(ptr) != IS_ARRAY) {
524 zval_dtor(ptr);
525 array_init(ptr);
526 }
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);
529 } else if (hkey.h) {
530 ptr = zend_hash_index_update(Z_ARRVAL_P(ptr), hkey.h, test_ptr);
531 } else {
532 ptr = zend_hash_next_index_insert(Z_ARRVAL_P(ptr), test_ptr);
533 }
534 break;
535 }
536 }
537
538 }
539 }
540
541 /* bubble up */
542 while (Z_TYPE_P(ptr) == IS_ARRAY) {
543 zval *tmp = zend_hash_get_current_data(Z_ARRVAL_P(ptr));
544
545 if (tmp) {
546 ptr = tmp;
547 } else {
548 break;
549 }
550 }
551 *current_param = ptr;
552 }
553
554 static void push_param(HashTable *params, php_http_params_state_t *state, const php_http_params_opts_t *opts)
555 {
556 if (state->val.str) {
557 if (0 < (state->val.len = state->input.str - state->val.str)) {
558 sanitize_value(opts->flags, state->val.str, state->val.len, state->current.val, state->rfc5987);
559 } else {
560 ZVAL_EMPTY_STRING(state->current.val);
561 }
562 state->rfc5987 = 0;
563 } else if (state->arg.str) {
564 if (0 < (state->arg.len = state->input.str - state->arg.str)) {
565 zval val, key;
566 zend_bool rfc5987 = 0;
567
568 ZVAL_NULL(&key);
569 sanitize_key(opts->flags, state->arg.str, state->arg.len, &key, &rfc5987);
570 state->rfc5987 = rfc5987;
571 if (Z_TYPE(key) == IS_STRING && Z_STRLEN(key)) {
572 ZVAL_TRUE(&val);
573
574 if (rfc5987) {
575 zval *rfc;
576
577 if ((rfc = zend_hash_str_find(Z_ARRVAL_P(state->current.args), ZEND_STRL("*rfc5987*")))) {
578 state->current.val = zend_symtable_str_update(Z_ARRVAL_P(rfc), Z_STRVAL(key), Z_STRLEN(key), &val);
579 } else {
580 zval tmp;
581
582 array_init_size(&tmp, 1);
583 state->current.val = zend_symtable_str_update(Z_ARRVAL(tmp), Z_STRVAL(key), Z_STRLEN(key), &val);
584 zend_symtable_str_update(Z_ARRVAL_P(state->current.args), ZEND_STRL("*rfc5987*"), &tmp);
585 }
586 } else {
587 state->current.val = zend_symtable_str_update(Z_ARRVAL_P(state->current.args), Z_STRVAL(key), Z_STRLEN(key), &val);
588 }
589 }
590 zval_dtor(&key);
591 }
592 } else if (state->param.str) {
593 if (0 < (state->param.len = state->input.str - state->param.str)) {
594 zval prm, arg, val, key;
595 zend_bool rfc5987 = 0;
596
597 ZVAL_NULL(&key);
598 if (opts->flags & PHP_HTTP_PARAMS_RFC5988) {
599 sanitize_rfc5988(state->param.str, state->param.len, &key);
600 } else {
601 sanitize_key(opts->flags, state->param.str, state->param.len, &key, &rfc5987);
602 state->rfc5987 = rfc5987;
603 }
604 if (Z_TYPE(key) == IS_ARRAY) {
605 merge_param(params, &key, &state->current.val, &state->current.args);
606 } else if (Z_TYPE(key) == IS_STRING && Z_STRLEN(key)) {
607 // FIXME: array_init_size(&prm, 2);
608 array_init(&prm);
609
610 if (!Z_ISUNDEF(opts->defval)) {
611 ZVAL_COPY_VALUE(&val, &opts->defval);
612 zval_copy_ctor(&val);
613 } else {
614 ZVAL_TRUE(&val);
615 }
616 if (rfc5987 && (opts->flags & PHP_HTTP_PARAMS_RFC5987)) {
617 state->current.val = zend_hash_str_update(Z_ARRVAL(prm), "*rfc5987*", lenof("*rfc5987*"), &val);
618 } else {
619 state->current.val = zend_hash_str_update(Z_ARRVAL(prm), "value", lenof("value"), &val);
620 }
621 // FIXME: array_init_size(&arg, 3);
622 array_init(&arg);
623 state->current.args = zend_hash_str_update(Z_ARRVAL(prm), "arguments", lenof("arguments"), &arg);
624 state->current.param = zend_symtable_str_update(params, Z_STRVAL(key), Z_STRLEN(key), &prm);
625 }
626 zval_ptr_dtor(&key);
627 }
628 }
629 }
630
631 static inline zend_bool check_str(const char *chk_str, size_t chk_len, const char *sep_str, size_t sep_len) {
632 return 0 < sep_len && chk_len >= sep_len && *chk_str == *sep_str && !memcmp(chk_str + 1, sep_str + 1, sep_len - 1);
633 }
634
635 static size_t check_sep(php_http_params_state_t *state, php_http_params_token_t **separators)
636 {
637 php_http_params_token_t **sep = separators;
638
639 if (state->quotes || state->escape) {
640 return 0;
641 }
642
643 if (sep) while (*sep) {
644 if (check_str(state->input.str, state->input.len, (*sep)->str, (*sep)->len)) {
645 return (*sep)->len;
646 }
647 ++sep;
648 }
649 return 0;
650 }
651
652 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)
653 {
654 size_t sep_len;
655
656 state->input.str += skip;
657 state->input.len -= skip;
658
659 while ( (param && (sep_len = check_sep(state, param)))
660 || (arg && (sep_len = check_sep(state, arg)))
661 || (val && (sep_len = check_sep(state, val)))
662 ) {
663 state->input.str += sep_len;
664 state->input.len -= sep_len;
665 }
666 }
667
668 HashTable *php_http_params_parse(HashTable *params, const php_http_params_opts_t *opts)
669 {
670 php_http_params_state_t state = {{NULL,0}, {NULL,0}, {NULL,0}, {NULL,0}, {NULL,NULL,NULL}, 0, 0};
671
672 state.input.str = opts->input.str;
673 state.input.len = opts->input.len;
674
675 if (!params) {
676 ALLOC_HASHTABLE(params);
677 ZEND_INIT_SYMTABLE(params);
678 }
679
680 while (state.input.len) {
681 if ((opts->flags & PHP_HTTP_PARAMS_RFC5988) && !state.arg.str) {
682 if (*state.input.str == '<') {
683 state.quotes = 1;
684 } else if (*state.input.str == '>') {
685 state.quotes = 0;
686 }
687 } else if (*state.input.str == '"' && !state.escape) {
688 state.quotes = !state.quotes;
689 } else {
690 state.escape = (*state.input.str == '\\');
691 }
692
693 if (!state.param.str) {
694 /* initialize */
695 skip_sep(0, &state, opts->param, opts->arg, opts->val);
696 state.param.str = state.input.str;
697 } else {
698 size_t sep_len;
699 /* are we at a param separator? */
700 if (0 < (sep_len = check_sep(&state, opts->param))) {
701 push_param(params, &state, opts);
702
703 skip_sep(sep_len, &state, opts->param, opts->arg, opts->val);
704
705 /* start off with a new param */
706 state.param.str = state.input.str;
707 state.param.len = 0;
708 state.arg.str = NULL;
709 state.arg.len = 0;
710 state.val.str = NULL;
711 state.val.len = 0;
712
713 continue;
714
715 } else
716 /* are we at an arg separator? */
717 if (0 < (sep_len = check_sep(&state, opts->arg))) {
718 push_param(params, &state, opts);
719
720 skip_sep(sep_len, &state, NULL, opts->arg, opts->val);
721
722 /* continue with a new arg */
723 state.arg.str = state.input.str;
724 state.arg.len = 0;
725 state.val.str = NULL;
726 state.val.len = 0;
727
728 continue;
729
730 } else
731 /* are we at a val separator? */
732 if (0 < (sep_len = check_sep(&state, opts->val))) {
733 /* only handle separator if we're not already reading in a val */
734 if (!state.val.str) {
735 push_param(params, &state, opts);
736
737 skip_sep(sep_len, &state, NULL, NULL, opts->val);
738
739 state.val.str = state.input.str;
740 state.val.len = 0;
741
742 continue;
743 }
744 }
745 }
746
747 if (state.input.len) {
748 ++state.input.str;
749 --state.input.len;
750 }
751 }
752 /* finalize */
753 push_param(params, &state, opts);
754
755 return params;
756 }
757
758 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)
759 {
760 char *str;
761 size_t len;
762
763 if (buf->used) {
764 php_http_buffer_append(buf, ass, asl);
765 }
766
767 prepare_key(flags, key_str, key_len, &str, &len);
768 php_http_buffer_append(buf, str, len);
769 efree(str);
770 }
771
772 static inline void shift_rfc5987(php_http_buffer_t *buf, zval *zvalue, const char *vss, size_t vsl, unsigned flags)
773 {
774 HashTable *ht = HASH_OF(zvalue);
775 zval *zdata, tmp;
776 zend_string *zs;
777 php_http_arrkey_t key = {0};
778
779 if ((zdata = zend_hash_get_current_data(ht))
780 && HASH_KEY_NON_EXISTENT != zend_hash_get_current_key(ht, &key.key, &key.h)
781 ) {
782 php_http_arrkey_stringify(&key, NULL);
783 php_http_buffer_appendf(buf, "*%.*sutf-8'%.*s'",
784 (int) (vsl > INT_MAX ? INT_MAX : vsl), vss,
785 (int) (key.key->len > INT_MAX ? INT_MAX : key.key->len), key.key->val);
786 php_http_arrkey_dtor(&key);
787
788 if (Z_TYPE_P(zdata) == IS_INDIRECT) {
789 zdata = Z_INDIRECT_P(zdata);
790 }
791 zs = zval_get_string(zdata);
792 ZVAL_STR(&tmp, zs);
793 prepare_value(flags | PHP_HTTP_PARAMS_URLENCODED, &tmp);
794 php_http_buffer_append(buf, Z_STRVAL(tmp), Z_STRLEN(tmp));
795 zval_ptr_dtor(&tmp);
796 }
797 }
798
799 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)
800 {
801 char *str;
802 size_t len;
803
804 if (buf->used) {
805 php_http_buffer_append(buf, ass, asl);
806 }
807
808 prepare_key(flags, key_str, key_len, &str, &len);
809 php_http_buffer_appends(buf, "<");
810 php_http_buffer_append(buf, str, len);
811 php_http_buffer_appends(buf, ">");
812 efree(str);
813 }
814
815 static inline void shift_rfc5988_val(php_http_buffer_t *buf, zval *zv, const char *vss, size_t vsl, unsigned flags)
816 {
817 zend_string *zs = zval_get_string(zv);
818
819 quote_string(&zs, 1);
820 php_http_buffer_append(buf, vss, vsl);
821 php_http_buffer_append(buf, zs->val, zs->len);
822
823 zend_string_release(zs);
824 }
825
826 static inline void shift_val(php_http_buffer_t *buf, zval *zvalue, const char *vss, size_t vsl, unsigned flags)
827 {
828 zval tmp;
829 zend_string *zs;
830
831 switch (Z_TYPE_P(zvalue)) {
832 case IS_TRUE:
833 break;
834
835 case IS_FALSE:
836 php_http_buffer_append(buf, vss, vsl);
837 php_http_buffer_appends(buf, "0");
838 break;
839
840 default:
841 zs = zval_get_string(zvalue);
842
843 ZVAL_STR(&tmp, zs);
844 prepare_value(flags, &tmp);
845 php_http_buffer_append(buf, vss, vsl);
846 php_http_buffer_append(buf, Z_STRVAL(tmp), Z_STRLEN(tmp));
847
848 zval_ptr_dtor(&tmp);
849 break;
850 }
851 }
852
853 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)
854 {
855 if (Z_TYPE_P(zvalue) == IS_ARRAY || Z_TYPE_P(zvalue) == IS_OBJECT) {
856 php_http_arrkey_t key;
857 HashTable *ht = HASH_OF(zvalue);
858 zval *val;
859 zend_bool rfc5987 = !strcmp(key_str, "*rfc5987*");
860
861 if (!rfc5987) {
862 shift_key(buf, key_str, key_len, ass, asl, flags);
863 }
864 ZEND_HASH_FOREACH_KEY_VAL_IND(ht, key.h, key.key, val)
865 {
866 /* did you mean recursion? */
867 php_http_arrkey_stringify(&key, NULL);
868 if (rfc5987 && (Z_TYPE_P(val) == IS_ARRAY || Z_TYPE_P(val) == IS_OBJECT)) {
869 shift_key(buf, key.key->val, key.key->len, ass, asl, flags);
870 shift_rfc5987(buf, val, vss, vsl, flags);
871 } else {
872 shift_arg(buf, key.key->val, key.key->len, val, ass, asl, vss, vsl, flags);
873 }
874 php_http_arrkey_dtor(&key);
875 }
876 ZEND_HASH_FOREACH_END();
877 } else {
878 shift_key(buf, key_str, key_len, ass, asl, flags);
879
880 if (flags & PHP_HTTP_PARAMS_RFC5988) {
881 switch (key_len) {
882 case lenof("rel"):
883 case lenof("title"):
884 case lenof("anchor"):
885 /* some args must be quoted */
886 if (0 <= php_http_select_str(key_str, 3, "rel", "title", "anchor")) {
887 shift_rfc5988_val(buf, zvalue, vss, vsl, flags);
888 return;
889 }
890 break;
891 }
892 }
893
894 shift_val(buf, zvalue, vss, vsl, flags);
895 }
896 }
897
898 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)
899 {
900 if (Z_TYPE_P(zvalue) == IS_ARRAY || Z_TYPE_P(zvalue) == IS_OBJECT) {
901 /* treat as arguments, unless we care for dimensions or rfc5987 */
902 if (flags & PHP_HTTP_PARAMS_DIMENSION) {
903 php_http_buffer_t *keybuf = php_http_buffer_from_string(key_str, key_len);
904 prepare_dimension(buf, keybuf, zvalue, pss, psl, vss, vsl, flags);
905 php_http_buffer_free(&keybuf);
906 } else if (rfc5987) {
907 shift_key(buf, key_str, key_len, pss, psl, flags);
908 shift_rfc5987(buf, zvalue, vss, vsl, flags);
909 } else {
910 shift_arg(buf, key_str, key_len, zvalue, ass, asl, vss, vsl, flags);
911 }
912 } else {
913 if (flags & PHP_HTTP_PARAMS_RFC5988) {
914 shift_rfc5988(buf, key_str, key_len, pss, psl, flags);
915 } else {
916 shift_key(buf, key_str, key_len, pss, psl, flags);
917 }
918 shift_val(buf, zvalue, vss, vsl, flags);
919 }
920 }
921
922 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)
923 {
924 zval *zparam;
925 php_http_arrkey_t key;
926 zend_bool rfc5987 = 0;
927
928 if (!buf) {
929 buf = php_http_buffer_init(NULL);
930 }
931
932 ZEND_HASH_FOREACH_KEY_VAL(params, key.h, key.key, zparam)
933 {
934 zval *zvalue, *zargs;
935
936 if (Z_TYPE_P(zparam) != IS_ARRAY) {
937 zvalue = zparam;
938 } else {
939 if (!(zvalue = zend_hash_str_find(Z_ARRVAL_P(zparam), ZEND_STRL("value")))) {
940 if (!(zvalue = zend_hash_str_find(Z_ARRVAL_P(zparam), ZEND_STRL("*rfc5987*")))) {
941 zvalue = zparam;
942 } else {
943 rfc5987 = 1;
944 }
945 }
946 }
947
948 php_http_arrkey_stringify(&key, NULL);
949 shift_param(buf, key.key->val, key.key->len, zvalue, pss, psl, ass, asl, vss, vsl, flags, rfc5987);
950 php_http_arrkey_dtor(&key);
951
952 if (Z_TYPE_P(zparam) == IS_ARRAY) {
953 zval *tmp = zend_hash_str_find(Z_ARRVAL_P(zparam), ZEND_STRL("arguments"));
954
955 if (tmp) {
956 zvalue = tmp;
957 } else if (zvalue == zparam) {
958 continue;
959 } else {
960 zvalue = zparam;
961 }
962 }
963
964 if (Z_TYPE_P(zvalue) == IS_ARRAY) {
965 ZEND_HASH_FOREACH_KEY_VAL(Z_ARRVAL_P(zvalue), key.h, key.key, zargs)
966 {
967 if (zvalue == zparam && key.key && zend_string_equals_literal(key.key, "value")) {
968 continue;
969 }
970
971 php_http_arrkey_stringify(&key, NULL);
972 shift_arg(buf, key.key->val, key.key->len, zargs, ass, asl, vss, vsl, flags);
973 php_http_arrkey_dtor(&key);
974 }
975 ZEND_HASH_FOREACH_END();
976 }
977 }
978 ZEND_HASH_FOREACH_END();
979
980 php_http_buffer_shrink(buf);
981 php_http_buffer_fix(buf);
982
983 return buf;
984 }
985
986 php_http_params_token_t **php_http_params_separator_init(zval *zv)
987 {
988 zval *sep, ztmp;
989 php_http_params_token_t **ret, **tmp;
990
991 if (!zv) {
992 return NULL;
993 }
994
995 ZVAL_DUP(&ztmp, zv);
996 zv = &ztmp;
997 convert_to_array(zv);
998
999 ret = ecalloc(zend_hash_num_elements(Z_ARRVAL_P(zv)) + 1, sizeof(*ret));
1000
1001 tmp = ret;
1002 ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(zv), sep)
1003 {
1004 zend_string *zs = zval_get_string(sep);
1005
1006 if (zs->len) {
1007 *tmp = emalloc(sizeof(**tmp));
1008 (*tmp)->str = estrndup(zs->val, (*tmp)->len = zs->len);
1009 ++tmp;
1010 }
1011 zend_string_release(zs);
1012 }
1013 ZEND_HASH_FOREACH_END();
1014
1015 zval_ptr_dtor(&ztmp);
1016
1017 *tmp = NULL;
1018 return ret;
1019 }
1020
1021 void php_http_params_separator_free(php_http_params_token_t **separator)
1022 {
1023 php_http_params_token_t **sep = separator;
1024 if (sep) {
1025 while (*sep) {
1026 PTR_FREE((*sep)->str);
1027 efree(*sep);
1028 ++sep;
1029 }
1030 efree(separator);
1031 }
1032 }
1033
1034 static zend_class_entry *php_http_params_class_entry;
1035 zend_class_entry *php_http_params_get_class_entry(void)
1036 {
1037 return php_http_params_class_entry;
1038 }
1039
1040 ZEND_BEGIN_ARG_INFO_EX(ai_HttpParams___construct, 0, 0, 0)
1041 ZEND_ARG_INFO(0, params)
1042 ZEND_ARG_INFO(0, param_sep)
1043 ZEND_ARG_INFO(0, arg_sep)
1044 ZEND_ARG_INFO(0, val_sep)
1045 ZEND_ARG_INFO(0, flags)
1046 ZEND_END_ARG_INFO();
1047 PHP_METHOD(HttpParams, __construct)
1048 {
1049 zval *zparams = NULL, *param_sep = NULL, *arg_sep = NULL, *val_sep = NULL;
1050 zend_long flags = PHP_HTTP_PARAMS_DEFAULT;
1051 zend_error_handling zeh;
1052 zend_string *zs;
1053
1054 php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS(), "|z!/z/z/z/l", &zparams, &param_sep, &arg_sep, &val_sep, &flags), invalid_arg, return);
1055
1056 zend_replace_error_handling(EH_THROW, php_http_get_exception_runtime_class_entry(), &zeh);
1057 {
1058 switch (ZEND_NUM_ARGS()) {
1059 case 5:
1060 zend_update_property_long(php_http_params_class_entry, getThis(), ZEND_STRL("flags"), flags);
1061 /* no break */
1062 case 4:
1063 zend_update_property(php_http_params_class_entry, getThis(), ZEND_STRL("val_sep"), val_sep);
1064 /* no break */
1065 case 3:
1066 zend_update_property(php_http_params_class_entry, getThis(), ZEND_STRL("arg_sep"), arg_sep);
1067 /* no break */
1068 case 2:
1069 zend_update_property(php_http_params_class_entry, getThis(), ZEND_STRL("param_sep"), param_sep);
1070 /* no break */
1071 }
1072
1073 if (zparams) {
1074 switch (Z_TYPE_P(zparams)) {
1075 case IS_OBJECT:
1076 case IS_ARRAY:
1077 convert_to_array(zparams);
1078 zend_update_property(php_http_params_class_entry, getThis(), ZEND_STRL("params"), zparams);
1079 break;
1080 default:
1081 zs = zval_get_string(zparams);
1082 if (zs->len) {
1083 zval tmp;
1084
1085 php_http_params_opts_t opts = {
1086 {zs->val, zs->len},
1087 php_http_params_separator_init(zend_read_property(php_http_params_class_entry, getThis(), ZEND_STRL("param_sep"), 0, &tmp)),
1088 php_http_params_separator_init(zend_read_property(php_http_params_class_entry, getThis(), ZEND_STRL("arg_sep"), 0, &tmp)),
1089 php_http_params_separator_init(zend_read_property(php_http_params_class_entry, getThis(), ZEND_STRL("val_sep"), 0, &tmp)),
1090 {{0}}, flags
1091 };
1092
1093 array_init(&tmp);
1094 php_http_params_parse(Z_ARRVAL(tmp), &opts);
1095 zend_update_property(php_http_params_class_entry, getThis(), ZEND_STRL("params"), &tmp);
1096 zval_ptr_dtor(&tmp);
1097
1098 php_http_params_separator_free(opts.param);
1099 php_http_params_separator_free(opts.arg);
1100 php_http_params_separator_free(opts.val);
1101 }
1102 zend_string_release(zs);
1103 break;
1104 }
1105 } else {
1106 zval tmp;
1107
1108 array_init(&tmp);
1109 zend_update_property(php_http_params_class_entry, getThis(), ZEND_STRL("params"), &tmp);
1110 zval_ptr_dtor(&tmp);
1111 }
1112 }
1113 zend_restore_error_handling(&zeh);
1114 }
1115
1116 ZEND_BEGIN_ARG_INFO_EX(ai_HttpParams_toArray, 0, 0, 0)
1117 ZEND_END_ARG_INFO();
1118 PHP_METHOD(HttpParams, toArray)
1119 {
1120 zval zparams_tmp, *zparams;
1121
1122 if (SUCCESS != zend_parse_parameters_none()) {
1123 return;
1124 }
1125 zparams = zend_read_property(php_http_params_class_entry, getThis(), ZEND_STRL("params"), 0, &zparams_tmp);
1126 RETURN_ZVAL(zparams, 1, 0);
1127 }
1128
1129 ZEND_BEGIN_ARG_INFO_EX(ai_HttpParams_toString, 0, 0, 0)
1130 ZEND_END_ARG_INFO();
1131 PHP_METHOD(HttpParams, toString)
1132 {
1133 zval *tmp, *zparams, *zpsep, *zasep, *zvsep;
1134 zval zparams_tmp, flags_tmp, psep_tmp, asep_tmp, vsep_tmp;
1135 zend_string *psep, *asep, *vsep;
1136 long flags;
1137 php_http_buffer_t buf;
1138
1139 zparams = zend_read_property(php_http_params_class_entry, getThis(), ZEND_STRL("params"), 0, &zparams_tmp);
1140 convert_to_array_ex(zparams);
1141 flags = zval_get_long(zend_read_property(php_http_params_class_entry, getThis(), ZEND_STRL("flags"), 0, &flags_tmp));
1142
1143 zpsep = zend_read_property(php_http_params_class_entry, getThis(), ZEND_STRL("param_sep"), 0, &psep_tmp);
1144 if (Z_TYPE_P(zpsep) == IS_ARRAY && (tmp = zend_hash_get_current_data(Z_ARRVAL_P(zpsep)))) {
1145 psep = zval_get_string(tmp);
1146 } else {
1147 psep = zval_get_string(zpsep);
1148 }
1149 zasep = zend_read_property(php_http_params_class_entry, getThis(), ZEND_STRL("arg_sep"), 0, &asep_tmp);
1150 if (Z_TYPE_P(zasep) == IS_ARRAY && (tmp = zend_hash_get_current_data(Z_ARRVAL_P(zasep)))) {
1151 asep = zval_get_string(tmp);
1152 } else {
1153 asep = zval_get_string(zasep);
1154 }
1155 zvsep = zend_read_property(php_http_params_class_entry, getThis(), ZEND_STRL("val_sep"), 0, &vsep_tmp);
1156 if (Z_TYPE_P(zvsep) == IS_ARRAY && (tmp = zend_hash_get_current_data(Z_ARRVAL_P(zvsep)))) {
1157 vsep = zval_get_string(tmp);
1158 } else {
1159 vsep = zval_get_string(zvsep);
1160 }
1161
1162 php_http_buffer_init(&buf);
1163 php_http_params_to_string(&buf, Z_ARRVAL_P(zparams), psep->val, psep->len, asep->val, asep->len, vsep->val, vsep->len, flags);
1164
1165 zend_string_release(psep);
1166 zend_string_release(asep);
1167 zend_string_release(vsep);
1168
1169 RETVAL_STR(php_http_cs2zs(buf.data, buf.used));
1170 }
1171
1172 ZEND_BEGIN_ARG_INFO_EX(ai_HttpParams_offsetExists, 0, 0, 1)
1173 ZEND_ARG_INFO(0, name)
1174 ZEND_END_ARG_INFO();
1175 PHP_METHOD(HttpParams, offsetExists)
1176 {
1177 zend_string *name;
1178 zval zparams_tmp, *zparam, *zparams;
1179
1180 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS(), "S", &name)) {
1181 return;
1182 }
1183
1184 zparams = zend_read_property(php_http_params_class_entry, getThis(), ZEND_STRL("params"), 0, &zparams_tmp);
1185
1186 if (Z_TYPE_P(zparams) == IS_ARRAY && (zparam = zend_symtable_find(Z_ARRVAL_P(zparams), name))) {
1187 RETVAL_BOOL(Z_TYPE_P(zparam) != IS_NULL);
1188 } else {
1189 RETVAL_FALSE;
1190 }
1191 }
1192
1193 ZEND_BEGIN_ARG_INFO_EX(ai_HttpParams_offsetGet, 0, 0, 1)
1194 ZEND_ARG_INFO(0, name)
1195 ZEND_END_ARG_INFO();
1196 PHP_METHOD(HttpParams, offsetGet)
1197 {
1198 zend_string *name;
1199 zval zparams_tmp, *zparam, *zparams;
1200
1201 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS(), "S", &name)) {
1202 return;
1203 }
1204
1205 zparams = zend_read_property(php_http_params_class_entry, getThis(), ZEND_STRL("params"), 0, &zparams_tmp);
1206
1207 if (Z_TYPE_P(zparams) == IS_ARRAY && (zparam = zend_symtable_find(Z_ARRVAL_P(zparams), name))) {
1208 RETVAL_ZVAL(zparam, 1, 0);
1209 }
1210 }
1211
1212 ZEND_BEGIN_ARG_INFO_EX(ai_HttpParams_offsetUnset, 0, 0, 1)
1213 ZEND_ARG_INFO(0, name)
1214 ZEND_END_ARG_INFO();
1215 PHP_METHOD(HttpParams, offsetUnset)
1216 {
1217 zend_string *name;
1218 zval zparams_tmp, *zparams;
1219
1220 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS(), "S", &name)) {
1221 return;
1222 }
1223
1224 zparams = zend_read_property(php_http_params_class_entry, getThis(), ZEND_STRL("params"), 0, &zparams_tmp);
1225
1226 if (Z_TYPE_P(zparams) == IS_ARRAY) {
1227 zend_symtable_del(Z_ARRVAL_P(zparams), name);
1228 }
1229 }
1230
1231 ZEND_BEGIN_ARG_INFO_EX(ai_HttpParams_offsetSet, 0, 0, 2)
1232 ZEND_ARG_INFO(0, name)
1233 ZEND_ARG_INFO(0, value)
1234 ZEND_END_ARG_INFO();
1235 PHP_METHOD(HttpParams, offsetSet)
1236 {
1237 zend_string *name;
1238 zval zparams_tmp, *zparam, *zparams, *nvalue;
1239
1240 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS(), "Sz", &name, &nvalue)) {
1241 return;
1242 }
1243
1244 zparams = zend_read_property(php_http_params_class_entry, getThis(), ZEND_STRL("params"), 0, &zparams_tmp);
1245 convert_to_array(zparams);
1246
1247 if (name->len) {
1248 if (Z_TYPE_P(nvalue) == IS_ARRAY) {
1249 if ((zparam = zend_symtable_find(Z_ARRVAL_P(zparams), name))) {
1250 convert_to_array(zparam);
1251 array_join(Z_ARRVAL_P(nvalue), Z_ARRVAL_P(zparam), 0, 0);
1252 } else {
1253 Z_TRY_ADDREF_P(nvalue);
1254 add_assoc_zval_ex(zparams, name->val, name->len, nvalue);
1255 }
1256 } else {
1257 zval tmp;
1258
1259 if ((zparam = zend_symtable_find(Z_ARRVAL_P(zparams), name))) {
1260 ZVAL_DUP(&tmp, zparam);
1261 convert_to_array(&tmp);
1262 } else {
1263 array_init(&tmp);
1264 }
1265
1266 Z_TRY_ADDREF_P(nvalue);
1267 add_assoc_zval_ex(&tmp, ZEND_STRL("value"), nvalue);
1268 add_assoc_zval_ex(zparams, name->val, name->len, &tmp);
1269 }
1270 } else {
1271 zval arr;
1272 zend_string *zs = zval_get_string(nvalue);
1273
1274 array_init(&arr);
1275 add_assoc_bool_ex(&arr, ZEND_STRL("value"), 1);
1276 add_assoc_zval_ex(zparams, zs->val, zs->len, &arr);
1277 zend_string_release(zs);
1278 }
1279 }
1280
1281 static zend_function_entry php_http_params_methods[] = {
1282 PHP_ME(HttpParams, __construct, ai_HttpParams___construct, ZEND_ACC_PUBLIC|ZEND_ACC_CTOR|ZEND_ACC_FINAL)
1283
1284 PHP_ME(HttpParams, toArray, ai_HttpParams_toArray, ZEND_ACC_PUBLIC)
1285 PHP_ME(HttpParams, toString, ai_HttpParams_toString, ZEND_ACC_PUBLIC)
1286 ZEND_MALIAS(HttpParams, __toString, toString, ai_HttpParams_toString, ZEND_ACC_PUBLIC)
1287
1288 PHP_ME(HttpParams, offsetExists, ai_HttpParams_offsetExists, ZEND_ACC_PUBLIC)
1289 PHP_ME(HttpParams, offsetUnset, ai_HttpParams_offsetUnset, ZEND_ACC_PUBLIC)
1290 PHP_ME(HttpParams, offsetSet, ai_HttpParams_offsetSet, ZEND_ACC_PUBLIC)
1291 PHP_ME(HttpParams, offsetGet, ai_HttpParams_offsetGet, ZEND_ACC_PUBLIC)
1292
1293 EMPTY_FUNCTION_ENTRY
1294 };
1295
1296 PHP_MINIT_FUNCTION(http_params)
1297 {
1298 zend_class_entry ce = {0};
1299
1300 INIT_NS_CLASS_ENTRY(ce, "http", "Params", php_http_params_methods);
1301 php_http_params_class_entry = zend_register_internal_class(&ce);
1302 php_http_params_class_entry->create_object = php_http_params_object_new;
1303 zend_class_implements(php_http_params_class_entry, 1, zend_ce_arrayaccess);
1304
1305 zend_declare_class_constant_stringl(php_http_params_class_entry, ZEND_STRL("DEF_PARAM_SEP"), ZEND_STRL(","));
1306 zend_declare_class_constant_stringl(php_http_params_class_entry, ZEND_STRL("DEF_ARG_SEP"), ZEND_STRL(";"));
1307 zend_declare_class_constant_stringl(php_http_params_class_entry, ZEND_STRL("DEF_VAL_SEP"), ZEND_STRL("="));
1308 zend_declare_class_constant_stringl(php_http_params_class_entry, ZEND_STRL("COOKIE_PARAM_SEP"), ZEND_STRL(""));
1309
1310 zend_declare_class_constant_long(php_http_params_class_entry, ZEND_STRL("PARSE_RAW"), PHP_HTTP_PARAMS_RAW);
1311 zend_declare_class_constant_long(php_http_params_class_entry, ZEND_STRL("PARSE_ESCAPED"), PHP_HTTP_PARAMS_ESCAPED);
1312 zend_declare_class_constant_long(php_http_params_class_entry, ZEND_STRL("PARSE_URLENCODED"), PHP_HTTP_PARAMS_URLENCODED);
1313 zend_declare_class_constant_long(php_http_params_class_entry, ZEND_STRL("PARSE_DIMENSION"), PHP_HTTP_PARAMS_DIMENSION);
1314 zend_declare_class_constant_long(php_http_params_class_entry, ZEND_STRL("PARSE_RFC5987"), PHP_HTTP_PARAMS_RFC5987);
1315 zend_declare_class_constant_long(php_http_params_class_entry, ZEND_STRL("PARSE_RFC5988"), PHP_HTTP_PARAMS_RFC5988);
1316 zend_declare_class_constant_long(php_http_params_class_entry, ZEND_STRL("PARSE_DEFAULT"), PHP_HTTP_PARAMS_DEFAULT);
1317 zend_declare_class_constant_long(php_http_params_class_entry, ZEND_STRL("PARSE_QUERY"), PHP_HTTP_PARAMS_QUERY);
1318
1319 zend_declare_property_null(php_http_params_class_entry, ZEND_STRL("params"), ZEND_ACC_PUBLIC);
1320 zend_declare_property_stringl(php_http_params_class_entry, ZEND_STRL("param_sep"), ZEND_STRL(","), ZEND_ACC_PUBLIC);
1321 zend_declare_property_stringl(php_http_params_class_entry, ZEND_STRL("arg_sep"), ZEND_STRL(";"), ZEND_ACC_PUBLIC);
1322 zend_declare_property_stringl(php_http_params_class_entry, ZEND_STRL("val_sep"), ZEND_STRL("="), ZEND_ACC_PUBLIC);
1323 zend_declare_property_long(php_http_params_class_entry, ZEND_STRL("flags"), PHP_HTTP_PARAMS_DEFAULT, ZEND_ACC_PUBLIC);
1324
1325 return SUCCESS;
1326 }
1327
1328 /*
1329 * Local variables:
1330 * tab-width: 4
1331 * c-basic-offset: 4
1332 * End:
1333 * vim600: noet sw=4 ts=4 fdm=marker
1334 * vim<600: noet sw=4 ts=4
1335 */
1336