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