1c5ec4214c076c9f9ba9cba44cf3a459fa815007
[m6w6/ext-http] / http_response_object.c
1 /*
2 +----------------------------------------------------------------------+
3 | PECL :: http |
4 +----------------------------------------------------------------------+
5 | This source file is subject to version 3.0 of the PHP license, that |
6 | is bundled with this package in the file LICENSE, and is available |
7 | through the world-wide-web at http://www.php.net/license/3_0.txt. |
8 | If you did not receive a copy of the PHP license and are unable to |
9 | obtain it through the world-wide-web, please send a note to |
10 | license@php.net so we can mail you a copy immediately. |
11 +----------------------------------------------------------------------+
12 | Copyright (c) 2004-2005 Michael Wallner <mike@php.net> |
13 +----------------------------------------------------------------------+
14 */
15
16 /* $Id$ */
17
18
19 #ifdef HAVE_CONFIG_H
20 # include "config.h"
21 #endif
22 #include "php.h"
23
24 #ifdef ZEND_ENGINE_2
25
26 #include "SAPI.h"
27 #include "php_ini.h"
28
29 #include "php_http.h"
30 #include "php_http_api.h"
31 #include "php_http_std_defs.h"
32 #include "php_http_response_object.h"
33 #include "php_http_exception_object.h"
34 #include "php_http_send_api.h"
35 #include "php_http_cache_api.h"
36
37 #include "missing.h"
38
39 ZEND_EXTERN_MODULE_GLOBALS(http);
40
41 #define GET_STATIC_PROP(n) *GET_STATIC_PROP_EX(http_response_object_ce, n)
42 #define UPD_STATIC_PROP(t, n, v) UPD_STATIC_PROP_EX(http_response_object_ce, t, n, v)
43 #define SET_STATIC_PROP(n, v) SET_STATIC_PROP_EX(http_response_object_ce, n, v)
44
45 #define HTTP_BEGIN_ARGS(method, req_args) HTTP_BEGIN_ARGS_EX(HttpResponse, method, 0, req_args)
46 #define HTTP_EMPTY_ARGS(method, ret_ref) HTTP_EMPTY_ARGS_EX(HttpResponse, method, ret_ref)
47 #define HTTP_RESPONSE_ME(method, visibility) PHP_ME(HttpResponse, method, HTTP_ARGS(HttpResponse, method), visibility|ZEND_ACC_STATIC)
48 #define HTTP_RESPONSE_ALIAS(method, func) HTTP_STATIC_ME_ALIAS(method, func, HTTP_ARGS(HttpResponse, method))
49
50 HTTP_BEGIN_ARGS(setHeader, 2)
51 HTTP_ARG_VAL(name, 0)
52 HTTP_ARG_VAL(value, 0)
53 HTTP_ARG_VAL(replace, 0)
54 HTTP_END_ARGS;
55
56 HTTP_BEGIN_ARGS(getHeader, 0)
57 HTTP_ARG_VAL(name, 0)
58 HTTP_END_ARGS;
59
60 HTTP_EMPTY_ARGS(getETag, 0);
61 HTTP_BEGIN_ARGS(setETag, 1)
62 HTTP_ARG_VAL(etag, 0)
63 HTTP_END_ARGS;
64
65 HTTP_EMTPY_ARGS(getLastModified, 0);
66 HTTP_BEGIN_ARGS(setLastModified, 1)
67 HTTP_ARG_VAL(timestamp, 0)
68 HTTP_END_ARGS;
69
70 HTTP_EMPTY_ARGS(getCache, 0);
71 HTTP_BEGIN_ARGS(setCache, 1)
72 HTTP_ARG_VAL(cache, 0)
73 HTTP_END_ARGS;
74
75 HTTP_EMPTY_ARGS(getGzip, 0);
76 HTTP_BEGIN_ARGS(setGzip, 1)
77 HTTP_ARG_VAL(gzip, 0)
78 HTTP_END_ARGS;
79
80 HTTP_EMPTY_ARGS(getCacheControl, 0);
81 HTTP_BEGIN_ARGS(setCacheControl, 1)
82 HTTP_ARG_VAL(cache_control, 0)
83 HTTP_ARG_VAL(max_age, 0)
84 HTTP_END_ARGS;
85
86 HTTP_EMPTY_ARGS(getContentType, 0);
87 HTTP_BEGIN_ARGS(setContentType, 1)
88 HTTP_ARG_VAL(content_type, 0)
89 HTTP_END_ARGS;
90
91 HTTP_EMPTY_ARGS(getContentDisposition, 0);
92 HTTP_BEGIN_ARGS(setContentDisposition, 1)
93 HTTP_ARG_VAL(filename, 0)
94 HTTP_ARG_VAL(send_inline, 0)
95 HTTP_END_ARGS;
96
97 HTTP_EMPTY_ARGS(getThrottleDelay, 0);
98 HTTP_BEGIN_ARGS(setThrottleDelay, 1)
99 HTTP_ARG_VAL(seconds, 0)
100 HTTP_END_ARGS;
101
102 HTTP_EMPTY_ARGS(getBufferSize, 0);
103 HTTP_BEGIN_ARGS(setBufferSize, 1)
104 HTTP_ARG_VAL(bytes, 0)
105 HTTP_END_ARGS;
106
107 HTTP_EMPTY_ARGS(getData, 0);
108 HTTP_BEGIN_ARGS(setData, 1)
109 HTTP_ARG_VAL(data, 0)
110 HTTP_END_ARGS;
111
112 HTTP_EMPTY_ARGS(getStream, 0);
113 HTTP_BEGIN_ARGS(setStream, 1)
114 HTTP_ARG_VAL(stream, 0)
115 HTTP_END_ARGS;
116
117 HTTP_EMPTY_ARGS(getFile, 0);
118 HTTP_BEGIN_ARGS(setFile, 1)
119 HTTP_ARG_VAL(filepath, 0)
120 HTTP_END_ARGS;
121
122 HTTP_BEGIN_ARGS(send, 0)
123 HTTP_ARG_VAL(clean_ob, 0)
124 HTTP_END_ARGS;
125
126 HTTP_EMPTY_ARGS(capture, 0);
127
128 HTTP_BEGIN_ARGS(redirect, 0)
129 HTTP_ARG_VAL(url, 0)
130 HTTP_ARG_VAL(params, 0)
131 HTTP_ARG_VAL(session, 0)
132 HTTP_ARG_VAL(permanent, 0)
133 HTTP_END_ARGS;
134
135 HTTP_BEGIN_ARGS(status, 1)
136 HTTP_ARG_VAL(code, 0)
137 HTTP_END_ARGS;
138
139 HTTP_EMPTY_ARGS(getRequestHeaders, 0);
140 HTTP_EMPTY_ARGS(getRequestBody, 0);
141
142 #define http_response_object_declare_default_properties() _http_response_object_declare_default_properties(TSRMLS_C)
143 static inline void _http_response_object_declare_default_properties(TSRMLS_D);
144
145 HashTable http_response_statics;
146
147 zend_class_entry *http_response_object_ce;
148 zend_function_entry http_response_object_fe[] = {
149
150 HTTP_RESPONSE_ME(setHeader, ZEND_ACC_PUBLIC)
151 HTTP_RESPONSE_ME(getHeader, ZEND_ACC_PUBLIC)
152
153 HTTP_RESPONSE_ME(setETag, ZEND_ACC_PUBLIC)
154 HTTP_RESPONSE_ME(getETag, ZEND_ACC_PUBLIC)
155
156 HTTP_RESPONSE_ME(setLastModified, ZEND_ACC_PUBLIC)
157 HTTP_RESPONSE_ME(getLastModified, ZEND_ACC_PUBLIC)
158
159 HTTP_RESPONSE_ME(setContentDisposition, ZEND_ACC_PUBLIC)
160 HTTP_RESPONSE_ME(getContentDisposition, ZEND_ACC_PUBLIC)
161
162 HTTP_RESPONSE_ME(setContentType, ZEND_ACC_PUBLIC)
163 HTTP_RESPONSE_ME(getContentType, ZEND_ACC_PUBLIC)
164
165 HTTP_RESPONSE_ME(setCache, ZEND_ACC_PUBLIC)
166 HTTP_RESPONSE_ME(getCache, ZEND_ACC_PUBLIC)
167
168 HTTP_RESPONSE_ME(setCacheControl, ZEND_ACC_PUBLIC)
169 HTTP_RESPONSE_ME(getCacheControl, ZEND_ACC_PUBLIC)
170
171 HTTP_RESPONSE_ME(setGzip, ZEND_ACC_PUBLIC)
172 HTTP_RESPONSE_ME(getGzip, ZEND_ACC_PUBLIC)
173
174 HTTP_RESPONSE_ME(setThrottleDelay, ZEND_ACC_PUBLIC)
175 HTTP_RESPONSE_ME(getThrottleDelay, ZEND_ACC_PUBLIC)
176
177 HTTP_RESPONSE_ME(setBufferSize, ZEND_ACC_PUBLIC)
178 HTTP_RESPONSE_ME(getBufferSize, ZEND_ACC_PUBLIC)
179
180 HTTP_RESPONSE_ME(setData, ZEND_ACC_PUBLIC)
181 HTTP_RESPONSE_ME(getData, ZEND_ACC_PUBLIC)
182
183 HTTP_RESPONSE_ME(setFile, ZEND_ACC_PUBLIC)
184 HTTP_RESPONSE_ME(getFile, ZEND_ACC_PUBLIC)
185
186 HTTP_RESPONSE_ME(setStream, ZEND_ACC_PUBLIC)
187 HTTP_RESPONSE_ME(getStream, ZEND_ACC_PUBLIC)
188
189 HTTP_RESPONSE_ME(send, ZEND_ACC_PUBLIC)
190 HTTP_RESPONSE_ME(capture, ZEND_ACC_PUBLIC)
191
192 HTTP_RESPONSE_ALIAS(redirect, http_redirect)
193 HTTP_RESPONSE_ALIAS(status, http_send_status)
194 HTTP_RESPONSE_ALIAS(getRequestHeaders, http_get_request_headers)
195 HTTP_RESPONSE_ALIAS(getRequestBody, http_get_request_body)
196
197 {NULL, NULL, NULL}
198 };
199
200 void _http_response_object_init(INIT_FUNC_ARGS)
201 {
202 HTTP_REGISTER_CLASS(HttpResponse, http_response_object, NULL, 0);
203 http_response_object_declare_default_properties();
204 }
205
206 static inline void _http_response_object_declare_default_properties(TSRMLS_D)
207 {
208 zend_class_entry *ce = http_response_object_ce;
209
210 DCL_STATIC_PROP(PRIVATE, bool, sent, 0);
211 DCL_STATIC_PROP(PRIVATE, bool, catch, 0);
212 DCL_STATIC_PROP(PRIVATE, long, mode, -1);
213 DCL_STATIC_PROP(PROTECTED, bool, cache, 0);
214 DCL_STATIC_PROP(PROTECTED, bool, gzip, 0);
215 DCL_STATIC_PROP(PROTECTED, long, stream, 0);
216 DCL_STATIC_PROP_N(PROTECTED, file);
217 DCL_STATIC_PROP_N(PROTECTED, data);
218 DCL_STATIC_PROP_N(PROTECTED, eTag);
219 DCL_STATIC_PROP(PROTECTED, long, lastModified, 0);
220 DCL_STATIC_PROP_N(PROTECTED, cacheControl);
221 DCL_STATIC_PROP_N(PROTECTED, contentType);
222 DCL_STATIC_PROP_N(PROTECTED, contentDisposition);
223 DCL_STATIC_PROP(PROTECTED, long, bufferSize, HTTP_SENDBUF_SIZE);
224 DCL_STATIC_PROP(PROTECTED, double, throttleDelay, 0.0);
225 DCL_STATIC_PROP_N(PROTECTED, headers);
226 }
227
228 /* ### USERLAND ### */
229
230 /* {{{ proto static bool HttpResponse::setHeader(string name, mixed value[, bool replace = true)
231 */
232 PHP_METHOD(HttpResponse, setHeader)
233 {
234 zend_bool replace = 1;
235 char *name;
236 int name_len = 0;
237 zval *value = NULL, *headers, **header;
238
239 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sz/!|b", &name, &name_len, &value, &replace)) {
240 RETURN_FALSE;
241 }
242 if (!name_len) {
243 http_error(HE_WARNING, HTTP_E_HEADER, "Cannot send anonymous headers");
244 RETURN_FALSE;
245 }
246
247 headers = GET_STATIC_PROP(headers);
248
249 if (Z_TYPE_P(headers) != IS_ARRAY) {
250 convert_to_array(headers);
251 }
252
253 /* delete header if value == null */
254 if (!value || Z_TYPE_P(value) == IS_NULL) {
255 RETURN_SUCCESS(zend_hash_del(Z_ARRVAL_P(headers), name, name_len + 1));
256 }
257
258 if (Z_TYPE_P(value) != IS_STRING) {
259 convert_to_string_ex(&value);
260 }
261
262 /* convert old header to an array and add new one if header exists and replace == false */
263 if (replace || (SUCCESS != zend_hash_find(Z_ARRVAL_P(headers), name, name_len + 1, (void **) &header))) {
264 RETURN_SUCCESS(add_assoc_stringl_ex(headers, name, name_len + 1, Z_STRVAL_P(value), Z_STRLEN_P(value), 1));
265 } else {
266 convert_to_array(*header);
267 RETURN_SUCCESS(add_next_index_stringl(*header, Z_STRVAL_P(value), Z_STRLEN_P(value), 1));
268 }
269 }
270 /* }}} */
271
272 /* {{{ proto static mixed HttpResponse::getHeader([string name])
273 */
274 PHP_METHOD(HttpResponse, getHeader)
275 {
276 char *name = NULL;
277 int name_len = 0;
278 zval *headers, **header;
279
280 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s", &name, &name_len)) {
281 RETURN_FALSE;
282 }
283
284 headers = GET_STATIC_PROP(headers);
285 if (Z_TYPE_P(headers) != IS_ARRAY) {
286 convert_to_array(headers);
287 }
288
289 if (!name || !name_len) {
290 array_init(return_value);
291 array_copy(headers, return_value);
292 } else if (SUCCESS == zend_hash_find(Z_ARRVAL_P(headers), name, name_len + 1, (void **) &header)) {
293 RETURN_ZVAL(*header, ZVAL_PTR_DTOR, 1);
294 } else {
295 RETURN_NULL();
296 }
297 }
298 /* }}} */
299
300 /* {{{ proto static bool HttpResponse::setCache(bool cache)
301 *
302 * Whether it sould be attempted to cache the entitity.
303 * This will result in necessary caching headers and checks of clients
304 * "If-Modified-Since" and "If-None-Match" headers. If one of those headers
305 * matches a "304 Not Modified" status code will be issued.
306 *
307 * NOTE: If you're using sessions, be shure that you set session.cache_limiter
308 * to something more appropriate than "no-cache"!
309 */
310 PHP_METHOD(HttpResponse, setCache)
311 {
312 zend_bool do_cache = 0;
313
314 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "b", &do_cache)) {
315 RETURN_FALSE;
316 }
317
318 RETURN_SUCCESS(UPD_STATIC_PROP(bool, cache, do_cache));
319 }
320 /* }}} */
321
322 /* {{{ proto static bool HttpResponse::getCache()
323 *
324 * Get current caching setting.
325 */
326 PHP_METHOD(HttpResponse, getCache)
327 {
328 NO_ARGS;
329
330 IF_RETVAL_USED {
331 RETURN_BOOL(Z_LVAL_P(GET_STATIC_PROP(cache)));
332 }
333 }
334 /* }}}*/
335
336 /* {{{ proto static bool HttpResponse::setGzip(bool gzip)
337 *
338 * Enable on-thy-fly gzipping of the sent entity. NOT IMPLEMENTED YET.
339 */
340 PHP_METHOD(HttpResponse, setGzip)
341 {
342 zend_bool do_gzip = 0;
343
344 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "b", &do_gzip)) {
345 RETURN_FALSE;
346 }
347
348 RETURN_SUCCESS(UPD_STATIC_PROP(bool, gzip, do_gzip));
349 }
350 /* }}} */
351
352 /* {{{ proto static bool HttpResponse::getGzip()
353 *
354 * Get current gzipping setting.
355 */
356 PHP_METHOD(HttpResponse, getGzip)
357 {
358 NO_ARGS;
359
360 IF_RETVAL_USED {
361 RETURN_BOOL(Z_LVAL_P(GET_STATIC_PROP(gzip)));
362 }
363 }
364 /* }}} */
365
366 /* {{{ proto static bool HttpResponse::setCacheControl(string control[, long max_age = 0])
367 *
368 * Set a custom cache-control header, usually being "private" or "public";
369 * The max_age parameter controls how long the cache entry is valid on the client side.
370 */
371 PHP_METHOD(HttpResponse, setCacheControl)
372 {
373 char *ccontrol, *cctl;
374 int cc_len;
375 long max_age = 0;
376
377 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|l", &ccontrol, &cc_len, &max_age)) {
378 RETURN_FALSE;
379 }
380
381 if (strcmp(ccontrol, "public") && strcmp(ccontrol, "private") && strcmp(ccontrol, "no-cache")) {
382 http_error_ex(HE_WARNING, HTTP_E_INVALID_PARAM, "Cache-Control '%s' doesn't match public, private or no-cache", ccontrol);
383 RETURN_FALSE;
384 } else {
385 spprintf(&cctl, 0, "%s, must-revalidate, max_age=%ld", ccontrol, max_age);
386 RETVAL_SUCCESS(UPD_STATIC_PROP(string, cacheControl, cctl));
387 efree(cctl);
388 }
389 }
390 /* }}} */
391
392 /* {{{ proto static string HttpResponse::getCacheControl()
393 *
394 * Get current Cache-Control header setting.
395 */
396 PHP_METHOD(HttpResponse, getCacheControl)
397 {
398 NO_ARGS;
399
400 IF_RETVAL_USED {
401 zval *ccontrol = GET_STATIC_PROP(cacheControl);
402 RETURN_STRINGL(Z_STRVAL_P(ccontrol), Z_STRLEN_P(ccontrol), 1);
403 }
404 }
405 /* }}} */
406
407 /* {{{ proto static bool HttpResponse::setContentType(string content_type)
408 *
409 * Set the content-type of the sent entity.
410 */
411 PHP_METHOD(HttpResponse, setContentType)
412 {
413 char *ctype;
414 int ctype_len;
415
416 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &ctype, &ctype_len)) {
417 RETURN_FALSE;
418 }
419
420 if (!strchr(ctype, '/')) {
421 http_error_ex(HE_WARNING, HTTP_E_INVALID_PARAM, "Content type '%s' doesn't seem to contain a primary and a secondary part", ctype);
422 RETURN_FALSE;
423 }
424
425 RETURN_SUCCESS(UPD_STATIC_PROP(string, contentType, ctype));
426 }
427 /* }}} */
428
429 /* {{{ proto static string HttpResponse::getContentType()
430 *
431 * Get current Content-Type header setting.
432 */
433 PHP_METHOD(HttpResponse, getContentType)
434 {
435 NO_ARGS;
436
437 IF_RETVAL_USED {
438 zval *ctype = GET_STATIC_PROP(contentType);
439 RETURN_STRINGL(Z_STRVAL_P(ctype), Z_STRLEN_P(ctype), 1);
440 }
441 }
442 /* }}} */
443
444 /* {{{ proto static bool HttpResponse::setContentDisposition(string filename[, bool inline = false])
445 *
446 * Set the Content-Disposition of the sent entity. This setting aims to suggest
447 * the receiveing user agent how to handle the sent entity; usually the client
448 * will show the user a "Save As..." popup.
449 */
450 PHP_METHOD(HttpResponse, setContentDisposition)
451 {
452 char *file, *cd;
453 int file_len;
454 zend_bool send_inline = 0;
455
456 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|b", &file, &file_len, &send_inline)) {
457 RETURN_FALSE;
458 }
459
460 spprintf(&cd, 0, "%s; filename=\"%s\"", send_inline ? "inline" : "attachment", file);
461 RETVAL_SUCCESS(UPD_STATIC_PROP(string, contentDisposition, cd));
462 efree(cd);
463 }
464 /* }}} */
465
466 /* {{{ proto static string HttpResponse::getContentDisposition()
467 *
468 * Get current Content-Disposition setting.
469 */
470 PHP_METHOD(HttpResponse, getContentDisposition)
471 {
472 NO_ARGS;
473
474 IF_RETVAL_USED {
475 zval *cd = GET_STATIC_PROP(contentDisposition);
476 RETURN_STRINGL(Z_STRVAL_P(cd), Z_STRLEN_P(cd), 1);
477 }
478 }
479 /* }}} */
480
481 /* {{{ proto static bool HttpResponse::setETag(string etag)
482 *
483 * Set a custom ETag. Use this only if you know what you're doing.
484 */
485 PHP_METHOD(HttpResponse, setETag)
486 {
487 char *etag;
488 int etag_len;
489
490 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &etag, &etag_len)) {
491 RETURN_FALSE;
492 }
493
494 RETURN_SUCCESS(UPD_STATIC_PROP(string, eTag, etag));
495 }
496 /* }}} */
497
498 /* {{{ proto static string HttpResponse::getETag()
499 *
500 * Get calculated or previously set custom ETag.
501 */
502 PHP_METHOD(HttpResponse, getETag)
503 {
504 NO_ARGS;
505
506 IF_RETVAL_USED {
507 zval *etag = GET_STATIC_PROP(eTag);
508 RETURN_STRINGL(Z_STRVAL_P(etag), Z_STRLEN_P(etag), 1);
509 }
510 }
511 /* }}} */
512
513 /* {{{ proto static bool HttpResponse::setLastModified(long timestamp)
514 *
515 * Set a custom Last-Modified date.
516 */
517 PHP_METHOD(HttpResponse, setLastModified)
518 {
519 long lm;
520
521 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &lm)) {
522 RETURN_FALSE;
523 }
524
525 RETURN_SUCCESS(UPD_STATIC_PROP(long, lastModified, lm));
526 }
527 /* }}} */
528
529 /* {{{ proto static HttpResponse::getLastModified()
530 *
531 * Get calculated or previously set custom Last-Modified date.
532 */
533 PHP_METHOD(HttpResponse, getLastModified)
534 {
535 NO_ARGS;
536
537 IF_RETVAL_USED {
538 RETURN_LONG(Z_LVAL_P(GET_STATIC_PROP(lastModified)));
539 }
540 }
541 /* }}} */
542
543 /* {{{ proto static bool HttpResponse::setThrottleDelay(double seconds)
544 *
545 */
546 PHP_METHOD(HttpResponse, setThrottleDelay)
547 {
548 double seconds;
549
550 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "d", &seconds)) {
551 RETURN_FALSE;
552 }
553 RETURN_SUCCESS(UPD_STATIC_PROP(double, throttleDelay, seconds));
554 }
555 /* }}} */
556
557 /* {{{ proto static double HttpResponse::getThrottleDelay()
558 *
559 */
560 PHP_METHOD(HttpResponse, getThrottleDelay)
561 {
562 NO_ARGS;
563
564 IF_RETVAL_USED {
565 RETURN_DOUBLE(Z_DVAL_P(GET_STATIC_PROP(throttleDelay)));
566 }
567 }
568 /* }}} */
569
570 /* {{{ proto static bool HttpResponse::setBufferSize(long bytes)
571 *
572 */
573 PHP_METHOD(HttpResponse, setBufferSize)
574 {
575 long bytes;
576
577 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &bytes)) {
578 RETURN_FALSE;
579 }
580 RETURN_SUCCESS(UPD_STATIC_PROP(long, bufferSize, bytes));
581 }
582 /* }}} */
583
584 /* {{{ proto static long HttpResponse::getBufferSize()
585 *
586 */
587 PHP_METHOD(HttpResponse, getBufferSize)
588 {
589 NO_ARGS;
590
591 IF_RETVAL_USED {
592 RETURN_LONG(Z_LVAL_P(GET_STATIC_PROP(bufferSize)));
593 }
594 }
595 /* }}} */
596
597 /* {{{ proto static bool HttpResponse::setData(string data)
598 *
599 * Set the data to be sent.
600 */
601 PHP_METHOD(HttpResponse, setData)
602 {
603 zval *the_data;
604
605 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &the_data)) {
606 RETURN_FALSE;
607 }
608 if (Z_TYPE_P(the_data) != IS_STRING) {
609 convert_to_string_ex(&the_data);
610 }
611
612 if ( (SUCCESS != SET_STATIC_PROP(data, the_data)) ||
613 (SUCCESS != UPD_STATIC_PROP(long, mode, SEND_DATA))) {
614 RETURN_FALSE;
615 }
616
617 if (!(Z_LVAL_P(GET_STATIC_PROP(lastModified)) > 0)) {
618 UPD_STATIC_PROP(long, lastModified, http_last_modified(the_data, SEND_DATA));
619 }
620 if (!Z_STRLEN_P(GET_STATIC_PROP(eTag))) {
621 char *etag = http_etag(Z_STRVAL_P(the_data), Z_STRLEN_P(the_data), SEND_DATA);
622 UPD_STATIC_PROP(string, eTag, etag);
623 efree(etag);
624 }
625
626 RETURN_TRUE;
627 }
628 /* }}} */
629
630 /* {{{ proto static string HttpResponse::getData()
631 *
632 * Get the previously set data to be sent.
633 */
634 PHP_METHOD(HttpResponse, getData)
635 {
636 NO_ARGS;
637
638 IF_RETVAL_USED {
639 zval *the_data = GET_STATIC_PROP(data);
640 RETURN_STRINGL(Z_STRVAL_P(the_data), Z_STRLEN_P(the_data), 1);
641 }
642 }
643 /* }}} */
644
645 /* {{{ proto static bool HttpResponse::setStream(resource stream)
646 *
647 * Set the resource to be sent.
648 */
649 PHP_METHOD(HttpResponse, setStream)
650 {
651 zval *the_stream;
652 php_stream *the_real_stream;
653
654 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &the_stream)) {
655 RETURN_FALSE;
656 }
657 zend_list_addref(Z_LVAL_P(the_stream));
658 php_stream_from_zval(the_real_stream, &the_stream);
659
660 if ( (SUCCESS != UPD_STATIC_PROP(long, stream, Z_LVAL_P(the_stream))) ||
661 (SUCCESS != UPD_STATIC_PROP(long, mode, SEND_RSRC))) {
662 RETURN_FALSE;
663 }
664
665 if (!(Z_LVAL_P(GET_STATIC_PROP(lastModified)) > 0)) {
666 UPD_STATIC_PROP(long, lastModified, http_last_modified(the_real_stream, SEND_RSRC));
667 }
668 if (!Z_STRLEN_P(GET_STATIC_PROP(eTag))) {
669 char *etag = http_etag(the_real_stream, 0, SEND_RSRC);
670 UPD_STATIC_PROP(string, eTag, etag);
671 efree(etag);
672 }
673
674 RETURN_TRUE;
675 }
676 /* }}} */
677
678 /* {{{ proto static resource HttpResponse::getStream()
679 *
680 * Get the previously set resource to be sent.
681 */
682 PHP_METHOD(HttpResponse, getStream)
683 {
684 NO_ARGS;
685
686 IF_RETVAL_USED {
687 RETURN_RESOURCE(Z_LVAL_P(GET_STATIC_PROP(stream)));
688 }
689 }
690 /* }}} */
691
692 /* {{{ proto static bool HttpResponse::setFile(string file)
693 *
694 * Set the file to be sent.
695 */
696 PHP_METHOD(HttpResponse, setFile)
697 {
698 char *the_file;
699 int file_len;
700
701 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &the_file, &file_len)) {
702 RETURN_FALSE;
703 }
704
705 if ( (SUCCESS != UPD_STATIC_PROP(string, file, the_file)) ||
706 (SUCCESS != UPD_STATIC_PROP(long, mode, -1))) {
707 RETURN_FALSE;
708 }
709
710 if (!(Z_LVAL_P(GET_STATIC_PROP(lastModified)))) {
711 UPD_STATIC_PROP(long, lastModified, http_last_modified(the_file, -1));
712 }
713 if (!Z_STRLEN_P(GET_STATIC_PROP(eTag))) {
714 char *etag = http_etag(the_file, 0, -1);
715 UPD_STATIC_PROP(string, eTag, etag);
716 efree(etag);
717 }
718
719 RETURN_TRUE;
720 }
721 /* }}} */
722
723 /* {{{ proto static string HttpResponse::getFile()
724 *
725 * Get the previously set file to be sent.
726 */
727 PHP_METHOD(HttpResponse, getFile)
728 {
729 NO_ARGS;
730
731 IF_RETVAL_USED {
732 zval *the_file = GET_STATIC_PROP(file);
733 RETURN_STRINGL(Z_STRVAL_P(the_file), Z_STRLEN_P(the_file), 1);
734 }
735 }
736 /* }}} */
737
738 /* {{{ proto static bool HttpResponse::send([bool clean_ob = true])
739 *
740 * Finally send the entity.
741 *
742 * Example:
743 * <pre>
744 * <?php
745 * HttpResponse::setCache(true);
746 * HttpResponse::setContentType('application/pdf');
747 * HttpResponse::setContentDisposition("$user.pdf", false);
748 * HttpResponse::setFile('sheet.pdf');
749 * HttpResponse::send();
750 * ?>
751 * </pre>
752 */
753 PHP_METHOD(HttpResponse, send)
754 {
755 zval *sent, *headers;
756 zend_bool clean_ob = 1;
757
758 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|b", &clean_ob)) {
759 RETURN_FALSE;
760 }
761 if (SG(headers_sent)) {
762 http_error(HE_WARNING, HTTP_E_RESPONSE, "Cannot send HttpResponse, headers have already been sent");
763 RETURN_FALSE;
764 }
765
766 sent = GET_STATIC_PROP(sent);
767 if (Z_LVAL_P(sent)) {
768 http_error(HE_WARNING, HTTP_E_RESPONSE, "Cannot send HttpResponse, response has already been sent");
769 RETURN_FALSE;
770 } else {
771 Z_LVAL_P(sent) = 1;
772 }
773
774 /* capture mode */
775 if (Z_BVAL_P(GET_STATIC_PROP(catch))) {
776 zval *the_data;
777
778 MAKE_STD_ZVAL(the_data);
779 php_ob_get_buffer(the_data TSRMLS_CC);
780
781 SET_STATIC_PROP(data, the_data);
782 ZVAL_LONG(GET_STATIC_PROP(mode), SEND_DATA);
783
784 if (!Z_STRLEN_P(GET_STATIC_PROP(eTag))) {
785 char *etag = http_etag(Z_STRVAL_P(the_data), Z_STRLEN_P(the_data), SEND_DATA);
786 UPD_STATIC_PROP(string, eTag, etag);
787 efree(etag);
788 }
789
790 clean_ob = 1;
791 }
792
793 if (clean_ob) {
794 /* interrupt on-the-fly etag generation */
795 HTTP_G(etag).started = 0;
796 /* discard previous output buffers */
797 php_end_ob_buffers(0 TSRMLS_CC);
798 }
799
800 /* custom headers */
801 headers = GET_STATIC_PROP(headers);
802 if (Z_TYPE_P(headers) == IS_ARRAY) {
803 char *name = NULL;
804 ulong idx = 0;
805 zval **value;
806
807 FOREACH_KEYVAL(headers, name, idx, value) {
808 if (name) {
809 if (Z_TYPE_PP(value) == IS_ARRAY) {
810 zend_bool first = 1;
811 zval **data;
812
813 FOREACH_VAL(*value, data) {
814 http_send_header_ex(name, strlen(name), Z_STRVAL_PP(data), Z_STRLEN_PP(data), first);
815 first = 0;
816 }
817 } else {
818 http_send_header_ex(name, strlen(name), Z_STRVAL_PP(value), Z_STRLEN_PP(value), 1);
819 }
820 name = NULL;
821 }
822 }
823 }
824
825 /* gzip */
826 if (Z_LVAL_P(GET_STATIC_PROP(gzip))) {
827 php_start_ob_buffer_named("ob_gzhandler", 0, 0 TSRMLS_CC);
828 } else {
829 php_start_ob_buffer(NULL, 0, 0 TSRMLS_CC);
830 }
831
832 /* caching */
833 if (Z_LVAL_P(GET_STATIC_PROP(cache))) {
834 zval *cctl, *etag, *lmod;
835
836 etag = GET_STATIC_PROP(eTag);
837 lmod = GET_STATIC_PROP(lastModified);
838 cctl = GET_STATIC_PROP(cacheControl);
839
840 http_cache_etag(Z_STRVAL_P(etag), Z_STRLEN_P(etag), Z_STRVAL_P(cctl), Z_STRLEN_P(cctl));
841 http_cache_last_modified(Z_LVAL_P(lmod), Z_LVAL_P(lmod) ? Z_LVAL_P(lmod) : time(NULL), Z_STRVAL_P(cctl), Z_STRLEN_P(cctl));
842 }
843
844 /* content type */
845 {
846 zval *ctype = GET_STATIC_PROP(contentType);
847 if (Z_STRLEN_P(ctype)) {
848 http_send_content_type(Z_STRVAL_P(ctype), Z_STRLEN_P(ctype));
849 } else {
850 char *ctypes = INI_STR("default_mimetype");
851 size_t ctlen = ctypes ? strlen(ctypes) : 0;
852
853 if (ctlen) {
854 http_send_content_type(ctypes, ctlen);
855 } else {
856 http_send_content_type("application/x-octetstream", lenof("application/x-octetstream"));
857 }
858 }
859 }
860
861 /* content disposition */
862 {
863 zval *cd = GET_STATIC_PROP(contentDisposition);
864 if (Z_STRLEN_P(cd)) {
865 http_send_header_ex("Content-Disposition", lenof("Content-Disposition"), Z_STRVAL_P(cd), Z_STRLEN_P(cd), 1);
866 }
867 }
868
869 /* throttling */
870 {
871 HTTP_G(send).buffer_size = Z_LVAL_P(GET_STATIC_PROP(bufferSize));
872 HTTP_G(send).throttle_delay = Z_DVAL_P(GET_STATIC_PROP(throttleDelay));
873 }
874
875 /* send */
876 {
877 switch (Z_LVAL_P(GET_STATIC_PROP(mode)))
878 {
879 case SEND_DATA:
880 {
881 zval *zdata = GET_STATIC_PROP(data);
882 RETURN_SUCCESS(http_send_data(Z_STRVAL_P(zdata), Z_STRLEN_P(zdata)));
883 }
884
885 case SEND_RSRC:
886 {
887 php_stream *the_real_stream;
888 zval *the_stream = GET_STATIC_PROP(stream);
889 the_stream->type = IS_RESOURCE;
890 php_stream_from_zval(the_real_stream, &the_stream);
891 RETURN_SUCCESS(http_send_stream(the_real_stream));
892 }
893
894 default:
895 {
896 RETURN_SUCCESS(http_send_file(Z_STRVAL_P(GET_STATIC_PROP(file))));
897 }
898 }
899 }
900 }
901 /* }}} */
902
903 /* {{{ proto static void HttpResponse::capture()
904 *
905 * Capture script output.
906 *
907 * Example:
908 * <pre>
909 * <?php
910 * HttpResponse::setCache(true);
911 * HttpResponse::capture();
912 * // script follows
913 * // note that you need to call
914 * HttpResponse::send();
915 * // at the end of the script unless
916 * // you use PHP-5.1 or greater
917 * ?>
918 * </pre>
919 */
920 PHP_METHOD(HttpResponse, capture)
921 {
922 zval *do_catch;
923
924 NO_ARGS;
925
926 MAKE_STD_ZVAL(do_catch);
927 ZVAL_LONG(do_catch, 1);
928
929 SET_STATIC_PROP(catch, do_catch);
930
931 php_end_ob_buffers(0 TSRMLS_CC);
932 php_start_ob_buffer(NULL, 0, 0 TSRMLS_CC);
933
934 #if (PHP_MAJOR_VERSION > 5) || (PHP_MINOR_VERSION > 0)
935 /* register shutdown function --
936 messing around with ob and headers only works in PHP-5.1 or greater */
937 {
938 zval func, retval, arg, *argp[1];
939
940 INIT_PZVAL(&arg);
941 INIT_PZVAL(&func);
942 INIT_PZVAL(&retval);
943 ZVAL_STRINGL(&func, "register_shutdown_function", lenof("register_shutdown_function"), 0);
944
945 array_init(&arg);
946 add_next_index_stringl(&arg, "HttpResponse", lenof("HttpResponse"), 1);
947 add_next_index_stringl(&arg, "send", lenof("send"), 1);
948 argp[0] = &arg;
949 call_user_function(EG(function_table), NULL, &func, &retval, 1, argp TSRMLS_CC);
950 zval_dtor(&arg);
951 }
952 #endif
953 }
954 /* }}} */
955
956 #endif /* ZEND_ENGINE_2 */
957
958 /*
959 * Local variables:
960 * tab-width: 4
961 * c-basic-offset: 4
962 * End:
963 * vim600: noet sw=4 ts=4 fdm=marker
964 * vim<600: noet sw=4 ts=4
965 */
966