separate php_http_env_response and implement content encoding
authorMichael Wallner <mike@php.net>
Fri, 10 Jun 2011 19:00:18 +0000 (19:00 +0000)
committerMichael Wallner <mike@php.net>
Fri, 10 Jun 2011 19:00:18 +0000 (19:00 +0000)
config9.m4
package.xml
php_http.c
php_http.h
php_http_encoding.h
php_http_env.c
php_http_env.h
php_http_env_response.c [new file with mode: 0644]
php_http_env_response.h [new file with mode: 0644]
php_http_misc.c

index c4486df3cbaf3feb56a97a64aa927027eed1bb87..a6155d7fbad276149b5abc3af31cbec5c4a9b7d8 100644 (file)
@@ -430,6 +430,7 @@ dnl ----
                php_http_cookie.c \
                php_http_encoding.c \
                php_http_env.c \
+               php_http_env_response.c \
                php_http_etag.c \
                php_http_exception.c \
                php_http_filter.c \
@@ -473,6 +474,7 @@ dnl ----
                php_http_cookie.h \
                php_http_encoding.h \
                php_http_env.h \
+               php_http_env_response.h \
                php_http_etag.h \
                php_http_exception.h \
                php_http_filter.h \
index 636317a123f0454bc398a9f469ce24691f4dc27b..32fe526488276135cf741a2fe0fa9f30533ab2a5 100644 (file)
@@ -62,6 +62,8 @@ List of changes (TBD):
    <file role="src" name="php_http_encoding.h"/>
    <file role="src" name="php_http_env.c"/>
    <file role="src" name="php_http_env.h"/>
+   <file role="src" name="php_http_enc_response.c"/>
+   <file role="src" name="php_http_enc_response.h"/>
    <file role="src" name="php_http_etag.c"/>
    <file role="src" name="php_http_etag.h"/>
    <file role="src" name="php_http_exception.c"/>
index e5456c4cfbb4151fe08e7528f8fe52bba8e13b1c..ebe73ccae447cfc6f90a52676f36c2eddbc4f007 100644 (file)
@@ -162,6 +162,7 @@ PHP_MINIT_FUNCTION(http)
        || SUCCESS != PHP_MINIT_CALL(http_request_pool)
        || SUCCESS != PHP_MINIT_CALL(http_url)
        || SUCCESS != PHP_MINIT_CALL(http_env)
+       || SUCCESS != PHP_MINIT_CALL(http_env_response)
        ) {
                return FAILURE;
        }
index 822ac1d9b013c456e0b400254e6cfea2f46aec76..b1bc57c4d6cacca6c9c17e81279e00a04733fb40 100644 (file)
@@ -87,6 +87,7 @@ extern void _dpf(int type, const char *data, size_t length);
 #include "php_http_cookie.h"
 #include "php_http_encoding.h"
 #include "php_http_env.h"
+#include "php_http_env_response.h"
 #include "php_http_etag.h"
 #include "php_http_exception.h"
 #include "php_http_fluently_callable.h"
index 9e700a774bc4f0f5689590c940c309926c06d00b..23a33022ec8c4a2511ef1c6c82cd55e445be011d 100644 (file)
 #ifndef PHP_HTTP_ENCODING_H
 #define PHP_HTTP_ENCODING_H
 
-PHP_HTTP_API int php_http_encoding_response_start(size_t content_length, zend_bool ignore_http_ohandler TSRMLS_DC);
-
 extern PHP_MINIT_FUNCTION(http_encoding);
 extern PHP_RINIT_FUNCTION(http_encoding);
 extern PHP_RSHUTDOWN_FUNCTION(http_encoding);
 
-typedef enum php_http_encoding_type {
-       PHP_HTTP_ENCODING_NONE,
-       PHP_HTTP_ENCODING_GZIP,
-       PHP_HTTP_ENCODING_DEFLATE,
-} php_http_encoding_type_t;
-
 #define PHP_HTTP_INFLATE_ROUNDS 100
 
 #define PHP_HTTP_DEFLATE_BUFFER_SIZE_GUESS(S) \
index 072c309d595823ddcaf61548a64966fd0a042b37..511b878efbe134543e7791ea13c616c0d74447d5 100644 (file)
 #include "php_http.h"
 
 #include <main/SAPI.h>
-#include <ext/date/php_date.h>
-#include <ext/standard/php_string.h>
 
 PHP_RINIT_FUNCTION(http_env)
 {
-       PHP_HTTP_G->env.response.last_modified = 0;
-       PHP_HTTP_G->env.response.throttle_chunk = 0;
-       PHP_HTTP_G->env.response.throttle_delay = 0;
        PHP_HTTP_G->env.request.time = sapi_get_request_time(TSRMLS_C);
 
        return SUCCESS;
@@ -38,11 +33,6 @@ PHP_RSHUTDOWN_FUNCTION(http_env)
        if (PHP_HTTP_G->env.request.body) {
                php_http_message_body_free(&PHP_HTTP_G->env.request.body);
        }
-       if (PHP_HTTP_G->env.response.body) {
-               php_http_message_body_free(&PHP_HTTP_G->env.response.body);
-       }
-       STR_SET(PHP_HTTP_G->env.response.content_type, NULL);
-       STR_SET(PHP_HTTP_G->env.response.etag, NULL);
 
        if (PHP_HTTP_G->env.server_var) {
                zval_ptr_dtor(&PHP_HTTP_G->env.server_var);
@@ -481,607 +471,6 @@ PHP_HTTP_API STATUS php_http_env_set_response_header_value(long http_code, const
        }
 }
 
-static void set_container_value(zval *container, const char *name_str, size_t name_len, int type, const void *value_ptr, size_t value_len TSRMLS_DC)
-{
-       if (Z_TYPE_P(container) == IS_OBJECT) {
-               /* stupid non-const api */
-               char *name = estrndup(name_str, name_len);
-               switch (type) {
-                       case IS_DOUBLE:
-                               zend_update_property_double(Z_OBJCE_P(container), container, name, name_len, *(double *)value_ptr TSRMLS_CC);
-                               break;
-                       case IS_LONG:
-                               zend_update_property_long(Z_OBJCE_P(container), container, name, name_len, *(long *)value_ptr TSRMLS_CC);
-                               break;
-                       case IS_STRING:
-                               zend_update_property_stringl(Z_OBJCE_P(container), container, name, name_len, value_ptr, value_len TSRMLS_CC);
-                               break;
-               }
-               efree(name);
-       } else {
-               convert_to_array(container);
-               switch (type) {
-                       case IS_DOUBLE:
-                               add_assoc_double_ex(container, name_str, name_len + 1, *(double *)value_ptr);
-                               break;
-                       case IS_LONG:
-                               add_assoc_long_ex(container, name_str, name_len + 1, *(long *)value_ptr);
-                               break;
-                       case IS_STRING: {
-                               char *value = estrndup(value_ptr, value_len);
-                               add_assoc_stringl_ex(container, name_str, name_len + 1, value, value_len, 0);
-                               break;
-                       }
-               }
-       }
-}
-
-PHP_HTTP_API void php_http_env_set_response_throttle_rate(zval *container, size_t chunk_size, double delay TSRMLS_CC)
-{
-       long chunk_size_long = (long) chunk_size;
-
-       set_container_value(container, ZEND_STRL("throttleDelay"), IS_DOUBLE, &delay, 0 TSRMLS_CC);
-       set_container_value(container, ZEND_STRL("throttleChunk"), IS_LONG, &chunk_size_long, 0 TSRMLS_CC);
-       if (Z_TYPE_P(container) == IS_OBJECT) {
-               zend_update_property_double(Z_OBJCE_P(container), container, ZEND_STRL("throttleDelay"), delay TSRMLS_CC);
-               zend_update_property_long(Z_OBJCE_P(container), container, ZEND_STRL("throttleChunk"), chunk_size TSRMLS_CC);
-       } else {
-               convert_to_array(container);
-               add_assoc_double_ex(container, ZEND_STRS("throttleDelay"), delay);
-               add_assoc_long_ex(container, ZEND_STRS("throttleChunk"), chunk_size);
-       }
-}
-
-PHP_HTTP_API STATUS php_http_env_set_response_last_modified(zval *container, time_t t, char **sent_header TSRMLS_DC)
-{
-       STATUS ret;
-       char *lm_header_str, *date;
-       size_t lm_header_len;
-
-       if (t) {
-               if (!(date = php_format_date(ZEND_STRL(PHP_HTTP_DATE_FORMAT), t, 0 TSRMLS_CC))) {
-                       return FAILURE;
-               }
-
-               lm_header_len = spprintf(&lm_header_str, 0, "Last-Modified: %s", date);
-               STR_FREE(date);
-       } else {
-               lm_header_str = "Last-Modified:";
-               lm_header_len = lenof("Last-Modified:");
-       }
-
-       if (SUCCESS == (ret = php_http_env_set_response_header(0, lm_header_str, lm_header_len, 1 TSRMLS_CC))) {
-               set_container_value(container, ZEND_STRL("lastModified"), IS_LONG, &t, 0 TSRMLS_CC);
-       }
-
-       if (sent_header) {
-               *sent_header = lm_header_str;
-       } else if (t) {
-               STR_FREE(lm_header_str);
-       }
-
-       return ret;
-}
-
-PHP_HTTP_API STATUS php_http_env_set_response_etag(zval *container, const char *etag_str, size_t etag_len, char **sent_header TSRMLS_DC)
-{
-       STATUS ret;
-       char *etag = NULL, *etag_header_str;
-       size_t etag_header_len;
-
-       if (etag_len){
-               etag_header_len = spprintf(&etag_header_str, 0, "ETag: \"%s\"", etag_str);
-       } else {
-               etag_header_str = "ETag:";
-               etag_header_len = lenof("ETag:");
-       }
-
-       if (SUCCESS == (ret = php_http_env_set_response_header(0, etag_header_str, etag_header_len, 1 TSRMLS_CC))) {
-               set_container_value(container, ZEND_STRL(etag), IS_STRING, etag_str, etag_len TSRMLS_CC);
-       }
-
-       if (sent_header) {
-               *sent_header = etag_header_str;
-       } else if (etag_len) {
-               STR_FREE(etag_header_str);
-       }
-
-       return ret;
-}
-
-PHP_HTTP_API STATUS php_http_env_set_response_content_type(zval *container, const char *ct_str, size_t ct_len, char **sent_header TSRMLS_DC)
-{
-       STATUS ret;
-       char *ct_header_str;
-       size_t ct_header_len;
-
-       if (ct_len) {
-               PHP_HTTP_CHECK_CONTENT_TYPE(ct_str, return FAILURE);
-               ct_header_len = spprintf(&ct_header_str, 0, "Content-Type: %s", ct_str);
-       } else {
-               ct_header_str = "Content-Type:";
-               ct_header_len = lenof("Content-Type:");
-       }
-
-       if (SUCCESS == (ret = php_http_env_set_response_header(0, ct_header_str, ct_header_len, 1 TSRMLS_CC))) {
-               set_container_value(container, ZEND_STRL("contentType"), IS_STRING, ct_str, ct_len TSRMLS_CC);
-       }
-
-       if (sent_header) {
-               *sent_header = ct_header_str;
-       } else if (ct_len) {
-               STR_FREE(ct_header_str);
-       }
-
-       return ret;
-}
-
-PHP_HTTP_API STATUS php_http_env_set_response_content_disposition(zval *container, php_http_content_disposition_t d, const char *f_str, size_t f_len, char **sent_header TSRMLS_DC)
-{
-       STATUS ret;
-       char *tmp, *cd_header_str, *new_f_str;
-       int new_f_len;
-       size_t cd_header_len;
-
-       switch (d) {
-               case PHP_HTTP_CONTENT_DISPOSITION_NONE:
-                       break;
-               case PHP_HTTP_CONTENT_DISPOSITION_INLINE:
-                       tmp = "inline";
-                       break;
-               case PHP_HTTP_CONTENT_DISPOSITION_ATTACHMENT:
-                       tmp = "attachment";
-                       break;
-               default:
-                       php_http_error(HE_WARNING, PHP_HTTP_E_INVALID_PARAM, "Unknown content disposition (%d)", (int) d);
-                       return FAILURE;
-       }
-
-       if (f_len) {
-               new_f_str = php_addslashes(estrndup(f_str, f_len), f_len, &new_f_len, 0 TSRMLS_CC);
-               cd_header_len = spprintf(&cd_header_str, 0, "Content-Disposition: %s; filename=\"%.*s\"", tmp, new_f_len, new_f_str);
-               STR_FREE(new_f_str);
-       } else if (d) {
-               cd_header_len = spprintf(&cd_header_str, 0, "Content-Disposition: %s", tmp);
-       } else {
-               cd_header_str = "Content-Disposition:";
-               cd_header_len = lenof("Content-Disposition:");
-       }
-       
-       ret = php_http_env_set_response_header(0, cd_header_str, cd_header_len, 1 TSRMLS_CC);
-
-       if (sent_header) {
-               *sent_header = cd_header_str;
-       } else if (f_len || d){
-               STR_FREE(cd_header_str);
-       }
-
-       return ret;
-}
-
-PHP_HTTP_API STATUS php_http_env_set_response_cache_control(zval *container, const char *cc_str, size_t cc_len, char **sent_header TSRMLS_DC)
-{
-       STATUS ret;
-       char *cc_header_str;
-       size_t cc_header_len;
-
-       if (cc_len) {
-               cc_header_len = spprintf(&cc_header_str, 0, "Cache-Control: %s", cc_str);
-       } else {
-               cc_header_str = "Content-Disposition:";
-               cc_header_len = lenof("Content-Disposition:");
-       }
-
-       ret = php_http_env_set_response_header(0, cc_header_str, cc_header_len, 1 TSRMLS_CC);
-
-       if (sent_header) {
-               *sent_header = cc_header_str;
-       } else if (cc_len) {
-               STR_FREE(cc_header_str);
-       }
-
-       return ret;
-}
-
-static zval *get_container_value(zval *container, const char *name_str, size_t name_len TSRMLS_CC)
-{
-       zval *val, **valptr;
-
-       if (Z_TYPE_P(container) == IS_OBJECT) {
-               char *name = estrndup(name_str, name_len);
-               val = zend_read_property(Z_OBJCE_P(container), container, name, name_len, 0 TSRMLS_CC);
-               efree(name);
-       } else {
-               if (SUCCESS == zend_hash_find(Z_ARRVAL_P(container), name_str, name_len + 1, (void *) &valptr)) {
-                       val = *valptr;
-               } else {
-                       val = NULL;
-               }
-       }
-       if (val) {
-               Z_ADDREF_P(val);
-       }
-       return val;
-}
-
-PHP_HTTP_API php_http_cache_status_t php_http_env_is_response_cached_by_etag(zval *container, const char *header_str, size_t header_len TSRMLS_DC)
-{
-       int ret, free_etag = 0;
-       char *header, *etag;
-       zval *zetag, *zbody = NULL;
-
-       if (    !(header = php_http_env_get_request_header(header_str, header_len TSRMLS_CC))
-       ||              !(zbody = get_container_value(container, ZEND_STRL("body") TSRMLS_CC))
-       ||              !(Z_TYPE_P(zbody) == IS_OBJECT)
-       ||              !instanceof_function(Z_OBJCE_P(zbody), php_http_message_body_class_entry TSRMLS_CC)
-       ) {
-               STR_FREE(header);
-               if (zbody) {
-                       zval_ptr_dtor(&zbody);
-               }
-               return PHP_HTTP_CACHE_NO;
-       }
-
-       if ((zetag = get_container_value(container, ZEND_STRL("etag") TSRMLS_CC))) {
-               zval *zetag_copy = php_http_ztyp(IS_STRING, zetag);
-               zval_ptr_dtor(&zetag);
-               zetag = zetag_copy;
-       }
-
-       if (zetag && Z_STRLEN_P(zetag)) {
-               etag = Z_STRVAL_P(zetag);
-       } else {
-               etag = php_http_message_body_etag(((php_http_message_body_object_t *) zend_object_store_get_object(zbody TSRMLS_CC))->body);
-               php_http_env_set_response_etag(container, etag, strlen(etag), NULL TSRMLS_CC);
-               free_etag = 1;
-       }
-
-       if (zetag) {
-               zval_ptr_dtor(&zetag);
-       }
-
-       ret = php_http_match(header, etag, PHP_HTTP_MATCH_WORD);
-
-       if (free_etag) {
-               efree(etag);
-       }
-       efree(header);
-
-       return ret ? PHP_HTTP_CACHE_HIT : PHP_HTTP_CACHE_MISS;
-}
-
-PHP_HTTP_API php_http_cache_status_t php_http_env_is_response_cached_by_last_modified(zval *container, const char *header_str, size_t header_len TSRMLS_DC)
-{
-       char *header;
-       time_t ums, lm = 0;
-       zval *zbody = NULL, *zlm;
-
-       if (    !(header = php_http_env_get_request_header(header_str, header_len TSRMLS_CC))
-       ||              !(zbody = get_container_value(container, ZEND_STRL("body") TSRMLS_CC))
-       ||              !(Z_TYPE_P(zbody) == IS_OBJECT)
-       ||              !instanceof_function(Z_OBJCE_P(zbody), php_http_message_body_class_entry TSRMLS_CC)
-       ) {
-               STR_FREE(header);
-               if (zbody) {
-                       zval_ptr_dtor(&zbody);
-               }
-               return PHP_HTTP_CACHE_NO;
-       }
-
-       if ((zlm = get_container_value(container, ZEND_STRL("lastModified") TSRMLS_CC))) {
-               zval *zlm_copy = php_http_ztyp(IS_LONG, zlm);
-               zval_ptr_dtor(&zlm);
-               zlm = zlm_copy;
-       }
-
-       if (zlm && Z_LVAL_P(zlm) > 0) {
-               lm = Z_LVAL_P(zlm);
-       } else {
-               lm = php_http_message_body_mtime(((php_http_message_body_object_t *) zend_object_store_get_object(zbody TSRMLS_CC))->body);
-               php_http_env_set_response_last_modified(container, lm, NULL TSRMLS_CC);
-       }
-
-       if (zlm) {
-               zval_ptr_dtor(&zlm);
-       }
-
-       ums = php_parse_date(header, NULL TSRMLS_CC);
-       efree(header);
-
-       if (ums > 0 && ums <= lm) {
-               return PHP_HTTP_CACHE_HIT;
-       } else {
-               return PHP_HTTP_CACHE_MISS;
-       }
-}
-
-PHP_HTTP_API void php_http_env_set_response_body(zval *container, php_http_message_body_t *body)
-{
-       TSRMLS_FETCH_FROM_CTX(body->ts);
-       zend_object_value ov = php_http_message_body_object_new_ex(php_http_message_body_class_entry, php_http_message_body_copy(body, NULL, 0), NULL TSRMLS_CC);
-
-       set_container_value(container, ZEND_STRL("body"), IS_OBJECT, &ov, 0 TSRMLS_CC);
-}
-
-struct output_ctx {
-       php_http_buffer_t *buf;
-       zval *container;
-};
-
-static size_t output(void *context, const char *buf, size_t len TSRMLS_DC)
-{
-       struct output_ctx *ctx = context;
-
-       if (ctx->buf) {
-               zval *zcs;
-               size_t chunk_size = PHP_HTTP_SENDBUF_SIZE;
-
-               if ((zcs = get_container_value(ctx->container, ZEND_STRL("throttleChunk") TSRMLS_CC))) {
-                       zval *zcs_copy = php_http_ztyp(IS_LONG, zcs);
-
-                       zval_ptr_dtor(&zcs);
-                       chunk_size = Z_LVAL_P(zcs_copy);
-                       zval_ptr_dtor(&zcs_copy);
-               }
-               php_http_buffer_chunked_output(&ctx->buf, buf, len, buf ? chunk_size : 0, output, NULL TSRMLS_CC);
-       } else {
-               zval *ztd;
-
-
-               PHPWRITE(buf, len);
-
-               /*      we really only need to flush when throttling is enabled,
-                       because we push the data as fast as possible anyway if not */
-               if ((ztd = get_container_value(ctx->container, ZEND_STRL("throttleDelay") TSRMLS_CC))) {
-                       double delay;
-                       zval *ztd_copy = php_http_ztyp(IS_DOUBLE, ztd);
-
-                       zval_ptr_dtor(&ztd);
-                       delay = Z_DVAL_P(ztd_copy);
-                       zval_ptr_dtor(&ztd_copy);
-
-                       if (delay >= PHP_HTTP_DIFFSEC) {
-                               if (php_output_get_level(TSRMLS_C)) {
-                                       php_output_flush_all(TSRMLS_C);
-                               }
-                               if (!(php_output_get_status(TSRMLS_C) & PHP_OUTPUT_IMPLICITFLUSH)) {
-                                       sapi_flush(TSRMLS_C);
-                               }
-                               php_http_sleep(delay);
-                       }
-               }
-       }
-       return len;
-}
-
-PHP_HTTP_API STATUS php_http_env_send_response(zval *container TSRMLS_DC)
-{
-       struct output_ctx ctx = {NULL, container};
-       zval *zbody, *zheader, *zrcode, *zversion;
-       HashTable ranges;
-       php_http_range_status_t range_status;
-       php_http_message_body_t *body;
-       size_t body_size;
-
-       if (    !(zbody = get_container_value(container, ZEND_STRL("body") TSRMLS_CC))
-       ||              !(Z_TYPE_P(zbody) == IS_OBJECT)
-       ||              !instanceof_function(Z_OBJCE_P(zbody), php_http_message_body_class_entry TSRMLS_CC)
-       ) {
-               if (zbody) {
-                       zval_ptr_dtor(&zbody);
-               }
-               return FAILURE;
-       }
-
-       if ((zrcode = get_container_value(container, ZEND_STRL("responseCode") TSRMLS_CC))) {
-               zval *zrcode_copy = php_http_ztyp(IS_LONG, zrcode);
-
-               zval_ptr_dtor(&zrcode);
-               if (Z_LVAL_P(zrcode_copy) > 0) {
-                       php_http_env_set_response_code(Z_LVAL_P(zrcode_copy) TSRMLS_CC);
-               }
-               zval_ptr_dtor(&zrcode_copy);
-       }
-
-       if ((zversion = get_container_value(container, ZEND_STRL("httpVersion") TSRMLS_CC))) {
-               php_http_version_t v;
-               zval *zversion_copy = php_http_ztyp(IS_STRING, zversion);
-
-               zval_ptr_dtor(&zversion);
-               if (Z_STRLEN_P(zversion_copy) && php_http_version_parse(&v, Z_STRVAL_P(zversion_copy) TSRMLS_CC)) {
-                       php_http_env_set_response_protocol_version(&v TSRMLS_CC);
-                       php_http_version_dtor(&v);
-               }
-               zval_ptr_dtor(&zversion_copy);
-       }
-
-       if ((zheader = get_container_value(container, ZEND_STRL("headers") TSRMLS_CC))) {
-               if (Z_TYPE_P(zheader) == IS_ARRAY) {
-                       zval **val;
-                       HashPosition pos;
-                       php_http_array_hashkey_t key = php_http_array_hashkey_init(0);
-
-                       FOREACH_KEYVAL(pos, zheader, key, val) {
-                               if (key.type == HASH_KEY_IS_STRING) {
-                                       php_http_env_set_response_header_value(0, key.str, key.len - 1, *val, 1 TSRMLS_CC);
-                               }
-                       }
-               }
-               zval_ptr_dtor(&zheader);
-       }
-
-       body = ((php_http_message_body_object_t *) zend_object_store_get_object(zbody TSRMLS_CC))->body;
-       body_size = php_http_message_body_size(body);
-       php_http_env_set_response_header(0, ZEND_STRL("Accept-Ranges: bytes"), 1 TSRMLS_CC);
-       zend_hash_init(&ranges, 0, NULL, ZVAL_PTR_DTOR, 0);
-       range_status = php_http_env_get_request_ranges(&ranges, body_size TSRMLS_CC);
-
-       switch (range_status) {
-               case PHP_HTTP_RANGE_ERR:
-                       zend_hash_destroy(&ranges);
-                       if (!php_http_env_got_request_header(ZEND_STRL("If-Range") TSRMLS_CC)) {
-                               char *cr_header_str;
-                               size_t cr_header_len;
-
-                               cr_header_len = spprintf(&cr_header_str, 0, "Content-Range: bytes */%zu", body_size);
-                               php_http_env_set_response_header(416, cr_header_str, cr_header_len, 1 TSRMLS_CC);
-                               efree(cr_header_str);
-                               if (zbody) {
-                                       zval_ptr_dtor(&zbody);
-                               }
-                               return SUCCESS;
-                       }
-                       break;
-
-               case PHP_HTTP_RANGE_NO:
-                       /* send full entity */
-                       zend_hash_destroy(&ranges);
-                       break;
-
-               case PHP_HTTP_RANGE_OK:
-                       /* send content-range response */
-                       if (PHP_HTTP_CACHE_MISS == php_http_env_is_response_cached_by_etag(container, ZEND_STRL("If-Range") TSRMLS_CC)
-                       ||      PHP_HTTP_CACHE_MISS == php_http_env_is_response_cached_by_last_modified(container, ZEND_STRL("If-Range") TSRMLS_CC)
-                       ) {
-                               /* send full entity */
-                               zend_hash_destroy(&ranges);
-                               break;
-                       }
-                       if (PHP_HTTP_CACHE_MISS == php_http_env_is_response_cached_by_etag(container, ZEND_STRL("If-Match") TSRMLS_CC)
-                       ||      PHP_HTTP_CACHE_MISS == php_http_env_is_response_cached_by_last_modified(container, ZEND_STRL("If-Unmodified-Since") TSRMLS_CC)
-                       ||      PHP_HTTP_CACHE_MISS == php_http_env_is_response_cached_by_last_modified(container, ZEND_STRL("Unless-Modified-Since") TSRMLS_CC)
-                       ) {
-                               zend_hash_destroy(&ranges);
-                               php_http_env_set_response_code(412 TSRMLS_CC);
-                               if (zbody) {
-                                       zval_ptr_dtor(&zbody);
-                               }
-                               return SUCCESS;
-                       }
-                       if (zend_hash_num_elements(&ranges) == 1) {
-                               /* single range */
-                               zval **range, **begin, **end;
-
-                               if (SUCCESS != zend_hash_index_find(&ranges, 0, (void *) &range)
-                               ||      SUCCESS != zend_hash_index_find(Z_ARRVAL_PP(range), 0, (void *) &begin)
-                               ||      SUCCESS != zend_hash_index_find(Z_ARRVAL_PP(range), 1, (void *) &end)
-                               ) {
-                                       /* this should never happen */
-                                       zend_hash_destroy(&ranges);
-                                       php_http_env_set_response_code(500 TSRMLS_CC);
-                                       if (zbody) {
-                                               zval_ptr_dtor(&zbody);
-                                       }
-                                       return FAILURE;
-                               } else {
-                                       char *cr_header_str;
-                                       size_t cr_header_len;
-
-                                       cr_header_len = spprintf(&cr_header_str, 0, "Content-Range: bytes %ld-%ld/%zu", Z_LVAL_PP(begin), Z_LVAL_PP(end), body_size);
-                                       php_http_env_set_response_header(206, cr_header_str, cr_header_len, 1 TSRMLS_CC);
-                                       efree(cr_header_str);
-
-                                       /* send chunk */
-                                       php_http_message_body_to_callback(body, output, &ctx, Z_LVAL_PP(begin), Z_LVAL_PP(end) - Z_LVAL_PP(begin) + 1);
-                                       output(&ctx, NULL, 0 TSRMLS_CC);
-                                       zend_hash_destroy(&ranges);
-                                       if (zbody) {
-                                               zval_ptr_dtor(&zbody);
-                                       }
-                                       return SUCCESS;
-                               }
-                       } else {
-                               /* send multipart/byte-ranges message */
-                               HashPosition pos;
-                               zval **chunk, *zct;
-                               php_http_buffer_t preface;
-                               int free_ct = 0;
-                               char *content_type = "application/octet-stream";
-                               char boundary[32];
-
-                               if ((zct = get_container_value(container, ZEND_STRL("contentType") TSRMLS_CC))) {
-                                       zval *zct_copy = php_http_ztyp(IS_STRING, zct);
-
-                                       zval_ptr_dtor(&zct);
-                                       if (Z_STRLEN_P(zct_copy)) {
-                                               content_type = estrndup(Z_STRVAL_P(zct_copy), Z_STRLEN_P(zct_copy));
-                                               free_ct = 1;
-                                       }
-
-                                       zval_ptr_dtor(&zct);
-                               }
-
-                               php_http_boundary(boundary, sizeof(boundary));
-                               php_http_env_set_response_header_format(206, 1 TSRMLS_CC, "Content-Type: multipart/byteranges; boundary=%s", boundary);
-
-                               php_http_buffer_init(&preface);
-                               FOREACH_HASH_VAL(pos, &ranges, chunk) {
-                                       zval **begin, **end;
-
-                                       if (IS_ARRAY == Z_TYPE_PP(chunk)
-                                       &&      SUCCESS == zend_hash_index_find(Z_ARRVAL_PP(chunk), 0, (void *) &begin)
-                                       &&      IS_LONG == Z_TYPE_PP(begin)
-                                       &&      SUCCESS == zend_hash_index_find(Z_ARRVAL_PP(chunk), 1, (void *) &end)
-                                       &&      IS_LONG == Z_TYPE_PP(end)
-                                       ) {
-                                               php_http_buffer_appendf(&preface,
-                                                               PHP_HTTP_CRLF
-                                                               "--%s" PHP_HTTP_CRLF
-                                                               "Content-Type: %s" PHP_HTTP_CRLF
-                                                               "Content-Range: bytes %ld-%ld/%zu" PHP_HTTP_CRLF,
-                                                               /* - */
-                                                               boundary,
-                                                               content_type,
-                                                               Z_LVAL_PP(begin),
-                                                               Z_LVAL_PP(end),
-                                                               body_size
-                                               );
-                                               php_http_buffer_fix(&preface);
-                                               output(&ctx, PHP_HTTP_BUFFER_VAL(&preface), PHP_HTTP_BUFFER_LEN(&preface) TSRMLS_CC);
-                                               php_http_buffer_reset(&preface);
-
-                                               php_http_message_body_to_callback(body, output, &ctx, Z_LVAL_PP(begin), Z_LVAL_PP(end) - Z_LVAL_PP(begin) + 1);
-                                       }
-                               }
-                               php_http_buffer_appendf(&preface, PHP_HTTP_CRLF "--%s--", boundary);
-                               php_http_buffer_fix(&preface);
-                               output(&ctx, PHP_HTTP_BUFFER_VAL(&preface), PHP_HTTP_BUFFER_LEN(&preface) TSRMLS_CC);
-                               php_http_buffer_dtor(&preface);
-                               output(&ctx, NULL, 0 TSRMLS_CC);
-                               zend_hash_destroy(&ranges);
-                               if (zbody) {
-                                       zval_ptr_dtor(&zbody);
-                               }
-                               return SUCCESS;
-                       }
-                       break;
-       }
-
-       switch (php_http_env_is_response_cached_by_etag(container, ZEND_STRL("If-None-Match"))) {
-               case PHP_HTTP_CACHE_MISS:
-                       break;
-
-               case PHP_HTTP_CACHE_NO:
-                       if (PHP_HTTP_CACHE_HIT != php_http_env_is_response_cached_by_last_modified(container, ZEND_STRL("If-Modified-Since"))) {
-                               break;
-                       }
-
-               case PHP_HTTP_CACHE_HIT:
-                       php_http_env_set_response_code(304 TSRMLS_CC);
-                       if (zbody) {
-                               zval_ptr_dtor(&zbody);
-                       }
-                       return SUCCESS;
-       }
-
-       php_http_message_body_to_callback(body, output, &ctx, 0, 0);
-       output(&ctx, NULL, 0 TSRMLS_CC);
-
-       if (zbody) {
-               zval_ptr_dtor(&zbody);
-       }
-       return SUCCESS;
-}
 
 static PHP_HTTP_STRLIST(php_http_env_response_status) =
        PHP_HTTP_STRLIST_ITEM("Continue")
@@ -1172,22 +561,22 @@ PHP_HTTP_BEGIN_ARGS(setResponseCode, 1)
        PHP_HTTP_ARG_VAL(code, 0)
 PHP_HTTP_END_ARGS;
 
-PHP_HTTP_BEGIN_ARGS(negotiateLanguage, 0)
+PHP_HTTP_BEGIN_ARGS(negotiateLanguage, 1)
        PHP_HTTP_ARG_VAL(supported, 0)
        PHP_HTTP_ARG_VAL(result_array, 1)
 PHP_HTTP_END_ARGS;
 
-PHP_HTTP_BEGIN_ARGS(negotiateContentType, 0)
+PHP_HTTP_BEGIN_ARGS(negotiateContentType, 1)
        PHP_HTTP_ARG_VAL(supported, 0)
        PHP_HTTP_ARG_VAL(result_array, 1)
 PHP_HTTP_END_ARGS;
 
-PHP_HTTP_BEGIN_ARGS(negotiateCharset, 0)
+PHP_HTTP_BEGIN_ARGS(negotiateCharset, 1)
        PHP_HTTP_ARG_VAL(supported, 0)
        PHP_HTTP_ARG_VAL(result_array, 1)
 PHP_HTTP_END_ARGS;
 
-PHP_HTTP_BEGIN_ARGS(negotiate, 0)
+PHP_HTTP_BEGIN_ARGS(negotiate, 2)
        PHP_HTTP_ARG_VAL(value, 0)
        PHP_HTTP_ARG_VAL(supported, 0)
        PHP_HTTP_ARG_VAL(result_array, 1)
@@ -1222,7 +611,6 @@ zend_function_entry php_http_env_method_entry[] = {
 
        PHP_HTTP_ENV_ME(persistentHandlesStat)
        PHP_HTTP_ENV_ME(persistentHandlesClean)
-       PHP_HTTP_ENV_ME(persistentHandlesIdent)
 
        EMPTY_FUNCTION_ENTRY
 };
@@ -1477,21 +865,6 @@ PHP_METHOD(HttpEnv, persistentHandlesClean)
        }
 }
 
-PHP_METHOD(HttpEnv, persistentHandlesIdent)
-{
-       char *ident_str = NULL;
-       int ident_len = 0;
-
-       if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s", &ident_str, &ident_len)) {
-               RETVAL_STRING(zend_ini_string(ZEND_STRS("http.persistent.handles.ident"), 0), 1);
-               if (ident_str && ident_len) {
-                       zend_alter_ini_entry(ZEND_STRS("http.persistent.handles.ident"), ident_str, ident_len, ZEND_INI_USER, PHP_INI_STAGE_RUNTIME);
-               }
-               return;
-       }
-       RETURN_FALSE;
-}
-
 zend_class_entry *php_http_env_request_class_entry;
 
 #undef PHP_HTTP_BEGIN_ARGS
@@ -1521,209 +894,10 @@ PHP_METHOD(HttpEnvRequest, __construct)
        } end_error_handling();
 }
 
-
-zend_class_entry *php_http_env_response_class_entry;
-
-#undef PHP_HTTP_BEGIN_ARGS
-#undef PHP_HTTP_EMPTY_ARGS
-#define PHP_HTTP_BEGIN_ARGS(method, req_args)                  PHP_HTTP_BEGIN_ARGS_EX(HttpEnvResponse, method, 0, req_args)
-#define PHP_HTTP_EMPTY_ARGS(method)                                            PHP_HTTP_EMPTY_ARGS_EX(HttpEnvResponse, method, 0)
-#define PHP_HTTP_ENV_RESPONSE_ME(method, visibility)   PHP_ME(HttpEnvResponse, method, PHP_HTTP_ARGS(HttpEnvResponse, method), visibility)
-
-PHP_HTTP_EMPTY_ARGS(__construct);
-
-PHP_HTTP_BEGIN_ARGS(setContentType, 1)
-       PHP_HTTP_ARG_VAL(content_type, 0)
-PHP_HTTP_END_ARGS;
-
-PHP_HTTP_BEGIN_ARGS(setContentDisposition, 1)
-       PHP_HTTP_ARG_VAL(content_disposition, 0)
-       PHP_HTTP_ARG_VAL(filename, 0)
-PHP_HTTP_END_ARGS;
-
-PHP_HTTP_BEGIN_ARGS(setCacheControl, 1)
-       PHP_HTTP_ARG_VAL(cache_control, 0)
-PHP_HTTP_END_ARGS;
-
-PHP_HTTP_BEGIN_ARGS(setLastModified, 1)
-       PHP_HTTP_ARG_VAL(last_modified, 0)
-PHP_HTTP_END_ARGS;
-
-PHP_HTTP_BEGIN_ARGS(isCachedByLastModified, 0)
-       PHP_HTTP_ARG_VAL(header_name, 0)
-PHP_HTTP_END_ARGS;
-
-PHP_HTTP_BEGIN_ARGS(setEtag, 1)
-       PHP_HTTP_ARG_VAL(etag, 0)
-PHP_HTTP_END_ARGS;
-
-PHP_HTTP_BEGIN_ARGS(isCachedByEtag, 0)
-       PHP_HTTP_ARG_VAL(header_name, 0)
-PHP_HTTP_END_ARGS;
-
-PHP_HTTP_BEGIN_ARGS(setThrottleRate, 1)
-       PHP_HTTP_ARG_VAL(chunk_size, 0)
-       PHP_HTTP_ARG_VAL(delay, 0)
-PHP_HTTP_END_ARGS;
-
-PHP_HTTP_EMPTY_ARGS(send);
-
-
-zend_function_entry php_http_env_response_method_entry[] = {
-       PHP_HTTP_ENV_RESPONSE_ME(__construct, ZEND_ACC_PUBLIC|ZEND_ACC_CTOR)
-       PHP_HTTP_ENV_RESPONSE_ME(setContentType, ZEND_ACC_PUBLIC)
-       PHP_HTTP_ENV_RESPONSE_ME(setContentDisposition, ZEND_ACC_PUBLIC)
-       PHP_HTTP_ENV_RESPONSE_ME(setCacheControl, ZEND_ACC_PUBLIC)
-       PHP_HTTP_ENV_RESPONSE_ME(setLastModified, ZEND_ACC_PUBLIC)
-       PHP_HTTP_ENV_RESPONSE_ME(isCachedByLastModified, ZEND_ACC_PUBLIC)
-       PHP_HTTP_ENV_RESPONSE_ME(setEtag, ZEND_ACC_PUBLIC)
-       PHP_HTTP_ENV_RESPONSE_ME(isCachedByEtag, ZEND_ACC_PUBLIC)
-       PHP_HTTP_ENV_RESPONSE_ME(setThrottleRate, ZEND_ACC_PUBLIC)
-
-       PHP_HTTP_ENV_RESPONSE_ME(send, ZEND_ACC_PUBLIC)
-
-       EMPTY_FUNCTION_ENTRY
-};
-
-
-PHP_METHOD(HttpEnvResponse, __construct)
-{
-       with_error_handling(EH_THROW, php_http_exception_class_entry) {
-               if (SUCCESS == zend_parse_parameters_none()) {
-                       php_http_message_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
-
-                       with_error_handling(EH_THROW, php_http_exception_class_entry) {
-                               obj->message = php_http_message_init_env(obj->message, PHP_HTTP_RESPONSE TSRMLS_CC);
-                       } end_error_handling();
-               }
-       } end_error_handling();
-
-}
-
-PHP_METHOD(HttpEnvResponse, setContentType)
-{
-       char *ct_str;
-       int ct_len;
-
-       if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &ct_str, &ct_len)) {
-               RETURN_SUCCESS(php_http_env_set_response_content_type(getThis(), ct_str, ct_len, NULL TSRMLS_CC));
-       }
-       RETURN_FALSE;
-}
-
-PHP_METHOD(HttpEnvResponse, setContentDisposition)
-{
-       long cd;
-       char *file_str = NULL;
-       int file_len = 0;
-
-       if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l|s!", &cd, &file_str, &file_len)) {
-               RETURN_SUCCESS(php_http_env_set_response_content_disposition(getThis(), cd, file_str, file_len, NULL TSRMLS_CC));
-       }
-       RETURN_FALSE;
-}
-
-PHP_METHOD(HttpEnvResponse, setCacheControl)
-{
-       char *cc_str;
-       int cc_len;
-
-       if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &cc_str, &cc_len)) {
-               RETURN_SUCCESS(php_http_env_set_response_cache_control(getThis(), cc_str, cc_len, NULL TSRMLS_CC));
-       }
-       RETURN_FALSE;
-}
-
-PHP_METHOD(HttpEnvResponse, setLastModified)
-{
-       long last_modified;
-
-       if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &last_modified)) {
-               RETURN_SUCCESS(php_http_env_set_response_last_modified(getThis(), last_modified, NULL TSRMLS_CC));
-       }
-       RETURN_FALSE;
-}
-
-PHP_METHOD(HttpEnvResponse, isCachedByLastModified)
-{
-       char *header_name_str = NULL;
-       int header_name_len = 0;
-
-       if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s!", &header_name_str, &header_name_len)) {
-               if (!header_name_str || !header_name_len) {
-                       header_name_str = "If-Modified-Since";
-                       header_name_len = lenof("If-Modified-Since");
-               }
-               RETURN_LONG(php_http_env_is_response_cached_by_last_modified(getThis(), header_name_str, header_name_len TSRMLS_CC));
-       }
-       RETURN_FALSE;
-}
-
-PHP_METHOD(HttpEnvResponse, setEtag)
-{
-       char *etag_str;
-       int etag_len;
-
-       if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s!", &etag_str, &etag_len)) {
-               RETURN_SUCCESS(php_http_env_set_response_etag(getThis(), etag_str, etag_len, NULL TSRMLS_CC));
-       }
-       RETURN_FALSE;
-}
-
-PHP_METHOD(HttpEnvResponse, isCachedByEtag)
-{
-       char *header_name_str = NULL;
-       int header_name_len = 0;
-
-       if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &header_name_str, &header_name_len)) {
-               if (!header_name_str || !header_name_len) {
-                       header_name_str = "If-None-Match";
-                       header_name_len = lenof("If-None-Match");
-               }
-               RETURN_LONG(php_http_env_is_response_cached_by_etag(getThis(), header_name_str, header_name_len TSRMLS_CC));
-       }
-       RETURN_FALSE;
-}
-
-PHP_METHOD(HttpEnvResponse, setThrottleRate)
-{
-       long chunk_size;
-       double delay = 1;
-
-       if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l|d", &chunk_size, &delay)) {
-               php_http_env_set_response_throttle_rate(getThis(), chunk_size, delay TSRMLS_CC);
-               RETURN_TRUE;
-       }
-       RETURN_FALSE;
-}
-
-PHP_METHOD(HttpEnvResponse, send)
-{
-       if (SUCCESS == zend_parse_parameters_none()) {
-               RETURN_SUCCESS(php_http_env_send_response(getThis() TSRMLS_CC));
-       }
-       RETURN_FALSE;
-}
-
-
 PHP_MINIT_FUNCTION(http_env)
 {
        PHP_HTTP_REGISTER_CLASS(http, Env, http_env, NULL, 0);
        PHP_HTTP_REGISTER_CLASS(http\\env, Request, http_env_request, php_http_message_class_entry, 0);
-       PHP_HTTP_REGISTER_CLASS(http\\env, Response, http_env_response, php_http_message_class_entry, 0);
-
-       zend_declare_class_constant_long(php_http_env_response_class_entry, ZEND_STRL("CONTENT_DISPOSITION_INLINE"), PHP_HTTP_CONTENT_DISPOSITION_INLINE TSRMLS_CC);
-       zend_declare_class_constant_long(php_http_env_response_class_entry, ZEND_STRL("CONTENT_DISPOSITION_ATTACHMENT"), PHP_HTTP_CONTENT_DISPOSITION_ATTACHMENT TSRMLS_CC);
-
-       zend_declare_class_constant_long(php_http_env_response_class_entry, ZEND_STRL("CACHE_NO"), PHP_HTTP_CACHE_NO TSRMLS_CC);
-       zend_declare_class_constant_long(php_http_env_response_class_entry, ZEND_STRL("CACHE_HIT"), PHP_HTTP_CACHE_HIT TSRMLS_CC);
-       zend_declare_class_constant_long(php_http_env_response_class_entry, ZEND_STRL("CACHE_MISS"), PHP_HTTP_CACHE_MISS TSRMLS_CC);
-
-       zend_declare_property_null(php_http_env_response_class_entry, ZEND_STRL("contentType"), ZEND_ACC_PROTECTED TSRMLS_CC);
-       zend_declare_property_null(php_http_env_response_class_entry, ZEND_STRL("etag"), ZEND_ACC_PROTECTED TSRMLS_CC);
-       zend_declare_property_null(php_http_env_response_class_entry, ZEND_STRL("lastModified"), ZEND_ACC_PROTECTED TSRMLS_CC);
-       zend_declare_property_null(php_http_env_response_class_entry, ZEND_STRL("throttleDelay"), ZEND_ACC_PROTECTED TSRMLS_CC);
-       zend_declare_property_null(php_http_env_response_class_entry, ZEND_STRL("throttleChunk"), ZEND_ACC_PROTECTED TSRMLS_CC);
 
        return SUCCESS;
 }
index e78f7e3a80c5893e5548449703ce045e275d743e..94fff29c28aa2395aa6fe2b75bdc445266829d56 100644 (file)
@@ -23,16 +23,6 @@ struct php_http_env_globals {
        zval *server_var;
        char *etag_mode;
 
-       struct {
-               char *content_type;
-               php_http_message_body_t *body;
-               char *etag;
-               time_t last_modified;
-               double throttle_delay;
-               size_t throttle_chunk;
-               php_http_encoding_stream_t *deflate;
-       } response;
-
        struct {
                time_t time;
                HashTable *headers;
@@ -40,6 +30,11 @@ struct php_http_env_globals {
        } request;
 };
 
+typedef enum php_http_content_encoding {
+       PHP_HTTP_CONTENT_ENCODING_NONE,
+       PHP_HTTP_CONTENT_ENCODING_GZIP
+} php_http_content_encoding_t;
+
 typedef enum php_http_range_status {
        PHP_HTTP_RANGE_NO,
        PHP_HTTP_RANGE_OK,
@@ -77,17 +72,6 @@ PHP_HTTP_API STATUS php_http_env_set_response_header_format(long http_code, zend
 PHP_HTTP_API zval *php_http_env_get_server_var(const char *key_str, size_t key_len, zend_bool check TSRMLS_DC);
 #define php_http_env_got_server_var(v) (NULL != php_http_env_get_server_var((v), strlen(v), 1 TSRMLS_CC))
 
-PHP_HTTP_API STATUS php_http_env_set_response_last_modified(zval *container, time_t lm, char **sent_header TSRMLS_DC);
-PHP_HTTP_API STATUS php_http_env_set_response_etag(zval *container, const char *etag_str, size_t etag_len, char **sent_header TSRMLS_DC);
-PHP_HTTP_API STATUS php_http_env_set_response_content_type(zval *container, const char *ct_str, size_t ct_len, char **sent_header TSRMLS_DC);
-PHP_HTTP_API STATUS php_http_env_set_response_content_disposition(zval *container, php_http_content_disposition_t d, const char *f_str, size_t f_len, char **sent_header TSRMLS_DC);
-PHP_HTTP_API STATUS php_http_env_set_response_cache_control(zval *container, const char *cc_str, size_t cc_len, char **sent_header TSRMLS_DC);
-PHP_HTTP_API void php_http_env_set_response_throttle_rate(zval *container, size_t chunk_size, double delay TSRMLS_DC);
-PHP_HTTP_API void php_http_env_set_response_body(zval *container, php_http_message_body_t *body);
-PHP_HTTP_API STATUS php_http_env_send_response(zval *container TSRMLS_DC);
-PHP_HTTP_API php_http_cache_status_t php_http_env_is_response_cached_by_etag(zval *container, const char *header_str, size_t header_len TSRMLS_DC);
-PHP_HTTP_API php_http_cache_status_t php_http_env_is_response_cached_by_last_modified(zval *container, const char *header_str, size_t header_len TSRMLS_DC);
-
 extern zend_class_entry *php_http_env_class_entry;
 extern zend_function_entry php_http_env_method_entry[];
 
@@ -104,27 +88,12 @@ PHP_METHOD(HttpEnv, negotiateContentType);
 PHP_METHOD(HttpEnv, negotiate);
 PHP_METHOD(HttpEnv, persistentHandlesStat);
 PHP_METHOD(HttpEnv, persistentHandlesClean);
-PHP_METHOD(HttpEnv, persistentHandlesIdent);
 
 extern zend_class_entry *php_http_env_request_class_entry;
 extern zend_function_entry php_http_env_request_method_entry[];
 
 PHP_METHOD(HttpEnvRequest, __construct);
 
-extern zend_class_entry *php_http_env_response_class_entry;
-extern zend_function_entry php_http_env_response_method_entry[];
-
-PHP_METHOD(HttpEnvResponse, __construct);
-PHP_METHOD(HttpEnvResponse, setContentType);
-PHP_METHOD(HttpEnvResponse, setContentDisposition);
-PHP_METHOD(HttpEnvResponse, setCacheControl);
-PHP_METHOD(HttpEnvResponse, setLastModified);
-PHP_METHOD(HttpEnvResponse, isCachedByLastModified);
-PHP_METHOD(HttpEnvResponse, setEtag);
-PHP_METHOD(HttpEnvResponse, isCachedByEtag);
-PHP_METHOD(HttpEnvResponse, setThrottleRate);
-PHP_METHOD(HttpEnvResponse, send);
-
 PHP_MINIT_FUNCTION(http_env);
 PHP_RINIT_FUNCTION(http_env);
 PHP_RSHUTDOWN_FUNCTION(http_env);
diff --git a/php_http_env_response.c b/php_http_env_response.c
new file mode 100644 (file)
index 0000000..40fa941
--- /dev/null
@@ -0,0 +1,928 @@
+/*
+    +--------------------------------------------------------------------+
+    | PECL :: http                                                       |
+    +--------------------------------------------------------------------+
+    | Redistribution and use in source and binary forms, with or without |
+    | modification, are permitted provided that the conditions mentioned |
+    | in the accompanying LICENSE file are met.                          |
+    +--------------------------------------------------------------------+
+    | Copyright (c) 2004-2010, Michael Wallner <mike@php.net>            |
+    +--------------------------------------------------------------------+
+*/
+
+/* $Id $ */
+
+#include "php_http.h"
+
+#include <main/SAPI.h>
+#include <ext/date/php_date.h>
+#include <ext/standard/php_string.h>
+
+
+static void set_option(zval *options, const char *name_str, size_t name_len, int type, const void *value_ptr, size_t value_len TSRMLS_DC)
+{
+       if (Z_TYPE_P(options) == IS_OBJECT) {
+               /* stupid non-const api */
+               char *name = estrndup(name_str, name_len);
+               if (value_ptr) {
+                       switch (type) {
+                               case IS_DOUBLE:
+                                       zend_update_property_double(Z_OBJCE_P(options), options, name, name_len, *(double *)value_ptr TSRMLS_CC);
+                                       break;
+                               case IS_LONG:
+                                       zend_update_property_long(Z_OBJCE_P(options), options, name, name_len, *(long *)value_ptr TSRMLS_CC);
+                                       break;
+                               case IS_STRING:
+                                       zend_update_property_stringl(Z_OBJCE_P(options), options, name, name_len, value_ptr, value_len TSRMLS_CC);
+                                       break;
+                       }
+               } else {
+                       zend_update_property_null(Z_OBJCE_P(options), options, name, name_len TSRMLS_CC);
+               }
+               efree(name);
+       } else {
+               convert_to_array(options);
+               if (value_ptr) {
+                       switch (type) {
+                               case IS_DOUBLE:
+                                       add_assoc_double_ex(options, name_str, name_len + 1, *(double *)value_ptr);
+                                       break;
+                               case IS_LONG:
+                                       add_assoc_long_ex(options, name_str, name_len + 1, *(long *)value_ptr);
+                                       break;
+                               case IS_STRING: {
+                                       char *value = estrndup(value_ptr, value_len);
+                                       add_assoc_stringl_ex(options, name_str, name_len + 1, value, value_len, 0);
+                                       break;
+                               }
+                       }
+               } else {
+                       add_assoc_null_ex(options, name_str, name_len + 1);
+               }
+       }
+}
+static zval *get_option(zval *options, const char *name_str, size_t name_len TSRMLS_CC)
+{
+       zval *val, **valptr;
+
+       if (Z_TYPE_P(options) == IS_OBJECT) {
+               char *name = estrndup(name_str, name_len);
+               val = zend_read_property(Z_OBJCE_P(options), options, name, name_len, 0 TSRMLS_CC);
+               efree(name);
+       } else {
+               if (SUCCESS == zend_hash_find(Z_ARRVAL_P(options), name_str, name_len + 1, (void *) &valptr)) {
+                       val = *valptr;
+               } else {
+                       val = NULL;
+               }
+       }
+       if (val) {
+               Z_ADDREF_P(val);
+       }
+       return val;
+}
+
+PHP_HTTP_API php_http_cache_status_t php_http_env_is_response_cached_by_etag(zval *options, const char *header_str, size_t header_len TSRMLS_DC)
+{
+       int ret, free_etag = 0;
+       char *header, *etag;
+       zval *zetag, *zbody = NULL;
+
+       if (    !(header = php_http_env_get_request_header(header_str, header_len TSRMLS_CC))
+       ||              !(zbody = get_option(options, ZEND_STRL("body") TSRMLS_CC))
+       ||              !(Z_TYPE_P(zbody) == IS_OBJECT)
+       ||              !instanceof_function(Z_OBJCE_P(zbody), php_http_message_body_class_entry TSRMLS_CC)
+       ) {
+               STR_FREE(header);
+               if (zbody) {
+                       zval_ptr_dtor(&zbody);
+               }
+               return PHP_HTTP_CACHE_NO;
+       }
+
+       if ((zetag = get_option(options, ZEND_STRL("etag") TSRMLS_CC))) {
+               zval *zetag_copy = php_http_ztyp(IS_STRING, zetag);
+               zval_ptr_dtor(&zetag);
+               zetag = zetag_copy;
+       }
+
+       if (zetag && Z_STRLEN_P(zetag)) {
+               etag = Z_STRVAL_P(zetag);
+       } else {
+               etag = php_http_message_body_etag(((php_http_message_body_object_t *) zend_object_store_get_object(zbody TSRMLS_CC))->body);
+               set_option(options, ZEND_STRL("etag"), IS_STRING, etag, strlen(etag) TSRMLS_CC);
+               free_etag = 1;
+       }
+
+       if (zbody) {
+               zval_ptr_dtor(&zbody);
+       }
+
+       if (zetag) {
+               zval_ptr_dtor(&zetag);
+       }
+
+       ret = php_http_match(header, etag, PHP_HTTP_MATCH_WORD);
+
+       if (free_etag) {
+               efree(etag);
+       }
+       efree(header);
+
+       return ret ? PHP_HTTP_CACHE_HIT : PHP_HTTP_CACHE_MISS;
+}
+
+PHP_HTTP_API php_http_cache_status_t php_http_env_is_response_cached_by_last_modified(zval *options, const char *header_str, size_t header_len TSRMLS_DC)
+{
+       char *header;
+       time_t ums, lm = 0;
+       zval *zbody = NULL, *zlm;
+
+       if (    !(header = php_http_env_get_request_header(header_str, header_len TSRMLS_CC))
+       ||              !(zbody = get_option(options, ZEND_STRL("body") TSRMLS_CC))
+       ||              !(Z_TYPE_P(zbody) == IS_OBJECT)
+       ||              !instanceof_function(Z_OBJCE_P(zbody), php_http_message_body_class_entry TSRMLS_CC)
+       ) {
+               STR_FREE(header);
+               if (zbody) {
+                       zval_ptr_dtor(&zbody);
+               }
+               return PHP_HTTP_CACHE_NO;
+       }
+
+       if ((zlm = get_option(options, ZEND_STRL("lastModified") TSRMLS_CC))) {
+               zval *zlm_copy = php_http_ztyp(IS_LONG, zlm);
+               zval_ptr_dtor(&zlm);
+               zlm = zlm_copy;
+       }
+
+       if (zlm && Z_LVAL_P(zlm) > 0) {
+               lm = Z_LVAL_P(zlm);
+       } else {
+               lm = php_http_message_body_mtime(((php_http_message_body_object_t *) zend_object_store_get_object(zbody TSRMLS_CC))->body);
+               set_option(options, ZEND_STRL("lastModified"), IS_LONG, &lm, 0 TSRMLS_CC);
+       }
+
+       if (zlm) {
+               zval_ptr_dtor(&zlm);
+       }
+
+       ums = php_parse_date(header, NULL TSRMLS_CC);
+       efree(header);
+
+       if (ums > 0 && ums <= lm) {
+               return PHP_HTTP_CACHE_HIT;
+       } else {
+               return PHP_HTTP_CACHE_MISS;
+       }
+}
+
+static size_t output(void *context, const char *buf, size_t len TSRMLS_DC)
+{
+       php_http_env_response_t *r = context;
+       TSRMLS_FETCH_FROM_CTX(r->ts);
+
+       PHPWRITE(buf, len);
+
+       /*      we really only need to flush when throttling is enabled,
+               because we push the data as fast as possible anyway if not */
+       if (r->throttle.delay >= PHP_HTTP_DIFFSEC) {
+               if (php_output_get_level(TSRMLS_C)) {
+                       php_output_flush_all(TSRMLS_C);
+               }
+               if (!(php_output_get_status(TSRMLS_C) & PHP_OUTPUT_IMPLICITFLUSH)) {
+                       sapi_flush(TSRMLS_C);
+               }
+               php_http_sleep(r->throttle.delay);
+       }
+       return len;
+}
+
+#define php_http_env_response_send_done(r) php_http_env_response_send_data((r), NULL, 0)
+static STATUS php_http_env_response_send_data(php_http_env_response_t *r, const char *buf, size_t len)
+{
+       TSRMLS_FETCH_FROM_CTX(r->ts);
+       size_t chunk = r->throttle.chunk ? r->throttle.chunk : PHP_HTTP_SENDBUF_SIZE;
+
+       if (r->content.encoder) {
+               char *enc_str = NULL;
+               size_t enc_len = 0;
+
+               if (buf) {
+                       if (SUCCESS != php_http_encoding_stream_update(r->content.encoder, buf, len, &enc_str, &enc_len)) {
+                               return FAILURE;
+                       }
+               } else {
+                       if (SUCCESS != php_http_encoding_stream_finish(r->content.encoder, &enc_str, &enc_len)) {
+                               return FAILURE;
+                       }
+               }
+
+               if (enc_str) {
+                       php_http_buffer_chunked_output(&r->buffer, enc_str, enc_len, buf ? chunk : 0, output, r TSRMLS_CC);
+                       STR_FREE(enc_str);
+               }
+       } else {
+               php_http_buffer_chunked_output(&r->buffer, buf, len, buf ? chunk : 0, output, r TSRMLS_CC);
+       }
+
+       return SUCCESS;
+}
+
+PHP_HTTP_API php_http_env_response_t *php_http_env_response_init(php_http_env_response_t *r, zval *options TSRMLS_DC)
+{
+       php_http_env_response_t *free_r = NULL;
+
+       if (!r) {
+               r = free_r = emalloc(sizeof(*r));
+       }
+       memset(r, 0, sizeof(*r));
+
+       r->buffer = php_http_buffer_init(NULL);
+
+       Z_ADDREF_P(options);
+       r->options = options;
+
+       TSRMLS_SET_CTX(r->ts);
+
+       return r;
+}
+
+PHP_HTTP_API void php_http_env_response_dtor(php_http_env_response_t *r)
+{
+       php_http_buffer_free(&r->buffer);
+       zval_ptr_dtor(&r->options);
+       STR_FREE(r->content.type);
+       STR_FREE(r->content.encoding);
+       if (r->content.encoder) {
+               php_http_encoding_stream_free(&r->content.encoder);
+       }
+}
+
+PHP_HTTP_API void php_http_env_response_free(php_http_env_response_t **r)
+{
+       if (*r) {
+               php_http_env_response_dtor(*r);
+               efree(*r);
+               *r = NULL;
+       }
+}
+
+static STATUS php_http_env_response_send_head(php_http_env_response_t *r)
+{
+       STATUS ret = SUCCESS;
+       zval *zoption, *options = r->options;
+       TSRMLS_FETCH_FROM_CTX(r->ts);
+
+       if (r->done) {
+               return ret;
+       }
+
+       if ((zoption = get_option(options, ZEND_STRL("responseCode") TSRMLS_CC))) {
+               zval *zoption_copy = php_http_ztyp(IS_LONG, zoption);
+
+               zval_ptr_dtor(&zoption);
+               if (Z_LVAL_P(zoption_copy) > 0) {
+                       ret = php_http_env_set_response_code(Z_LVAL_P(zoption_copy) TSRMLS_CC);
+               }
+               zval_ptr_dtor(&zoption_copy);
+       }
+
+       if (ret != SUCCESS) {
+               return ret;
+       }
+
+       if ((zoption = get_option(options, ZEND_STRL("httpVersion") TSRMLS_CC))) {
+               php_http_version_t v;
+               zval *zoption_copy = php_http_ztyp(IS_STRING, zoption);
+
+               zval_ptr_dtor(&zoption);
+               if (Z_STRLEN_P(zoption_copy) && php_http_version_parse(&v, Z_STRVAL_P(zoption_copy) TSRMLS_CC)) {
+                       ret = php_http_env_set_response_protocol_version(&v TSRMLS_CC);
+                       php_http_version_dtor(&v);
+               }
+               zval_ptr_dtor(&zoption_copy);
+       }
+
+       if (ret != SUCCESS) {
+               return ret;
+       }
+
+       if ((zoption = get_option(options, ZEND_STRL("headers") TSRMLS_CC))) {
+               if (Z_TYPE_P(zoption) == IS_ARRAY) {
+                       zval **val;
+                       HashPosition pos;
+                       php_http_array_hashkey_t key = php_http_array_hashkey_init(0);
+
+                       FOREACH_KEYVAL(pos, zoption, key, val) {
+                               if (key.type == HASH_KEY_IS_STRING) {
+                                       if (SUCCESS != (ret = php_http_env_set_response_header_value(0, key.str, key.len - 1, *val, 1 TSRMLS_CC))) {
+                                               break;
+                                       }
+                               }
+                       }
+               }
+               zval_ptr_dtor(&zoption);
+       }
+
+       if (ret != SUCCESS) {
+               return ret;
+       }
+
+       if ((zoption = get_option(options, ZEND_STRL("contentType") TSRMLS_CC))) {
+               zval *zoption_copy = php_http_ztyp(IS_STRING, zoption);
+
+               zval_ptr_dtor(&zoption);
+               if (Z_STRLEN_P(zoption_copy)) {
+                       PHP_HTTP_CHECK_CONTENT_TYPE(Z_STRVAL_P(zoption_copy), ret = FAILURE) else {
+                               if (SUCCESS == (ret = php_http_env_set_response_header_format(0, 1 TSRMLS_CC, "Content-Type: %.*s", Z_STRLEN_P(zoption_copy), Z_STRVAL_P(zoption_copy)))) {
+                                       r->content.type = estrndup(Z_STRVAL_P(zoption_copy), Z_STRLEN_P(zoption_copy));
+                               }
+                       }
+               }
+               zval_ptr_dtor(&zoption_copy);
+       }
+
+       if (ret != SUCCESS) {
+               return ret;
+       }
+
+       if (r->range.status == PHP_HTTP_RANGE_OK) {
+               if (zend_hash_num_elements(&r->range.values) == 1) {
+                       zval **range, **begin, **end;
+
+                       if (SUCCESS != zend_hash_index_find(&r->range.values, 0, (void *) &range)
+                       ||      SUCCESS != zend_hash_index_find(Z_ARRVAL_PP(range), 0, (void *) &begin)
+                       ||      SUCCESS != zend_hash_index_find(Z_ARRVAL_PP(range), 1, (void *) &end)
+                       ) {
+                               /* this should never happen */
+                               zend_hash_destroy(&r->range.values);
+                               php_http_env_set_response_code(500 TSRMLS_CC);
+                               ret = FAILURE;
+                       } else {
+                               ret = php_http_env_set_response_header_format(206, 1 TSRMLS_CC, "Content-Range: bytes %ld-%ld/%zu", Z_LVAL_PP(begin), Z_LVAL_PP(end), r->content.length);
+                       }
+               } else {
+                       php_http_boundary(r->range.boundary, sizeof(r->range.boundary));
+                       ret = php_http_env_set_response_header_format(206, 1 TSRMLS_CC, "Content-Type: multipart/byteranges; boundary=%s", r->range.boundary);
+               }
+       } else {
+               if ((zoption = get_option(options, ZEND_STRL("cacheControl") TSRMLS_CC))) {
+                       zval *zoption_copy = php_http_ztyp(IS_STRING, zoption);
+
+                       zval_ptr_dtor(&zoption);
+                       if (Z_STRLEN_P(zoption_copy)) {
+                               ret = php_http_env_set_response_header_format(0, 1 TSRMLS_CC, "Cache-Control: %.*s", Z_STRLEN_P(zoption_copy), Z_STRVAL_P(zoption_copy) TSRMLS_CC);
+                       }
+                       zval_ptr_dtor(&zoption_copy);
+               }
+
+               if (ret != SUCCESS) {
+                       return ret;
+               }
+
+               if ((zoption = get_option(options, ZEND_STRL("contentDisposition") TSRMLS_CC))) {
+                       zval *zoption_copy = php_http_ztyp(IS_ARRAY, zoption);
+                       zval **zdisposition, **zfilename = NULL;
+
+                       zval_ptr_dtor(&zoption);
+                       if (SUCCESS == zend_hash_find(Z_ARRVAL_P(zoption_copy), ZEND_STRS("disposition"), (void *) &zdisposition)) {
+                               zval *zdisposition_copy = php_http_ztyp(IS_LONG, *zdisposition);
+                               char *tmp = NULL;
+
+                               switch (Z_LVAL_P(zdisposition_copy)) {
+                                       case PHP_HTTP_CONTENT_DISPOSITION_NONE:
+                                               ret = php_http_env_set_response_header_value(0, ZEND_STRL("Content-Disposition"), NULL, 1 TSRMLS_CC);
+                                       case PHP_HTTP_CONTENT_DISPOSITION_INLINE:
+                                               tmp = "inline";
+                                               break;
+                                       case PHP_HTTP_CONTENT_DISPOSITION_ATTACHMENT:
+                                               tmp = "attachment";
+                                               break;
+                               }
+
+                               if (tmp) {
+                                       if (SUCCESS != zend_hash_find(Z_ARRVAL_P(zoption_copy), ZEND_STRS("filename"), (void *) &zfilename)) {
+                                               ret = php_http_env_set_response_header_format(0, 1 TSRMLS_CC, "Content-Disposition: %s", tmp);
+                                       } else {
+                                               zval *zfilename_copy = php_http_ztyp(IS_STRING, *zfilename);
+
+                                               if (!Z_STRLEN_P(zfilename_copy)) {
+                                                       ret = php_http_env_set_response_header_format(0, 1 TSRMLS_CC, "Content-Disposition: %s", tmp);
+                                               } else {
+                                                       int new_f_len;
+                                                       char *new_f_str = php_addslashes(estrndup(Z_STRVAL_P(zfilename_copy), Z_STRLEN_P(zfilename_copy)), Z_STRLEN_P(zfilename_copy), &new_f_len, 0 TSRMLS_CC);
+
+                                                       ret = php_http_env_set_response_header_format(0, 1 TSRMLS_CC, "Content-Disposition: %s; filename=\"%.*s\"", tmp, new_f_len, new_f_str);
+                                                       STR_FREE(new_f_str);
+                                               }
+
+                                               zval_ptr_dtor(&zfilename_copy);
+                                       }
+                               }
+                               zval_ptr_dtor(&zdisposition_copy);
+                       }
+                       zval_ptr_dtor(&zoption_copy);
+               }
+
+               if (ret != SUCCESS) {
+                       return ret;
+               }
+
+               if ((zoption = get_option(options, ZEND_STRL("contentEncoding") TSRMLS_CC))) {
+                       zval *zoption_copy = php_http_ztyp(IS_LONG, zoption);
+                       zval zsupported;
+                       HashTable *result = NULL;
+
+                       zval_ptr_dtor(&zoption);
+                       switch (Z_LVAL_P(zoption_copy)) {
+                               case PHP_HTTP_CONTENT_ENCODING_GZIP:
+                                       INIT_PZVAL(&zsupported);
+                                       array_init(&zsupported);
+                                       add_next_index_stringl(&zsupported, ZEND_STRL("none"), 1);
+                                       add_next_index_stringl(&zsupported, ZEND_STRL("gzip"), 1);
+                                       add_next_index_stringl(&zsupported, ZEND_STRL("deflate"), 1);
+
+                                       if ((result = php_http_negotiate_encoding(Z_ARRVAL(zsupported) TSRMLS_CC))) {
+                                               char *key_str = NULL;
+                                               uint key_len = 0;
+
+                                               zend_hash_internal_pointer_reset(result);
+                                               if (HASH_KEY_IS_STRING == zend_hash_get_current_key_ex(result, &key_str, &key_len, NULL, 0, NULL)) {
+                                                       if (!strcmp(key_str, "gzip")) {
+                                                               if (!(r->content.encoder = php_http_encoding_stream_init(NULL, php_http_encoding_stream_get_deflate_ops(), PHP_HTTP_DEFLATE_TYPE_GZIP))) {
+                                                                       ret = FAILURE;
+                                                               } else if (SUCCESS == (ret = php_http_env_set_response_header(0, ZEND_STRL("Content-Encoding: gzip"), 1 TSRMLS_CC))) {
+                                                                       r->content.encoding = estrndup(key_str, key_len - 1);
+                                                               }
+                                                       } else if (!strcmp(key_str, "deflate")) {
+                                                               if (!(r->content.encoder = php_http_encoding_stream_init(NULL, php_http_encoding_stream_get_deflate_ops(), PHP_HTTP_DEFLATE_TYPE_ZLIB))) {
+                                                                       ret = FAILURE;
+                                                               } else if (SUCCESS == (ret = php_http_env_set_response_header(0, ZEND_STRL("Content-Encoding: deflate"), 1 TSRMLS_CC))) {
+                                                                       r->content.encoding = estrndup(key_str, key_len - 1);
+                                                               }
+                                                       } else {
+                                                               ret = php_http_env_set_response_header_value(0, ZEND_STRL("Content-Encoding"), NULL, 0 TSRMLS_CC);
+                                                       }
+
+                                                       if (SUCCESS == ret) {
+                                                               ret = php_http_env_set_response_header(0, ZEND_STRL("Vary: Accept-Encoding"), 0 TSRMLS_CC);
+                                                       }
+                                               }
+
+                                               zend_hash_destroy(result);
+                                               FREE_HASHTABLE(result);
+                                       }
+
+                                       zval_dtor(&zsupported);
+                                       break;
+
+                               case PHP_HTTP_CONTENT_ENCODING_NONE:
+                               default:
+                                       ret = php_http_env_set_response_header_value(0, ZEND_STRL("Content-Encoding"), NULL, 0 TSRMLS_CC);
+                                       break;
+                       }
+                       zval_ptr_dtor(&zoption_copy);
+               }
+
+               if (SUCCESS != ret) {
+                       return ret;
+               }
+
+               switch (php_http_env_is_response_cached_by_etag(options, ZEND_STRL("If-None-Match"))) {
+                       case PHP_HTTP_CACHE_MISS:
+                               break;
+
+                       case PHP_HTTP_CACHE_NO:
+                               if (PHP_HTTP_CACHE_HIT != php_http_env_is_response_cached_by_last_modified(options, ZEND_STRL("If-Modified-Since"))) {
+                                       break;
+                               }
+
+                       case PHP_HTTP_CACHE_HIT:
+                               ret = php_http_env_set_response_code(304 TSRMLS_CC);
+                               r->done = 1;
+                               break;
+               }
+
+               if ((zoption = get_option(options, ZEND_STRL("etag") TSRMLS_CC))) {
+                       ret = php_http_env_set_response_header_value(0, ZEND_STRL("ETag"), zoption, 1 TSRMLS_CC);
+                       zval_ptr_dtor(&zoption);
+               }
+               if ((zoption = get_option(options, ZEND_STRL("lastModified") TSRMLS_CC))) {
+                       zval *zoption_copy = php_http_ztyp(IS_LONG, zoption);
+
+                       zval_ptr_dtor(&zoption);
+                       if (Z_LVAL_P(zoption_copy)) {
+                               char *date = php_format_date(ZEND_STRL(PHP_HTTP_DATE_FORMAT), Z_LVAL_P(zoption_copy), 0 TSRMLS_CC);
+                               if (date) {
+                                       ret = php_http_env_set_response_header_format(0, 1 TSRMLS_CC, "Last-Modified: %s", date);
+                                       efree(date);
+                               }
+                       }
+                       zval_ptr_dtor(&zoption_copy);
+               }
+       }
+
+       return ret;
+}
+
+static STATUS php_http_env_response_send_body(php_http_env_response_t *r)
+{
+       STATUS ret = SUCCESS;
+       zval *zbody;
+
+       if (r->done) {
+               return ret;
+       }
+
+       if (    (zbody = get_option(r->options, ZEND_STRL("body") TSRMLS_CC))
+       &&              (Z_TYPE_P(zbody) == IS_OBJECT)
+       &&              instanceof_function(Z_OBJCE_P(zbody), php_http_message_body_class_entry TSRMLS_CC)
+       ) {
+               php_http_message_body_object_t *obj = zend_object_store_get_object(zbody TSRMLS_CC);
+
+               if (r->range.status == PHP_HTTP_RANGE_OK) {
+                       if (zend_hash_num_elements(&r->range.values) == 1) {
+                               /* single range */
+                               zval **range, **begin, **end;
+
+                               if (SUCCESS != zend_hash_index_find(&r->range.values, 0, (void *) &range)
+                               ||      SUCCESS != zend_hash_index_find(Z_ARRVAL_PP(range), 0, (void *) &begin)
+                               ||      SUCCESS != zend_hash_index_find(Z_ARRVAL_PP(range), 1, (void *) &end)
+                               ) {
+                                       /* this should never happen */
+                                       zend_hash_destroy(&r->range.values);
+                                       php_http_env_set_response_code(500 TSRMLS_CC);
+                                       ret = FAILURE;
+                               } else {
+                                       /* send chunk */
+                                       php_http_message_body_to_callback(obj->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);
+                                       php_http_env_response_send_done(r);
+                                       zend_hash_destroy(&r->range.values);
+                                       ret = SUCCESS;
+                               }
+                       } else {
+                               /* send multipart/byte-ranges message */
+                               HashPosition pos;
+                               zval **chunk;
+
+                               FOREACH_HASH_VAL(pos, &r->range.values, chunk) {
+                                       zval **begin, **end;
+
+                                       if (IS_ARRAY == Z_TYPE_PP(chunk)
+                                       &&      SUCCESS == zend_hash_index_find(Z_ARRVAL_PP(chunk), 0, (void *) &begin)
+                                       &&      IS_LONG == Z_TYPE_PP(begin)
+                                       &&      SUCCESS == zend_hash_index_find(Z_ARRVAL_PP(chunk), 1, (void *) &end)
+                                       &&      IS_LONG == Z_TYPE_PP(end)
+                                       ) {
+                                               php_http_buffer_appendf(r->buffer,
+                                                               PHP_HTTP_CRLF
+                                                               "--%s" PHP_HTTP_CRLF
+                                                               "Content-Type: %s" PHP_HTTP_CRLF
+                                                               "Content-Range: bytes %ld-%ld/%zu" PHP_HTTP_CRLF PHP_HTTP_CRLF,
+                                                               /* - */
+                                                               r->range.boundary,
+                                                               r->content.type ? r->content.type : "application/octet-stream",
+                                                               Z_LVAL_PP(begin),
+                                                               Z_LVAL_PP(end),
+                                                               r->content.length
+                                               );
+                                               php_http_message_body_to_callback(obj->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);
+                                       }
+                               }
+                               php_http_buffer_appendf(r->buffer, PHP_HTTP_CRLF "--%s--", r->range.boundary);
+                               php_http_env_response_send_done(r);
+                               zend_hash_destroy(&r->range.values);
+                       }
+
+               } else {
+                       php_http_message_body_to_callback(obj->body, (php_http_pass_callback_t) php_http_env_response_send_data, r, 0, 0);
+                       php_http_env_response_send_done(r);
+               }
+       }
+       if (zbody) {
+               zval_ptr_dtor(&zbody);
+       }
+       return ret;
+}
+
+PHP_HTTP_API STATUS php_http_env_response_send(php_http_env_response_t *r)
+{
+       zval *zbody;
+
+       /* check for ranges */
+       if (    (zbody = get_option(r->options, ZEND_STRL("body") TSRMLS_CC))
+       &&              (Z_TYPE_P(zbody) == IS_OBJECT)
+       &&              instanceof_function(Z_OBJCE_P(zbody), php_http_message_body_class_entry TSRMLS_CC)
+       ) {
+               php_http_message_body_object_t *obj = zend_object_store_get_object(zbody TSRMLS_CC);
+
+               r->content.length = php_http_message_body_size(obj->body);
+               zval_ptr_dtor(&zbody);
+
+               if (SUCCESS != php_http_env_set_response_header(0, ZEND_STRL("Accept-Ranges: bytes"), 1 TSRMLS_CC)) {
+                       return FAILURE;
+               } else {
+                       zend_hash_init(&r->range.values, 0, NULL, ZVAL_PTR_DTOR, 0);
+                       r->range.status = php_http_env_get_request_ranges(&r->range.values, r->content.length TSRMLS_CC);
+
+                       switch (r->range.status) {
+                               case PHP_HTTP_RANGE_NO:
+                                       zend_hash_destroy(&r->range.values);
+                                       break;
+
+                               case PHP_HTTP_RANGE_ERR:
+                                       if (php_http_env_got_request_header(ZEND_STRL("If-Range") TSRMLS_CC)) {
+                                               r->range.status = PHP_HTTP_RANGE_NO;
+                                               zend_hash_destroy(&r->range.values);
+                                       } else {
+                                               r->done = 1;
+                                               zend_hash_destroy(&r->range.values);
+                                               if (SUCCESS != php_http_env_set_response_header_format(416, 1 TSRMLS_CC, "Content-Range: bytes */%zu", r->content.length)) {
+                                                       return FAILURE;
+                                               }
+                                       }
+                                       break;
+
+                               case PHP_HTTP_RANGE_OK:
+                                       if (PHP_HTTP_CACHE_MISS == php_http_env_is_response_cached_by_etag(r->options, ZEND_STRL("If-Range") TSRMLS_CC)
+                                       ||      PHP_HTTP_CACHE_MISS == php_http_env_is_response_cached_by_last_modified(r->options, ZEND_STRL("If-Range") TSRMLS_CC)
+                                       ) {
+                                               r->range.status = PHP_HTTP_RANGE_NO;
+                                               zend_hash_destroy(&r->range.values);
+                                               break;
+                                       }
+                                       if (PHP_HTTP_CACHE_MISS == php_http_env_is_response_cached_by_etag(r->options, ZEND_STRL("If-Match") TSRMLS_CC)
+                                       ||      PHP_HTTP_CACHE_MISS == php_http_env_is_response_cached_by_last_modified(r->options, ZEND_STRL("If-Unmodified-Since") TSRMLS_CC)
+                                       ||      PHP_HTTP_CACHE_MISS == php_http_env_is_response_cached_by_last_modified(r->options, ZEND_STRL("Unless-Modified-Since") TSRMLS_CC)
+                                       ) {
+                                               r->done = 1;
+                                               zend_hash_destroy(&r->range.values);
+                                               if (SUCCESS != php_http_env_set_response_code(412 TSRMLS_CC)) {
+                                                       return FAILURE;
+                                               }
+                                               break;
+                                       }
+                       }
+               }
+       } else if (zbody) {
+               zval_ptr_dtor(&zbody);
+       }
+
+       if (SUCCESS != php_http_env_response_send_head(r)) {
+               return FAILURE;
+       }
+
+       if (SUCCESS != php_http_env_response_send_body(r)) {
+               return FAILURE;
+       }
+       return SUCCESS;
+}
+
+zend_class_entry *php_http_env_response_class_entry;
+
+#undef PHP_HTTP_BEGIN_ARGS
+#undef PHP_HTTP_EMPTY_ARGS
+#define PHP_HTTP_BEGIN_ARGS(method, req_args)                  PHP_HTTP_BEGIN_ARGS_EX(HttpEnvResponse, method, 0, req_args)
+#define PHP_HTTP_EMPTY_ARGS(method)                                            PHP_HTTP_EMPTY_ARGS_EX(HttpEnvResponse, method, 0)
+#define PHP_HTTP_ENV_RESPONSE_ME(method, visibility)   PHP_ME(HttpEnvResponse, method, PHP_HTTP_ARGS(HttpEnvResponse, method), visibility)
+
+PHP_HTTP_EMPTY_ARGS(__construct);
+
+PHP_HTTP_BEGIN_ARGS(setContentType, 1)
+       PHP_HTTP_ARG_VAL(content_type, 0)
+PHP_HTTP_END_ARGS;
+
+PHP_HTTP_BEGIN_ARGS(setContentEncoding, 1)
+       PHP_HTTP_ARG_VAL(content_encoding, 0)
+PHP_HTTP_END_ARGS;
+
+PHP_HTTP_BEGIN_ARGS(setContentDisposition, 1)
+       PHP_HTTP_ARG_VAL(content_disposition, 0)
+       PHP_HTTP_ARG_VAL(filename, 0)
+PHP_HTTP_END_ARGS;
+
+PHP_HTTP_BEGIN_ARGS(setCacheControl, 1)
+       PHP_HTTP_ARG_VAL(cache_control, 0)
+PHP_HTTP_END_ARGS;
+
+PHP_HTTP_BEGIN_ARGS(setLastModified, 1)
+       PHP_HTTP_ARG_VAL(last_modified, 0)
+PHP_HTTP_END_ARGS;
+
+PHP_HTTP_BEGIN_ARGS(isCachedByLastModified, 0)
+       PHP_HTTP_ARG_VAL(header_name, 0)
+PHP_HTTP_END_ARGS;
+
+PHP_HTTP_BEGIN_ARGS(setEtag, 1)
+       PHP_HTTP_ARG_VAL(etag, 0)
+PHP_HTTP_END_ARGS;
+
+PHP_HTTP_BEGIN_ARGS(isCachedByEtag, 0)
+       PHP_HTTP_ARG_VAL(header_name, 0)
+PHP_HTTP_END_ARGS;
+
+PHP_HTTP_BEGIN_ARGS(setThrottleRate, 1)
+       PHP_HTTP_ARG_VAL(chunk_size, 0)
+       PHP_HTTP_ARG_VAL(delay, 0)
+PHP_HTTP_END_ARGS;
+
+PHP_HTTP_EMPTY_ARGS(send);
+
+
+zend_function_entry php_http_env_response_method_entry[] = {
+       PHP_HTTP_ENV_RESPONSE_ME(__construct, ZEND_ACC_PUBLIC|ZEND_ACC_CTOR)
+       PHP_HTTP_ENV_RESPONSE_ME(setContentType, ZEND_ACC_PUBLIC)
+       PHP_HTTP_ENV_RESPONSE_ME(setContentDisposition, ZEND_ACC_PUBLIC)
+       PHP_HTTP_ENV_RESPONSE_ME(setContentEncoding, ZEND_ACC_PUBLIC)
+       PHP_HTTP_ENV_RESPONSE_ME(setCacheControl, ZEND_ACC_PUBLIC)
+       PHP_HTTP_ENV_RESPONSE_ME(setLastModified, ZEND_ACC_PUBLIC)
+       PHP_HTTP_ENV_RESPONSE_ME(isCachedByLastModified, ZEND_ACC_PUBLIC)
+       PHP_HTTP_ENV_RESPONSE_ME(setEtag, ZEND_ACC_PUBLIC)
+       PHP_HTTP_ENV_RESPONSE_ME(isCachedByEtag, ZEND_ACC_PUBLIC)
+       PHP_HTTP_ENV_RESPONSE_ME(setThrottleRate, ZEND_ACC_PUBLIC)
+
+       PHP_HTTP_ENV_RESPONSE_ME(send, ZEND_ACC_PUBLIC)
+
+       EMPTY_FUNCTION_ENTRY
+};
+
+
+PHP_METHOD(HttpEnvResponse, __construct)
+{
+       with_error_handling(EH_THROW, php_http_exception_class_entry) {
+               if (SUCCESS == zend_parse_parameters_none()) {
+                       php_http_message_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
+
+                       with_error_handling(EH_THROW, php_http_exception_class_entry) {
+                               obj->message = php_http_message_init_env(obj->message, PHP_HTTP_RESPONSE TSRMLS_CC);
+                       } end_error_handling();
+               }
+       } end_error_handling();
+
+}
+
+PHP_METHOD(HttpEnvResponse, setContentType)
+{
+       char *ct_str = NULL;
+       int ct_len = 0;
+
+       if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s!", &ct_str, &ct_len)) {
+               set_option(getThis(), ZEND_STRL("contentType"), IS_STRING, ct_str, ct_len TSRMLS_CC);
+       }
+}
+
+PHP_METHOD(HttpEnvResponse, setContentDisposition)
+{
+       long cd;
+       char *file_str = NULL;
+       int file_len = 0;
+
+       if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l|s!", &cd, &file_str, &file_len)) {
+               zval *arr;
+
+               MAKE_STD_ZVAL(arr);
+               array_init(arr);
+               add_assoc_long_ex(arr, ZEND_STRS("disposition"), cd);
+               if (file_len) {
+                       add_assoc_stringl_ex(arr, ZEND_STRS("filename"), file_str, file_len, 1);
+               }
+               zend_update_property(Z_OBJCE_P(getThis()), getThis(), ZEND_STRL("contentDisposition"), arr);
+               zval_ptr_dtor(&arr);
+       }
+}
+
+PHP_METHOD(HttpEnvResponse, setContentEncoding)
+{
+       long ce;
+
+       if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &ce)) {
+               set_option(getThis(), ZEND_STRL("contentEncoding"), IS_LONG, &ce, 0 TSRMLS_CC);
+       }
+}
+
+PHP_METHOD(HttpEnvResponse, setCacheControl)
+{
+       char *cc_str = NULL;
+       int cc_len = 0;
+
+       if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s!", &cc_str, &cc_len)) {
+               set_option(getThis(), ZEND_STRL("cacheControl"), IS_STRING, cc_str, cc_len TSRMLS_CC);
+       }
+}
+
+PHP_METHOD(HttpEnvResponse, setLastModified)
+{
+       long last_modified;
+
+       if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &last_modified)) {
+               set_option(getThis(), ZEND_STRL("lastModified"), IS_LONG, &last_modified, 0 TSRMLS_CC);
+       }
+}
+
+PHP_METHOD(HttpEnvResponse, isCachedByLastModified)
+{
+       char *header_name_str = NULL;
+       int header_name_len = 0;
+
+       if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s!", &header_name_str, &header_name_len)) {
+               if (!header_name_str || !header_name_len) {
+                       header_name_str = "If-Modified-Since";
+                       header_name_len = lenof("If-Modified-Since");
+               }
+               RETURN_LONG(php_http_env_is_response_cached_by_last_modified(getThis(), header_name_str, header_name_len TSRMLS_CC));
+       }
+       RETURN_FALSE;
+}
+
+PHP_METHOD(HttpEnvResponse, setEtag)
+{
+       char *etag_str = NULL;
+       int etag_len = 0;
+
+       if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s!", &etag_str, &etag_len)) {
+               set_option(getThis(), ZEND_STRL("etag"), IS_STRING, etag_str, etag_len TSRMLS_CC);
+       }
+}
+
+PHP_METHOD(HttpEnvResponse, isCachedByEtag)
+{
+       char *header_name_str = NULL;
+       int header_name_len = 0;
+
+       if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s!", &header_name_str, &header_name_len)) {
+               if (!header_name_str || !header_name_len) {
+                       header_name_str = "If-None-Match";
+                       header_name_len = lenof("If-None-Match");
+               }
+               RETURN_LONG(php_http_env_is_response_cached_by_etag(getThis(), header_name_str, header_name_len TSRMLS_CC));
+       }
+       RETURN_FALSE;
+}
+
+PHP_METHOD(HttpEnvResponse, setThrottleRate)
+{
+       long chunk_size;
+       double delay = 1;
+
+       if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l|d", &chunk_size, &delay)) {
+               long chunk_size_long = (long) chunk_size;
+
+               set_option(getThis(), ZEND_STRL("throttleDelay"), IS_DOUBLE, &delay, 0 TSRMLS_CC);
+               set_option(getThis(), ZEND_STRL("throttleChunk"), IS_LONG, &chunk_size_long, 0 TSRMLS_CC);
+               RETURN_TRUE;
+       }
+       RETURN_FALSE;
+}
+
+PHP_METHOD(HttpEnvResponse, send)
+{
+       RETVAL_FALSE;
+
+       if (SUCCESS == zend_parse_parameters_none()) {
+               php_http_env_response_t *r = php_http_env_response_init(NULL, getThis() TSRMLS_CC);
+
+               if (r) {
+                       RETVAL_SUCCESS(php_http_env_response_send(r));
+               }
+
+               php_http_env_response_free(&r);
+       }
+}
+
+PHP_MINIT_FUNCTION(http_env_response)
+{
+       PHP_HTTP_REGISTER_CLASS(http\\env, Response, http_env_response, php_http_message_class_entry, 0);
+
+       zend_declare_class_constant_long(php_http_env_response_class_entry, ZEND_STRL("CONTENT_DISPOSITION_NONE"), PHP_HTTP_CONTENT_DISPOSITION_NONE TSRMLS_CC);
+       zend_declare_class_constant_long(php_http_env_response_class_entry, ZEND_STRL("CONTENT_DISPOSITION_INLINE"), PHP_HTTP_CONTENT_DISPOSITION_INLINE TSRMLS_CC);
+       zend_declare_class_constant_long(php_http_env_response_class_entry, ZEND_STRL("CONTENT_DISPOSITION_ATTACHMENT"), PHP_HTTP_CONTENT_DISPOSITION_ATTACHMENT TSRMLS_CC);
+
+       zend_declare_class_constant_long(php_http_env_response_class_entry, ZEND_STRL("CONTENT_ENCODING_NONE"), PHP_HTTP_CONTENT_ENCODING_NONE TSRMLS_CC);
+       zend_declare_class_constant_long(php_http_env_response_class_entry, ZEND_STRL("CONTENT_ENCODING_GZIP"), PHP_HTTP_CONTENT_ENCODING_GZIP TSRMLS_CC);
+
+       zend_declare_class_constant_long(php_http_env_response_class_entry, ZEND_STRL("CACHE_NO"), PHP_HTTP_CACHE_NO TSRMLS_CC);
+       zend_declare_class_constant_long(php_http_env_response_class_entry, ZEND_STRL("CACHE_HIT"), PHP_HTTP_CACHE_HIT TSRMLS_CC);
+       zend_declare_class_constant_long(php_http_env_response_class_entry, ZEND_STRL("CACHE_MISS"), PHP_HTTP_CACHE_MISS TSRMLS_CC);
+
+       zend_declare_property_null(php_http_env_response_class_entry, ZEND_STRL("contentType"), ZEND_ACC_PROTECTED TSRMLS_CC);
+       zend_declare_property_null(php_http_env_response_class_entry, ZEND_STRL("contentDisposition"), ZEND_ACC_PROTECTED TSRMLS_CC);
+       zend_declare_property_null(php_http_env_response_class_entry, ZEND_STRL("contentEncoding"), ZEND_ACC_PROTECTED TSRMLS_CC);
+       zend_declare_property_null(php_http_env_response_class_entry, ZEND_STRL("cacheControl"), ZEND_ACC_PROTECTED TSRMLS_CC);
+       zend_declare_property_null(php_http_env_response_class_entry, ZEND_STRL("etag"), ZEND_ACC_PROTECTED TSRMLS_CC);
+       zend_declare_property_null(php_http_env_response_class_entry, ZEND_STRL("lastModified"), ZEND_ACC_PROTECTED TSRMLS_CC);
+       zend_declare_property_null(php_http_env_response_class_entry, ZEND_STRL("throttleDelay"), ZEND_ACC_PROTECTED TSRMLS_CC);
+       zend_declare_property_null(php_http_env_response_class_entry, ZEND_STRL("throttleChunk"), ZEND_ACC_PROTECTED TSRMLS_CC);
+
+       return SUCCESS;
+}
+
+
+/*
+ * Local variables:
+ * tab-width: 4
+ * c-basic-offset: 4
+ * End:
+ * vim600: noet sw=4 ts=4 fdm=marker
+ * vim<600: noet sw=4 ts=4
+ */
diff --git a/php_http_env_response.h b/php_http_env_response.h
new file mode 100644 (file)
index 0000000..67ae674
--- /dev/null
@@ -0,0 +1,60 @@
+#ifndef PHP_HTTP_ENV_RESPONSE_H
+#define PHP_HTTP_ENV_RESPONSE_H
+
+typedef struct php_http_env_response {
+       php_http_buffer_t *buffer;
+       zval *options;
+
+       struct {
+               size_t chunk;
+               double delay;
+       } throttle;
+
+       struct {
+               php_http_range_status_t status;
+               HashTable values;
+               char boundary[32];
+       } range;
+
+       struct {
+               size_t length;
+               char *type;
+               char *encoding;
+
+               php_http_encoding_stream_t *encoder;
+       } content;
+
+       zend_bool done;
+
+#ifdef ZTS
+       void ***ts;
+#endif
+} php_http_env_response_t;
+
+PHP_HTTP_API php_http_env_response_t *php_http_env_response_init(php_http_env_response_t *r, zval *options TSRMLS_DC);
+PHP_HTTP_API STATUS php_Http_env_response_send(php_http_env_response_t *r);
+PHP_HTTP_API void php_http_env_response_dtor(php_http_env_response_t *r);
+PHP_HTTP_API void php_http_env_response_free(php_http_env_response_t **r);
+
+PHP_HTTP_API php_http_cache_status_t php_http_env_is_response_cached_by_etag(zval *options, const char *header_str, size_t header_len TSRMLS_DC);
+PHP_HTTP_API php_http_cache_status_t php_http_env_is_response_cached_by_last_modified(zval *options, const char *header_str, size_t header_len TSRMLS_DC);
+
+extern zend_class_entry *php_http_env_response_class_entry;
+extern zend_function_entry php_http_env_response_method_entry[];
+
+PHP_METHOD(HttpEnvResponse, __construct);
+PHP_METHOD(HttpEnvResponse, setContentType);
+PHP_METHOD(HttpEnvResponse, setContentDisposition);
+PHP_METHOD(HttpEnvResponse, setContentEncoding);
+PHP_METHOD(HttpEnvResponse, setCacheControl);
+PHP_METHOD(HttpEnvResponse, setLastModified);
+PHP_METHOD(HttpEnvResponse, isCachedByLastModified);
+PHP_METHOD(HttpEnvResponse, setEtag);
+PHP_METHOD(HttpEnvResponse, isCachedByEtag);
+PHP_METHOD(HttpEnvResponse, setThrottleRate);
+PHP_METHOD(HttpEnvResponse, send);
+
+
+PHP_MINIT_FUNCTION(http_env_response);
+
+#endif
index f52c9d6348b3b7839cf90105e1805baaba01adcf..8caa319cf7310aa8d7e5dd32652572ee2e5fc126 100644 (file)
@@ -112,7 +112,7 @@ char *php_http_pretty_key(char *key, size_t key_len, zend_bool uctitle, zend_boo
 
 size_t php_http_boundary(char *buf, size_t buf_len TSRMLS_DC)
 {
-       return snprintf(buf, buf_len, "%lu%0.9f", (ulong) PHP_HTTP_G->env.request.time, (float) php_combined_lcg(TSRMLS_C));
+       return snprintf(buf, buf_len, "%15.15F", PHP_HTTP_G->env.request.time * php_combined_lcg(TSRMLS_C));
 }
 
 /* ARRAYS */