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