X-Git-Url: https://git.m6w6.name/?p=m6w6%2Fext-http;a=blobdiff_plain;f=php_http_env_response.c;h=0bcc21d9127ee025c2f7026b7fa7288bb28d495f;hp=04c8a980fc88946a5f41a94a484b729ad858b5bc;hb=refs%2Fheads%2Fv2.3.x;hpb=22d8bd9cce8969e37269f1e817da0904a6af522c diff --git a/php_http_env_response.c b/php_http_env_response.c index 04c8a98..0bcc21d 100644 --- a/php_http_env_response.c +++ b/php_http_env_response.c @@ -883,6 +883,8 @@ 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; @@ -892,19 +894,27 @@ typedef struct php_http_env_response_stream_ctx { static STATUS 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; @@ -915,19 +925,22 @@ 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, 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 { zval *tmp = php_http_ztyp(IS_STRING, *val); @@ -937,28 +950,52 @@ static void php_http_env_response_stream_header(php_http_env_response_stream_ctx ctx->chunked = 0; } } - php_stream_write(ctx->stream, Z_STRVAL_P(tmp), Z_STRLEN_P(tmp)); - php_stream_write_string(ctx->stream, PHP_HTTP_CRLF); + 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_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_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_stream_write_string(ctx->stream, "Transfer-Encoding: chunked" PHP_HTTP_CRLF PHP_HTTP_CRLF); - } else { - php_stream_write_string(ctx->stream, PHP_HTTP_CRLF); + php_http_buffer_appends(&header_buf, "Transfer-Encoding: chunked" PHP_HTTP_CRLF); } - ctx->started = 1; - return SUCCESS; + 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) { @@ -1074,18 +1111,10 @@ 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) @@ -1106,23 +1135,25 @@ static STATUS php_http_env_response_stream_flush(php_http_env_response_t *r) } static STATUS 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; }