X-Git-Url: https://git.m6w6.name/?p=m6w6%2Fext-http;a=blobdiff_plain;f=php_http_env_response.c;h=67a6d5830e4bf4d07b63b7db7b5b1df4b108050c;hp=7ccad74fb722c4d7a78e1f39998112346dc43363;hb=3974e77c71879dda04f7fdc92eb0ff4f647dc1f2;hpb=064dc1aacc34f21fb86fa50c76656e9df0e69bc3 diff --git a/php_http_env_response.c b/php_http_env_response.c index 7ccad74..67a6d58 100644 --- a/php_http_env_response.c +++ b/php_http_env_response.c @@ -263,7 +263,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) +static ZEND_RESULT_CODE php_http_env_response_send_data(php_http_env_response_t *r, const char *buf, size_t len) { size_t chunks_sent, chunk = r->throttle.chunk ? r->throttle.chunk : PHP_HTTP_SENDBUF_SIZE; TSRMLS_FETCH_FROM_CTX(r->ts); @@ -351,9 +351,9 @@ 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); @@ -608,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); @@ -696,7 +696,7 @@ static STATUS php_http_env_response_send_body(php_http_env_response_t *r) return ret; } -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; @@ -784,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); @@ -808,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); @@ -820,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); @@ -835,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); @@ -853,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; } @@ -883,6 +883,7 @@ 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; @@ -890,7 +891,7 @@ typedef struct php_http_env_response_stream_ctx { 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; @@ -924,6 +925,9 @@ 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); @@ -952,7 +956,7 @@ static void php_http_env_response_stream_header(php_http_env_response_stream_ctx } } } -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; @@ -980,8 +984,16 @@ static STATUS php_http_env_response_stream_start(php_http_env_response_stream_ct } php_http_buffer_appends(&header_buf, PHP_HTTP_CRLF); - ctx->started = (header_buf.used == php_stream_write(ctx->stream, header_buf.data, header_buf.used)); + 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; } @@ -991,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; @@ -1003,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; @@ -1015,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; @@ -1052,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); @@ -1063,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); @@ -1074,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; @@ -1085,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); @@ -1099,21 +1111,13 @@ static STATUS php_http_env_response_stream_write(php_http_env_response_t *r, con } } - if (stream_ctx->chunked && 0 == php_stream_printf(stream_ctx->stream TSRMLS_CC, "%lx" PHP_HTTP_CRLF, (unsigned long) data_len)) { - return FAILURE; - } - if (data_len != php_stream_write(stream_ctx->stream, data_str, data_len)) { return FAILURE; } - if (stream_ctx->chunked && 2 != php_stream_write_string(stream_ctx->stream, PHP_HTTP_CRLF)) { - 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); @@ -1129,25 +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; } } - if (stream_ctx->chunked && 5 != php_stream_write_string(stream_ctx->stream, "0" PHP_HTTP_CRLF PHP_HTTP_CRLF)) { - return FAILURE; + 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); } - stream_ctx->finished = 1; + ctx->finished = 1; return SUCCESS; }