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