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