X-Git-Url: https://git.m6w6.name/?p=m6w6%2Fext-http;a=blobdiff_plain;f=php_http_env_response.c;h=9bc3f5bc394e743f3759dd0ee0da869b893266c1;hp=7c76c081870ed8fbfe9ec7a6a345b2dbecc2cb4f;hb=8840a33028d28fa4f351b08b7502c1642c758af5;hpb=be9585b914061f84c5d5939673d29ecc048ab1d7 diff --git a/php_http_env_response.c b/php_http_env_response.c index 7c76c08..9bc3f5b 100644 --- a/php_http_env_response.c +++ b/php_http_env_response.c @@ -207,7 +207,9 @@ php_http_cache_status_t php_http_env_is_response_cached_by_last_modified(zval *o static zend_bool php_http_env_response_is_cacheable(php_http_env_response_t *r, php_http_message_t *request) { - if (r->ops->get_status(r) >= 400) { + long status = r->ops->get_status(r); + + if (status && status / 100 != 2) { return 0; } @@ -834,6 +836,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; @@ -844,6 +847,7 @@ typedef struct php_http_env_response_stream_ctx { 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; ctx = ecalloc(1, sizeof(*ctx)); @@ -851,6 +855,7 @@ static ZEND_RESULT_CODE php_http_env_response_stream_init(php_http_env_response_ ++GC_REFCOUNT(ctx->stream->res); ZEND_INIT_SYMTABLE(&ctx->header); php_http_version_init(&ctx->version, 1, 1); + php_stream_set_option(ctx->stream, PHP_STREAM_OPTION_WRITE_BUFFER, PHP_STREAM_BUFFER_FULL, &buffer_size); ctx->status_code = 200; ctx->chunked = 1; ctx->request = get_request(&r->options); @@ -868,19 +873,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; + if (ctx->chunked_filter) { + ctx->chunked_filter = php_stream_filter_remove(ctx->chunked_filter, 1); + } zend_hash_destroy(&ctx->header); zend_list_delete(ctx->stream->res); efree(ctx); r->ctx = NULL; } -static void php_http_env_response_stream_header(php_http_env_response_stream_ctx_t *ctx, HashTable *header) +static void php_http_env_response_stream_header(php_http_env_response_stream_ctx_t *ctx, HashTable *header, php_http_buffer_t *buf) { zval *val; ZEND_HASH_FOREACH_VAL(header, val) { if (Z_TYPE_P(val) == IS_ARRAY) { - php_http_env_response_stream_header(ctx, Z_ARRVAL_P(val)); + php_http_env_response_stream_header(ctx, Z_ARRVAL_P(val), buf); } else { zend_string *zs = zval_get_string(val); @@ -890,8 +898,8 @@ static void php_http_env_response_stream_header(php_http_env_response_stream_ctx ctx->chunked = 0; } } - php_stream_write(ctx->stream, zs->val, zs->len); - php_stream_write_string(ctx->stream, PHP_HTTP_CRLF); + php_http_buffer_append(buf, zs->val, zs->len); + php_http_buffer_appends(buf, PHP_HTTP_CRLF); zend_string_release(zs); } } @@ -899,11 +907,14 @@ static void php_http_env_response_stream_header(php_http_env_response_stream_ctx } static ZEND_RESULT_CODE php_http_env_response_stream_start(php_http_env_response_stream_ctx_t *ctx) { + php_http_buffer_t header_buf; + if (ctx->started || ctx->finished) { return FAILURE; } - php_stream_printf(ctx->stream, "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_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) { @@ -914,18 +925,27 @@ static ZEND_RESULT_CODE php_http_env_response_stream_start(php_http_env_response ctx->chunked = 0; } - php_http_env_response_stream_header(ctx, &ctx->header); + php_http_env_response_stream_header(ctx, &ctx->header, &header_buf); /* enable chunked transfer encoding */ if (ctx->chunked) { - php_stream_write_string(ctx->stream, "Transfer-Encoding: chunked" PHP_HTTP_CRLF); + php_http_buffer_appends(&header_buf, "Transfer-Encoding: chunked" PHP_HTTP_CRLF); } - php_stream_write_string(ctx->stream, PHP_HTTP_CRLF); + php_http_buffer_appends(&header_buf, PHP_HTTP_CRLF); - ctx->started = 1; + 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); - return SUCCESS; + if (ctx->chunked) { + ctx->chunked_filter = php_stream_filter_create("http.chunked_encode", NULL, 0); + 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) { @@ -1039,18 +1059,10 @@ static ZEND_RESULT_CODE php_http_env_response_stream_write(php_http_env_response } } - 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 ZEND_RESULT_CODE php_http_env_response_stream_flush(php_http_env_response_t *r) @@ -1070,22 +1082,24 @@ static ZEND_RESULT_CODE php_http_env_response_stream_flush(php_http_env_response } 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; - if (stream_ctx->finished) { + if (ctx->finished) { return FAILURE; } - if (!stream_ctx->started) { - if (SUCCESS != php_http_env_response_stream_start(stream_ctx)) { + if (!ctx->started) { + if (SUCCESS != php_http_env_response_stream_start(ctx)) { 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); } - stream_ctx->finished = 1; + ctx->finished = 1; return SUCCESS; } @@ -1144,8 +1158,15 @@ static PHP_METHOD(HttpEnvResponse, __invoke) 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 (!obj->body) { + php_http_message_object_init_body_object(obj); + } + + if (ob_flags & PHP_OUTPUT_HANDLER_CLEAN) { + php_stream_truncate_set_size(php_http_message_body_stream(obj->message->body), 0); + } else { + php_http_message_body_append(obj->message->body, ob_str, ob_len); + } RETURN_TRUE; } } @@ -1160,7 +1181,7 @@ static PHP_METHOD(HttpEnvResponse, setEnvRequest) php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS(), "|O", &env_req, php_http_message_class_entry), invalid_arg, return); set_option(getThis(), ZEND_STRL("request"), IS_OBJECT, env_req, 0); - RETVAL_ZVAL_FAST(getThis()); + RETVAL_ZVAL(getThis(), 1, 0); } ZEND_BEGIN_ARG_INFO_EX(ai_HttpEnvResponse_setContentType, 0, 0, 1) @@ -1174,7 +1195,7 @@ static PHP_METHOD(HttpEnvResponse, setContentType) php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS(), "s!", &ct_str, &ct_len), invalid_arg, return); set_option(getThis(), ZEND_STRL("contentType"), IS_STRING, ct_str, ct_len); - RETVAL_ZVAL_FAST(getThis()); + RETVAL_ZVAL(getThis(), 1, 0); } ZEND_BEGIN_ARG_INFO_EX(ai_HttpEnvResponse_setContentDisposition, 0, 0, 1) @@ -1187,7 +1208,7 @@ static PHP_METHOD(HttpEnvResponse, setContentDisposition) php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS(), "a", &zdisposition), invalid_arg, return); zend_update_property(Z_OBJCE_P(getThis()), getThis(), ZEND_STRL("contentDisposition"), zdisposition); - RETVAL_ZVAL_FAST(getThis()); + RETVAL_ZVAL(getThis(), 1, 0); } ZEND_BEGIN_ARG_INFO_EX(ai_HttpEnvResponse_setContentEncoding, 0, 0, 1) @@ -1200,7 +1221,7 @@ static PHP_METHOD(HttpEnvResponse, setContentEncoding) php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS(), "l", &ce), invalid_arg, return); set_option(getThis(), ZEND_STRL("contentEncoding"), IS_LONG, &ce, 0); - RETVAL_ZVAL_FAST(getThis()); + RETVAL_ZVAL(getThis(), 1, 0); } ZEND_BEGIN_ARG_INFO_EX(ai_HttpEnvResponse_setCacheControl, 0, 0, 1) @@ -1214,7 +1235,7 @@ static PHP_METHOD(HttpEnvResponse, setCacheControl) php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS(), "s!", &cc_str, &cc_len), invalid_arg, return); set_option(getThis(), ZEND_STRL("cacheControl"), IS_STRING, cc_str, cc_len); - RETVAL_ZVAL_FAST(getThis()); + RETVAL_ZVAL(getThis(), 1, 0); } ZEND_BEGIN_ARG_INFO_EX(ai_HttpEnvResponse_setLastModified, 0, 0, 1) @@ -1227,7 +1248,7 @@ static PHP_METHOD(HttpEnvResponse, setLastModified) php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS(), "l", &last_modified), invalid_arg, return); set_option(getThis(), ZEND_STRL("lastModified"), IS_LONG, &last_modified, 0); - RETVAL_ZVAL_FAST(getThis()); + RETVAL_ZVAL(getThis(), 1, 0); } ZEND_BEGIN_ARG_INFO_EX(ai_HttpEnvResponse_isCachedByLastModified, 0, 0, 0) @@ -1259,7 +1280,7 @@ static PHP_METHOD(HttpEnvResponse, setEtag) php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS(), "s!", &etag_str, &etag_len), invalid_arg, return); set_option(getThis(), ZEND_STRL("etag"), IS_STRING, etag_str, etag_len); - RETVAL_ZVAL_FAST(getThis()); + RETVAL_ZVAL(getThis(), 1, 0); } ZEND_BEGIN_ARG_INFO_EX(ai_HttpEnvResponse_isCachedByEtag, 0, 0, 0) @@ -1270,7 +1291,7 @@ static PHP_METHOD(HttpEnvResponse, isCachedByEtag) char *header_name_str = NULL; size_t header_name_len = 0; - if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s!", &header_name_str, &header_name_len)) { + if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS(), "|s!", &header_name_str, &header_name_len)) { if (!header_name_str || !header_name_len) { header_name_str = "If-None-Match"; header_name_len = lenof("If-None-Match"); @@ -1292,7 +1313,7 @@ static PHP_METHOD(HttpEnvResponse, setThrottleRate) set_option(getThis(), ZEND_STRL("throttleDelay"), IS_DOUBLE, &delay, 0); set_option(getThis(), ZEND_STRL("throttleChunk"), IS_LONG, &chunk_size, 0); - RETVAL_ZVAL_FAST(getThis()); + RETVAL_ZVAL(getThis(), 1, 0); } ZEND_BEGIN_ARG_INFO_EX(ai_HttpEnvResponse_setCookie, 0, 0, 1) @@ -1318,7 +1339,7 @@ static PHP_METHOD(HttpEnvResponse, setCookie) case IS_ARRAY: list = php_http_cookie_list_from_struct(NULL, zcookie_new); zcookie_new = &tmp; - ZVAL_OBJECT(zcookie_new, &php_http_cookie_object_new_ex(php_http_cookie_class_entry, list)->zo, 1); + ZVAL_OBJECT(zcookie_new, &php_http_cookie_object_new_ex(php_http_cookie_class_entry, list)->zo, 0); break; default: @@ -1326,14 +1347,14 @@ static PHP_METHOD(HttpEnvResponse, setCookie) list = php_http_cookie_list_parse(NULL, zs->val, zs->len, 0, NULL); zend_string_release(zs); zcookie_new = &tmp; - ZVAL_OBJECT(zcookie_new, &php_http_cookie_object_new_ex(php_http_cookie_class_entry, list)->zo, 1); + ZVAL_OBJECT(zcookie_new, &php_http_cookie_object_new_ex(php_http_cookie_class_entry, list)->zo, 0); } zend_restore_error_handling(&zeh); set_cookie(getThis(), zcookie_new); zval_ptr_dtor(zcookie_new); - RETVAL_ZVAL_FAST(getThis()); + RETVAL_ZVAL(getThis(), 1, 0); } ZEND_BEGIN_ARG_INFO_EX(ai_HttpEnvResponse_send, 0, 0, 0)