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