2 +--------------------------------------------------------------------+
4 +--------------------------------------------------------------------+
5 | Redistribution and use in source and binary forms, with or without |
6 | modification, are permitted provided that the conditions mentioned |
7 | in the accompanying LICENSE file are met. |
8 +--------------------------------------------------------------------+
9 | Copyright (c) 2004-2014, Michael Wallner <mike@php.net> |
10 +--------------------------------------------------------------------+
13 #include "php_http_api.h"
15 static void set_option(zval
*options
, const char *name_str
, size_t name_len
, int type
, void *value_ptr
, size_t value_len TSRMLS_DC
)
17 if (Z_TYPE_P(options
) == IS_OBJECT
) {
21 zend_update_property_double(Z_OBJCE_P(options
), options
, name_str
, name_len
, *(double *)value_ptr TSRMLS_CC
);
24 zend_update_property_long(Z_OBJCE_P(options
), options
, name_str
, name_len
, *(long *)value_ptr TSRMLS_CC
);
27 zend_update_property_stringl(Z_OBJCE_P(options
), options
, name_str
, name_len
, value_ptr
, value_len TSRMLS_CC
);
31 zend_update_property(Z_OBJCE_P(options
), options
, name_str
, name_len
, value_ptr TSRMLS_CC
);
35 zend_update_property_null(Z_OBJCE_P(options
), options
, name_str
, name_len TSRMLS_CC
);
38 convert_to_array(options
);
42 add_assoc_double_ex(options
, name_str
, name_len
+ 1, *(double *)value_ptr
);
45 add_assoc_long_ex(options
, name_str
, name_len
+ 1, *(long *)value_ptr
);
48 char *value
= estrndup(value_ptr
, value_len
);
49 add_assoc_stringl_ex(options
, name_str
, name_len
+ 1, value
, value_len
, 0);
53 Z_ADDREF_P(value_ptr
);
54 add_assoc_zval_ex(options
, name_str
, name_len
+ 1, value_ptr
);
59 add_assoc_null_ex(options
, name_str
, name_len
+ 1);
63 static zval
*get_option(zval
*options
, const char *name_str
, size_t name_len TSRMLS_DC
)
67 if (Z_TYPE_P(options
) == IS_OBJECT
) {
68 val
= zend_read_property(Z_OBJCE_P(options
), options
, name_str
, name_len
, 0 TSRMLS_CC
);
70 if (SUCCESS
== zend_symtable_find(Z_ARRVAL_P(options
), name_str
, name_len
+ 1, (void *) &valptr
)) {
81 static php_http_message_body_t
*get_body(zval
*options TSRMLS_DC
)
84 php_http_message_body_t
*body
= NULL
;
86 if ((zbody
= get_option(options
, ZEND_STRL("body") TSRMLS_CC
))) {
87 if ((Z_TYPE_P(zbody
) == IS_OBJECT
) && instanceof_function(Z_OBJCE_P(zbody
), php_http_message_body_class_entry TSRMLS_CC
)) {
88 php_http_message_body_object_t
*body_obj
= zend_object_store_get_object(zbody TSRMLS_CC
);
90 body
= body_obj
->body
;
92 zval_ptr_dtor(&zbody
);
97 static php_http_message_t
*get_request(zval
*options TSRMLS_DC
)
100 php_http_message_t
*request
= NULL
;
102 if ((zrequest
= get_option(options
, ZEND_STRL("request") TSRMLS_CC
))) {
103 if (Z_TYPE_P(zrequest
) == IS_OBJECT
&& instanceof_function(Z_OBJCE_P(zrequest
), php_http_message_class_entry TSRMLS_CC
)) {
104 php_http_message_object_t
*request_obj
= zend_object_store_get_object(zrequest TSRMLS_CC
);
106 request
= request_obj
->message
;
108 zval_ptr_dtor(&zrequest
);
113 static void set_cookie(zval
*options
, zval
*zcookie_new TSRMLS_DC
)
117 php_http_array_hashkey_t key
= php_http_array_hashkey_init(0);
118 php_http_cookie_object_t
*obj
= zend_object_store_get_object(zcookie_new TSRMLS_CC
);
120 zcookies_set
= get_option(options
, ZEND_STRL("cookies") TSRMLS_CC
);
121 if (!zcookies_set
|| Z_TYPE_P(zcookies_set
) != IS_ARRAY
) {
123 zval_ptr_dtor(&zcookies_set
);
125 MAKE_STD_ZVAL(zcookies_set
);
126 array_init_size(zcookies_set
, zend_hash_num_elements(&obj
->list
->cookies
));
128 SEPARATE_ZVAL(&zcookies_set
);
131 FOREACH_HASH_KEY(pos
, &obj
->list
->cookies
, key
) {
132 Z_ADDREF_P(zcookie_new
);
133 if (key
.type
== HASH_KEY_IS_STRING
) {
134 add_assoc_zval_ex(zcookies_set
, key
.str
, key
.len
, zcookie_new
);
136 add_index_zval(zcookies_set
, key
.num
, zcookie_new
);
140 set_option(options
, ZEND_STRL("cookies"), IS_ARRAY
, zcookies_set
, 0 TSRMLS_CC
);
141 zval_ptr_dtor(&zcookies_set
);
144 php_http_cache_status_t
php_http_env_is_response_cached_by_etag(zval
*options
, const char *header_str
, size_t header_len
, php_http_message_t
*request TSRMLS_DC
)
146 php_http_cache_status_t ret
= PHP_HTTP_CACHE_NO
;
148 char *header
= NULL
, *etag
;
149 php_http_message_body_t
*body
;
153 if (!(body
= get_body(options TSRMLS_CC
))) {
157 if ((zetag
= get_option(options
, ZEND_STRL("etag") TSRMLS_CC
))) {
158 zval
*zetag_copy
= php_http_ztyp(IS_STRING
, zetag
);
159 zval_ptr_dtor(&zetag
);
163 if (zetag
&& Z_STRLEN_P(zetag
)) {
164 etag
= Z_STRVAL_P(zetag
);
165 } else if ((etag
= php_http_message_body_etag(body
))) {
166 set_option(options
, ZEND_STRL("etag"), IS_STRING
, etag
, strlen(etag
) TSRMLS_CC
);
171 zval_ptr_dtor(&zetag
);
174 if (etag
&& (header
= php_http_env_get_request_header(header_str
, header_len
, NULL
, request TSRMLS_CC
))) {
175 ret
= php_http_match(header
, etag
, PHP_HTTP_MATCH_WORD
) ? PHP_HTTP_CACHE_HIT
: PHP_HTTP_CACHE_MISS
;
186 php_http_cache_status_t
php_http_env_is_response_cached_by_last_modified(zval
*options
, const char *header_str
, size_t header_len
, php_http_message_t
*request TSRMLS_DC
)
188 php_http_cache_status_t ret
= PHP_HTTP_CACHE_NO
;
191 php_http_message_body_t
*body
;
194 if (!(body
= get_body(options TSRMLS_CC
))) {
198 if ((zlm
= get_option(options
, ZEND_STRL("lastModified") TSRMLS_CC
))) {
199 zval
*zlm_copy
= php_http_ztyp(IS_LONG
, zlm
);
204 if (zlm
&& Z_LVAL_P(zlm
) > 0) {
207 lm
= php_http_message_body_mtime(body
);
208 set_option(options
, ZEND_STRL("lastModified"), IS_LONG
, &lm
, 0 TSRMLS_CC
);
215 if ((header
= php_http_env_get_request_header(header_str
, header_len
, NULL
, request TSRMLS_CC
))) {
216 ums
= php_parse_date(header
, NULL
);
218 if (ums
> 0 && ums
>= lm
) {
219 ret
= PHP_HTTP_CACHE_HIT
;
221 ret
= PHP_HTTP_CACHE_MISS
;
229 static zend_bool
php_http_env_response_is_cacheable(php_http_env_response_t
*r
, php_http_message_t
*request
)
231 long status
= r
->ops
->get_status(r
);
232 TSRMLS_FETCH_FROM_CTX(r
->ts
);
234 if (status
&& status
/ 100 != 2) {
238 if (php_http_env_got_request_header(ZEND_STRL("Authorization"), request TSRMLS_CC
)) {
242 if (-1 == php_http_select_str(php_http_env_get_request_method(request TSRMLS_CC
), 2, "HEAD", "GET")) {
249 static size_t output(void *context
, char *buf
, size_t len TSRMLS_DC
)
251 php_http_env_response_t
*r
= context
;
253 if (SUCCESS
!= r
->ops
->write(r
, buf
, len
)) {
257 /* we really only need to flush when throttling is enabled,
258 because we push the data as fast as possible anyway if not */
259 if (r
->throttle
.delay
>= PHP_HTTP_DIFFSEC
) {
261 php_http_sleep(r
->throttle
.delay
);
266 #define php_http_env_response_send_done(r) php_http_env_response_send_data((r), NULL, 0)
267 static ZEND_RESULT_CODE
php_http_env_response_send_data(php_http_env_response_t
*r
, const char *buf
, size_t len
)
269 size_t chunks_sent
, chunk
= r
->throttle
.chunk
? r
->throttle
.chunk
: PHP_HTTP_SENDBUF_SIZE
;
270 TSRMLS_FETCH_FROM_CTX(r
->ts
);
272 if (r
->content
.encoder
) {
273 char *enc_str
= NULL
;
277 if (SUCCESS
!= php_http_encoding_stream_update(r
->content
.encoder
, buf
, len
, &enc_str
, &enc_len
)) {
281 if (SUCCESS
!= php_http_encoding_stream_finish(r
->content
.encoder
, &enc_str
, &enc_len
)) {
289 chunks_sent
= php_http_buffer_chunked_output(&r
->buffer
, enc_str
, enc_len
, buf
? chunk
: 0, output
, r TSRMLS_CC
);
292 chunks_sent
= php_http_buffer_chunked_output(&r
->buffer
, buf
, len
, buf
? chunk
: 0, output
, r TSRMLS_CC
);
295 return chunks_sent
!= (size_t) -1 ? SUCCESS
: FAILURE
;
298 php_http_env_response_t
*php_http_env_response_init(php_http_env_response_t
*r
, zval
*options
, php_http_env_response_ops_t
*ops
, void *init_arg TSRMLS_DC
)
303 r
= emalloc(sizeof(*r
));
305 memset(r
, 0, sizeof(*r
));
310 r
->ops
= php_http_env_response_get_sapi_ops();
313 r
->buffer
= php_http_buffer_init(NULL
);
316 r
->options
= options
;
318 TSRMLS_SET_CTX(r
->ts
);
320 if (r
->ops
->init
&& (SUCCESS
!= r
->ops
->init(r
, init_arg
))) {
322 php_http_env_response_free(&r
);
324 php_http_env_response_dtor(r
);
332 void php_http_env_response_dtor(php_http_env_response_t
*r
)
337 php_http_buffer_free(&r
->buffer
);
338 zval_ptr_dtor(&r
->options
);
339 PTR_FREE(r
->content
.type
);
340 PTR_FREE(r
->content
.encoding
);
341 if (r
->content
.encoder
) {
342 php_http_encoding_stream_free(&r
->content
.encoder
);
346 void php_http_env_response_free(php_http_env_response_t
**r
)
349 php_http_env_response_dtor(*r
);
355 static ZEND_RESULT_CODE
php_http_env_response_send_head(php_http_env_response_t
*r
, php_http_message_t
*request
)
357 ZEND_RESULT_CODE ret
= SUCCESS
;
358 zval
*zoption
, *options
= r
->options
;
359 TSRMLS_FETCH_FROM_CTX(r
->ts
);
365 if ((zoption
= get_option(options
, ZEND_STRL("headers") TSRMLS_CC
))) {
366 if (Z_TYPE_P(zoption
) == IS_ARRAY
) {
367 php_http_header_to_callback(Z_ARRVAL_P(zoption
), 0, (php_http_pass_format_callback_t
) r
->ops
->set_header
, r TSRMLS_CC
);
369 zval_ptr_dtor(&zoption
);
372 if ((zoption
= get_option(options
, ZEND_STRL("responseCode") TSRMLS_CC
))) {
373 zval
*zoption_copy
= php_http_ztyp(IS_LONG
, zoption
);
375 zval_ptr_dtor(&zoption
);
376 if (Z_LVAL_P(zoption_copy
) > 0) {
377 ret
= r
->ops
->set_status(r
, Z_LVAL_P(zoption_copy
));
379 zval_ptr_dtor(&zoption_copy
);
382 if (ret
!= SUCCESS
) {
386 if ((zoption
= get_option(options
, ZEND_STRL("httpVersion") TSRMLS_CC
))) {
387 php_http_version_t v
;
388 zval
*zoption_copy
= php_http_ztyp(IS_STRING
, zoption
);
390 zval_ptr_dtor(&zoption
);
391 if (Z_STRLEN_P(zoption_copy
) && php_http_version_parse(&v
, Z_STRVAL_P(zoption_copy
) TSRMLS_CC
)) {
392 ret
= r
->ops
->set_protocol_version(r
, &v
);
393 php_http_version_dtor(&v
);
395 zval_ptr_dtor(&zoption_copy
);
398 if (ret
!= SUCCESS
) {
402 if ((zoption
= get_option(options
, ZEND_STRL("cookies") TSRMLS_CC
))) {
403 if (Z_TYPE_P(zoption
) == IS_ARRAY
) {
407 FOREACH_VAL(pos
, zoption
, zcookie
) {
408 if (Z_TYPE_PP(zcookie
) == IS_OBJECT
&& instanceof_function(Z_OBJCE_PP(zcookie
), php_http_cookie_class_entry TSRMLS_CC
)) {
409 php_http_cookie_object_t
*obj
= zend_object_store_get_object(*zcookie TSRMLS_CC
);
413 php_http_cookie_list_to_string(obj
->list
, &str
, &len
);
414 if (SUCCESS
!= (ret
= r
->ops
->add_header(r
, "Set-Cookie: %s", str
))) {
422 zval_ptr_dtor(&zoption
);
425 if (ret
!= SUCCESS
) {
429 if ((zoption
= get_option(options
, ZEND_STRL("contentType") TSRMLS_CC
))) {
430 zval
*zoption_copy
= php_http_ztyp(IS_STRING
, zoption
);
432 zval_ptr_dtor(&zoption
);
433 if (Z_STRLEN_P(zoption_copy
) && strchr(Z_STRVAL_P(zoption_copy
), '/')) {
434 if (SUCCESS
== (ret
= r
->ops
->set_header(r
, "Content-Type: %.*s", Z_STRLEN_P(zoption_copy
), Z_STRVAL_P(zoption_copy
)))) {
435 r
->content
.type
= estrndup(Z_STRVAL_P(zoption_copy
), Z_STRLEN_P(zoption_copy
));
438 zval_ptr_dtor(&zoption_copy
);
441 if (ret
!= SUCCESS
) {
445 if (r
->range
.status
== PHP_HTTP_RANGE_OK
) {
446 if (zend_hash_num_elements(&r
->range
.values
) == 1) {
447 zval
**range
, **begin
, **end
;
449 if ( 1 == php_http_array_list(&r
->range
.values TSRMLS_CC
, 1, &range
)
450 && 2 == php_http_array_list(Z_ARRVAL_PP(range
) TSRMLS_CC
, 2, &begin
, &end
)
452 if (SUCCESS
== (ret
= r
->ops
->set_status(r
, 206))) {
453 ret
= r
->ops
->set_header(r
, "Content-Range: bytes %ld-%ld/%zu", Z_LVAL_PP(begin
), Z_LVAL_PP(end
), r
->content
.length
);
456 /* this should never happen */
457 zend_hash_destroy(&r
->range
.values
);
461 php_http_boundary(r
->range
.boundary
, sizeof(r
->range
.boundary
) TSRMLS_CC
);
462 if (SUCCESS
== (ret
= r
->ops
->set_status(r
, 206))) {
463 ret
= r
->ops
->set_header(r
, "Content-Type: multipart/byteranges; boundary=%s", r
->range
.boundary
);
467 if ((zoption
= get_option(options
, ZEND_STRL("cacheControl") TSRMLS_CC
))) {
468 zval
*zoption_copy
= php_http_ztyp(IS_STRING
, zoption
);
470 zval_ptr_dtor(&zoption
);
471 if (Z_STRLEN_P(zoption_copy
)) {
472 ret
= r
->ops
->set_header(r
, "Cache-Control: %.*s", Z_STRLEN_P(zoption_copy
), Z_STRVAL_P(zoption_copy
));
474 zval_ptr_dtor(&zoption_copy
);
477 if (ret
!= SUCCESS
) {
481 if ((zoption
= get_option(options
, ZEND_STRL("contentDisposition") TSRMLS_CC
))) {
482 zval
*zoption_copy
= php_http_ztyp(IS_ARRAY
, zoption
);
483 php_http_buffer_t buf
;
485 php_http_buffer_init(&buf
);
486 if (php_http_params_to_string(&buf
, Z_ARRVAL_P(zoption_copy
), ZEND_STRL(","), ZEND_STRL(";"), ZEND_STRL("="), PHP_HTTP_PARAMS_DEFAULT TSRMLS_CC
)) {
488 ret
= r
->ops
->set_header(r
, "Content-Disposition: %.*s", buf
.used
, buf
.data
);
492 php_http_buffer_dtor(&buf
);
493 zval_ptr_dtor(&zoption_copy
);
494 zval_ptr_dtor(&zoption
);
497 if (ret
!= SUCCESS
) {
501 if ((zoption
= get_option(options
, ZEND_STRL("contentEncoding") TSRMLS_CC
))) {
502 zval
*zoption_copy
= php_http_ztyp(IS_LONG
, zoption
);
504 HashTable
*result
= NULL
;
506 zval_ptr_dtor(&zoption
);
507 switch (Z_LVAL_P(zoption_copy
)) {
508 case PHP_HTTP_CONTENT_ENCODING_GZIP
:
509 INIT_PZVAL(&zsupported
);
510 array_init(&zsupported
);
511 add_next_index_stringl(&zsupported
, ZEND_STRL("none"), 1);
512 add_next_index_stringl(&zsupported
, ZEND_STRL("gzip"), 1);
513 add_next_index_stringl(&zsupported
, ZEND_STRL("deflate"), 1);
515 if ((result
= php_http_negotiate_encoding(Z_ARRVAL(zsupported
), request TSRMLS_CC
))) {
516 char *key_str
= NULL
;
519 zend_hash_internal_pointer_reset(result
);
520 if (HASH_KEY_IS_STRING
== zend_hash_get_current_key_ex(result
, &key_str
, &key_len
, NULL
, 0, NULL
)) {
521 if (!strcmp(key_str
, "gzip")) {
522 if (!(r
->content
.encoder
= php_http_encoding_stream_init(NULL
, php_http_encoding_stream_get_deflate_ops(), PHP_HTTP_DEFLATE_TYPE_GZIP TSRMLS_CC
))) {
524 } else if (SUCCESS
== (ret
= r
->ops
->set_header(r
, "Content-Encoding: gzip"))) {
525 r
->content
.encoding
= estrndup(key_str
, key_len
- 1);
527 } else if (!strcmp(key_str
, "deflate")) {
528 if (!(r
->content
.encoder
= php_http_encoding_stream_init(NULL
, php_http_encoding_stream_get_deflate_ops(), PHP_HTTP_DEFLATE_TYPE_ZLIB TSRMLS_CC
))) {
530 } else if (SUCCESS
== (ret
= r
->ops
->set_header(r
, "Content-Encoding: deflate"))) {
531 r
->content
.encoding
= estrndup(key_str
, key_len
- 1);
534 ret
= r
->ops
->del_header(r
, ZEND_STRL("Content-Encoding"));
537 if (SUCCESS
== ret
) {
538 ret
= r
->ops
->add_header(r
, "Vary: Accept-Encoding");
542 zend_hash_destroy(result
);
543 FREE_HASHTABLE(result
);
546 zval_dtor(&zsupported
);
549 case PHP_HTTP_CONTENT_ENCODING_NONE
:
551 ret
= r
->ops
->del_header(r
, ZEND_STRL("Content-Encoding"));
554 zval_ptr_dtor(&zoption_copy
);
557 if (SUCCESS
!= ret
) {
561 if (php_http_env_response_is_cacheable(r
, request
)) {
562 switch (php_http_env_is_response_cached_by_etag(options
, ZEND_STRL("If-None-Match"), request TSRMLS_CC
)) {
563 case PHP_HTTP_CACHE_MISS
:
566 case PHP_HTTP_CACHE_NO
:
567 if (PHP_HTTP_CACHE_HIT
!= php_http_env_is_response_cached_by_last_modified(options
, ZEND_STRL("If-Modified-Since"), request TSRMLS_CC
)) {
572 case PHP_HTTP_CACHE_HIT
:
573 ret
= r
->ops
->set_status(r
, 304);
578 if ((zoption
= get_option(options
, ZEND_STRL("etag") TSRMLS_CC
))) {
579 zval
*zoption_copy
= php_http_ztyp(IS_STRING
, zoption
);
581 zval_ptr_dtor(&zoption
);
582 if (*Z_STRVAL_P(zoption_copy
) != '"' && strncmp(Z_STRVAL_P(zoption_copy
), "W/\"", 3)) {
583 ret
= r
->ops
->set_header(r
, "ETag: \"%s\"", Z_STRVAL_P(zoption_copy
));
585 ret
= r
->ops
->set_header(r
, "ETag: %s", Z_STRVAL_P(zoption_copy
));
587 zval_ptr_dtor(&zoption_copy
);
589 if ((zoption
= get_option(options
, ZEND_STRL("lastModified") TSRMLS_CC
))) {
590 zval
*zoption_copy
= php_http_ztyp(IS_LONG
, zoption
);
592 zval_ptr_dtor(&zoption
);
593 if (Z_LVAL_P(zoption_copy
)) {
594 char *date
= php_format_date(ZEND_STRL(PHP_HTTP_DATE_FORMAT
), Z_LVAL_P(zoption_copy
), 0 TSRMLS_CC
);
596 ret
= r
->ops
->set_header(r
, "Last-Modified: %s", date
);
600 zval_ptr_dtor(&zoption_copy
);
608 static ZEND_RESULT_CODE
php_http_env_response_send_body(php_http_env_response_t
*r
)
610 ZEND_RESULT_CODE ret
= SUCCESS
;
612 php_http_message_body_t
*body
;
613 TSRMLS_FETCH_FROM_CTX(r
->ts
);
619 if ((body
= get_body(r
->options TSRMLS_CC
))) {
620 if ((zoption
= get_option(r
->options
, ZEND_STRL("throttleDelay") TSRMLS_CC
))) {
621 if (Z_TYPE_P(zoption
) == IS_DOUBLE
) {
622 r
->throttle
.delay
= Z_DVAL_P(zoption
);
624 zval_ptr_dtor(&zoption
);
626 if ((zoption
= get_option(r
->options
, ZEND_STRL("throttleChunk") TSRMLS_CC
))) {
627 if (Z_TYPE_P(zoption
) == IS_LONG
) {
628 r
->throttle
.chunk
= Z_LVAL_P(zoption
);
630 zval_ptr_dtor(&zoption
);
633 if (r
->range
.status
== PHP_HTTP_RANGE_OK
) {
634 if (zend_hash_num_elements(&r
->range
.values
) == 1) {
636 zval
**range
, **begin
, **end
;
638 if ( 1 == php_http_array_list(&r
->range
.values TSRMLS_CC
, 1, &range
)
639 && 2 == php_http_array_list(Z_ARRVAL_PP(range
) TSRMLS_CC
, 2, &begin
, &end
)
642 ret
= php_http_message_body_to_callback(body
, (php_http_pass_callback_t
) php_http_env_response_send_data
, r
, Z_LVAL_PP(begin
), Z_LVAL_PP(end
) - Z_LVAL_PP(begin
) + 1);
643 if (ret
== SUCCESS
) {
644 ret
= php_http_env_response_send_done(r
);
646 zend_hash_destroy(&r
->range
.values
);
648 /* this should never happen */
649 zend_hash_destroy(&r
->range
.values
);
650 r
->ops
->set_status(r
, 500);
655 /* send multipart/byte-ranges message */
659 FOREACH_HASH_VAL(pos
, &r
->range
.values
, chunk
) {
662 if (2 == php_http_array_list(Z_ARRVAL_PP(chunk
) TSRMLS_CC
, 2, &begin
, &end
)) {
663 php_http_buffer_appendf(r
->buffer
,
666 "Content-Type: %s" PHP_HTTP_CRLF
667 "Content-Range: bytes %ld-%ld/%zu" PHP_HTTP_CRLF PHP_HTTP_CRLF
,
670 r
->content
.type
? r
->content
.type
: "application/octet-stream",
675 ret
= php_http_message_body_to_callback(body
, (php_http_pass_callback_t
) php_http_env_response_send_data
, r
, Z_LVAL_PP(begin
), Z_LVAL_PP(end
) - Z_LVAL_PP(begin
) + 1);
679 if (ret
== SUCCESS
) {
680 php_http_buffer_appendf(r
->buffer
, PHP_HTTP_CRLF
"--%s--", r
->range
.boundary
);
681 ret
= php_http_env_response_send_done(r
);
683 zend_hash_destroy(&r
->range
.values
);
687 ret
= php_http_message_body_to_callback(body
, (php_http_pass_callback_t
) php_http_env_response_send_data
, r
, 0, 0);
688 if (ret
== SUCCESS
) {
689 ret
= php_http_env_response_send_done(r
);
696 ZEND_RESULT_CODE
php_http_env_response_send(php_http_env_response_t
*r
)
698 php_http_message_t
*request
;
699 php_http_message_body_t
*body
;
700 TSRMLS_FETCH_FROM_CTX(r
->ts
);
702 request
= get_request(r
->options TSRMLS_CC
);
704 /* check for ranges */
705 if ((body
= get_body(r
->options TSRMLS_CC
))) {
706 r
->content
.length
= php_http_message_body_size(body
);
708 if (SUCCESS
!= r
->ops
->set_header(r
, "Accept-Ranges: bytes")) {
711 zend_hash_init(&r
->range
.values
, 0, NULL
, ZVAL_PTR_DTOR
, 0);
712 r
->range
.status
= php_http_env_get_request_ranges(&r
->range
.values
, r
->content
.length
, request TSRMLS_CC
);
714 switch (r
->range
.status
) {
715 case PHP_HTTP_RANGE_NO
:
716 zend_hash_destroy(&r
->range
.values
);
719 case PHP_HTTP_RANGE_ERR
:
720 if (php_http_env_got_request_header(ZEND_STRL("If-Range"), request TSRMLS_CC
)) {
721 r
->range
.status
= PHP_HTTP_RANGE_NO
;
722 zend_hash_destroy(&r
->range
.values
);
725 zend_hash_destroy(&r
->range
.values
);
726 if (SUCCESS
!= r
->ops
->set_status(r
, 416)) {
729 if (SUCCESS
!= r
->ops
->set_header(r
, "Content-Range: bytes */%zu", r
->content
.length
)) {
735 case PHP_HTTP_RANGE_OK
:
736 if (PHP_HTTP_CACHE_MISS
== php_http_env_is_response_cached_by_etag(r
->options
, ZEND_STRL("If-Range"), request TSRMLS_CC
)
737 || PHP_HTTP_CACHE_MISS
== php_http_env_is_response_cached_by_last_modified(r
->options
, ZEND_STRL("If-Range"), request TSRMLS_CC
)
739 r
->range
.status
= PHP_HTTP_RANGE_NO
;
740 zend_hash_destroy(&r
->range
.values
);
743 if (PHP_HTTP_CACHE_MISS
== php_http_env_is_response_cached_by_etag(r
->options
, ZEND_STRL("If-Match"), request TSRMLS_CC
)
744 || PHP_HTTP_CACHE_MISS
== php_http_env_is_response_cached_by_last_modified(r
->options
, ZEND_STRL("If-Unmodified-Since"), request TSRMLS_CC
)
745 || PHP_HTTP_CACHE_MISS
== php_http_env_is_response_cached_by_last_modified(r
->options
, ZEND_STRL("Unless-Modified-Since"), request TSRMLS_CC
)
748 zend_hash_destroy(&r
->range
.values
);
749 if (SUCCESS
!= r
->ops
->set_status(r
, 412)) {
760 if (SUCCESS
!= php_http_env_response_send_head(r
, request
)) {
761 php_error_docref(NULL TSRMLS_CC
, E_WARNING
, "Failed to send response headers");
765 if (SUCCESS
!= php_http_env_response_send_body(r
)) {
766 php_error_docref(NULL TSRMLS_CC
, E_WARNING
, "Failed to send response body");
770 if (SUCCESS
!= r
->ops
->finish(r
)) {
771 php_error_docref(NULL TSRMLS_CC
, E_WARNING
, "Failed to finish response");
778 static long php_http_env_response_sapi_get_status(php_http_env_response_t
*r
)
780 TSRMLS_FETCH_FROM_CTX(r
->ts
);
782 return php_http_env_get_response_code(TSRMLS_C
);
784 static ZEND_RESULT_CODE
php_http_env_response_sapi_set_status(php_http_env_response_t
*r
, long http_code
)
786 TSRMLS_FETCH_FROM_CTX(r
->ts
);
788 return php_http_env_set_response_code(http_code TSRMLS_CC
);
790 static ZEND_RESULT_CODE
php_http_env_response_sapi_set_protocol_version(php_http_env_response_t
*r
, php_http_version_t
*v
)
792 TSRMLS_FETCH_FROM_CTX(r
->ts
);
794 return php_http_env_set_response_protocol_version(v TSRMLS_CC
);
796 static ZEND_RESULT_CODE
php_http_env_response_sapi_set_header(php_http_env_response_t
*r
, const char *fmt
, ...)
798 ZEND_RESULT_CODE ret
;
800 TSRMLS_FETCH_FROM_CTX(r
->ts
);
803 ret
= php_http_env_set_response_header_va(0, 1, fmt
, args TSRMLS_CC
);
808 static ZEND_RESULT_CODE
php_http_env_response_sapi_add_header(php_http_env_response_t
*r
, const char *fmt
, ...)
810 ZEND_RESULT_CODE ret
;
812 TSRMLS_FETCH_FROM_CTX(r
->ts
);
815 ret
= php_http_env_set_response_header_va(0, 0, fmt
, args TSRMLS_CC
);
820 static ZEND_RESULT_CODE
php_http_env_response_sapi_del_header(php_http_env_response_t
*r
, const char *header_str
, size_t header_len
)
822 TSRMLS_FETCH_FROM_CTX(r
->ts
);
824 return php_http_env_set_response_header_value(0, header_str
, header_len
, NULL
, 1 TSRMLS_CC
);
826 static ZEND_RESULT_CODE
php_http_env_response_sapi_write(php_http_env_response_t
*r
, const char *data_str
, size_t data_len
)
828 TSRMLS_FETCH_FROM_CTX(r
->ts
);
830 if (0 < PHPWRITE(data_str
, data_len
)) {
835 static ZEND_RESULT_CODE
php_http_env_response_sapi_flush(php_http_env_response_t
*r
)
837 TSRMLS_FETCH_FROM_CTX(r
->ts
);
839 #if PHP_VERSION_ID >= 50400
840 if (php_output_get_level(TSRMLS_C
)) {
841 php_output_flush_all(TSRMLS_C
);
843 if (!(php_output_get_status(TSRMLS_C
) & PHP_OUTPUT_IMPLICITFLUSH
)) {
844 sapi_flush(TSRMLS_C
);
847 php_end_ob_buffer(1, 1 TSRMLS_CC
);
848 sapi_flush(TSRMLS_C
);
853 static ZEND_RESULT_CODE
php_http_env_response_sapi_finish(php_http_env_response_t
*r
)
858 static php_http_env_response_ops_t php_http_env_response_sapi_ops
= {
861 php_http_env_response_sapi_get_status
,
862 php_http_env_response_sapi_set_status
,
863 php_http_env_response_sapi_set_protocol_version
,
864 php_http_env_response_sapi_set_header
,
865 php_http_env_response_sapi_add_header
,
866 php_http_env_response_sapi_del_header
,
867 php_http_env_response_sapi_write
,
868 php_http_env_response_sapi_flush
,
869 php_http_env_response_sapi_finish
872 php_http_env_response_ops_t
*php_http_env_response_get_sapi_ops(void)
874 return &php_http_env_response_sapi_ops
;
877 typedef struct php_http_env_response_stream_ctx
{
879 php_http_version_t version
;
883 php_stream_filter
*chunked_filter
;
884 php_http_message_t
*request
;
889 } php_http_env_response_stream_ctx_t
;
891 static ZEND_RESULT_CODE
php_http_env_response_stream_init(php_http_env_response_t
*r
, void *init_arg
)
893 php_http_env_response_stream_ctx_t
*ctx
;
894 size_t buffer_size
= 0x1000;
895 TSRMLS_FETCH_FROM_CTX(r
->ts
);
897 ctx
= ecalloc(1, sizeof(*ctx
));
899 ctx
->stream
= init_arg
;
900 if (!ctx
->stream
|| SUCCESS
!= zend_list_addref(ctx
->stream
->rsrc_id
)) {
904 php_stream_set_option(ctx
->stream
, PHP_STREAM_OPTION_WRITE_BUFFER
, PHP_STREAM_BUFFER_FULL
, &buffer_size
);
905 zend_hash_init(&ctx
->header
, 0, NULL
, ZVAL_PTR_DTOR
, 0);
906 php_http_version_init(&ctx
->version
, 1, 1 TSRMLS_CC
);
907 ctx
->status_code
= 200;
909 ctx
->request
= get_request(r
->options TSRMLS_CC
);
911 /* there are some limitations regarding TE:chunked, see https://tools.ietf.org/html/rfc7230#section-3.3.1 */
912 if (ctx
->request
&& ctx
->request
->http
.version
.major
== 1 && ctx
->request
->http
.version
.minor
== 0) {
913 ctx
->version
.minor
= 0;
920 static void php_http_env_response_stream_dtor(php_http_env_response_t
*r
)
922 php_http_env_response_stream_ctx_t
*ctx
= r
->ctx
;
923 TSRMLS_FETCH_FROM_CTX(r
->ts
);
925 if (ctx
->chunked_filter
) {
926 ctx
->chunked_filter
= php_stream_filter_remove(ctx
->chunked_filter
, 1 TSRMLS_CC
);
928 zend_hash_destroy(&ctx
->header
);
929 zend_list_delete(ctx
->stream
->rsrc_id
);
933 static void php_http_env_response_stream_header(php_http_env_response_stream_ctx_t
*ctx
, HashTable
*header
, php_http_buffer_t
*buf TSRMLS_DC
)
938 FOREACH_HASH_VAL(pos
, header
, val
) {
939 if (Z_TYPE_PP(val
) == IS_ARRAY
) {
940 php_http_env_response_stream_header(ctx
, Z_ARRVAL_PP(val
), buf TSRMLS_CC
);
942 zval
*tmp
= php_http_ztyp(IS_STRING
, *val
);
945 /* disable chunked transfer encoding if we've got an explicit content-length */
946 if (!strncasecmp(Z_STRVAL_P(tmp
), "Content-Length:", lenof("Content-Length:"))) {
950 php_http_buffer_append(buf
, Z_STRVAL_P(tmp
), Z_STRLEN_P(tmp
));
951 php_http_buffer_appends(buf
, PHP_HTTP_CRLF
);
956 static ZEND_RESULT_CODE
php_http_env_response_stream_start(php_http_env_response_stream_ctx_t
*ctx TSRMLS_DC
)
958 php_http_buffer_t header_buf
;
960 if (ctx
->started
|| ctx
->finished
) {
964 php_http_buffer_init(&header_buf
);
965 php_http_buffer_appendf(&header_buf
, "HTTP/%u.%u %ld %s" PHP_HTTP_CRLF
, ctx
->version
.major
, ctx
->version
.minor
, ctx
->status_code
, php_http_env_get_response_status_for_code(ctx
->status_code
));
967 /* there are some limitations regarding TE:chunked, see https://tools.ietf.org/html/rfc7230#section-3.3.1 */
968 if (ctx
->version
.major
== 1 && ctx
->version
.minor
== 0) {
970 } else if (ctx
->status_code
== 204 || ctx
->status_code
/100 == 1) {
972 } else if (ctx
->request
&& ctx
->status_code
/100 == 2 && !strcasecmp(ctx
->request
->http
.info
.request
.method
, "CONNECT")) {
976 php_http_env_response_stream_header(ctx
, &ctx
->header
, &header_buf TSRMLS_CC
);
978 /* enable chunked transfer encoding */
980 php_http_buffer_appends(&header_buf
, "Transfer-Encoding: chunked" PHP_HTTP_CRLF
);
982 php_http_buffer_appends(&header_buf
, PHP_HTTP_CRLF
);
984 if (header_buf
.used
== php_stream_write(ctx
->stream
, header_buf
.data
, header_buf
.used
)) {
987 php_http_buffer_dtor(&header_buf
);
988 php_stream_flush(ctx
->stream
);
991 ctx
->chunked_filter
= php_stream_filter_create("http.chunked_encode", NULL
, 0 TSRMLS_CC
);
992 php_stream_filter_append(&ctx
->stream
->writefilters
, ctx
->chunked_filter
);
995 return ctx
->started
? SUCCESS
: FAILURE
;
997 static long php_http_env_response_stream_get_status(php_http_env_response_t
*r
)
999 php_http_env_response_stream_ctx_t
*ctx
= r
->ctx
;
1001 return ctx
->status_code
;
1003 static ZEND_RESULT_CODE
php_http_env_response_stream_set_status(php_http_env_response_t
*r
, long http_code
)
1005 php_http_env_response_stream_ctx_t
*stream_ctx
= r
->ctx
;
1007 if (stream_ctx
->started
|| stream_ctx
->finished
) {
1011 stream_ctx
->status_code
= http_code
;
1015 static ZEND_RESULT_CODE
php_http_env_response_stream_set_protocol_version(php_http_env_response_t
*r
, php_http_version_t
*v
)
1017 php_http_env_response_stream_ctx_t
*stream_ctx
= r
->ctx
;
1019 if (stream_ctx
->started
|| stream_ctx
->finished
) {
1023 memcpy(&stream_ctx
->version
, v
, sizeof(stream_ctx
->version
));
1027 static ZEND_RESULT_CODE
php_http_env_response_stream_set_header_ex(php_http_env_response_t
*r
, zend_bool replace
, const char *fmt
, va_list argv
)
1029 php_http_env_response_stream_ctx_t
*stream_ctx
= r
->ctx
;
1030 char *header_end
, *header_str
= NULL
;
1031 size_t header_len
= 0;
1032 zval
*zheader
, **zheader_ptr
;
1034 if (stream_ctx
->started
|| stream_ctx
->finished
) {
1038 header_len
= vspprintf(&header_str
, 0, fmt
, argv
);
1040 if (!(header_end
= strchr(header_str
, ':'))) {
1047 if (!replace
&& (SUCCESS
== zend_hash_find(&stream_ctx
->header
, header_str
, header_end
- header_str
+ 1, (void *) &zheader_ptr
))) {
1048 convert_to_array(*zheader_ptr
);
1050 return add_next_index_stringl(*zheader_ptr
, header_str
, header_len
, 0);
1052 MAKE_STD_ZVAL(zheader
);
1053 ZVAL_STRINGL(zheader
, header_str
, header_len
, 0);
1055 if (SUCCESS
!= zend_hash_update(&stream_ctx
->header
, header_str
, header_end
- header_str
+ 1, (void *) &zheader
, sizeof(zval
*), NULL
)) {
1056 zval_ptr_dtor(&zheader
);
1064 static ZEND_RESULT_CODE
php_http_env_response_stream_set_header(php_http_env_response_t
*r
, const char *fmt
, ...)
1066 ZEND_RESULT_CODE ret
;
1069 va_start(argv
, fmt
);
1070 ret
= php_http_env_response_stream_set_header_ex(r
, 1, fmt
, argv
);
1075 static ZEND_RESULT_CODE
php_http_env_response_stream_add_header(php_http_env_response_t
*r
, const char *fmt
, ...)
1077 ZEND_RESULT_CODE ret
;
1080 va_start(argv
, fmt
);
1081 ret
= php_http_env_response_stream_set_header_ex(r
, 0, fmt
, argv
);
1086 static ZEND_RESULT_CODE
php_http_env_response_stream_del_header(php_http_env_response_t
*r
, const char *header_str
, size_t header_len
)
1088 php_http_env_response_stream_ctx_t
*stream_ctx
= r
->ctx
;
1090 if (stream_ctx
->started
|| stream_ctx
->finished
) {
1094 zend_hash_del(&stream_ctx
->header
, header_str
, header_len
+ 1);
1097 static ZEND_RESULT_CODE
php_http_env_response_stream_write(php_http_env_response_t
*r
, const char *data_str
, size_t data_len
)
1099 php_http_env_response_stream_ctx_t
*stream_ctx
= r
->ctx
;
1100 TSRMLS_FETCH_FROM_CTX(r
->ts
);
1102 if (stream_ctx
->finished
) {
1105 if (!stream_ctx
->started
) {
1106 if (SUCCESS
!= php_http_env_response_stream_start(stream_ctx TSRMLS_CC
)) {
1111 if (data_len
!= php_stream_write(stream_ctx
->stream
, data_str
, data_len
)) {
1117 static ZEND_RESULT_CODE
php_http_env_response_stream_flush(php_http_env_response_t
*r
)
1119 php_http_env_response_stream_ctx_t
*stream_ctx
= r
->ctx
;
1120 TSRMLS_FETCH_FROM_CTX(r
->ts
);
1122 if (stream_ctx
->finished
) {
1125 if (!stream_ctx
->started
) {
1126 if (SUCCESS
!= php_http_env_response_stream_start(stream_ctx TSRMLS_CC
)) {
1131 return php_stream_flush(stream_ctx
->stream
);
1133 static ZEND_RESULT_CODE
php_http_env_response_stream_finish(php_http_env_response_t
*r
)
1135 php_http_env_response_stream_ctx_t
*ctx
= r
->ctx
;
1136 TSRMLS_FETCH_FROM_CTX(r
->ts
);
1138 if (ctx
->finished
) {
1141 if (!ctx
->started
) {
1142 if (SUCCESS
!= php_http_env_response_stream_start(ctx TSRMLS_CC
)) {
1147 php_stream_flush(ctx
->stream
);
1148 if (ctx
->chunked
&& ctx
->chunked_filter
) {
1149 php_stream_filter_flush(ctx
->chunked_filter
, 1);
1150 ctx
->chunked_filter
= php_stream_filter_remove(ctx
->chunked_filter
, 1 TSRMLS_CC
);
1158 static php_http_env_response_ops_t php_http_env_response_stream_ops
= {
1159 php_http_env_response_stream_init
,
1160 php_http_env_response_stream_dtor
,
1161 php_http_env_response_stream_get_status
,
1162 php_http_env_response_stream_set_status
,
1163 php_http_env_response_stream_set_protocol_version
,
1164 php_http_env_response_stream_set_header
,
1165 php_http_env_response_stream_add_header
,
1166 php_http_env_response_stream_del_header
,
1167 php_http_env_response_stream_write
,
1168 php_http_env_response_stream_flush
,
1169 php_http_env_response_stream_finish
1172 php_http_env_response_ops_t
*php_http_env_response_get_stream_ops(void)
1174 return &php_http_env_response_stream_ops
;
1177 #define PHP_HTTP_ENV_RESPONSE_OBJECT_INIT(obj) \
1179 if (!obj->message) { \
1180 obj->message = php_http_message_init_env(NULL, PHP_HTTP_RESPONSE TSRMLS_CC); \
1184 ZEND_BEGIN_ARG_INFO_EX(ai_HttpEnvResponse___construct
, 0, 0, 0)
1185 ZEND_END_ARG_INFO();
1186 static PHP_METHOD(HttpEnvResponse
, __construct
)
1188 php_http_message_object_t
*obj
;
1190 php_http_expect(SUCCESS
== zend_parse_parameters_none(), invalid_arg
, return);
1192 obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1194 php_http_expect(obj
->message
= php_http_message_init_env(obj
->message
, PHP_HTTP_RESPONSE TSRMLS_CC
), unexpected_val
, return);
1197 ZEND_BEGIN_ARG_INFO_EX(ai_HttpEnvResponse___invoke
, 0, 0, 1)
1198 ZEND_ARG_INFO(0, ob_string
)
1199 ZEND_ARG_INFO(0, ob_flags
)
1200 ZEND_END_ARG_INFO();
1201 static PHP_METHOD(HttpEnvResponse
, __invoke
)
1207 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s|l", &ob_str
, &ob_len
, &ob_flags
)) {
1208 php_http_message_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1210 PHP_HTTP_ENV_RESPONSE_OBJECT_INIT(obj
);
1213 php_http_message_object_init_body_object(obj
);
1215 php_http_message_body_append(obj
->message
->body
, ob_str
, ob_len
);
1216 #if PHP_VERSION_ID >= 50400
1217 if (ob_flags
& PHP_OUTPUT_HANDLER_CLEAN
) {
1218 php_stream_truncate_set_size(php_http_message_body_stream(obj
->message
->body
), 0);
1222 RETURN_EMPTY_STRING();
1227 ZEND_BEGIN_ARG_INFO_EX(ai_HttpEnvResponse_setEnvRequest
, 0, 0, 1)
1228 ZEND_ARG_OBJ_INFO(0, env_request
, http
\\Message
, 1)
1229 ZEND_END_ARG_INFO();
1230 static PHP_METHOD(HttpEnvResponse
, setEnvRequest
)
1232 zval
*env_req
= NULL
;
1234 php_http_expect(SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|O", &env_req
, php_http_message_class_entry
), invalid_arg
, return);
1236 set_option(getThis(), ZEND_STRL("request"), IS_OBJECT
, env_req
, 0 TSRMLS_CC
);
1237 RETVAL_ZVAL(getThis(), 1, 0);
1240 ZEND_BEGIN_ARG_INFO_EX(ai_HttpEnvResponse_setContentType
, 0, 0, 1)
1241 ZEND_ARG_INFO(0, content_type
)
1242 ZEND_END_ARG_INFO();
1243 static PHP_METHOD(HttpEnvResponse
, setContentType
)
1245 char *ct_str
= NULL
;
1248 php_http_expect(SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s!", &ct_str
, &ct_len
), invalid_arg
, return);
1250 set_option(getThis(), ZEND_STRL("contentType"), IS_STRING
, ct_str
, ct_len TSRMLS_CC
);
1251 RETVAL_ZVAL(getThis(), 1, 0);
1254 ZEND_BEGIN_ARG_INFO_EX(ai_HttpEnvResponse_setContentDisposition
, 0, 0, 1)
1255 ZEND_ARG_ARRAY_INFO(0, disposition_params
, 1)
1256 ZEND_END_ARG_INFO();
1257 static PHP_METHOD(HttpEnvResponse
, setContentDisposition
)
1261 php_http_expect(SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "a", &zdisposition
), invalid_arg
, return);
1263 zend_update_property(Z_OBJCE_P(getThis()), getThis(), ZEND_STRL("contentDisposition"), zdisposition TSRMLS_CC
);
1264 RETVAL_ZVAL(getThis(), 1, 0);
1267 ZEND_BEGIN_ARG_INFO_EX(ai_HttpEnvResponse_setContentEncoding
, 0, 0, 1)
1268 ZEND_ARG_INFO(0, content_encoding
)
1269 ZEND_END_ARG_INFO();
1270 static PHP_METHOD(HttpEnvResponse
, setContentEncoding
)
1274 php_http_expect(SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "l", &ce
), invalid_arg
, return);
1276 set_option(getThis(), ZEND_STRL("contentEncoding"), IS_LONG
, &ce
, 0 TSRMLS_CC
);
1277 RETVAL_ZVAL(getThis(), 1, 0);
1280 ZEND_BEGIN_ARG_INFO_EX(ai_HttpEnvResponse_setCacheControl
, 0, 0, 1)
1281 ZEND_ARG_INFO(0, cache_control
)
1282 ZEND_END_ARG_INFO();
1283 static PHP_METHOD(HttpEnvResponse
, setCacheControl
)
1285 char *cc_str
= NULL
;
1288 php_http_expect(SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s!", &cc_str
, &cc_len
), invalid_arg
, return);
1290 set_option(getThis(), ZEND_STRL("cacheControl"), IS_STRING
, cc_str
, cc_len TSRMLS_CC
);
1291 RETVAL_ZVAL(getThis(), 1, 0);
1294 ZEND_BEGIN_ARG_INFO_EX(ai_HttpEnvResponse_setLastModified
, 0, 0, 1)
1295 ZEND_ARG_INFO(0, last_modified
)
1296 ZEND_END_ARG_INFO();
1297 static PHP_METHOD(HttpEnvResponse
, setLastModified
)
1301 php_http_expect(SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "l", &last_modified
), invalid_arg
, return);
1303 set_option(getThis(), ZEND_STRL("lastModified"), IS_LONG
, &last_modified
, 0 TSRMLS_CC
);
1304 RETVAL_ZVAL(getThis(), 1, 0);
1307 ZEND_BEGIN_ARG_INFO_EX(ai_HttpEnvResponse_isCachedByLastModified
, 0, 0, 0)
1308 ZEND_ARG_INFO(0, header_name
)
1309 ZEND_END_ARG_INFO();
1310 static PHP_METHOD(HttpEnvResponse
, isCachedByLastModified
)
1312 char *header_name_str
= NULL
;
1313 int header_name_len
= 0;
1315 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|s!", &header_name_str
, &header_name_len
)) {
1316 if (!header_name_str
|| !header_name_len
) {
1317 header_name_str
= "If-Modified-Since";
1318 header_name_len
= lenof("If-Modified-Since");
1321 RETURN_LONG(php_http_env_is_response_cached_by_last_modified(getThis(), header_name_str
, header_name_len
, get_request(getThis() TSRMLS_CC
) TSRMLS_CC
));
1325 ZEND_BEGIN_ARG_INFO_EX(ai_HttpEnvResponse_setEtag
, 0, 0, 1)
1326 ZEND_ARG_INFO(0, etag
)
1327 ZEND_END_ARG_INFO();
1328 static PHP_METHOD(HttpEnvResponse
, setEtag
)
1330 char *etag_str
= NULL
;
1333 php_http_expect(SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s!", &etag_str
, &etag_len
), invalid_arg
, return);
1335 set_option(getThis(), ZEND_STRL("etag"), IS_STRING
, etag_str
, etag_len TSRMLS_CC
);
1336 RETVAL_ZVAL(getThis(), 1, 0);
1339 ZEND_BEGIN_ARG_INFO_EX(ai_HttpEnvResponse_isCachedByEtag
, 0, 0, 0)
1340 ZEND_ARG_INFO(0, header_name
)
1341 ZEND_END_ARG_INFO();
1342 static PHP_METHOD(HttpEnvResponse
, isCachedByEtag
)
1344 char *header_name_str
= NULL
;
1345 int header_name_len
= 0;
1347 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|s!", &header_name_str
, &header_name_len
)) {
1348 if (!header_name_str
|| !header_name_len
) {
1349 header_name_str
= "If-None-Match";
1350 header_name_len
= lenof("If-None-Match");
1352 RETURN_LONG(php_http_env_is_response_cached_by_etag(getThis(), header_name_str
, header_name_len
, get_request(getThis() TSRMLS_CC
) TSRMLS_CC
));
1356 ZEND_BEGIN_ARG_INFO_EX(ai_HttpEnvResponse_setThrottleRate
, 0, 0, 1)
1357 ZEND_ARG_INFO(0, chunk_size
)
1358 ZEND_ARG_INFO(0, delay
)
1359 ZEND_END_ARG_INFO();
1360 static PHP_METHOD(HttpEnvResponse
, setThrottleRate
)
1365 php_http_expect(SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "l|d", &chunk_size
, &delay
), invalid_arg
, return);
1367 set_option(getThis(), ZEND_STRL("throttleDelay"), IS_DOUBLE
, &delay
, 0 TSRMLS_CC
);
1368 set_option(getThis(), ZEND_STRL("throttleChunk"), IS_LONG
, &chunk_size
, 0 TSRMLS_CC
);
1369 RETVAL_ZVAL(getThis(), 1, 0);
1372 ZEND_BEGIN_ARG_INFO_EX(ai_HttpEnvResponse_setCookie
, 0, 0, 1)
1373 ZEND_ARG_INFO(0, cookie
)
1374 ZEND_END_ARG_INFO();
1375 static PHP_METHOD(HttpEnvResponse
, setCookie
)
1378 zend_error_handling zeh
;
1379 php_http_cookie_list_t
*list
= NULL
;
1381 php_http_expect(SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "z", &zcookie_new
), invalid_arg
, return);
1383 zend_replace_error_handling(EH_THROW
, php_http_exception_unexpected_val_class_entry
, &zeh TSRMLS_CC
);
1384 switch (Z_TYPE_P(zcookie_new
)) {
1386 if (instanceof_function(Z_OBJCE_P(zcookie_new
), php_http_cookie_class_entry TSRMLS_CC
)) {
1387 Z_ADDREF_P(zcookie_new
);
1392 list
= php_http_cookie_list_from_struct(NULL
, zcookie_new TSRMLS_CC
);
1393 MAKE_STD_ZVAL(zcookie_new
);
1394 ZVAL_OBJVAL(zcookie_new
, php_http_cookie_object_new_ex(php_http_cookie_class_entry
, list
, NULL TSRMLS_CC
), 0);
1398 zcookie_new
= php_http_ztyp(IS_STRING
, zcookie_new
);
1399 list
= php_http_cookie_list_parse(NULL
, Z_STRVAL_P(zcookie_new
), Z_STRLEN_P(zcookie_new
), 0, NULL TSRMLS_CC
);
1400 zval_ptr_dtor(&zcookie_new
);
1401 MAKE_STD_ZVAL(zcookie_new
);
1402 ZVAL_OBJVAL(zcookie_new
, php_http_cookie_object_new_ex(php_http_cookie_class_entry
, list
, NULL TSRMLS_CC
), 0);
1404 zend_restore_error_handling(&zeh TSRMLS_CC
);
1406 set_cookie(getThis(), zcookie_new TSRMLS_CC
);
1407 zval_ptr_dtor(&zcookie_new
);
1409 RETVAL_ZVAL(getThis(), 1, 0);
1412 ZEND_BEGIN_ARG_INFO_EX(ai_HttpEnvResponse_send
, 0, 0, 0)
1413 ZEND_ARG_INFO(0, stream
)
1414 ZEND_END_ARG_INFO();
1415 static PHP_METHOD(HttpEnvResponse
, send
)
1417 zval
*zstream
= NULL
;
1418 php_stream
*s
= NULL
;
1420 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|r", &zstream
)) {
1421 /* first flush the output layer to avoid conflicting headers and output;
1422 * also, ob_start($thisEnvResponse) might have been called */
1423 #if PHP_VERSION_ID >= 50400
1424 php_output_end_all(TSRMLS_C
);
1426 php_end_ob_buffers(1 TSRMLS_CC
);
1430 php_http_env_response_t
*r
;
1432 php_stream_from_zval(s
, &zstream
);
1433 r
= php_http_env_response_init(NULL
, getThis(), php_http_env_response_get_stream_ops(), s TSRMLS_CC
);
1438 RETVAL_BOOL(SUCCESS
== php_http_env_response_send(r
));
1439 php_http_env_response_free(&r
);
1441 php_http_env_response_t r
;
1443 if (!php_http_env_response_init(&r
, getThis(), NULL
, NULL TSRMLS_CC
)) {
1447 RETVAL_BOOL(SUCCESS
== php_http_env_response_send(&r
));
1448 php_http_env_response_dtor(&r
);
1453 static zend_function_entry php_http_env_response_methods
[] = {
1454 PHP_ME(HttpEnvResponse
, __construct
, ai_HttpEnvResponse___construct
, ZEND_ACC_PUBLIC
|ZEND_ACC_CTOR
)
1455 PHP_ME(HttpEnvResponse
, __invoke
, ai_HttpEnvResponse___invoke
, ZEND_ACC_PUBLIC
)
1456 PHP_ME(HttpEnvResponse
, setEnvRequest
, ai_HttpEnvResponse_setEnvRequest
, ZEND_ACC_PUBLIC
)
1457 PHP_ME(HttpEnvResponse
, setCookie
, ai_HttpEnvResponse_setCookie
, ZEND_ACC_PUBLIC
)
1458 PHP_ME(HttpEnvResponse
, setContentType
, ai_HttpEnvResponse_setContentType
, ZEND_ACC_PUBLIC
)
1459 PHP_ME(HttpEnvResponse
, setContentDisposition
, ai_HttpEnvResponse_setContentDisposition
, ZEND_ACC_PUBLIC
)
1460 PHP_ME(HttpEnvResponse
, setContentEncoding
, ai_HttpEnvResponse_setContentEncoding
, ZEND_ACC_PUBLIC
)
1461 PHP_ME(HttpEnvResponse
, setCacheControl
, ai_HttpEnvResponse_setCacheControl
, ZEND_ACC_PUBLIC
)
1462 PHP_ME(HttpEnvResponse
, setLastModified
, ai_HttpEnvResponse_setLastModified
, ZEND_ACC_PUBLIC
)
1463 PHP_ME(HttpEnvResponse
, isCachedByLastModified
, ai_HttpEnvResponse_isCachedByLastModified
, ZEND_ACC_PUBLIC
)
1464 PHP_ME(HttpEnvResponse
, setEtag
, ai_HttpEnvResponse_setEtag
, ZEND_ACC_PUBLIC
)
1465 PHP_ME(HttpEnvResponse
, isCachedByEtag
, ai_HttpEnvResponse_isCachedByEtag
, ZEND_ACC_PUBLIC
)
1466 PHP_ME(HttpEnvResponse
, setThrottleRate
, ai_HttpEnvResponse_setThrottleRate
, ZEND_ACC_PUBLIC
)
1467 PHP_ME(HttpEnvResponse
, send
, ai_HttpEnvResponse_send
, ZEND_ACC_PUBLIC
)
1468 EMPTY_FUNCTION_ENTRY
1471 zend_class_entry
*php_http_env_response_class_entry
;
1473 PHP_MINIT_FUNCTION(http_env_response
)
1475 zend_class_entry ce
= {0};
1477 INIT_NS_CLASS_ENTRY(ce
, "http\\Env", "Response", php_http_env_response_methods
);
1478 php_http_env_response_class_entry
= zend_register_internal_class_ex(&ce
, php_http_message_class_entry
, NULL TSRMLS_CC
);
1480 zend_declare_class_constant_long(php_http_env_response_class_entry
, ZEND_STRL("CONTENT_ENCODING_NONE"), PHP_HTTP_CONTENT_ENCODING_NONE TSRMLS_CC
);
1481 zend_declare_class_constant_long(php_http_env_response_class_entry
, ZEND_STRL("CONTENT_ENCODING_GZIP"), PHP_HTTP_CONTENT_ENCODING_GZIP TSRMLS_CC
);
1483 zend_declare_class_constant_long(php_http_env_response_class_entry
, ZEND_STRL("CACHE_NO"), PHP_HTTP_CACHE_NO TSRMLS_CC
);
1484 zend_declare_class_constant_long(php_http_env_response_class_entry
, ZEND_STRL("CACHE_HIT"), PHP_HTTP_CACHE_HIT TSRMLS_CC
);
1485 zend_declare_class_constant_long(php_http_env_response_class_entry
, ZEND_STRL("CACHE_MISS"), PHP_HTTP_CACHE_MISS TSRMLS_CC
);
1487 zend_declare_property_null(php_http_env_response_class_entry
, ZEND_STRL("request"), ZEND_ACC_PROTECTED TSRMLS_CC
);
1488 zend_declare_property_null(php_http_env_response_class_entry
, ZEND_STRL("cookies"), ZEND_ACC_PROTECTED TSRMLS_CC
);
1489 zend_declare_property_null(php_http_env_response_class_entry
, ZEND_STRL("contentType"), ZEND_ACC_PROTECTED TSRMLS_CC
);
1490 zend_declare_property_null(php_http_env_response_class_entry
, ZEND_STRL("contentDisposition"), ZEND_ACC_PROTECTED TSRMLS_CC
);
1491 zend_declare_property_null(php_http_env_response_class_entry
, ZEND_STRL("contentEncoding"), ZEND_ACC_PROTECTED TSRMLS_CC
);
1492 zend_declare_property_null(php_http_env_response_class_entry
, ZEND_STRL("cacheControl"), ZEND_ACC_PROTECTED TSRMLS_CC
);
1493 zend_declare_property_null(php_http_env_response_class_entry
, ZEND_STRL("etag"), ZEND_ACC_PROTECTED TSRMLS_CC
);
1494 zend_declare_property_null(php_http_env_response_class_entry
, ZEND_STRL("lastModified"), ZEND_ACC_PROTECTED TSRMLS_CC
);
1495 zend_declare_property_null(php_http_env_response_class_entry
, ZEND_STRL("throttleDelay"), ZEND_ACC_PROTECTED TSRMLS_CC
);
1496 zend_declare_property_null(php_http_env_response_class_entry
, ZEND_STRL("throttleChunk"), ZEND_ACC_PROTECTED TSRMLS_CC
);
1507 * vim600: noet sw=4 ts=4 fdm=marker
1508 * vim<600: noet sw=4 ts=4