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
)
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
);
24 zend_update_property_long(Z_OBJCE_P(options
), options
, name_str
, name_len
, *(zend_long
*)value_ptr
);
27 zend_update_property_stringl(Z_OBJCE_P(options
), options
, name_str
, name_len
, value_ptr
, value_len
);
31 zend_update_property(Z_OBJCE_P(options
), options
, name_str
, name_len
, value_ptr
);
35 zend_update_property_null(Z_OBJCE_P(options
), options
, name_str
, name_len
);
38 convert_to_array(options
);
42 add_assoc_double_ex(options
, name_str
, name_len
, *(double *)value_ptr
);
45 add_assoc_long_ex(options
, name_str
, name_len
, *(zend_long
*)value_ptr
);
48 zend_string
*value
= zend_string_init(value_ptr
, value_len
, 0);
49 add_assoc_str_ex(options
, name_str
, name_len
, value
);
53 Z_ADDREF_P(value_ptr
);
54 add_assoc_zval_ex(options
, name_str
, name_len
, value_ptr
);
59 add_assoc_null_ex(options
, name_str
, name_len
);
63 static zval
*get_option(zval
*options
, const char *name_str
, size_t name_len
, zval
*tmp
)
67 if (Z_TYPE_P(options
) == IS_OBJECT
) {
68 val
= zend_read_property(Z_OBJCE_P(options
), options
, name_str
, name_len
, 0, tmp
);
69 } else if (Z_TYPE_P(options
) == IS_ARRAY
) {
70 val
= zend_symtable_str_find(Z_ARRVAL_P(options
), name_str
, name_len
);
79 static php_http_message_body_t
*get_body(zval
*options
)
81 zval zbody_tmp
, *zbody
;
82 php_http_message_body_t
*body
= NULL
;
84 if ((zbody
= get_option(options
, ZEND_STRL("body"), &zbody_tmp
))) {
85 if ((Z_TYPE_P(zbody
) == IS_OBJECT
) && instanceof_function(Z_OBJCE_P(zbody
), php_http_get_message_body_class_entry())) {
86 php_http_message_body_object_t
*body_obj
= PHP_HTTP_OBJ(NULL
, zbody
);
88 body
= body_obj
->body
;
90 Z_TRY_DELREF_P(zbody
);
95 static php_http_message_t
*get_request(zval
*options
)
97 zval zrequest_tmp
, *zrequest
;
98 php_http_message_t
*request
= NULL
;
100 if ((zrequest
= get_option(options
, ZEND_STRL("request"), &zrequest_tmp
))) {
101 if (Z_TYPE_P(zrequest
) == IS_OBJECT
&& instanceof_function(Z_OBJCE_P(zrequest
), php_http_message_get_class_entry())) {
102 php_http_message_object_t
*request_obj
= PHP_HTTP_OBJ(NULL
, zrequest
);
104 request
= request_obj
->message
;
106 Z_TRY_DELREF_P(zrequest
);
111 static void set_cookie(zval
*options
, zval
*zcookie_new
)
113 zval tmp
, zcookies_set_tmp
, *zcookies_set
;
114 php_http_arrkey_t key
;
115 php_http_cookie_object_t
*obj
= PHP_HTTP_OBJ(NULL
, zcookie_new
);
118 zcookies_set
= get_option(options
, ZEND_STRL("cookies"), &zcookies_set_tmp
);
119 if (zcookies_set
&& Z_TYPE_P(zcookies_set
) == IS_ARRAY
) {
120 array_copy(Z_ARRVAL_P(zcookies_set
), Z_ARRVAL(tmp
));
121 zval_ptr_dtor(zcookies_set
);
124 ZEND_HASH_FOREACH_KEY(&obj
->list
->cookies
, key
.h
, key
.key
)
126 Z_ADDREF_P(zcookie_new
);
128 add_assoc_zval_ex(&tmp
, key
.key
->val
, key
.key
->len
, zcookie_new
);
130 add_index_zval(&tmp
, key
.h
, zcookie_new
);
133 ZEND_HASH_FOREACH_END();
135 set_option(options
, ZEND_STRL("cookies"), IS_ARRAY
, &tmp
, 0);
139 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
)
141 php_http_cache_status_t ret
= PHP_HTTP_CACHE_NO
;
142 char *header
= NULL
, *etag
= NULL
;
143 php_http_message_body_t
*body
;
144 zval zetag_tmp
, *zetag
;
147 if (!(body
= get_body(options
))) {
151 if ((zetag
= get_option(options
, ZEND_STRL("etag"), &zetag_tmp
)) && Z_TYPE_P(zetag
) != IS_NULL
) {
152 zend_string
*zs
= zval_get_string(zetag
);
153 etag
= estrndup(zs
->val
, zs
->len
);
154 zend_string_release(zs
);
155 zval_ptr_dtor(zetag
);
158 if (!etag
&& (etag
= php_http_message_body_etag(body
))) {
159 set_option(options
, ZEND_STRL("etag"), IS_STRING
, etag
, strlen(etag
));
162 if (etag
&& (header
= php_http_env_get_request_header(header_str
, header_len
, NULL
, request
))) {
163 ret
= php_http_match(header
, etag
, PHP_HTTP_MATCH_WORD
) ? PHP_HTTP_CACHE_HIT
: PHP_HTTP_CACHE_MISS
;
172 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
)
174 php_http_cache_status_t ret
= PHP_HTTP_CACHE_NO
;
177 php_http_message_body_t
*body
;
180 if (!(body
= get_body(options
))) {
184 if ((zlm
= get_option(options
, ZEND_STRL("lastModified"), &zlm_tmp
))) {
185 lm
= zval_get_long(zlm
);
190 lm
= php_http_message_body_mtime(body
);
191 set_option(options
, ZEND_STRL("lastModified"), IS_LONG
, &lm
, 0);
194 if ((header
= php_http_env_get_request_header(header_str
, header_len
, NULL
, request
))) {
195 ums
= php_parse_date(header
, NULL
);
197 if (ums
> 0 && ums
>= lm
) {
198 ret
= PHP_HTTP_CACHE_HIT
;
200 ret
= PHP_HTTP_CACHE_MISS
;
208 static zend_bool
php_http_env_response_is_cacheable(php_http_env_response_t
*r
, php_http_message_t
*request
)
210 long status
= r
->ops
->get_status(r
);
212 if (status
&& status
/ 100 != 2) {
216 if (php_http_env_got_request_header(ZEND_STRL("Authorization"), request
)) {
220 if (-1 == php_http_select_str(php_http_env_get_request_method(request
), 2, "HEAD", "GET")) {
227 static size_t output(void *context
, char *buf
, size_t len
)
229 php_http_env_response_t
*r
= context
;
231 if (SUCCESS
!= r
->ops
->write(r
, buf
, len
)) {
235 /* we really only need to flush when throttling is enabled,
236 because we push the data as fast as possible anyway if not */
237 if (r
->throttle
.delay
>= PHP_HTTP_DIFFSEC
) {
239 php_http_sleep(r
->throttle
.delay
);
244 static ZEND_RESULT_CODE
php_http_env_response_send_data(php_http_env_response_t
*r
, const char *buf
, size_t len
)
246 size_t chunks_sent
, chunk
= r
->throttle
.chunk
? r
->throttle
.chunk
: PHP_HTTP_SENDBUF_SIZE
;
248 if (r
->content
.encoder
) {
249 char *enc_str
= NULL
;
253 if (SUCCESS
!= php_http_encoding_stream_update(r
->content
.encoder
, buf
, len
, &enc_str
, &enc_len
)) {
257 if (SUCCESS
!= php_http_encoding_stream_finish(r
->content
.encoder
, &enc_str
, &enc_len
)) {
265 chunks_sent
= php_http_buffer_chunked_output(&r
->buffer
, enc_str
, enc_len
, buf
? chunk
: 0, output
, r
);
268 chunks_sent
= php_http_buffer_chunked_output(&r
->buffer
, buf
, len
, buf
? chunk
: 0, output
, r
);
271 return chunks_sent
!= (size_t) -1 ? SUCCESS
: FAILURE
;
274 static inline ZEND_RESULT_CODE
php_http_env_response_send_done(php_http_env_response_t
*r
)
276 return php_http_env_response_send_data(r
, NULL
, 0);
279 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
)
284 r
= emalloc(sizeof(*r
));
286 memset(r
, 0, sizeof(*r
));
291 r
->ops
= php_http_env_response_get_sapi_ops();
294 r
->buffer
= php_http_buffer_init(NULL
);
296 ZVAL_COPY(&r
->options
, options
);
298 if (r
->ops
->init
&& (SUCCESS
!= r
->ops
->init(r
, init_arg
))) {
300 php_http_env_response_free(&r
);
302 php_http_env_response_dtor(r
);
310 void php_http_env_response_dtor(php_http_env_response_t
*r
)
315 php_http_buffer_free(&r
->buffer
);
316 zval_ptr_dtor(&r
->options
);
317 PTR_FREE(r
->content
.type
);
318 PTR_FREE(r
->content
.encoding
);
319 if (r
->content
.encoder
) {
320 php_http_encoding_stream_free(&r
->content
.encoder
);
324 void php_http_env_response_free(php_http_env_response_t
**r
)
327 php_http_env_response_dtor(*r
);
333 static ZEND_RESULT_CODE
php_http_env_response_send_head(php_http_env_response_t
*r
, php_http_message_t
*request
)
335 ZEND_RESULT_CODE ret
= SUCCESS
;
336 zval zoption_tmp
, *zoption
, *options
= &r
->options
;
342 if ((zoption
= get_option(options
, ZEND_STRL("headers"), &zoption_tmp
))) {
343 if (Z_TYPE_P(zoption
) == IS_ARRAY
) {
344 php_http_header_to_callback(Z_ARRVAL_P(zoption
), 0, (php_http_pass_format_callback_t
) r
->ops
->set_header
, r
);
346 zval_ptr_dtor(zoption
);
349 if (ret
!= SUCCESS
) {
353 if ((zoption
= get_option(options
, ZEND_STRL("responseCode"), &zoption_tmp
))) {
354 zend_long rc
= zval_get_long(zoption
);
356 zval_ptr_dtor(zoption
);
358 ret
= r
->ops
->set_status(r
, rc
);
362 if (ret
!= SUCCESS
) {
366 if ((zoption
= get_option(options
, ZEND_STRL("httpVersion"), &zoption_tmp
))) {
367 php_http_version_t v
;
368 zend_string
*zs
= zval_get_string(zoption
);
370 zval_ptr_dtor(zoption
);
371 if (zs
->len
&& php_http_version_parse(&v
, zs
->val
)) {
372 ret
= r
->ops
->set_protocol_version(r
, &v
);
373 php_http_version_dtor(&v
);
375 zend_string_release(zs
);
378 if (ret
!= SUCCESS
) {
382 if ((zoption
= get_option(options
, ZEND_STRL("cookies"), &zoption_tmp
))) {
383 if (Z_TYPE_P(zoption
) == IS_ARRAY
) {
386 ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(zoption
), zcookie
)
388 if (Z_TYPE_P(zcookie
) == IS_OBJECT
&& instanceof_function(Z_OBJCE_P(zcookie
), php_http_cookie_get_class_entry())) {
389 php_http_cookie_object_t
*obj
= PHP_HTTP_OBJ(NULL
, zcookie
);
393 php_http_cookie_list_to_string(obj
->list
, &str
, &len
);
394 if (SUCCESS
!= (ret
= r
->ops
->add_header(r
, "Set-Cookie: %s", str
))) {
401 ZEND_HASH_FOREACH_END();
403 zval_ptr_dtor(zoption
);
406 if (ret
!= SUCCESS
) {
410 if ((zoption
= get_option(options
, ZEND_STRL("contentType"), &zoption_tmp
))) {
411 zend_string
*zs
= zval_get_string(zoption
);
413 zval_ptr_dtor(zoption
);
414 if (zs
->len
&& strchr(zs
->val
, '/')) {
415 if (SUCCESS
== (ret
= r
->ops
->set_header(r
, "Content-Type: %.*s", zs
->len
, zs
->val
))) {
416 r
->content
.type
= estrndup(zs
->val
, zs
->len
);
419 zend_string_release(zs
);
422 if (ret
!= SUCCESS
) {
426 if (r
->range
.status
== PHP_HTTP_RANGE_OK
) {
427 if (zend_hash_num_elements(&r
->range
.values
) == 1) {
428 zval
*range
, *begin
, *end
;
430 if ( 1 == php_http_array_list(&r
->range
.values
, 1, &range
)
431 && 2 == php_http_array_list(Z_ARRVAL_P(range
), 2, &begin
, &end
)
433 if (SUCCESS
== (ret
= r
->ops
->set_status(r
, 206))) {
434 ret
= r
->ops
->set_header(r
, "Content-Range: bytes %ld-%ld/%zu", Z_LVAL_P(begin
), Z_LVAL_P(end
), r
->content
.length
);
437 /* this should never happen */
438 zend_hash_destroy(&r
->range
.values
);
442 php_http_boundary(r
->range
.boundary
, sizeof(r
->range
.boundary
));
443 if (SUCCESS
== (ret
= r
->ops
->set_status(r
, 206))) {
444 ret
= r
->ops
->set_header(r
, "Content-Type: multipart/byteranges; boundary=%s", r
->range
.boundary
);
448 if ((zoption
= get_option(options
, ZEND_STRL("cacheControl"), &zoption_tmp
))) {
449 zend_string
*zs
= zval_get_string(zoption
);
451 zval_ptr_dtor(zoption
);
453 ret
= r
->ops
->set_header(r
, "Cache-Control: %.*s", zs
->len
, zs
->val
);
455 zend_string_release(zs
);
458 if (ret
!= SUCCESS
) {
462 if ((zoption
= get_option(options
, ZEND_STRL("contentDisposition"), &zoption_tmp
))) {
464 if (Z_TYPE_P(zoption
) == IS_ARRAY
) {
465 php_http_buffer_t buf
;
467 php_http_buffer_init(&buf
);
468 if (php_http_params_to_string(&buf
, Z_ARRVAL_P(zoption
), ZEND_STRL(","), ZEND_STRL(";"), ZEND_STRL("="), PHP_HTTP_PARAMS_DEFAULT
)) {
470 ret
= r
->ops
->set_header(r
, "Content-Disposition: %.*s", buf
.used
, buf
.data
);
474 php_http_buffer_dtor(&buf
);
476 zval_ptr_dtor(zoption
);
479 if (ret
!= SUCCESS
) {
483 if ((zoption
= get_option(options
, ZEND_STRL("contentEncoding"), &zoption_tmp
))) {
484 zend_long ce
= zval_get_long(zoption
);
486 HashTable
*result
= NULL
;
488 zval_ptr_dtor(zoption
);
490 case PHP_HTTP_CONTENT_ENCODING_GZIP
:
491 array_init(&zsupported
);
492 add_next_index_stringl(&zsupported
, ZEND_STRL("none"));
493 add_next_index_stringl(&zsupported
, ZEND_STRL("gzip"));
494 add_next_index_stringl(&zsupported
, ZEND_STRL("deflate"));
496 if ((result
= php_http_negotiate_encoding(Z_ARRVAL(zsupported
), request
))) {
497 zend_string
*key_str
= NULL
;
498 zend_ulong index
= 0;
500 zend_hash_internal_pointer_reset(result
);
501 if (HASH_KEY_IS_STRING
== zend_hash_get_current_key(result
, &key_str
, &index
)) {
502 if (zend_string_equals_literal(key_str
, "gzip")) {
503 if (!(r
->content
.encoder
= php_http_encoding_stream_init(NULL
, php_http_encoding_stream_get_deflate_ops(), PHP_HTTP_DEFLATE_TYPE_GZIP
))) {
505 } else if (SUCCESS
== (ret
= r
->ops
->set_header(r
, "Content-Encoding: gzip"))) {
506 r
->content
.encoding
= estrndup(key_str
->val
, key_str
->len
);
508 } else if (zend_string_equals_literal(key_str
, "deflate")) {
509 if (!(r
->content
.encoder
= php_http_encoding_stream_init(NULL
, php_http_encoding_stream_get_deflate_ops(), PHP_HTTP_DEFLATE_TYPE_ZLIB
))) {
511 } else if (SUCCESS
== (ret
= r
->ops
->set_header(r
, "Content-Encoding: deflate"))) {
512 r
->content
.encoding
= estrndup(key_str
->val
, key_str
->len
);
515 ret
= r
->ops
->del_header(r
, ZEND_STRL("Content-Encoding"));
518 if (SUCCESS
== ret
) {
519 ret
= r
->ops
->add_header(r
, "Vary: Accept-Encoding");
523 zend_hash_destroy(result
);
524 FREE_HASHTABLE(result
);
527 zval_dtor(&zsupported
);
530 case PHP_HTTP_CONTENT_ENCODING_NONE
:
532 ret
= r
->ops
->del_header(r
, ZEND_STRL("Content-Encoding"));
537 if (SUCCESS
!= ret
) {
541 if (php_http_env_response_is_cacheable(r
, request
)) {
542 switch (php_http_env_is_response_cached_by_etag(options
, ZEND_STRL("If-None-Match"), request
)) {
543 case PHP_HTTP_CACHE_MISS
:
546 case PHP_HTTP_CACHE_NO
:
547 if (PHP_HTTP_CACHE_HIT
!= php_http_env_is_response_cached_by_last_modified(options
, ZEND_STRL("If-Modified-Since"), request
)) {
552 case PHP_HTTP_CACHE_HIT
:
553 ret
= r
->ops
->set_status(r
, 304);
558 if ((zoption
= get_option(options
, ZEND_STRL("etag"), &zoption_tmp
))) {
559 zend_string
*zs
= zval_get_string(zoption
);
561 zval_ptr_dtor(zoption
);
562 if (*zs
->val
!= '"' && strncmp(zs
->val
, "W/\"", 3)) {
563 ret
= r
->ops
->set_header(r
, "ETag: \"%s\"", zs
->val
);
565 ret
= r
->ops
->set_header(r
, "ETag: %s", zs
->val
);
567 zend_string_release(zs
);
569 if ((zoption
= get_option(options
, ZEND_STRL("lastModified"), &zoption_tmp
))) {
570 zend_long lm
= zval_get_long(zoption
);
572 zval_ptr_dtor(zoption
);
574 zend_string
*date
= php_format_date(ZEND_STRL(PHP_HTTP_DATE_FORMAT
), lm
, 0);
576 ret
= r
->ops
->set_header(r
, "Last-Modified: %s", date
->val
);
577 zend_string_release(date
);
587 static ZEND_RESULT_CODE
php_http_env_response_send_body(php_http_env_response_t
*r
)
589 ZEND_RESULT_CODE ret
= SUCCESS
;
590 zval zoption_tmp
, *zoption
;
591 php_http_message_body_t
*body
;
597 if ((body
= get_body(&r
->options
))) {
598 if ((zoption
= get_option(&r
->options
, ZEND_STRL("throttleDelay"), &zoption_tmp
))) {
599 r
->throttle
.delay
= zval_get_double(zoption
);
600 zval_ptr_dtor(zoption
);
602 if ((zoption
= get_option(&r
->options
, ZEND_STRL("throttleChunk"), &zoption_tmp
))) {
603 r
->throttle
.chunk
= zval_get_long(zoption
);
604 zval_ptr_dtor(zoption
);
607 if (r
->range
.status
== PHP_HTTP_RANGE_OK
) {
608 if (zend_hash_num_elements(&r
->range
.values
) == 1) {
610 zval
*range
, *begin
, *end
;
612 if ( 1 == php_http_array_list(&r
->range
.values
, 1, &range
)
613 && 2 == php_http_array_list(Z_ARRVAL_P(range
), 2, &begin
, &end
)
616 ret
= php_http_message_body_to_callback(body
, (php_http_pass_callback_t
) php_http_env_response_send_data
, r
, Z_LVAL_P(begin
), Z_LVAL_P(end
) - Z_LVAL_P(begin
) + 1);
617 if (ret
== SUCCESS
) {
618 ret
= php_http_env_response_send_done(r
);
620 zend_hash_destroy(&r
->range
.values
);
622 /* this should never happen */
623 zend_hash_destroy(&r
->range
.values
);
624 r
->ops
->set_status(r
, 500);
629 /* send multipart/byte-ranges message */
632 ZEND_HASH_FOREACH_VAL(&r
->range
.values
, chunk
)
636 if (2 == php_http_array_list(Z_ARRVAL_P(chunk
), 2, &begin
, &end
)) {
637 php_http_buffer_appendf(r
->buffer
,
640 "Content-Type: %s" PHP_HTTP_CRLF
641 "Content-Range: bytes %ld-%ld/%zu" PHP_HTTP_CRLF PHP_HTTP_CRLF
,
644 r
->content
.type
? r
->content
.type
: "application/octet-stream",
649 ret
= php_http_message_body_to_callback(body
, (php_http_pass_callback_t
) php_http_env_response_send_data
, r
, Z_LVAL_P(begin
), Z_LVAL_P(end
) - Z_LVAL_P(begin
) + 1);
652 ZEND_HASH_FOREACH_END();
654 if (ret
== SUCCESS
) {
655 php_http_buffer_appendf(r
->buffer
, PHP_HTTP_CRLF
"--%s--", r
->range
.boundary
);
656 ret
= php_http_env_response_send_done(r
);
658 zend_hash_destroy(&r
->range
.values
);
662 ret
= php_http_message_body_to_callback(body
, (php_http_pass_callback_t
) php_http_env_response_send_data
, r
, 0, 0);
663 if (ret
== SUCCESS
) {
664 ret
= php_http_env_response_send_done(r
);
671 ZEND_RESULT_CODE
php_http_env_response_send(php_http_env_response_t
*r
)
673 php_http_message_t
*request
;
674 php_http_message_body_t
*body
;
676 request
= get_request(&r
->options
);
678 /* check for ranges */
679 if ((body
= get_body(&r
->options
))) {
680 r
->content
.length
= php_http_message_body_size(body
);
682 if (SUCCESS
!= r
->ops
->set_header(r
, "Accept-Ranges: bytes")) {
685 ZEND_INIT_SYMTABLE_EX(&r
->range
.values
, 0, 0);
686 r
->range
.status
= php_http_env_get_request_ranges(&r
->range
.values
, r
->content
.length
, request
);
688 switch (r
->range
.status
) {
689 case PHP_HTTP_RANGE_NO
:
690 zend_hash_destroy(&r
->range
.values
);
693 case PHP_HTTP_RANGE_ERR
:
694 if (php_http_env_got_request_header(ZEND_STRL("If-Range"), request
)) {
695 r
->range
.status
= PHP_HTTP_RANGE_NO
;
696 zend_hash_destroy(&r
->range
.values
);
699 zend_hash_destroy(&r
->range
.values
);
700 if (SUCCESS
!= r
->ops
->set_status(r
, 416)) {
703 if (SUCCESS
!= r
->ops
->set_header(r
, "Content-Range: bytes */%zu", r
->content
.length
)) {
709 case PHP_HTTP_RANGE_OK
:
710 if (PHP_HTTP_CACHE_MISS
== php_http_env_is_response_cached_by_etag(&r
->options
, ZEND_STRL("If-Range"), request
)
711 || PHP_HTTP_CACHE_MISS
== php_http_env_is_response_cached_by_last_modified(&r
->options
, ZEND_STRL("If-Range"), request
)
713 r
->range
.status
= PHP_HTTP_RANGE_NO
;
714 zend_hash_destroy(&r
->range
.values
);
717 if (PHP_HTTP_CACHE_MISS
== php_http_env_is_response_cached_by_etag(&r
->options
, ZEND_STRL("If-Match"), request
)
718 || PHP_HTTP_CACHE_MISS
== php_http_env_is_response_cached_by_last_modified(&r
->options
, ZEND_STRL("If-Unmodified-Since"), request
)
719 || PHP_HTTP_CACHE_MISS
== php_http_env_is_response_cached_by_last_modified(&r
->options
, ZEND_STRL("Unless-Modified-Since"), request
)
722 zend_hash_destroy(&r
->range
.values
);
723 if (SUCCESS
!= r
->ops
->set_status(r
, 412)) {
734 if (SUCCESS
!= php_http_env_response_send_head(r
, request
)) {
735 php_error_docref(NULL
, E_WARNING
, "Failed to send response headers");
739 if (SUCCESS
!= php_http_env_response_send_body(r
)) {
740 php_error_docref(NULL
, E_WARNING
, "Failed to send response body");
744 if (SUCCESS
!= r
->ops
->finish(r
)) {
745 php_error_docref(NULL
, E_WARNING
, "Failed to finish response");
752 static long php_http_env_response_sapi_get_status(php_http_env_response_t
*r
)
754 return php_http_env_get_response_code();
756 static ZEND_RESULT_CODE
php_http_env_response_sapi_set_status(php_http_env_response_t
*r
, long http_code
)
758 return php_http_env_set_response_code(http_code
);
760 static ZEND_RESULT_CODE
php_http_env_response_sapi_set_protocol_version(php_http_env_response_t
*r
, php_http_version_t
*v
)
763 return php_http_env_set_response_protocol_version(v
);
765 static ZEND_RESULT_CODE
php_http_env_response_sapi_set_header(php_http_env_response_t
*r
, const char *fmt
, ...)
767 ZEND_RESULT_CODE ret
;
771 ret
= php_http_env_set_response_header_va(0, 1, fmt
, args
);
776 static ZEND_RESULT_CODE
php_http_env_response_sapi_add_header(php_http_env_response_t
*r
, const char *fmt
, ...)
778 ZEND_RESULT_CODE ret
;
782 ret
= php_http_env_set_response_header_va(0, 0, fmt
, args
);
787 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
)
789 return php_http_env_set_response_header_value(0, header_str
, header_len
, NULL
, 1);
791 static ZEND_RESULT_CODE
php_http_env_response_sapi_write(php_http_env_response_t
*r
, const char *data_str
, size_t data_len
)
793 if (0 < PHPWRITE(data_str
, data_len
)) {
798 static ZEND_RESULT_CODE
php_http_env_response_sapi_flush(php_http_env_response_t
*r
)
800 if (php_output_get_level()) {
801 php_output_flush_all();
803 if (!(php_output_get_status() & PHP_OUTPUT_IMPLICITFLUSH
)) {
809 static ZEND_RESULT_CODE
php_http_env_response_sapi_finish(php_http_env_response_t
*r
)
814 static php_http_env_response_ops_t php_http_env_response_sapi_ops
= {
817 php_http_env_response_sapi_get_status
,
818 php_http_env_response_sapi_set_status
,
819 php_http_env_response_sapi_set_protocol_version
,
820 php_http_env_response_sapi_set_header
,
821 php_http_env_response_sapi_add_header
,
822 php_http_env_response_sapi_del_header
,
823 php_http_env_response_sapi_write
,
824 php_http_env_response_sapi_flush
,
825 php_http_env_response_sapi_finish
828 php_http_env_response_ops_t
*php_http_env_response_get_sapi_ops(void)
830 return &php_http_env_response_sapi_ops
;
833 typedef struct php_http_env_response_stream_ctx
{
835 php_http_version_t version
;
839 php_stream_filter
*chunked_filter
;
840 php_http_message_t
*request
;
845 } php_http_env_response_stream_ctx_t
;
847 static ZEND_RESULT_CODE
php_http_env_response_stream_init(php_http_env_response_t
*r
, void *init_arg
)
849 php_http_env_response_stream_ctx_t
*ctx
;
850 size_t buffer_size
= 0x1000;
852 ctx
= ecalloc(1, sizeof(*ctx
));
854 ctx
->stream
= init_arg
;
855 ++GC_REFCOUNT(ctx
->stream
->res
);
856 ZEND_INIT_SYMTABLE(&ctx
->header
);
857 php_http_version_init(&ctx
->version
, 1, 1);
858 php_stream_set_option(ctx
->stream
, PHP_STREAM_OPTION_WRITE_BUFFER
, PHP_STREAM_BUFFER_FULL
, &buffer_size
);
859 ctx
->status_code
= 200;
861 ctx
->request
= get_request(&r
->options
);
863 /* there are some limitations regarding TE:chunked, see https://tools.ietf.org/html/rfc7230#section-3.3.1 */
864 if (ctx
->request
&& ctx
->request
->http
.version
.major
== 1 && ctx
->request
->http
.version
.minor
== 0) {
865 ctx
->version
.minor
= 0;
872 static void php_http_env_response_stream_dtor(php_http_env_response_t
*r
)
874 php_http_env_response_stream_ctx_t
*ctx
= r
->ctx
;
876 if (ctx
->chunked_filter
) {
877 ctx
->chunked_filter
= php_stream_filter_remove(ctx
->chunked_filter
, 1);
879 zend_hash_destroy(&ctx
->header
);
880 zend_list_delete(ctx
->stream
->res
);
884 static void php_http_env_response_stream_header(php_http_env_response_stream_ctx_t
*ctx
, HashTable
*header
, php_http_buffer_t
*buf
)
888 ZEND_HASH_FOREACH_VAL(header
, val
)
890 if (Z_TYPE_P(val
) == IS_ARRAY
) {
891 php_http_env_response_stream_header(ctx
, Z_ARRVAL_P(val
), buf
);
893 zend_string
*zs
= zval_get_string(val
);
896 /* disable chunked transfer encoding if we've got an explicit content-length */
897 if (!strncasecmp(zs
->val
, "Content-Length:", lenof("Content-Length:"))) {
901 php_http_buffer_append(buf
, zs
->val
, zs
->len
);
902 php_http_buffer_appends(buf
, PHP_HTTP_CRLF
);
903 zend_string_release(zs
);
906 ZEND_HASH_FOREACH_END();
908 static ZEND_RESULT_CODE
php_http_env_response_stream_start(php_http_env_response_stream_ctx_t
*ctx
)
910 php_http_buffer_t header_buf
;
912 if (ctx
->started
|| ctx
->finished
) {
916 php_http_buffer_init(&header_buf
);
917 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
));
919 /* there are some limitations regarding TE:chunked, see https://tools.ietf.org/html/rfc7230#section-3.3.1 */
920 if (ctx
->version
.major
== 1 && ctx
->version
.minor
== 0) {
922 } else if (ctx
->status_code
== 204 || ctx
->status_code
/100 == 1) {
924 } else if (ctx
->request
&& ctx
->status_code
/100 == 2 && !strcasecmp(ctx
->request
->http
.info
.request
.method
, "CONNECT")) {
928 php_http_env_response_stream_header(ctx
, &ctx
->header
, &header_buf
);
930 /* enable chunked transfer encoding */
932 php_http_buffer_appends(&header_buf
, "Transfer-Encoding: chunked" PHP_HTTP_CRLF
);
935 php_http_buffer_appends(&header_buf
, PHP_HTTP_CRLF
);
937 if (header_buf
.used
== php_stream_write(ctx
->stream
, header_buf
.data
, header_buf
.used
)) {
940 php_http_buffer_dtor(&header_buf
);
941 php_stream_flush(ctx
->stream
);
944 ctx
->chunked_filter
= php_stream_filter_create("http.chunked_encode", NULL
, 0);
945 php_stream_filter_append(&ctx
->stream
->writefilters
, ctx
->chunked_filter
);
948 return ctx
->started
? SUCCESS
: FAILURE
;
950 static long php_http_env_response_stream_get_status(php_http_env_response_t
*r
)
952 php_http_env_response_stream_ctx_t
*ctx
= r
->ctx
;
954 return ctx
->status_code
;
956 static ZEND_RESULT_CODE
php_http_env_response_stream_set_status(php_http_env_response_t
*r
, long http_code
)
958 php_http_env_response_stream_ctx_t
*stream_ctx
= r
->ctx
;
960 if (stream_ctx
->started
|| stream_ctx
->finished
) {
964 stream_ctx
->status_code
= http_code
;
968 static ZEND_RESULT_CODE
php_http_env_response_stream_set_protocol_version(php_http_env_response_t
*r
, php_http_version_t
*v
)
970 php_http_env_response_stream_ctx_t
*stream_ctx
= r
->ctx
;
972 if (stream_ctx
->started
|| stream_ctx
->finished
) {
976 memcpy(&stream_ctx
->version
, v
, sizeof(stream_ctx
->version
));
980 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
)
982 php_http_env_response_stream_ctx_t
*stream_ctx
= r
->ctx
;
983 char *header_end
, *header_str
= NULL
;
984 size_t header_len
= 0;
985 zval zheader
, *zheader_ptr
;
986 zend_string
*header_key
;
989 if (stream_ctx
->started
|| stream_ctx
->finished
) {
993 header_len
= vspprintf(&header_str
, 0, fmt
, argv
);
995 if (!(header_end
= strchr(header_str
, ':'))) {
1000 header_key
= zend_string_init(header_str
, header_end
- header_str
, 0);
1002 if (!replace
&& (zheader_ptr
= zend_hash_find(&stream_ctx
->header
, header_key
))) {
1003 convert_to_array(zheader_ptr
);
1004 rv
= add_next_index_str(zheader_ptr
, php_http_cs2zs(header_str
, header_len
));
1006 ZVAL_STR(&zheader
, php_http_cs2zs(header_str
, header_len
));
1008 rv
= zend_hash_update(&stream_ctx
->header
, header_key
, &zheader
)
1009 ? SUCCESS
: FAILURE
;
1012 zend_string_release(header_key
);
1016 static ZEND_RESULT_CODE
php_http_env_response_stream_set_header(php_http_env_response_t
*r
, const char *fmt
, ...)
1018 ZEND_RESULT_CODE ret
;
1021 va_start(argv
, fmt
);
1022 ret
= php_http_env_response_stream_set_header_ex(r
, 1, fmt
, argv
);
1027 static ZEND_RESULT_CODE
php_http_env_response_stream_add_header(php_http_env_response_t
*r
, const char *fmt
, ...)
1029 ZEND_RESULT_CODE ret
;
1032 va_start(argv
, fmt
);
1033 ret
= php_http_env_response_stream_set_header_ex(r
, 0, fmt
, argv
);
1038 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
)
1040 php_http_env_response_stream_ctx_t
*stream_ctx
= r
->ctx
;
1042 if (stream_ctx
->started
|| stream_ctx
->finished
) {
1046 zend_hash_str_del(&stream_ctx
->header
, header_str
, header_len
);
1049 static ZEND_RESULT_CODE
php_http_env_response_stream_write(php_http_env_response_t
*r
, const char *data_str
, size_t data_len
)
1051 php_http_env_response_stream_ctx_t
*stream_ctx
= r
->ctx
;
1053 if (stream_ctx
->finished
) {
1056 if (!stream_ctx
->started
) {
1057 if (SUCCESS
!= php_http_env_response_stream_start(stream_ctx
)) {
1062 if (data_len
!= php_stream_write(stream_ctx
->stream
, data_str
, data_len
)) {
1068 static ZEND_RESULT_CODE
php_http_env_response_stream_flush(php_http_env_response_t
*r
)
1070 php_http_env_response_stream_ctx_t
*stream_ctx
= r
->ctx
;
1072 if (stream_ctx
->finished
) {
1075 if (!stream_ctx
->started
) {
1076 if (SUCCESS
!= php_http_env_response_stream_start(stream_ctx
)) {
1081 return php_stream_flush(stream_ctx
->stream
);
1083 static ZEND_RESULT_CODE
php_http_env_response_stream_finish(php_http_env_response_t
*r
)
1085 php_http_env_response_stream_ctx_t
*ctx
= r
->ctx
;
1087 if (ctx
->finished
) {
1090 if (!ctx
->started
) {
1091 if (SUCCESS
!= php_http_env_response_stream_start(ctx
)) {
1096 php_stream_flush(ctx
->stream
);
1097 if (ctx
->chunked
&& ctx
->chunked_filter
) {
1098 php_stream_filter_flush(ctx
->chunked_filter
, 1);
1099 ctx
->chunked_filter
= php_stream_filter_remove(ctx
->chunked_filter
, 1);
1107 static php_http_env_response_ops_t php_http_env_response_stream_ops
= {
1108 php_http_env_response_stream_init
,
1109 php_http_env_response_stream_dtor
,
1110 php_http_env_response_stream_get_status
,
1111 php_http_env_response_stream_set_status
,
1112 php_http_env_response_stream_set_protocol_version
,
1113 php_http_env_response_stream_set_header
,
1114 php_http_env_response_stream_add_header
,
1115 php_http_env_response_stream_del_header
,
1116 php_http_env_response_stream_write
,
1117 php_http_env_response_stream_flush
,
1118 php_http_env_response_stream_finish
1121 php_http_env_response_ops_t
*php_http_env_response_get_stream_ops(void)
1123 return &php_http_env_response_stream_ops
;
1126 #define PHP_HTTP_ENV_RESPONSE_OBJECT_INIT(obj) \
1128 if (!obj->message) { \
1129 obj->message = php_http_message_init_env(NULL, PHP_HTTP_RESPONSE); \
1133 ZEND_BEGIN_ARG_INFO_EX(ai_HttpEnvResponse___construct
, 0, 0, 0)
1134 ZEND_END_ARG_INFO();
1135 static PHP_METHOD(HttpEnvResponse
, __construct
)
1137 php_http_message_object_t
*obj
;
1139 php_http_expect(SUCCESS
== zend_parse_parameters_none(), invalid_arg
, return);
1141 obj
= PHP_HTTP_OBJ(NULL
, getThis());
1143 php_http_expect(obj
->message
= php_http_message_init_env(obj
->message
, PHP_HTTP_RESPONSE
), unexpected_val
, return);
1146 ZEND_BEGIN_ARG_INFO_EX(ai_HttpEnvResponse___invoke
, 0, 0, 1)
1147 ZEND_ARG_INFO(0, ob_string
)
1148 ZEND_ARG_INFO(0, ob_flags
)
1149 ZEND_END_ARG_INFO();
1150 static PHP_METHOD(HttpEnvResponse
, __invoke
)
1154 zend_long ob_flags
= 0;
1156 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS(), "s|l", &ob_str
, &ob_len
, &ob_flags
)) {
1157 php_http_message_object_t
*obj
= PHP_HTTP_OBJ(NULL
, getThis());
1159 PHP_HTTP_ENV_RESPONSE_OBJECT_INIT(obj
);
1162 php_http_message_object_init_body_object(obj
);
1165 if (ob_flags
& PHP_OUTPUT_HANDLER_CLEAN
) {
1166 php_stream_truncate_set_size(php_http_message_body_stream(obj
->message
->body
), 0);
1168 php_http_message_body_append(obj
->message
->body
, ob_str
, ob_len
);
1174 ZEND_BEGIN_ARG_INFO_EX(ai_HttpEnvResponse_setEnvRequest
, 0, 0, 1)
1175 ZEND_ARG_OBJ_INFO(0, env_request
, http
\\Message
, 1)
1176 ZEND_END_ARG_INFO();
1177 static PHP_METHOD(HttpEnvResponse
, setEnvRequest
)
1179 zval
*env_req
= NULL
;
1181 php_http_expect(SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS(), "|O", &env_req
, php_http_message_get_class_entry()), invalid_arg
, return);
1183 set_option(getThis(), ZEND_STRL("request"), IS_OBJECT
, env_req
, 0);
1184 RETVAL_ZVAL(getThis(), 1, 0);
1187 ZEND_BEGIN_ARG_INFO_EX(ai_HttpEnvResponse_setContentType
, 0, 0, 1)
1188 ZEND_ARG_INFO(0, content_type
)
1189 ZEND_END_ARG_INFO();
1190 static PHP_METHOD(HttpEnvResponse
, setContentType
)
1192 char *ct_str
= NULL
;
1195 php_http_expect(SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS(), "s!", &ct_str
, &ct_len
), invalid_arg
, return);
1197 set_option(getThis(), ZEND_STRL("contentType"), IS_STRING
, ct_str
, ct_len
);
1198 RETVAL_ZVAL(getThis(), 1, 0);
1201 ZEND_BEGIN_ARG_INFO_EX(ai_HttpEnvResponse_setContentDisposition
, 0, 0, 1)
1202 ZEND_ARG_ARRAY_INFO(0, disposition_params
, 1)
1203 ZEND_END_ARG_INFO();
1204 static PHP_METHOD(HttpEnvResponse
, setContentDisposition
)
1208 php_http_expect(SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS(), "a", &zdisposition
), invalid_arg
, return);
1210 zend_update_property(Z_OBJCE_P(getThis()), getThis(), ZEND_STRL("contentDisposition"), zdisposition
);
1211 RETVAL_ZVAL(getThis(), 1, 0);
1214 ZEND_BEGIN_ARG_INFO_EX(ai_HttpEnvResponse_setContentEncoding
, 0, 0, 1)
1215 ZEND_ARG_INFO(0, content_encoding
)
1216 ZEND_END_ARG_INFO();
1217 static PHP_METHOD(HttpEnvResponse
, setContentEncoding
)
1221 php_http_expect(SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS(), "l", &ce
), invalid_arg
, return);
1223 set_option(getThis(), ZEND_STRL("contentEncoding"), IS_LONG
, &ce
, 0);
1224 RETVAL_ZVAL(getThis(), 1, 0);
1227 ZEND_BEGIN_ARG_INFO_EX(ai_HttpEnvResponse_setCacheControl
, 0, 0, 1)
1228 ZEND_ARG_INFO(0, cache_control
)
1229 ZEND_END_ARG_INFO();
1230 static PHP_METHOD(HttpEnvResponse
, setCacheControl
)
1232 char *cc_str
= NULL
;
1235 php_http_expect(SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS(), "s!", &cc_str
, &cc_len
), invalid_arg
, return);
1237 set_option(getThis(), ZEND_STRL("cacheControl"), IS_STRING
, cc_str
, cc_len
);
1238 RETVAL_ZVAL(getThis(), 1, 0);
1241 ZEND_BEGIN_ARG_INFO_EX(ai_HttpEnvResponse_setLastModified
, 0, 0, 1)
1242 ZEND_ARG_INFO(0, last_modified
)
1243 ZEND_END_ARG_INFO();
1244 static PHP_METHOD(HttpEnvResponse
, setLastModified
)
1246 zend_long last_modified
;
1248 php_http_expect(SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS(), "l", &last_modified
), invalid_arg
, return);
1250 set_option(getThis(), ZEND_STRL("lastModified"), IS_LONG
, &last_modified
, 0);
1251 RETVAL_ZVAL(getThis(), 1, 0);
1254 ZEND_BEGIN_ARG_INFO_EX(ai_HttpEnvResponse_isCachedByLastModified
, 0, 0, 0)
1255 ZEND_ARG_INFO(0, header_name
)
1256 ZEND_END_ARG_INFO();
1257 static PHP_METHOD(HttpEnvResponse
, isCachedByLastModified
)
1259 char *header_name_str
= NULL
;
1260 size_t header_name_len
= 0;
1262 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS(), "|s!", &header_name_str
, &header_name_len
)) {
1263 if (!header_name_str
|| !header_name_len
) {
1264 header_name_str
= "If-Modified-Since";
1265 header_name_len
= lenof("If-Modified-Since");
1268 RETURN_LONG(php_http_env_is_response_cached_by_last_modified(getThis(), header_name_str
, header_name_len
, get_request(getThis())));
1272 ZEND_BEGIN_ARG_INFO_EX(ai_HttpEnvResponse_setEtag
, 0, 0, 1)
1273 ZEND_ARG_INFO(0, etag
)
1274 ZEND_END_ARG_INFO();
1275 static PHP_METHOD(HttpEnvResponse
, setEtag
)
1277 char *etag_str
= NULL
;
1278 size_t etag_len
= 0;
1280 php_http_expect(SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS(), "s!", &etag_str
, &etag_len
), invalid_arg
, return);
1282 set_option(getThis(), ZEND_STRL("etag"), IS_STRING
, etag_str
, etag_len
);
1283 RETVAL_ZVAL(getThis(), 1, 0);
1286 ZEND_BEGIN_ARG_INFO_EX(ai_HttpEnvResponse_isCachedByEtag
, 0, 0, 0)
1287 ZEND_ARG_INFO(0, header_name
)
1288 ZEND_END_ARG_INFO();
1289 static PHP_METHOD(HttpEnvResponse
, isCachedByEtag
)
1291 char *header_name_str
= NULL
;
1292 size_t header_name_len
= 0;
1294 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS(), "|s!", &header_name_str
, &header_name_len
)) {
1295 if (!header_name_str
|| !header_name_len
) {
1296 header_name_str
= "If-None-Match";
1297 header_name_len
= lenof("If-None-Match");
1299 RETURN_LONG(php_http_env_is_response_cached_by_etag(getThis(), header_name_str
, header_name_len
, get_request(getThis())));
1303 ZEND_BEGIN_ARG_INFO_EX(ai_HttpEnvResponse_setThrottleRate
, 0, 0, 1)
1304 ZEND_ARG_INFO(0, chunk_size
)
1305 ZEND_ARG_INFO(0, delay
)
1306 ZEND_END_ARG_INFO();
1307 static PHP_METHOD(HttpEnvResponse
, setThrottleRate
)
1309 zend_long chunk_size
;
1312 php_http_expect(SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS(), "l|d", &chunk_size
, &delay
), invalid_arg
, return);
1314 set_option(getThis(), ZEND_STRL("throttleDelay"), IS_DOUBLE
, &delay
, 0);
1315 set_option(getThis(), ZEND_STRL("throttleChunk"), IS_LONG
, &chunk_size
, 0);
1316 RETVAL_ZVAL(getThis(), 1, 0);
1319 ZEND_BEGIN_ARG_INFO_EX(ai_HttpEnvResponse_setCookie
, 0, 0, 1)
1320 ZEND_ARG_INFO(0, cookie
)
1321 ZEND_END_ARG_INFO();
1322 static PHP_METHOD(HttpEnvResponse
, setCookie
)
1324 zval
*zcookie_new
, tmp
;
1326 zend_error_handling zeh
;
1327 php_http_cookie_list_t
*list
= NULL
;
1329 php_http_expect(SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS(), "z", &zcookie_new
), invalid_arg
, return);
1331 zend_replace_error_handling(EH_THROW
, php_http_get_exception_unexpected_val_class_entry(), &zeh
);
1332 switch (Z_TYPE_P(zcookie_new
)) {
1334 if (instanceof_function(Z_OBJCE_P(zcookie_new
), php_http_cookie_get_class_entry())) {
1335 Z_ADDREF_P(zcookie_new
);
1340 list
= php_http_cookie_list_from_struct(NULL
, zcookie_new
);
1342 ZVAL_OBJECT(zcookie_new
, &php_http_cookie_object_new_ex(php_http_cookie_get_class_entry(), list
)->zo
, 0);
1346 zs
= zval_get_string(zcookie_new
);
1347 list
= php_http_cookie_list_parse(NULL
, zs
->val
, zs
->len
, 0, NULL
);
1348 zend_string_release(zs
);
1350 ZVAL_OBJECT(zcookie_new
, &php_http_cookie_object_new_ex(php_http_cookie_get_class_entry(), list
)->zo
, 0);
1352 zend_restore_error_handling(&zeh
);
1354 set_cookie(getThis(), zcookie_new
);
1355 zval_ptr_dtor(zcookie_new
);
1357 RETVAL_ZVAL(getThis(), 1, 0);
1360 ZEND_BEGIN_ARG_INFO_EX(ai_HttpEnvResponse_send
, 0, 0, 0)
1361 ZEND_ARG_INFO(0, stream
)
1362 ZEND_END_ARG_INFO();
1363 static PHP_METHOD(HttpEnvResponse
, send
)
1365 zval
*zstream
= NULL
;
1366 php_stream
*s
= NULL
;
1368 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS(), "|r", &zstream
)) {
1369 /* first flush the output layer to avoid conflicting headers and output;
1370 * also, ob_start($thisEnvResponse) might have been called */
1371 php_output_end_all();
1374 php_http_env_response_t
*r
;
1376 php_stream_from_zval(s
, zstream
);
1377 r
= php_http_env_response_init(NULL
, getThis(), php_http_env_response_get_stream_ops(), s
);
1382 RETVAL_BOOL(SUCCESS
== php_http_env_response_send(r
));
1383 php_http_env_response_free(&r
);
1385 php_http_env_response_t r
;
1387 if (!php_http_env_response_init(&r
, getThis(), NULL
, NULL
)) {
1391 RETVAL_BOOL(SUCCESS
== php_http_env_response_send(&r
));
1392 php_http_env_response_dtor(&r
);
1397 static zend_function_entry php_http_env_response_methods
[] = {
1398 PHP_ME(HttpEnvResponse
, __construct
, ai_HttpEnvResponse___construct
, ZEND_ACC_PUBLIC
|ZEND_ACC_CTOR
)
1399 PHP_ME(HttpEnvResponse
, __invoke
, ai_HttpEnvResponse___invoke
, ZEND_ACC_PUBLIC
)
1400 PHP_ME(HttpEnvResponse
, setEnvRequest
, ai_HttpEnvResponse_setEnvRequest
, ZEND_ACC_PUBLIC
)
1401 PHP_ME(HttpEnvResponse
, setCookie
, ai_HttpEnvResponse_setCookie
, ZEND_ACC_PUBLIC
)
1402 PHP_ME(HttpEnvResponse
, setContentType
, ai_HttpEnvResponse_setContentType
, ZEND_ACC_PUBLIC
)
1403 PHP_ME(HttpEnvResponse
, setContentDisposition
, ai_HttpEnvResponse_setContentDisposition
, ZEND_ACC_PUBLIC
)
1404 PHP_ME(HttpEnvResponse
, setContentEncoding
, ai_HttpEnvResponse_setContentEncoding
, ZEND_ACC_PUBLIC
)
1405 PHP_ME(HttpEnvResponse
, setCacheControl
, ai_HttpEnvResponse_setCacheControl
, ZEND_ACC_PUBLIC
)
1406 PHP_ME(HttpEnvResponse
, setLastModified
, ai_HttpEnvResponse_setLastModified
, ZEND_ACC_PUBLIC
)
1407 PHP_ME(HttpEnvResponse
, isCachedByLastModified
, ai_HttpEnvResponse_isCachedByLastModified
, ZEND_ACC_PUBLIC
)
1408 PHP_ME(HttpEnvResponse
, setEtag
, ai_HttpEnvResponse_setEtag
, ZEND_ACC_PUBLIC
)
1409 PHP_ME(HttpEnvResponse
, isCachedByEtag
, ai_HttpEnvResponse_isCachedByEtag
, ZEND_ACC_PUBLIC
)
1410 PHP_ME(HttpEnvResponse
, setThrottleRate
, ai_HttpEnvResponse_setThrottleRate
, ZEND_ACC_PUBLIC
)
1411 PHP_ME(HttpEnvResponse
, send
, ai_HttpEnvResponse_send
, ZEND_ACC_PUBLIC
)
1412 EMPTY_FUNCTION_ENTRY
1415 static zend_class_entry
*php_http_env_response_class_entry
;
1416 zend_class_entry
*php_http_get_env_response_class_entry(void)
1418 return php_http_env_response_class_entry
;
1421 PHP_MINIT_FUNCTION(http_env_response
)
1423 zend_class_entry ce
= {0};
1425 INIT_NS_CLASS_ENTRY(ce
, "http\\Env", "Response", php_http_env_response_methods
);
1426 php_http_env_response_class_entry
= zend_register_internal_class_ex(&ce
, php_http_message_get_class_entry());
1428 zend_declare_class_constant_long(php_http_env_response_class_entry
, ZEND_STRL("CONTENT_ENCODING_NONE"), PHP_HTTP_CONTENT_ENCODING_NONE
);
1429 zend_declare_class_constant_long(php_http_env_response_class_entry
, ZEND_STRL("CONTENT_ENCODING_GZIP"), PHP_HTTP_CONTENT_ENCODING_GZIP
);
1431 zend_declare_class_constant_long(php_http_env_response_class_entry
, ZEND_STRL("CACHE_NO"), PHP_HTTP_CACHE_NO
);
1432 zend_declare_class_constant_long(php_http_env_response_class_entry
, ZEND_STRL("CACHE_HIT"), PHP_HTTP_CACHE_HIT
);
1433 zend_declare_class_constant_long(php_http_env_response_class_entry
, ZEND_STRL("CACHE_MISS"), PHP_HTTP_CACHE_MISS
);
1435 zend_declare_property_null(php_http_env_response_class_entry
, ZEND_STRL("request"), ZEND_ACC_PROTECTED
);
1436 zend_declare_property_null(php_http_env_response_class_entry
, ZEND_STRL("cookies"), ZEND_ACC_PROTECTED
);
1437 zend_declare_property_null(php_http_env_response_class_entry
, ZEND_STRL("contentType"), ZEND_ACC_PROTECTED
);
1438 zend_declare_property_null(php_http_env_response_class_entry
, ZEND_STRL("contentDisposition"), ZEND_ACC_PROTECTED
);
1439 zend_declare_property_null(php_http_env_response_class_entry
, ZEND_STRL("contentEncoding"), ZEND_ACC_PROTECTED
);
1440 zend_declare_property_null(php_http_env_response_class_entry
, ZEND_STRL("cacheControl"), ZEND_ACC_PROTECTED
);
1441 zend_declare_property_null(php_http_env_response_class_entry
, ZEND_STRL("etag"), ZEND_ACC_PROTECTED
);
1442 zend_declare_property_null(php_http_env_response_class_entry
, ZEND_STRL("lastModified"), ZEND_ACC_PROTECTED
);
1443 zend_declare_property_null(php_http_env_response_class_entry
, ZEND_STRL("throttleDelay"), ZEND_ACC_PROTECTED
);
1444 zend_declare_property_null(php_http_env_response_class_entry
, ZEND_STRL("throttleChunk"), ZEND_ACC_PROTECTED
);
1455 * vim600: noet sw=4 ts=4 fdm=marker
1456 * vim<600: noet sw=4 ts=4