Skip to content

Commit fc5176b

Browse files
committed
Added hprose session.serialize_handler.
1 parent 00d6b4c commit fc5176b

File tree

9 files changed

+195
-41
lines changed

9 files changed

+195
-41
lines changed

hprose_class_manager.c

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,9 @@ char * _hprose_class_manager_get_alias(char *name, int32_t len, int32_t* len_ptr
5454
alias = estrndup(name, len);
5555
*len_ptr = len;
5656
str_replace('\\', '_', alias, len);
57-
hprose_class_manager_register(name, len, alias, len);
57+
if (HPROSE_G(active)) {
58+
hprose_class_manager_register(name, len, alias, len);
59+
}
5860
}
5961
else {
6062
alias = estrndup(HB_BUF_P(_alias), HB_LEN_P(_alias));
@@ -69,7 +71,9 @@ zend_string *_hprose_class_manager_get_alias(char *name, int32_t len TSRMLS_DC)
6971
if (!HPROSE_G(cache1) || (_alias = zend_hash_str_find_ptr(HPROSE_G(cache1), name, len)) == NULL) {
7072
alias = zend_string_init(name, len, 0);
7173
str_replace('\\', '_', alias->val, len);
72-
hprose_class_manager_register(name, len, alias->val, len);
74+
if (HPROSE_G(active)) {
75+
hprose_class_manager_register(name, len, alias->val, len);
76+
}
7377
}
7478
else {
7579
alias = zend_string_copy(HB_STR_P(_alias));
@@ -88,7 +92,9 @@ char * _hprose_class_manager_get_class(char *alias, int32_t len, int32_t* len_pt
8892
if (!class_exists(alias, len, 0) && !class_exists(alias, len, 1)) {
8993
str_replace('_', '\\', name, len);
9094
if (class_exists(name, len, 0) || class_exists(name, len, 1)) {
91-
hprose_class_manager_register(name, len, alias, len);
95+
if (HPROSE_G(active)) {
96+
hprose_class_manager_register(name, len, alias, len);
97+
}
9298
}
9399
else {
94100
efree(name);
@@ -112,7 +118,9 @@ zend_string *_hprose_class_manager_get_class(char *alias, int32_t len TSRMLS_DC)
112118
if (!class_exists(alias, len, 0) && !class_exists(alias, len, 1)) {
113119
str_replace('_', '\\', name->val, len);
114120
if (_class_exists(name, 0) || _class_exists(name, 1)) {
115-
hprose_class_manager_register(name->val, len, alias, len);
121+
if (HPROSE_G(active)) {
122+
hprose_class_manager_register(name->val, len, alias, len);
123+
}
116124
}
117125
else {
118126
zend_string_release(name);
@@ -208,10 +216,12 @@ HPROSE_STARTUP_FUNCTION(class_manager) {
208216
HPROSE_ACTIVATE_FUNCTION(class_manager) {
209217
HPROSE_G(cache1) = NULL;
210218
HPROSE_G(cache2) = NULL;
219+
HPROSE_G(active) = 1;
211220
return SUCCESS;
212221
}
213222

214223
HPROSE_DEACTIVATE_FUNCTION(class_manager) {
224+
HPROSE_G(active) = 0;
215225
if (HPROSE_G(cache1)) {
216226
zend_hash_destroy(HPROSE_G(cache1));
217227
FREE_HASHTABLE(HPROSE_G(cache1));

hprose_common.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ ZEND_TSRMLS_CACHE_EXTERN();
5656
ZEND_BEGIN_MODULE_GLOBALS(hprose)
5757
HashTable *cache1;
5858
HashTable *cache2;
59+
zend_bool active;
5960
ZEND_END_MODULE_GLOBALS(hprose)
6061

6162
#ifdef ZTS

hprose_formatter.c

Lines changed: 112 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
* *
1414
* hprose formatter for pecl source file. *
1515
* *
16-
* LastModified: May 10, 2015 *
16+
* LastModified: Sep 1, 2015 *
1717
* Author: Ma Bingyao <andot@hprose.com> *
1818
* *
1919
\**********************************************************/
@@ -64,69 +64,62 @@ static void hprose_serialize(hprose_bytes_io *stream, zval *val, zend_bool simpl
6464
hprose_writer_destroy(&writer);
6565
}
6666

67-
static zend_always_inline void hprose_fast_serialize(zval *val, zend_bool simple, zval *return_value TSRMLS_DC) {
68-
hprose_bytes_io stream;
69-
hprose_bytes_io_init(&stream, NULL, 0);
67+
static zend_always_inline void hprose_fast_serialize(hprose_bytes_io *stream, zval *val, zend_bool simple TSRMLS_DC) {
7068
if (!val) {
71-
_hprose_writer_write_null(&stream); return;
69+
_hprose_writer_write_null(stream); return;
7270
}
7371
switch (Z_TYPE_P(val)) {
7472
case IS_NULL:
75-
_hprose_writer_write_null(&stream); break;
73+
_hprose_writer_write_null(stream); break;
7674
case IS_LONG:
77-
_hprose_writer_write_long(&stream, Z_LVAL_P(val)); break;
75+
_hprose_writer_write_long(stream, Z_LVAL_P(val)); break;
7876
case IS_DOUBLE:
79-
_hprose_writer_write_double(&stream, Z_DVAL_P(val)); break;
77+
_hprose_writer_write_double(stream, Z_DVAL_P(val)); break;
8078
#if PHP_MAJOR_VERSION < 7
8179
case IS_BOOL:
82-
_hprose_writer_write_bool(&stream, Z_BVAL_P(val)); break;
80+
_hprose_writer_write_bool(stream, Z_BVAL_P(val)); break;
8381
#else /* PHP_MAJOR_VERSION < 7 */
8482
case IS_UNDEF:
85-
_hprose_writer_write_null(&stream); break;
83+
_hprose_writer_write_null(stream); break;
8684
case IS_TRUE:
87-
_hprose_writer_write_true(&stream); break;
85+
_hprose_writer_write_true(stream); break;
8886
case IS_FALSE:
89-
_hprose_writer_write_false(&stream); break;
87+
_hprose_writer_write_false(stream); break;
9088
#endif
9189
case IS_STRING: {
9290
char * s = Z_STRVAL_P(val);
9391
int32_t l = Z_STRLEN_P(val);
9492
if (l == 0) {
95-
_hprose_writer_write_empty(&stream);
93+
_hprose_writer_write_empty(stream);
9694
}
9795
else if (is_utf8(s, l)) {
9896
if (l < 4 && ustrlen(s, l) == 1) {
99-
_hprose_writer_write_utf8char(&stream, val);
97+
_hprose_writer_write_utf8char(stream, val);
10098
}
10199
else {
102-
_hprose_writer_write_string_with_ref(NULL, &stream, val);
100+
_hprose_writer_write_string_with_ref(NULL, stream, val);
103101
}
104102
}
105103
else {
106-
_hprose_writer_write_bytes_with_ref(NULL, &stream, val);
104+
_hprose_writer_write_bytes_with_ref(NULL, stream, val);
107105
}
108106
break;
109107
}
110108
case IS_OBJECT: {
111109
zend_class_entry *ce = Z_OBJCE_P(val);
112110
if (instanceof_function(ce, get_hprose_bytes_io_ce() TSRMLS_CC)) {
113-
_hprose_writer_write_bytes_io_with_ref(NULL, &stream, val TSRMLS_CC);
111+
_hprose_writer_write_bytes_io_with_ref(NULL, stream, val TSRMLS_CC);
114112
}
115113
else if (instanceof_function(ce, php_date_get_date_ce() TSRMLS_CC)) {
116-
_hprose_writer_write_datetime_with_ref(NULL, &stream, val TSRMLS_CC);
114+
_hprose_writer_write_datetime_with_ref(NULL, stream, val TSRMLS_CC);
117115
}
118116
else {
119-
hprose_serialize(&stream, val, simple TSRMLS_CC); break;
117+
hprose_serialize(stream, val, simple TSRMLS_CC); break;
120118
}
121119
}
122120
default:
123-
hprose_serialize(&stream, val, simple TSRMLS_CC); break;
121+
hprose_serialize(stream, val, simple TSRMLS_CC); break;
124122
}
125-
#if PHP_MAJOR_VERSION < 7
126-
RETVAL_STRINGL_0(HB_BUF(stream), HB_LEN(stream));
127-
#else
128-
RETVAL_STR(HB_STR(stream));
129-
#endif
130123
}
131124

132125
static void hprose_reader_fast_unserialize(hprose_reader *_this, char tag, zval *return_value TSRMLS_DC) {
@@ -235,15 +228,108 @@ static zend_always_inline void hprose_fast_unserialize(hprose_bytes_io *stream,
235228
}
236229
}
237230

231+
#if HAVE_PHP_SESSION
232+
PS_SERIALIZER_ENCODE_FUNC(hprose) {
233+
hprose_bytes_io stream;
234+
hprose_bytes_io_init(&stream, NULL, 0);
235+
#if PHP_MAJOR_VERSION < 7
236+
hprose_fast_serialize(&stream, PS(http_session_vars), 0 TSRMLS_CC);
237+
if (newlen) {
238+
*newlen = HB_LEN(stream);
239+
}
240+
*newstr = HB_BUF(stream);
241+
return SUCCESS;
242+
#else
243+
hprose_fast_serialize(&stream, &PS(http_session_vars), 0 TSRMLS_CC);
244+
return stream.s;
245+
#endif
246+
}
247+
248+
PS_SERIALIZER_DECODE_FUNC(hprose) {
249+
#if PHP_MAJOR_VERSION < 7
250+
int ret;
251+
HashPosition tmp_hash_pos;
252+
char *key_str;
253+
ulong key_long;
254+
uint key_len;
255+
zval *tmp;
256+
zval **value;
257+
#else
258+
zend_string *str, *key_str;
259+
zval tmp, *value;
260+
#endif
261+
HashTable *tmp_hash;
262+
hprose_bytes_io stream;
263+
if (vallen > 0) {
264+
#if PHP_MAJOR_VERSION < 7
265+
hprose_bytes_io_init_readonly(&stream, val, vallen);
266+
MAKE_STD_ZVAL(tmp);
267+
hprose_fast_unserialize(&stream, 0, tmp TSRMLS_CC);
268+
tmp_hash = HASH_OF(tmp);
269+
if (tmp_hash) {
270+
zend_hash_internal_pointer_reset_ex(tmp_hash, &tmp_hash_pos);
271+
while (zend_hash_get_current_data_ex(
272+
tmp_hash, (void *)&value, &tmp_hash_pos) == SUCCESS) {
273+
ret = zend_hash_get_current_key_ex(
274+
tmp_hash, &key_str, &key_len, &key_long, 0, &tmp_hash_pos);
275+
switch (ret) {
276+
case HASH_KEY_IS_LONG:
277+
/* ??? */
278+
break;
279+
case HASH_KEY_IS_STRING:
280+
php_set_session_var(
281+
key_str, key_len - 1, *value, NULL TSRMLS_CC);
282+
php_add_session_var(key_str, key_len - 1 TSRMLS_CC);
283+
break;
284+
}
285+
zend_hash_move_forward_ex(tmp_hash, &tmp_hash_pos);
286+
}
287+
}
288+
#else
289+
str = zend_string_init(val, vallen, 0);
290+
hprose_bytes_io_init_readonly(&stream, str);
291+
hprose_fast_unserialize(&stream, 0, &tmp TSRMLS_CC);
292+
tmp_hash = HASH_OF(&tmp);
293+
if (tmp_hash) {
294+
ZEND_HASH_FOREACH_STR_KEY_VAL(tmp_hash, key_str, value) {
295+
if (key_str) {
296+
php_set_session_var(key_str, value, NULL);
297+
php_add_session_var(key_str);
298+
ZVAL_UNDEF(value);
299+
}
300+
} ZEND_HASH_FOREACH_END();
301+
}
302+
zend_string_release(str);
303+
#endif
304+
zval_ptr_dtor(&tmp);
305+
}
306+
if (EG(exception)) {
307+
#if PHP_MAJOR_VERSION < 7
308+
zend_clear_exception(TSRMLS_C);
309+
#else
310+
zend_clear_exception();
311+
#endif
312+
}
313+
return SUCCESS;
314+
}
315+
#endif
316+
238317
/* {{{ proto string hprose_serialize(mixed $val, bool $simple = false)
239318
serialize php value to hprose format data */
240319
ZEND_FUNCTION(hprose_serialize) {
241320
zval *val;
242321
zend_bool simple = 0;
322+
hprose_bytes_io stream;
243323
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z!|b", &val, &simple) == FAILURE) {
244324
return;
245325
}
246-
hprose_fast_serialize(val, simple, return_value TSRMLS_CC);
326+
hprose_bytes_io_init(&stream, NULL, 0);
327+
hprose_fast_serialize(&stream, val, simple TSRMLS_CC);
328+
#if PHP_MAJOR_VERSION < 7
329+
RETVAL_STRINGL_0(HB_BUF(stream), HB_LEN(stream));
330+
#else
331+
RETVAL_STR(HB_STR(stream));
332+
#endif
247333
}
248334
/* }}} */
249335

hprose_formatter.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
* *
1414
* hprose formatter for pecl header file. *
1515
* *
16-
* LastModified: May 8, 2015 *
16+
* LastModified: Sep 1, 2015 *
1717
* Author: Ma Bingyao <andot@hprose.com> *
1818
* *
1919
\**********************************************************/
@@ -25,13 +25,20 @@
2525
#include "hprose_bytes_io.h"
2626
#include "hprose_writer.h"
2727
#include "hprose_reader.h"
28+
#if HAVE_PHP_SESSION
29+
#include "ext/session/php_session.h" /* for php_session_register_serializer */
30+
#endif
2831

2932
BEGIN_EXTERN_C()
3033

3134
zend_class_entry *get_hprose_formatter_ce();
3235

3336
HPROSE_STARTUP_FUNCTION(formatter);
3437

38+
#if HAVE_PHP_SESSION
39+
PS_SERIALIZER_FUNCS(hprose);
40+
#endif
41+
3542
ZEND_FUNCTION(hprose_serialize);
3643
ZEND_FUNCTION(hprose_unserialize);
3744

hprose_writer.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -419,7 +419,7 @@ static void _hprose_writer_write_hashtable(hprose_writer *_this, hprose_writer_r
419419
if (zend_hash_get_current_key_ex(ht, &str, &len, &index, 0, position) == HASH_KEY_IS_STRING) {
420420
zval key;
421421
ZVAL_STRINGL(&key, str, len - 1, 0);
422-
_hprose_writer_write_string_with_ref(refer, stream, &key);
422+
_hprose_writer_serialize(_this, refer, stream, &key TSRMLS_CC);
423423
}
424424
else {
425425
_hprose_writer_write_ulong(stream, (uint64_t)index);
@@ -680,11 +680,12 @@ static void _hprose_writer_write_object(hprose_writer *_this, hprose_writer_refe
680680
zval **e, *value;
681681
zend_hash_get_current_data(props_ht, (void **)&e);
682682
value = zend_hash_str_find_ptr(ht, Z_STRVAL_PP(e), Z_STRLEN_PP(e));
683+
_hprose_writer_serialize(_this, refer, stream, value TSRMLS_CC);
683684
#else
684685
zval *e = zend_hash_get_current_data(props_ht);
685686
zval *value = zend_hash_str_find_ptr(ht, Z_STRVAL_P(e), Z_STRLEN_P(e));
686-
#endif
687687
_hprose_writer_serialize(_this, refer, stream, value TSRMLS_CC);
688+
#endif
688689
zend_hash_move_forward(props_ht);
689690
}
690691
}

package.xml

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,18 +46,18 @@ http://pear.php.net/dtd/package-2.0.xsd">
4646
<email>mabingyao@gmail.com</email>
4747
<active>yes</active>
4848
</lead>
49-
<date>2015-08-23</date>
50-
<time>12:29:18</time>
49+
<date>2015-09-01</date>
50+
<time>23:59:44</time>
5151
<version>
52-
<release>1.6.0</release>
53-
<api>1.6.0</api>
52+
<release>1.6.1</release>
53+
<api>1.6.1</api>
5454
</version>
5555
<stability>
5656
<release>stable</release>
5757
<api>stable</api>
5858
</stability>
5959
<license uri="http://mit-license.org/">MIT</license>
60-
<notes>Removed future, client and server implementations.
60+
<notes>Added hprose session.serialize_handler.
6161
</notes>
6262
<contents>
6363
<dir name="/">
@@ -93,6 +93,7 @@ http://pear.php.net/dtd/package-2.0.xsd">
9393
<file role="test" name="raw_reader.phpt" />
9494
<file role="test" name="reader.phpt" />
9595
<file role="test" name="formatter.phpt" />
96+
<file role="test" name="session.phpt" />
9697
</dir>
9798
</dir>
9899
</contents>
@@ -111,6 +112,20 @@ http://pear.php.net/dtd/package-2.0.xsd">
111112
<configureoption default="yes" name="enable-hprose" prompt="whether to enable hprose support" />
112113
</extsrcrelease>
113114
<changelog>
115+
<release>
116+
<version>
117+
<release>1.6.1</release>
118+
<api>1.6.1</api>
119+
</version>
120+
<stability>
121+
<release>stable</release>
122+
<api>stable</api>
123+
</stability>
124+
<date>2015-09-01</date>
125+
<license uri="http://mit-license.org/">MIT</license>
126+
<notes>Added hprose session.serialize_handler.
127+
</notes>
128+
</release>
114129
<release>
115130
<version>
116131
<release>1.6.0</release>

0 commit comments

Comments
 (0)