-
-
Notifications
You must be signed in to change notification settings - Fork 319
/
yar_response.c
148 lines (123 loc) · 4.52 KB
/
yar_response.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
/*
+----------------------------------------------------------------------+
| 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 |
| [email protected] so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Author: Xinchen Hui <[email protected]> |
| Zhenyu Zhang <[email protected]> |
+----------------------------------------------------------------------+
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "php.h"
#include "php_yar.h"
#include "yar_exception.h"
#include "yar_request.h"
#include "yar_response.h"
yar_response_t * php_yar_response_instance() /* {{{ */ {
yar_response_t *response = ecalloc(1, sizeof(yar_response_t));
return response;
} /* }}} */
int php_yar_response_bind_request(yar_response_t *response, yar_request_t *request) /* {{{ */ {
response->id = request->id;
return 1;
} /* }}} */
void php_yar_response_alter_body(yar_response_t *response, zend_string *body, int method) /* {{{ */ {
response->out = body;
} /* }}} */
void php_yar_response_set_error(yar_response_t *response, int type, char *message, unsigned len) /* {{{ */ {
ZVAL_STRINGL(&response->err, message, len);
response->status = type;
} /* }}} */
void php_yar_response_set_exception(yar_response_t *response, zend_object *ex) /* {{{ */ {
zval rv;
zval *msg, *code, *file, *line;
zend_class_entry *ce;
#if PHP_VERSION_ID < 80000
zval zv;
ZVAL_OBJ(&zv, ex);
ce = Z_OBJCE(zv);
msg = zend_read_property(ce, &zv, ZEND_STRL("message"), 0, &rv);
code = zend_read_property(ce, &zv, ZEND_STRL("code"), 0, &rv);
file = zend_read_property(ce, &zv, ZEND_STRL("file"), 0, &rv);
line = zend_read_property(ce, &zv, ZEND_STRL("line"), 0, &rv);
#else
ce = ex->ce;
msg = zend_read_property(ce, ex, ZEND_STRL("message"), 0, &rv);
code = zend_read_property(ce, ex, ZEND_STRL("code"), 0, &rv);
file = zend_read_property(ce, ex, ZEND_STRL("file"), 0, &rv);
line = zend_read_property(ce, ex, ZEND_STRL("line"), 0, &rv);
#endif
array_init(&response->err);
Z_TRY_ADDREF_P(msg);
Z_TRY_ADDREF_P(code);
Z_TRY_ADDREF_P(file);
Z_TRY_ADDREF_P(line);
add_assoc_zval_ex(&response->err, ZEND_STRL("message"), msg);
add_assoc_zval_ex(&response->err, ZEND_STRL("code"), code);
add_assoc_zval_ex(&response->err, ZEND_STRL("file"), file);
add_assoc_zval_ex(&response->err, ZEND_STRL("line"), line);
add_assoc_str_ex(&response->err, ZEND_STRL("_type"), ce->name);
response->status = YAR_ERR_EXCEPTION;
} /* }}} */
void php_yar_response_set_retval(yar_response_t *response, zval *retval) /* {{{ */ {
ZVAL_COPY(&response->retval, retval);
} /* }}} */
void php_yar_response_map_retval(yar_response_t *response, zval *ret) /* {{{ */ {
zval *zv;
HashTable *ht;
if (IS_ARRAY != Z_TYPE_P(ret)) {
return;
}
ht = Z_ARRVAL_P(ret);
if ((zv = zend_hash_find(ht, ZSTR_CHAR('i'))) == NULL) {
return;
}
response->id = zval_get_long(zv);
if ((zv = zend_hash_find(ht, ZSTR_CHAR('s'))) == NULL) {
return;
}
response->status = zval_get_long(zv);
if (response->status == YAR_ERR_OKEY) {
if ((zv = zend_hash_find(ht, ZSTR_CHAR('o'))) != NULL) {
if (Z_TYPE_P(zv) == IS_STRING) {
response->out = Z_STR_P(zv);
ZVAL_NULL(zv);
}
}
if ((zv = zend_hash_find(ht, ZSTR_CHAR('r'))) != NULL) {
ZVAL_COPY_VALUE(&response->retval, zv);
ZVAL_NULL(zv);
}
} else if ((zv = zend_hash_find(ht, ZSTR_CHAR('e'))) != NULL) {
ZVAL_COPY_VALUE(&response->err, zv);
ZVAL_NULL(zv);
}
}
/* }}} */
void php_yar_response_destroy(yar_response_t *response) /* {{{ */ {
if (response->out) {
zend_string_release(response->out);
}
zval_ptr_dtor(&response->retval);
zval_ptr_dtor(&response->err);
efree(response);
} /* }}} */
/*
* 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
*/