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