X-Git-Url: https://git.m6w6.name/?p=m6w6%2Fext-http;a=blobdiff_plain;f=php_http_env_response.c;h=2f2f161a676655bab5578d412dea6bc4206aaffb;hp=2c7547e0e40ae2419a4dc723bd0afdb8bbdc17b4;hb=b1c7be1f12467741ddf92d04361c45006dc07db7;hpb=39c577a412719fa721ae26dd2d87a63fb8ab849a diff --git a/php_http_env_response.c b/php_http_env_response.c index 2c7547e..2f2f161 100644 --- a/php_http_env_response.c +++ b/php_http_env_response.c @@ -6,33 +6,34 @@ | modification, are permitted provided that the conditions mentioned | | in the accompanying LICENSE file are met. | +--------------------------------------------------------------------+ - | Copyright (c) 2004-2011, Michael Wallner | + | Copyright (c) 2004-2014, Michael Wallner | +--------------------------------------------------------------------+ */ #include "php_http_api.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) +static void set_option(zval *options, const char *name_str, size_t name_len, int type, void *value_ptr, size_t value_len TSRMLS_DC) { 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); + zend_update_property_double(Z_OBJCE_P(options), options, name_str, 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); + zend_update_property_long(Z_OBJCE_P(options), options, name_str, 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); + zend_update_property_stringl(Z_OBJCE_P(options), options, name_str, name_len, value_ptr, value_len TSRMLS_CC); + break; + case IS_ARRAY: + case IS_OBJECT: + zend_update_property(Z_OBJCE_P(options), options, name_str, name_len, value_ptr TSRMLS_CC); break; } } else { - zend_update_property_null(Z_OBJCE_P(options), options, name, name_len TSRMLS_CC); + zend_update_property_null(Z_OBJCE_P(options), options, name_str, name_len TSRMLS_CC); } - efree(name); } else { convert_to_array(options); if (value_ptr) { @@ -47,6 +48,11 @@ static void set_option(zval *options, const char *name_str, size_t name_len, int char *value = estrndup(value_ptr, value_len); add_assoc_stringl_ex(options, name_str, name_len + 1, value, value_len, 0); break; + case IS_ARRAY: + case IS_OBJECT: + Z_ADDREF_P(value_ptr); + add_assoc_zval_ex(options, name_str, name_len + 1, value_ptr); + break; } } } else { @@ -59,9 +65,7 @@ static zval *get_option(zval *options, const char *name_str, size_t name_len TSR 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); + val = zend_read_property(Z_OBJCE_P(options), options, name_str, name_len, 0 TSRMLS_CC); } else { if (SUCCESS == zend_symtable_find(Z_ARRVAL_P(options), name_str, name_len + 1, (void *) &valptr)) { val = *valptr; @@ -74,21 +78,79 @@ static zval *get_option(zval *options, const char *name_str, size_t name_len TSR } return val; } +static php_http_message_body_t *get_body(zval *options TSRMLS_DC) +{ + zval *zbody; + php_http_message_body_t *body = NULL; + + if ((zbody = get_option(options, ZEND_STRL("body") TSRMLS_CC))) { + if ((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 *body_obj = zend_object_store_get_object(zbody TSRMLS_CC); -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) + body = body_obj->body; + } + zval_ptr_dtor(&zbody); + } + + return body; +} +static php_http_message_t *get_request(zval *options TSRMLS_DC) +{ + zval *zrequest; + php_http_message_t *request = NULL; + + if ((zrequest = get_option(options, ZEND_STRL("request") TSRMLS_CC))) { + if (Z_TYPE_P(zrequest) == IS_OBJECT && instanceof_function(Z_OBJCE_P(zrequest), php_http_message_class_entry TSRMLS_CC)) { + php_http_message_object_t *request_obj = zend_object_store_get_object(zrequest TSRMLS_CC); + + request = request_obj->message; + } + zval_ptr_dtor(&zrequest); + } + + return request; +} +static void set_cookie(zval *options, zval *zcookie_new TSRMLS_DC) +{ + HashPosition pos; + zval *zcookies_set; + php_http_array_hashkey_t key = php_http_array_hashkey_init(0); + php_http_cookie_object_t *obj = zend_object_store_get_object(zcookie_new TSRMLS_CC); + + zcookies_set = get_option(options, ZEND_STRL("cookies") TSRMLS_CC); + if (!zcookies_set || Z_TYPE_P(zcookies_set) != IS_ARRAY) { + if (zcookies_set) { + zval_ptr_dtor(&zcookies_set); + } + MAKE_STD_ZVAL(zcookies_set); + array_init_size(zcookies_set, zend_hash_num_elements(&obj->list->cookies)); + } else { + SEPARATE_ZVAL(&zcookies_set); + } + + FOREACH_HASH_KEY(pos, &obj->list->cookies, key) { + Z_ADDREF_P(zcookie_new); + if (key.type == HASH_KEY_IS_STRING) { + add_assoc_zval_ex(zcookies_set, key.str, key.len, zcookie_new); + } else { + add_index_zval(zcookies_set, key.num, zcookie_new); + } + } + + set_option(options, ZEND_STRL("cookies"), IS_ARRAY, zcookies_set, 0 TSRMLS_CC); + zval_ptr_dtor(&zcookies_set); +} + +php_http_cache_status_t php_http_env_is_response_cached_by_etag(zval *options, const char *header_str, size_t header_len, php_http_message_t *request TSRMLS_DC) { php_http_cache_status_t ret = PHP_HTTP_CACHE_NO; int free_etag = 0; char *header = NULL, *etag; - zval *zetag, *zbody = NULL; + php_http_message_body_t *body; + zval *zetag; - if ( !(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_get_class_entry() TSRMLS_CC) - ) { - if (zbody) { - zval_ptr_dtor(&zbody); - } + + if (!(body = get_body(options TSRMLS_CC))) { return ret; } @@ -100,45 +162,37 @@ PHP_HTTP_API php_http_cache_status_t php_http_env_is_response_cached_by_etag(zva if (zetag && Z_STRLEN_P(zetag)) { etag = Z_STRVAL_P(zetag); - } else if ((etag = php_http_message_body_etag(((php_http_message_body_object_t *) zend_object_store_get_object(zbody TSRMLS_CC))->body))) { + } else if ((etag = php_http_message_body_etag(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); } - if (etag && (header = php_http_env_get_request_header(header_str, header_len, NULL TSRMLS_CC))) { + if (etag && (header = php_http_env_get_request_header(header_str, header_len, NULL, request TSRMLS_CC))) { ret = php_http_match(header, etag, PHP_HTTP_MATCH_WORD) ? PHP_HTTP_CACHE_HIT : PHP_HTTP_CACHE_MISS; } if (free_etag) { efree(etag); } - STR_FREE(header); + PTR_FREE(header); return ret; } -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) +php_http_cache_status_t php_http_env_is_response_cached_by_last_modified(zval *options, const char *header_str, size_t header_len, php_http_message_t *request TSRMLS_DC) { + php_http_cache_status_t ret = PHP_HTTP_CACHE_NO; char *header; time_t ums, lm = 0; - zval *zbody = NULL, *zlm; + php_http_message_body_t *body; + zval *zlm; - if ( !(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_get_class_entry() TSRMLS_CC) - ) { - if (zbody) { - zval_ptr_dtor(&zbody); - } - return PHP_HTTP_CACHE_NO; + if (!(body = get_body(options TSRMLS_CC))) { + return ret; } if ((zlm = get_option(options, ZEND_STRL("lastModified") TSRMLS_CC))) { @@ -150,49 +204,59 @@ PHP_HTTP_API php_http_cache_status_t php_http_env_is_response_cached_by_last_mod 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); + lm = php_http_message_body_mtime(body); set_option(options, ZEND_STRL("lastModified"), IS_LONG, &lm, 0 TSRMLS_CC); } - zval_ptr_dtor(&zbody); if (zlm) { zval_ptr_dtor(&zlm); } - if (!(header = php_http_env_get_request_header(header_str, header_len, NULL TSRMLS_CC))) { - return PHP_HTTP_CACHE_NO; + if ((header = php_http_env_get_request_header(header_str, header_len, NULL, request TSRMLS_CC))) { + ums = php_parse_date(header, NULL); + + if (ums > 0 && ums >= lm) { + ret = PHP_HTTP_CACHE_HIT; + } else { + ret = PHP_HTTP_CACHE_MISS; + } + } + + PTR_FREE(header); + return ret; +} + +static zend_bool php_http_env_response_is_cacheable(php_http_env_response_t *r, php_http_message_t *request) +{ + TSRMLS_FETCH_FROM_CTX(r->ts); + + if (r->ops->get_status(r) >= 400) { + return 0; } - ums = php_parse_date(header, NULL); - efree(header); + if (php_http_env_got_request_header(ZEND_STRL("Authorization"), request TSRMLS_CC)) { + return 0; + } - if (ums > 0 && ums >= lm) { - return PHP_HTTP_CACHE_HIT; - } else { - return PHP_HTTP_CACHE_MISS; + if (-1 == php_http_select_str(php_http_env_get_request_method(request TSRMLS_CC), 2, "HEAD", "GET")) { + return 0; } + + return 1; } static size_t output(void *context, char *buf, size_t len TSRMLS_DC) { php_http_env_response_t *r = context; - PHPWRITE(buf, len); + if (SUCCESS != r->ops->write(r, buf, len)) { + return (size_t) -1; + } /* 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_VERSION_ID >= 50400 - 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); - } -#else - php_end_ob_buffer(1, 1 TSRMLS_CC); - sapi_flush(TSRMLS_C); -#endif + r->ops->flush(r); php_http_sleep(r->throttle.delay); } return len; @@ -201,7 +265,7 @@ static size_t output(void *context, char *buf, size_t len TSRMLS_DC) #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) { - size_t chunk = r->throttle.chunk ? r->throttle.chunk : PHP_HTTP_SENDBUF_SIZE; + size_t chunks_sent, chunk = r->throttle.chunk ? r->throttle.chunk : PHP_HTTP_SENDBUF_SIZE; TSRMLS_FETCH_FROM_CTX(r->ts); if (r->content.encoder) { @@ -218,26 +282,33 @@ static STATUS php_http_env_response_send_data(php_http_env_response_t *r, const } } - 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); + if (!enc_str) { + return SUCCESS; } + chunks_sent = php_http_buffer_chunked_output(&r->buffer, enc_str, enc_len, buf ? chunk : 0, output, r TSRMLS_CC); + PTR_FREE(enc_str); } else { - php_http_buffer_chunked_output(&r->buffer, buf, len, buf ? chunk : 0, output, r TSRMLS_CC); + chunks_sent = php_http_buffer_chunked_output(&r->buffer, buf, len, buf ? chunk : 0, output, r TSRMLS_CC); } - return SUCCESS; + return chunks_sent != (size_t) -1 ? SUCCESS : FAILURE; } -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 *php_http_env_response_init(php_http_env_response_t *r, zval *options, php_http_env_response_ops_t *ops, void *init_arg TSRMLS_DC) { - php_http_env_response_t *free_r = NULL; + zend_bool free_r; - if (!r) { - r = free_r = emalloc(sizeof(*r)); + if ((free_r = !r)) { + r = emalloc(sizeof(*r)); } memset(r, 0, sizeof(*r)); + if (ops) { + r->ops = ops; + } else { + r->ops = php_http_env_response_get_sapi_ops(); + } + r->buffer = php_http_buffer_init(NULL); Z_ADDREF_P(options); @@ -245,21 +316,33 @@ PHP_HTTP_API php_http_env_response_t *php_http_env_response_init(php_http_env_re TSRMLS_SET_CTX(r->ts); + if (r->ops->init && (SUCCESS != r->ops->init(r, init_arg))) { + if (free_r) { + php_http_env_response_free(&r); + } else { + php_http_env_response_dtor(r); + r = NULL; + } + } + return r; } -PHP_HTTP_API void php_http_env_response_dtor(php_http_env_response_t *r) +void php_http_env_response_dtor(php_http_env_response_t *r) { + if (r->ops->dtor) { + r->ops->dtor(r); + } php_http_buffer_free(&r->buffer); zval_ptr_dtor(&r->options); - STR_FREE(r->content.type); - STR_FREE(r->content.encoding); + PTR_FREE(r->content.type); + PTR_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) +void php_http_env_response_free(php_http_env_response_t **r) { if (*r) { php_http_env_response_dtor(*r); @@ -268,7 +351,7 @@ PHP_HTTP_API void php_http_env_response_free(php_http_env_response_t **r) } } -static STATUS php_http_env_response_send_head(php_http_env_response_t *r) +static STATUS php_http_env_response_send_head(php_http_env_response_t *r, php_http_message_t *request) { STATUS ret = SUCCESS; zval *zoption, *options = r->options; @@ -278,12 +361,23 @@ static STATUS php_http_env_response_send_head(php_http_env_response_t *r) return ret; } + if ((zoption = get_option(options, ZEND_STRL("headers") TSRMLS_CC))) { + if (Z_TYPE_P(zoption) == IS_ARRAY) { + php_http_header_to_callback(Z_ARRVAL_P(zoption), 0, (php_http_pass_format_callback_t) r->ops->set_header, r TSRMLS_CC); + } + zval_ptr_dtor(&zoption); + } + + if (ret != SUCCESS) { + 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); + ret = r->ops->set_status(r, Z_LVAL_P(zoption_copy)); } zval_ptr_dtor(&zoption_copy); } @@ -298,7 +392,7 @@ static STATUS php_http_env_response_send_head(php_http_env_response_t *r) 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); + ret = r->ops->set_protocol_version(r, &v); php_http_version_dtor(&v); } zval_ptr_dtor(&zoption_copy); @@ -308,17 +402,23 @@ static STATUS php_http_env_response_send_head(php_http_env_response_t *r) return ret; } - if ((zoption = get_option(options, ZEND_STRL("headers") TSRMLS_CC))) { + if ((zoption = get_option(options, ZEND_STRL("cookies") 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); + zval **zcookie; - 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))) { + FOREACH_VAL(pos, zoption, zcookie) { + if (Z_TYPE_PP(zcookie) == IS_OBJECT && instanceof_function(Z_OBJCE_PP(zcookie), php_http_cookie_class_entry TSRMLS_CC)) { + php_http_cookie_object_t *obj = zend_object_store_get_object(*zcookie TSRMLS_CC); + char *str; + size_t len; + + php_http_cookie_list_to_string(obj->list, &str, &len); + if (SUCCESS != (ret = r->ops->add_header(r, "Set-Cookie: %s", str))) { + efree(str); break; } + efree(str); } } } @@ -333,11 +433,9 @@ static STATUS php_http_env_response_send_head(php_http_env_response_t *r) 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)); - } + if (Z_STRLEN_P(zoption_copy) && strchr(Z_STRVAL_P(zoption_copy), '/')) { + if (SUCCESS == (ret = r->ops->set_header(r, "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); @@ -354,16 +452,19 @@ static STATUS php_http_env_response_send_head(php_http_env_response_t *r) if ( 1 == php_http_array_list(&r->range.values TSRMLS_CC, 1, &range) && 2 == php_http_array_list(Z_ARRVAL_PP(range) TSRMLS_CC, 2, &begin, &end) ) { - 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); + if (SUCCESS == (ret = r->ops->set_status(r, 206))) { + ret = r->ops->set_header(r, "Content-Range: bytes %ld-%ld/%zu", Z_LVAL_PP(begin), Z_LVAL_PP(end), r->content.length); + } } else { /* this should never happen */ zend_hash_destroy(&r->range.values); - php_http_env_set_response_code(500 TSRMLS_CC); ret = FAILURE; } } else { php_http_boundary(r->range.boundary, sizeof(r->range.boundary) TSRMLS_CC); - ret = php_http_env_set_response_header_format(206, 1 TSRMLS_CC, "Content-Type: multipart/byteranges; boundary=%s", r->range.boundary); + if (SUCCESS == (ret = r->ops->set_status(r, 206))) { + ret = r->ops->set_header(r, "Content-Type: multipart/byteranges; boundary=%s", r->range.boundary); + } } } else { if ((zoption = get_option(options, ZEND_STRL("cacheControl") TSRMLS_CC))) { @@ -371,7 +472,7 @@ static STATUS php_http_env_response_send_head(php_http_env_response_t *r) 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); + ret = r->ops->set_header(r, "Cache-Control: %.*s", Z_STRLEN_P(zoption_copy), Z_STRVAL_P(zoption_copy)); } zval_ptr_dtor(&zoption_copy); } @@ -386,7 +487,9 @@ static STATUS php_http_env_response_send_head(php_http_env_response_t *r) php_http_buffer_init(&buf); if (php_http_params_to_string(&buf, Z_ARRVAL_P(zoption_copy), ZEND_STRL(","), ZEND_STRL(";"), ZEND_STRL("="), PHP_HTTP_PARAMS_DEFAULT TSRMLS_CC)) { - ret = php_http_env_set_response_header_format(0, 1 TSRMLS_CC, "Content-Disposition: %s", PHP_HTTP_BUFFER_VAL(&buf)); + if (buf.used) { + ret = r->ops->set_header(r, "Content-Disposition: %.*s", buf.used, buf.data); + } } php_http_buffer_dtor(&buf); @@ -412,7 +515,7 @@ static STATUS php_http_env_response_send_head(php_http_env_response_t *r) 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))) { + if ((result = php_http_negotiate_encoding(Z_ARRVAL(zsupported), request TSRMLS_CC))) { char *key_str = NULL; uint key_len = 0; @@ -421,21 +524,21 @@ static STATUS php_http_env_response_send_head(php_http_env_response_t *r) 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 TSRMLS_CC))) { ret = FAILURE; - } else if (SUCCESS == (ret = php_http_env_set_response_header(0, ZEND_STRL("Content-Encoding: gzip"), 1 TSRMLS_CC))) { + } else if (SUCCESS == (ret = r->ops->set_header(r, "Content-Encoding: gzip"))) { 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 TSRMLS_CC))) { ret = FAILURE; - } else if (SUCCESS == (ret = php_http_env_set_response_header(0, ZEND_STRL("Content-Encoding: deflate"), 1 TSRMLS_CC))) { + } else if (SUCCESS == (ret = r->ops->set_header(r, "Content-Encoding: deflate"))) { 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); + ret = r->ops->del_header(r, ZEND_STRL("Content-Encoding")); } if (SUCCESS == ret) { - ret = php_http_env_set_response_header(0, ZEND_STRL("Vary: Accept-Encoding"), 0 TSRMLS_CC); + ret = r->ops->add_header(r, "Vary: Accept-Encoding"); } } @@ -448,7 +551,7 @@ static STATUS php_http_env_response_send_head(php_http_env_response_t *r) case PHP_HTTP_CONTENT_ENCODING_NONE: default: - ret = php_http_env_set_response_header_value(0, ZEND_STRL("Content-Encoding"), NULL, 0 TSRMLS_CC); + ret = r->ops->del_header(r, ZEND_STRL("Content-Encoding")); break; } zval_ptr_dtor(&zoption_copy); @@ -458,24 +561,19 @@ static STATUS php_http_env_response_send_head(php_http_env_response_t *r) return ret; } - if (php_http_env_get_response_code(TSRMLS_C) < 400 && !php_http_env_got_request_header(ZEND_STRL("Authorization") TSRMLS_CC) - && ( !SG(request_info).request_method - || !strcasecmp(SG(request_info).request_method, "GET") - || !strcasecmp(SG(request_info).request_method, "HEAD") - ) - ) { - switch (php_http_env_is_response_cached_by_etag(options, ZEND_STRL("If-None-Match") TSRMLS_CC)) { + if (php_http_env_response_is_cacheable(r, request)) { + switch (php_http_env_is_response_cached_by_etag(options, ZEND_STRL("If-None-Match"), request TSRMLS_CC)) { 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") TSRMLS_CC)) { + if (PHP_HTTP_CACHE_HIT != php_http_env_is_response_cached_by_last_modified(options, ZEND_STRL("If-Modified-Since"), request TSRMLS_CC)) { break; } /* no break */ case PHP_HTTP_CACHE_HIT: - ret = php_http_env_set_response_code(304 TSRMLS_CC); + ret = r->ops->set_status(r, 304); r->done = 1; break; } @@ -485,9 +583,9 @@ static STATUS php_http_env_response_send_head(php_http_env_response_t *r) zval_ptr_dtor(&zoption); if (*Z_STRVAL_P(zoption_copy) != '"' && strncmp(Z_STRVAL_P(zoption_copy), "W/\"", 3)) { - ret = php_http_env_set_response_header_format(0, 1 TSRMLS_CC, "ETag: \"%s\"", Z_STRVAL_P(zoption_copy)); + ret = r->ops->set_header(r, "ETag: \"%s\"", Z_STRVAL_P(zoption_copy)); } else { - ret = php_http_env_set_response_header_value(0, ZEND_STRL("ETag"), zoption_copy, 1 TSRMLS_CC); + ret = r->ops->set_header(r, "ETag: %s", Z_STRVAL_P(zoption_copy)); } zval_ptr_dtor(&zoption_copy); } @@ -498,7 +596,7 @@ static STATUS php_http_env_response_send_head(php_http_env_response_t *r) 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); + ret = r->ops->set_header(r, "Last-Modified: %s", date); efree(date); } } @@ -513,19 +611,15 @@ static STATUS php_http_env_response_send_head(php_http_env_response_t *r) static STATUS php_http_env_response_send_body(php_http_env_response_t *r) { STATUS ret = SUCCESS; - zval *zbody, *zoption; + zval *zoption; + php_http_message_body_t *body; TSRMLS_FETCH_FROM_CTX(r->ts); 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_get_class_entry() TSRMLS_CC) - ) { - php_http_message_body_object_t *obj = zend_object_store_get_object(zbody TSRMLS_CC); - + if ((body = get_body(r->options TSRMLS_CC))) { if ((zoption = get_option(r->options, ZEND_STRL("throttleDelay") TSRMLS_CC))) { if (Z_TYPE_P(zoption) == IS_DOUBLE) { r->throttle.delay = Z_DVAL_P(zoption); @@ -548,15 +642,15 @@ static STATUS php_http_env_response_send_body(php_http_env_response_t *r) && 2 == php_http_array_list(Z_ARRVAL_PP(range) TSRMLS_CC, 2, &begin, &end) ) { /* 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); + ret = php_http_message_body_to_callback(body, (php_http_pass_callback_t) php_http_env_response_send_data, r, Z_LVAL_PP(begin), Z_LVAL_PP(end) - Z_LVAL_PP(begin) + 1); + if (ret == SUCCESS) { + ret = php_http_env_response_send_done(r); + } zend_hash_destroy(&r->range.values); - ret = SUCCESS; } else { - fprintf(stderr, "wut?"); /* this should never happen */ zend_hash_destroy(&r->range.values); - php_http_env_set_response_code(500 TSRMLS_CC); + r->ops->set_status(r, 500); ret = FAILURE; } @@ -581,45 +675,44 @@ static STATUS php_http_env_response_send_body(php_http_env_response_t *r) 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); + ret = php_http_message_body_to_callback(body, (php_http_pass_callback_t) php_http_env_response_send_data, r, Z_LVAL_PP(begin), Z_LVAL_PP(end) - Z_LVAL_PP(begin) + 1); } } - php_http_buffer_appendf(r->buffer, PHP_HTTP_CRLF "--%s--", r->range.boundary); - php_http_env_response_send_done(r); + + if (ret == SUCCESS) { + php_http_buffer_appendf(r->buffer, PHP_HTTP_CRLF "--%s--", r->range.boundary); + ret = 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); + ret = php_http_message_body_to_callback(body, (php_http_pass_callback_t) php_http_env_response_send_data, r, 0, 0); + if (ret == SUCCESS) { + ret = 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) +STATUS php_http_env_response_send(php_http_env_response_t *r) { - zval *zbody; + php_http_message_t *request; + php_http_message_body_t *body; TSRMLS_FETCH_FROM_CTX(r->ts); - /* 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_get_class_entry() TSRMLS_CC) - ) { - php_http_message_body_object_t *obj = zend_object_store_get_object(zbody TSRMLS_CC); + request = get_request(r->options TSRMLS_CC); - r->content.length = php_http_message_body_size(obj->body); - zval_ptr_dtor(&zbody); + /* check for ranges */ + if ((body = get_body(r->options TSRMLS_CC))) { + r->content.length = php_http_message_body_size(body); - if (SUCCESS != php_http_env_set_response_header(0, ZEND_STRL("Accept-Ranges: bytes"), 1 TSRMLS_CC)) { + if (SUCCESS != r->ops->set_header(r, "Accept-Ranges: bytes")) { 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); + r->range.status = php_http_env_get_request_ranges(&r->range.values, r->content.length, request TSRMLS_CC); switch (r->range.status) { case PHP_HTTP_RANGE_NO: @@ -627,33 +720,36 @@ PHP_HTTP_API STATUS php_http_env_response_send(php_http_env_response_t *r) break; case PHP_HTTP_RANGE_ERR: - if (php_http_env_got_request_header(ZEND_STRL("If-Range") TSRMLS_CC)) { + if (php_http_env_got_request_header(ZEND_STRL("If-Range"), request 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)) { + if (SUCCESS != r->ops->set_status(r, 416)) { + return FAILURE; + } + if (SUCCESS != r->ops->set_header(r, "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) + if (PHP_HTTP_CACHE_MISS == php_http_env_is_response_cached_by_etag(r->options, ZEND_STRL("If-Range"), request TSRMLS_CC) + || PHP_HTTP_CACHE_MISS == php_http_env_is_response_cached_by_last_modified(r->options, ZEND_STRL("If-Range"), request 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) + if (PHP_HTTP_CACHE_MISS == php_http_env_is_response_cached_by_etag(r->options, ZEND_STRL("If-Match"), request TSRMLS_CC) + || PHP_HTTP_CACHE_MISS == php_http_env_is_response_cached_by_last_modified(r->options, ZEND_STRL("If-Unmodified-Since"), request TSRMLS_CC) + || PHP_HTTP_CACHE_MISS == php_http_env_is_response_cached_by_last_modified(r->options, ZEND_STRL("Unless-Modified-Since"), request TSRMLS_CC) ) { r->done = 1; zend_hash_destroy(&r->range.values); - if (SUCCESS != php_http_env_set_response_code(412 TSRMLS_CC)) { + if (SUCCESS != r->ops->set_status(r, 412)) { return FAILURE; } break; @@ -662,113 +758,450 @@ PHP_HTTP_API STATUS php_http_env_response_send(php_http_env_response_t *r) break; } } - } else if (zbody) { - zval_ptr_dtor(&zbody); } - if (SUCCESS != php_http_env_response_send_head(r)) { + if (SUCCESS != php_http_env_response_send_head(r, request)) { + php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed to send response headers"); return FAILURE; } if (SUCCESS != php_http_env_response_send_body(r)) { + php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed to send response body"); + return FAILURE; + } + + if (SUCCESS != r->ops->finish(r)) { + php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed to finish response"); return FAILURE; } + return SUCCESS; } -#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) +static long php_http_env_response_sapi_get_status(php_http_env_response_t *r) +{ + TSRMLS_FETCH_FROM_CTX(r->ts); -PHP_HTTP_EMPTY_ARGS(__construct); + return php_http_env_get_response_code(TSRMLS_C); +} +static STATUS php_http_env_response_sapi_set_status(php_http_env_response_t *r, long http_code) +{ + TSRMLS_FETCH_FROM_CTX(r->ts); -PHP_HTTP_BEGIN_ARGS(__invoke, 1) - PHP_HTTP_ARG_VAL(ob_string, 0) - PHP_HTTP_ARG_VAL(ob_flags, 0) -PHP_HTTP_END_ARGS; + return php_http_env_set_response_code(http_code TSRMLS_CC); +} +static STATUS php_http_env_response_sapi_set_protocol_version(php_http_env_response_t *r, php_http_version_t *v) +{ + TSRMLS_FETCH_FROM_CTX(r->ts); -PHP_HTTP_BEGIN_ARGS(setContentType, 1) - PHP_HTTP_ARG_VAL(content_type, 0) -PHP_HTTP_END_ARGS; + return php_http_env_set_response_protocol_version(v TSRMLS_CC); +} +static STATUS php_http_env_response_sapi_set_header(php_http_env_response_t *r, const char *fmt, ...) +{ + STATUS ret; + va_list args; + TSRMLS_FETCH_FROM_CTX(r->ts); -PHP_HTTP_BEGIN_ARGS(setContentEncoding, 1) - PHP_HTTP_ARG_VAL(content_encoding, 0) -PHP_HTTP_END_ARGS; + va_start(args, fmt); + ret = php_http_env_set_response_header_va(0, 1, fmt, args TSRMLS_CC); + va_end(args); -PHP_HTTP_BEGIN_ARGS(setContentDisposition, 1) - PHP_HTTP_ARG_ARR(disposition_params, 1, 0) -PHP_HTTP_END_ARGS; + return ret; +} +static STATUS php_http_env_response_sapi_add_header(php_http_env_response_t *r, const char *fmt, ...) +{ + STATUS ret; + va_list args; + TSRMLS_FETCH_FROM_CTX(r->ts); -PHP_HTTP_BEGIN_ARGS(setCacheControl, 1) - PHP_HTTP_ARG_VAL(cache_control, 0) -PHP_HTTP_END_ARGS; + va_start(args, fmt); + ret = php_http_env_set_response_header_va(0, 0, fmt, args TSRMLS_CC); + va_end(args); -PHP_HTTP_BEGIN_ARGS(setLastModified, 1) - PHP_HTTP_ARG_VAL(last_modified, 0) -PHP_HTTP_END_ARGS; + return ret; +} +static STATUS php_http_env_response_sapi_del_header(php_http_env_response_t *r, const char *header_str, size_t header_len) +{ + TSRMLS_FETCH_FROM_CTX(r->ts); -PHP_HTTP_BEGIN_ARGS(isCachedByLastModified, 0) - PHP_HTTP_ARG_VAL(header_name, 0) -PHP_HTTP_END_ARGS; + return php_http_env_set_response_header_value(0, header_str, header_len, NULL, 1 TSRMLS_CC); +} +static STATUS php_http_env_response_sapi_write(php_http_env_response_t *r, const char *data_str, size_t data_len) +{ + TSRMLS_FETCH_FROM_CTX(r->ts); -PHP_HTTP_BEGIN_ARGS(setEtag, 1) - PHP_HTTP_ARG_VAL(etag, 0) -PHP_HTTP_END_ARGS; + if (0 < PHPWRITE(data_str, data_len)) { + return SUCCESS; + } + return FAILURE; +} +static STATUS php_http_env_response_sapi_flush(php_http_env_response_t *r) +{ + TSRMLS_FETCH_FROM_CTX(r->ts); + +#if PHP_VERSION_ID >= 50400 + 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); + } +#else + php_end_ob_buffer(1, 1 TSRMLS_CC); + sapi_flush(TSRMLS_C); +#endif -PHP_HTTP_BEGIN_ARGS(isCachedByEtag, 0) - PHP_HTTP_ARG_VAL(header_name, 0) -PHP_HTTP_END_ARGS; + return SUCCESS; +} +static STATUS php_http_env_response_sapi_finish(php_http_env_response_t *r) +{ + return SUCCESS; +} + +static php_http_env_response_ops_t php_http_env_response_sapi_ops = { + NULL, + NULL, + php_http_env_response_sapi_get_status, + php_http_env_response_sapi_set_status, + php_http_env_response_sapi_set_protocol_version, + php_http_env_response_sapi_set_header, + php_http_env_response_sapi_add_header, + php_http_env_response_sapi_del_header, + php_http_env_response_sapi_write, + php_http_env_response_sapi_flush, + php_http_env_response_sapi_finish +}; + +php_http_env_response_ops_t *php_http_env_response_get_sapi_ops(void) +{ + return &php_http_env_response_sapi_ops; +} -PHP_HTTP_BEGIN_ARGS(setThrottleRate, 1) - PHP_HTTP_ARG_VAL(chunk_size, 0) - PHP_HTTP_ARG_VAL(delay, 0) -PHP_HTTP_END_ARGS; +typedef struct php_http_env_response_stream_ctx { + HashTable header; + php_http_version_t version; + long status_code; -PHP_HTTP_EMPTY_ARGS(send); + php_stream *stream; + php_stream_filter *chunked_filter; + php_http_message_t *request; -static zend_class_entry *php_http_env_response_class_entry; + unsigned started:1; + unsigned finished:1; + unsigned chunked:1; +} php_http_env_response_stream_ctx_t; -zend_class_entry *php_http_env_response_get_class_entry(void) +static STATUS php_http_env_response_stream_init(php_http_env_response_t *r, void *init_arg) { - return php_http_env_response_class_entry; + php_http_env_response_stream_ctx_t *ctx; + size_t buffer_size = 0x1000; + TSRMLS_FETCH_FROM_CTX(r->ts); + + ctx = ecalloc(1, sizeof(*ctx)); + + ctx->stream = init_arg; + if (!ctx->stream || SUCCESS != zend_list_addref(ctx->stream->rsrc_id)) { + efree(ctx); + return FAILURE; + } + php_stream_set_option(ctx->stream, PHP_STREAM_OPTION_WRITE_BUFFER, PHP_STREAM_BUFFER_FULL, &buffer_size); + zend_hash_init(&ctx->header, 0, NULL, ZVAL_PTR_DTOR, 0); + php_http_version_init(&ctx->version, 1, 1 TSRMLS_CC); + ctx->status_code = 200; + ctx->chunked = 1; + ctx->request = get_request(r->options TSRMLS_CC); + + /* there are some limitations regarding TE:chunked, see https://tools.ietf.org/html/rfc7230#section-3.3.1 */ + if (ctx->request && ctx->request->http.version.major == 1 && ctx->request->http.version.minor == 0) { + ctx->version.minor = 0; + } + + r->ctx = ctx; + + return SUCCESS; } +static void php_http_env_response_stream_dtor(php_http_env_response_t *r) +{ + php_http_env_response_stream_ctx_t *ctx = r->ctx; + TSRMLS_FETCH_FROM_CTX(r->ts); -static 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(__invoke, ZEND_ACC_PUBLIC) - 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) + if (ctx->chunked_filter) { + php_stream_filter_free(ctx->chunked_filter TSRMLS_CC); + } + zend_hash_destroy(&ctx->header); + zend_list_delete(ctx->stream->rsrc_id); + efree(ctx); + r->ctx = NULL; +} +static void php_http_env_response_stream_header(php_http_env_response_stream_ctx_t *ctx, HashTable *header, php_http_buffer_t *buf TSRMLS_DC) +{ + HashPosition pos; + zval **val; - PHP_HTTP_ENV_RESPONSE_ME(send, ZEND_ACC_PUBLIC) + FOREACH_HASH_VAL(pos, header, val) { + if (Z_TYPE_PP(val) == IS_ARRAY) { + php_http_env_response_stream_header(ctx, Z_ARRVAL_PP(val), buf TSRMLS_CC); + } else { + zval *tmp = php_http_ztyp(IS_STRING, *val); - EMPTY_FUNCTION_ENTRY -}; + if (ctx->chunked) { + /* disable chunked transfer encoding if we've got an explicit content-length */ + if (!strncasecmp(Z_STRVAL_P(tmp), "Content-Length:", lenof("Content-Length:"))) { + ctx->chunked = 0; + } + } + php_http_buffer_append(buf, Z_STRVAL_P(tmp), Z_STRLEN_P(tmp)); + php_http_buffer_appends(buf, PHP_HTTP_CRLF); + zval_ptr_dtor(&tmp); + } + } +} +static STATUS php_http_env_response_stream_start(php_http_env_response_stream_ctx_t *ctx TSRMLS_DC) +{ + php_http_buffer_t header_buf; + + if (ctx->started || ctx->finished) { + return FAILURE; + } + + php_http_buffer_init(&header_buf); + 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)); + + /* there are some limitations regarding TE:chunked, see https://tools.ietf.org/html/rfc7230#section-3.3.1 */ + if (ctx->version.major == 1 && ctx->version.minor == 0) { + ctx->chunked = 0; + } else if (ctx->status_code == 204 || ctx->status_code/100 == 1) { + ctx->chunked = 0; + } else if (ctx->request && ctx->status_code/100 == 2 && !strcasecmp(ctx->request->http.info.request.method, "CONNECT")) { + ctx->chunked = 0; + } + + php_http_env_response_stream_header(ctx, &ctx->header, &header_buf TSRMLS_CC); + + /* enable chunked transfer encoding */ + if (ctx->chunked) { + php_http_buffer_appends(&header_buf, "Transfer-Encoding: chunked" PHP_HTTP_CRLF); + } + php_http_buffer_appends(&header_buf, PHP_HTTP_CRLF); + + if (header_buf.used == php_stream_write(ctx->stream, header_buf.data, header_buf.used)) { + ctx->started = 1; + } + php_http_buffer_dtor(&header_buf); + php_stream_flush(ctx->stream); + + if (ctx->chunked) { + ctx->chunked_filter = php_stream_filter_create("http.chunked_encode", NULL, 0 TSRMLS_CC); + php_stream_filter_append(&ctx->stream->writefilters, ctx->chunked_filter); + } + + return ctx->started ? SUCCESS : FAILURE; +} +static long php_http_env_response_stream_get_status(php_http_env_response_t *r) +{ + php_http_env_response_stream_ctx_t *ctx = r->ctx; + + return ctx->status_code; +} +static STATUS php_http_env_response_stream_set_status(php_http_env_response_t *r, long http_code) +{ + php_http_env_response_stream_ctx_t *stream_ctx = r->ctx; + + if (stream_ctx->started || stream_ctx->finished) { + return FAILURE; + } + + stream_ctx->status_code = http_code; + + return SUCCESS; +} +static STATUS php_http_env_response_stream_set_protocol_version(php_http_env_response_t *r, php_http_version_t *v) +{ + php_http_env_response_stream_ctx_t *stream_ctx = r->ctx; + + if (stream_ctx->started || stream_ctx->finished) { + return FAILURE; + } + + memcpy(&stream_ctx->version, v, sizeof(stream_ctx->version)); + + return SUCCESS; +} +static STATUS php_http_env_response_stream_set_header_ex(php_http_env_response_t *r, zend_bool replace, const char *fmt, va_list argv) +{ + php_http_env_response_stream_ctx_t *stream_ctx = r->ctx; + char *header_end, *header_str = NULL; + size_t header_len = 0; + zval *zheader, **zheader_ptr; + + if (stream_ctx->started || stream_ctx->finished) { + return FAILURE; + } + + header_len = vspprintf(&header_str, 0, fmt, argv); + + if (!(header_end = strchr(header_str, ':'))) { + efree(header_str); + return FAILURE; + } + + *header_end = '\0'; + + if (!replace && (SUCCESS == zend_hash_find(&stream_ctx->header, header_str, header_end - header_str + 1, (void *) &zheader_ptr))) { + convert_to_array(*zheader_ptr); + *header_end = ':'; + return add_next_index_stringl(*zheader_ptr, header_str, header_len, 0); + } else { + MAKE_STD_ZVAL(zheader); + ZVAL_STRINGL(zheader, header_str, header_len, 0); + + if (SUCCESS != zend_hash_update(&stream_ctx->header, header_str, header_end - header_str + 1, (void *) &zheader, sizeof(zval *), NULL)) { + zval_ptr_dtor(&zheader); + return FAILURE; + } + + *header_end = ':'; + return SUCCESS; + } +} +static STATUS php_http_env_response_stream_set_header(php_http_env_response_t *r, const char *fmt, ...) +{ + STATUS ret; + va_list argv; + + va_start(argv, fmt); + ret = php_http_env_response_stream_set_header_ex(r, 1, fmt, argv); + va_end(argv); + + return ret; +} +static STATUS php_http_env_response_stream_add_header(php_http_env_response_t *r, const char *fmt, ...) +{ + STATUS ret; + va_list argv; + + va_start(argv, fmt); + ret = php_http_env_response_stream_set_header_ex(r, 0, fmt, argv); + va_end(argv); + + return ret; +} +static STATUS php_http_env_response_stream_del_header(php_http_env_response_t *r, const char *header_str, size_t header_len) +{ + php_http_env_response_stream_ctx_t *stream_ctx = r->ctx; + + if (stream_ctx->started || stream_ctx->finished) { + return FAILURE; + } + + zend_hash_del(&stream_ctx->header, header_str, header_len + 1); + return SUCCESS; +} +static STATUS php_http_env_response_stream_write(php_http_env_response_t *r, const char *data_str, size_t data_len) +{ + php_http_env_response_stream_ctx_t *stream_ctx = r->ctx; + TSRMLS_FETCH_FROM_CTX(r->ts); + + if (stream_ctx->finished) { + return FAILURE; + } + if (!stream_ctx->started) { + if (SUCCESS != php_http_env_response_stream_start(stream_ctx TSRMLS_CC)) { + return FAILURE; + } + } + + if (data_len != php_stream_write(stream_ctx->stream, data_str, data_len)) { + return FAILURE; + } + + return SUCCESS; +} +static STATUS php_http_env_response_stream_flush(php_http_env_response_t *r) +{ + php_http_env_response_stream_ctx_t *stream_ctx = r->ctx; + TSRMLS_FETCH_FROM_CTX(r->ts); + if (stream_ctx->finished) { + return FAILURE; + } + if (!stream_ctx->started) { + if (SUCCESS != php_http_env_response_stream_start(stream_ctx TSRMLS_CC)) { + return FAILURE; + } + } -PHP_METHOD(HttpEnvResponse, __construct) + return php_stream_flush(stream_ctx->stream); +} +static STATUS php_http_env_response_stream_finish(php_http_env_response_t *r) { - with_error_handling(EH_THROW, php_http_exception_get_class_entry()) { - if (SUCCESS == zend_parse_parameters_none()) { - php_http_message_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC); + php_http_env_response_stream_ctx_t *ctx = r->ctx; + TSRMLS_FETCH_FROM_CTX(r->ts); - with_error_handling(EH_THROW, php_http_exception_get_class_entry()) { - obj->message = php_http_message_init_env(obj->message, PHP_HTTP_RESPONSE TSRMLS_CC); - } end_error_handling(); + if (ctx->finished) { + return FAILURE; + } + if (!ctx->started) { + if (SUCCESS != php_http_env_response_stream_start(ctx TSRMLS_CC)) { + return FAILURE; } - } end_error_handling(); + } + + php_stream_flush(ctx->stream); + if (ctx->chunked && ctx->chunked_filter) { + php_stream_filter_flush(ctx->chunked_filter, 1); + ctx->chunked_filter = php_stream_filter_remove(ctx->chunked_filter, 1 TSRMLS_CC); + } + ctx->finished = 1; + + return SUCCESS; } -PHP_METHOD(HttpEnvResponse, __invoke) +static php_http_env_response_ops_t php_http_env_response_stream_ops = { + php_http_env_response_stream_init, + php_http_env_response_stream_dtor, + php_http_env_response_stream_get_status, + php_http_env_response_stream_set_status, + php_http_env_response_stream_set_protocol_version, + php_http_env_response_stream_set_header, + php_http_env_response_stream_add_header, + php_http_env_response_stream_del_header, + php_http_env_response_stream_write, + php_http_env_response_stream_flush, + php_http_env_response_stream_finish +}; + +php_http_env_response_ops_t *php_http_env_response_get_stream_ops(void) +{ + return &php_http_env_response_stream_ops; +} + +#define PHP_HTTP_ENV_RESPONSE_OBJECT_INIT(obj) \ + do { \ + if (!obj->message) { \ + obj->message = php_http_message_init_env(NULL, PHP_HTTP_RESPONSE TSRMLS_CC); \ + } \ + } while (0) + +ZEND_BEGIN_ARG_INFO_EX(ai_HttpEnvResponse___construct, 0, 0, 0) +ZEND_END_ARG_INFO(); +static PHP_METHOD(HttpEnvResponse, __construct) +{ + php_http_message_object_t *obj; + + php_http_expect(SUCCESS == zend_parse_parameters_none(), invalid_arg, return); + + obj = zend_object_store_get_object(getThis() TSRMLS_CC); + + php_http_expect(obj->message = php_http_message_init_env(obj->message, PHP_HTTP_RESPONSE TSRMLS_CC), unexpected_val, return); +} + +ZEND_BEGIN_ARG_INFO_EX(ai_HttpEnvResponse___invoke, 0, 0, 1) + ZEND_ARG_INFO(0, ob_string) + ZEND_ARG_INFO(0, ob_flags) +ZEND_END_ARG_INFO(); +static PHP_METHOD(HttpEnvResponse, __invoke) { char *ob_str; int ob_len; @@ -777,62 +1210,102 @@ PHP_METHOD(HttpEnvResponse, __invoke) if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|l", &ob_str, &ob_len, &ob_flags)) { php_http_message_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC); - if (obj->body.handle || SUCCESS == php_http_new(&obj->body, php_http_message_body_get_class_entry(), (php_http_new_t) php_http_message_body_object_new_ex, NULL, (void *) php_http_message_body_copy(&obj->message->body, NULL, 0), NULL TSRMLS_CC)) { - php_http_message_body_append(&obj->message->body, ob_str, ob_len); - RETURN_TRUE; - } - RETURN_FALSE; + PHP_HTTP_ENV_RESPONSE_OBJECT_INIT(obj); + + php_http_message_object_init_body_object(obj); + php_http_message_body_append(obj->message->body, ob_str, ob_len); +#if PHP_VERSION_ID >= 50400 + RETURN_TRUE; +#else + RETURN_EMPTY_STRING(); +#endif } } -PHP_METHOD(HttpEnvResponse, setContentType) +ZEND_BEGIN_ARG_INFO_EX(ai_HttpEnvResponse_setEnvRequest, 0, 0, 1) + ZEND_ARG_OBJ_INFO(0, env_request, http\\Message, 1) +ZEND_END_ARG_INFO(); +static PHP_METHOD(HttpEnvResponse, setEnvRequest) +{ + zval *env_req = NULL; + + php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|O", &env_req, php_http_message_class_entry), invalid_arg, return); + + set_option(getThis(), ZEND_STRL("request"), IS_OBJECT, env_req, 0 TSRMLS_CC); + RETVAL_ZVAL(getThis(), 1, 0); +} + +ZEND_BEGIN_ARG_INFO_EX(ai_HttpEnvResponse_setContentType, 0, 0, 1) + ZEND_ARG_INFO(0, content_type) +ZEND_END_ARG_INFO(); +static 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_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s!", &ct_str, &ct_len), invalid_arg, return); + + set_option(getThis(), ZEND_STRL("contentType"), IS_STRING, ct_str, ct_len TSRMLS_CC); + RETVAL_ZVAL(getThis(), 1, 0); } -PHP_METHOD(HttpEnvResponse, setContentDisposition) +ZEND_BEGIN_ARG_INFO_EX(ai_HttpEnvResponse_setContentDisposition, 0, 0, 1) + ZEND_ARG_ARRAY_INFO(0, disposition_params, 1) +ZEND_END_ARG_INFO(); +static PHP_METHOD(HttpEnvResponse, setContentDisposition) { zval *zdisposition; - if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a", &zdisposition)) { - zend_update_property(Z_OBJCE_P(getThis()), getThis(), ZEND_STRL("contentDisposition"), zdisposition TSRMLS_CC); - } + php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a", &zdisposition), invalid_arg, return); + + zend_update_property(Z_OBJCE_P(getThis()), getThis(), ZEND_STRL("contentDisposition"), zdisposition TSRMLS_CC); + RETVAL_ZVAL(getThis(), 1, 0); } -PHP_METHOD(HttpEnvResponse, setContentEncoding) +ZEND_BEGIN_ARG_INFO_EX(ai_HttpEnvResponse_setContentEncoding, 0, 0, 1) + ZEND_ARG_INFO(0, content_encoding) +ZEND_END_ARG_INFO(); +static 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_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &ce), invalid_arg, return); + + set_option(getThis(), ZEND_STRL("contentEncoding"), IS_LONG, &ce, 0 TSRMLS_CC); + RETVAL_ZVAL(getThis(), 1, 0); } -PHP_METHOD(HttpEnvResponse, setCacheControl) +ZEND_BEGIN_ARG_INFO_EX(ai_HttpEnvResponse_setCacheControl, 0, 0, 1) + ZEND_ARG_INFO(0, cache_control) +ZEND_END_ARG_INFO(); +static 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_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s!", &cc_str, &cc_len), invalid_arg, return); + + set_option(getThis(), ZEND_STRL("cacheControl"), IS_STRING, cc_str, cc_len TSRMLS_CC); + RETVAL_ZVAL(getThis(), 1, 0); } -PHP_METHOD(HttpEnvResponse, setLastModified) +ZEND_BEGIN_ARG_INFO_EX(ai_HttpEnvResponse_setLastModified, 0, 0, 1) + ZEND_ARG_INFO(0, last_modified) +ZEND_END_ARG_INFO(); +static 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_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &last_modified), invalid_arg, return); + + set_option(getThis(), ZEND_STRL("lastModified"), IS_LONG, &last_modified, 0 TSRMLS_CC); + RETVAL_ZVAL(getThis(), 1, 0); } -PHP_METHOD(HttpEnvResponse, isCachedByLastModified) +ZEND_BEGIN_ARG_INFO_EX(ai_HttpEnvResponse_isCachedByLastModified, 0, 0, 0) + ZEND_ARG_INFO(0, header_name) +ZEND_END_ARG_INFO(); +static PHP_METHOD(HttpEnvResponse, isCachedByLastModified) { char *header_name_str = NULL; int header_name_len = 0; @@ -842,22 +1315,29 @@ PHP_METHOD(HttpEnvResponse, isCachedByLastModified) 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_LONG(php_http_env_is_response_cached_by_last_modified(getThis(), header_name_str, header_name_len, get_request(getThis() TSRMLS_CC) TSRMLS_CC)); } - RETURN_FALSE; } -PHP_METHOD(HttpEnvResponse, setEtag) +ZEND_BEGIN_ARG_INFO_EX(ai_HttpEnvResponse_setEtag, 0, 0, 1) + ZEND_ARG_INFO(0, etag) +ZEND_END_ARG_INFO(); +static 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_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s!", &etag_str, &etag_len), invalid_arg, return); + + set_option(getThis(), ZEND_STRL("etag"), IS_STRING, etag_str, etag_len TSRMLS_CC); + RETVAL_ZVAL(getThis(), 1, 0); } -PHP_METHOD(HttpEnvResponse, isCachedByEtag) +ZEND_BEGIN_ARG_INFO_EX(ai_HttpEnvResponse_isCachedByEtag, 0, 0, 0) + ZEND_ARG_INFO(0, header_name) +ZEND_END_ARG_INFO(); +static PHP_METHOD(HttpEnvResponse, isCachedByEtag) { char *header_name_str = NULL; int header_name_len = 0; @@ -867,42 +1347,133 @@ PHP_METHOD(HttpEnvResponse, isCachedByEtag) 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_LONG(php_http_env_is_response_cached_by_etag(getThis(), header_name_str, header_name_len, get_request(getThis() TSRMLS_CC) TSRMLS_CC)); } - RETURN_FALSE; } -PHP_METHOD(HttpEnvResponse, setThrottleRate) +ZEND_BEGIN_ARG_INFO_EX(ai_HttpEnvResponse_setThrottleRate, 0, 0, 1) + ZEND_ARG_INFO(0, chunk_size) + ZEND_ARG_INFO(0, delay) +ZEND_END_ARG_INFO(); +static 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)) { - set_option(getThis(), ZEND_STRL("throttleDelay"), IS_DOUBLE, &delay, 0 TSRMLS_CC); - set_option(getThis(), ZEND_STRL("throttleChunk"), IS_LONG, &chunk_size, 0 TSRMLS_CC); - RETURN_TRUE; + php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l|d", &chunk_size, &delay), invalid_arg, return); + + set_option(getThis(), ZEND_STRL("throttleDelay"), IS_DOUBLE, &delay, 0 TSRMLS_CC); + set_option(getThis(), ZEND_STRL("throttleChunk"), IS_LONG, &chunk_size, 0 TSRMLS_CC); + RETVAL_ZVAL(getThis(), 1, 0); +} + +ZEND_BEGIN_ARG_INFO_EX(ai_HttpEnvResponse_setCookie, 0, 0, 1) + ZEND_ARG_INFO(0, cookie) +ZEND_END_ARG_INFO(); +static PHP_METHOD(HttpEnvResponse, setCookie) +{ + zval *zcookie_new; + zend_error_handling zeh; + php_http_cookie_list_t *list = NULL; + + php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &zcookie_new), invalid_arg, return); + + zend_replace_error_handling(EH_THROW, php_http_exception_unexpected_val_class_entry, &zeh TSRMLS_CC); + switch (Z_TYPE_P(zcookie_new)) { + case IS_OBJECT: + if (instanceof_function(Z_OBJCE_P(zcookie_new), php_http_cookie_class_entry TSRMLS_CC)) { + Z_ADDREF_P(zcookie_new); + break; + } + /* no break */ + case IS_ARRAY: + list = php_http_cookie_list_from_struct(NULL, zcookie_new TSRMLS_CC); + MAKE_STD_ZVAL(zcookie_new); + ZVAL_OBJVAL(zcookie_new, php_http_cookie_object_new_ex(php_http_cookie_class_entry, list, NULL TSRMLS_CC), 0); + break; + + default: + zcookie_new = php_http_ztyp(IS_STRING, zcookie_new); + list = php_http_cookie_list_parse(NULL, Z_STRVAL_P(zcookie_new), Z_STRLEN_P(zcookie_new), 0, NULL TSRMLS_CC); + zval_ptr_dtor(&zcookie_new); + MAKE_STD_ZVAL(zcookie_new); + ZVAL_OBJVAL(zcookie_new, php_http_cookie_object_new_ex(php_http_cookie_class_entry, list, NULL TSRMLS_CC), 0); } - RETURN_FALSE; + zend_restore_error_handling(&zeh TSRMLS_CC); + + set_cookie(getThis(), zcookie_new TSRMLS_CC); + zval_ptr_dtor(&zcookie_new); + + RETVAL_ZVAL(getThis(), 1, 0); } -PHP_METHOD(HttpEnvResponse, send) +ZEND_BEGIN_ARG_INFO_EX(ai_HttpEnvResponse_send, 0, 0, 0) + ZEND_ARG_INFO(0, stream) +ZEND_END_ARG_INFO(); +static PHP_METHOD(HttpEnvResponse, send) { - RETVAL_FALSE; + zval *zstream = NULL; + php_stream *s = NULL; + + if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|r", &zstream)) { + /* first flush the output layer to avoid conflicting headers and output; + * also, ob_start($thisEnvResponse) might have been called */ +#if PHP_VERSION_ID >= 50400 + php_output_end_all(TSRMLS_C); +#else + php_end_ob_buffers(1 TSRMLS_CC); +#endif - if (SUCCESS == zend_parse_parameters_none()) { - php_http_env_response_t *r = php_http_env_response_init(NULL, getThis() TSRMLS_CC); + if (zstream) { + php_http_env_response_t *r; - if (r) { - RETVAL_SUCCESS(php_http_env_response_send(r)); - } + php_stream_from_zval(s, &zstream); + r = php_http_env_response_init(NULL, getThis(), php_http_env_response_get_stream_ops(), s TSRMLS_CC); + if (!r) { + RETURN_FALSE; + } - php_http_env_response_free(&r); + RETVAL_BOOL(SUCCESS == php_http_env_response_send(r)); + php_http_env_response_free(&r); + } else { + php_http_env_response_t r; + + if (!php_http_env_response_init(&r, getThis(), NULL, NULL TSRMLS_CC)) { + RETURN_FALSE; + } + + RETVAL_BOOL(SUCCESS == php_http_env_response_send(&r)); + php_http_env_response_dtor(&r); + } } } +static zend_function_entry php_http_env_response_methods[] = { + PHP_ME(HttpEnvResponse, __construct, ai_HttpEnvResponse___construct, ZEND_ACC_PUBLIC|ZEND_ACC_CTOR) + PHP_ME(HttpEnvResponse, __invoke, ai_HttpEnvResponse___invoke, ZEND_ACC_PUBLIC) + PHP_ME(HttpEnvResponse, setEnvRequest, ai_HttpEnvResponse_setEnvRequest, ZEND_ACC_PUBLIC) + PHP_ME(HttpEnvResponse, setCookie, ai_HttpEnvResponse_setCookie, ZEND_ACC_PUBLIC) + PHP_ME(HttpEnvResponse, setContentType, ai_HttpEnvResponse_setContentType, ZEND_ACC_PUBLIC) + PHP_ME(HttpEnvResponse, setContentDisposition, ai_HttpEnvResponse_setContentDisposition, ZEND_ACC_PUBLIC) + PHP_ME(HttpEnvResponse, setContentEncoding, ai_HttpEnvResponse_setContentEncoding, ZEND_ACC_PUBLIC) + PHP_ME(HttpEnvResponse, setCacheControl, ai_HttpEnvResponse_setCacheControl, ZEND_ACC_PUBLIC) + PHP_ME(HttpEnvResponse, setLastModified, ai_HttpEnvResponse_setLastModified, ZEND_ACC_PUBLIC) + PHP_ME(HttpEnvResponse, isCachedByLastModified, ai_HttpEnvResponse_isCachedByLastModified, ZEND_ACC_PUBLIC) + PHP_ME(HttpEnvResponse, setEtag, ai_HttpEnvResponse_setEtag, ZEND_ACC_PUBLIC) + PHP_ME(HttpEnvResponse, isCachedByEtag, ai_HttpEnvResponse_isCachedByEtag, ZEND_ACC_PUBLIC) + PHP_ME(HttpEnvResponse, setThrottleRate, ai_HttpEnvResponse_setThrottleRate, ZEND_ACC_PUBLIC) + PHP_ME(HttpEnvResponse, send, ai_HttpEnvResponse_send, ZEND_ACC_PUBLIC) + EMPTY_FUNCTION_ENTRY +}; + +zend_class_entry *php_http_env_response_class_entry; + PHP_MINIT_FUNCTION(http_env_response) { - PHP_HTTP_REGISTER_CLASS(http\\Env, Response, http_env_response, php_http_message_get_class_entry(), 0); + zend_class_entry ce = {0}; + + INIT_NS_CLASS_ENTRY(ce, "http\\Env", "Response", php_http_env_response_methods); + php_http_env_response_class_entry = zend_register_internal_class_ex(&ce, php_http_message_class_entry, NULL 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); @@ -911,6 +1482,8 @@ PHP_MINIT_FUNCTION(http_env_response) 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("request"), ZEND_ACC_PROTECTED TSRMLS_CC); + zend_declare_property_null(php_http_env_response_class_entry, ZEND_STRL("cookies"), ZEND_ACC_PROTECTED 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);