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