forked from laruence/yaf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
yaf_response.c
383 lines (310 loc) · 10.5 KB
/
yaf_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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
/*
+----------------------------------------------------------------------+
| Yet Another Framework |
+----------------------------------------------------------------------+
| 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]> |
+----------------------------------------------------------------------+
*/
/* $Id: yaf_response.c 315957 2011-09-01 09:03:32Z laruence $ */
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "php.h"
#include "php_ini.h"
#include "main/SAPI.h"
#include "Zend/zend_interfaces.h"
#include "php_yaf.h"
#include "yaf_namespace.h"
#include "yaf_response.h"
#include "yaf_exception.h"
zend_class_entry *yaf_response_ce;
#include "response/http.c"
#include "response/cli.c"
/** {{{ yaf_response_t * yaf_response_instance(yaf_response_t *this_ptr, char *sapi_name TSRMLS_DC)
*/
yaf_response_t * yaf_response_instance(yaf_response_t *this_ptr, char *sapi_name TSRMLS_DC) {
zval *header;
zend_class_entry *ce;
yaf_response_t *instance;
if (strncasecmp(sapi_name, "cli", 3)) {
ce = yaf_response_http_ce;
} else {
ce = yaf_response_cli_ce;
}
if (this_ptr) {
instance = this_ptr;
} else {
MAKE_STD_ZVAL(instance);
object_init_ex(instance, ce);
}
MAKE_STD_ZVAL(header);
array_init(header);
zend_update_property(ce, instance, ZEND_STRL(YAF_RESPONSE_PROPERTY_NAME_HEADER), header TSRMLS_CC);
zval_ptr_dtor(&header);
zend_update_property_string(ce, instance, ZEND_STRL(YAF_RESPONSE_PROPERTY_NAME_BODY), "" TSRMLS_CC);
return instance;
}
/* }}} */
/** {{{ static int yaf_response_set_body(yaf_response_t *response, char *name, int name_len, char *body, long body_len TSRMLS_DC)
*/
#if 0
static int yaf_response_set_body(yaf_response_t *response, char *name, int name_len, char *body, long body_len TSRMLS_DC) {
zval *zbody;
zend_class_entry *response_ce;
if (!body_len) {
return 1;
}
response_ce = Z_OBJCE_P(response);
zbody = zend_read_property(response_ce, response, ZEND_STRL(YAF_RESPONSE_PROPERTY_NAME_BODY), 1 TSRMLS_CC);
zval_dtor(zbody);
efree(zbody);
MAKE_STD_ZVAL(zbody);
ZVAL_STRINGL(zbody, body, body_len, 1);
zend_update_property(response_ce, response, ZEND_STRL(YAF_RESPONSE_PROPERTY_NAME_BODY), zbody TSRMLS_CC);
return 1;
}
#endif
/* }}} */
/** {{{ int yaf_response_alter_body(yaf_response_t *response, char *name, int name_len, char *body, long body_len, int prepend TSRMLS_DC)
*/
int yaf_response_alter_body(yaf_response_t *response, char *name, int name_len, char *body, long body_len, int prepend TSRMLS_DC) {
zval *zbody;
char *obody;
if (!body_len) {
return 1;
}
zbody = zend_read_property(yaf_response_ce, response, ZEND_STRL(YAF_RESPONSE_PROPERTY_NAME_BODY), 1 TSRMLS_CC);
obody = Z_STRVAL_P(zbody);
if (prepend) {
Z_STRLEN_P(zbody) = spprintf(&Z_STRVAL_P(zbody), 0, "%s%s", body, obody);
} else {
Z_STRLEN_P(zbody) = spprintf(&Z_STRVAL_P(zbody), 0, "%s%s", obody, body);
}
efree(obody);
return 1;
}
/* }}} */
/** {{{ int yaf_response_clear_body(yaf_response_t *response TSRMLS_DC)
*/
int yaf_response_clear_body(yaf_response_t *response TSRMLS_DC) {
zend_update_property_string(yaf_response_ce, response, ZEND_STRL(YAF_RESPONSE_PROPERTY_NAME_BODY), "" TSRMLS_CC);
return 1;
}
/* }}} */
/** {{{ int yaf_response_set_redirect(yaf_response_t *response, char *url, int len TSRMLS_DC)
*/
int yaf_response_set_redirect(yaf_response_t *response, char *url, int len TSRMLS_DC) {
sapi_header_line ctr = {0};
ctr.line_len = spprintf(&(ctr.line), 0, "%s %s", "Location:", url);
ctr.response_code = 0;
if (sapi_header_op(SAPI_HEADER_REPLACE, &ctr TSRMLS_CC) == SUCCESS) {
efree(ctr.line);
return 1;
}
efree(ctr.line);
return 0;
}
/* }}} */
/** {{{ int yaf_response_send(yaf_response_t *response TSRMLS_DC)
*/
int yaf_response_send(yaf_response_t *response TSRMLS_DC) {
zval *body = zend_read_property(yaf_response_ce, response, ZEND_STRL(YAF_RESPONSE_PROPERTY_NAME_BODY), 1 TSRMLS_CC);
if (IS_STRING == Z_TYPE_P(body) && Z_STRLEN_P(body) > 0) {
if (php_write(Z_STRVAL_P(body), Z_STRLEN_P(body) TSRMLS_CC) == FAILURE) {
return 0;
}
}
return 1;
}
/* }}} */
/** {{{ zval * yaf_response_get_body(yaf_response_t *response TSRMLS_DC)
*/
zval * yaf_response_get_body(yaf_response_t *response TSRMLS_DC) {
zval *body = zend_read_property(yaf_response_ce, response, ZEND_STRL(YAF_RESPONSE_PROPERTY_NAME_BODY), 1 TSRMLS_CC);
return body;
}
/* }}} */
/** {{{ proto private Yaf_Response_Abstract::__construct()
*/
PHP_METHOD(yaf_response, __construct) {
(void)yaf_response_instance(getThis(), sapi_module.name TSRMLS_CC);
}
/* }}} */
/** {{{ proto public Yaf_Response_Abstract::__desctruct(void)
*/
PHP_METHOD(yaf_response, __destruct) {
}
/* }}} */
/** {{{ proto public Yaf_Response_Abstract::appenBody($body, $name = NULL)
*/
PHP_METHOD(yaf_response, appendBody) {
char *body, *name = NULL;
uint body_len, name_len = 0;
yaf_response_t *self;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|s", &body, &body_len, &name, &name_len) == FAILURE) {
return;
}
self = getThis();
if (yaf_response_alter_body(self, name, name_len, body, body_len, 0 TSRMLS_CC)) {
RETURN_ZVAL(self, 1, 0);
}
RETURN_FALSE;
}
/* }}} */
/** {{{ proto public Yaf_Response_Abstract::prependBody($body, $name = NULL)
*/
PHP_METHOD(yaf_response, prependBody) {
char *body, *name = NULL;
uint body_len, name_len = 0;
yaf_response_t *self;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|s", &body, &body_len, &name, &name_len) == FAILURE) {
return;
}
self = getThis();
if (yaf_response_alter_body(self, name, name_len, body, body_len, 1 TSRMLS_CC)) {
RETURN_ZVAL(self, 1, 0);
}
RETURN_FALSE;
}
/* }}} */
/** {{{ proto public Yaf_Response_Abstract::setHeader($name, $value, $replace = 0)
*/
PHP_METHOD(yaf_response, setHeader) {
RETURN_FALSE;
}
/* }}} */
/** {{{ proto parotected Yaf_Response_Abstract::setAllHeaders(void)
*/
PHP_METHOD(yaf_response, setAllHeaders) {
RETURN_FALSE;
}
/* }}} */
/** {{{ proto public Yaf_Response_Abstract::getHeader(void)
*/
PHP_METHOD(yaf_response, getHeader) {
RETURN_NULL();
}
/* }}} */
/** {{{ proto public Yaf_Response_Abstract::clearHeaders(void)
*/
PHP_METHOD(yaf_response, clearHeaders) {
RETURN_FALSE;
}
/* }}} */
/** {{{ proto public Yaf_Response_Abstract::setRedirect(string $url)
*/
PHP_METHOD(yaf_response, setRedirect) {
char *url;
uint url_len;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &url, &url_len) == FAILURE) {
return;
}
if (!url_len) {
RETURN_FALSE;
}
RETURN_BOOL(yaf_response_set_redirect(getThis(), url, url_len TSRMLS_CC));
}
/* }}} */
/** {{{ proto public Yaf_Response_Abstract::setBody($body, $name = NULL)
*/
PHP_METHOD(yaf_response, setBody) {
char *body, *name = NULL;
uint body_len, name_len = 0;
yaf_response_t *self;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|s", &body, &body_len, &name, &name_len) == FAILURE) {
return;
}
self = getThis();
if (yaf_response_alter_body(self, name, name_len, body, body_len, 0 TSRMLS_CC)) {
RETURN_ZVAL(self, 1, 0);
}
RETURN_FALSE;
}
/* }}} */
/** {{{ proto public Yaf_Response_Abstract::clearBody(void)
*/
PHP_METHOD(yaf_response, clearBody) {
if (yaf_response_clear_body(getThis() TSRMLS_CC)) {
RETURN_ZVAL(getThis(), 1, 0);
}
RETURN_FALSE;
}
/* }}} */
/** {{{ proto public Yaf_Response_Abstract::getBody(void)
*/
PHP_METHOD(yaf_response, getBody) {
zval *body;
body = yaf_response_get_body(getThis() TSRMLS_CC);
RETURN_ZVAL(body, 1, 0);
}
/* }}} */
/** {{{ proto public Yaf_Response_Abstract::response(void)
*/
PHP_METHOD(yaf_response, response) {
RETURN_BOOL(yaf_response_send(getThis() TSRMLS_CC));
}
/* }}} */
/** {{{ proto public Yaf_Response_Abstract::__toString(void)
*/
PHP_METHOD(yaf_response, __toString) {
zval *body = zend_read_property(yaf_response_ce, getThis(), ZEND_STRL(YAF_RESPONSE_PROPERTY_NAME_BODY), 1 TSRMLS_CC);
RETURN_ZVAL(body, 1, 0);
}
/* }}} */
/** {{{ proto public Yaf_Response_Abstract::__clone(void)
*/
PHP_METHOD(yaf_response, __clone) {
}
/* }}} */
/** {{{ yaf_response_methods
*/
zend_function_entry yaf_response_methods[] = {
PHP_ME(yaf_response, __construct, NULL, ZEND_ACC_PUBLIC|ZEND_ACC_CTOR)
PHP_ME(yaf_response, __destruct, NULL, ZEND_ACC_PUBLIC|ZEND_ACC_DTOR)
PHP_ME(yaf_response, __clone, NULL, ZEND_ACC_PRIVATE)
PHP_ME(yaf_response, __toString, NULL, ZEND_ACC_PRIVATE)
PHP_ME(yaf_response, setBody, NULL, ZEND_ACC_PUBLIC)
PHP_ME(yaf_response, appendBody, NULL, ZEND_ACC_PUBLIC)
PHP_ME(yaf_response, prependBody, NULL, ZEND_ACC_PUBLIC)
PHP_ME(yaf_response, clearBody, NULL, ZEND_ACC_PUBLIC)
PHP_ME(yaf_response, getBody, NULL, ZEND_ACC_PUBLIC)
PHP_ME(yaf_response, setHeader, NULL, ZEND_ACC_PUBLIC)
PHP_ME(yaf_response, setAllHeaders, NULL, ZEND_ACC_PROTECTED)
PHP_ME(yaf_response, getHeader, NULL, ZEND_ACC_PUBLIC)
PHP_ME(yaf_response, clearHeaders, NULL, ZEND_ACC_PUBLIC)
PHP_ME(yaf_response, setRedirect, NULL, ZEND_ACC_PUBLIC)
PHP_ME(yaf_response, response, NULL, ZEND_ACC_PUBLIC)
{NULL, NULL, NULL}
};
/* }}} */
/** {{{ YAF_STARTUP_FUNCTION
*/
YAF_STARTUP_FUNCTION(response) {
zend_class_entry ce;
YAF_INIT_CLASS_ENTRY(ce, "Yaf_Response_Abstract", "Yaf\\Response_Abstract", yaf_response_methods);
yaf_response_ce = zend_register_internal_class_ex(&ce, NULL, NULL TSRMLS_CC);
yaf_response_ce->ce_flags |= ZEND_ACC_EXPLICIT_ABSTRACT_CLASS;
zend_declare_property_null(yaf_response_ce, ZEND_STRL(YAF_RESPONSE_PROPERTY_NAME_HEADER), ZEND_ACC_PROTECTED TSRMLS_CC);
zend_declare_property_null(yaf_response_ce, ZEND_STRL(YAF_RESPONSE_PROPERTY_NAME_BODY), ZEND_ACC_PROTECTED TSRMLS_CC);
zend_declare_property_bool(yaf_response_ce, ZEND_STRL(YAF_RESPONSE_PROPERTY_NAME_HEADEREXCEPTION), 0, ZEND_ACC_PROTECTED TSRMLS_CC);
YAF_STARTUP(response_http);
YAF_STARTUP(response_cli);
return SUCCESS;
}
/* }}} */
/*
* 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
*/