forked from laruence/yar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
yar_request.c
159 lines (127 loc) · 4.3 KB
/
yar_request.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/*
+----------------------------------------------------------------------+
| Yar - Light, concurrent RPC framework |
+----------------------------------------------------------------------+
| Copyright (c) 2012-2013 The PHP Group |
+----------------------------------------------------------------------+
| This source file is subject to version 3.01 of the PHP license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
| http://www.php.net/license/3_01.txt |
| If you did not receive a copy of the PHP license and are unable to |
| obtain it through the world-wide-web, please send a note to |
| license@php.net so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Author: Xinchen Hui <laruence@php.net> |
| Zhenyu Zhang <zhangzhenyu@php.net> |
+----------------------------------------------------------------------+
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "php.h"
#include "ext/standard/php_lcg.h" /* for php_combined_lcg’ */
#include "ext/standard/php_rand.h" /* for php_mt_rand */
#include "php_yar.h"
#include "yar_exception.h"
#include "yar_response.h"
#include "yar_request.h"
#include "yar_packager.h"
yar_request_t *php_yar_request_instance(zend_string *method, zval *params, zval *options) /* {{{ */ {
yar_request_t *request = ecalloc(1, sizeof(yar_request_t));
if (!BG(mt_rand_is_seeded)) {
php_mt_srand(GENERATE_SEED());
}
request->id = (long)php_mt_rand();
request->method = zend_string_copy(method);
if (params) {
ZVAL_COPY(&request->parameters, params);
}
if (options) {
ZVAL_COPY(&request->options, options);
}
return request;
}
/* }}} */
yar_request_t * php_yar_request_unpack(zval *body) /* {{{ */ {
yar_request_t *req;
zval *pzval;
HashTable *ht;
req = (yar_request_t *)ecalloc(sizeof(yar_request_t), 1);
if (IS_ARRAY != Z_TYPE_P(body)) {
return req;
}
ht = Z_ARRVAL_P(body);
if ((pzval = zend_hash_str_find(ht, "i", sizeof("i") - 1)) != NULL) {
req->id = zval_get_long(pzval);
}
if ((pzval = zend_hash_str_find(ht, "m", sizeof("m") - 1)) != NULL) {
req->method = zval_get_string(pzval);
}
if ((pzval = zend_hash_str_find(ht, "p", sizeof("p") - 1)) != NULL) {
if (IS_ARRAY != Z_TYPE_P(pzval)) {
convert_to_array(pzval);
}
ZVAL_COPY(&req->parameters, pzval);
}
return req;
} /* }}} */
zend_string *php_yar_request_pack(yar_request_t *request, char **msg) /* {{{ */ {
zval zreq;
zend_string *payload;
char *packager_name = NULL;
/* @TODO: this is ugly, which needs options stash in request */
if (IS_ARRAY == Z_TYPE(request->options)) {
zval *pzval;
if ((pzval = zend_hash_index_find(Z_ARRVAL(request->options), YAR_OPT_PACKAGER)) && IS_STRING == Z_TYPE_P(pzval)) {
packager_name = Z_STRVAL_P(pzval);
}
}
array_init(&zreq);
add_assoc_long_ex(&zreq, ZEND_STRL("i"), request->id);
add_assoc_str_ex(&zreq, ZEND_STRL("m"), zend_string_copy(request->method));
if (IS_ARRAY == Z_TYPE(request->parameters)) {
Z_TRY_ADDREF(request->parameters);
add_assoc_zval_ex(&zreq, ZEND_STRL("p"), &request->parameters);
} else {
zval tmp;
array_init(&tmp);
add_assoc_zval_ex(&zreq, ZEND_STRL("p"), &tmp);
}
if (!(payload = php_yar_packager_pack(packager_name, &zreq, msg))) {
zval_ptr_dtor(&zreq);
return NULL;
}
zval_ptr_dtor(&zreq);
return payload;
}
/* }}} */
void php_yar_request_destroy(yar_request_t *request) /* {{{ */ {
if (request->method) {
zend_string_release(request->method);
}
zval_ptr_dtor(&request->parameters);
zval_ptr_dtor(&request->options);
efree(request);
}
/* }}} */
int php_yar_request_valid(yar_request_t *req, yar_response_t *response, char **msg) /* {{{ */ {
response->id = req->id;
if (!req->method) {
spprintf(msg, 0, "%s", "need specifical request method");
return 0;
}
if (Z_ISUNDEF(req->parameters)) {
spprintf(msg, 0, "%s", "need specifical request parameters");
return 0;
}
return 1;
} /* }}} */
/*
* Local variables:
* tab-width: 4
* c-basic-offset: 4
* End:
* vim600: noet sw=4 ts=4 fdm=marker
* vim<600: noet sw=4 ts=4
*/