f7cb216c5ba4bbf0f52a31544f7080caff8cc813
[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 USE_STATIC_PROP() USE_STATIC_PROP_EX(http_response_object_ce)
42 #define GET_STATIC_PROP(n) *GET_STATIC_PROP_EX(http_response_object_ce, n)
43 #define SET_STATIC_PROP(n, v) SET_STATIC_PROP_EX(http_response_object_ce, n, v)
44 #define SET_STATIC_PROP_STRING(n, s, d) SET_STATIC_PROP_STRING_EX(http_response_object_ce, n, s, d)
45 #define SET_STATIC_PROP_STRINGL(n, s, l, d) SET_STATIC_PROP_STRINGL_EX(http_response_object_ce, n, s, l, d)
46
47 #define HTTP_BEGIN_ARGS(method, req_args) HTTP_BEGIN_ARGS_EX(HttpResponse, method, 0, req_args)
48 #define HTTP_EMPTY_ARGS(method, ret_ref) HTTP_EMPTY_ARGS_EX(HttpResponse, method, ret_ref)
49 #define HTTP_RESPONSE_ME(method, visibility) PHP_ME(HttpResponse, method, HTTP_ARGS(HttpResponse, method), visibility|ZEND_ACC_STATIC)
50
51 HTTP_EMPTY_ARGS(getETag, 0);
52 HTTP_BEGIN_ARGS(setETag, 1)
53 HTTP_ARG_VAL(etag, 0)
54 HTTP_END_ARGS;
55
56 HTTP_EMPTY_ARGS(getCache, 0);
57 HTTP_BEGIN_ARGS(setCache, 1)
58 HTTP_ARG_VAL(cache, 0)
59 HTTP_END_ARGS;
60
61 HTTP_EMPTY_ARGS(getGzip, 0);
62 HTTP_BEGIN_ARGS(setGzip, 1)
63 HTTP_ARG_VAL(gzip, 0)
64 HTTP_END_ARGS;
65
66 HTTP_EMPTY_ARGS(getCacheControl, 0);
67 HTTP_BEGIN_ARGS(setCacheControl, 1)
68 HTTP_ARG_VAL(cache_control, 0)
69 HTTP_ARG_VAL(max_age, 0)
70 HTTP_END_ARGS;
71
72 HTTP_EMPTY_ARGS(getContentType, 0);
73 HTTP_BEGIN_ARGS(setContentType, 1)
74 HTTP_ARG_VAL(content_type, 0)
75 HTTP_END_ARGS;
76
77 HTTP_EMPTY_ARGS(getContentDisposition, 0);
78 HTTP_BEGIN_ARGS(setContentDisposition, 1)
79 HTTP_ARG_VAL(filename, 0)
80 HTTP_ARG_VAL(send_inline, 0)
81 HTTP_END_ARGS;
82
83 HTTP_EMPTY_ARGS(getThrottleDelay, 0);
84 HTTP_BEGIN_ARGS(setThrottleDelay, 1)
85 HTTP_ARG_VAL(seconds, 0)
86 HTTP_END_ARGS;
87
88 HTTP_EMPTY_ARGS(getBufferSize, 0);
89 HTTP_BEGIN_ARGS(setBufferSize, 1)
90 HTTP_ARG_VAL(bytes, 0)
91 HTTP_END_ARGS;
92
93 HTTP_EMPTY_ARGS(getData, 0);
94 HTTP_BEGIN_ARGS(setData, 1)
95 HTTP_ARG_VAL(data, 0)
96 HTTP_END_ARGS;
97
98 HTTP_EMPTY_ARGS(getStream, 0);
99 HTTP_BEGIN_ARGS(setStream, 1)
100 HTTP_ARG_VAL(stream, 0)
101 HTTP_END_ARGS;
102
103 HTTP_EMPTY_ARGS(getFile, 0);
104 HTTP_BEGIN_ARGS(setFile, 1)
105 HTTP_ARG_VAL(filepath, 0)
106 HTTP_END_ARGS;
107
108 HTTP_BEGIN_ARGS(send, 0)
109 HTTP_ARG_VAL(clean_ob, 0)
110 HTTP_END_ARGS;
111
112 HTTP_EMPTY_ARGS(capture, 0);
113
114 #define http_response_object_declare_default_properties() _http_response_object_declare_default_properties(TSRMLS_C)
115 static inline void _http_response_object_declare_default_properties(TSRMLS_D);
116
117 zend_class_entry *http_response_object_ce;
118 zend_function_entry http_response_object_fe[] = {
119
120 HTTP_RESPONSE_ME(setETag, ZEND_ACC_PUBLIC)
121 HTTP_RESPONSE_ME(getETag, ZEND_ACC_PUBLIC)
122
123 HTTP_RESPONSE_ME(setContentDisposition, ZEND_ACC_PUBLIC)
124 HTTP_RESPONSE_ME(getContentDisposition, ZEND_ACC_PUBLIC)
125
126 HTTP_RESPONSE_ME(setContentType, ZEND_ACC_PUBLIC)
127 HTTP_RESPONSE_ME(getContentType, ZEND_ACC_PUBLIC)
128
129 HTTP_RESPONSE_ME(setCache, ZEND_ACC_PUBLIC)
130 HTTP_RESPONSE_ME(getCache, ZEND_ACC_PUBLIC)
131
132 HTTP_RESPONSE_ME(setCacheControl, ZEND_ACC_PUBLIC)
133 HTTP_RESPONSE_ME(getCacheControl, ZEND_ACC_PUBLIC)
134
135 HTTP_RESPONSE_ME(setGzip, ZEND_ACC_PUBLIC)
136 HTTP_RESPONSE_ME(getGzip, ZEND_ACC_PUBLIC)
137
138 HTTP_RESPONSE_ME(setThrottleDelay, ZEND_ACC_PUBLIC)
139 HTTP_RESPONSE_ME(getThrottleDelay, ZEND_ACC_PUBLIC)
140
141 HTTP_RESPONSE_ME(setBufferSize, ZEND_ACC_PUBLIC)
142 HTTP_RESPONSE_ME(getBufferSize, ZEND_ACC_PUBLIC)
143
144 HTTP_RESPONSE_ME(setData, ZEND_ACC_PUBLIC)
145 HTTP_RESPONSE_ME(getData, ZEND_ACC_PUBLIC)
146
147 HTTP_RESPONSE_ME(setFile, ZEND_ACC_PUBLIC)
148 HTTP_RESPONSE_ME(getFile, ZEND_ACC_PUBLIC)
149
150 HTTP_RESPONSE_ME(setStream, ZEND_ACC_PUBLIC)
151 HTTP_RESPONSE_ME(getStream, ZEND_ACC_PUBLIC)
152
153 HTTP_RESPONSE_ME(send, ZEND_ACC_PUBLIC)
154 HTTP_RESPONSE_ME(capture, ZEND_ACC_PUBLIC)
155
156 {NULL, NULL, NULL}
157 };
158
159 void _http_response_object_init(INIT_FUNC_ARGS)
160 {
161 HTTP_REGISTER_CLASS(HttpResponse, http_response_object, NULL, 0);
162 http_response_object_declare_default_properties();
163 }
164
165 static inline void _http_response_object_declare_default_properties(TSRMLS_D)
166 {
167 zend_class_entry *ce = http_response_object_ce;
168
169 DCL_STATIC_PROP(PRIVATE, bool, sent, 0);
170 DCL_STATIC_PROP(PRIVATE, bool, catch, 0);
171 DCL_STATIC_PROP(PRIVATE, long, mode, -1);
172 DCL_STATIC_PROP(PROTECTED, bool, cache, 0);
173 DCL_STATIC_PROP(PROTECTED, bool, gzip, 0);
174 DCL_STATIC_PROP(PROTECTED, long, stream, 0);
175 DCL_STATIC_PROP_N(PROTECTED, file);
176 DCL_STATIC_PROP_N(PROTECTED, data);
177 DCL_STATIC_PROP_N(PROTECTED, eTag);
178 DCL_STATIC_PROP(PROTECTED, long, lastModified, 0);
179 DCL_STATIC_PROP_N(PROTECTED, cacheControl);
180 DCL_STATIC_PROP_N(PROTECTED, contentType);
181 DCL_STATIC_PROP_N(PROTECTED, contentDisposition);
182 DCL_STATIC_PROP(PROTECTED, long, bufferSize, HTTP_SENDBUF_SIZE);
183 DCL_STATIC_PROP(PROTECTED, double, throttleDelay, 0.0);
184
185 DCL_STATIC_PROP(PUBLIC, string, dummy, "EMPTY");
186 }
187
188 /* ### USERLAND ### */
189
190 /* {{{ proto static bool HttpResponse::setCache(bool cache)
191 *
192 * Whether it sould be attempted to cache the entitity.
193 * This will result in necessary caching headers and checks of clients
194 * "If-Modified-Since" and "If-None-Match" headers. If one of those headers
195 * matches a "304 Not Modified" status code will be issued.
196 *
197 * NOTE: If you're using sessions, be shure that you set session.cache_limiter
198 * to something more appropriate than "no-cache"!
199 */
200 PHP_METHOD(HttpResponse, setCache)
201 {
202 zend_bool do_cache = 0;
203
204 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "b", &do_cache)) {
205 RETURN_FALSE;
206 }
207
208 ZVAL_BOOL(GET_STATIC_PROP(cache), do_cache);
209 RETURN_TRUE;
210 }
211 /* }}} */
212
213 /* {{{ proto static bool HttpResponse::getCache()
214 *
215 * Get current caching setting.
216 */
217 PHP_METHOD(HttpResponse, getCache)
218 {
219 NO_ARGS;
220
221 IF_RETVAL_USED {
222 RETURN_BOOL(Z_LVAL_P(GET_STATIC_PROP(cache)));
223 }
224 }
225 /* }}}*/
226
227 /* {{{ proto static bool HttpResponse::setGzip(bool gzip)
228 *
229 * Enable on-thy-fly gzipping of the sent entity. NOT IMPLEMENTED YET.
230 */
231 PHP_METHOD(HttpResponse, setGzip)
232 {
233 zend_bool do_gzip = 0;
234
235 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "b", &do_gzip)) {
236 RETURN_FALSE;
237 }
238
239 ZVAL_BOOL(GET_STATIC_PROP(gzip), do_gzip);
240 RETURN_TRUE;
241 }
242 /* }}} */
243
244 /* {{{ proto static bool HttpResponse::getGzip()
245 *
246 * Get current gzipping setting.
247 */
248 PHP_METHOD(HttpResponse, getGzip)
249 {
250 NO_ARGS;
251
252 IF_RETVAL_USED {
253 RETURN_BOOL(Z_LVAL_P(GET_STATIC_PROP(gzip)));
254 }
255 }
256 /* }}} */
257
258 /* {{{ proto static bool HttpResponse::setCacheControl(string control[, long max_age = 0])
259 *
260 * Set a custom cache-control header, usually being "private" or "public";
261 * The max_age parameter controls how long the cache entry is valid on the client side.
262 */
263 PHP_METHOD(HttpResponse, setCacheControl)
264 {
265 char *ccontrol, *cctl;
266 int cc_len;
267 long max_age = 0;
268
269 #define HTTP_CACHECONTROL_TEMPLATE "%s, must-revalidate, max_age=%ld"
270
271 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|l", &ccontrol, &cc_len, &max_age)) {
272 RETURN_FALSE;
273 }
274
275 if (strcmp(ccontrol, "public") && strcmp(ccontrol, "private") && strcmp(ccontrol, "no-cache")) {
276 http_error_ex(E_WARNING, HTTP_E_PARAM, "Cache-Control '%s' doesn't match public, private or no-cache", ccontrol);
277 RETURN_FALSE;
278 } else {
279 USE_STATIC_PROP();
280 spprintf(&cctl, 0, HTTP_CACHECONTROL_TEMPLATE, ccontrol, max_age);
281 SET_STATIC_PROP_STRING(cacheControl, cctl, 0);
282 RETURN_TRUE;
283 }
284 }
285 /* }}} */
286
287 /* {{{ proto static string HttpResponse::getCacheControl()
288 *
289 * Get current Cache-Control header setting.
290 */
291 PHP_METHOD(HttpResponse, getCacheControl)
292 {
293 NO_ARGS;
294
295 IF_RETVAL_USED {
296 zval *ccontrol = GET_STATIC_PROP(cacheControl);
297 RETURN_STRINGL(Z_STRVAL_P(ccontrol), Z_STRLEN_P(ccontrol), 1);
298 }
299 }
300 /* }}} */
301
302 /* {{{ proto static bool HttpResponse::setContentType(string content_type)
303 *
304 * Set the content-type of the sent entity.
305 */
306 PHP_METHOD(HttpResponse, setContentType)
307 {
308 char *ctype;
309 int ctype_len;
310
311 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &ctype, &ctype_len)) {
312 RETURN_FALSE;
313 }
314
315 if (!strchr(ctype, '/')) {
316 http_error_ex(E_WARNING, HTTP_E_PARAM, "Content type '%s' doesn't seem to contain a primary and a secondary part", ctype);
317 RETURN_FALSE;
318 }
319
320 USE_STATIC_PROP();
321 SET_STATIC_PROP_STRINGL(contentType, ctype, ctype_len, 1);
322 RETURN_TRUE;
323 }
324 /* }}} */
325
326 /* {{{ proto static string HttpResponse::getContentType()
327 *
328 * Get current Content-Type header setting.
329 */
330 PHP_METHOD(HttpResponse, getContentType)
331 {
332 NO_ARGS;
333
334 IF_RETVAL_USED {
335 zval *ctype = GET_STATIC_PROP(contentType);
336 RETURN_STRINGL(Z_STRVAL_P(ctype), Z_STRLEN_P(ctype), 1);
337 }
338 }
339 /* }}} */
340
341 /* {{{ proto static bool HttpResponse::setContentDisposition(string filename[, bool inline = false])
342 *
343 * Set the Content-Disposition of the sent entity. This setting aims to suggest
344 * the receiveing user agent how to handle the sent entity; usually the client
345 * will show the user a "Save As..." popup.
346 */
347 PHP_METHOD(HttpResponse, setContentDisposition)
348 {
349 char *file, *cd;
350 int file_len;
351 zend_bool send_inline = 0;
352
353 #define HTTP_CONTENTDISPOSITION_TEMPLATE "%s; filename=\"%s\""
354
355 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|b", &file, &file_len, &send_inline)) {
356 RETURN_FALSE;
357 }
358
359 spprintf(&cd, 0, HTTP_CONTENTDISPOSITION_TEMPLATE, send_inline ? "inline" : "attachment", file);
360 SET_STATIC_PROP_STRING(contentDisposition, cd, 0);
361 RETURN_TRUE;
362 }
363 /* }}} */
364
365 /* {{{ proto static string HttpResponse::getContentDisposition()
366 *
367 * Get current Content-Disposition setting.
368 */
369 PHP_METHOD(HttpResponse, getContentDisposition)
370 {
371 NO_ARGS;
372
373 IF_RETVAL_USED {
374 zval *cd = GET_STATIC_PROP(contentDisposition);
375 RETURN_STRINGL(Z_STRVAL_P(cd), Z_STRLEN_P(cd), 1);
376 }
377 }
378 /* }}} */
379
380 /* {{{ proto static bool HttpResponse::setETag(string etag)
381 *
382 * Set a custom ETag. Use this only if you know what you're doing.
383 */
384 PHP_METHOD(HttpResponse, setETag)
385 {
386 char *etag;
387 int etag_len;
388
389 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &etag, &etag_len)) {
390 RETURN_FALSE;
391 }
392
393 USE_STATIC_PROP();
394 SET_STATIC_PROP_STRINGL(eTag, etag, etag_len, 1);
395 RETURN_TRUE;
396 }
397 /* }}} */
398
399 /* {{{ proto static string HttpResponse::getETag()
400 *
401 * Get the previously set custom ETag.
402 */
403 PHP_METHOD(HttpResponse, getETag)
404 {
405 NO_ARGS;
406
407 IF_RETVAL_USED {
408 zval *etag = GET_STATIC_PROP(eTag);
409 RETURN_STRINGL(Z_STRVAL_P(etag), Z_STRLEN_P(etag), 1);
410 }
411 }
412 /* }}} */
413
414 /* {{{ proto static void HttpResponse::setThrottleDelay(double seconds)
415 *
416 */
417 PHP_METHOD(HttpResponse, setThrottleDelay)
418 {
419 double seconds;
420
421 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "d", &seconds)) {
422 ZVAL_DOUBLE(GET_STATIC_PROP(throttleDelay), seconds);
423 }
424 }
425 /* }}} */
426
427 /* {{{ proto static double HttpResponse::getThrottleDelay()
428 *
429 */
430 PHP_METHOD(HttpResponse, getThrottleDelay)
431 {
432 NO_ARGS;
433
434 IF_RETVAL_USED {
435 RETURN_DOUBLE(Z_DVAL_P(GET_STATIC_PROP(throttleDelay)));
436 }
437 }
438 /* }}} */
439
440 /* {{{ proto static void HttpResponse::setBufferSize(long bytes)
441 *
442 */
443 PHP_METHOD(HttpResponse, setBufferSize)
444 {
445 long bytes;
446
447 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &bytes)) {
448 ZVAL_LONG(GET_STATIC_PROP(bufferSize), bytes);
449 }
450 }
451 /* }}} */
452
453 /* {{{ proto static long HttpResponse::getBufferSize()
454 *
455 */
456 PHP_METHOD(HttpResponse, getBufferSize)
457 {
458 NO_ARGS;
459
460 IF_RETVAL_USED {
461 RETURN_LONG(Z_LVAL_P(GET_STATIC_PROP(bufferSize)));
462 }
463 }
464 /* }}} */
465
466 /* {{{ proto static bool HttpResponse::setData(string data)
467 *
468 * Set the data to be sent.
469 */
470 PHP_METHOD(HttpResponse, setData)
471 {
472 zval *the_data;
473
474 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &the_data)) {
475 RETURN_FALSE;
476 }
477 convert_to_string_ex(&the_data);
478
479 USE_STATIC_PROP();
480 SET_STATIC_PROP(data, the_data);
481 ZVAL_LONG(GET_STATIC_PROP(lastModified), http_last_modified(the_data, SEND_DATA));
482 ZVAL_LONG(GET_STATIC_PROP(mode), SEND_DATA);
483 if (!Z_STRLEN_P(GET_STATIC_PROP(eTag))) {
484 SET_STATIC_PROP_STRING(eTag, http_etag(Z_STRVAL_P(the_data), Z_STRLEN_P(the_data), SEND_DATA), 0);
485 }
486
487 RETURN_TRUE;
488 }
489 /* }}} */
490
491 /* {{{ proto static string HttpResponse::getData()
492 *
493 * Get the previously set data to be sent.
494 */
495 PHP_METHOD(HttpResponse, getData)
496 {
497 NO_ARGS;
498
499 IF_RETVAL_USED {
500 zval *the_data = GET_STATIC_PROP(data);
501 RETURN_STRINGL(Z_STRVAL_P(the_data), Z_STRLEN_P(the_data), 1);
502 }
503 }
504 /* }}} */
505
506 /* {{{ proto static bool HttpResponse::setStream(resource stream)
507 *
508 * Set the resource to be sent.
509 */
510 PHP_METHOD(HttpResponse, setStream)
511 {
512 zval *the_stream;
513 php_stream *the_real_stream;
514
515 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &the_stream)) {
516 RETURN_FALSE;
517 }
518 zend_list_addref(Z_LVAL_P(the_stream));
519 php_stream_from_zval(the_real_stream, &the_stream);
520
521 USE_STATIC_PROP();
522 ZVAL_LONG(GET_STATIC_PROP(stream), Z_LVAL_P(the_stream));
523 ZVAL_LONG(GET_STATIC_PROP(lastModified), http_last_modified(the_real_stream, SEND_RSRC));
524 ZVAL_LONG(GET_STATIC_PROP(mode), SEND_RSRC);
525 if (!Z_STRLEN_P(GET_STATIC_PROP(eTag))) {
526 SET_STATIC_PROP_STRING(eTag, http_etag(the_real_stream, 0, SEND_RSRC), 0);
527 }
528
529 RETURN_TRUE;
530 }
531 /* }}} */
532
533 /* {{{ proto static resource HttpResponse::getStream()
534 *
535 * Get the previously set resource to be sent.
536 */
537 PHP_METHOD(HttpResponse, getStream)
538 {
539 NO_ARGS;
540
541 IF_RETVAL_USED {
542 RETURN_RESOURCE(Z_LVAL_P(GET_STATIC_PROP(stream)));
543 }
544 }
545 /* }}} */
546
547 /* {{{ proto static bool HttpResponse::setFile(string file)
548 *
549 * Set the file to be sent.
550 */
551 PHP_METHOD(HttpResponse, setFile)
552 {
553 zval *the_file;
554
555 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &the_file)) {
556 RETURN_FALSE;
557 }
558
559 convert_to_string_ex(&the_file);
560 USE_STATIC_PROP();
561 SET_STATIC_PROP(file, the_file);
562 ZVAL_LONG(GET_STATIC_PROP(lastModified), http_last_modified(the_file, -1));
563 ZVAL_LONG(GET_STATIC_PROP(mode), -1);
564 if (!Z_STRLEN_P(GET_STATIC_PROP(eTag))) {
565 SET_STATIC_PROP_STRING(eTag, http_etag(the_file, 0, -1), 0);
566 }
567
568 RETURN_TRUE;
569 }
570 /* }}} */
571
572 /* {{{ proto static string HttpResponse::getFile()
573 *
574 * Get the previously set file to be sent.
575 */
576 PHP_METHOD(HttpResponse, getFile)
577 {
578 NO_ARGS;
579
580 IF_RETVAL_USED {
581 zval *the_file = GET_STATIC_PROP(file);
582 RETURN_STRINGL(Z_STRVAL_P(the_file), Z_STRLEN_P(the_file), 1);
583 }
584 }
585 /* }}} */
586
587 /* {{{ proto static bool HttpResponse::send([bool clean_ob = true])
588 *
589 * Finally send the entity.
590 *
591 * Example:
592 * <pre>
593 * <?php
594 * HttpResponse::setCache(true);
595 * HttpResponse::setContentType('application/pdf');
596 * HttpResponse::setContentDisposition("$user.pdf", false);
597 * HttpResponse::setFile('sheet.pdf');
598 * HttpResponse::send();
599 * ?>
600 * </pre>
601 */
602 PHP_METHOD(HttpResponse, send)
603 {
604 zval *sent;
605 zend_bool clean_ob = 1;
606
607 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|b", &clean_ob)) {
608 RETURN_FALSE;
609 }
610 if (SG(headers_sent)) {
611 http_error(E_WARNING, HTTP_E_HEADER, "Cannot send HttpResponse, headers have already been sent");
612 RETURN_FALSE;
613 }
614
615 sent = GET_STATIC_PROP(sent);
616 if (Z_LVAL_P(sent)) {
617 http_error(E_WARNING, HTTP_E_UNKOWN, "Cannot send HttpResponse, response has already been sent");
618 RETURN_FALSE;
619 } else {
620 Z_LVAL_P(sent) = 1;
621 }
622
623 /* capture mode */
624 if (Z_LVAL_P(GET_STATIC_PROP(catch))) {
625 zval the_data;
626
627 INIT_PZVAL(&the_data);
628 php_ob_get_buffer(&the_data TSRMLS_CC);
629
630 USE_STATIC_PROP();
631 SET_STATIC_PROP(data, &the_data);
632 ZVAL_LONG(GET_STATIC_PROP(mode), SEND_DATA);
633
634 if (!Z_STRLEN_P(GET_STATIC_PROP(eTag))) {
635 SET_STATIC_PROP_STRING(eTag, http_etag(Z_STRVAL(the_data), Z_STRLEN(the_data), SEND_DATA), 0);
636 }
637 zval_dtor(&the_data);
638
639 clean_ob = 1;
640 }
641
642 if (clean_ob) {
643 /* interrupt on-the-fly etag generation */
644 HTTP_G(etag).started = 0;
645 /* discard previous output buffers */
646 php_end_ob_buffers(0 TSRMLS_CC);
647 }
648
649 /* gzip */
650 if (Z_LVAL_P(GET_STATIC_PROP(gzip))) {
651 php_start_ob_buffer_named("ob_gzhandler", 0, 1 TSRMLS_CC);
652 } else {
653 php_start_ob_buffer(NULL, 0, 0 TSRMLS_CC);
654 }
655
656 /* caching */
657 if (Z_LVAL_P(GET_STATIC_PROP(cache))) {
658 zval *cctl, *etag, *lmod;
659
660 etag = GET_STATIC_PROP(eTag);
661 lmod = GET_STATIC_PROP(lastModified);
662 cctl = GET_STATIC_PROP(cacheControl);
663
664 http_cache_etag(Z_STRVAL_P(etag), Z_STRLEN_P(etag), Z_STRVAL_P(cctl), Z_STRLEN_P(cctl));
665 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));
666 }
667
668 /* content type */
669 {
670 zval *ctype = GET_STATIC_PROP(contentType);
671 if (Z_STRLEN_P(ctype)) {
672 http_send_content_type(Z_STRVAL_P(ctype), Z_STRLEN_P(ctype));
673 } else {
674 char *ctypes = INI_STR("default_mimetype");
675 size_t ctlen = ctypes ? strlen(ctypes) : 0;
676
677 if (ctlen) {
678 http_send_content_type(ctypes, ctlen);
679 } else {
680 http_send_content_type("application/x-octetstream", lenof("application/x-octetstream"));
681 }
682 }
683 }
684
685 /* content disposition */
686 {
687 zval *cd = GET_STATIC_PROP(contentDisposition);
688 if (Z_STRLEN_P(cd)) {
689 char *cds;
690
691 spprintf(&cds, 0, "Content-Disposition: %s", Z_STRVAL_P(cd));
692 http_send_header(cds);
693 efree(cds);
694 }
695 }
696
697 /* throttling */
698 {
699 HTTP_G(send).buffer_size = Z_LVAL_P(GET_STATIC_PROP(bufferSize));
700 HTTP_G(send).throttle_delay = Z_DVAL_P(GET_STATIC_PROP(throttleDelay));
701 }
702
703 /* send */
704 {
705 switch (Z_LVAL_P(GET_STATIC_PROP(mode)))
706 {
707 case SEND_DATA:
708 {
709 zval *zdata = GET_STATIC_PROP(data);
710 RETURN_SUCCESS(http_send_data(Z_STRVAL_P(zdata), Z_STRLEN_P(zdata)));
711 }
712
713 case SEND_RSRC:
714 {
715 php_stream *the_real_stream;
716 zval *the_stream = GET_STATIC_PROP(stream);
717 the_stream->type = IS_RESOURCE;
718 php_stream_from_zval(the_real_stream, &the_stream);
719 RETURN_SUCCESS(http_send_stream(the_real_stream));
720 }
721
722 default:
723 {
724 RETURN_SUCCESS(http_send_file(Z_STRVAL_P(GET_STATIC_PROP(file))));
725 }
726 }
727 }
728 }
729 /* }}} */
730
731 /* {{{ proto static void HttpResponse::capture()
732 *
733 * Capture script output.
734 *
735 * Example:
736 * <pre>
737 * <?php
738 * HttpResponse::setCache(true);
739 * HttpResponse::capture();
740 * // script follows
741 * ?>
742 * </pre>
743 */
744 PHP_METHOD(HttpResponse, capture)
745 {
746 zval do_catch;
747
748 NO_ARGS;
749
750 INIT_PZVAL(&do_catch);
751 ZVAL_LONG(&do_catch, 1);
752 USE_STATIC_PROP();
753 SET_STATIC_PROP(catch, &do_catch);
754
755 php_end_ob_buffers(0 TSRMLS_CC);
756 php_start_ob_buffer(NULL, 0, 0 TSRMLS_CC);
757
758 /* register shutdown function */
759 {
760 zval func, retval, arg, *argp[1];
761
762 INIT_PZVAL(&arg);
763 INIT_PZVAL(&func);
764 INIT_PZVAL(&retval);
765 ZVAL_STRINGL(&func, "register_shutdown_function", lenof("register_shutdown_function"), 0);
766
767 array_init(&arg);
768 add_next_index_stringl(&arg, "HttpResponse", lenof("HttpResponse"), 1);
769 add_next_index_stringl(&arg, "send", lenof("send"), 1);
770 argp[0] = &arg;
771 call_user_function(EG(function_table), NULL, &func, &retval, 1, argp TSRMLS_CC);
772 zval_dtor(&arg);
773 }
774 }
775 /* }}} */
776
777 #endif /* ZEND_ENGINE_2 */
778
779 /*
780 * Local variables:
781 * tab-width: 4
782 * c-basic-offset: 4
783 * End:
784 * vim600: noet sw=4 ts=4 fdm=marker
785 * vim<600: noet sw=4 ts=4
786 */
787