projects
/
m6w6
/
ext-http
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
|
github
raw
|
inline
| side by side
add recent curl options
[m6w6/ext-http]
/
php_http_env_response.c
diff --git
a/php_http_env_response.c
b/php_http_env_response.c
index a4431b60af08c9be636272ea6110220aaae6ae78..20a31eb90fea41b0ad73aa22c9e8011867dc1c95 100644
(file)
--- a/
php_http_env_response.c
+++ b/
php_http_env_response.c
@@
-228,9
+228,10
@@
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)
{
static zend_bool php_http_env_response_is_cacheable(php_http_env_response_t *r, php_http_message_t *request)
{
+ long status = r->ops->get_status(r);
TSRMLS_FETCH_FROM_CTX(r->ts);
TSRMLS_FETCH_FROM_CTX(r->ts);
- if (
r->ops->get_status(r) >= 400
) {
+ if (
status && status / 100 != 2
) {
return 0;
}
return 0;
}
@@
-263,7
+264,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)
}
#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);
{
size_t chunks_sent, chunk = r->throttle.chunk ? r->throttle.chunk : PHP_HTTP_SENDBUF_SIZE;
TSRMLS_FETCH_FROM_CTX(r->ts);
@@
-351,9
+352,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);
zval *zoption, *options = r->options;
TSRMLS_FETCH_FROM_CTX(r->ts);
@@
-608,9
+609,9
@@
static STATUS php_http_env_response_send_head(php_http_env_response_t *r, php_ht
return ret;
}
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);
zval *zoption;
php_http_message_body_t *body;
TSRMLS_FETCH_FROM_CTX(r->ts);
@@
-696,7
+697,7
@@
static STATUS php_http_env_response_send_body(php_http_env_response_t *r)
return ret;
}
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;
{
php_http_message_t *request;
php_http_message_body_t *body;
@@
-784,21
+785,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);
}
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);
}
{
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);
}
{
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);
va_list args;
TSRMLS_FETCH_FROM_CTX(r->ts);
@@
-808,9
+809,9
@@
static STATUS php_http_env_response_sapi_set_header(php_http_env_response_t *r,
return ret;
}
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);
va_list args;
TSRMLS_FETCH_FROM_CTX(r->ts);
@@
-820,13
+821,13
@@
static STATUS php_http_env_response_sapi_add_header(php_http_env_response_t *r,
return ret;
}
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);
}
{
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);
{
TSRMLS_FETCH_FROM_CTX(r->ts);
@@
-835,7
+836,7
@@
static STATUS php_http_env_response_sapi_write(php_http_env_response_t *r, const
}
return FAILURE;
}
}
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);
{
TSRMLS_FETCH_FROM_CTX(r->ts);
@@
-853,7
+854,7
@@
static STATUS php_http_env_response_sapi_flush(php_http_env_response_t *r)
return SUCCESS;
}
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;
}
{
return SUCCESS;
}
@@
-883,6
+884,7
@@
typedef struct php_http_env_response_stream_ctx {
long status_code;
php_stream *stream;
long status_code;
php_stream *stream;
+ php_stream_filter *chunked_filter;
php_http_message_t *request;
unsigned started:1;
php_http_message_t *request;
unsigned started:1;
@@
-890,18
+892,20
@@
typedef struct php_http_env_response_stream_ctx {
unsigned chunked:1;
} php_http_env_response_stream_ctx_t;
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;
{
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;
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;
}
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;
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;
@@
-922,19
+926,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);
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;
}
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) {
{
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);
} else {
zval *tmp = php_http_ztyp(IS_STRING, *val);
@@
-944,19
+951,22
@@
static void php_http_env_response_stream_header(php_http_env_response_stream_ctx
ctx->chunked = 0;
}
}
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);
}
}
}
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;
}
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_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) {
/* 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) {
@@
-967,17
+977,26
@@
static STATUS php_http_env_response_stream_start(php_http_env_response_stream_ct
ctx->chunked = 0;
}
ctx->chunked = 0;
}
- php_http_env_response_stream_header(ctx, &ctx->header TSRMLS_CC);
+ php_http_env_response_stream_header(ctx, &ctx->header
, &header_buf
TSRMLS_CC);
/* enable chunked transfer encoding */
if (ctx->chunked) {
/* 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 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)
{
}
static long php_http_env_response_stream_get_status(php_http_env_response_t *r)
{
@@
-985,7
+1004,7
@@
static long php_http_env_response_stream_get_status(php_http_env_response_t *r)
return ctx->status_code;
}
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;
{
php_http_env_response_stream_ctx_t *stream_ctx = r->ctx;
@@
-997,7
+1016,7
@@
static STATUS php_http_env_response_stream_set_status(php_http_env_response_t *r
return SUCCESS;
}
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;
{
php_http_env_response_stream_ctx_t *stream_ctx = r->ctx;
@@
-1009,7
+1028,7
@@
static STATUS php_http_env_response_stream_set_protocol_version(php_http_env_res
return SUCCESS;
}
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;
{
php_http_env_response_stream_ctx_t *stream_ctx = r->ctx;
char *header_end, *header_str = NULL;
@@
-1046,9
+1065,9
@@
static STATUS php_http_env_response_stream_set_header_ex(php_http_env_response_t
return SUCCESS;
}
}
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);
va_list argv;
va_start(argv, fmt);
@@
-1057,9
+1076,9
@@
static STATUS php_http_env_response_stream_set_header(php_http_env_response_t *r
return ret;
}
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);
va_list argv;
va_start(argv, fmt);
@@
-1068,7
+1087,7
@@
static STATUS php_http_env_response_stream_add_header(php_http_env_response_t *r
return ret;
}
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;
{
php_http_env_response_stream_ctx_t *stream_ctx = r->ctx;
@@
-1079,7
+1098,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;
}
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);
{
php_http_env_response_stream_ctx_t *stream_ctx = r->ctx;
TSRMLS_FETCH_FROM_CTX(r->ts);
@@
-1093,21
+1112,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 (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;
}
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);
{
php_http_env_response_stream_ctx_t *stream_ctx = r->ctx;
TSRMLS_FETCH_FROM_CTX(r->ts);
@@
-1123,25
+1134,27
@@
static STATUS php_http_env_response_stream_flush(php_http_env_response_t *r)
return php_stream_flush(stream_ctx->stream);
}
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);
TSRMLS_FETCH_FROM_CTX(r->ts);
- if (
stream_
ctx->finished) {
+ if (ctx->finished) {
return FAILURE;
}
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;
}
}
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;
}
return SUCCESS;
}
@@
-1200,9
+1213,14
@@
static PHP_METHOD(HttpEnvResponse, __invoke)
PHP_HTTP_ENV_RESPONSE_OBJECT_INIT(obj);
PHP_HTTP_ENV_RESPONSE_OBJECT_INIT(obj);
- php_http_message_object_init_body_object(obj);
+ if (!obj->body) {
+ 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
php_http_message_body_append(obj->message->body, ob_str, ob_len);
#if PHP_VERSION_ID >= 50400
+ if (ob_flags & PHP_OUTPUT_HANDLER_CLEAN) {
+ php_stream_truncate_set_size(php_http_message_body_stream(obj->message->body), 0);
+ }
RETURN_TRUE;
#else
RETURN_EMPTY_STRING();
RETURN_TRUE;
#else
RETURN_EMPTY_STRING();