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