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