- etag caching improvements
[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 #include "missing.h"
25
26 /* broken static properties in PHP 5.0 */
27 #if defined(ZEND_ENGINE_2) && !defined(WONKY)
28
29 #include "SAPI.h"
30 #include "php_ini.h"
31
32 #include "php_http.h"
33 #include "php_http_api.h"
34 #include "php_http_std_defs.h"
35 #include "php_http_response_object.h"
36 #include "php_http_exception_object.h"
37 #include "php_http_send_api.h"
38 #include "php_http_cache_api.h"
39 #include "php_http_headers_api.h"
40
41 #ifdef HTTP_HAVE_MHASH
42 # include <mhash.h>
43 #endif
44 #ifdef HTTP_HAVE_MAGIC
45 # include <magic.h>
46 #endif
47
48 ZEND_EXTERN_MODULE_GLOBALS(http);
49
50 #define GET_STATIC_PROP(n) *GET_STATIC_PROP_EX(http_response_object_ce, n)
51 #define UPD_STATIC_PROP(t, n, v) UPD_STATIC_PROP_EX(http_response_object_ce, t, n, v)
52 #define SET_STATIC_PROP(n, v) SET_STATIC_PROP_EX(http_response_object_ce, n, v)
53 #define UPD_STATIC_STRL(n, v, l) UPD_STATIC_STRL_EX(http_response_object_ce, n, v, l)
54
55 #define HTTP_BEGIN_ARGS(method, req_args) HTTP_BEGIN_ARGS_EX(HttpResponse, method, 0, req_args)
56 #define HTTP_EMPTY_ARGS(method, ret_ref) HTTP_EMPTY_ARGS_EX(HttpResponse, method, ret_ref)
57 #define HTTP_RESPONSE_ME(method, visibility) PHP_ME(HttpResponse, method, HTTP_ARGS(HttpResponse, method), visibility|ZEND_ACC_STATIC)
58 #define HTTP_RESPONSE_ALIAS(method, func) HTTP_STATIC_ME_ALIAS(method, func, HTTP_ARGS(HttpResponse, method))
59
60 HTTP_BEGIN_ARGS(setHeader, 2)
61 HTTP_ARG_VAL(name, 0)
62 HTTP_ARG_VAL(value, 0)
63 HTTP_ARG_VAL(replace, 0)
64 HTTP_END_ARGS;
65
66 HTTP_BEGIN_ARGS(getHeader, 0)
67 HTTP_ARG_VAL(name, 0)
68 HTTP_END_ARGS;
69
70 HTTP_EMPTY_ARGS(getETag, 0);
71 HTTP_BEGIN_ARGS(setETag, 1)
72 HTTP_ARG_VAL(etag, 0)
73 HTTP_END_ARGS;
74
75 HTTP_EMPTY_ARGS(getLastModified, 0);
76 HTTP_BEGIN_ARGS(setLastModified, 1)
77 HTTP_ARG_VAL(timestamp, 0)
78 HTTP_END_ARGS;
79
80 HTTP_EMPTY_ARGS(getCache, 0);
81 HTTP_BEGIN_ARGS(setCache, 1)
82 HTTP_ARG_VAL(cache, 0)
83 HTTP_END_ARGS;
84
85 HTTP_EMPTY_ARGS(getGzip, 0);
86 HTTP_BEGIN_ARGS(setGzip, 1)
87 HTTP_ARG_VAL(gzip, 0)
88 HTTP_END_ARGS;
89
90 HTTP_EMPTY_ARGS(getCacheControl, 0);
91 HTTP_BEGIN_ARGS(setCacheControl, 1)
92 HTTP_ARG_VAL(cache_control, 0)
93 HTTP_ARG_VAL(max_age, 0)
94 HTTP_END_ARGS;
95
96 HTTP_EMPTY_ARGS(getContentType, 0);
97 HTTP_BEGIN_ARGS(setContentType, 1)
98 HTTP_ARG_VAL(content_type, 0)
99 HTTP_END_ARGS;
100
101 HTTP_BEGIN_ARGS(guessContentType, 1)
102 HTTP_ARG_VAL(magic_file, 0)
103 HTTP_ARG_VAL(magic_mode, 0)
104 HTTP_END_ARGS;
105
106 HTTP_EMPTY_ARGS(getContentDisposition, 0);
107 HTTP_BEGIN_ARGS(setContentDisposition, 1)
108 HTTP_ARG_VAL(filename, 0)
109 HTTP_ARG_VAL(send_inline, 0)
110 HTTP_END_ARGS;
111
112 HTTP_EMPTY_ARGS(getThrottleDelay, 0);
113 HTTP_BEGIN_ARGS(setThrottleDelay, 1)
114 HTTP_ARG_VAL(seconds, 0)
115 HTTP_END_ARGS;
116
117 HTTP_EMPTY_ARGS(getBufferSize, 0);
118 HTTP_BEGIN_ARGS(setBufferSize, 1)
119 HTTP_ARG_VAL(bytes, 0)
120 HTTP_END_ARGS;
121
122 HTTP_EMPTY_ARGS(getData, 0);
123 HTTP_BEGIN_ARGS(setData, 1)
124 HTTP_ARG_VAL(data, 0)
125 HTTP_END_ARGS;
126
127 HTTP_EMPTY_ARGS(getStream, 0);
128 HTTP_BEGIN_ARGS(setStream, 1)
129 HTTP_ARG_VAL(stream, 0)
130 HTTP_END_ARGS;
131
132 HTTP_EMPTY_ARGS(getFile, 0);
133 HTTP_BEGIN_ARGS(setFile, 1)
134 HTTP_ARG_VAL(filepath, 0)
135 HTTP_END_ARGS;
136
137 HTTP_BEGIN_ARGS(send, 0)
138 HTTP_ARG_VAL(clean_ob, 0)
139 HTTP_END_ARGS;
140
141 HTTP_EMPTY_ARGS(capture, 0);
142
143 HTTP_BEGIN_ARGS(redirect, 0)
144 HTTP_ARG_VAL(url, 0)
145 HTTP_ARG_VAL(params, 0)
146 HTTP_ARG_VAL(session, 0)
147 HTTP_ARG_VAL(permanent, 0)
148 HTTP_END_ARGS;
149
150 HTTP_BEGIN_ARGS(status, 1)
151 HTTP_ARG_VAL(code, 0)
152 HTTP_END_ARGS;
153
154 HTTP_EMPTY_ARGS(getRequestHeaders, 0);
155 HTTP_EMPTY_ARGS(getRequestBody, 0);
156
157 #define http_response_object_declare_default_properties() _http_response_object_declare_default_properties(TSRMLS_C)
158 static inline void _http_response_object_declare_default_properties(TSRMLS_D);
159 #define http_grab_response_headers _http_grab_response_headers
160 static void _http_grab_response_headers(void *data, void *arg TSRMLS_DC);
161
162 zend_class_entry *http_response_object_ce;
163 zend_function_entry http_response_object_fe[] = {
164
165 HTTP_RESPONSE_ME(setHeader, ZEND_ACC_PUBLIC)
166 HTTP_RESPONSE_ME(getHeader, ZEND_ACC_PUBLIC)
167
168 HTTP_RESPONSE_ME(setETag, ZEND_ACC_PUBLIC)
169 HTTP_RESPONSE_ME(getETag, ZEND_ACC_PUBLIC)
170
171 HTTP_RESPONSE_ME(setLastModified, ZEND_ACC_PUBLIC)
172 HTTP_RESPONSE_ME(getLastModified, ZEND_ACC_PUBLIC)
173
174 HTTP_RESPONSE_ME(setContentDisposition, ZEND_ACC_PUBLIC)
175 HTTP_RESPONSE_ME(getContentDisposition, ZEND_ACC_PUBLIC)
176
177 HTTP_RESPONSE_ME(setContentType, ZEND_ACC_PUBLIC)
178 HTTP_RESPONSE_ME(getContentType, ZEND_ACC_PUBLIC)
179
180 HTTP_RESPONSE_ME(guessContentType, ZEND_ACC_PUBLIC)
181
182 HTTP_RESPONSE_ME(setCache, ZEND_ACC_PUBLIC)
183 HTTP_RESPONSE_ME(getCache, ZEND_ACC_PUBLIC)
184
185 HTTP_RESPONSE_ME(setCacheControl, ZEND_ACC_PUBLIC)
186 HTTP_RESPONSE_ME(getCacheControl, ZEND_ACC_PUBLIC)
187
188 HTTP_RESPONSE_ME(setGzip, ZEND_ACC_PUBLIC)
189 HTTP_RESPONSE_ME(getGzip, ZEND_ACC_PUBLIC)
190
191 HTTP_RESPONSE_ME(setThrottleDelay, ZEND_ACC_PUBLIC)
192 HTTP_RESPONSE_ME(getThrottleDelay, ZEND_ACC_PUBLIC)
193
194 HTTP_RESPONSE_ME(setBufferSize, ZEND_ACC_PUBLIC)
195 HTTP_RESPONSE_ME(getBufferSize, ZEND_ACC_PUBLIC)
196
197 HTTP_RESPONSE_ME(setData, ZEND_ACC_PUBLIC)
198 HTTP_RESPONSE_ME(getData, ZEND_ACC_PUBLIC)
199
200 HTTP_RESPONSE_ME(setFile, ZEND_ACC_PUBLIC)
201 HTTP_RESPONSE_ME(getFile, ZEND_ACC_PUBLIC)
202
203 HTTP_RESPONSE_ME(setStream, ZEND_ACC_PUBLIC)
204 HTTP_RESPONSE_ME(getStream, ZEND_ACC_PUBLIC)
205
206 HTTP_RESPONSE_ME(send, ZEND_ACC_PUBLIC)
207 HTTP_RESPONSE_ME(capture, ZEND_ACC_PUBLIC)
208
209 HTTP_RESPONSE_ALIAS(redirect, http_redirect)
210 HTTP_RESPONSE_ALIAS(status, http_send_status)
211 HTTP_RESPONSE_ALIAS(getRequestHeaders, http_get_request_headers)
212 HTTP_RESPONSE_ALIAS(getRequestBody, http_get_request_body)
213
214 EMPTY_FUNCTION_ENTRY
215 };
216
217 void _http_response_object_init(INIT_FUNC_ARGS)
218 {
219 HTTP_REGISTER_CLASS(HttpResponse, http_response_object, NULL, 0);
220 http_response_object_declare_default_properties();
221 }
222
223 static inline void _http_response_object_declare_default_properties(TSRMLS_D)
224 {
225 zend_class_entry *ce = http_response_object_ce;
226
227 DCL_STATIC_PROP(PRIVATE, bool, sent, 0);
228 DCL_STATIC_PROP(PRIVATE, bool, catch, 0);
229 DCL_STATIC_PROP(PRIVATE, long, mode, -1);
230 DCL_STATIC_PROP(PRIVATE, long, stream, 0);
231 DCL_STATIC_PROP_N(PRIVATE, file);
232 DCL_STATIC_PROP_N(PRIVATE, data);
233 DCL_STATIC_PROP(PROTECTED, bool, cache, 0);
234 DCL_STATIC_PROP(PROTECTED, bool, gzip, 0);
235 DCL_STATIC_PROP_N(PROTECTED, eTag);
236 DCL_STATIC_PROP(PROTECTED, long, lastModified, 0);
237 DCL_STATIC_PROP_N(PROTECTED, cacheControl);
238 DCL_STATIC_PROP_N(PROTECTED, contentType);
239 DCL_STATIC_PROP_N(PROTECTED, contentDisposition);
240 DCL_STATIC_PROP(PROTECTED, long, bufferSize, HTTP_SENDBUF_SIZE);
241 DCL_STATIC_PROP(PROTECTED, double, throttleDelay, 0.0);
242
243 #ifndef WONKY
244 DCL_CONST(long, "REDIRECT", HTTP_REDIRECT);
245 DCL_CONST(long, "REDIRECT_PERM", HTTP_REDIRECT_PERM);
246 DCL_CONST(long, "REDIRECT_POST", HTTP_REDIRECT_POST);
247 DCL_CONST(long, "REDIRECT_TEMP", HTTP_REDIRECT_TEMP);
248
249 DCL_CONST(long, "ETAG_MD5", HTTP_ETAG_MD5);
250 DCL_CONST(long, "ETAG_SHA1", HTTP_ETAG_SHA1);
251 DCL_CONST(long, "ETAG_CRC32", HTTP_ETAG_CRC32);
252
253 # ifdef HTTP_HAVE_MHASH
254 {
255 int l, i, c = mhash_count();
256
257 for (i = 0; i <= c; ++i) {
258 char const_name[256] = {0};
259 const char *hash_name = mhash_get_hash_name_static(i);
260
261 if (hash_name) {
262 l = snprintf(const_name, 255, "ETAG_MHASH_%s", hash_name);
263 zend_declare_class_constant_long(ce, const_name, l, i TSRMLS_CC);
264 }
265 }
266 }
267 # endif /* HTTP_HAVE_MHASH */
268 #endif /* WONKY */
269 }
270
271 static void _http_grab_response_headers(void *data, void *arg TSRMLS_DC)
272 {
273 phpstr_appendl(PHPSTR(arg), ((sapi_header_struct *)data)->header);
274 phpstr_appends(PHPSTR(arg), HTTP_CRLF);
275 }
276
277 /* ### USERLAND ### */
278
279 /* {{{ proto static bool HttpResponse::setHeader(string name, mixed value[, bool replace = true])
280 *
281 * Send an HTTP header.
282 *
283 * Expects a string parameter containing the name of the header and a mixed
284 * parameter containing the value of the header, which will be converted to
285 * a string. Additionally accepts an optional boolean parameter, which
286 * specifies whether an existing header should be replaced. If the second
287 * parameter is unset no header with this name will be sent.
288 *
289 * Returns TRUE on success, or FALSE on failure.
290 *
291 * Throws HttpHeaderException if http.only_exceptions is TRUE.
292 */
293 PHP_METHOD(HttpResponse, setHeader)
294 {
295 zend_bool replace = 1;
296 char *name;
297 int name_len = 0;
298 zval *value = NULL;
299
300 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sz/!|b", &name, &name_len, &value, &replace)) {
301 RETURN_FALSE;
302 }
303 if (SG(headers_sent)) {
304 http_error(HE_WARNING, HTTP_E_HEADER, "Cannot add another header when headers have already been sent");
305 RETURN_FALSE;
306 }
307 if (!name_len) {
308 http_error(HE_WARNING, HTTP_E_HEADER, "Cannot send anonymous headers");
309 RETURN_FALSE;
310 }
311
312 /* delete header if value == null */
313 if (!value || Z_TYPE_P(value) == IS_NULL) {
314 RETURN_SUCCESS(http_send_header_ex(name, name_len, "", 0, replace, NULL));
315 }
316 /* send multiple header if replace is false and value is an array */
317 if (!replace && Z_TYPE_P(value) == IS_ARRAY) {
318 zval **data;
319
320 FOREACH_VAL(value, data) {
321 convert_to_string_ex(data);
322 if (SUCCESS != http_send_header_ex(name, name_len, Z_STRVAL_PP(data), Z_STRLEN_PP(data), 0, NULL)) {
323 RETURN_FALSE;
324 }
325 }
326 RETURN_TRUE;
327 }
328 /* send standard header */
329 if (Z_TYPE_P(value) != IS_STRING) {
330 convert_to_string_ex(&value);
331 }
332 RETURN_SUCCESS(http_send_header_ex(name, name_len, Z_STRVAL_P(value), Z_STRLEN_P(value), replace, NULL));
333 }
334 /* }}} */
335
336 /* {{{ proto static mixed HttpResponse::getHeader([string name])
337 *
338 * Get header(s) about to be sent.
339 *
340 * Accepts a string as optional parameter which specifies the name of the
341 * header to read. If the parameter is empty or omitted, an associative array
342 * with all headers will be returned.
343 *
344 * Returns either a string containing the value of the header matching name,
345 * FALSE on failure, or an associative array with all headers.
346 */
347 PHP_METHOD(HttpResponse, getHeader)
348 {
349 char *name = NULL;
350 int name_len = 0;
351 phpstr headers;
352
353 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s", &name, &name_len)) {
354 RETURN_FALSE;
355 }
356
357 phpstr_init(&headers);
358 zend_llist_apply_with_argument(&SG(sapi_headers).headers, http_grab_response_headers, &headers TSRMLS_CC);
359 phpstr_fix(&headers);
360
361 if (name && name_len) {
362 zval **header;
363 HashTable headers_ht;
364
365 zend_hash_init(&headers_ht, sizeof(zval *), NULL, ZVAL_PTR_DTOR, 0);
366 if ( (SUCCESS == http_parse_headers_ex(PHPSTR_VAL(&headers), &headers_ht, 1)) &&
367 (SUCCESS == zend_hash_find(&headers_ht, name, name_len + 1, (void **) &header))) {
368 RETVAL_ZVAL(*header, 1, 0);
369 } else {
370 RETVAL_NULL();
371 }
372 zend_hash_destroy(&headers_ht);
373 } else {
374 array_init(return_value);
375 http_parse_headers_ex(PHPSTR_VAL(&headers), Z_ARRVAL_P(return_value), 1);
376 }
377
378 phpstr_dtor(&headers);
379 }
380 /* }}} */
381
382 /* {{{ proto static bool HttpResponse::setCache(bool cache)
383 *
384 * Whether it sould be attempted to cache the entitity.
385 * This will result in necessary caching headers and checks of clients
386 * "If-Modified-Since" and "If-None-Match" headers. If one of those headers
387 * matches a "304 Not Modified" status code will be issued.
388 *
389 * NOTE: If you're using sessions, be shure that you set session.cache_limiter
390 * to something more appropriate than "no-cache"!
391 *
392 * Expects a boolean as parameter specifying whether caching should be attempted.
393 *
394 * Returns TRUE ons success, or FALSE on failure.
395 */
396 PHP_METHOD(HttpResponse, setCache)
397 {
398 zend_bool do_cache = 0;
399
400 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "b", &do_cache)) {
401 RETURN_FALSE;
402 }
403
404 RETURN_SUCCESS(UPD_STATIC_PROP(bool, cache, do_cache));
405 }
406 /* }}} */
407
408 /* {{{ proto static bool HttpResponse::getCache()
409 *
410 * Get current caching setting.
411 *
412 * Returns TRUE if caching should be attempted, else FALSE.
413 */
414 PHP_METHOD(HttpResponse, getCache)
415 {
416 NO_ARGS;
417
418 IF_RETVAL_USED {
419 zval *cache = convert_to_type_ex(IS_BOOL, GET_STATIC_PROP(cache));
420
421 RETURN_ZVAL(cache, 1, 0);
422 }
423 }
424 /* }}}*/
425
426 /* {{{ proto static bool HttpResponse::setGzip(bool gzip)
427 *
428 * Enable on-thy-fly gzipping of the sent entity.
429 *
430 * Expects a boolean as parameter indicating if GZip compression should be enabled.
431 *
432 * Returns TRUE on success, or FALSE on failure.
433 */
434 PHP_METHOD(HttpResponse, setGzip)
435 {
436 zend_bool do_gzip = 0;
437
438 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "b", &do_gzip)) {
439 RETURN_FALSE;
440 }
441
442 RETURN_SUCCESS(UPD_STATIC_PROP(bool, gzip, do_gzip));
443 }
444 /* }}} */
445
446 /* {{{ proto static bool HttpResponse::getGzip()
447 *
448 * Get current gzipping setting.
449 *
450 * Returns TRUE if GZip compression is enabled, else FALSE.
451 */
452 PHP_METHOD(HttpResponse, getGzip)
453 {
454 NO_ARGS;
455
456 IF_RETVAL_USED {
457 zval *gzip = convert_to_type_ex(IS_BOOL, GET_STATIC_PROP(gzip));
458
459 RETURN_ZVAL(gzip, 1, 0);
460 }
461 }
462 /* }}} */
463
464 /* {{{ proto static bool HttpResponse::setCacheControl(string control[, int max_age = 0])
465 *
466 * Set a custom cache-control header, usually being "private" or "public";
467 * The max_age parameter controls how long the cache entry is valid on the client side.
468 *
469 * Expects a string parameter containing the primary cache control setting.
470 * Addtitionally accepts an int parameter specifying the max-age setting.
471 *
472 * Returns TRUE on success, or FALSE if control does not match one of
473 * "public" , "private" or "no-cache".
474 *
475 * Throws HttpInvalidParamException if http.only_exceptions is TRUE.
476 */
477 PHP_METHOD(HttpResponse, setCacheControl)
478 {
479 char *ccontrol, *cctl;
480 int cc_len;
481 long max_age = 0;
482
483 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|l", &ccontrol, &cc_len, &max_age)) {
484 RETURN_FALSE;
485 }
486
487 if (strcmp(ccontrol, "public") && strcmp(ccontrol, "private") && strcmp(ccontrol, "no-cache")) {
488 http_error_ex(HE_WARNING, HTTP_E_INVALID_PARAM, "Cache-Control '%s' doesn't match public, private or no-cache", ccontrol);
489 RETURN_FALSE;
490 } else {
491 size_t cctl_len = spprintf(&cctl, 0, "%s, must-revalidate, max_age=%ld", ccontrol, max_age);
492 RETVAL_SUCCESS(UPD_STATIC_STRL(cacheControl, cctl, cctl_len));
493 efree(cctl);
494 }
495 }
496 /* }}} */
497
498 /* {{{ proto static string HttpResponse::getCacheControl()
499 *
500 * Get current Cache-Control header setting.
501 *
502 * Returns the current cache control setting as a string like sent in a header.
503 */
504 PHP_METHOD(HttpResponse, getCacheControl)
505 {
506 NO_ARGS;
507
508 IF_RETVAL_USED {
509 zval *ccontrol = convert_to_type_ex(IS_STRING, GET_STATIC_PROP(cacheControl));
510
511 RETURN_ZVAL(ccontrol, 1, 0);
512 }
513 }
514 /* }}} */
515
516 /* {{{ proto static bool HttpResponse::setContentType(string content_type)
517 *
518 * Set the content-type of the sent entity.
519 *
520 * Expects a string as parameter specifying the content type of the sent entity.
521 *
522 * Returns TRUE on success, or FALSE if the content type does not seem to
523 * contain a primary and secondary content type part.
524 *
525 * Throws HttpInvalidParamException if http.only_exceptions is TRUE.
526 */
527 PHP_METHOD(HttpResponse, setContentType)
528 {
529 char *ctype;
530 int ctype_len;
531
532 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &ctype, &ctype_len)) {
533 RETURN_FALSE;
534 }
535
536 if (!strchr(ctype, '/')) {
537 http_error_ex(HE_WARNING, HTTP_E_INVALID_PARAM, "Content type '%s' doesn't seem to contain a primary and a secondary part", ctype);
538 RETURN_FALSE;
539 }
540
541 RETURN_SUCCESS(UPD_STATIC_STRL(contentType, ctype, ctype_len));
542 }
543 /* }}} */
544
545 /* {{{ proto static string HttpResponse::getContentType()
546 *
547 * Get current Content-Type header setting.
548 *
549 * Returns the currently set content type as string.
550 */
551 PHP_METHOD(HttpResponse, getContentType)
552 {
553 NO_ARGS;
554
555 IF_RETVAL_USED {
556 zval *ctype = convert_to_type_ex(IS_STRING, GET_STATIC_PROP(contentType));
557
558 RETURN_ZVAL(ctype, 1, 0);
559 }
560 }
561 /* }}} */
562
563 /* {{{ proto static string HttpResponse::guessContentType(string magic_file[, int magic_mode = MAGIC_MIME])
564 *
565 * Attempts to guess the content type of supplied payload through libmagic.
566 * If the attempt is successful, the guessed content type will automatically
567 * be set as response content type.
568 *
569 * Expects a string parameter specifying the magic.mime database to use.
570 * Additionally accepts an optional int parameter, being flags for libmagic.
571 *
572 * Returns the guessed content type on success, or FALSE on failure.
573 *
574 * Throws HttpRuntimeException, HttpInvalidParamException
575 * if http.only_exceptions is TRUE.
576 */
577 PHP_METHOD(HttpResponse, guessContentType)
578 {
579 char *magic_file, *ct = NULL;
580 int magic_file_len;
581 long magic_mode = 0;
582
583 RETVAL_FALSE;
584
585 #ifdef HTTP_HAVE_MAGIC
586 magic_mode = MAGIC_MIME;
587
588 SET_EH_THROW_HTTP();
589 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|l", &magic_file, &magic_file_len, &magic_mode)) {
590 switch (Z_LVAL_P(GET_STATIC_PROP(mode))) {
591 case SEND_DATA:
592 {
593 zval *data = GET_STATIC_PROP(data);
594 ct = http_guess_content_type(magic_file, magic_mode, Z_STRVAL_P(data), Z_STRLEN_P(data), SEND_DATA);
595 }
596 break;
597
598 case SEND_RSRC:
599 {
600 php_stream *s;
601 zval *z = GET_STATIC_PROP(stream);
602 z->type = IS_RESOURCE;
603 php_stream_from_zval(s, &z);
604 ct = http_guess_content_type(magic_file, magic_mode, s, 0, SEND_RSRC);
605 }
606 break;
607
608 default:
609 ct = http_guess_content_type(magic_file, magic_mode, Z_STRVAL_P(GET_STATIC_PROP(file)), 0, -1);
610 break;
611 }
612 if (ct) {
613 UPD_STATIC_PROP(string, contentType, ct);
614 RETVAL_STRING(ct, 0);
615 }
616 }
617 SET_EH_NORMAL();
618 #else
619 http_error(HE_THROW, HTTP_E_RUNTIME, "Cannot guess Content-Type; libmagic not available");
620 #endif
621 }
622 /* }}} */
623
624 /* {{{ proto static bool HttpResponse::setContentDisposition(string filename[, bool inline = false])
625 *
626 * Set the Content-Disposition. The Content-Disposition header is very useful
627 * if the data actually sent came from a file or something similar, that should
628 * be "saved" by the client/user (i.e. by browsers "Save as..." popup window).
629 *
630 * Expects a string parameter specifying the file name the "Save as..." dialogue
631 * should display. Optionally accepts a bool parameter, which, if set to true
632 * and the user agent knows how to handle the content type, will probably not
633 * cause the popup window to be shown.
634 *
635 * Returns TRUE on success or FALSE on failure.
636 */
637 PHP_METHOD(HttpResponse, setContentDisposition)
638 {
639 char *file, *cd;
640 int file_len;
641 size_t cd_len;
642 zend_bool send_inline = 0;
643
644 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|b", &file, &file_len, &send_inline)) {
645 RETURN_FALSE;
646 }
647
648 cd_len = spprintf(&cd, 0, "%s; filename=\"%s\"", send_inline ? "inline" : "attachment", file);
649 RETVAL_SUCCESS(UPD_STATIC_STRL(contentDisposition, cd, cd_len));
650 efree(cd);
651 }
652 /* }}} */
653
654 /* {{{ proto static string HttpResponse::getContentDisposition()
655 *
656 * Get current Content-Disposition setting.
657 *
658 * Returns the current content disposition as string like sent in a header.
659 */
660 PHP_METHOD(HttpResponse, getContentDisposition)
661 {
662 NO_ARGS;
663
664 IF_RETVAL_USED {
665 zval *cd = convert_to_type_ex(IS_STRING, GET_STATIC_PROP(contentDisposition));
666
667 RETURN_ZVAL(cd, 1, 0);
668 }
669 }
670 /* }}} */
671
672 /* {{{ proto static bool HttpResponse::setETag(string etag)
673 *
674 * Set a custom ETag. Use this only if you know what you're doing.
675 *
676 * Expects an unquoted string as parameter containing the ETag.
677 *
678 * Returns TRUE on success, or FALSE on failure.
679 */
680 PHP_METHOD(HttpResponse, setETag)
681 {
682 char *etag;
683 int etag_len;
684
685 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &etag, &etag_len)) {
686 RETURN_FALSE;
687 }
688
689 RETURN_SUCCESS(UPD_STATIC_STRL(eTag, etag, etag_len));
690 }
691 /* }}} */
692
693 /* {{{ proto static string HttpResponse::getETag()
694 *
695 * Get calculated or previously set custom ETag.
696 *
697 * Returns the calculated or previously set ETag as unquoted string.
698 */
699 PHP_METHOD(HttpResponse, getETag)
700 {
701 NO_ARGS;
702
703 IF_RETVAL_USED {
704 zval *etag = convert_to_type_ex(IS_STRING, GET_STATIC_PROP(eTag));
705
706 RETURN_ZVAL(etag, 1, 0);
707 }
708 }
709 /* }}} */
710
711 /* {{{ proto static bool HttpResponse::setLastModified(int timestamp)
712 *
713 * Set a custom Last-Modified date.
714 *
715 * Expects an unix timestamp as parameter representing the last modification
716 * time of the sent entity.
717 *
718 * Returns TRUE on success, or FALSE on failure.
719 */
720 PHP_METHOD(HttpResponse, setLastModified)
721 {
722 long lm;
723
724 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &lm)) {
725 RETURN_FALSE;
726 }
727
728 RETURN_SUCCESS(UPD_STATIC_PROP(long, lastModified, lm));
729 }
730 /* }}} */
731
732 /* {{{ proto static int HttpResponse::getLastModified()
733 *
734 * Get calculated or previously set custom Last-Modified date.
735 *
736 * Returns the calculated or previously set unix timestamp.
737 */
738 PHP_METHOD(HttpResponse, getLastModified)
739 {
740 NO_ARGS;
741
742 IF_RETVAL_USED {
743 zval *lm = convert_to_type_ex(IS_LONG, GET_STATIC_PROP(lastModified));
744
745 RETURN_ZVAL(lm, 1, 0);
746 }
747 }
748 /* }}} */
749
750 /* {{{ proto static bool HttpResponse::setThrottleDelay(double seconds)
751 *
752 * Sets the throttle delay for use with HttpResponse::setBufferSize().
753 *
754 * Provides a basic throttling mechanism, which will yield the current process
755 * resp. thread until the entity has been completely sent, though.
756 *
757 * Note: This doesn't really work with the FastCGI SAPI.
758 *
759 * Expects a double parameter specifying the seconds too sleep() after
760 * each chunk sent.
761 *
762 * Returns TRUE on success, or FALSE on failure.
763 */
764 PHP_METHOD(HttpResponse, setThrottleDelay)
765 {
766 double seconds;
767
768 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "d", &seconds)) {
769 RETURN_FALSE;
770 }
771 RETURN_SUCCESS(UPD_STATIC_PROP(double, throttleDelay, seconds));
772 }
773 /* }}} */
774
775 /* {{{ proto static double HttpResponse::getThrottleDelay()
776 *
777 * Get the current throttle delay.
778 *
779 * Returns a double representing the throttle delay in seconds.
780 */
781 PHP_METHOD(HttpResponse, getThrottleDelay)
782 {
783 NO_ARGS;
784
785 IF_RETVAL_USED {
786 zval *delay = convert_to_type_ex(IS_DOUBLE, GET_STATIC_PROP(throttleDelay));
787
788 RETURN_ZVAL(delay, 1, 0);
789 }
790 }
791 /* }}} */
792
793 /* {{{ proto static bool HttpResponse::setBufferSize(int bytes)
794 *
795 * Sets the send buffer size for use with HttpResponse::setThrottleDelay().
796 *
797 * Provides a basic throttling mechanism, which will yield the current process
798 * resp. thread until the entity has been completely sent, though.
799 *
800 * Note: This doesn't really work with the FastCGI SAPI.
801 *
802 * Expects an int parameter representing the chunk size in bytes.
803 *
804 * Returns TRUE on success, or FALSE on failure.
805 */
806 PHP_METHOD(HttpResponse, setBufferSize)
807 {
808 long bytes;
809
810 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &bytes)) {
811 RETURN_FALSE;
812 }
813 RETURN_SUCCESS(UPD_STATIC_PROP(long, bufferSize, bytes));
814 }
815 /* }}} */
816
817 /* {{{ proto static int HttpResponse::getBufferSize()
818 *
819 * Get current buffer size.
820 *
821 * Returns an int representing the current buffer size in bytes.
822 */
823 PHP_METHOD(HttpResponse, getBufferSize)
824 {
825 NO_ARGS;
826
827 IF_RETVAL_USED {
828 zval *size = convert_to_type_ex(IS_LONG, GET_STATIC_PROP(bufferSize));
829
830 RETURN_ZVAL(size, 1, 0);
831 }
832 }
833 /* }}} */
834
835 /* {{{ proto static bool HttpResponse::setData(mixed data)
836 *
837 * Set the data to be sent.
838 *
839 * Expects one parameter, which will be converted to a string and contains
840 * the data to send.
841 *
842 * Returns TRUE on success, or FALSE on failure.
843 */
844 PHP_METHOD(HttpResponse, setData)
845 {
846 zval *the_data;
847
848 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &the_data)) {
849 RETURN_FALSE;
850 }
851 if (Z_TYPE_P(the_data) != IS_STRING) {
852 convert_to_string_ex(&the_data);
853 }
854
855 if ( (SUCCESS != SET_STATIC_PROP(data, the_data)) ||
856 (SUCCESS != UPD_STATIC_PROP(long, mode, SEND_DATA))) {
857 RETURN_FALSE;
858 }
859
860 if (!(Z_LVAL_P(convert_to_type_ex(IS_LONG, GET_STATIC_PROP(lastModified))) > 0)) {
861 UPD_STATIC_PROP(long, lastModified, http_last_modified(the_data, SEND_DATA));
862 }
863 if (!Z_STRLEN_P(convert_to_type_ex(IS_STRING, GET_STATIC_PROP(eTag)))) {
864 char *etag = http_etag(Z_STRVAL_P(the_data), Z_STRLEN_P(the_data), SEND_DATA);
865 if (etag) {
866 UPD_STATIC_PROP(string, eTag, etag);
867 efree(etag);
868 }
869 }
870
871 RETURN_TRUE;
872 }
873 /* }}} */
874
875 /* {{{ proto static string HttpResponse::getData()
876 *
877 * Get the previously set data to be sent.
878 *
879 * Returns a string containing the previously set data to send.
880 */
881 PHP_METHOD(HttpResponse, getData)
882 {
883 NO_ARGS;
884
885 IF_RETVAL_USED {
886 zval *the_data = GET_STATIC_PROP(data);
887
888 RETURN_ZVAL(the_data, 1, 0);
889 }
890 }
891 /* }}} */
892
893 /* {{{ proto static bool HttpResponse::setStream(resource stream)
894 *
895 * Set the resource to be sent.
896 *
897 * Expects a resource parameter referencing an already opened stream from
898 * which the data to send will be read.
899 *
900 * Returns TRUE on success, or FALSE on failure.
901 */
902 PHP_METHOD(HttpResponse, setStream)
903 {
904 zval *the_stream;
905 php_stream *the_real_stream;
906 php_stream_statbuf ssb;
907
908 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &the_stream)) {
909 RETURN_FALSE;
910 }
911
912 php_stream_from_zval(the_real_stream, &the_stream);
913 if (php_stream_stat(the_real_stream, &ssb)) {
914 RETURN_FALSE;
915 }
916
917 if ( (SUCCESS != UPD_STATIC_PROP(long, stream, Z_LVAL_P(the_stream))) ||
918 (SUCCESS != UPD_STATIC_PROP(long, mode, SEND_RSRC))) {
919 RETURN_FALSE;
920 }
921 zend_list_addref(Z_LVAL_P(the_stream));
922
923 if (!(Z_LVAL_P(convert_to_type_ex(IS_LONG, GET_STATIC_PROP(lastModified))) > 0)) {
924 UPD_STATIC_PROP(long, lastModified, http_last_modified(the_real_stream, SEND_RSRC));
925 }
926 if (!Z_STRLEN_P(convert_to_type_ex(IS_STRING, GET_STATIC_PROP(eTag)))) {
927 char *etag = http_etag(the_real_stream, 0, SEND_RSRC);
928 if (etag) {
929 UPD_STATIC_PROP(string, eTag, etag);
930 efree(etag);
931 }
932 }
933
934 RETURN_TRUE;
935 }
936 /* }}} */
937
938 /* {{{ proto static resource HttpResponse::getStream()
939 *
940 * Get the previously set resource to be sent.
941 *
942 * Returns the previously set resource.
943 */
944 PHP_METHOD(HttpResponse, getStream)
945 {
946 NO_ARGS;
947
948 IF_RETVAL_USED {
949 RETURN_RESOURCE(Z_LVAL_P(convert_to_type_ex(IS_LONG, GET_STATIC_PROP(stream))));
950 }
951 }
952 /* }}} */
953
954 /* {{{ proto static bool HttpResponse::setFile(string file)
955 *
956 * Set the file to be sent.
957 *
958 * Expects a string as parameter, specifying the path to the file to send.
959 *
960 * Returns TRUE on success, or FALSE on failure.
961 */
962 PHP_METHOD(HttpResponse, setFile)
963 {
964 char *the_file;
965 int file_len;
966 php_stream_statbuf ssb;
967
968 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &the_file, &file_len)) {
969 RETURN_FALSE;
970 }
971
972 if (php_stream_stat_path(the_file, &ssb)) {
973 RETURN_FALSE;
974 }
975
976 if ( (SUCCESS != UPD_STATIC_STRL(file, the_file, file_len)) ||
977 (SUCCESS != UPD_STATIC_PROP(long, mode, -1))) {
978 RETURN_FALSE;
979 }
980
981 if (!(Z_LVAL_P(convert_to_type_ex(IS_LONG, GET_STATIC_PROP(lastModified))) > 0)) {
982 UPD_STATIC_PROP(long, lastModified, http_last_modified(the_file, -1));
983 }
984 if (!Z_STRLEN_P(convert_to_type_ex(IS_STRING, GET_STATIC_PROP(eTag)))) {
985 char *etag = http_etag(the_file, 0, -1);
986 if (etag) {
987 UPD_STATIC_PROP(string, eTag, etag);
988 efree(etag);
989 }
990 }
991
992 RETURN_TRUE;
993 }
994 /* }}} */
995
996 /* {{{ proto static string HttpResponse::getFile()
997 *
998 * Get the previously set file to be sent.
999 *
1000 * Returns the previously set path to the file to send as string.
1001 */
1002 PHP_METHOD(HttpResponse, getFile)
1003 {
1004 NO_ARGS;
1005
1006 IF_RETVAL_USED {
1007 zval *the_file = convert_to_type_ex(IS_STRING, GET_STATIC_PROP(file));
1008
1009 RETURN_ZVAL(the_file, 1, 0);
1010 }
1011 }
1012 /* }}} */
1013
1014 /* {{{ proto static bool HttpResponse::send([bool clean_ob = true])
1015 *
1016 * Finally send the entity.
1017 *
1018 * Accepts an optional boolean parameter, specifying wheter the ouput
1019 * buffers should be discarded prior sending. A successful caching attempt
1020 * will cause a script termination, and write a log entry if the INI setting
1021 * http.cache_log is set.
1022 *
1023 * Returns TRUE on success, or FALSE on failure.
1024 *
1025 * Throws HttpHeaderException, HttpResponseException if http.onyl_excpetions is TRUE.
1026 *
1027 * Example:
1028 * <pre>
1029 * <?php
1030 * HttpResponse::setCache(true);
1031 * HttpResponse::setContentType('application/pdf');
1032 * HttpResponse::setContentDisposition("$user.pdf", false);
1033 * HttpResponse::setFile('sheet.pdf');
1034 * HttpResponse::send();
1035 * ?>
1036 * </pre>
1037 */
1038 PHP_METHOD(HttpResponse, send)
1039 {
1040 zval *sent;
1041 zend_bool clean_ob = 1;
1042
1043 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|b", &clean_ob)) {
1044 RETURN_FALSE;
1045 }
1046 if (SG(headers_sent)) {
1047 http_error(HE_WARNING, HTTP_E_RESPONSE, "Cannot send HttpResponse, headers have already been sent");
1048 RETURN_FALSE;
1049 }
1050
1051 sent = GET_STATIC_PROP(sent);
1052 if (zval_is_true(sent)) {
1053 http_error(HE_WARNING, HTTP_E_RESPONSE, "Cannot send HttpResponse, response has already been sent");
1054 RETURN_FALSE;
1055 } else {
1056 Z_LVAL_P(sent) = 1;
1057 }
1058
1059 /* capture mode */
1060 if (zval_is_true(GET_STATIC_PROP(catch))) {
1061 zval *the_data;
1062
1063 MAKE_STD_ZVAL(the_data);
1064 php_ob_get_buffer(the_data TSRMLS_CC);
1065 SET_STATIC_PROP(data, the_data);
1066 ZVAL_LONG(GET_STATIC_PROP(mode), SEND_DATA);
1067
1068 if (!Z_STRLEN_P(convert_to_type_ex(IS_STRING, GET_STATIC_PROP(eTag)))) {
1069 char *etag = http_etag(Z_STRVAL_P(the_data), Z_STRLEN_P(the_data), SEND_DATA);
1070 if (etag) {
1071 UPD_STATIC_PROP(string, eTag, etag);
1072 efree(etag);
1073 }
1074 }
1075 zval_ptr_dtor(&the_data);
1076
1077 clean_ob = 1;
1078 }
1079
1080 if (clean_ob) {
1081 /* interrupt on-the-fly etag generation */
1082 HTTP_G(etag).started = 0;
1083 /* discard previous output buffers */
1084 php_end_ob_buffers(0 TSRMLS_CC);
1085 }
1086
1087 /* caching */
1088 if (zval_is_true(GET_STATIC_PROP(cache))) {
1089 zval *cctl, *etag, *lmod;
1090
1091 etag = convert_to_type_ex(IS_STRING, GET_STATIC_PROP(eTag));
1092 lmod = convert_to_type_ex(IS_LONG, GET_STATIC_PROP(lastModified));
1093 cctl = convert_to_type_ex(IS_STRING, GET_STATIC_PROP(cacheControl));
1094
1095 http_cache_etag(Z_STRVAL_P(etag), Z_STRLEN_P(etag), Z_STRVAL_P(cctl), Z_STRLEN_P(cctl));
1096 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));
1097 }
1098
1099 /* content type */
1100 {
1101 zval *ctype = convert_to_type_ex(IS_STRING, GET_STATIC_PROP(contentType));
1102 if (Z_STRLEN_P(ctype)) {
1103 http_send_content_type(Z_STRVAL_P(ctype), Z_STRLEN_P(ctype));
1104 } else {
1105 char *ctypes = INI_STR("default_mimetype");
1106 size_t ctlen = ctypes ? strlen(ctypes) : 0;
1107
1108 if (ctlen) {
1109 http_send_content_type(ctypes, ctlen);
1110 } else {
1111 http_send_content_type("application/x-octetstream", lenof("application/x-octetstream"));
1112 }
1113 }
1114 }
1115
1116 /* content disposition */
1117 {
1118 zval *cd = GET_STATIC_PROP(contentDisposition);
1119 if (Z_STRLEN_P(cd)) {
1120 http_send_header_ex("Content-Disposition", lenof("Content-Disposition"), Z_STRVAL_P(cd), Z_STRLEN_P(cd), 1, NULL);
1121 }
1122 }
1123
1124 /* throttling */
1125 {
1126 HTTP_G(send).buffer_size = Z_LVAL_P(convert_to_type_ex(IS_LONG, GET_STATIC_PROP(bufferSize)));
1127 HTTP_G(send).throttle_delay = Z_DVAL_P(convert_to_type_ex(IS_DOUBLE, GET_STATIC_PROP(throttleDelay)));
1128 }
1129
1130 /* gzip */
1131 if (zval_is_true(GET_STATIC_PROP(gzip))) {
1132 php_start_ob_buffer_named("ob_gzhandler", HTTP_G(send).buffer_size, 0 TSRMLS_CC);
1133 } else {
1134 php_start_ob_buffer(NULL, HTTP_G(send).buffer_size, 0 TSRMLS_CC);
1135 }
1136
1137 /* send */
1138 switch (Z_LVAL_P(GET_STATIC_PROP(mode)))
1139 {
1140 case SEND_DATA:
1141 {
1142 zval *zdata = convert_to_type_ex(IS_STRING, GET_STATIC_PROP(data));
1143 RETURN_SUCCESS(http_send_data_ex(Z_STRVAL_P(zdata), Z_STRLEN_P(zdata), 1));
1144 }
1145
1146 case SEND_RSRC:
1147 {
1148 php_stream *the_real_stream;
1149 zval *the_stream = convert_to_type_ex(IS_LONG, GET_STATIC_PROP(stream));
1150 the_stream->type = IS_RESOURCE;
1151 php_stream_from_zval(the_real_stream, &the_stream);
1152 RETURN_SUCCESS(http_send_stream_ex(the_real_stream, 0, 1));
1153 }
1154
1155 default:
1156 {
1157 RETURN_SUCCESS(http_send_file_ex(Z_STRVAL_P(convert_to_type_ex(IS_STRING, GET_STATIC_PROP(file))), 1));
1158 }
1159 }
1160 }
1161 /* }}} */
1162
1163 /* {{{ proto static void HttpResponse::capture()
1164 *
1165 * Capture script output.
1166 *
1167 * Example:
1168 * <pre>
1169 * <?php
1170 * HttpResponse::setCache(true);
1171 * HttpResponse::capture();
1172 * // script follows
1173 * ?>
1174 * </pre>
1175 */
1176 PHP_METHOD(HttpResponse, capture)
1177 {
1178 NO_ARGS;
1179
1180 UPD_STATIC_PROP(long, catch, 1);
1181
1182 php_end_ob_buffers(0 TSRMLS_CC);
1183 php_start_ob_buffer(NULL, 40960, 0 TSRMLS_CC);
1184
1185 /* register shutdown function */
1186 {
1187 zval func, retval, arg, *argp[1];
1188
1189 INIT_PZVAL(&arg);
1190 INIT_PZVAL(&func);
1191 INIT_PZVAL(&retval);
1192 ZVAL_STRINGL(&func, "register_shutdown_function", lenof("register_shutdown_function"), 0);
1193
1194 array_init(&arg);
1195 add_next_index_stringl(&arg, "HttpResponse", lenof("HttpResponse"), 1);
1196 add_next_index_stringl(&arg, "send", lenof("send"), 1);
1197 argp[0] = &arg;
1198 call_user_function(EG(function_table), NULL, &func, &retval, 1, argp TSRMLS_CC);
1199 zval_dtor(&arg);
1200 }
1201 }
1202 /* }}} */
1203
1204 #endif /* ZEND_ENGINE_2 && !WONKY */
1205
1206 /*
1207 * Local variables:
1208 * tab-width: 4
1209 * c-basic-offset: 4
1210 * End:
1211 * vim600: noet sw=4 ts=4 fdm=marker
1212 * vim<600: noet sw=4 ts=4
1213 */
1214