X-Git-Url: https://git.m6w6.name/?p=m6w6%2Fext-http;a=blobdiff_plain;f=php_http_env_response.c;h=67a6d5830e4bf4d07b63b7db7b5b1df4b108050c;hp=67841877f5a97623b94f7a4e2c6ed7ecf3098128;hb=refs%2Fheads%2Fv2.4.x;hpb=d31439f6176e10c612a991546e15a27f755343c6 diff --git a/php_http_env_response.c b/php_http_env_response.c index 6784187..67a6d58 100644 --- a/php_http_env_response.c +++ b/php_http_env_response.c @@ -6,7 +6,7 @@ | modification, are permitted provided that the conditions mentioned | | in the accompanying LICENSE file are met. | +--------------------------------------------------------------------+ - | Copyright (c) 2004-2013, Michael Wallner | + | Copyright (c) 2004-2014, Michael Wallner | +--------------------------------------------------------------------+ */ @@ -26,6 +26,7 @@ static void set_option(zval *options, const char *name_str, size_t name_len, int case IS_STRING: 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; @@ -47,6 +48,7 @@ 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); @@ -108,8 +110,38 @@ static php_http_message_t *get_request(zval *options TSRMLS_DC) 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); + } + } -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, php_http_message_t *request TSRMLS_DC) + 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; @@ -147,11 +179,11 @@ PHP_HTTP_API php_http_cache_status_t php_http_env_is_response_cached_by_etag(zva 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, php_http_message_t *request 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; @@ -190,7 +222,7 @@ PHP_HTTP_API php_http_cache_status_t php_http_env_is_response_cached_by_last_mod } } - STR_FREE(header); + PTR_FREE(header); return ret; } @@ -202,7 +234,7 @@ static zend_bool php_http_env_response_is_cacheable(php_http_env_response_t *r, return 0; } - if (php_http_env_got_request_header(ZEND_STRL("Authorizsation"), request TSRMLS_CC)) { + if (php_http_env_got_request_header(ZEND_STRL("Authorization"), request TSRMLS_CC)) { return 0; } @@ -217,7 +249,9 @@ static size_t output(void *context, char *buf, size_t len TSRMLS_DC) { php_http_env_response_t *r = context; - r->ops->write(r, 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 */ @@ -229,9 +263,9 @@ 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) +static ZEND_RESULT_CODE 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) { @@ -248,18 +282,19 @@ 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, php_http_env_response_ops_t *ops, void *init_arg 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) { zend_bool free_r; @@ -293,21 +328,21 @@ PHP_HTTP_API php_http_env_response_t *php_http_env_response_init(php_http_env_re 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); @@ -316,9 +351,9 @@ 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, php_http_message_t *request) +static ZEND_RESULT_CODE php_http_env_response_send_head(php_http_env_response_t *r, php_http_message_t *request) { - STATUS ret = SUCCESS; + ZEND_RESULT_CODE ret = SUCCESS; zval *zoption, *options = r->options; TSRMLS_FETCH_FROM_CTX(r->ts); @@ -326,6 +361,17 @@ static STATUS php_http_env_response_send_head(php_http_env_response_t *r, php_ht 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); @@ -356,9 +402,25 @@ static STATUS php_http_env_response_send_head(php_http_env_response_t *r, php_ht 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) { - php_http_headers_to_callback(Z_ARRVAL_P(zoption), 0, (php_http_pass_format_callback_t) r->ops->set_header, r TSRMLS_CC); + HashPosition pos; + zval **zcookie; + + 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); + } + } } zval_ptr_dtor(&zoption); } @@ -371,11 +433,9 @@ static STATUS php_http_env_response_send_head(php_http_env_response_t *r, php_ht 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 = 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)); - } + 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); @@ -548,9 +608,9 @@ static STATUS php_http_env_response_send_head(php_http_env_response_t *r, php_ht return ret; } -static STATUS php_http_env_response_send_body(php_http_env_response_t *r) +static ZEND_RESULT_CODE php_http_env_response_send_body(php_http_env_response_t *r) { - STATUS ret = SUCCESS; + ZEND_RESULT_CODE ret = SUCCESS; zval *zoption; php_http_message_body_t *body; TSRMLS_FETCH_FROM_CTX(r->ts); @@ -582,10 +642,11 @@ 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(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 { /* this should never happen */ zend_hash_destroy(&r->range.values); @@ -614,23 +675,28 @@ 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(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(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); + } } } return ret; } -PHP_HTTP_API STATUS php_http_env_response_send(php_http_env_response_t *r) +ZEND_RESULT_CODE php_http_env_response_send(php_http_env_response_t *r) { php_http_message_t *request; php_http_message_body_t *body; @@ -695,14 +761,17 @@ PHP_HTTP_API STATUS php_http_env_response_send(php_http_env_response_t *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; } @@ -715,21 +784,21 @@ static long php_http_env_response_sapi_get_status(php_http_env_response_t *r) 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) +static ZEND_RESULT_CODE php_http_env_response_sapi_set_status(php_http_env_response_t *r, long http_code) { TSRMLS_FETCH_FROM_CTX(r->ts); 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) +static ZEND_RESULT_CODE 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); 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, ...) +static ZEND_RESULT_CODE php_http_env_response_sapi_set_header(php_http_env_response_t *r, const char *fmt, ...) { - STATUS ret; + ZEND_RESULT_CODE ret; va_list args; TSRMLS_FETCH_FROM_CTX(r->ts); @@ -739,9 +808,9 @@ static STATUS php_http_env_response_sapi_set_header(php_http_env_response_t *r, return ret; } -static STATUS php_http_env_response_sapi_add_header(php_http_env_response_t *r, const char *fmt, ...) +static ZEND_RESULT_CODE php_http_env_response_sapi_add_header(php_http_env_response_t *r, const char *fmt, ...) { - STATUS ret; + ZEND_RESULT_CODE ret; va_list args; TSRMLS_FETCH_FROM_CTX(r->ts); @@ -751,13 +820,13 @@ static STATUS php_http_env_response_sapi_add_header(php_http_env_response_t *r, 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) +static ZEND_RESULT_CODE php_http_env_response_sapi_del_header(php_http_env_response_t *r, const char *header_str, size_t header_len) { TSRMLS_FETCH_FROM_CTX(r->ts); 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) +static ZEND_RESULT_CODE 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); @@ -766,7 +835,7 @@ static STATUS php_http_env_response_sapi_write(php_http_env_response_t *r, const } return FAILURE; } -static STATUS php_http_env_response_sapi_flush(php_http_env_response_t *r) +static ZEND_RESULT_CODE php_http_env_response_sapi_flush(php_http_env_response_t *r) { TSRMLS_FETCH_FROM_CTX(r->ts); @@ -784,7 +853,7 @@ static STATUS php_http_env_response_sapi_flush(php_http_env_response_t *r) return SUCCESS; } -static STATUS php_http_env_response_sapi_finish(php_http_env_response_t *r) +static ZEND_RESULT_CODE php_http_env_response_sapi_finish(php_http_env_response_t *r) { return SUCCESS; } @@ -803,7 +872,7 @@ static php_http_env_response_ops_t php_http_env_response_sapi_ops = { php_http_env_response_sapi_finish }; -PHP_HTTP_API php_http_env_response_ops_t *php_http_env_response_get_sapi_ops(void) +php_http_env_response_ops_t *php_http_env_response_get_sapi_ops(void) { return &php_http_env_response_sapi_ops; } @@ -814,26 +883,38 @@ typedef struct php_http_env_response_stream_ctx { long status_code; php_stream *stream; + php_stream_filter *chunked_filter; + php_http_message_t *request; unsigned started:1; unsigned finished:1; + unsigned chunked:1; } php_http_env_response_stream_ctx_t; -static STATUS php_http_env_response_stream_init(php_http_env_response_t *r, void *init_arg) +static ZEND_RESULT_CODE php_http_env_response_stream_init(php_http_env_response_t *r, void *init_arg) { 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 (SUCCESS != zend_list_addref(ctx->stream->rsrc_id)) { + 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; @@ -844,36 +925,77 @@ 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); + if (ctx->chunked_filter) { + ctx->chunked_filter = php_stream_filter_remove(ctx->chunked_filter, 1 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 TSRMLS_DC) +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; - FOREACH_HASH_VAL(pos, &ctx->header, val) { + FOREACH_HASH_VAL(pos, header, val) { if (Z_TYPE_PP(val) == IS_ARRAY) { - php_http_env_response_stream_header(ctx, Z_ARRVAL_PP(val) TSRMLS_CC); + php_http_env_response_stream_header(ctx, Z_ARRVAL_PP(val), buf TSRMLS_CC); } else { - php_stream_write(ctx->stream, Z_STRVAL_PP(val), Z_STRLEN_PP(val)); - php_stream_write_string(ctx->stream, PHP_HTTP_CRLF); + zval *tmp = php_http_ztyp(IS_STRING, *val); + + 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) +static ZEND_RESULT_CODE 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_stream_printf(ctx->stream TSRMLS_CC, "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)); - php_http_env_response_stream_header(ctx, &ctx->header TSRMLS_CC); - php_stream_write_string(ctx->stream, PHP_HTTP_CRLF); - ctx->started = 1; - return SUCCESS; + 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) { @@ -881,7 +1003,7 @@ static long php_http_env_response_stream_get_status(php_http_env_response_t *r) return ctx->status_code; } -static STATUS php_http_env_response_stream_set_status(php_http_env_response_t *r, long http_code) +static ZEND_RESULT_CODE 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; @@ -893,7 +1015,7 @@ static STATUS php_http_env_response_stream_set_status(php_http_env_response_t *r return SUCCESS; } -static STATUS php_http_env_response_stream_set_protocol_version(php_http_env_response_t *r, php_http_version_t *v) +static ZEND_RESULT_CODE 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; @@ -905,7 +1027,7 @@ static STATUS php_http_env_response_stream_set_protocol_version(php_http_env_res 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) +static ZEND_RESULT_CODE php_http_env_response_stream_set_header_ex(php_http_env_response_t *r, zend_bool replace, const char *fmt, va_list argv) { php_http_env_response_stream_ctx_t *stream_ctx = r->ctx; char *header_end, *header_str = NULL; @@ -942,9 +1064,9 @@ static STATUS php_http_env_response_stream_set_header_ex(php_http_env_response_t return SUCCESS; } } -static STATUS php_http_env_response_stream_set_header(php_http_env_response_t *r, const char *fmt, ...) +static ZEND_RESULT_CODE php_http_env_response_stream_set_header(php_http_env_response_t *r, const char *fmt, ...) { - STATUS ret; + ZEND_RESULT_CODE ret; va_list argv; va_start(argv, fmt); @@ -953,9 +1075,9 @@ static STATUS php_http_env_response_stream_set_header(php_http_env_response_t *r return ret; } -static STATUS php_http_env_response_stream_add_header(php_http_env_response_t *r, const char *fmt, ...) +static ZEND_RESULT_CODE php_http_env_response_stream_add_header(php_http_env_response_t *r, const char *fmt, ...) { - STATUS ret; + ZEND_RESULT_CODE ret; va_list argv; va_start(argv, fmt); @@ -964,7 +1086,7 @@ static STATUS php_http_env_response_stream_add_header(php_http_env_response_t *r 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) +static ZEND_RESULT_CODE php_http_env_response_stream_del_header(php_http_env_response_t *r, const char *header_str, size_t header_len) { php_http_env_response_stream_ctx_t *stream_ctx = r->ctx; @@ -975,7 +1097,7 @@ static STATUS php_http_env_response_stream_del_header(php_http_env_response_t *r 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) +static ZEND_RESULT_CODE 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); @@ -989,11 +1111,13 @@ static STATUS php_http_env_response_stream_write(php_http_env_response_t *r, con } } - php_stream_write(stream_ctx->stream, data_str, data_len); + 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) +static ZEND_RESULT_CODE 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); @@ -1009,21 +1133,27 @@ static STATUS php_http_env_response_stream_flush(php_http_env_response_t *r) return php_stream_flush(stream_ctx->stream); } -static STATUS php_http_env_response_stream_finish(php_http_env_response_t *r) +static ZEND_RESULT_CODE php_http_env_response_stream_finish(php_http_env_response_t *r) { - php_http_env_response_stream_ctx_t *stream_ctx = r->ctx; + php_http_env_response_stream_ctx_t *ctx = r->ctx; TSRMLS_FETCH_FROM_CTX(r->ts); - if (stream_ctx->finished) { + if (ctx->finished) { return FAILURE; } - if (!stream_ctx->started) { - if (SUCCESS != php_http_env_response_stream_start(stream_ctx TSRMLS_CC)) { + if (!ctx->started) { + if (SUCCESS != php_http_env_response_stream_start(ctx TSRMLS_CC)) { return FAILURE; } } - stream_ctx->finished = 1; + 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; } @@ -1042,7 +1172,7 @@ static php_http_env_response_ops_t php_http_env_response_stream_ops = { php_http_env_response_stream_finish }; -PHP_HTTP_API php_http_env_response_ops_t *php_http_env_response_get_stream_ops(void) +php_http_env_response_ops_t *php_http_env_response_get_stream_ops(void) { return &php_http_env_response_stream_ops; } @@ -1058,17 +1188,13 @@ ZEND_BEGIN_ARG_INFO_EX(ai_HttpEnvResponse___construct, 0, 0, 0) ZEND_END_ARG_INFO(); static PHP_METHOD(HttpEnvResponse, __construct) { - with_error_handling(EH_THROW, php_http_exception_class_entry) { - php_http_message_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC); + php_http_message_object_t *obj; - if (SUCCESS == zend_parse_parameters_none()) { - 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(); - } - PHP_HTTP_ENV_RESPONSE_OBJECT_INIT(obj); - } end_error_handling(); + 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) @@ -1086,11 +1212,13 @@ static PHP_METHOD(HttpEnvResponse, __invoke) PHP_HTTP_ENV_RESPONSE_OBJECT_INIT(obj); - if (obj->body || SUCCESS == php_http_new(NULL, php_http_message_body_class_entry, (php_http_new_t) php_http_message_body_object_new_ex, NULL, (void *) php_http_message_body_init(&obj->message->body, NULL TSRMLS_CC), (void *) &obj->body TSRMLS_CC)) { - php_http_message_body_append(obj->message->body, ob_str, ob_len); - RETURN_TRUE; - } - RETURN_FALSE; + 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 } } @@ -1101,10 +1229,10 @@ static PHP_METHOD(HttpEnvResponse, setEnvRequest) { zval *env_req = NULL; - if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|O", &env_req, php_http_message_class_entry)) { - set_option(getThis(), ZEND_STRL("request"), IS_OBJECT, env_req, 0 TSRMLS_CC); - RETVAL_ZVAL(getThis(), 1, 0); - } + 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) @@ -1115,10 +1243,10 @@ 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); - RETVAL_ZVAL(getThis(), 1, 0); - } + 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); } ZEND_BEGIN_ARG_INFO_EX(ai_HttpEnvResponse_setContentDisposition, 0, 0, 1) @@ -1128,10 +1256,10 @@ 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); - RETVAL_ZVAL(getThis(), 1, 0); - } + 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); } ZEND_BEGIN_ARG_INFO_EX(ai_HttpEnvResponse_setContentEncoding, 0, 0, 1) @@ -1141,10 +1269,10 @@ 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); - RETVAL_ZVAL(getThis(), 1, 0); - } + 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); } ZEND_BEGIN_ARG_INFO_EX(ai_HttpEnvResponse_setCacheControl, 0, 0, 1) @@ -1155,10 +1283,10 @@ 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); - RETVAL_ZVAL(getThis(), 1, 0); - } + 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); } ZEND_BEGIN_ARG_INFO_EX(ai_HttpEnvResponse_setLastModified, 0, 0, 1) @@ -1168,10 +1296,10 @@ 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); - RETVAL_ZVAL(getThis(), 1, 0); - } + 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); } ZEND_BEGIN_ARG_INFO_EX(ai_HttpEnvResponse_isCachedByLastModified, 0, 0, 0) @@ -1200,10 +1328,10 @@ 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); - RETVAL_ZVAL(getThis(), 1, 0); - } + 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); } ZEND_BEGIN_ARG_INFO_EX(ai_HttpEnvResponse_isCachedByEtag, 0, 0, 0) @@ -1232,11 +1360,51 @@ 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); - RETVAL_ZVAL(getThis(), 1, 0); - } + 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); + } + zend_restore_error_handling(&zeh TSRMLS_CC); + + set_cookie(getThis(), zcookie_new TSRMLS_CC); + zval_ptr_dtor(&zcookie_new); + + RETVAL_ZVAL(getThis(), 1, 0); } ZEND_BEGIN_ARG_INFO_EX(ai_HttpEnvResponse_send, 0, 0, 0) @@ -1255,6 +1423,7 @@ static PHP_METHOD(HttpEnvResponse, send) #else php_end_ob_buffers(1 TSRMLS_CC); #endif + if (zstream) { php_http_env_response_t *r; @@ -1283,6 +1452,7 @@ 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) @@ -1313,6 +1483,7 @@ PHP_MINIT_FUNCTION(http_env_response) 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);