2 +--------------------------------------------------------------------+
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-2011, Michael Wallner <mike@php.net> |
10 +--------------------------------------------------------------------+
15 #include <ext/standard/php_string.h>
16 #include <Zend/zend_interfaces.h>
18 static php_http_params_token_t def_param_sep
= {",", 1}, *def_param_sep_ptr
[] = {&def_param_sep
, NULL
};
19 static php_http_params_token_t def_arg_sep
= {";", 1}, *def_arg_sep_ptr
[] = {&def_arg_sep
, NULL
};
20 static php_http_params_token_t def_val_sep
= {"=", 1}, *def_val_sep_ptr
[] = {&def_val_sep
, NULL
};
21 static php_http_params_opts_t def_opts
= {
22 .param
= def_param_sep_ptr
,
23 .arg
= def_arg_sep_ptr
,
24 .val
= def_val_sep_ptr
27 PHP_HTTP_API php_http_params_opts_t
*php_http_params_opts_default_get(php_http_params_opts_t
*opts
)
30 opts
= emalloc(sizeof(*opts
));
33 memcpy(opts
, &def_opts
, sizeof(def_opts
));
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
;
48 } php_http_params_state_t
;
50 static void push_param(HashTable
*params
, php_http_params_state_t
*state
, const php_http_params_opts_t
*opts TSRMLS_DC
)
53 if (0 < (state
->val
.len
= state
->input
.str
- state
->val
.str
)) {
54 php_trim(state
->val
.str
, state
->val
.len
, NULL
, 0, *(state
->current
.val
), 3 TSRMLS_CC
);
56 } else if (state
->arg
.str
) {
57 if (0 < (state
->arg
.len
= state
->input
.str
- state
->arg
.str
)) {
61 php_trim(state
->arg
.str
, state
->arg
.len
, NULL
, 0, &key
, 3 TSRMLS_CC
);
64 zend_symtable_update(Z_ARRVAL_PP(state
->current
.args
), Z_STRVAL(key
), Z_STRLEN(key
) + 1, (void *) &val
, sizeof(zval
*), (void *) &state
->current
.val
);
68 } else if (state
->param
.str
) {
69 if (0 < (state
->param
.len
= state
->input
.str
- state
->param
.str
)) {
70 zval
*prm
, *arg
, *val
, key
;
76 zend_hash_update(Z_ARRVAL_P(prm
), "value", sizeof("value"), (void *) &val
, sizeof(zval
*), (void *) &state
->current
.val
);
80 zend_hash_update(Z_ARRVAL_P(prm
), "arguments", sizeof("arguments"), (void *) &arg
, sizeof(zval
*), (void *) &state
->current
.args
);
83 php_trim(state
->param
.str
, state
->param
.len
, NULL
, 0, &key
, 3 TSRMLS_CC
);
84 zend_symtable_update(params
, Z_STRVAL(key
), Z_STRLEN(key
) + 1, (void *) &prm
, sizeof(zval
*), (void *) &state
->current
.param
);
91 static inline zend_bool
check_str(const char *chk_str
, size_t chk_len
, const char *sep_str
, size_t sep_len
) {
92 return 0 < sep_len
&& chk_len
>= sep_len
&& !memcmp(chk_str
, sep_str
, sep_len
);
95 static size_t check_sep(php_http_params_state_t
*state
, php_http_params_token_t
**separators
)
97 php_http_params_token_t
**sep
= separators
;
99 if (sep
) while (*sep
) {
100 if (check_str(state
->input
.str
, state
->input
.len
, (*sep
)->str
, (*sep
)->len
)) {
108 PHP_HTTP_API HashTable
*php_http_params_parse(HashTable
*params
, const php_http_params_opts_t
*opts TSRMLS_DC
)
110 php_http_params_state_t state
= {
111 .input
.str
= opts
->input
.str
,
112 .input
.len
= opts
->input
.len
,
122 ALLOC_HASHTABLE(params
);
123 ZEND_INIT_SYMTABLE(params
);
126 while (state
.input
.len
) {
127 if (!state
.param
.str
) {
129 state
.param
.str
= state
.input
.str
;
132 /* are we at a param separator? */
133 if (0 < (sep_len
= check_sep(&state
, opts
->param
))) {
134 push_param(params
, &state
, opts TSRMLS_CC
);
136 /* start off with a new param */
137 state
.param
.str
= state
.input
.str
+ sep_len
;
139 state
.arg
.str
= NULL
;
141 state
.val
.str
= NULL
;
144 /* are we at an arg separator? */
145 if (0 < (sep_len
= check_sep(&state
, opts
->arg
))) {
146 push_param(params
, &state
, opts TSRMLS_CC
);
148 /* continue with a new arg */
149 state
.arg
.str
= state
.input
.str
+ sep_len
;
151 state
.val
.str
= NULL
;
154 /* are we at a val separator? */
155 if (0 < (sep_len
= check_sep(&state
, opts
->val
))) {
156 /* only handle separator if we're not already reading in a val */
157 if (!state
.val
.str
) {
158 push_param(params
, &state
, opts TSRMLS_CC
);
160 state
.val
.str
= state
.input
.str
+ sep_len
;
170 push_param(params
, &state
, opts TSRMLS_CC
);
175 #define PHP_HTTP_BEGIN_ARGS(method, req_args) PHP_HTTP_BEGIN_ARGS_EX(HttpParams, method, 0, req_args)
176 #define PHP_HTTP_EMPTY_ARGS(method) PHP_HTTP_EMPTY_ARGS_EX(HttpParams, method, 0)
177 #define PHP_HTTP_PARAMS_ME(method, visibility) PHP_ME(HttpParams, method, PHP_HTTP_ARGS(HttpParams, method), visibility)
178 #define PHP_HTTP_PARAMS_GME(method, visibility) PHP_ME(HttpParams, method, PHP_HTTP_ARGS(HttpParams, __getter), visibility)
180 PHP_HTTP_BEGIN_ARGS(__construct
, 0)
181 PHP_HTTP_ARG_VAL(params
, 0)
182 PHP_HTTP_ARG_VAL(flags
, 0)
185 PHP_HTTP_EMPTY_ARGS(toArray
);
186 PHP_HTTP_EMPTY_ARGS(toString
);
188 PHP_HTTP_BEGIN_ARGS(offsetExists
, 1)
189 PHP_HTTP_ARG_VAL(name
, 0)
192 PHP_HTTP_BEGIN_ARGS(offsetUnset
, 1)
193 PHP_HTTP_ARG_VAL(name
, 0)
196 PHP_HTTP_BEGIN_ARGS(offsetGet
, 1)
197 PHP_HTTP_ARG_VAL(name
, 0)
200 PHP_HTTP_BEGIN_ARGS(offsetSet
, 2)
201 PHP_HTTP_ARG_VAL(name
, 0)
202 PHP_HTTP_ARG_VAL(value
, 0)
205 zend_class_entry
*php_http_params_class_entry
;
206 zend_function_entry php_http_params_method_entry
[] = {
207 PHP_HTTP_PARAMS_ME(__construct
, ZEND_ACC_PUBLIC
|ZEND_ACC_CTOR
|ZEND_ACC_FINAL
)
209 PHP_HTTP_PARAMS_ME(toArray
, ZEND_ACC_PUBLIC
)
210 PHP_HTTP_PARAMS_ME(toString
, ZEND_ACC_PUBLIC
)
211 ZEND_MALIAS(HttpParams
, __toString
, toString
, PHP_HTTP_ARGS(HttpParams
, toString
), ZEND_ACC_PUBLIC
)
213 PHP_HTTP_PARAMS_ME(offsetExists
, ZEND_ACC_PUBLIC
)
214 PHP_HTTP_PARAMS_ME(offsetUnset
, ZEND_ACC_PUBLIC
)
215 PHP_HTTP_PARAMS_ME(offsetSet
, ZEND_ACC_PUBLIC
)
216 PHP_HTTP_PARAMS_ME(offsetGet
, ZEND_ACC_PUBLIC
)
221 PHP_MINIT_FUNCTION(http_params
)
223 PHP_HTTP_REGISTER_CLASS(http
, Params
, http_params
, php_http_object_class_entry
, 0);
225 zend_class_implements(php_http_params_class_entry TSRMLS_CC
, 1, zend_ce_arrayaccess
);
227 zend_declare_class_constant_stringl(php_http_params_class_entry
, ZEND_STRL("DEF_PARAM_SEP"), ZEND_STRL(",") TSRMLS_CC
);
228 zend_declare_class_constant_stringl(php_http_params_class_entry
, ZEND_STRL("DEF_ARG_SEP"), ZEND_STRL(";") TSRMLS_CC
);
229 zend_declare_class_constant_stringl(php_http_params_class_entry
, ZEND_STRL("DEF_VAL_SEP"), ZEND_STRL("=") TSRMLS_CC
);
230 zend_declare_class_constant_stringl(php_http_params_class_entry
, ZEND_STRL("COOKIE_PARAM_SEP"), ZEND_STRL("") TSRMLS_CC
);
232 zend_declare_property_null(php_http_params_class_entry
, ZEND_STRL("params"), ZEND_ACC_PUBLIC TSRMLS_CC
);
233 zend_declare_property_stringl(php_http_params_class_entry
, ZEND_STRL("param_sep"), ZEND_STRL(","), ZEND_ACC_PUBLIC TSRMLS_CC
);
234 zend_declare_property_stringl(php_http_params_class_entry
, ZEND_STRL("arg_sep"), ZEND_STRL(";"), ZEND_ACC_PUBLIC TSRMLS_CC
);
235 zend_declare_property_stringl(php_http_params_class_entry
, ZEND_STRL("val_sep"), ZEND_STRL("="), ZEND_ACC_PUBLIC TSRMLS_CC
);
240 static php_http_params_token_t
**parse_sep(zval
*zv TSRMLS_DC
)
244 php_http_params_token_t
**ret
, **tmp
;
250 zv
= php_http_ztyp(IS_ARRAY
, zv
);
251 ret
= ecalloc(zend_hash_num_elements(Z_ARRVAL_P(zv
)) + 1, sizeof(*ret
));
254 FOREACH_VAL(pos
, zv
, sep
) {
255 zval
*zt
= php_http_ztyp(IS_STRING
, *sep
);
257 if (Z_STRLEN_P(zt
)) {
258 *tmp
= emalloc(sizeof(**tmp
));
259 (*tmp
)->str
= estrndup(Z_STRVAL_P(zt
), (*tmp
)->len
= Z_STRLEN_P(zt
));
270 static void free_sep(php_http_params_token_t
**separator
) {
271 php_http_params_token_t
**sep
= separator
;
274 STR_FREE((*sep
)->str
);
282 PHP_METHOD(HttpParams
, __construct
)
284 with_error_handling(EH_THROW
, php_http_exception_class_entry
) {
285 zval
*zcopy
, *zparams
= NULL
, *param_sep
= NULL
, *arg_sep
= NULL
, *val_sep
= NULL
;
287 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|z!/z/z/z/", &zparams
, ¶m_sep
, &arg_sep
, &val_sep
)) {
288 switch (ZEND_NUM_ARGS()) {
290 zend_update_property(php_http_params_class_entry
, getThis(), ZEND_STRL("param_sep"), param_sep TSRMLS_CC
);
292 zend_update_property(php_http_params_class_entry
, getThis(), ZEND_STRL("arg_sep"), arg_sep TSRMLS_CC
);
294 zend_update_property(php_http_params_class_entry
, getThis(), ZEND_STRL("val_sep"), val_sep TSRMLS_CC
);
298 switch (Z_TYPE_P(zparams
)) {
301 zcopy
= php_http_zsep(1, IS_ARRAY
, zparams
);
302 zend_update_property(php_http_params_class_entry
, getThis(), ZEND_STRL("params"), zcopy TSRMLS_CC
);
303 zval_ptr_dtor(&zcopy
);
306 zcopy
= php_http_ztyp(IS_STRING
, zparams
);
307 if (Z_STRLEN_P(zcopy
)) {
308 php_http_params_opts_t opts
= {
309 .input
.str
= Z_STRVAL_P(zcopy
),
310 .input
.len
= Z_STRLEN_P(zcopy
),
311 .param
= parse_sep(zend_read_property(php_http_params_class_entry
, getThis(), ZEND_STRL("param_sep"), 0 TSRMLS_CC
) TSRMLS_CC
),
312 .arg
= parse_sep(zend_read_property(php_http_params_class_entry
, getThis(), ZEND_STRL("arg_sep"), 0 TSRMLS_CC
) TSRMLS_CC
),
313 .val
= parse_sep(zend_read_property(php_http_params_class_entry
, getThis(), ZEND_STRL("val_sep"), 0 TSRMLS_CC
) TSRMLS_CC
)
316 MAKE_STD_ZVAL(zparams
);
318 php_http_params_parse(Z_ARRVAL_P(zparams
), &opts TSRMLS_CC
);
319 zend_update_property(php_http_params_class_entry
, getThis(), ZEND_STRL("params"), zparams TSRMLS_CC
);
320 zval_ptr_dtor(&zparams
);
322 free_sep(opts
.param
);
326 zval_ptr_dtor(&zcopy
);
330 MAKE_STD_ZVAL(zparams
);
332 zend_update_property(php_http_params_class_entry
, getThis(), ZEND_STRL("params"), zparams TSRMLS_CC
);
333 zval_ptr_dtor(&zparams
);
336 } end_error_handling();
339 PHP_METHOD(HttpParams
, toArray
)
341 if (SUCCESS
!= zend_parse_parameters_none()) {
344 RETURN_PROP(php_http_params_class_entry
, "params");
347 PHP_METHOD(HttpParams
, toString
)
349 zval
*zparams
, *zpsep
, *zasep
, *zvsep
;
350 zval
**zparam
, **zvalue
, **zargs
, **zarg
;
351 HashPosition pos1
, pos2
;
352 php_http_array_hashkey_t key1
= php_http_array_hashkey_init(0), key2
= php_http_array_hashkey_init(0);
353 php_http_buffer_t buf
;
355 zparams
= php_http_ztyp(IS_ARRAY
, zend_read_property(php_http_params_class_entry
, getThis(), ZEND_STRL("params"), 0 TSRMLS_CC
));
356 zpsep
= php_http_ztyp(IS_STRING
, zend_read_property(php_http_params_class_entry
, getThis(), ZEND_STRL("param_sep"), 0 TSRMLS_CC
));
357 zasep
= php_http_ztyp(IS_STRING
, zend_read_property(php_http_params_class_entry
, getThis(), ZEND_STRL("arg_sep"), 0 TSRMLS_CC
));
358 zvsep
= php_http_ztyp(IS_STRING
, zend_read_property(php_http_params_class_entry
, getThis(), ZEND_STRL("val_sep"), 0 TSRMLS_CC
));
359 php_http_buffer_init(&buf
);
361 FOREACH_KEYVAL(pos1
, zparams
, key1
, zparam
) {
363 if (PHP_HTTP_BUFFER_LEN(&buf
)) {
364 php_http_buffer_append(&buf
, Z_STRVAL_P(zpsep
), Z_STRLEN_P(zpsep
));
368 if (key1
.type
== HASH_KEY_IS_STRING
) {
369 php_http_buffer_append(&buf
, key1
.str
, key1
.len
- 1);
371 php_http_buffer_appendf(&buf
, "%lu", key1
.num
);
374 if (Z_TYPE_PP(zparam
) == IS_ARRAY
) {
376 if (SUCCESS
== zend_hash_find(Z_ARRVAL_PP(zparam
), ZEND_STRS("value"), (void *) &zvalue
)) {
377 if (Z_TYPE_PP(zvalue
) != IS_BOOL
) {
378 zval
*tmp
= php_http_ztyp(IS_STRING
, *zvalue
);
380 php_http_buffer_append(&buf
, Z_STRVAL_P(zvsep
), Z_STRLEN_P(zvsep
));
381 php_http_buffer_append(&buf
, Z_STRVAL_P(tmp
), Z_STRLEN_P(tmp
));
383 } else if (!Z_BVAL_PP(zvalue
)) {
384 php_http_buffer_append(&buf
, Z_STRVAL_P(zvsep
), Z_STRLEN_P(zvsep
));
385 php_http_buffer_appends(&buf
, "0");
389 if (SUCCESS
== zend_hash_find(Z_ARRVAL_PP(zparam
), ZEND_STRS("arguments"), (void *) &zargs
)) {
390 if (Z_TYPE_PP(zargs
) == IS_ARRAY
) {
391 FOREACH_KEYVAL(pos2
, *zargs
, key2
, zarg
) {
393 if (PHP_HTTP_BUFFER_LEN(&buf
)) {
394 php_http_buffer_append(&buf
, Z_STRVAL_P(zasep
), Z_STRLEN_P(zasep
));
398 if (key2
.type
== HASH_KEY_IS_STRING
) {
399 php_http_buffer_append(&buf
, key2
.str
, key2
.len
- 1);
401 php_http_buffer_appendf(&buf
, "%lu", key2
.num
);
404 if (Z_TYPE_PP(zarg
) != IS_BOOL
) {
405 zval
*tmp
= php_http_ztyp(IS_STRING
, *zarg
);
407 php_http_buffer_append(&buf
, Z_STRVAL_P(zvsep
), Z_STRLEN_P(zvsep
));
408 php_http_buffer_append(&buf
, Z_STRVAL_P(tmp
), Z_STRLEN_P(tmp
));
410 } else if (!Z_BVAL_PP(zarg
)) {
411 php_http_buffer_append(&buf
, Z_STRVAL_P(zvsep
), Z_STRLEN_P(zvsep
));
412 php_http_buffer_appends(&buf
, "0");
420 zval_ptr_dtor(&zparams
);
421 zval_ptr_dtor(&zpsep
);
422 zval_ptr_dtor(&zasep
);
423 zval_ptr_dtor(&zvsep
);
425 php_http_buffer_shrink(&buf
);
426 RETVAL_PHP_HTTP_BUFFER_VAL(&buf
);
429 PHP_METHOD(HttpParams
, offsetExists
)
434 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &name_str
, &name_len
)) {
435 zval
**zparam
, *zparams
= php_http_ztyp(IS_ARRAY
, zend_read_property(php_http_params_class_entry
, getThis(), ZEND_STRL("params"), 0 TSRMLS_CC
));
437 if (SUCCESS
== zend_symtable_find(Z_ARRVAL_P(zparams
), name_str
, name_len
+ 1, (void *) &zparam
)) {
438 RETVAL_BOOL(Z_TYPE_PP(zparam
) != IS_NULL
);
442 zval_ptr_dtor(&zparams
);
446 PHP_METHOD(HttpParams
, offsetGet
)
451 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &name_str
, &name_len
)) {
452 zval
**zparam
, *zparams
= php_http_ztyp(IS_ARRAY
, zend_read_property(php_http_params_class_entry
, getThis(), ZEND_STRL("params"), 0 TSRMLS_CC
));
454 if (SUCCESS
== zend_symtable_find(Z_ARRVAL_P(zparams
), name_str
, name_len
+ 1, (void *) &zparam
)) {
455 RETVAL_ZVAL(*zparam
, 1, 0);
458 zval_ptr_dtor(&zparams
);
463 PHP_METHOD(HttpParams
, offsetUnset
)
468 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &name_str
, &name_len
)) {
469 zval
*zparams
= php_http_zsep(1, IS_ARRAY
, zend_read_property(php_http_params_class_entry
, getThis(), ZEND_STRL("params"), 0 TSRMLS_CC
));
471 zend_symtable_del(Z_ARRVAL_P(zparams
), name_str
, name_len
+ 1);
472 zend_update_property(php_http_params_class_entry
, getThis(), ZEND_STRL("params"), zparams TSRMLS_CC
);
474 zval_ptr_dtor(&zparams
);
478 PHP_METHOD(HttpParams
, offsetSet
)
484 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "sz", &name_str
, &name_len
, &nvalue
)) {
485 zval
**zparam
, *zparams
= php_http_zsep(1, IS_ARRAY
, zend_read_property(php_http_params_class_entry
, getThis(), ZEND_STRL("params"), 0 TSRMLS_CC
));
488 if (Z_TYPE_P(nvalue
) == IS_ARRAY
) {
491 if (SUCCESS
== zend_symtable_find(Z_ARRVAL_P(zparams
), name_str
, name_len
+ 1, (void *) &zparam
)) {
492 new_zparam
= php_http_zsep(1, IS_ARRAY
, *zparam
);
493 array_join(Z_ARRVAL_P(nvalue
), Z_ARRVAL_P(new_zparam
), 0, 0);
496 Z_ADDREF_P(new_zparam
);
498 add_assoc_zval_ex(zparams
, name_str
, name_len
+ 1, new_zparam
);
502 if (SUCCESS
== zend_symtable_find(Z_ARRVAL_P(zparams
), name_str
, name_len
+ 1, (void *) &zparam
)) {
503 tmp
= php_http_zsep(1, IS_ARRAY
, *zparam
);
510 add_assoc_zval_ex(tmp
, ZEND_STRS("value"), nvalue
);
511 add_assoc_zval_ex(zparams
, name_str
, name_len
+ 1, tmp
);
514 zval
*tmp
= php_http_ztyp(IS_STRING
, nvalue
), *arr
;
518 add_assoc_bool_ex(arr
, ZEND_STRS("value"), 1);
519 add_assoc_zval_ex(zparams
, Z_STRVAL_P(tmp
), Z_STRLEN_P(tmp
) + 1, arr
);
523 zend_update_property(php_http_params_class_entry
, getThis(), ZEND_STRL("params"), zparams TSRMLS_CC
);
524 zval_ptr_dtor(&zparams
);
533 * vim600: noet sw=4 ts=4 fdm=marker
534 * vim<600: noet sw=4 ts=4